// filename: MPIBroadcastHypercube.cpp // Broadcasting on the hypercube #include #include #include #include #include #include using namespace std; class Gray{ public: vector code(int n){ vector v; v.push_back(0); for(int i=0; i < n; i++){ int h = 1 << i; // 100000 with i zeros int len = v.size(); for(int j = len-1;j>=0;j--){ v.push_back(h+v[j]); } } return v; } }; int lowest_non_zero_bit(int order, int code){ // we take the convention from the course, that the lowest nonzero bit of 0 is 1 << i where i == "order of the gray code" if(code == 0) return order; else{ int temp = code; int i=0; while(temp % 2 == 0){ i++; temp = temp / 2; } return i; } } vector neighbours(int order, int code){ vector res; int lnz = lowest_non_zero_bit(order,code); if (lnz==0) return res; else{ for(int i=0;i reverse_lookup(vector * graycode){ int n = graycode->size(); vector res(n); for(int i=0;i toGray = g.code(order); vector fromGray = reverse_lookup(&toGray); // we build a reverse lookup table from the Gray codes of all nodes so as to be able to retrieve their actual rank in constant time if (rank==0){ int value = rand() % 1001; printf("I am process 0 and am now sending out the value %d\n",value); vector rootNeighbors = neighbours(order,0); for(int i=0;i< rootNeighbors.size();i++){ int neighbRank = fromGray[rootNeighbors[i]]; // we retrieve the actual rank of the current neighbour from its Gray code if (neighbRank) grayRank << " and I am waiting for a message from my predecessor in the binomial tree " << predecessor << endl; int received_value; MPI_Recv(&received_value,1,MPI_INT,predecessor,0,MPI_COMM_WORLD,&status); vector rootNeighbors = neighbours(order,grayRank); if (rootNeighbors.size() == 0){ cout << "I am process " << rank << " of gray code " << (bitset<8>) grayRank << " and I have no descendants, so I will stop here!" << endl; } else{ cout << "I am process " << rank << " of gray code " << (bitset<8>) grayRank << " and am now sending out the value " << received_value << " to my neighbors "<< endl; for(int i=0;i< rootNeighbors.size();i++){ int neighbRank = fromGray[rootNeighbors[i]]; // we retrieve the actual rank of the current neighbour from its Gray code if (neighbRank