]> git.somenet.org - pub/astra/parallel.git/blob - scan/scan.c
i can haz graphs plz?
[pub/astra/parallel.git] / scan / scan.c
1 /*
2  * Inclusive scan
3  */
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <ctype.h>
8 #include <getopt.h>
9 #include <unistd.h>
10 #include <sys/stat.h>
11 #include <mpi.h>
12 #include "scan.h"
13
14 // tag for control messages
15 #define KEY 1337 
16
17 /* This one's binary name. */
18 char* binname = "unset";
19
20 void usage () {
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");
24 #ifdef DEBUG
25         fprintf(stderr, "***** BIG RED WARNING: COMPILED WITH XDEBUG - YOU ARE ON YOUR OWN! *****\n");
26 #endif
27 #ifdef DEBUG
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");
31 #endif
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");
34 }
35
36
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]);
41         }
42         fprintf(stdout, "]\n");
43 }
44
45
46 int main(int argc, char *argv[]){
47         int tmp;
48         int tmp2;
49         FILE *file = NULL;
50         MPI_Request request;
51         unsigned long blocksize;
52         unsigned long *databuf;
53
54         /* timings. */
55         double startTime = 0;
56         double prepTime = 0;
57         double algoTime = 0;
58         double postpTime = 0;
59         double endTime = 0;
60
61         /* options */
62         unsigned long size = 0;
63         char *filename = "numlist.bin";
64
65         /* store out name for usage(); */
66         binname = argv[0];
67
68         /* node info stuff*/
69         int rank, nodes;
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);
75 #ifdef DEBUG
76         fprintf(stderr, "***** BIG RED WARNING: COMPILED WITH XDEBUG - YOU ARE ON YOUR OWN! *****\n");
77 #endif
78 #ifdef DEBUG
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);
82 #endif
83
84         /* getopt stuff */
85         int c;
86         opterr = 0;
87         while ((c = getopt (argc, argv, "n:f:")) != -1) switch (c){
88                 case 'n':
89                         size = strtoul (optarg,NULL,0);
90                         break;
91                 case 'f':
92                         filename = optarg;
93                         break;
94                 case '?':
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);
99                         else
100                                 fprintf (stderr, "Unknown option character `\\x%x'.\n", optopt);
101                         if (rank == 0)usage();
102                         MPI_Finalize();
103                         exit(2);
104                         
105                 default:
106                         if (rank == 0)usage();
107                         MPI_Finalize();
108                         exit(2);
109         }
110
111         /* sanize size, calculate blocksize, init databuf. */
112         if ((file = fopen(filename, "r"))) {
113                 fclose(file);
114                 file = NULL;
115                 struct stat st;
116                 stat(filename, &st);
117
118                 if (size == 0){
119                         size = st.st_size;
120                 }
121 #ifdef DEBUG
122 #ifndef XDEBUG
123                 if (size > 64) size = 64;
124 #endif
125 #endif
126                 if (size < 0 || size > st.st_size){
127                         if(rank == 0){
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);
129                                 usage();
130                         }
131                         MPI_Finalize();
132                         exit(3);
133                 }
134         }else{
135                 if(rank == 0){
136                         fprintf (stderr, "[%d/%d:%s] File %s does not exist.\n",rank,nodes,name,filename);
137                         usage();
138                 }
139                 MPI_Finalize();
140                 exit(3);
141         }
142
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);
146                 MPI_Finalize();
147                 exit(4);
148         }
149
150         blocksize = size/nodes;
151         if(blocksize*nodes < size){
152                 blocksize++;
153                 if(rank == 0)
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);
155         }
156
157         if(rank == 0){
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));
159
160                 /* read file. */
161                 if ((file = fopen(filename, "r"))) {
162                         for (unsigned long i = 0; i < size; i++) {
163                                 databuf[i] = fgetc(file);
164                         }
165                         fclose(file);
166                 }else{
167                         fprintf (stderr, "[%d/%d:%s] File %s could not be read.\n",rank,nodes,name,filename);
168                         usage();
169                         MPI_Finalize();
170                         exit(3);
171                 }
172 #ifdef DEBUG
173                 fprintf(stdout, "[%d/%d:%s] file read - distributing work.\n",rank,nodes,name);
174 #endif
175                 startTime = MPI_Wtime();
176                 MPI_Isend(databuf, size-1, MPI_LONG, 0, KEY, MPI_COMM_WORLD, &request);
177         }
178
179         /* receive work and propagate to next node, if not last node. */
180         if (startTime == 0) startTime = MPI_Wtime();
181         tmp = rank -1;
182         if(tmp < 0) tmp = 0;
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();
186
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];
190         }
191         algoTime = MPI_Wtime();
192 #ifdef DEBUG
193         fprintf(stdout, "[%d/%d:%s] proc array ",rank,nodes,name);
194         array_contents(databuf, size);
195 #endif
196
197
198         /* receive sums */
199         if(rank != 0){
200                 MPI_Recv(databuf, rank*blocksize, MPI_LONG, tmp, KEY, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
201 #ifdef DEBUG
202                 fprintf(stdout, "[%d/%d:%s] rcv array ",rank,nodes,name);
203                 array_contents(databuf, size);
204 #endif
205                 for(unsigned long i = rank*blocksize; i < ((rank+1)*blocksize) && i < size; i++){
206                         databuf[i] += databuf[((rank*blocksize)-1)];
207                 }
208 #ifdef DEBUG
209                 fprintf(stdout, "[%d/%d:%s] added array ",rank,nodes,name);
210                 array_contents(databuf, size);
211 #endif
212         }
213         postpTime = MPI_Wtime();
214
215         tmp = rank +1;
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);
220
221         /* receive result by root */
222         if(rank == 0){
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);
227 #ifdef DEBUG
228                 fprintf(stdout, "[%d/%d:%s] res array ",rank,nodes,name);
229                 array_contents(databuf, size);
230 #endif
231         }
232
233 #ifdef DEBUG
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);
236 #endif
237         MPI_Finalize();
238         return 0;
239 }
240