// Sorting using a generalized pattern pipeline B. Wilkinson Dec 19, 2015. #include #include #include #include "suzaku.h" // Basic Suzaku macros #define D 1 // # of data items in slave data. #define P 4 // Number of processes -- this code must be run only with this number of processes, each process only handles one number int main(int argc, char *argv[]) { int i, j, p,pid; // p is actual number of processes when executing program int T = 3 * P; // number of time steps double A[D]; // data to send (D = 1). Needs to be array as generalized send is a routine not a macro double B[P][D]; // received data, from each source static double largest = 0; for (i = 0; i < D; i++) A[i] = 0; // initialize to zero for (i = 0; i < P; i++) // initialize receive so can see what received for (j = 0; j < D; j++) B[i][j] = -999; srand(1); // initialize rand() SZ_Init(p); // initialize MPI message-passing environment if (p != P) // number of processes hardcoded printf("ERROR number of processes must be %d\n",P); SZ_Parallel_begin // parallel section, all processes do this SZ_Pattern_init("pipeline",D); // set up slave interconnections SZ_Print_connection_graph(); // for checking for (i = 0; i < T; i++) { pid = SZ_Get_process_num(); // identify process if (pid ==0) { // master generates next number to sort, ends with a terminator if (i < P) A[0] = rand()% 100; // P numbers, a number between 0 and 99 else A[0] = 999; // otherwise terminator printf("Master sends %3.0f and receives %3.0f\n",A[0],B[0][0]); } else { // slaves execute compute, using B to create A. if (B[0][0] > largest) { A[0] = largest; // copy current largest into send array largest = B[0][0]; // replace largest with received number } else { A[0] = B[0][0]; // copy received number into send array } } SZ_Generalized_send(A,B); // sent compute results to connected slaves, includes master to slave, slave to master SZ_Barrier(); // wait for every process to complete } SZ_Parallel_end; // end of parallel SZ_Finalize(); return 0; }