/* Jonathan Hauenstein - University of Notre Dame Center for Applied Mathematics Minicourse on Parallel Computing April 24, May 1, May 8, 2008 Sample programs are designed for educational use */ #include #include #include int main(int argc, char **argv) { int my_id, num_proc, flag; MPI_Status status; // initialize MPI MPI_Init(&argc, &argv); // find unique id MPI_Comm_rank(MPI_COMM_WORLD, &my_id); // find the number of processes running MPI_Comm_size(MPI_COMM_WORLD, &num_proc); // test for message waiting MPI_Iprobe(MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &flag, &status); printf("id: %d flag: %d\n", my_id, flag); // wait here MPI_Barrier(MPI_COMM_WORLD); // send from id 0 to id 1 if (my_id == 0) { // construct the things to send int i, size = 96, tag = 101; int *my_values = (int *)malloc(size * sizeof(int)); for (i = 0; i < size; i++) my_values[i] = i; MPI_Send(my_values, size, MPI_INT, 1, tag, MPI_COMM_WORLD); free(my_values); } else if (my_id == 1) { // setup a recv buffer int recv_size, *my_values = NULL; MPI_Probe(MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &status); MPI_Get_count(&status, MPI_INT, &recv_size); printf("\nprobe values: recv size: %d MPI_SOURCE: %d MPI_TAG: %d MPI_ERROR: %d\n\n", recv_size, status.MPI_SOURCE, status.MPI_TAG, status.MPI_ERROR); my_values = (int *)malloc(recv_size * sizeof(int)); MPI_Recv(my_values, recv_size, MPI_INT, MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &status); free(my_values); } // finalize MPI MPI_Finalize(); return 0; }