-
Notifications
You must be signed in to change notification settings - Fork 1
/
comm.scm
371 lines (302 loc) · 7.34 KB
/
comm.scm
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
;;
;; Chicken MPI interface. Based on the Caml/MPI interface by Xavier
;; Leroy.
;;
;; Copyright 2007-2018 Ivan Raikov.
;;
;; This program is free software: you can redistribute it and/or
;; modify it under the terms of the GNU General Public License as
;; published by the Free Software Foundation, either version 3 of the
;; License, or (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful, but
;; WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
;; General Public License for more details.
;;
;; A full copy of the GPL license can be found at
;; <http://www.gnu.org/licenses/>.
;;
;; Handling of communicators
; Include into generated code, but don't parse:
#>
static C_word MPI_comm_p(C_word obj)
{
if (C_immediatep(obj)) {
return C_SCHEME_FALSE;
} else if (C_block_header(obj) == MPI_COMM_TAG)
{
return C_SCHEME_TRUE;
} else {
return C_SCHEME_FALSE;
}
}
static C_word MPI_check_comm (C_word obj)
{
if (C_immediatep(obj))
{
chicken_MPI_exception (MPI_ERR_COMM, "MPI_check_comm",
32, "invalid MPI communicator object");
} else if (C_block_header(obj) == MPI_COMM_TAG)
{
return C_SCHEME_UNDEFINED;
} else
{
chicken_MPI_exception (MPI_ERR_COMM, "MPI_check_comm",
32, "invalid MPI communicator object");
}
}
<#
(define MPI:comm? (foreign-lambda scheme-object "MPI_comm_p" scheme-object))
(define MPI_comm_finalizer
(foreign-safe-lambda* void ((scheme-object comm))
#<<END
MPI_Comm *x;
MPI_check_comm (comm);
x = Comm_val (comm);
MPI_Comm_free (x);
END
))
(define MPI_alloc_comm
(foreign-primitive scheme-object ((nonnull-c-pointer comm)
(scheme-object finalizer))
#<<END
C_word result;
chicken_MPI_comm_t newcomm;
newcomm.tag = MPI_COMM_TAG;
newcomm.comm_data = comm;
result = (C_word)&newcomm;
//C_do_register_finalizer(result, finalizer);
C_return (result);
END
))
(define MPI_comm_world
(foreign-primitive nonnull-c-pointer ()
#<<END
C_word result;
result = (C_word)MPI_COMM_WORLD;
C_return (result);
END
))
(define (MPI:get-comm-world)
(let ((w (MPI_comm_world)))
(MPI_alloc_comm w MPI_comm_finalizer)))
(define MPI:comm-size
(foreign-primitive scheme-object ((scheme-object x))
#<<END
C_word *ptr;
C_word result;
int size;
if (MPI_comm_p (x))
{
MPI_Comm_size (Comm_val(x), &size);
ptr = C_alloc (C_SIZEOF_FLONUM);
result = C_int_to_num (&ptr, size);
}
else
{
result = C_SCHEME_FALSE;
}
C_return (result);
END
))
(define MPI:comm-rank
(foreign-primitive scheme-object ((scheme-object x))
#<<END
C_word result;
C_word *ptr;
int rank;
if (MPI_comm_p (x))
{
MPI_Comm_rank (Comm_val(x), &rank);
ptr = C_alloc (C_SIZEOF_FLONUM);
result = C_int_to_num (&ptr, rank);
}
else
{
result = C_SCHEME_FALSE;
}
C_return (result);
END
))
#>
C_word MPI_comm_compare(C_word comm1, C_word comm2)
{
int res;
res = 0;
if ((MPI_comm_p (comm1)) && (MPI_comm_p (comm2)))
{
MPI_Comm_compare(Comm_val(comm1), Comm_val(comm2), &res);
}
if (res == 0)
return C_SCHEME_TRUE;
else
return C_SCHEME_FALSE;
}
<#
(define MPI:comm-equal? (foreign-lambda scheme-object "MPI_comm_compare" scheme-object scheme-object))
(define MPI_comm_split
(foreign-primitive nonnull-c-pointer ((scheme-object comm) (integer color) (integer key))
#<<END
C_word result;
MPI_Comm newcomm;
MPI_check_comm (comm);
if (MPI_comm_p (comm))
{
MPI_Comm_split(Comm_val(comm), color, key, &newcomm);
result = (C_word)newcomm;
}
else
{
result = (C_word)NULL;
};
C_return(result);
END
))
(define (MPI:comm-split comm color split)
(MPI_alloc_comm (MPI_comm_split comm color split) MPI_comm_finalizer))
#>
C_word MPI_get_undefined(void)
{
return C_fix(MPI_UNDEFINED);
}
<#
(define MPI_comm_create
(foreign-primitive nonnull-c-pointer ((scheme-object comm) (scheme-object group))
#<<END
C_word result;
MPI_Comm newcomm;
MPI_check_comm (comm);
if ((MPI_comm_p (comm)) && (MPI_group_p (group)))
{
MPI_Comm_create(Comm_val(comm), Group_val(group), &newcomm);
result = (C_word)newcomm;
}
else
{
result = (C_word)NULL;
}
C_return (result);
END
))
(define (MPI:comm-create comm group)
(MPI_alloc_comm (MPI_comm_create comm group) MPI_comm_finalizer))
(define MPI:undefined (foreign-lambda scheme-object "MPI_get_undefined"))
(define MPI_cart_create
(foreign-primitive nonnull-c-pointer ((scheme-object comm)
(integer ndims)
(integer nperiods)
(nonnull-u32vector dims)
(nonnull-u32vector periods)
(bool reorder))
#<<END
MPI_Comm newcomm;
C_word result;
MPI_check_comm (comm);
if (MPI_comm_p (comm))
{
MPI_Cart_create(Comm_val(comm), ndims, dims, periods,
reorder, &newcomm);
result = (C_word)newcomm;
}
else
{
result = (C_word)NULL;
}
C_return(result);
END
))
(define (MPI:make-cart comm dims periods reorder)
(MPI_alloc_comm
(MPI_cart_create
comm (u32vector-length dims) (u32vector-length periods)
dims periods reorder)
MPI_comm_finalizer))
(define MPI_dims_create
(foreign-primitive scheme-object ((integer nnodes)
(integer ndims)
(scheme-object dims))
#<<END
C_word result;
unsigned int *vdims;
if (C_vectorp(dims))
{
vdims = C_c_u32vector(dims);
MPI_Dims_create(nnodes, ndims, vdims);
result = dims;
}
else
{
result = C_SCHEME_FALSE;
};
C_return(result);
END
))
(define (MPI:make-dims nnodes ndims)
(if (integer? ndims)
(MPI_dims_create nnodes ndims (make-u32vector ndims))
(MPI_dims_create nnodes (u32vector-length ndims) ndims)))
(define MPI:cart-rank
(foreign-primitive scheme-object ((scheme-object comm)
(scheme-object coords))
#<<END
C_word *ptr;
C_word result;
int ndims, rank;
int * vcoords;
if ((C_vectorp(coords)) && (MPI_comm_p (comm)))
{
ndims = C_u_i_32vector_length(coords);
vcoords = C_c_s32vector(coords);
MPI_Cart_rank(Comm_val(comm), vcoords, &rank);
ptr = C_alloc (C_SIZEOF_FLONUM);
result = C_int_to_num (&ptr, rank);
}
else
{
result = C_SCHEME_FALSE;
}
C_return (result);
END
))
(define MPI_cart_dim
(foreign-primitive scheme-object ((scheme-object comm))
#<<END
C_word result;
int ndims;
if (MPI_comm_p (comm))
{
MPI_Cartdim_get(Comm_val(comm), &ndims);
result = C_fix(ndims);
}
else
{
result = C_SCHEME_FALSE;
}
C_return (result);
END
))
(define MPI_cart_coords
(foreign-primitive scheme-object ((scheme-object comm)
(integer rank)
(integer ndims)
(scheme-object coords))
#<<END
C_word result;
int * vcoords;
if ((C_vectorp(coords)) && MPI_comm_p (comm))
{
vcoords = C_c_s32vector(coords);
MPI_Cart_coords(Comm_val(comm), rank, ndims, vcoords);
result = coords;
}
else
{
result = C_SCHEME_FALSE;
}
C_return (result);
END
))
(define (MPI:cart-coords comm rank)
(let ((ndims (MPI_cart_dim comm)))
(MPI_cart_coords comm rank ndims (make-s32vector ndims))))