/* 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 i, my_id, num_proc, size, position; double *vec = NULL; char buf[1000]; 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); if (my_id == 0) { // setup vec size = 12; vec = (double *)malloc(size * sizeof(double)); for (i = 0; i < size; i++) vec[i] = 0.5 * i; // setup buf - first put the size and then put the vec position = 0; MPI_Pack(&size, 1, MPI_INT, buf, 1000, &position, MPI_COMM_WORLD); printf("position after int: %d\n", position); MPI_Pack(vec, size, MPI_DOUBLE, buf, 1000, &position, MPI_COMM_WORLD); printf("position after adding %d doubles: %d\n", size, position); // send the data MPI_Send(buf, position, MPI_PACKED, 1, 0, MPI_COMM_WORLD); } else if (my_id == 1) { // recv the data MPI_Recv(buf, 1000, MPI_PACKED, 0, 0, MPI_COMM_WORLD, &status); // find the size position = 0; MPI_Unpack(buf, 1000, &position, &size, 1, MPI_INT, MPI_COMM_WORLD); // setup vec vec = (double *)malloc(size * sizeof(double)); MPI_Unpack(buf, 1000, &position, vec, size, MPI_DOUBLE, MPI_COMM_WORLD); printf("\nsize: %d vec[0]: %f, ..., vec[%d]: %f\n\n", size, vec[0], size - 1, vec[size - 1]); } // clear memory free(vec); // finalize MPI MPI_Finalize(); return 0; }