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 -1 = read all)\n\n");
27 void array_contents(unsigned long arr[], unsigned long size){
28 fprintf(stdout, "[%li", arr[0]);
29 for(unsigned long i = 1; i < size; i++){
30 fprintf(stdout, ", %li", arr[i]);
32 fprintf(stdout, "]\n");
36 int main(int argc, char *argv[]){
41 unsigned long blocksize;
42 unsigned long *databuf;
45 unsigned long size = 0;
46 char *filename = "numlist.bin";
48 /* store out name for usage(); */
53 char name[MPI_MAX_PROCESSOR_NAME];
54 MPI_Init(&argc,&argv);
55 MPI_Comm_size(MPI_COMM_WORLD,&nodes);
56 MPI_Comm_rank(MPI_COMM_WORLD,&rank);
57 MPI_Get_processor_name(name,&tmp);
58 //fprintf(stdout, "[%d/%d:%s] openMPI initialised.\n",rank,nodes,name);
63 while ((c = getopt (argc, argv, "n:f:")) != -1) switch (c){
65 size = strtoul (optarg,NULL,0);
71 if (optopt == 'f' || optopt == 'n')
72 fprintf (stderr, "Option -%c requires an argument.\n", optopt);
73 else if (isprint (optopt))
74 fprintf (stderr, "Unknown option `-%c'.\n", optopt);
76 fprintf (stderr, "Unknown option character `\\x%x'.\n", optopt);
77 if (rank == 0)usage();
82 if (rank == 0)usage();
87 /* sanize size, calculate blocksize, init databuf. */
88 if ((file = fopen(filename, "r"))) {
93 if (size <= 0 || size > st.st_size){
98 fprintf (stdout, "[%d/%d:%s] File %s does not exist.\n",rank,nodes,name,filename);
105 databuf = malloc(size * sizeof(unsigned long));
106 if(databuf == NULL) {
107 fprintf(stdout, "[%d/%d:%s] malloc for databuf failed.\n",rank,nodes,name);
112 blocksize = size/nodes;
113 if(blocksize*nodes < size){
116 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);
120 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));
123 if ((file = fopen(filename, "r"))) {
124 for (unsigned long i = 0; i < size; i++) {
125 databuf[i] = fgetc(file);
129 fprintf (stdout, "[%d/%d:%s] File %s could not be read.\n",rank,nodes,name,filename);
135 fprintf(stdout, "[%d/%d:%s] file read - distributing work.\n",rank,nodes,name);
136 MPI_Isend(databuf, size-1, MPI_LONG, 0, KEY, MPI_COMM_WORLD, &request);
139 /* stuff done by all others */
142 MPI_Recv(databuf, size, MPI_LONG, tmp, KEY, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
144 /* propagate data, if not last node. */
145 if(rank +1 < nodes) MPI_Send(databuf, size, MPI_LONG, rank+1, KEY, MPI_COMM_WORLD);
147 /* do processing here. */
148 for(unsigned long i = (rank*blocksize+1); i < ((rank+1)*blocksize) && i < size; i++){
149 databuf[i] = databuf[(i-1)] + databuf[i];
151 //fprintf(stdout, "[%d/%d:%s] proc array ",rank,nodes,name);
152 //array_contents(databuf, size);
157 MPI_Recv(databuf, rank*blocksize, MPI_LONG, tmp, KEY, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
158 // fprintf(stdout, "[%d/%d:%s] rcv array ",rank,nodes,name);
159 // array_contents(databuf, size);
160 for(unsigned long i = rank*blocksize; i < ((rank+1)*blocksize) && i < size; i++){
161 databuf[i] += databuf[((rank*blocksize)-1)];
163 // fprintf(stdout, "[%d/%d:%s] added array ",rank,nodes,name);
164 // array_contents(databuf, size);
168 if(tmp >= nodes) tmp = 0;
169 tmp2 = ((rank+1)*blocksize);
170 if(tmp2 > size) tmp2 = size;
171 MPI_Send(databuf, tmp2, MPI_LONG, tmp, KEY, MPI_COMM_WORLD);
173 /* receive result by root */
175 MPI_Recv(databuf, size, MPI_LONG, nodes-1, KEY, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
176 fprintf(stdout, "[%d/%d:%s] result: %li\n",rank,nodes,name,databuf[size-1]);
177 // fprintf(stdout, "[%d/%d:%s] res array ",rank,nodes,name);
178 // array_contents(databuf, size);