13 // tag for control messages
16 void array_contents ( unsigned long arr[], unsigned long size ) {
17 fprintf (stdout, "[%li", arr[0]);
18 for (unsigned long i = 1; i < size; i++) {
19 fprintf (stdout, ", %li", arr[i]);
21 fprintf (stdout, "]\n");
25 int main(int argc, char *argv[]){
31 char name[MPI_MAX_PROCESSOR_NAME];
32 MPI_Init(&argc,&argv);
33 MPI_Comm_size(MPI_COMM_WORLD,&nodes);
34 MPI_Comm_rank(MPI_COMM_WORLD,&rank);
35 MPI_Get_processor_name(name,&tmp);
36 fprintf(stdout, "[%d/%d:%s] openMPI initialised.\n",rank,nodes,name);
39 unsigned long size = 18;
40 unsigned long blocksize = size/nodes;
41 if(blocksize*nodes < size){
43 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);
46 unsigned long databuf[size];
52 /* stuff done by root */
54 fprintf(stdout, "[%d/%d:%s] I am root. Size: %li nodes: %d blocksize: %li (n*b: %li) (n*b+1: %li)- reading data...\n",rank,nodes,name,size,nodes,blocksize,nodes*blocksize, nodes*(blocksize+1));
55 for (unsigned long i = 0; i < size; i++) {
58 MPI_Isend(&databuf, size-1, MPI_LONG, 0, KEY, MPI_COMM_WORLD, &request);
61 // MPI_Recv(&databuf,1,MPI_INT,nodes-1,KEY,MPI_COMM_WORLD,MPI_STATUS_IGNORE);
62 /* stuff done by all others */
65 MPI_Recv(&databuf, size, MPI_LONG, tmp, KEY, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
67 /* propagate data, if not last node. */
68 if(rank +1 < nodes) MPI_Send(&databuf, size, MPI_LONG, rank+1, KEY, MPI_COMM_WORLD);
70 /* do processing here. */
71 for (unsigned long i = (rank*blocksize+1); i < ((rank+1)*blocksize) && i < size; i++) {
72 databuf[i] = databuf[(i-1)] + databuf[i];
74 fprintf(stdout, "[%d/%d:%s] proc array ",rank,nodes,name);
75 array_contents(databuf, size);
80 MPI_Recv(&databuf, rank*blocksize, MPI_LONG, tmp, KEY, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
81 fprintf(stdout, "[%d/%d:%s] rcv array ",rank,nodes,name);
82 array_contents(databuf, size);
83 for (unsigned long i = rank*blocksize; i < ((rank+1)*blocksize); i++) {
84 databuf[i] += databuf[((rank*blocksize)-1)];
86 fprintf(stdout, "[%d/%d:%s] added array ",rank,nodes,name);
87 array_contents(databuf, size);
91 if (tmp >= nodes)tmp = 0;
92 tmp2 = ((rank+1)*blocksize);
93 if (tmp2 > size)tmp2 = size;
94 MPI_Send(&databuf, tmp2, MPI_LONG, tmp, KEY, MPI_COMM_WORLD);
96 /* receive result by root */
98 MPI_Recv(&databuf, size, MPI_LONG, nodes-1, KEY, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
99 fprintf(stdout, "[%d/%d:%s] I AM ROOT! got data from %d.\n",rank,nodes,name,nodes-1);
100 fprintf(stdout, "[%d/%d:%s] result array ",rank,nodes,name);
101 array_contents(databuf, size);