-
Notifications
You must be signed in to change notification settings - Fork 1
/
mpilib.c
226 lines (161 loc) · 5.07 KB
/
mpilib.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#include <stdio.h>
#include <mpi.h>
#include "mpiexport.h"
typedef int rank_t;
static void mlton_MPI_error_handler (MPI_Comm * comm, int * errcode, ...)
{
char errmsg[MPI_MAX_ERROR_STRING + 1];
int resultlen;
MPI_Error_string(*errcode, errmsg, &resultlen);
errmsg[resultlen-1] = 0;
fprintf(stderr, "%s\n", errmsg);
mlton_MPI_exception (*errcode, resultlen, errmsg);
}
void mlton_MPI_Init (Int32 argc, Pointer argv, Pointer argv_lens)
{
int i, cargvsz, slen;
char *s, *x, **cargv;
MPI_Errhandler hdlr;
cargvsz = (argc+1) * sizeof(char *);
if ((cargv = malloc(cargvsz)) != NULL)
{
if (argc > 0)
{
for (i = 0; i < argc; i++)
{
x = ((char **)argv)[i];
slen = ((int *)argv_lens)[i];
if (( s = malloc (slen+1)) != NULL)
{
memcpy (s, (char *)x, slen);
s[slen] = 0;
cargv[i] = s;
} else
{
cargv[i] = NULL;
}
x += slen;
}
} else
{
i = 0;
}
cargv[i] = NULL;
MPI_Init(&argc, &cargv);
for (i = 0; i < argc; i++)
{
s = cargv[i];
if (s != NULL)
{
free (s);
}
}
memset (cargv, (int)NULL, cargvsz);
free (cargv);
}
MPI_Errhandler_create((MPI_Handler_function *)mlton_MPI_error_handler, &hdlr);
MPI_Errhandler_set(MPI_COMM_WORLD, hdlr);
}
void *mlton_MPI_comm_world (void)
{
return MPI_COMM_WORLD;
}
int mlton_MPI_get_any_tag(void)
{
return MPI_ANY_TAG;
}
int mlton_MPI_get_any_source (void)
{
return (MPI_ANY_SOURCE);
}
void mlton_MPI_probe (int *status_count, int *status_source, int *status_tag,
int source, int tag, MPI_Comm comm)
{
MPI_Status status;
MPI_Probe(source, tag, comm, &status);
MPI_Get_count(&status, MPI_UINT8_T, status_count);
*status_source = status.MPI_SOURCE;
*status_tag = status.MPI_TAG;
}
int mlton_MPI_Recv_Word8 (uint8_t *v, size_t n, int source, int tag, MPI_Comm comm)
{
return MPI_Recv(v, n, MPI_UINT8_T, source, tag, comm, MPI_STATUS_IGNORE);
}
int mlton_MPI_Send_Word8 (uint8_t *v, size_t n, int dest, int tag, MPI_Comm comm)
{
int status;
status = MPI_Send(v, n, MPI_UINT8_T, dest, tag, comm);
return status;
}
int mlton_MPI_Bcast_Word8 (uint8_t *v, size_t n, uint8_t *out, int root, MPI_Comm comm)
{
int status = 0;
rank_t myrank;
MPI_Comm_rank(comm, &myrank);
if (myrank == root)
{
memcpy(out, v, sizeof(*v)*n);
}
status = MPI_Bcast(out, n, MPI_UINT8_T, root, comm);
return status;
}
int mlton_MPI_Scatter_Word8 (uint8_t *sendbuf, int sendcount,
uint8_t *recvbuf, int nrecv, int root, MPI_Comm comm)
{
int result;
result = MPI_Scatter (sendbuf, sendcount, MPI_UINT8_T,
recvbuf, nrecv, MPI_UINT8_T,
root, comm);
return result;
}
int mlton_MPI_Scatterv_Word8 (uint8_t *sendbuf, int *sendcounts, int *displs,
uint8_t *recvbuf, int nrecv, int root, MPI_Comm comm)
{
int result;
result = MPI_Scatterv (sendbuf, sendcounts, displs, MPI_UINT8_T,
recvbuf, nrecv, MPI_UINT8_T,
root, comm);
return result;
}
int mlton_MPI_Gather_Word8 (uint8_t *sendbuf, size_t sn, uint8_t *recvbuf,
int recvcount, int root, MPI_Comm comm)
{
int result;
result = MPI_Gather(sendbuf, sn, MPI_UINT8_T, recvbuf, recvcount, MPI_UINT8_T, root, comm);
return result;
}
int mlton_MPI_Gatherv_Word8 (uint8_t *sendbuf, size_t sn, uint8_t *recvbuf,
int *recvcounts, int *displs, int root, MPI_Comm comm)
{
int result;
result = MPI_Gatherv(sendbuf, sn, MPI_UINT8_T, recvbuf, recvcounts, displs, MPI_UINT8_T, root, comm);
return result;
}
int mlton_MPI_Allgather_Word8 (uint8_t *sendbuf, size_t sn, uint8_t *recvbuf,
int recvcount, MPI_Comm comm)
{
int result;
result = MPI_Allgather(sendbuf, sn, MPI_UINT8_T, recvbuf, recvcount, MPI_UINT8_T, comm);
return result;
}
int mlton_MPI_Allgatherv_Word8 (uint8_t *sendbuf, size_t sn, uint8_t *recvbuf,
int *recvcounts, int *displs, MPI_Comm comm)
{
int result;
result = MPI_Allgatherv(sendbuf, sn, MPI_UINT8_T, recvbuf, recvcounts, displs, MPI_UINT8_T, comm);
return result;
}
int mlton_MPI_Alltoall_Word8 (uint8_t *sendbuf, size_t sn, uint8_t *recvbuf,
int recvcount, MPI_Comm comm)
{
int result;
result = MPI_Alltoall(sendbuf, sn, MPI_UINT8_T, recvbuf, recvcount, MPI_UINT8_T, comm);
return result;
}
int mlton_MPI_Alltoallv_Word8 (uint8_t *sendbuf, int *sendcounts, int *sdispls,
uint8_t *recvbuf, int *recvcounts, int *rdispls, MPI_Comm comm)
{
int result;
result = MPI_Alltoallv(sendbuf, sendcounts, sdispls, MPI_UINT8_T, recvbuf, recvcounts, rdispls, MPI_UINT8_T, comm);
return result;
}