| 1 | /* Run the program with 2 processes: mpiexec -n 2 a.out */
|
|---|
| 2 |
|
|---|
| 3 | #include <stdio.h>
|
|---|
| 4 | #include <stdlib.h>
|
|---|
| 5 | #include <pthread.h>
|
|---|
| 6 | #include "mpi.h"
|
|---|
| 7 |
|
|---|
| 8 | int rank;
|
|---|
| 9 |
|
|---|
| 10 | void *run_test(void * arg)
|
|---|
| 11 | {
|
|---|
| 12 | MPI_Status reqstat;
|
|---|
| 13 | int i, j, x, y;
|
|---|
| 14 | int peer = rank ? 0 : 1;
|
|---|
| 15 |
|
|---|
| 16 | for (j = 0; j < 2; j++) {
|
|---|
| 17 | if (rank % 2) {
|
|---|
| 18 | for (i = 0; i < 3; i++)
|
|---|
| 19 | MPI_Send(&x, 0, MPI_CHAR, peer, 0, MPI_COMM_WORLD);
|
|---|
| 20 | for (i = 0; i < 3; i++)
|
|---|
| 21 | MPI_Recv(&y, 0, MPI_CHAR, peer, 0, MPI_COMM_WORLD, &reqstat);
|
|---|
| 22 | }
|
|---|
| 23 | else {
|
|---|
| 24 | for (i = 0; i < 3; i++)
|
|---|
| 25 | MPI_Recv(&y, 0, MPI_CHAR, peer, 0, MPI_COMM_WORLD, &reqstat);
|
|---|
| 26 | for (i = 0; i < 3; i++)
|
|---|
| 27 | MPI_Send(&x, 0, MPI_CHAR, peer, 0, MPI_COMM_WORLD);
|
|---|
| 28 | }
|
|---|
| 29 | }
|
|---|
| 30 | return 0;
|
|---|
| 31 | }
|
|---|
| 32 |
|
|---|
| 33 | int main(int argc, char ** argv)
|
|---|
| 34 | {
|
|---|
| 35 | pthread_t thread;
|
|---|
| 36 | int i, zero = 0, pmode, nprocs;
|
|---|
| 37 |
|
|---|
| 38 | MPI_Init_thread(&argc, &argv, MPI_THREAD_MULTIPLE, &pmode);
|
|---|
| 39 | if (pmode != MPI_THREAD_MULTIPLE) {
|
|---|
| 40 | fprintf(stderr, "Thread Multiple not supported by the MPI implementation\n");
|
|---|
| 41 | MPI_Abort(MPI_COMM_WORLD, -1);
|
|---|
| 42 | }
|
|---|
| 43 |
|
|---|
| 44 | MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
|
|---|
| 45 | MPI_Comm_rank(MPI_COMM_WORLD, &rank);
|
|---|
| 46 |
|
|---|
| 47 | if (nprocs != 2) {
|
|---|
| 48 | fprintf(stderr, "Need two processes\n");
|
|---|
| 49 | MPI_Abort(MPI_COMM_WORLD, -1);
|
|---|
| 50 | }
|
|---|
| 51 |
|
|---|
| 52 | pthread_create(&thread, NULL, run_test, NULL);
|
|---|
| 53 | run_test(&zero);
|
|---|
| 54 | pthread_join(thread, NULL);
|
|---|
| 55 |
|
|---|
| 56 | MPI_Finalize();
|
|---|
| 57 |
|
|---|
| 58 | return 0;
|
|---|
| 59 | }
|
|---|