-
Notifications
You must be signed in to change notification settings - Fork 1
/
mpi.sml
701 lines (542 loc) · 24.4 KB
/
mpi.sml
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
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
structure MPI =
struct
exception MPIError of int * string
structure P = MLton.Pointer
type comm = P.t
type group = P.t
structure WordArray = Word8Array
structure WordArraySlice = Word8ArraySlice
structure WordVector = Word8Vector
structure WordVectorSlice = Word8VectorSlice
exception AssertionFailed
fun assert true = ()
| assert false = raise AssertionFailed
val e = _export "mlton_MPI_exception": (int * int * string -> unit) -> unit;
val _ = e (fn (i, len, msg) => raise MPIError (i, msg))
val cInit = _import "mlton_MPI_Init" : int * string array * int array -> unit;
fun Init (args) =
let
val argc = List.length args
val argv = Array.fromList args
val argvsz = Array.fromList (map String.size args)
in
cInit (argc, argv, argvsz)
end
val Success = 0
val Finalize = _import "MPI_Finalize" : unit -> unit;
val Wtime = _import "MPI_Wtime": unit -> real ;
val Barrier = _import "MPI_Barrier" : comm -> int ;
structure Comm =
struct
val World =
let
val f = _import "mlton_MPI_comm_world" : unit -> comm ;
in
f()
end
val cSize = _import "MPI_Comm_size" : comm * int ref -> int ;
fun Size (comm) =
let
val i = ref ~1
in
cSize (comm, i); !i
end
val cRank = _import "MPI_Comm_rank" : comm * int ref -> int;
fun Rank (comm) =
let
val i = ref ~1
in
cRank (comm, i); !i
end
val cCompare = _import "MPI_Comm_compare" : comm * comm * int ref -> int;
fun Equal (comm1,comm2) =
let
val i = ref ~1
in
cCompare (comm1, comm2, i); ((!i) = 0)
end
val cCreate = _import "MPI_Comm_create" : comm * group * comm ref -> int;
fun Create (comm,group) =
let
val p = ref P.null
in
cCreate (comm, group, p); !p
end
val cSplit = _import "MPI_Comm_split" : comm * int * int * comm ref -> int;
fun Split (comm,color,key) =
let
val p = ref P.null
in
cSplit (comm,color,key,p); !p
end
val cCartCreate = _import "MPI_Cart_create" : comm * int * Int32Array.array * Int32Array.array * bool * comm ref -> int;
fun CartCreate (comm,dims,periods,reorder) =
let
val p = ref P.null
val ndims = Int32Array.length dims
in
cCartCreate (comm, ndims, dims, periods, reorder, p); !p
end
val cDimsCreate = _import "MPI_Dims_create" : int * int * Int32Array.array -> int;
fun DimsCreate (nnodes, ndims) =
let
val v = Int32Array.array (ndims, ~1)
in
cDimsCreate (nnodes,ndims,v); v
end
val cCartRank = _import "MPI_Cart_rank" : comm * Int32Array.array * int ref -> int ;
fun CartRank (comm, coords) =
let
val i = ref ~1
in
cCartRank (comm, coords, i); !i
end
val cCartdimGet = _import "MPI_Cartdim_get" : comm * int ref -> int ;
fun CartdimGet (comm) =
let
val i = ref ~1
in
cCartdimGet (comm, i); !i
end
val cCartCoords = _import "MPI_Cart_coords" : comm * int * int * Int32Array.array -> int ;
fun CartCoords (comm,rank) =
let
val ndims = CartdimGet comm
val coords = Int32Array.array (ndims,~1)
in
cCartCoords (comm, rank, ndims, coords); coords
end
end
structure Group =
struct
val cSize = _import "MPI_Group_size" : group * int ref -> int;
fun Size (group) =
let
val sz = ref ~1
in
cSize (group,sz); !sz
end
val cRank = _import "MPI_Group_rank" : group * int ref -> int;
fun Rank (group) =
let
val rank = ref ~1
in
cRank (group,rank); !rank
end
val cTranslateRanks = _import "MPI_Group_translate_ranks" : group * int * Int32Array.array * group * Int32Array.array -> int ;
fun TranslateRanks (group1, ranks1, group2) =
let
val nranks = Int32Array.length ranks1
val ranks2 = Int32Array.array (nranks, ~1)
in
cTranslateRanks (group1, nranks, ranks1, group2, ranks2)
end
val cCommGroup = _import "MPI_Comm_group" : group * group ref -> int ;
fun CommGroup (comm) =
let
val p = ref P.null
in
cCommGroup (comm,p); !p
end
val cUnion = _import "MPI_Group_union" : group * group * group ref -> int ;
fun Union (group1,group2) =
let
val p = ref P.null
in
cUnion (group1,group2,p); !p
end
val cDiff = _import "MPI_Group_difference" : group * group * group ref -> int ;
fun Diff (group1,group2) =
let
val p = ref P.null
in
cDiff (group1,group2,p); !p
end
val cIntersection = _import "MPI_Group_intersection" : group * group * group ref -> int ;
fun Intersection (group1,group2) =
let
val p = ref P.null
in
cIntersection (group1,group2,p); !p
end
val cIncl = _import "MPI_Group_incl" : group * int * Int32Array.array * group ref -> int ;
fun Incl (group,ranks) =
let
val p = ref P.null
val nranks = Int32Array.length ranks
in
cIncl (group,nranks,ranks,p); !p
end
val cExcl = _import "MPI_Group_excl" : group * int * Int32Array.array * group ref -> int ;
fun Excl (group,ranks) =
let
val p = ref P.null
val nranks = Int32Array.length ranks
in
cExcl (group,nranks,ranks,p); !p
end
end
structure Message =
struct
val AnyTag =
let
val f = _import "mlton_MPI_get_any_tag" : unit -> int;
in
f()
end
val AnySource =
let
val f = _import "mlton_MPI_get_any_source" : unit -> int;
in
f()
end
val cProbe = _import "mlton_MPI_probe" : int ref * int ref * int ref * int * int * comm -> unit;
fun Probe (source, tag, comm) =
let
val status_count = ref ~1
val status_source = ref ~1
val status_tag = ref ~1
in
cProbe (status_count, status_source, status_tag,
source, tag, comm);
((!status_count), (!status_source), (!status_tag))
end
val cSendW8 = _import "mlton_MPI_Send_Word8" : WordVector.vector * int * int * int * comm -> int;
val cRecvW8 = _import "mlton_MPI_Recv_Word8" : WordArray.array * int * int * int * comm -> int;
fun Send (pu: 'a Pickle.pu) (data: 'a, dest, tag, comm) =
let
val buf = Pickle.pickle pu data
val n = WordVector.length buf
in
cSendW8 (buf, n, dest, tag, comm)
end
fun Recv (pu: 'a Pickle.pu) (source, tag, comm) =
let
val (n, _, _) = Probe (source, tag, comm)
val r = WordArray.array (n, 0w0)
val status = cRecvW8 (r, n, source, tag, comm)
in
if (not (status = 0))
then raise MPIError (status, "Recv error")
else Pickle.unpickle pu (WordArray.vector r)
end
end
structure Collective =
struct
val cBcastW8 = _import "mlton_MPI_Bcast_Word8" : WordVector.vector * int * WordArray.array * int * comm -> int;
val cScatterW8 = _import "mlton_MPI_Scatter_Word8" : WordVector.vector * int * WordArray.array * int * int * comm -> int;
val cScattervW8 = _import "mlton_MPI_Scatterv_Word8" : WordVector.vector * IntArray.array * IntArray.array * WordArray.array * int * int * comm -> int;
val cGatherW8 = _import "mlton_MPI_Gather_Word8" : WordVector.vector * int * WordArray.array * int * int * comm -> int;
val cGathervW8 = _import "mlton_MPI_Gatherv_Word8" : WordVector.vector * int * WordArray.array * IntArray.array * IntArray.array * int * comm -> int;
val cAllgatherW8 = _import "mlton_MPI_Allgather_Word8" : WordVector.vector * int * WordArray.array * int * comm -> int;
val cAllgathervW8 = _import "mlton_MPI_Allgatherv_Word8" : WordVector.vector * int * WordArray.array * IntArray.array * IntArray.array * comm -> int;
val cAlltoallW8 = _import "mlton_MPI_Alltoall_Word8" : WordVector.vector * int * WordArray.array * int * comm -> int;
val cAlltoallvW8 = _import "mlton_MPI_Alltoallv_Word8" : WordVector.vector * IntArray.array * IntArray.array * WordArray.array * IntArray.array * IntArray.array * comm -> int;
fun Bcast (pu: 'a Pickle.pu) (data, root, comm) =
let
val myrank = Comm.Rank comm
in
if myrank = root
then
(let
val input = valOf data
val buf = Pickle.pickle pu input
val szbuf = Pickle.pickle Pickle.int (WordVector.length buf)
val recvbuf = WordArray.array(WordVector.length szbuf, 0w0)
val status = cBcastW8 (szbuf, WordVector.length szbuf, recvbuf, root, comm)
val _ = assert (status = Success)
val sz = Pickle.unpickle Pickle.int (WordArray.vector recvbuf)
val recvbuf = WordArray.array(sz, 0w0)
val status = cBcastW8 (buf, sz, recvbuf, root, comm)
val _ = assert (status = Success)
in
Pickle.unpickle pu (WordArray.vector recvbuf)
end)
else (let
val szbuf = Pickle.pickle Pickle.int 0
val recvbuf = WordArray.array(WordVector.length szbuf, 0w0)
val status = cBcastW8 (szbuf, WordVector.length szbuf, recvbuf, root, comm)
val _ = assert (status = Success)
val sz = Pickle.unpickle Pickle.int (WordArray.vector recvbuf)
val recvbuf = WordArray.array(sz, 0w0)
val status = cBcastW8 (szbuf, sz, recvbuf, root, comm)
val _ = assert (status = Success)
in
Pickle.unpickle pu (WordArray.vector recvbuf)
end)
end
fun Scatter (pu: 'a Pickle.pu) (data: 'a list option, root, comm): 'a =
let
val myrank = Comm.Rank comm
in
if myrank = root
then (let
val nprocs = Comm.Size comm
val lst = valOf data
val _ = assert((List.length lst) = nprocs)
val pkls = List.map (Pickle.pickle pu) lst
val szbuf = Pickle.pickle Pickle.int (WordVector.length (hd pkls))
val recvbuf = WordArray.array(WordVector.length szbuf, 0w0)
val status = cBcastW8 (szbuf, WordVector.length szbuf, recvbuf, root, comm)
val _ = assert (status = Success)
(* Allocates receive buffer *)
val sz = Pickle.unpickle Pickle.int (WordArray.vector recvbuf)
val myrecvbuf = WordArray.array (sz, 0w0)
(* Allocates and fills send buffer *)
val sendbuf = WordVectorSlice.concat (List.map (fn(v) => (WordVectorSlice.slice (v,0,SOME sz))) pkls)
in
(* Performs the scatter & returns received value *)
cScatterW8 (sendbuf, sz, myrecvbuf, sz, root, comm);
Pickle.unpickle pu (WordArray.vector myrecvbuf)
end)
else
(let
val szbuf = Pickle.pickle Pickle.int 0
val recvbuf = WordArray.array(WordVector.length szbuf, 0w0)
val status = cBcastW8 (szbuf, WordVector.length szbuf, recvbuf, root, comm)
val _ = assert (status = Success)
val sz = Pickle.unpickle Pickle.int (WordArray.vector recvbuf)
val recvbuf = WordArray.array(sz, 0w0)
val status = cScatterW8 (szbuf, sz, recvbuf, sz, root, comm)
val _ = assert (status = Success)
in
Pickle.unpickle pu (WordArray.vector recvbuf)
end)
end
fun Scatterv (pu: 'a Pickle.pu) (data: 'a list option, root, comm): 'a =
let
val myrank = Comm.Rank comm
in
if myrank = root
then
(let
val nprocs = Comm.Size comm
val lst = valOf data
val _ = assert((List.length lst) = nprocs)
val pkls = List.map (Pickle.pickle pu) lst
val sendlengths = List.map WordVector.length pkls
val displs = List.rev (#2(List.foldl (fn (len,(i,lst)) => (i+len,i :: lst)) (0,[]) sendlengths))
(* Scatters the lengths of the buffers to all the processes *)
val mylen = Scatter Pickle.int (SOME sendlengths, root, comm)
(* Allocates receive buffer *)
val myrecvbuf = WordArray.array (mylen, 0w0)
(* Allocates and fills send buffer *)
val sendbuf = WordVectorSlice.concat (List.map (fn(v) => (WordVectorSlice.slice (v,0,NONE))) pkls)
(* Performs the scatter & returns received value *)
val status = cScattervW8 (sendbuf, IntArray.fromList sendlengths, IntArray.fromList displs,
myrecvbuf, mylen, root, comm)
val _ = assert (status = Success)
in
Pickle.unpickle pu (WordArray.vector myrecvbuf)
end)
else
(let
val sendbuf = Pickle.pickle Pickle.int 0
(* Receives the length of the buffer for this process *)
val mylen = Scatter Pickle.int (NONE, root, comm)
(* Allocates receive buffer *)
val myrecvbuf = WordArray.array (mylen, 0w0)
(* Performs the scatter & returns received value *)
val status = cScattervW8 (sendbuf, IntArray.fromList [], IntArray.fromList [],
myrecvbuf, mylen, root, comm)
val _ = assert (status = Success)
in
Pickle.unpickle pu (WordArray.vector myrecvbuf)
end)
end
fun Gather (pu: 'a Pickle.pu) (data: 'a, root, comm) =
let
val nprocs = Comm.Size comm
val myrank = Comm.Rank comm
val mydata = Pickle.pickle pu data
val mylen = WordVector.length mydata
in
if myrank = root
then
(let
(* Allocate receive buffer *)
val sz = mylen * nprocs
val recvbuf = WordArray.array (sz, 0w0)
(* Gather the data *)
val status = cGatherW8 (mydata, mylen, recvbuf, mylen, root, comm)
val _ = assert (status = Success)
(* Build a list of results and return *)
val uf = Pickle.unpickle pu
val results = List.tabulate
(nprocs,
fn(i) =>
let
val offset = i*mylen
val s = WordArraySlice.slice (recvbuf, offset, SOME(mylen))
in
uf (WordArraySlice.vector s)
end)
in
results
end)
else
(let
val recvbuf = WordArray.array (0, 0w0)
val status = cGatherW8 (mydata, mylen, recvbuf, mylen, root, comm)
val _ = assert (status = Success)
in
[]
end)
end
fun Gatherv (pu: 'a Pickle.pu) (data: 'a, root, comm) =
let
val nprocs = Comm.Size comm
val myrank = Comm.Rank comm
val mydata = Pickle.pickle pu data
val mylen = WordVector.length mydata
val recvlengths = Gather Pickle.int (mylen, root, comm)
in
if myrank = root
then
(let
(* Gather the data *)
val recvbuf = WordArray.array (List.foldl (op +) 0 recvlengths, 0w0)
val displs = List.rev (#2(List.foldl (fn (len,(i,lst)) => (i+len,i :: lst)) (0,[]) recvlengths))
val status = cGathervW8 (mydata, mylen, recvbuf, IntArray.fromList recvlengths,
IntArray.fromList displs, root, comm)
val _ = assert (status = Success)
(* Build a list of results and return *)
val uf = Pickle.unpickle pu
val (results,_) = List.foldl
(fn(count,(ax,offset)) =>
let
val s = WordArraySlice.slice (recvbuf, offset, SOME(count))
in
((uf (WordArraySlice.vector s))::ax, offset+count)
end)
([],0) recvlengths
in
List.rev results
end)
else
(* If not root, send our length *)
(let
val recvbuf = WordArray.array (0, 0w0)
val status = cGathervW8 (mydata, mylen, recvbuf, IntArray.fromList [],
IntArray.fromList [], root, comm)
val _ = assert (status = Success)
in
[]
end)
end
fun Allgather (pu: 'a Pickle.pu) (data: 'a, comm) =
let
val nprocs = Comm.Size comm
val myrank = Comm.Rank comm
val mydata = Pickle.pickle pu data
val mylen = WordVector.length mydata
(* Allocate receive buffer *)
val sz = mylen * nprocs
val recvbuf = WordArray.array (sz, 0w0)
(* Gather the data *)
val status = cAllgatherW8 (mydata, mylen, recvbuf, mylen, comm)
val _ = assert (status = Success)
(* Build a list of results and return *)
val uf = Pickle.unpickle pu
val results = List.tabulate
(nprocs,
fn(i) =>
let
val offset = i*mylen
val s = WordArraySlice.slice (recvbuf, offset, SOME(mylen))
in
uf (WordArraySlice.vector s)
end)
in
results
end
fun Allgatherv (pu: 'a Pickle.pu) (data: 'a, comm) =
let
val nprocs = Comm.Size comm
val myrank = Comm.Rank comm
val mydata = Pickle.pickle pu data
val mylen = WordVector.length mydata
val recvlengths = Allgather Pickle.int (mylen, comm)
in
let
(* Gather the data *)
val recvbuf = WordArray.array (List.foldl (op +) 0 recvlengths, 0w0)
val displs = List.rev (#2(List.foldl (fn (len,(i,lst)) => (i+len,i :: lst)) (0,[]) recvlengths))
val status = cAllgathervW8 (mydata, mylen, recvbuf, IntArray.fromList recvlengths,
IntArray.fromList displs, comm)
val _ = assert (status = Success)
(* Build a list of results and return *)
val uf = Pickle.unpickle pu
val (results,_) = List.foldl
(fn(count,(ax,offset)) =>
let
val s = WordArraySlice.slice (recvbuf, offset, SOME(count))
in
((uf (WordArraySlice.vector s))::ax, offset+count)
end)
([],0) recvlengths
in
List.rev results
end
end
fun Alltoall (pu: 'a Pickle.pu) (data: 'a list, comm) =
let
val nprocs = Comm.Size comm
val myrank = Comm.Rank comm
val _ = assert((List.length data) = nprocs)
val pkls = List.map (Pickle.pickle pu) data
val mylen = WordVector.length (hd pkls)
(* Allocate receive buffer *)
val sz = mylen * nprocs
val recvbuf = WordArray.array (sz, 0w0)
(* Allocates and fills send buffer *)
val sendbuf = WordVectorSlice.concat (List.map (fn(v) => (WordVectorSlice.slice (v,0,SOME mylen))) pkls)
val status = cAlltoallW8 (sendbuf, mylen, recvbuf, mylen, comm)
val _ = assert (status = Success)
(* Build a list of results and return *)
val uf = Pickle.unpickle pu
val results = List.tabulate
(nprocs,
fn(i) =>
let
val offset = i*mylen
val s = WordArraySlice.slice (recvbuf, offset, SOME(mylen))
in
uf (WordArraySlice.vector s)
end)
in
results
end
fun Alltoallv (pu: 'a Pickle.pu) (data: 'a list, comm) =
let
val nprocs = Comm.Size comm
val myrank = Comm.Rank comm
val _ = assert((List.length data) = nprocs)
val pkls = List.map (Pickle.pickle pu) data
val sendlengths = List.map WordVector.length pkls
val sdispls = List.rev (#2(List.foldl (fn (len,(i,lst)) => (i+len,i :: lst)) (0,[]) sendlengths))
val recvlengths = Alltoall Pickle.int (sendlengths, comm)
(* Allocates receive buffer *)
val sz = List.foldl (op +) 0 recvlengths
val recvbuf = WordArray.array (sz, 0w0)
(* Allocates and fills send buffer *)
val sendbuf = WordVectorSlice.concat (List.map (fn(v) => (WordVectorSlice.slice (v,0,NONE))) pkls)
val rdispls = List.rev (#2(List.foldl (fn (len,(i,lst)) => (i+len,i :: lst)) (0,[]) recvlengths))
val status = cAlltoallvW8 (sendbuf, IntArray.fromList sendlengths, IntArray.fromList sdispls, recvbuf,
IntArray.fromList recvlengths, IntArray.fromList rdispls, comm)
val _ = assert (status = Success)
(* Build a list of results and return *)
val uf = Pickle.unpickle pu
val (results,_) = List.foldl
(fn(count,(ax,offset)) =>
let
val s = WordArraySlice.slice (recvbuf, offset, SOME(count))
in
((uf (WordArraySlice.vector s))::ax, offset+count)
end)
([],0) recvlengths
in
List.rev results
end
end
end