14 // tag for control messages
17 /* This one's binary name. */
18 char* binname = "unset";
21 fprintf(stderr, "\nUsage: mpirun -node 1-32 -nnp 1 %s [-f <string>] [-n <number>]\n", binname);
22 fprintf(stderr, "\t -f: set the filename of the randfile. (defaults to \"numlist.bin\")\n");
23 fprintf(stderr, "\t -n: set the number count to read from randfile. (defaults to 0 = read all)\n");
25 fprintf(stderr, "***** BIG RED WARNING: COMPILED WITH XDEBUG - YOU ARE ON YOUR OWN! *****\n");
28 fprintf(stderr, "*** COMPILED WITH DEBUG - size is limited to 64 ***\n");
29 fprintf(stderr, "Compile with XDEBUG or without DEBUG to get past this limit.\n");
30 fprintf(stderr, "You might want to compile this without the DEBUG flag to get less noice.\n");
32 fprintf(stderr, "\nErrorcodes:\n");
33 fprintf(stderr, "\t0: Everything went OK.\n\t1: General error.\n\t2: Getopt/wrong usage error.\n\t3: Inputfile error.\n\t4: Memory error.\n\n");
37 void array_contents(unsigned long arr[], unsigned long size){
38 fprintf(stdout, "[%li", arr[0]);
39 for(unsigned long i = 1; i < size; i++){
40 fprintf(stdout, ", %li", arr[i]);
42 fprintf(stdout, "]\n");
46 int main(int argc, char *argv[]){
51 unsigned long blocksize;
52 unsigned long *databuf;
62 unsigned long size = 0;
63 char *filename = "numlist.bin";
65 /* store out name for usage(); */
70 char name[MPI_MAX_PROCESSOR_NAME];
71 MPI_Init(&argc,&argv);
72 MPI_Comm_size(MPI_COMM_WORLD,&nodes);
73 MPI_Comm_rank(MPI_COMM_WORLD,&rank);
74 MPI_Get_processor_name(name,&tmp);
76 fprintf(stderr, "***** BIG RED WARNING: COMPILED WITH XDEBUG - YOU ARE ON YOUR OWN! *****\n");
79 fprintf(stderr, "*** COMPILED WITH DEBUG - size is limited to 64 ***\n");
80 fprintf(stderr, "Compile with XDEBUG or without DEBUG to get past this limit.\n");
81 fprintf(stdout, "[%d/%d:%s] openMPI initialised.\n",rank,nodes,name);
87 while ((c = getopt (argc, argv, "n:f:")) != -1) switch (c){
89 size = strtoul (optarg,NULL,0);
95 if (optopt == 'f' || optopt == 'n')
96 fprintf (stderr, "Option -%c requires an argument.\n", optopt);
97 else if (isprint (optopt))
98 fprintf (stderr, "Unknown option `-%c'.\n", optopt);
100 fprintf (stderr, "Unknown option character `\\x%x'.\n", optopt);
101 if (rank == 0)usage();
106 if (rank == 0)usage();
111 /* sanize size, calculate blocksize, init databuf. */
112 if ((file = fopen(filename, "r"))) {
123 if (size > 64) size = 64;
126 if (size < 0 || size > st.st_size){
128 fprintf (stderr, "[%d/%d:%s] Cannot read %li numbers from \"%s\" - only has %li bytes.\n",rank,nodes,name,size,filename,st.st_size);
136 fprintf (stderr, "[%d/%d:%s] File %s does not exist.\n",rank,nodes,name,filename);
143 databuf = malloc(size * sizeof(unsigned long));
144 if(databuf == NULL) {
145 fprintf(stdout, "[%d/%d:%s] malloc for databuf failed.\n",rank,nodes,name);
150 blocksize = size/nodes;
151 if(blocksize*nodes < size){
154 fprintf(stderr, "[%d/%d:%s] perfect split impossible: n:%d, s:%li -> bs:%li (off: %li)\n",rank,nodes,name,nodes,size,blocksize,(nodes*blocksize)-size);
158 fprintf(stdout, "[%d/%d:%s] INFO s: %li n: %d bs: %li n*b: %li n*(b+1): %li\n",rank,nodes,name,size,nodes,blocksize,nodes*blocksize, nodes*(blocksize+1));
161 if ((file = fopen(filename, "r"))) {
162 for (unsigned long i = 0; i < size; i++) {
163 databuf[i] = fgetc(file);
167 fprintf (stderr, "[%d/%d:%s] File %s could not be read.\n",rank,nodes,name,filename);
173 fprintf(stdout, "[%d/%d:%s] file read - distributing work.\n",rank,nodes,name);
175 startTime = MPI_Wtime();
176 MPI_Isend(databuf, size-1, MPI_LONG, 0, KEY, MPI_COMM_WORLD, &request);
179 /* receive work and propagate to next node, if not last node. */
180 if (startTime == 0) startTime = MPI_Wtime();
183 MPI_Recv(databuf, size, MPI_LONG, tmp, KEY, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
184 if(rank +1 < nodes) MPI_Send(databuf, size, MPI_LONG, rank+1, KEY, MPI_COMM_WORLD);
185 prepTime = MPI_Wtime();
187 /* do actual work here. */
188 for(unsigned long i = (rank*blocksize+1); i < ((rank+1)*blocksize) && i < size; i++){
189 databuf[i] = databuf[(i-1)] + databuf[i];
191 algoTime = MPI_Wtime();
193 fprintf(stdout, "[%d/%d:%s] proc array ",rank,nodes,name);
194 array_contents(databuf, size);
200 MPI_Recv(databuf, rank*blocksize, MPI_LONG, tmp, KEY, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
202 fprintf(stdout, "[%d/%d:%s] rcv array ",rank,nodes,name);
203 array_contents(databuf, size);
205 for(unsigned long i = rank*blocksize; i < ((rank+1)*blocksize) && i < size; i++){
206 databuf[i] += databuf[((rank*blocksize)-1)];
209 fprintf(stdout, "[%d/%d:%s] added array ",rank,nodes,name);
210 array_contents(databuf, size);
213 postpTime = MPI_Wtime();
216 if(tmp >= nodes) tmp = 0;
217 tmp2 = ((rank+1)*blocksize);
218 if(tmp2 > size) tmp2 = size;
219 MPI_Send(databuf, tmp2, MPI_LONG, tmp, KEY, MPI_COMM_WORLD);
221 /* receive result by root */
223 MPI_Recv(databuf, size, MPI_LONG, nodes-1, KEY, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
224 endTime = MPI_Wtime();
225 fprintf(stdout, "[%d/%d:%s] result: %li\n",rank,nodes,name,databuf[size-1]);
226 fprintf(stdout, "[%d/%d:%s] timings: prep:%f algo:%f postp:%f end:%f\n",rank,nodes,name,prepTime-startTime,algoTime-startTime,postpTime-startTime,endTime-startTime);
228 fprintf(stdout, "[%d/%d:%s] res array ",rank,nodes,name);
229 array_contents(databuf, size);
234 if(endTime == 0) endTime = MPI_Wtime();
235 fprintf(stdout, "[%d/%d:%s] timings: prep:%f algo:%f postp:%f end:%f\n",rank,nodes,name,prepTime-startTime,algoTime-startTime,postpTime-startTime,endTime-startTime);