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, "\n");
36 void array_contents(unsigned long arr[], unsigned long size){
37 fprintf(stdout, "[%li", arr[0]);
38 for(unsigned long i = 1; i < size; i++){
39 fprintf(stdout, ", %li", arr[i]);
41 fprintf(stdout, "]\n");
45 int main(int argc, char *argv[]){
50 unsigned long blocksize;
51 unsigned long *databuf;
61 unsigned long size = 0;
62 char *filename = "numlist.bin";
64 /* store out name for usage(); */
69 char name[MPI_MAX_PROCESSOR_NAME];
70 MPI_Init(&argc,&argv);
71 MPI_Comm_size(MPI_COMM_WORLD,&nodes);
72 MPI_Comm_rank(MPI_COMM_WORLD,&rank);
73 MPI_Get_processor_name(name,&tmp);
75 fprintf(stderr, "***** BIG RED WARNING: COMPILED WITH XDEBUG - YOU ARE ON YOUR OWN! *****\n");
78 fprintf(stderr, "*** COMPILED WITH DEBUG - size is limited to 64 ***\n");
79 fprintf(stderr, "Compile with XDEBUG or without DEBUG to get past this limit.\n");
80 fprintf(stdout, "[%d/%d:%s] openMPI initialised.\n",rank,nodes,name);
86 while ((c = getopt (argc, argv, "n:f:")) != -1) switch (c){
88 size = strtoul (optarg,NULL,0);
94 if (optopt == 'f' || optopt == 'n')
95 fprintf (stderr, "Option -%c requires an argument.\n", optopt);
96 else if (isprint (optopt))
97 fprintf (stderr, "Unknown option `-%c'.\n", optopt);
99 fprintf (stderr, "Unknown option character `\\x%x'.\n", optopt);
100 if (rank == 0)usage();
105 if (rank == 0)usage();
110 /* sanize size, calculate blocksize, init databuf. */
111 if ((file = fopen(filename, "r"))) {
116 if (size <= 0 || size > st.st_size){
121 if (size > 64) size = 64;
126 fprintf (stdout, "[%d/%d:%s] File %s does not exist.\n",rank,nodes,name,filename);
133 databuf = malloc(size * sizeof(unsigned long));
134 if(databuf == NULL) {
135 fprintf(stdout, "[%d/%d:%s] malloc for databuf failed.\n",rank,nodes,name);
140 blocksize = size/nodes;
141 if(blocksize*nodes < size){
144 fprintf(stdout, "[%d/%d:%s] perfect split impossible: n:%d, s:%li -> bs:%li (off: %li)\n",rank,nodes,name,nodes,size,blocksize,(nodes*blocksize)-size);
148 fprintf(stdout, "[%d/%d:%s] 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));
151 if ((file = fopen(filename, "r"))) {
152 for (unsigned long i = 0; i < size; i++) {
153 databuf[i] = fgetc(file);
157 fprintf (stdout, "[%d/%d:%s] File %s could not be read.\n",rank,nodes,name,filename);
163 fprintf(stdout, "[%d/%d:%s] file read - distributing work.\n",rank,nodes,name);
164 startTime = MPI_Wtime();
165 MPI_Isend(databuf, size-1, MPI_LONG, 0, KEY, MPI_COMM_WORLD, &request);
168 /* stuff done by all nodes */
169 if (startTime == 0) startTime = MPI_Wtime();
172 MPI_Recv(databuf, size, MPI_LONG, tmp, KEY, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
174 /* propagate data, if not last node. */
175 if(rank +1 < nodes) MPI_Send(databuf, size, MPI_LONG, rank+1, KEY, MPI_COMM_WORLD);
176 prepTime = MPI_Wtime();
178 /* do processing here. */
179 for(unsigned long i = (rank*blocksize+1); i < ((rank+1)*blocksize) && i < size; i++){
180 databuf[i] = databuf[(i-1)] + databuf[i];
182 algoTime = MPI_Wtime();
184 fprintf(stdout, "[%d/%d:%s] proc array ",rank,nodes,name);
185 array_contents(databuf, size);
191 MPI_Recv(databuf, rank*blocksize, MPI_LONG, tmp, KEY, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
193 fprintf(stdout, "[%d/%d:%s] rcv array ",rank,nodes,name);
194 array_contents(databuf, size);
196 for(unsigned long i = rank*blocksize; i < ((rank+1)*blocksize) && i < size; i++){
197 databuf[i] += databuf[((rank*blocksize)-1)];
200 fprintf(stdout, "[%d/%d:%s] added array ",rank,nodes,name);
201 array_contents(databuf, size);
204 postpTime = MPI_Wtime();
207 if(tmp >= nodes) tmp = 0;
208 tmp2 = ((rank+1)*blocksize);
209 if(tmp2 > size) tmp2 = size;
210 MPI_Send(databuf, tmp2, MPI_LONG, tmp, KEY, MPI_COMM_WORLD);
212 /* receive result by root */
214 MPI_Recv(databuf, size, MPI_LONG, nodes-1, KEY, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
215 endTime = MPI_Wtime();
216 fprintf(stdout, "[%d/%d:%s] result: %li\n",rank,nodes,name,databuf[size-1]);
218 fprintf(stdout, "[%d/%d:%s] res array ",rank,nodes,name);
219 array_contents(databuf, size);
223 if(endTime == 0) endTime = MPI_Wtime();
224 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);