-
Notifications
You must be signed in to change notification settings - Fork 0
/
mpiWork.c
160 lines (132 loc) · 3.14 KB
/
mpiWork.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
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include <unistd.h>
#include <sys/types.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
#include <string.h>
#include <dlfcn.h>
#include <limits.h>
#include <time.h>
#include <pthread.h>
#include "kernel.h"
typedef struct thread_args {
thdata *data;
void (*entry)(thdata *);
unsigned long repeat;
} thargs;
static unsigned int strtoui_max(const char *str, char **endptr, int base)
{
unsigned long tmp = strtoul(optarg, endptr, base);
if (tmp > UINT_MAX)
return UINT_MAX;
else
return (unsigned int) tmp;
}
static void *thread(void *arg)
{
int r;
thargs *args = (thargs *) arg;
for (r=0; r<args->repeat; r++)
(*args->entry)(args->data);
return NULL;
}
int main(int argc, char** argv) {
int rc = EXIT_FAILURE;
int c;
unsigned long repeat = 1;
unsigned long r;
int provided;
int rank;
int worldSz;
char *kernel = NULL;
void *handle;
void (*entry)(thdata *);
const char *error;
unsigned int seed = time(NULL);
pthread_t *threads;
unsigned int t;
unsigned int thread_count = 0;
thargs* args;
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);
thdata data;
data.rank = rank;
data.commsz = worldSz;
data.id = rank;
data.peers = 1;
while ((c = getopt (argc, argv, "k:r:s:t:")) != -1)
switch (c) {
case 'k':
kernel = strdup(optarg);
break;
case 'r':
repeat = strtoul(optarg, NULL, 10);
break;
case 's':
seed = strtoui_max(optarg, NULL, 10);
break;
case 't':
thread_count = strtoui_max(optarg, NULL, 10);
}
if (kernel == NULL) {
fprintf(stderr, "Must supply kernel to run!\n");
goto failure;
}
handle = dlopen(kernel, RTLD_NOW);
if (handle == NULL) {
fprintf(stderr, "Unable to load kernel: %s\n", dlerror());
goto failure;
}
*(void **) (&entry) = dlsym(handle, "kernel_entry");
error = dlerror();
if (error != NULL) {
fprintf(stderr, "Unable to start kernel: %s\n", error);
goto failure;
}
if( !rank )
printf("Seed: %d\n", seed);
srand(seed);
double t0 = MPI_Wtime();
if (thread_count == 0) {
for (r=0; r<repeat; r++)
(*entry)(&data);
} else {
if( !rank )
fprintf(stderr, "Starting %u threads\n", thread_count);
threads = calloc(thread_count, sizeof(pthread_t));
args = calloc(thread_count,sizeof(thargs));
for (t = 0; t < thread_count; t++) {
args[t].repeat = repeat;
args[t].entry = entry;
args[t].data = calloc(1,sizeof(thdata));
args[t].data->rank = rank;
args[t].data->commsz = worldSz;
args[t].data->id = (rank*thread_count)+t;
args[t].data->peers = thread_count;
pthread_create(&threads[t], NULL, thread, &args[t]);
}
for (t = 0; t < thread_count; t++) {
pthread_join(threads[t], NULL);
}
for (t = 0; t < thread_count; t++) {
free(args[t].data);
}
free(args);
free(threads);
}
MPI_Barrier(MPI_COMM_WORLD);
if( !rank )
fprintf(stderr, "realTime %f\n", MPI_Wtime()-t0);
rc = EXIT_SUCCESS;
failure:
if (kernel != NULL)
free(kernel);
MPI_Finalize();
return rc;
}