-
Notifications
You must be signed in to change notification settings - Fork 0
/
mpiBaseTest31t.cc
99 lines (79 loc) · 2.22 KB
/
mpiBaseTest31t.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <omp.h>
#include <vector>
#include <mpi.h>
#include <assert.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
//#define PRINTF(args,...) printf( args, ##__VA_ARGS__ )
#define PRINTF(args,...)
struct Info {
int numIterations;
int tid;
int rank;
int other;
pthread_barrier_t* barrier;
size_t threadPart;
char* sendBuf;
char* recvBuf;
};
void *work( void * info );
double doTest31t( int rank, int numIterations, char* sendBuf, char* recvBuf, int numThreads, size_t threadPart )
{
double start;
std::vector<pthread_t> threads(numThreads);
int rc;
std::vector<Info> infoV(numThreads);
Info info;
info.numIterations = numIterations;
info.rank = rank;
info.other = (rank + 1) % 2;
info.sendBuf = sendBuf;
info.recvBuf = recvBuf;
info.threadPart = threadPart;
pthread_barrier_t barrier;
rc = pthread_barrier_init(&barrier,NULL, numThreads );
info.barrier = &barrier;
assert(0 == rc);
for ( unsigned j = 1; j < threads.size(); j++) {
infoV[j] = info;
infoV[j].tid = j;
rc = pthread_create( &threads[j], NULL, work, &infoV[j] );
assert(0 == rc);
}
info.tid = 0;
start = MPI_Wtime();
work( &info );
double val = MPI_Wtime() - start;
for ( unsigned j = 1; j < threads.size(); j++) {
pthread_join( threads[j], NULL );
}
PRINTF("%s():%d %d threadPart=%lu\n",__func__,__LINE__,rank,threadPart);
return val;
}
void *work( void * x )
{
Info* info = (Info*) x;
int rc;
int iteration;
int tid = info->tid;
size_t threadPart = info->threadPart;
int other = info->other;
char* sendBuf = info->sendBuf;
char* recvBuf = info->recvBuf;
PRINTF("%s():%d rank=%d tid=%d enter\n",__func__,__LINE__,info->rank,tid);
for ( iteration = 0; iteration < info->numIterations; iteration++ ) {
pthread_barrier_wait( info->barrier );
if ( 0 == info->rank ) {
rc = MPI_Send( (char*) sendBuf + tid * threadPart, threadPart, MPI_CHAR, other, tid, MPI_COMM_WORLD );
assert( rc == MPI_SUCCESS );
} else {
rc = MPI_Recv( (char*) recvBuf + tid * threadPart, threadPart, MPI_CHAR, other, tid, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
assert( rc == MPI_SUCCESS );
}
}
PRINTF("%s():%d rank=%d tid=%d done\n",__func__,__LINE__,info->rank,tid);
return NULL;
}