-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththreadWork.c
55 lines (49 loc) · 1.42 KB
/
threadWork.c
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
#include <unistd.h>
#include <sys/types.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <mpi.h>
#include "kernel.h"
int main(int argc, char** argv) {
int i, threadCnt;
int rank;
int worldSz;
int provided;
double t0;
MPI_Init_thread(&argc, &argv, MPI_THREAD_MULTIPLE, &provided);
if( provided != MPI_THREAD_MULTIPLE ) {
fprintf(stderr, "Error: MPI MPI_THREAD_MULTIPLE not supported\n");
return 0;
}
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &worldSz);
if( worldSz != 1 ) {
fprintf(stderr, "Error: Only one mpi rank is allowed\n");
return 0;
}
if( argc != 2 ) {
fprintf(stderr, "Usage: %s <num threads>\n", argv[0]);
return 0;
}
threadCnt = atoi(argv[1]);
thdata* data = (thdata*) calloc(threadCnt,sizeof(thdata));
pthread_t* threads = (pthread_t*) calloc(threadCnt,sizeof(pthread_t));
t0 = MPI_Wtime();
for(i=0; i<threadCnt; i++) {
data[i].rank = rank;
data[i].commsz = worldSz;
data[i].id = (rank*threadCnt)+i;
data[i].peers = threadCnt;
pthread_create (&(threads[i]), NULL, (void*) &kernelComm, (void *) &(data[i]));
}
for(i=0; i<threadCnt; i++)
pthread_join(threads[i], NULL);
if( !rank )
fprintf(stderr, "realTime %.3f\n", MPI_Wtime()-t0);
free(data);
free(threads);
MPI_Finalize();
return 0;
}