| 1 | /* FILE: mpithreads_sendrecv.c
|
|---|
| 2 | *
|
|---|
| 3 | * DESCRIPTION: 2 MPI Communicators, each of which groups 2 MPI
|
|---|
| 4 | * processes, each MPI process has 1 thread. Threads communicate with
|
|---|
| 5 | * each other but no inter-communication.
|
|---|
| 6 | *
|
|---|
| 7 | * This program is a CIVL benchmark for POR of MPI programs: Threads
|
|---|
| 8 | * will communicate through different communicators are independent
|
|---|
| 9 | * with each other.
|
|---|
| 10 | *
|
|---|
| 11 | * Author: Ziqing Luo
|
|---|
| 12 | *
|
|---|
| 13 | */
|
|---|
| 14 | #include "mpi.h"
|
|---|
| 15 | #include <pthread.h>
|
|---|
| 16 | #include <stdio.h>
|
|---|
| 17 | #include <stdlib.h>
|
|---|
| 18 | #include <assert.h>
|
|---|
| 19 |
|
|---|
| 20 | typedef struct thread_info Thread_Info;
|
|---|
| 21 |
|
|---|
| 22 | struct thread_info {
|
|---|
| 23 | int tid;
|
|---|
| 24 | MPI_Comm comm;
|
|---|
| 25 | };
|
|---|
| 26 |
|
|---|
| 27 | void * thread(void * arg) {
|
|---|
| 28 | Thread_Info * info = (Thread_Info*)arg;
|
|---|
| 29 | int rank;
|
|---|
| 30 |
|
|---|
| 31 | MPI_Comm_rank(info->comm, &rank);
|
|---|
| 32 | if (rank == 0) {
|
|---|
| 33 | MPI_Send(NULL, 0, MPI_INT, 1, 0, info->comm);
|
|---|
| 34 | MPI_Recv(NULL, 0, MPI_INT, MPI_ANY_SOURCE,
|
|---|
| 35 | 0, info->comm, MPI_STATUS_IGNORE);
|
|---|
| 36 | } else {
|
|---|
| 37 | MPI_Recv(NULL, 0, MPI_INT, MPI_ANY_SOURCE,
|
|---|
| 38 | 0, info->comm, MPI_STATUS_IGNORE);
|
|---|
| 39 | MPI_Send(NULL, 0, MPI_INT, 0, 0, info->comm);
|
|---|
| 40 | }
|
|---|
| 41 | }
|
|---|
| 42 |
|
|---|
| 43 | int main() {
|
|---|
| 44 | int size;
|
|---|
| 45 | pthread_t threads[2];
|
|---|
| 46 | void * status;
|
|---|
| 47 | Thread_Info thread_infos[2];
|
|---|
| 48 | int provided;
|
|---|
| 49 |
|
|---|
| 50 | #ifdef _CIVL
|
|---|
| 51 | MPI_Init(NULL, NULL);
|
|---|
| 52 | #else
|
|---|
| 53 | MPI_Init_thread(NULL, NULL, MPI_THREAD_MULTIPLE, &provided);
|
|---|
| 54 | #endif
|
|---|
| 55 | assert(provided == MPI_THREAD_MULTIPLE);
|
|---|
| 56 | MPI_Comm_size(MPI_COMM_WORLD, &size);
|
|---|
| 57 | assert(size == 2);
|
|---|
| 58 | MPI_Comm_dup(MPI_COMM_WORLD, &(&thread_infos[1])->comm);
|
|---|
| 59 |
|
|---|
| 60 | thread_infos[0].tid = 0;
|
|---|
| 61 | thread_infos[0].comm = MPI_COMM_WORLD;
|
|---|
| 62 | thread_infos[1].tid = 1;
|
|---|
| 63 |
|
|---|
| 64 | pthread_create(&threads[0], NULL, thread, thread_infos);
|
|---|
| 65 | pthread_create(&threads[1], NULL, thread, thread_infos + 1);
|
|---|
| 66 | for (int i = 0; i < 2; i++)
|
|---|
| 67 | pthread_join(threads[i], &status);
|
|---|
| 68 | MPI_Comm_free(&thread_infos[1].comm);
|
|---|
| 69 | MPI_Finalize();
|
|---|
| 70 | return 0;
|
|---|
| 71 | }
|
|---|