-
Notifications
You must be signed in to change notification settings - Fork 1
/
msgs.scm
633 lines (447 loc) · 17.1 KB
/
msgs.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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
;;
;; 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/>.
;;
;; Point-to-point communication
; Include into generated code, but don't parse:
#>
void MPI_send_fixnum (C_word data, C_word dest, C_word tag, C_word comm)
{
int n, vdest, vtag;
MPI_check_comm(comm);
n = C_unfix(data);
vdest = (int)C_num_to_int (dest);
vtag = (int)C_num_to_int (tag);
MPI_Send(&n, 1, MPI_INT, vdest, vtag, Comm_val(comm));
}
void MPI_send_int (C_word data, C_word dest, C_word tag, C_word comm)
{
long n; int vdest, vtag;
MPI_check_comm(comm);
n = C_num_to_long(data);
vdest = (int)C_num_to_int (dest);
vtag = (int)C_num_to_int (tag);
MPI_Send(&n, 1, MPI_LONG, vdest, vtag, Comm_val(comm));
}
void MPI_send_flonum (C_word data, C_word dest, C_word tag, C_word comm)
{
double n; int vdest, vtag;
MPI_check_comm(comm);
n = C_c_double(data);
vdest = (int)C_num_to_int (dest);
vtag = (int)C_num_to_int (tag);
MPI_Send(&n, 1, MPI_DOUBLE, vdest, vtag, Comm_val(comm));
}
void MPI_send_u8vector (C_word data, C_word dest, C_word tag, C_word comm)
{
unsigned char *vect; int len, vdest, vtag;
MPI_check_comm(comm);
vect = C_c_u8vector(data);
len = C_8vector_length(data);
vdest = (int)C_num_to_int (dest);
vtag = (int)C_num_to_int (tag);
MPI_Send(vect, len, MPI_UNSIGNED_CHAR, vdest, vtag, Comm_val(comm));
}
void MPI_send_s8vector (C_word data, C_word dest, C_word tag, C_word comm)
{
char *vect; int len, vdest, vtag;
MPI_check_comm(comm);
vect = C_c_s8vector(data);
len = C_8vector_length(data);
vdest = (int)C_num_to_int (dest);
vtag = (int)C_num_to_int (tag);
MPI_Send(vect, len, MPI_SIGNED_CHAR, vdest, vtag, Comm_val(comm));
}
void MPI_send_u16vector (C_word data, C_word dest, C_word tag, C_word comm)
{
unsigned short *vect; int len, vdest, vtag;
MPI_check_comm(comm);
vect = C_c_u16vector(data);
len = C_16vector_length(data);
vdest = (int)C_num_to_int (dest);
vtag = (int)C_num_to_int (tag);
MPI_Send(vect, len, MPI_UNSIGNED_SHORT, vdest, vtag, Comm_val(comm));
}
void MPI_send_s16vector (C_word data, C_word dest, C_word tag, C_word comm)
{
short *vect; int len, vdest, vtag;
MPI_check_comm(comm);
vect = C_c_s16vector(data);
len = C_16vector_length(data);
vdest = (int)C_num_to_int (dest);
vtag = (int)C_num_to_int (tag);
MPI_Send(vect, len, MPI_SHORT, vdest, vtag, Comm_val(comm));
}
void MPI_send_u32vector (C_word data, C_word dest, C_word tag, C_word comm)
{
unsigned int *vect; int len, vdest, vtag;
MPI_check_comm(comm);
vect = C_c_u32vector(data);
len = C_32vector_length(data);
vdest = (int)C_num_to_int (dest);
vtag = (int)C_num_to_int (tag);
MPI_Send(vect, len, MPI_UNSIGNED, vdest, vtag, Comm_val(comm));
}
void MPI_send_s32vector (C_word data, C_word dest, C_word tag, C_word comm)
{
int *vect; int len, vdest, vtag;
MPI_check_comm(comm);
vect = C_c_s32vector(data);
len = C_32vector_length(data);
vdest = (int)C_num_to_int (dest);
vtag = (int)C_num_to_int (tag);
MPI_Send(vect, len, MPI_INT, vdest, vtag, Comm_val(comm));
}
void MPI_send_f32vector (C_word data, C_word dest, C_word tag, C_word comm)
{
float *vect; int len, vdest, vtag;
MPI_check_comm(comm);
vect = C_c_f32vector(data);
len = C_32vector_length(data);
vdest = (int)C_num_to_int (dest);
vtag = (int)C_num_to_int (tag);
MPI_Send(vect, len, MPI_FLOAT, vdest, vtag, Comm_val(comm));
}
void MPI_send_f64vector (C_word data, C_word dest, C_word tag, C_word comm)
{
double *vect; int len, vdest, vtag;
MPI_check_comm(comm);
vect = C_c_f64vector(data);
len = C_64vector_length(data);
vdest = (int)C_num_to_int (dest);
vtag = (int)C_num_to_int (tag);
MPI_Send(vect, len, MPI_DOUBLE, vdest, vtag, Comm_val(comm));
}
void MPI_send_bytevector (C_word data, C_word dest, C_word tag, C_word comm)
{
char * buffer;
int len; int vdest, vtag;
MPI_check_comm(comm);
C_i_check_bytevector (data);
vdest = (int)C_num_to_int (dest);
vtag = (int)C_num_to_int (tag);
len = C_bytevector_length (data);
buffer = C_c_bytevector (data);
MPI_Send(buffer, len, MPI_BYTE, vdest, vtag, Comm_val(comm));
}
void MPI_send_data (C_word ty, int count, C_word data, C_word dest, C_word tag, C_word comm)
{
char * buffer;
int len; int vdest, vtag;
MPI_check_comm(comm);
MPI_check_datatype (ty);
C_i_check_bytevector (data);
vdest = (int)C_num_to_int (dest);
vtag = (int)C_num_to_int (tag);
buffer = C_c_bytevector (data);
MPI_Send(buffer, count, Datatype_val(ty), vdest, vtag, Comm_val(comm));
}
<#
;; Sending data
(define MPI:send-fixnum (foreign-lambda void "MPI_send_fixnum"
scheme-object scheme-object scheme-object scheme-object ))
(define MPI:send-int (foreign-lambda void "MPI_send_int"
scheme-object scheme-object scheme-object scheme-object ))
(define MPI:send-flonum (foreign-lambda void "MPI_send_flonum"
scheme-object scheme-object scheme-object scheme-object ))
(define MPI:send-u8vector (foreign-lambda void "MPI_send_u8vector"
scheme-object scheme-object scheme-object scheme-object ))
(define MPI:send-s8vector (foreign-lambda void "MPI_send_s8vector"
scheme-object scheme-object scheme-object scheme-object ))
(define MPI:send-u16vector (foreign-lambda void "MPI_send_u16vector"
scheme-object scheme-object scheme-object scheme-object ))
(define MPI:send-s16vector (foreign-lambda void "MPI_send_s16vector"
scheme-object scheme-object scheme-object scheme-object ))
(define MPI:send-u32vector (foreign-lambda void "MPI_send_u32vector"
scheme-object scheme-object scheme-object scheme-object ))
(define MPI:send-s32vector (foreign-lambda void "MPI_send_s32vector"
scheme-object scheme-object scheme-object scheme-object ))
(define MPI:send-f32vector (foreign-lambda void "MPI_send_f32vector"
scheme-object scheme-object scheme-object scheme-object ))
(define MPI:send-f64vector (foreign-lambda void "MPI_send_f64vector"
scheme-object scheme-object scheme-object scheme-object ))
(define MPI_send_bytevector (foreign-lambda void "MPI_send_bytevector"
scheme-object scheme-object scheme-object scheme-object ))
(define MPI_send_data (foreign-lambda void "MPI_send_data"
scheme-object int scheme-object scheme-object scheme-object scheme-object ))
(define (MPI:send-bytevector blob dest tag comm)
(MPI_send_bytevector blob dest tag comm))
(define (MPI:send ty count x dest tag comm)
(MPI_send_data ty count x dest tag comm))
;; Probe for pending messages and determine length
(define MPI:probe
(foreign-primitive ((scheme-object ty)
(integer source)
(integer tag)
(scheme-object comm))
#<<EOF
MPI_Status status;
int count;
C_word status_count, status_source, status_tag;
C_word *ptr;
MPI_check_comm(comm);
MPI_check_datatype(ty);
MPI_Probe(source, tag, Comm_val(comm), &status);
MPI_Get_count(&status, Datatype_val(ty), &count);
status_count = C_fix(count);
ptr = C_alloc (C_SIZEOF_FLONUM);
status_source = C_int_to_num (&ptr, status.MPI_SOURCE);
ptr = C_alloc (C_SIZEOF_FLONUM);
status_tag = C_int_to_num (&ptr, status.MPI_TAG);
#if defined(C_BINARY_VERSION) && (C_BINARY_VERSION >= 8)
C_word rval[5] = { C_SCHEME_UNDEFINED, C_k, status_count, status_source, status_tag };
C_values(5, rval);
#else
C_values(5, C_SCHEME_UNDEFINED, C_k, status_count, status_source, status_tag );
#endif
EOF
))
(define MPI:receive-int
(foreign-primitive scheme-object ((integer source)
(integer tag)
(scheme-object comm))
#<<EOF
long n;
C_word result; C_word *ptr;
MPI_check_comm(comm);
MPI_Recv(&n, 1, MPI_LONG, source, tag, Comm_val(comm), MPI_STATUS_IGNORE);
ptr = C_alloc (C_SIZEOF_FLONUM);
result = C_long_to_num (&ptr, n);
C_return(result);
EOF
))
(define MPI:receive-flonum
(foreign-primitive scheme-object ((integer source)
(integer tag)
(scheme-object comm))
#<<EOF
double n; C_word *ptr;
C_word result;
MPI_check_comm(comm);
MPI_Recv(&n, 1, MPI_DOUBLE, source, tag, Comm_val(comm), MPI_STATUS_IGNORE);
ptr = C_alloc (C_SIZEOF_FLONUM);
result = C_flonum (&ptr, n);
C_return(result);
EOF
))
(define MPI:receive-fixnum
(foreign-primitive scheme-object ((integer source)
(integer tag)
(scheme-object comm))
#<<EOF
int n;
C_word result;
MPI_check_comm(comm);
MPI_Recv(&n, 1, MPI_INT, source, tag, Comm_val(comm), MPI_STATUS_IGNORE);
C_return(C_fix(n));
EOF
))
#>
C_word MPI_receive_u8vector (C_word data, C_word source, C_word tag, C_word comm)
{
unsigned char *vect; int len, vsource, vtag;
MPI_check_comm(comm);
vsource = (int)C_num_to_int (source);
vtag = (int)C_num_to_int (tag);
vect = C_c_u8vector(data);
len = C_8vector_length(data);
MPI_Recv(vect, len, MPI_UNSIGNED_CHAR, vsource, vtag, Comm_val(comm), MPI_STATUS_IGNORE);
C_return(data);
}
C_word MPI_receive_s8vector (C_word data, C_word source, C_word tag, C_word comm)
{
char *vect; int len, vsource, vtag;
MPI_check_comm(comm);
vect = C_c_s8vector(data);
len = C_8vector_length(data);
vsource = (int)C_num_to_int (source);
vtag = (int)C_num_to_int (tag);
MPI_Recv(vect, len, MPI_SIGNED_CHAR, vsource, vtag, Comm_val(comm), MPI_STATUS_IGNORE);
C_return(data);
}
C_word MPI_receive_u16vector (C_word data, C_word source, C_word tag, C_word comm)
{
unsigned short *vect; int len, vsource, vtag;
MPI_check_comm(comm);
vect = C_c_u16vector(data);
len = C_16vector_length(data);
vsource = (int)C_num_to_int (source);
vtag = (int)C_num_to_int (tag);
MPI_Recv(vect, len, MPI_UNSIGNED_SHORT, vsource, vtag, Comm_val(comm), MPI_STATUS_IGNORE);
C_return(data);
}
C_word MPI_receive_s16vector (C_word data, C_word source, C_word tag, C_word comm)
{
short *vect; int len, vsource, vtag;
MPI_check_comm(comm);
vect = C_c_s16vector(data);
len = C_16vector_length(data);
vsource = (int)C_num_to_int (source);
vtag = (int)C_num_to_int (tag);
MPI_Recv(vect, len, MPI_SHORT, vsource, vtag, Comm_val(comm), MPI_STATUS_IGNORE);
C_return(data);
}
C_word MPI_receive_u32vector (C_word data, C_word source, C_word tag, C_word comm)
{
unsigned int *vect; int len, vsource, vtag;
MPI_check_comm(comm);
vect = C_c_u32vector(data);
len = C_32vector_length(data);
vsource = (int)C_num_to_int (source);
vtag = (int)C_num_to_int (tag);
MPI_Recv(vect, len, MPI_UNSIGNED, vsource, vtag, Comm_val(comm), MPI_STATUS_IGNORE);
C_return(data);
}
C_word MPI_receive_s32vector (C_word data, C_word source, C_word tag, C_word comm)
{
int *vect; int len, vsource, vtag;
MPI_check_comm(comm);
vect = C_c_s32vector(data);
len = C_32vector_length(data);
vsource = (int)C_num_to_int (source);
vtag = (int)C_num_to_int (tag);
MPI_Recv(vect, len, MPI_INT, vsource, vtag, Comm_val(comm), MPI_STATUS_IGNORE);
C_return(data);
}
C_word MPI_receive_f32vector (C_word data, C_word source, C_word tag, C_word comm)
{
float *vect; int len, vsource, vtag;
MPI_check_comm(comm);
vect = C_c_f32vector(data);
len = C_32vector_length(data);
vsource = (int)C_num_to_int (source);
vtag = (int)C_num_to_int (tag);
MPI_Recv(vect, len, MPI_FLOAT, vsource, vtag, Comm_val(comm), MPI_STATUS_IGNORE);
C_return(data);
}
C_word MPI_receive_f64vector (C_word data, C_word source, C_word tag, C_word comm)
{
double *vect; int len, vsource, vtag;
MPI_check_comm(comm);
vect = C_c_f64vector(data);
len = C_64vector_length(data);
vsource = (int)C_num_to_int (source);
vtag = (int)C_num_to_int (tag);
MPI_Recv(vect, len, MPI_DOUBLE, vsource, vtag, Comm_val(comm), MPI_STATUS_IGNORE);
C_return(data);
}
C_word MPI_receive_bytevector (C_word data, C_word source, C_word tag, C_word comm)
{
char * buffer;
long len; int vsource, vtag;
MPI_check_comm(comm);
C_i_check_bytevector (data);
vsource = (int)C_num_to_int (source);
vtag = (int)C_num_to_int (tag);
len = C_bytevector_length (data);
buffer = C_c_bytevector (data);
MPI_Recv(buffer, len, MPI_BYTE, vsource, vtag, Comm_val(comm), MPI_STATUS_IGNORE);
C_return (data);
}
C_word MPI_receive_data (C_word ty, int count, C_word data, C_word source, C_word tag, C_word comm)
{
char * buffer;
int vsource, vtag;
MPI_check_comm(comm);
C_i_check_bytevector (data);
vsource = (int)C_num_to_int (source);
vtag = (int)C_num_to_int (tag);
buffer = C_c_bytevector (data);
MPI_Recv(buffer, count, Datatype_val(ty), vsource, vtag, Comm_val(comm), MPI_STATUS_IGNORE);
C_return (data);
}
<#
;; Receiving data
(define MPI_receive_u8vector (foreign-lambda scheme-object "MPI_receive_u8vector"
scheme-object scheme-object scheme-object scheme-object ))
(define MPI_receive_s8vector (foreign-lambda scheme-object "MPI_receive_s8vector"
scheme-object scheme-object scheme-object scheme-object ))
(define MPI_receive_u16vector (foreign-lambda scheme-object "MPI_receive_u16vector"
scheme-object scheme-object scheme-object scheme-object ))
(define MPI_receive_s16vector (foreign-lambda scheme-object "MPI_receive_s16vector"
scheme-object scheme-object scheme-object scheme-object ))
(define MPI_receive_u32vector (foreign-lambda scheme-object "MPI_receive_u32vector"
scheme-object scheme-object scheme-object scheme-object ))
(define MPI_receive_s32vector (foreign-lambda scheme-object "MPI_receive_s32vector"
scheme-object scheme-object scheme-object scheme-object ))
(define MPI_receive_f32vector (foreign-lambda scheme-object "MPI_receive_f32vector"
scheme-object scheme-object scheme-object scheme-object ))
(define MPI_receive_f64vector (foreign-lambda scheme-object "MPI_receive_f64vector"
scheme-object scheme-object scheme-object scheme-object ))
(define MPI_receive_bytevector (foreign-lambda scheme-object "MPI_receive_bytevector"
scheme-object scheme-object scheme-object scheme-object ))
(define MPI_receive_data (foreign-lambda scheme-object "MPI_receive_data"
scheme-object int scheme-object scheme-object scheme-object scheme-object ))
(define (make-receive ty makev recv)
(lambda (source tag comm)
(let-values (((count actual-source actual-tag) (MPI:probe ty source tag comm)))
(let ((buffer (makev count)))
(recv buffer source tag comm)
))
))
(define-syntax define-srfi4-receive
(er-macro-transformer
(lambda (x r c)
(let* ((type (cadr x))
(%define (r 'define))
(makev (string->symbol (string-append "make-" (symbol->string type) "vector")))
(recv (string->symbol (string-append "MPI_receive_" (symbol->string type) "vector")))
(name (string->symbol (string-append "MPI:receive-" (symbol->string type) "vector")))
(ty (string->symbol (string-append "MPI:type-" (symbol->string type)))))
`(,%define ,name (make-receive ,ty ,makev ,recv))))))
(define-srfi4-receive s8)
(define-srfi4-receive u8)
(define-srfi4-receive s16)
(define-srfi4-receive u16)
(define-srfi4-receive s32)
(define-srfi4-receive u32)
(define-srfi4-receive f32)
(define-srfi4-receive f64)
(define MPI:receive-bytevector (make-receive MPI:type-byte make-blob MPI_receive_bytevector))
(define (MPI:receive-bytevector-with-status source tag comm)
(let-values (((count actual-source actual-tag) (MPI:probe MPI:type-byte source tag comm)))
(let ((buffer (make-blob count)))
(let ((v (MPI_receive_bytevector buffer source tag comm)))
(values v actual-source actual-tag)))
))
(define (MPI:receive ty source tag comm)
(let-values (((count actual-source actual-tag) (MPI:probe ty source tag comm)))
(let ((buffer (make-blob (* count (MPI:type-size ty)))))
(MPI_receive_data ty count buffer actual-source actual-tag comm))
))
(define (MPI:receive-with-status ty source tag comm)
(let-values (((count actual-source actual-tag) (MPI:probe ty source tag comm)))
(let ((buffer (make-blob (* count (MPI:type-size ty)))))
(let ((v (MPI_receive_data ty count buffer source tag comm)))
(values v actual-source actual-tag)))
))
;; Auxiliaries
#>
int MPI_get_any_tag(void)
{
return MPI_ANY_TAG;
}
int MPI_get_any_source (void)
{
return (MPI_ANY_SOURCE);
}
<#
(define MPI_get_any_tag (foreign-lambda integer "MPI_get_any_tag"))
(define MPI_get_any_source (foreign-lambda integer "MPI_get_any_source"))
(define MPI:any-tag (MPI_get_any_tag))
(define MPI:any-source (MPI_get_any_source))