-
Notifications
You must be signed in to change notification settings - Fork 274
/
binding_f77.py
1139 lines (1016 loc) · 50.9 KB
/
binding_f77.py
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
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
##
## Copyright (C) by Argonne National Laboratory
## See COPYRIGHT in top-level directory
##
from local_python import MPI_API_Global as G
from local_python.binding_common import *
from local_python import RE
import re
def dump_f77_c_func(func):
func_name = get_function_name(func)
f_mapping = get_kind_map('F90')
c_mapping = get_kind_map('C')
c_param_list = []
c_param_list_end = [] # for string args, ref: FORT_END_LEN
code_list_common = []
end_list_common = []
# code for HAVE_FINT_IS_INT
c_arg_list_A = []
code_list_A = []
end_list_A = []
# code for #!defined HAVE_FINT_IS_INT
c_arg_list_B = []
code_list_B = []
end_list_B = []
need_ATTR_AINT = False
is_custom_fn = False # custom function body
need_skip_ierr = False
if re.match(r'MPI_((\w+)_(get|set)_attr|Attr_(put|get))', func['name'], re.IGNORECASE):
need_ATTR_AINT = True
elif re.match(r'MPI.*_(DUP|DELETE|COPY)_FN|MPI_CONVERSION_FN_NULL', func['name'], re.IGNORECASE):
is_custom_fn = True
elif re.match(r'MPI_F_sync_reg', func['name'], re.IGNORECASE):
is_custom_fn = True
if len(func['parameters']) > 0:
last_p = func['parameters'][-1]
if last_p['kind'] == 'ERROR_CODE' and re.search(r'c_parameter', last_p['suppress']):
need_skip_ierr = True
if re.match(r'MPI_Pcontrol', func['name'], re.IGNORECASE):
need_skip_ierr = True
# ---- internal functions --------------------
# input as MPI_Fint
def dump_p(p):
c_type = c_mapping[p['kind']]
if "length" in p and p['length']:
if re.match(r'mpi_i?neighbor_', func['name'], re.IGNORECASE):
if p['kind'] == "DISPLACEMENT":
# neighbor_alltoallw - sdispls and rdispls
dump_direct_pointer(p['name'], "MPI_Aint")
elif re.search(r'_allgatherv', func['name'], re.IGNORECASE):
dump_array_in(p['name'], c_type, "indegree")
elif re.match(r'(sendcounts|sdispls|sendtypes)', p['name'], re.IGNORECASE):
dump_array_in(p['name'], c_type, "indegree")
else:
dump_array_in(p['name'], c_type, "outdegree")
elif re.match(r'mpi_.*(gatherv|scatterv|alltoall[vw]|reduce_scatter)', func['name'], re.IGNORECASE):
dump_array_in(p['name'], c_type, "comm_size")
else:
raise Exception("Unhandled: %s - %s, length=%s" % (func['name'], p['name'], p['length']))
else:
c_param_list.append("MPI_Fint *" + p['name'])
c_arg_list_A.append("(%s) (*%s)" % (c_type, p['name']))
c_arg_list_B.append("(%s) (*%s)" % (c_type, p['name']))
def dump_scalar_out(v, f_type, c_type):
c_param_list.append("%s *%s" % (f_type, v))
c_arg_list_A.append("&%s_i" % v)
c_arg_list_B.append("&%s_i" % v)
code_list_common.append("%s %s_i;" % (c_type, v))
end_list_common.append("*%s = (%s) %s_i;" % (v, f_type, v))
# void *
def dump_buf(buf, check_in_place):
# void *
c_param_list.append("void *%s" % buf)
c_arg_list_A.append(buf)
c_arg_list_B.append(buf)
code_list_common.append("if (%s == MPIR_F_MPI_BOTTOM) {" % buf)
code_list_common.append(" %s = MPI_BOTTOM;" % buf)
if check_in_place:
code_list_common.append("} else if (%s == MPIR_F_MPI_IN_PLACE) {" % buf)
code_list_common.append(" %s = MPI_IN_PLACE;" % buf)
code_list_common.append("}")
code_list_common.append("")
# MPI_Status
def dump_status(v, is_in, is_out):
c_param_list.append("MPI_Fint *%s" % v)
def dump_FINT_is_INT_pre():
c_arg_list_A.append("(MPI_Status *) %s" % v)
if is_out:
code_list_A.append("if (%s == MPI_F_STATUS_IGNORE) {" % v)
code_list_A.append(" %s = (MPI_Fint *) MPI_STATUS_IGNORE;" % v)
code_list_A.append("}")
def dump_FINT_not_INT_pre():
c_arg_list_B.append("status_arg")
code_list_B.append("MPI_Status * status_arg;")
code_list_B.append("int status_i[MPI_F_STATUS_SIZE];")
if is_out:
code_list_B.append("if (%s == MPI_F_STATUS_IGNORE) {" % v)
code_list_B.append(" status_arg = MPI_STATUS_IGNORE;")
code_list_B.append("} else {")
# NOTE: always copy in because some fields (e.g. MPI_ERROR) may need be untouched
code_list_B.append("INDENT")
copy_status_in()
code_list_B.append("DEDENT")
code_list_B.append("}")
if is_in:
copy_status_in()
def dump_FINT_not_INT_post():
if is_out:
end_list_B.append("if (%s != MPI_F_STATUS_IGNORE) {" % v)
code_list_B.append("INDENT")
copy_status_out()
code_list_B.append("DEDENT")
end_list_B.append("}")
def copy_status_in():
code_list_B.append("status_arg = (MPI_Status *) status_i;")
code_list_B.append("for (int i = 0; i < MPI_F_STATUS_SIZE; i++) {")
code_list_B.append(" status_i[i] = (int) %s[i];" % v)
code_list_B.append("}")
def copy_status_out():
end_list_B.append("for (int i = 0; i < MPI_F_STATUS_SIZE; i++) {")
end_list_B.append(" %s[i] = (MPI_Fint) status_i[i];" % v)
end_list_B.append("}")
dump_FINT_is_INT_pre()
dump_FINT_not_INT_pre()
dump_FINT_not_INT_post()
# e.g. MPI_Status array_of_statuses[]
def dump_statuses(v, incount, outcount, is_in, is_out):
c_param_list.append("MPI_Fint *%s" % v)
def dump_FINT_is_INT_pre():
c_arg_list_A.append("(MPI_Status *) %s" % v)
if is_out:
code_list_A.append("if (%s == MPI_F_STATUSES_IGNORE) {" % v)
code_list_A.append(" %s = (MPI_Fint *) MPI_STATUSES_IGNORE;" % v)
code_list_A.append("}")
def dump_FINT_not_INT_pre():
c_arg_list_B.append("statuses_i")
code_list_B.append("MPI_Status *statuses_i;")
if is_out:
code_list_B.append("if (%s == MPI_F_STATUSES_IGNORE) {" % v)
code_list_B.append(" statuses_i = MPI_STATUS_IGNORE;")
code_list_B.append("} else {")
else:
code_list_B.append("{")
code_list_B.append("INDENT")
copy_statuses_in()
code_list_B.append("DEDENT")
code_list_B.append("}")
def dump_FINT_not_INT_post():
if is_out:
end_list_B.append("if (%s != MPI_F_STATUSES_IGNORE) {" % v)
end_list_B.append("INDENT")
copy_statuses_out()
end_list_B.append("DEDENT")
end_list_B.append("}")
else:
end_list_B.append("free(statuses_i);")
def copy_statuses_in():
code_list_B.append("statuses_i = malloc(%s * sizeof(MPI_Status));" % incount)
code_list_B.append("int *p = (int *) statuses_i;")
code_list_B.append("for (int i = 0; i < %s * MPI_F_STATUS_SIZE; i++) {" % incount)
code_list_B.append(" p[i] = (int) %s[i];" % v)
code_list_B.append("}")
def copy_statuses_out():
end_list_B.append("int *p = (int *) statuses_i;")
end_list_B.append("for (int i = 0; i < %s * MPI_F_STATUS_SIZE; i++) {" % outcount)
end_list_B.append(" %s[i] = (MPI_Fint) p[i];" % v)
end_list_B.append("}")
end_list_B.append("free(statuses_i);")
dump_FINT_is_INT_pre()
dump_FINT_not_INT_pre()
dump_FINT_not_INT_post()
# scalar int output, e.g. MPI_Request *req_out
def dump_int_out(v, c_type, is_inout):
c_param_list.append("MPI_Fint *%s" % v)
def dump_FINT_is_INT_pre():
c_arg_list_A.append("(%s *) %s" % (c_type, v))
def dump_FINT_not_INT_pre():
c_arg_list_B.append("&%s_i" % v)
code_list_B.append("%s %s_i;" % (c_type, v))
if is_inout:
code_list_B.append("%s_i = (%s) *%s;" % (v, c_type, v))
def dump_FINT_not_INT_post():
end_list_B.append("*%s = (MPI_Fint) %s_i;" % (v, v))
dump_FINT_is_INT_pre()
dump_FINT_not_INT_pre()
dump_FINT_not_INT_post()
# scalar integer input, but we assume Fortran use the same c_type
def dump_c_type_in(v, c_type):
c_param_list.append("%s *%s" % (c_type, v))
c_arg_list_A.append('*' + v)
c_arg_list_B.append('*' + v)
# e.g. MPI_Address
def dump_aint_out_check(v):
c_param_list.append("MPI_Fint *%s" % v)
c_arg_list_A.append("&%s_i" % v)
c_arg_list_B.append("&%s_i" % v)
code_list_common.append("MPI_Aint %s_i;" % v)
end_list_common.append("*%s = (MPI_Fint) %s_i;" % (v, v))
end_list_common.append("if ((MPI_Aint) (*%s) != %s_i) {" % (v, v))
end_list_common.append(" /* Unfortunately, there isn't an easy way to invoke error handler */")
end_list_common.append(" *ierr = MPI_ERR_OTHER;")
end_list_common.append("}")
# arrays, e.g. MPI_Request array_of_reqs[]
# NOTE: we also handle MPI_UNWEIGHTED, MPI_WEIGHTS_EMPTY
def dump_array_in(v, c_type, count):
dump_array(v, c_type, count, count, True, False)
def dump_array_out(v, c_type, count):
dump_array(v, c_type, count, count, False, True)
def dump_array_special_in(v, c_type, count):
dump_array(v, c_type, count, count, True, False, True)
def dump_array_special_out(v, c_type, count):
dump_array(v, c_type, count, count, False, True, True)
def dump_array(v, c_type, incount, outcount, is_in=True, is_out=False, has_special=False):
c_param_list.append("MPI_Fint *%s" % v)
def dump_FINT_is_INT_pre():
c_arg_list_A.append("(int *) %s" % v)
dump_check_special(True)
def dump_FINT_not_INT_pre():
c_arg_list_B.append("%s_i" % v)
code_list_B.append("%s *%s_i;" % (c_type, v))
dump_check_special(False)
dump_check_special_begin(code_list_B)
code_list_B.append("%s_i = malloc(sizeof(%s) * %s);" % (v, c_type, incount))
if is_in:
code_list_B.append("for (int i = 0; i < %s; i++) {" % incount)
code_list_B.append(" %s_i[i] = (%s) %s[i];" % (v, c_type, v))
code_list_B.append("}")
dump_check_special_end(code_list_B)
def dump_FINT_not_INT_post():
dump_check_special_begin(end_list_B)
if is_out:
end_list_B.append("for (int i = 0; i < %s; i++) {" % outcount)
if v == 'periods':
# hack for logical
end_list_B.append(" %s[i] = MPII_TO_FLOG(%s_i[i]);" % (v, v))
else:
end_list_B.append(" %s[i] = (MPI_Fint) %s_i[i];" % (v, v))
end_list_B.append("}")
end_list_B.append("free(%s_i);" % v)
dump_check_special_end(end_list_B)
def dump_check_special(FINT_is_INT):
def check_weights():
if FINT_is_INT:
code_list_A.append("if (%s == MPIR_F_MPI_UNWEIGHTED) {" % v)
code_list_A.append(" %s = (MPI_Fint *) MPI_UNWEIGHTED;" % v)
code_list_A.append("} else if (%s == MPIR_F_MPI_WEIGHTS_EMPTY) {" % v)
code_list_A.append(" %s = (MPI_Fint *) MPI_WEIGHTS_EMPTY;" % v)
code_list_A.append("}")
else:
code_list_B.append("int %s_is_special = 0;" % v)
code_list_B.append("if (%s == MPIR_F_MPI_UNWEIGHTED) {" % v)
code_list_B.append(" %s_i = MPI_UNWEIGHTED;" % v)
code_list_B.append(" %s_is_special = 1;" % v)
code_list_B.append("} else if (%s == MPIR_F_MPI_WEIGHTS_EMPTY) {" % v)
code_list_B.append(" %s_i = MPI_WEIGHTS_EMPTY;" % v)
code_list_B.append(" %s_is_special = 1;" % v)
code_list_B.append("}")
def check_errcodes():
if FINT_is_INT:
code_list_A.append("if (%s == MPI_F_ERRCODES_IGNORE) {" % v)
code_list_A.append(" %s = (MPI_Fint *) MPI_ERRCODES_IGNORE;" % v)
code_list_A.append("}")
else:
code_list_B.append("int %s_is_special = 0;" % v)
code_list_B.append("if (%s == MPI_F_ERRCODES_IGNORE) {" % v)
code_list_B.append(" %s_i = MPI_ERRCODES_IGNORE;" % v)
code_list_B.append(" %s_is_special = 1;" % v)
code_list_B.append("}")
if has_special:
if is_in and re.match(r'(source|dest)?weights$', v):
check_weights()
elif is_out and re.match(r'.*_errcodes$', v):
check_errcodes()
else:
raise Exception("Unhandled special parameter: %s - %s" % (func['name'], v))
def dump_check_special_begin(code_list):
if has_special:
code_list.append("if (!%s_is_special) {" % v)
code_list.append("INDENT")
def dump_check_special_end(code_list):
if has_special:
code_list.append("DEDENT")
code_list.append("}")
dump_FINT_is_INT_pre()
dump_FINT_not_INT_pre()
dump_FINT_not_INT_post()
def dump_array_int_to_aint(v, count):
c_param_list.append("MPI_Fint *%s" % v)
c_arg_list_A.append("%s_i" % v)
c_arg_list_B.append("%s_i" % v)
code_list_common.append("MPI_Aint *%s_i;" % v)
code_list_common.append("#ifdef HAVE_AINT_DIFFERENT_THAN_FINT")
code_list_common.append("%s_i = malloc(%s * sizeof(MPI_Aint));" % (v, count))
code_list_common.append("for (int i = 0; i < %s; i++) {" % count)
code_list_common.append(" %s_i[i] = (MPI_Aint) %s[i];" % (v, v))
code_list_common.append("}")
code_list_common.append("#else")
code_list_common.append("%s_i = %s;" % (v, v))
code_list_common.append("#endif")
end_list_common.append("#ifdef HAVE_AINT_DIFFERENT_THAN_FINT")
end_list_common.append("free(%s_i);" % v)
end_list_common.append("#endif")
def dump_string_in(v):
c_param_list.append("char *%s FORT_MIXED_LEN(%s_len)" % (v, v))
c_param_list_end.append("FORT_END_LEN(%s_len)" % v)
c_arg_list_A.append("%s_i" % v)
c_arg_list_B.append("%s_i" % v)
code_list_common.append("char *%s_i = MPIR_fort_dup_str(%s, %s_len);" % (v, v, v))
end_list_common.append("free(%s_i);" % (v))
def dump_string_out(v, flag=None):
c_param_list.append("char *%s FORT_MIXED_LEN(%s_len)" % (v, v))
c_param_list_end.append("FORT_END_LEN(%s_len)" % v)
c_arg_list_A.append("%s_i" % v)
c_arg_list_B.append("%s_i" % v)
code_list_common.append("char *%s_i = malloc(%s_len + 1);" % (v, v))
flag_cond = "*ierr == MPI_SUCCESS"
if flag:
flag_cond += " && " + flag
end_list_common.append("if (%s) {" % flag_cond)
end_list_common.append("INDENT")
end_list_common.append("MPIR_fort_copy_str_from_c(%s, %s_len, %s_i);" % (v, v, v))
end_list_common.append("DEDENT")
end_list_common.append("}")
end_list_common.append("free(%s_i);" % v)
def dump_string_array(v, count="0"):
c_param_list.append("char *%s FORT_MIXED_LEN(%s_len)" % (v, v))
c_param_list_end.append("FORT_END_LEN(%s_len)" % v)
c_arg_list_A.append("%s_i" % v)
c_arg_list_B.append("%s_i" % v)
code_list_common.append("char **%s_i;" % v)
code_list_common.append("if (%s == MPI_F_ARGV_NULL) {" % v)
code_list_common.append(" %s_i = MPI_ARGV_NULL;" % v)
code_list_common.append("} else {")
code_list_common.append(" %s_i = MPIR_fort_dup_str_array(%s, %s_len, %s_len, %s);" % (v, v, v, v, count))
code_list_common.append("}")
end_list_common.append("if (%s != MPI_F_ARGV_NULL) {" % v)
end_list_common.append(" MPIR_fort_free_str_array(%s_i);" % v)
end_list_common.append("}")
def dump_string_2darray(v, count):
c_param_list.append("char *%s FORT_MIXED_LEN(%s_len)" % (v, v))
c_param_list_end.append("FORT_END_LEN(%s_len)" % v)
c_arg_list_A.append("%s_i" % v)
c_arg_list_B.append("%s_i" % v)
code_list_common.append("char ***%s_i;" % v)
code_list_common.append("if (%s == MPI_F_ARGVS_NULL) {" % v)
code_list_common.append(" %s_i = MPI_ARGVS_NULL;" % v)
code_list_common.append("} else {")
code_list_common.append(" %s_i = MPIR_fort_dup_str_2d_array(%s, %s_len, %s);" % (v, v, v, count))
code_list_common.append("}")
end_list_common.append("if (%s != MPI_F_ARGVS_NULL) {" % v)
end_list_common.append(" MPIR_fort_free_str_2d_array(%s_i, %s);" % (v, count))
end_list_common.append("}")
def dump_file_in(v):
c_param_list.append("MPI_Fint *%s" % v)
c_arg_list_A.append("MPI_File_f2c(*%s)" % v)
c_arg_list_B.append("MPI_File_f2c(*%s)" % v)
def dump_file_out(v, is_inout):
c_param_list.append("MPI_Fint *%s" % v)
c_arg_list_A.append("&%s_i" % v)
c_arg_list_B.append("&%s_i" % v)
if is_inout:
code_list_common.append("MPI_File %s_i = MPI_File_f2c(*%s);" % (v, v))
else:
code_list_common.append("MPI_File %s_i;" % v)
end_list_common.append("*%s = MPI_File_c2f(%s_i);" % (v, v))
def dump_logical_in(v):
c_param_list.append("MPI_Fint *%s" % v)
c_arg_list_A.append("*%s" % v)
c_arg_list_B.append("*%s" % v)
def dump_logical_out(v):
c_param_list.append("MPI_Fint *%s" % v)
c_arg_list_A.append("&%s_i" % v)
c_arg_list_B.append("&%s_i" % v)
code_list_common.append("int %s_i;" % v)
end_list_common.append("if (*ierr == MPI_SUCCESS) {")
end_list_common.append(" *%s = MPII_TO_FLOG(%s_i);" % (v, v))
end_list_common.append("}")
def dump_index_in(v):
c_param_list.append("MPI_Fint *%s" % v)
c_arg_list_A.append("(int) (*%s - 1)" % v)
c_arg_list_B.append("(int) (*%s - 1)" % v)
def dump_index_out(v):
c_param_list.append("MPI_Fint *%s" % v)
c_arg_list_A.append("&%s_i" % v)
c_arg_list_B.append("&%s_i" % v)
code_list_common.append("int %s_i;" % v)
end_list_common.append("if (*ierr == MPI_SUCCESS) {")
end_list_common.append(" if (%s_i == MPI_UNDEFINED) {" % v)
end_list_common.append(" *%s = %s_i;" % (v, v))
end_list_common.append(" } else {")
end_list_common.append(" *%s = %s_i + 1;" % (v, v))
end_list_common.append(" }")
end_list_common.append("}")
def dump_string_len_inout(v):
# only used in MPI_Info_get_string
c_param_list.append("MPI_Fint *%s" % v)
c_arg_list_A.append("&%s_i" % v)
c_arg_list_B.append("&%s_i" % v)
code_list_common.append("int %s_i = (*%s > 0) ? (int) (*%s + 1) : 0;" % (v, v, v))
end_list_common.append("*%s = (%s_i > 0) ? (MPI_Fint) (%s_i - 1) : 0;" % (v, v, v))
def dump_index_array_out(v, incount, outcount):
dump_array(v, 'int', incount, outcount, False, True)
end_list_common.append("for (int i = 0; i < %s; i++) {" % outcount)
end_list_common.append(" %s[i] += 1;" % v)
end_list_common.append("}")
def dump_function(v, func_type):
c_param_list.append("%s %s" % (func_type, v))
c_arg_list_A.append(v)
c_arg_list_B.append(v)
if func_type == "MPI_Datarep_conversion_function":
# FIXME: check name mangling
code_list_common.append("if (%s == (MPI_Datarep_conversion_function *) mpi_conversion_fn_null_) {" % v)
code_list_common.append(" %s = NULL;" % v)
code_list_common.append("}")
def dump_direct_pointer(v, c_type):
c_param_list.append("%s *%s" % (c_type, v))
c_arg_list_A.append(v)
c_arg_list_B.append(v)
def dump_attr_in(v, c_type):
c_param_list.append("%s *%s" % (c_type, v))
c_arg_list_A.append("(void *) (intptr_t) (*%s)" % v)
c_arg_list_B.append("(void *) (intptr_t) (*%s)" % v)
def dump_attr_out(v, c_type, flag):
c_param_list.append("%s *%s" % (c_type, v))
c_arg_list_A.append("&%s_i" % v)
c_arg_list_B.append("&%s_i" % v)
code_list_common.append("void *%s_i;" % v)
end_list_common.append("if (*ierr || !%s) {" % flag)
end_list_common.append(" *%s = 0;" % v)
end_list_common.append("} else {")
end_list_common.append(" *%s = (MPI_Aint) %s_i;" % (v, v))
end_list_common.append("}")
def dump_handle_create(v, c_type):
c_param_list.append("MPI_Fint *%s" % v)
# note: when fint is int, both the handle and the handler interface are compatible with C
c_arg_list_A.append("(%s *) %s" % (c_type, v))
c_arg_list_B.append("&%s_i" % v)
code_list_B.append("int %s_i;" % v)
end_list_B.append("if (!*ierr) {")
if c_type == "MPI_Errhandler":
end_list_B.append(" MPII_Errhandler_set_fc(%s_i);" % v)
elif c_type == "MPI_Op":
end_list_B.append(" MPII_Op_set_fc(%s_i);" % v)
end_list_B.append(" *%s = (MPI_Fint) %s_i;" % (v, v))
end_list_B.append("}")
def dump_sum(codelist, sum_v, sum_n, array):
codelist.append("int %s = 0;" % sum_v)
codelist.append("for (int i = 0; i < *%s; i++) {" % sum_n)
codelist.append(" %s += (int) %s[i];" % (sum_v, array))
codelist.append("}")
def dump_mpi_decl_begin(name, param_str, return_type):
G.out.append("FORT_DLL_SPEC %s FORT_CALL %s(%s) {" % (return_type, name, param_str))
G.out.append("INDENT")
if re.match(r'MPI_File_|MPI_Register_datarep', func['name'], re.IGNORECASE):
G.out.append("#ifndef MPI_MODE_RDONLY")
G.out.append("*ierr = MPI_ERR_INTERN;")
G.out.append("#else")
def dump_mpi_decl_end():
if re.match(r'MPI_File_|MPI_Register_datarep', func['name'], re.IGNORECASE):
G.out.append("#endif")
G.out.append("DEDENT")
G.out.append("}")
def dump_custom_functions(func):
# assume the last parameter is ierror -- forum don't name it consistently
err = func['parameters'][-1]['name']
if re.match(r'MPI_CONVERSION_FN_NULL', func['name'], re.IGNORECASE):
G.out.append("/* dummy function, not callable */")
G.out.append("return 0;")
elif re.match(r'MPI.*_DUP_FN', func['name'], re.IGNORECASE):
G.out.append("* (MPI_Aint *) attribute_val_out = * (MPI_Aint *) attribute_val_in;")
G.out.append("*flag = MPII_TO_FLOG(1);")
G.out.append("*%s = MPI_SUCCESS;" % err)
elif re.match(r'MPI.*_NULL_COPY_FN', func['name'], re.IGNORECASE):
G.out.append("*flag = MPII_TO_FLOG(0);")
G.out.append("*%s = MPI_SUCCESS;" % err)
elif re.match(r'MPI.*_NULL_DELETE_FN', func['name'], re.IGNORECASE):
G.out.append("*%s = MPI_SUCCESS;" % err)
elif re.match(r'MPI_F_sync_reg', func['name'], re.IGNORECASE):
G.out.append("*ierr = MPI_SUCCESS;")
else:
raise Exception("Unhandled dummy function - %s" % func['name'])
# ----------------------------------
def process_func_parameters():
n = len(func['parameters'])
i = 0
while i < n:
p = func['parameters'][i]
if p['large_only']:
i += 1
continue
(group_kind, group_count) = ("", 0)
if i + 3 <= n and RE.search(r'BUFFER', p['kind']):
group_kind, group_count = get_userbuffer_group(func['name'], func['parameters'], i)
if group_count > 0:
p2 = func['parameters'][i + 1]
p3 = func['parameters'][i + 2]
if group_count == 3:
dump_buf(p['name'], re.search('-inplace', group_kind))
dump_p(p2)
dump_p(p3)
elif group_count == 4:
# e.g. Alltoallv
p4 = func['parameters'][i + 3]
dump_buf(p['name'], re.search('-inplace', group_kind))
dump_p(p2)
dump_p(p3)
dump_p(p4)
elif group_count == 5:
# reduce
p4 = func['parameters'][i + 3]
p5 = func['parameters'][i + 4]
dump_buf(p['name'], True)
dump_buf(p2['name'], False)
dump_p(p3)
dump_p(p4)
dump_p(p5)
i += group_count
continue
else:
i += 1
if f90_param_need_skip(p):
pass;
elif p['kind'] == "BUFFER":
dump_buf(p['name'], False)
elif p['kind'] == "STRING":
if p['param_direction'] == 'out':
if re.match(r'MPI_Info_get$', func['name'], re.IGNORECASE):
dump_string_out(p['name'], '*flag')
elif re.match(r'MPI_Info_get_string$', func['name'], re.IGNORECASE):
code_list_common.append("int buflen_nonzero = (*buflen > 0);")
dump_string_out(p['name'], '(*flag && buflen_nonzero)')
else:
dump_string_out(p['name'])
else:
dump_string_in(p['name'])
elif p['kind'] == "STRING_ARRAY":
if p['name'] == 'array_of_commands':
dump_string_array(p['name'], "*count")
else:
dump_string_array(p['name'])
elif p['kind'] == "STRING_2DARRAY":
if re.match(r'MPI_Comm_spawn_multiple$', func['name'], re.IGNORECASE):
dump_string_2darray(p['name'], "*count")
else:
raise Exception("Not expected: %s - %s" % (func['name'], p['name']))
elif p['kind'] == "STATUS":
if p['param_direction'] == 'out':
if p['length'] is None:
dump_status(p['name'], False, True)
elif RE.match(r'mpix?_(wait|test)all', func['name'], re.IGNORECASE):
dump_statuses(p['name'], "(*count)", "(*count)", False, True)
elif RE.match(r'mpix?_(wait|test)some', func['name'], re.IGNORECASE):
dump_statuses(p['name'], "(*incount)", "(*outcount)", False, True)
else:
raise Exception("Unhandled: %s - %s" % (func['name'], p['name']))
else:
if p['length'] is None:
dump_status(p['name'], True, False)
else:
raise Exception("Unhandled: %s - %s" % (func['name'], p['name']))
elif RE.match(r'(source|dest)?weights$', p['name']):
if RE.match(r'MPI_Dist_graph_create$', func['name'], re.IGNORECASE):
# NOTE: total_degrees is pre-calculated
dump_array_special_in(p['name'], "int", "total_degrees")
elif RE.match(r'MPI_Dist_graph_create_adjacent$', func['name'], re.IGNORECASE):
if p['name'] == 'sourceweights':
dump_array_special_in(p['name'], "int", "(*indegree)")
else:
dump_array_special_in(p['name'], "int", "(*outdegree)")
elif RE.match(r'mpi_dist_graph_neighbors$', func['name'], re.IGNORECASE):
if p['name'] == 'sourceweights':
dump_array_out(p['name'], "int", "(*maxindegree)")
else:
dump_array_out(p['name'], "int", "(*maxoutdegree)")
else:
raise Exception("Not handled: %s - %s" % (func['name'], p['name']))
elif RE.match(r'array_of_errcodes', p['name']):
if re.match(r'MPI_Comm_spawn$', func['name'], re.IGNORECASE):
dump_array_special_out(p['name'], "int", "(*maxprocs)")
elif re.match(r'MPI_Comm_spawn_multiple$', func['name'], re.IGNORECASE):
# NOTE: total_procs is pre-calculated
dump_array_special_out(p['name'], "int", "total_procs")
else:
raise Exception("Not handled: %s - %s" % (func['name'], p['name']))
elif p['kind'] == "FILE":
if p['param_direction'] == 'out':
dump_file_out(p['name'], False)
elif p['param_direction'] == 'inout':
dump_file_out(p['name'], True)
else:
dump_file_in(p['name'])
elif p['kind'] == "LOGICAL":
if re.match(r'MPI_Cart_sub$', func['name'], re.IGNORECASE):
# remain_dims
dump_array_in(p['name'], 'int', "maxdims")
elif p['length']:
# assume input (MPI_Cart_create - periods)
if p['length'] == '*':
raise Exception("Unhandled: %s - %s" % (func['name'], p['name']))
count = "(*%s)" % p['length']
dump_array(p['name'], 'int', count, count, param_is_in(p), param_is_out(p))
elif p['param_direction'] == 'out':
dump_logical_out(p['name'])
else:
dump_logical_in(p['name'])
elif p['kind'] == "INDEX":
# tricky, not all need to be 1-based
if p['length']:
if re.match(r'MPI_(Test|Wait)some', func['name'], re.IGNORECASE):
dump_index_array_out(p['name'], '(*incount)', '(*outcount)')
elif re.match(r'MPI_Graph_get', func['name'], re.IGNORECASE):
dump_array_out(p['name'], 'int', '(*maxindex)')
elif re.match(r'MPI_Graph_(create|map)', func['name'], re.IGNORECASE):
dump_array_in(p['name'], 'int', "(*%s)" % p['length'])
else:
raise Exception("Unhandled: %s - %s" % (func['name'], p['name']))
elif p['param_direction'] == 'out':
dump_index_out(p['name'])
elif p['name'] == "direction": # MPI_Cart_shift
dump_p(p)
else:
dump_index_in(p['name'])
elif re.match(r'FUNCTION|FUNCTION_SMALL|POLYFUNCTION', p['kind']):
dump_function(p['name'], p['func_type'])
elif re.match(r'EXTRA_STATE|C_BUFFER2|C_BUFFER', p['kind']):
if p['param_direction'] == 'out':
dump_scalar_out(p['name'], "MPI_Aint", "void *")
elif p['param_direction'] != "inout":
dump_direct_pointer(p['name'], "void")
else:
raise Exception("Unhandled: %s - %s" % (func['name'], p['name']))
elif re.match(r'ATTRIBUTE_VAL', p['kind']):
if re.match(r'MPI_((Comm|Type|Win)_get_attr)', func['name'], re.IGNORECASE):
dump_attr_out(p['name'], "MPI_Aint", "flag_i")
elif re.match(r'MPI_((Comm|Type|Win)_set_attr)', func['name'], re.IGNORECASE):
dump_attr_in(p['name'], "MPI_Aint")
elif re.match(r'MPI_Attr_get', func['name'], re.IGNORECASE):
dump_attr_out(p['name'], "MPI_Fint", "flag_i")
elif re.match(r'MPI_Attr_put', func['name'], re.IGNORECASE):
dump_attr_in(p['name'], "MPI_Fint")
elif is_custom_fn:
# Must be e.g. MPI_DUP_FN. We'll special treat them
c_param_list.append("void *%s" % p['name'])
pass
else:
raise Exception("Unhandled: func %s - %s" % (func['name'], p['name']))
elif re.match(r'MPI_(Aint|Count|Offset)', c_mapping[p['kind']]):
c_type = c_mapping[p['kind']]
if re.match(r'MPI_Type_(extent|lb|ub)', func['name'], re.IGNORECASE):
# old function, output as INTEGER
dump_scalar_out(p['name'], "MPI_Fint", "MPI_Aint")
elif re.match(r'MPI_Type_hvector', func['name'], re.IGNORECASE):
# old function, input as INTEGER
dump_p(p)
elif re.match(r'MPI_Type_(hindexed|struct)', func['name'], re.IGNORECASE):
# old function, input as INTEGER array
dump_array_int_to_aint(p['name'], '(*count)')
elif p['length']:
dump_direct_pointer(p['name'], c_type)
elif p['param_direction'] == 'out' or p['param_direction'] == 'inout':
if re.match(r'MPI_Address', func['name'], re.IGNORECASE):
dump_aint_out_check(p['name'])
else:
dump_direct_pointer(p['name'], c_type)
else:
dump_c_type_in(p['name'], c_type)
elif p['kind'] == "ERRHANDLER" and re.match(r'.*_create_errhandler', func['name'], re.IGNORECASE):
dump_handle_create(p['name'], "MPI_Errhandler")
elif p['kind'] == "OPERATION" and re.match(r'.*_op_create$', func['name'], re.IGNORECASE):
dump_handle_create(p['name'], "MPI_Op")
elif p['kind'] in G.handle_mpir_types or c_mapping[p['kind']] == "int":
c_type = c_mapping[p['kind']]
if p['length'] is None:
if p['param_direction'] == 'out':
dump_int_out(p['name'], c_type, False)
if p['kind'] == "KEYVAL":
end_list_common.append("if (!*ierr) {")
end_list_common.append(" MPII_Keyval_set_f90_proxy((int) *%s);" % p['name'])
end_list_common.append("}")
elif re.match(r'MPI_Grequest_start', func['name'], re.IGNORECASE):
end_list_common.append("if (!*ierr) {")
end_list_common.append(" MPII_Grequest_set_lang_f77((int) *%s);" % p['name'])
end_list_common.append("}")
elif p['param_direction'] == 'inout' or p['pointer']:
if re.match(r'MPI_Info_get_string$', func['name'], re.IGNORECASE):
dump_string_len_inout(p['name'])
else:
dump_int_out(p['name'], c_type, True)
else:
dump_p(p)
elif isinstance(p['length'], list):
if re.match(r'MPI_Group_range_(incl|excl)', func['name'], re.IGNORECASE):
c_param_list.append("MPI_Fint *%s" % p['name'])
c_arg_list_A.append("(int (*)[3]) %s" % p['name'])
c_arg_list_B.append("(int (*)[3]) %s" % p['name'])
else:
raise Exception("unhandled length: %s - %s" % (func['name'], p['name']))
else:
# FIXME: find length
count = p['length']
if count == '*':
if re.match(r'MPI_Dist_graph_create$', func['name'], re.IGNORECASE):
count = "total_degrees"
elif re.match(r'MPI_Cart_rank$', func['name'], re.IGNORECASE):
count = "maxdims"
elif re.match(r'MPI_Graph_(create|map)$', func['name'], re.IGNORECASE):
count = "indx[*nnodes - 1]"
elif re.match(r'MPI_(Wait|Test)some$', func['name'], re.IGNORECASE):
count = "(*incount)"
else:
raise Exception("Unhandled array[*]: %s - %s" % (func['name'], p['name']))
else:
count = "(*%s)" % count
if p['param_direction'] == 'out':
dump_array_out(p['name'], c_type, count)
elif p['param_direction'] == 'inout':
dump_array(p['name'], c_type, count, count, True, True)
else:
dump_array_in(p['name'], c_type, count)
else:
raise Exception("Unhandled: func %s - %s, kind = %s" % (func['name'], p['name'], p['kind']))
# -----------------------------------------------------------
# pre-process, such as pre-calculate total dimensions
if re.match(r'MPI_Dist_graph_create$', func['name'], re.IGNORECASE):
dump_sum(code_list_B, "total_degrees", "n", "degrees")
elif re.match(r'MPI_Comm_spawn_multiple$', func['name'], re.IGNORECASE):
dump_sum(code_list_B, "total_procs", "count", "array_of_maxprocs")
elif re.match(r'MPI_Cart_(rank|sub)$', func['name'], re.IGNORECASE):
code_list_B.append("int maxdims;")
code_list_B.append("MPI_Cartdim_get((MPI_Comm) (*comm), &maxdims);")
elif re.match(r'mpi_i?neighbor_(.*[vw])(_init)?$', func['name'], re.IGNORECASE):
code_list_B.append("int indegree, outdegree, weighted;")
code_list_B.append("MPI_Dist_graph_neighbors_count((MPI_Comm) (*comm), &indegree, &outdegree, &weighted);")
elif re.match(r'mpi_.*(reduce_scatter)', func['name'], re.IGNORECASE):
code_list_B.append("int comm_size;")
code_list_B.append("MPI_Comm_size((MPI_Comm) (*comm), &comm_size);")
elif re.match(r'mpi_.*(gatherv|scatterv|alltoall[vw])', func['name'], re.IGNORECASE):
code_list_B.append("int is_inter;")
code_list_B.append("int comm_size;")
code_list_B.append("MPI_Comm_test_inter((MPI_Comm) (*comm), &is_inter);")
code_list_B.append("if (is_inter) {")
code_list_B.append(" MPI_Comm_remote_size((MPI_Comm) (*comm), &comm_size);")
code_list_B.append("} else {")
code_list_B.append(" MPI_Comm_size((MPI_Comm) (*comm), &comm_size);")
code_list_B.append("}")
process_func_parameters()
c_func_name = func_name
if need_ATTR_AINT:
if RE.match(r'MPI_Attr_(get|put)', func['name'], re.IGNORECASE):
if RE.m.group(1) == 'put':
c_func_name = "MPII_Comm_set_attr"
else:
c_func_name = "MPII_Comm_get_attr"
c_arg_list_A.append("MPIR_ATTR_INT")
c_arg_list_B.append("MPIR_ATTR_INT")
else:
c_func_name = re.sub(r'MPI_', 'MPII_', func['name'])
c_arg_list_A.append("MPIR_ATTR_AINT")
c_arg_list_B.append("MPIR_ATTR_AINT")
elif re.match(r'MPI_(Init|Init_thread|Info_create_env)$', func['name'], re.IGNORECASE):
# argc, argv
c_arg_list_A.insert(0, "0, 0")
c_arg_list_B.insert(0, "0, 0")
if re.match(r'MPI_CONVERSION_FN_NULL', func['name'], re.IGNORECASE):
param_str = "void *userbuf, MPI_Datatype datatype, int count, void *filebuf, MPI_Offset position, void *extra_state"
return_type = "int"
elif 'return' not in func:
if not need_skip_ierr:
if c_param_list:
param_str = ', '.join(c_param_list) + ", MPI_Fint *ierr"
else:
param_str = "MPI_Fint *ierr"
else:
param_str = ', '.join(c_param_list)
return_type = "void"
else:
if not c_param_list:
param_str = "void"
else:
param_str = ', '.join(c_param_list)
return_type = c_mapping[func['return']]
if c_param_list_end:
param_str += ' ' + ' '.join(c_param_list_end)
use_name = dump_profiling(func_name, param_str, return_type)
G.out.append("")
dump_mpi_decl_begin(use_name, param_str, return_type)
if is_custom_fn:
dump_custom_functions(func)
elif re.match(r'MPI_Pcontrol', func['name'], re.IGNORECASE):
G.out.append("%s(%s);" % (c_func_name, ', '.join(c_arg_list_A)))
elif return_type != "void":
# these are all special functions, e.g. MPI_Wtime, MPI_Wtick, MPI_Aint_add, MPI_Aint_diff
G.out.append("return %s(%s);" % (c_func_name, ', '.join(c_arg_list_A)))
else:
# FIXME: need check name mangling before call mpirinitf_()
G.out.append("if (MPIR_F_NeedInit) {mpirinitf_(); MPIR_F_NeedInit = 0;}")
G.out.append("")
for l in code_list_common:
G.out.append(l)
if code_list_B:
G.out.append("#ifdef HAVE_FINT_IS_INT")
for l in code_list_A:
G.out.append(l)
G.out.append("*ierr = %s(%s);" % (c_func_name, ', '.join(c_arg_list_A)))
for l in end_list_A:
G.out.append(l)
if code_list_B:
G.out.append("")
G.out.append("#else /* ! HAVE_FINT_IS_INT */")
for l in code_list_B:
G.out.append(l)
G.out.append("*ierr = %s(%s);" % (c_func_name, ', '.join(c_arg_list_B)))
for l in end_list_B:
G.out.append(l)
G.out.append("#endif")
for l in end_list_common:
G.out.append(l)
dump_mpi_decl_end()
def dump_f77_c_file(f, lines):
print(" --> [%s]" % f)
with open(f, "w") as Out:
for l in G.copyright_c:
print(l, file=Out)
if f.endswith('.c'):
print("#include \"mpi_fortimpl.h\"", file=Out)
print("#include \"fortran_profile.h\"", file=Out)
print("", file=Out)
indent = 0
for l in lines:
if RE.match(r'#(if|elif|else|endif|define)', l):
print(l, file=Out)
elif RE.match(r'INDENT', l):
indent += 1
elif RE.match(r'DEDENT', l):
indent -= 1
else:
if indent > 0:
print(" " * indent, end='', file=Out)
print(l, file=Out)
#----------------------------------------
def dump_profiling(name, param_str, return_type):
pname = "P" + name
defines = ["F77_NAME_UPPER", "F77_NAME_LOWER", "F77_NAME_LOWER_USCORE", "F77_NAME_LOWER_2USCORE"]
names = [name.upper(), name.lower(), name.lower() + "_", name.lower() + "__"]
pnames = [pname.upper(), pname.lower(), pname.lower() + "_", pname.lower() + "__"]
# use e.g. mpi_send_ to define the function
use_idx = 2
def dump_multiple_pragma_weak(use_only_mpi_names):
for nm in names:
dump_mpi_decl(nm, param_str)
G.profile_out.append("")
for i in range(len(names)):
G.profile_out.append("#%s defined(%s)" % (get_if_or_elif(i), defines[i]))
for j in range(len(names)):
if use_only_mpi_names:
if i == j:
continue
G.profile_out.append("#pragma weak %s = %s" % (names[j], names[i]))
else:
G.profile_out.append("#pragma weak %s = %s" % (names[j], pnames[i]))
G.profile_out.append("#else")
G.profile_out.append("#error missing F77 name mangling")
G.profile_out.append("#endif")
def dump_elif_pragma_weak(have_pragma):
G.profile_out.append("#elif defined(%s)" % have_pragma)
for i in range(len(names)):
G.profile_out.append("#%s defined(%s)" % (get_if_or_elif(i), defines[i]))
if have_pragma == "HAVE_PRAGMA_WEAK":
dump_mpi_decl(names[i], param_str)
G.profile_out.append("#pragma weak %s = %s" % (names[i], pnames[i]))
elif have_pragma == "HAVE_PRAGMA_HP_SEC_DEF":
G.profile_out.append("#pragma _HP_SECONDARY_DEF %s %s" % (pnames[i], names[i]))
elif have_pragma == "HAVE_PRAGMA_CRI_DUP":
G.profile_out.append("#pragma _CRI duplicate %s as %s" % (names[i], pnames[i]))
G.profile_out.append("#endif")
G.profile_out.append("")
def dump_weak_attribute(use_only_mpi_names):
for i in range(len(names)):
G.profile_out.append("#%s defined(%s)" % (get_if_or_elif(i), defines[i]))
for j in range(len(names)):
if use_only_mpi_names:
if i == j:
dump_mpi_decl(names[i], param_str)
else:
dump_mpi_decl_weak_attr(names[j], param_str, names[i])
else:
dump_mpi_decl_weak_attr(names[j], param_str, pnames[i])
G.profile_out.append("#else")
G.profile_out.append("#error missing F77 name mangling")
G.profile_out.append("#endif")
def dump_pmpi_decl_multiple_pragma_weak():
for i in range(len(names)):
G.profile_out.append("#ifndef %s" % defines[i])
dump_mpi_decl(pnames[i], param_str)
G.profile_out.append("#endif")
for i in range(len(names)):