-
Notifications
You must be signed in to change notification settings - Fork 21
/
wrap.py
executable file
·1361 lines (1157 loc) · 53.8 KB
/
wrap.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
#!/usr/bin/env python
#################################################################################################
# Copyright (c) 2010, Lawrence Livermore National Security, LLC.
# Produced at the Lawrence Livermore National Laboratory
# Written by Todd Gamblin, tgamblin@llnl.gov.
# LLNL-CODE-417602
# All rights reserved.
#
# This file is part of Libra. For details, see http://github.com/tgamblin/libra.
# Please also read the LICENSE file for further information.
#
# Redistribution and use in source and binary forms, with or without modification, are
# permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this list of
# conditions and the disclaimer below.
# * Redistributions in binary form must reproduce the above copyright notice, this list of
# conditions and the disclaimer (as noted below) in the documentation and/or other materials
# provided with the distribution.
# * Neither the name of the LLNS/LLNL nor the names of its contributors may be used to endorse
# or promote products derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
# LAWRENCE LIVERMORE NATIONAL SECURITY, LLC, THE U.S. DEPARTMENT OF ENERGY OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#################################################################################################
from __future__ import print_function
usage_string = \
'''Usage: wrap.py [-fgd] [-i pmpi_init] [-c mpicc_name] [-o file] wrapper.w [...]
Python script for creating PMPI wrappers. Roughly follows the syntax of
the Argonne PMPI wrapper generator, with some enhancements.
Options:"
-d Just dump function declarations parsed out of mpi.h
-f Generate fortran wrappers in addition to C wrappers.
-g Generate reentry guards around wrapper functions.
-s Skip writing #includes, #defines, and other front-matter (for non-C output).
-c exe Provide name of MPI compiler (for parsing mpi.h). Default is \'mpicc\'.
-I dir Provide an extra include directory to use when parsing mpi.h.
-i pmpi_init Specify proper binding for the fortran pmpi_init function.
Default is \'pmpi_init_\'. Wrappers compiled for PIC will guess the
right binding automatically (use -DPIC when you compile dynamic libs).
-o file Send output to a file instead of stdout.
-w Do not print compiler warnings for deprecated MPI functions.
This option will add macros around {{callfn}} to disable (and
restore) the compilers diagnostic functions, if the compiler
supports this functionality.
by Todd Gamblin, tgamblin@llnl.gov
'''
import tempfile, getopt, subprocess, sys, os, re, types, itertools
# Default values for command-line parameters
mpicc = 'mpicc' # Default name for the MPI compiler
includes = [] # Default set of directories to inlucde when parsing mpi.h
pmpi_init_binding = "pmpi_init_" # Default binding for pmpi_init
output_fortran_wrappers = False # Don't print fortran wrappers by default
output_guards = False # Don't print reentry guards by default
skip_headers = False # Skip header information and defines (for non-C output)
dump_prototypes = False # Just exit and dump MPI protos if false.
ignore_deprecated = False # Do not print compiler warnings for deprecated MPI functions
# Possible legal bindings for the fortran version of PMPI_Init()
pmpi_init_bindings = ["PMPI_INIT", "pmpi_init", "pmpi_init_", "pmpi_init__"]
# Possible function return types to consider, used for declaration parser.
# In general, all MPI calls we care about return int. We include double
# to grab MPI_Wtick and MPI_Wtime, but we'll ignore the f2c and c2f calls
# that return MPI_Datatypes and other such things.
# MPI_Aint_add and MPI_Aint_diff return MPI_Aint, so include that too.
rtypes = ['int', 'double', 'MPI_Aint' ]
# If we find these strings in a declaration, exclude it from consideration.
exclude_strings = [ "c2f", "f2c", "typedef", "MPI_T_", "MPI_Comm_spawn" ]
# Regular expressions for start and end of declarations in mpi.h. These are
# used to get the declaration strings out for parsing with formal_re below.
begin_decl_re = re.compile("(" + "|".join(rtypes) + ")\s+(MPI_\w+)\s*\(")
exclude_re = re.compile("|".join(exclude_strings))
end_decl_re = re.compile("\).*\;")
# Regular Expression for splitting up args. Matching against this
# returns three groups: type info, arg name, and array info
formal_re = re.compile(
"\s*(" + # Start type
"(?:const)?\s*" + # Initial const
"\w+" # Type name (note: doesn't handle 'long long', etc. right now)
")\s*(" + # End type, begin pointers
"(?:\s*\*(?:\s*const)?)*" + # Look for 0 or more pointers with optional 'const'
")\s*" # End pointers
"(?:(\w+)\s*)?" + # Argument name. Optional.
"(\[.*\])?\s*$" # Array type. Also optional. Works for multidimensions b/c it's greedy.
)
# Fortran wrapper suffix
f_wrap_suffix = "_fortran_wrapper"
# Initial includes and defines for wrapper files.
wrapper_includes = '''
#include <mpi.h>
#include <stdio.h>
#include <stdlib.h>
#ifndef _EXTERN_C_
#ifdef __cplusplus
#define _EXTERN_C_ extern "C"
#else /* __cplusplus */
#define _EXTERN_C_
#endif /* __cplusplus */
#endif /* _EXTERN_C_ */
#ifdef MPICH_HAS_C2F
_EXTERN_C_ void *MPIR_ToPointer(int);
#endif // MPICH_HAS_C2F
#ifdef PIC
/* For shared libraries, declare these weak and figure out which one was linked
based on which init wrapper was called. See mpi_init wrappers. */
#pragma weak pmpi_init
#pragma weak PMPI_INIT
#pragma weak pmpi_init_
#pragma weak pmpi_init__
#endif /* PIC */
_EXTERN_C_ void pmpi_init(MPI_Fint *ierr);
_EXTERN_C_ void PMPI_INIT(MPI_Fint *ierr);
_EXTERN_C_ void pmpi_init_(MPI_Fint *ierr);
_EXTERN_C_ void pmpi_init__(MPI_Fint *ierr);
'''
# Macros used to suppress MPI deprecation warnings.
wrapper_diagnosics_macros = '''
/* Macros to enable and disable compiler warnings for deprecated functions.
* These macros will be used to silent warnings about deprecated MPI functions,
* as these should be wrapped, even if they are deprecated.
*
* Note: The macros support GCC and clang compilers only. For other compilers
* just add similar macros for your compiler.
*/
#if (defined(__GNUC__) && !defined(__clang__)) && \\
((__GNUC__ == 4 && __GNUC_MINOR__ >= 6) || __GNUC__ > 4)
#define WRAP_MPI_CALL_PREFIX \\
_Pragma("GCC diagnostic push"); \\
_Pragma("GCC diagnostic ignored \\"-Wdeprecated-declarations\\"");
#define WRAP_MPI_CALL_POSTFIX _Pragma("GCC diagnostic pop");
#elif defined(__clang__)
#define WRAP_MPI_CALL_PREFIX \\
_Pragma("clang diagnostic push"); \\
_Pragma("clang diagnostic ignored \\"-Wdeprecated-declarations\\"");
#define WRAP_MPI_CALL_POSTFIX _Pragma("clang diagnostic pop");
#else
#define WRAP_MPI_CALL_PREFIX
#define WRAP_MPI_CALL_POSTFIX
#endif
'''
# Default modifiers for generated bindings
default_modifiers = ["_EXTERN_C_"] # _EXTERN_C_ is #defined (or not) in wrapper_includes. See above.
# Set of MPI Handle types
mpi_handle_types = set(["MPI_Comm", "MPI_Errhandler", "MPI_File", "MPI_Group", "MPI_Info",
"MPI_Op", "MPI_Request", "MPI_Status", "MPI_Datatype", "MPI_Win" ])
# MPI Calls that have array parameters, and mappings from the array parameter positions to the position
# of the 'count' paramters that determine their size
mpi_array_calls = {
"MPI_Startall" : { 1:0 },
"MPI_Testall" : { 1:0, 3:0 },
"MPI_Testany" : { 1:0 },
"MPI_Testsome" : { 1:0, 4:0 },
"MPI_Type_create_struct" : { 3:0 },
"MPI_Type_get_contents" : { 6:1 },
"MPI_Type_struct" : { 3:0 },
"MPI_Waitall" : { 1:0, 2:0 },
"MPI_Waitany" : { 1:0 },
"MPI_Waitsome" : { 1:0, 4:0 }
}
def find_matching_paren(string, index, lparen='(', rparen=')'):
"""Find the closing paren corresponding to the open paren at <index>
in <string>. Optionally, can provide other characters to match on.
If found, returns the index of the matching parenthesis. If not found,
returns -1.
"""
if not string[index] == lparen:
raise ValueError("Character at index %d is '%s'. Expected '%s'"
% (index, string[index], lparen))
index += 1
count = 1
while index < len(string) and count > 0:
while index < len(string) and string[index] not in (lparen, rparen):
index += 1
if string[index] == lparen:
count += 1
elif string[index] == rparen:
count -= 1
if count == 0:
return index
else:
return -1
def isindex(str):
"""True if a string is something we can index an array with."""
try:
int(str)
return True
except ValueError:
return False
def once(function):
if not hasattr(function, "did_once"):
function()
function.did_once = True
# Returns MPI_Blah_[f2c,c2f] prefix for a handle type. MPI_Datatype is a special case.
def conversion_prefix(handle_type):
if handle_type == "MPI_Datatype":
return "MPI_Type"
else:
return handle_type
# Special join function for joining lines together. Puts "\n" at the end too.
def joinlines(list, sep="\n"):
if list:
return sep.join(list) + sep
else:
return ""
# Possible types of Tokens in input.
LBRACE, RBRACE, TEXT, IDENTIFIER = range(4)
class Token:
"""Represents tokens; generated from input by lexer and fed to parse()."""
def __init__(self, type, value, line=0):
self.type = type # Type of token
self.value = value # Text value
self.line = line
def __str__(self):
return "'%s'" % re.sub(r'\n', "\\\\n", self.value)
def isa(self, type):
return self.type == type
class LineTrackingLexer(object):
"""Base class for Lexers that keep track of line numbers."""
def __init__(self, lexicon):
self.line_no = -1
self.scanner = re.Scanner(lexicon)
def make_token(self, type, value):
token = Token(type, value, self.line_no)
self.line_no += value.count("\n")
return token
def lex(self, text):
self.line_no = 0
tokens, remainder = self.scanner.scan(text)
if remainder:
sys.stderr.write("Unlexable input:\n%s\n" % remainder)
sys.exit(1)
self.line_no = -1
return tokens
class OuterRegionLexer(LineTrackingLexer):
def __init__(self):
super(OuterRegionLexer, self).__init__([
(r'{{', self.lbrace),
(r'}}', self.rbrace),
(r'({(?!{)|}(?!})|[^{}])*', self.text)])
def lbrace(self, scanner, token): return self.make_token(LBRACE, token)
def rbrace(self, scanner, token): return self.make_token(RBRACE, token)
def text(self, scanner, token): return self.make_token(TEXT, token)
class OuterCommentLexer(OuterRegionLexer):
def __init__(self):
super(OuterRegionLexer, self).__init__([
(r'/\*(.|[\r\n])*?\*/', self.text), # multiline comment
(r'//(.|[\r\n])*?(?=[\r\n])', self.text), # single line comment
(r'{{', self.lbrace),
(r'}}', self.rbrace),
(r'({(?!{)|}(?!})|/(?![/*])|[^{}/])*', self.text)])
class InnerLexer(OuterRegionLexer):
def __init__(self):
super(OuterRegionLexer, self).__init__([
(r'{{', self.lbrace),
(r'}}', self.rbrace),
(r'(["\'])?((?:(?!\1)[^\\]|\\.)*)\1', self.quoted_id),
(r'([^\s]+)', self.identifier),
(r'\s+', None)])
def identifier(self, scanner, token): return self.make_token(IDENTIFIER, token)
def quoted_id(self, scanner, token):
# remove quotes from quoted ids. Note that ids and quoted ids are pretty much the same thing;
# the quotes are just optional. You only need them if you need spaces in your expression.
return self.make_token(IDENTIFIER, re.sub(r'^["\'](.*)["\']$', '\\1', token))
# Global current filename and function name for error msgs
cur_filename = ""
cur_function = None
class WrapSyntaxError(Exception):
"""Simple Class for syntax errors raised by the wrapper generator (rather than python)"""
pass
def syntax_error(msg):
# TODO: make line numbers actually work.
sys.stderr.write("%s:%d: %s\n" % (cur_filename, 0, msg))
if cur_function:
sys.stderr.write(" While handling %s.\n" % cur_function)
raise WrapSyntaxError
################################################################################
# MPI Semantics:
# Classes in this section describe MPI declarations and types. These are used
# to parse the mpi.h header and to generate wrapper code.
################################################################################
class Scope:
""" This is the very basic class for scopes in the wrapper generator. Scopes
are hierarchical and support nesting. They contain string keys mapped
to either string values or to macro functions.
Scopes also keep track of the particular macro they correspond to (macro_name).
"""
def __init__(self, enclosing_scope=None):
self.map = {}
self.enclosing_scope = enclosing_scope
self.macro_name = None # For better debugging error messages
def __getitem__(self, key):
if key in self.map: return self.map[key]
elif self.enclosing_scope: return self.enclosing_scope[key]
else: raise KeyError(key + " is not in scope.")
def __contains__(self, key):
if key in self.map: return True
elif self.enclosing_scope: return key in self.enclosing_scope
else: return False
def __setitem__(self, key, value):
self.map[key] = value
def include(self, map):
"""Add entire contents of the map (or scope) to this scope."""
self.map.update(map)
################################################################################
# MPI Semantics:
# Classes in this section describe MPI declarations and types. These are used
# to parse the mpi.h header and to generate wrapper code.
################################################################################
# Map from function name to declaration created from mpi.h.
mpi_functions = {}
class Param:
"""Descriptor for formal parameters of MPI functions.
Doesn't represent a full parse, only the initial type information,
name, and array info of the argument split up into strings.
"""
def __init__(self, type, pointers, name, array, pos):
self.type = type # Name of arg's type (might include things like 'const')
self.pointers = pointers # Pointers
self.name = name # Formal parameter name (from header or autogenerated)
self.array = array # Any array type information after the name
self.pos = pos # Position of arg in declartion
self.decl = None # This gets set later by Declaration
def setDeclaration(self, decl):
"""Needs to be called by Declaration to finish initing the arg."""
self.decl = decl
def isHandleArray(self):
"""True if this Param represents an array of MPI handle values."""
return (self.decl.name in mpi_array_calls
and self.pos in mpi_array_calls[self.decl.name])
def countParam(self):
"""If this Param is a handle array, returns the Param that represents the count of its elements"""
return self.decl.args[mpi_array_calls[self.decl.name][self.pos]]
def isHandle(self):
"""True if this Param is one of the MPI builtin handle types."""
return self.type in mpi_handle_types
def isStatus(self):
"""True if this Param is an MPI_Status. MPI_Status is handled differently
in c2f/f2c calls from the other handle types.
"""
return self.type == "MPI_Status"
def fortranFormal(self):
"""Prints out a formal parameter for a fortran wrapper."""
# There are only a few possible fortran arg types in our wrappers, since
# everything is a pointer.
if self.type == "MPI_Aint" or self.type.endswith("_function"):
ftype = self.type
else:
ftype = "MPI_Fint"
# Arrays don't come in as pointers (they're passed as arrays)
# Everything else is a pointer.
if self.pointers:
pointers = self.pointers
elif self.array:
pointers = ""
else:
pointers = "*"
# Put it all together and return the fortran wrapper type here.
arr = self.array or ''
return "%s %s%s%s" % (ftype, pointers, self.name, arr)
def cType(self):
if not self.type:
return ''
else:
arr = self.array or ''
pointers = self.pointers or ''
return "%s%s%s" % (self.type, pointers, arr)
def cFormal(self):
"""Prints out a formal parameter for a C wrapper."""
if not self.type:
return self.name # special case for '...'
else:
arr = self.array or ''
pointers = self.pointers or ''
return "%s %s%s%s" % (self.type, pointers, self.name, arr)
def castType(self):
arr = self.array or ''
pointers = self.pointers or ''
if '[]' in arr:
if arr.count('[') > 1:
pointers += '(*)' # need extra parens for, e.g., int[][3] -> int(*)[3]
else:
pointers += '*' # justa single array; can pass pointer.
arr = arr.replace('[]', '')
return "%s%s%s" % (self.type, pointers, arr)
def __str__(self):
return self.cFormal()
class Declaration:
""" Descriptor for simple MPI function declarations.
Contains return type, name of function, and a list of args.
"""
def __init__(self, rtype, name):
self.rtype = rtype
self.name = name
self.args = []
def addArgument(self, arg):
arg.setDeclaration(self)
self.args.append(arg)
def __iter__(self):
for arg in self.args: yield arg
def __str__(self):
return self.prototype()
def retType(self):
return self.rtype
def formals(self):
return [arg.cFormal() for arg in self.args]
def types(self):
return [arg.cType() for arg in self.args]
def argsNoEllipsis(self):
return filter(lambda arg: arg.name != "...", self.args)
def returnsErrorCode(self):
"""This is a special case for MPI_Wtime and MPI_Wtick.
These functions actually return a double value instead of an int error code.
"""
return self.rtype == "int"
def argNames(self):
return [arg.name for arg in self.argsNoEllipsis()]
def getArgName(self, index):
return self.argsNoEllipsis()[index].name
def fortranFormals(self):
formals = list(map(Param.fortranFormal, self.argsNoEllipsis()))
if self.name == "MPI_Init": formals = [] # Special case for init: no args in fortran
ierr = []
if self.returnsErrorCode(): ierr = ["MPI_Fint *ierr"]
return formals + ierr
def fortranArgNames(self):
names = self.argNames()
if self.name == "MPI_Init": names = []
ierr = []
if self.returnsErrorCode(): ierr = ["ierr"]
return names + ierr
def prototype(self, modifiers=""):
if modifiers: modifiers = joinlines(modifiers, " ")
return "%s%s %s(%s)" % (modifiers, self.retType(), self.name, ", ".join(self.formals()))
def pmpi_prototype(self, modifiers=""):
if modifiers: modifiers = joinlines(modifiers, " ")
return "%s%s P%s(%s)" % (modifiers, self.retType(), self.name, ", ".join(self.formals()))
def fortranPrototype(self, name=None, modifiers=""):
if not name: name = self.name
if modifiers: modifiers = joinlines(modifiers, " ")
if self.returnsErrorCode():
rtype = "void" # Fortran calls use ierr parameter instead
else:
rtype = self.rtype
return "%s%s %s(%s)" % (modifiers, rtype, name, ", ".join(self.fortranFormals()))
types = set()
all_pointers = set()
def enumerate_mpi_declarations(mpicc, includes):
""" Invokes mpicc's C preprocessor on a C file that includes mpi.h.
Parses the output for declarations, and yields each declaration to
the caller.
"""
# Create an input file that just includes <mpi.h>
tmpfile = tempfile.NamedTemporaryFile('w+b', -1, suffix='.c')
tmpname = "%s" % tmpfile.name
tmpfile.write(b'#include <mpi.h>')
tmpfile.write(b"\n")
tmpfile.flush()
# Run the mpicc -E on the temp file and pipe the output
# back to this process for parsing.
string_includes = ["-I"+dir for dir in includes]
mpicc_cmd = "%s -E %s" % (mpicc, " ".join(string_includes))
try:
popen = subprocess.Popen("%s %s" % (mpicc_cmd, tmpname), shell=True,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
except IOError:
sys.stderr.write("IOError: couldn't run '" + mpicc_cmd + "' for parsing mpi.h\n")
sys.exit(1)
# Parse out the declarations from the MPI file
mpi_h = popen.stdout
for line in mpi_h:
line = line.decode().strip()
begin = begin_decl_re.search(line)
if begin and not exclude_re.search(line):
# Grab return type and fn name from initial parse
return_type, fn_name = begin.groups()
# Accumulate rest of declaration (possibly multi-line)
while not end_decl_re.search(line):
line += " " + next(mpi_h).decode().strip()
# Split args up by commas so we can parse them independently
fn_and_paren = r'(%s\s*\()' % fn_name
match = re.search(fn_and_paren, line)
lparen = match.start(1) + len(match.group(1)) - 1
rparen = find_matching_paren(line, lparen)
if rparen < 0:
raise ValueError("Malformed declaration in header: '%s'" % line)
arg_string = line[lparen+1:rparen]
arg_list = list(map(lambda s: s.strip(), arg_string.split(",")))
# Handle functions that take no args specially
if arg_list == ['void']:
arg_list = []
# Parse formal parameter descriptors out of args
decl = Declaration(return_type, fn_name)
arg_num = 0
for arg in arg_list:
if arg == '...': # Special case for Pcontrol.
decl.addArgument(Param(None, None, '...', None, arg_num))
else:
match = formal_re.match(arg)
if not match:
sys.stderr.write("MATCH FAILED FOR: '%s' in %s\n" % (arg, fn_name))
sys.exit(1)
type, pointers, name, array = match.groups()
types.add(type)
all_pointers.add(pointers)
# If there's no name, make one up.
if not name: name = "arg_" + str(arg_num)
decl.addArgument(Param(type.strip(), pointers, name, array, arg_num))
arg_num += 1
yield decl
mpi_h.close()
return_code = popen.wait()
if return_code != 0:
sys.stderr.write("Error: Couldn't run '%s' for parsing mpi.h.\n" % mpicc_cmd)
sys.stderr.write(" Process exited with code %d.\n" % return_code)
sys.exit(1)
# Do some cleanup once we're done reading.
tmpfile.close()
def write_enter_guard(out, decl):
"""Prevent us from entering wrapper functions if we're already in a wrapper function.
Just call the PMPI function w/o the wrapper instead."""
if output_guards:
out.write(" if (in_wrapper) return P%s(%s);\n" % (decl.name, ", ".join(decl.argNames())))
out.write(" in_wrapper = 1;\n")
def write_exit_guard(out):
"""After a call, set in_wrapper back to 0 so we can enter the next call."""
if output_guards:
out.write(" in_wrapper = 0;\n")
def write_c_wrapper(out, decl, return_val, write_body):
"""Write the C wrapper for an MPI function."""
# Write the PMPI prototype here in case mpi.h doesn't define it
# (sadly the case with some MPI implementaitons)
out.write(decl.pmpi_prototype(default_modifiers))
out.write(";\n")
# Now write the wrapper function, which will call the PMPI function we declared.
out.write(decl.prototype(default_modifiers))
out.write(" { \n")
out.write(" %s %s = 0;\n" % (decl.retType(), return_val))
write_enter_guard(out, decl)
write_body(out)
write_exit_guard(out)
out.write(" return %s;\n" % return_val)
out.write("}\n\n")
def write_fortran_binding(out, decl, delegate_name, binding, stmts=None):
"""Outputs a wrapper for a particular fortran binding that delegates to the
primary Fortran wrapper. Optionally takes a list of statements to execute
before delegating.
"""
out.write(decl.fortranPrototype(binding, default_modifiers))
out.write(" { \n")
if stmts:
out.write(joinlines(map(lambda s: " " + s, stmts)))
if decl.returnsErrorCode():
# regular MPI fortran functions use an error code
out.write(" %s(%s);\n" % (delegate_name, ", ".join(decl.fortranArgNames())))
else:
# wtick and wtime return a value
out.write(" return %s(%s);\n" % (delegate_name, ", ".join(decl.fortranArgNames())))
out.write("}\n\n")
class FortranDelegation:
"""Class for constructing a call to a Fortran wrapper delegate function. Provides
storage for local temporary variables, copies of parameters, callsites for MPI-1 and
MPI-2, and writebacks to local pointer types.
"""
def __init__(self, decl, return_val):
self.decl = decl
self.return_val = return_val
self.temps = set()
self.copies = []
self.writebacks = []
self.actuals = []
self.mpich_actuals = []
def addTemp(self, type, name):
"""Adds a temp var with a particular name. Adds the same var only once."""
temp = " %s %s;" % (type, name)
self.temps.add(temp)
def addActual(self, actual):
self.actuals.append(actual)
self.mpich_actuals.append(actual)
def addActualMPICH(self, actual):
self.mpich_actuals.append(actual)
def addActualMPI2(self, actual):
self.actuals.append(actual)
def addWriteback(self, stmt):
self.writebacks.append(" %s" % stmt)
def addCopy(self, stmt):
self.copies.append(" %s" % stmt)
def write(self, out):
assert len(self.actuals) == len(self.mpich_actuals)
call = " %s = %s" % (self.return_val, self.decl.name)
mpich_call = "%s(%s);\n" % (call, ", ".join(self.mpich_actuals))
mpi2_call = "%s(%s);\n" % (call, ", ".join(self.actuals))
out.write(" %s %s = 0;\n" % (self.decl.retType(), self.return_val))
if mpich_call == mpi2_call and not (self.temps or self.copies or self.writebacks):
out.write(mpich_call)
else:
out.write("#if (!defined(MPICH_HAS_C2F) && defined(MPICH_NAME) && (MPICH_NAME == 1)) /* MPICH test */\n")
out.write(mpich_call)
out.write("#else /* MPI-2 safe call */\n")
out.write(joinlines(self.temps))
out.write(joinlines(self.copies))
out.write(mpi2_call)
out.write(joinlines(self.writebacks))
out.write("#endif /* MPICH test */\n")
def write_fortran_wrappers(out, decl, return_val):
"""Writes primary fortran wrapper that handles arg translation.
Also outputs bindings for this wrapper for different types of fortran compilers.
"""
delegate_name = decl.name + f_wrap_suffix
out.write(decl.fortranPrototype(delegate_name, ["static"]))
out.write(" { \n")
call = FortranDelegation(decl, return_val)
if decl.name == "MPI_Init":
# Use out.write() here so it comes at very beginning of wrapper function
out.write(" int argc = 0;\n");
out.write(" char ** argv = NULL;\n");
call.addActual("&argc");
call.addActual("&argv");
call.write(out)
out.write(" *ierr = %s;\n" % return_val)
out.write("}\n\n")
# Write out various bindings that delegate to the main fortran wrapper
write_fortran_binding(out, decl, delegate_name, "MPI_INIT", ["fortran_init = 1;"])
write_fortran_binding(out, decl, delegate_name, "mpi_init", ["fortran_init = 2;"])
write_fortran_binding(out, decl, delegate_name, "mpi_init_", ["fortran_init = 3;"])
write_fortran_binding(out, decl, delegate_name, "mpi_init__", ["fortran_init = 4;"])
return
# This look processes the rest of the call for all other routines.
for arg in decl.args:
if arg.name == "...": # skip ellipsis
continue
if not (arg.pointers or arg.array):
if not arg.isHandle():
# These are pass-by-value arguments, so just deref and pass thru
dereferenced = "*%s" % arg.name
call.addActual(dereferenced)
else:
# Non-ptr, non-arr handles need to be converted with MPI_Blah_f2c
# No special case for MPI_Status here because MPI_Statuses are never passed by value.
call.addActualMPI2("%s_f2c(*%s)" % (conversion_prefix(arg.type), arg.name))
call.addActualMPICH("(%s)(*%s)" % (arg.type, arg.name))
else:
if not arg.isHandle():
# Non-MPI handle pointer types can be passed w/o dereferencing, but need to
# cast to correct pointer type first (from MPI_Fint*).
call.addActual("(%s)%s" % (arg.castType(), arg.name))
else:
# For MPI-1, assume ints, cross fingers, and pass things straight through.
call.addActualMPICH("(%s*)%s" % (arg.type, arg.name))
conv = conversion_prefix(arg.type)
temp = "temp_%s" % arg.name
# For MPI-2, other pointer and array types need temporaries and special conversions.
if not arg.isHandleArray():
call.addTemp(arg.type, temp)
call.addActualMPI2("&%s" % temp)
if arg.isStatus():
call.addCopy("%s_f2c(%s, &%s);" % (conv, arg.name, temp))
call.addWriteback("%s_c2f(&%s, %s);" % (conv, temp, arg.name))
else:
call.addCopy("%s = %s_f2c(*%s);" % (temp, conv, arg.name))
call.addWriteback("*%s = %s_c2f(%s);" % (arg.name, conv, temp))
else:
# Make temporary variables for the array and the loop var
temp_arr_type = "%s*" % arg.type
call.addTemp(temp_arr_type, temp)
call.addTemp("int", "i")
# generate a copy and a writeback statement for this type of handle
if arg.isStatus():
copy = " %s_f2c(&%s[i], &%s[i])" % (conv, arg.name, temp)
writeback = " %s_c2f(&%s[i], &%s[i])" % (conv, temp, arg.name)
else:
copy = " temp_%s[i] = %s_f2c(%s[i])" % (arg.name, conv, arg.name)
writeback = " %s[i] = %s_c2f(temp_%s[i])" % (arg.name, conv, arg.name)
# Generate the call surrounded by temp array allocation, copies, writebacks, and temp free
count = "*%s" % arg.countParam().name
call.addCopy("%s = (%s)malloc(sizeof(%s) * %s);" %
(temp, temp_arr_type, arg.type, count))
call.addCopy("for (i=0; i < %s; i++)" % count)
call.addCopy("%s;" % copy)
call.addActualMPI2(temp)
call.addWriteback("for (i=0; i < %s; i++)" % count)
call.addWriteback("%s;" % writeback)
call.addWriteback("free(%s);" % temp)
call.write(out)
if decl.returnsErrorCode():
out.write(" *ierr = %s;\n" % return_val)
else:
out.write(" return %s;\n" % return_val)
out.write("}\n\n")
# Write out various bindings that delegate to the main fortran wrapper
write_fortran_binding(out, decl, delegate_name, decl.name.upper())
write_fortran_binding(out, decl, delegate_name, decl.name.lower())
write_fortran_binding(out, decl, delegate_name, decl.name.lower() + "_")
write_fortran_binding(out, decl, delegate_name, decl.name.lower() + "__")
################################################################################
# Macros:
# - functions annotated as @macro or @bodymacro define the global macros and
# basic pieces of the generator.
# - include_decl is used to include MPI declarations into function scopes.
################################################################################
# Table of global macros
macros = {}
# This decorator adds macro functions to the outermost function scope.
def macro(macro_name, **attrs):
def decorate(fun):
macros[macro_name] = fun # Add macro to outer scope under supplied name
fun.has_body = False # By default, macros have no body.
for key in attrs: # Optionally set/override attributes
setattr(fun, key, attrs[key])
return fun
return decorate
def handle_list(list_name, list, args):
"""This function handles indexing lists used as macros in the wrapper generator.
There are two syntaxes:
{{<list_name>}} Evaluates to the whole list, e.g. 'foo, bar, baz'
{{<list_name> <index>}} Evaluates to a particular element of a list.
"""
if not args:
return list
else:
len(args) == 1 or syntax_error("Wrong number of args for list expression.")
try:
return list[int(args[0])]
except ValueError:
syntax_error("Invald index value: '%s'" % args[0])
except IndexError:
syntax_error("Index out of range in '%s': %d" % (list_name, index))
class TypeApplier:
"""This class implements a Macro function for applying something callable to
args in a decl with a particular type.
"""
def __init__(self, decl):
self.decl = decl
def __call__(self, out, scope, args, children):
len(args) == 2 or syntax_error("Wrong number of args in apply macro.")
type, macro_name = args
for arg in self.decl.args:
if arg.cType() == type:
out.write("%s(%s);\n" % (macro_name, arg.name))
def include_decl(scope, decl):
"""This function is used by macros to include attributes MPI declarations in their scope."""
scope["ret_type"] = decl.retType()
scope["args"] = decl.argNames()
scope["nargs"] = len(decl.argNames())
scope["types"] = decl.types()
scope["formals"] = decl.formals()
scope["apply_to_type"] = TypeApplier(decl)
scope.function_name = decl.name
# These are old-stype, deprecated names.
def get_arg(out, scope, args, children):
return handle_list("args", decl.argNames(), args)
scope["get_arg"] = get_arg
scope["applyToType"] = scope["apply_to_type"]
scope["retType"] = scope["ret_type"]
scope["argList"] = "(%s)" % ", ".join(scope["args"])
scope["argTypeList"] = "(%s)" % ", ".join(scope["formals"])
def all_but(fn_list):
"""Return a list of all mpi functions except those in fn_list"""
all_mpi = set(mpi_functions.keys())
diff = all_mpi - set(fn_list)
return [x for x in sorted(diff)]
@macro("foreachfn", has_body=True)
def foreachfn(out, scope, args, children):
"""Iterate over all functions listed in args."""
args or syntax_error("Error: foreachfn requires function name argument.")
global cur_function
fn_var = args[0]
for fn_name in args[1:]:
cur_function = fn_name
if not fn_name in mpi_functions:
syntax_error(fn_name + " is not an MPI function")
fn = mpi_functions[fn_name]
fn_scope = Scope(scope)
fn_scope[fn_var] = fn_name
include_decl(fn_scope, fn)
for child in children:
child.evaluate(out, fn_scope)
cur_function = None
@macro("fn", has_body=True)
def fn(out, scope, args, children):
"""Iterate over listed functions and generate skeleton too."""
args or syntax_error("Error: fn requires function name argument.")
global cur_function
fn_var = args[0]
for fn_name in args[1:]:
cur_function = fn_name
if not fn_name in mpi_functions:
syntax_error(fn_name + " is not an MPI function")
fn = mpi_functions[fn_name]
return_val = "_wrap_py_return_val"
fn_scope = Scope(scope)
fn_scope[fn_var] = fn_name
include_decl(fn_scope, fn)
fn_scope["ret_val"] = return_val
fn_scope["returnVal"] = fn_scope["ret_val"] # deprecated name.
if ignore_deprecated:
c_call = "%s\n%s = P%s(%s);\n%s" % ("WRAP_MPI_CALL_PREFIX", return_val, fn.name, ", ".join(fn.argNames()), "WRAP_MPI_CALL_POSTFIX")
else:
c_call = "%s = P%s(%s);" % (return_val, fn.name, ", ".join(fn.argNames()))
if fn_name == "MPI_Init" and output_fortran_wrappers:
def callfn(out, scope, args, children):
# All this is to deal with fortran, since fortran's MPI_Init() function is different
# from C's. We need to make sure to delegate specifically to the fortran init wrapping.
# For dynamic libs, we use weak symbols to pick it automatically. For static libs, need
# to rely on input from the user via pmpi_init_binding and the -i option.
out.write(" if (fortran_init) {\n")
out.write("#ifdef PIC\n")
out.write(" if (!PMPI_INIT && !pmpi_init && !pmpi_init_ && !pmpi_init__) {\n")
out.write(" fprintf(stderr, \"ERROR: Couldn't find fortran pmpi_init function. Link against static library instead.\\n\");\n")
out.write(" exit(1);\n")
out.write(" }")
out.write(" switch (fortran_init) {\n")
out.write(" case 1: PMPI_INIT(&%s); break;\n" % return_val)
out.write(" case 2: pmpi_init(&%s); break;\n" % return_val)
out.write(" case 3: pmpi_init_(&%s); break;\n" % return_val)
out.write(" case 4: pmpi_init__(&%s); break;\n" % return_val)
out.write(" default:\n")
out.write(" fprintf(stderr, \"NO SUITABLE FORTRAN MPI_INIT BINDING\\n\");\n")
out.write(" break;\n")
out.write(" }\n")
out.write("#else /* !PIC */\n")
out.write(" %s(&%s);\n" % (pmpi_init_binding, return_val))
out.write("#endif /* !PIC */\n")
out.write(" } else {\n")
out.write(" %s\n" % c_call)
out.write(" }\n")
fn_scope["callfn"] = callfn
def write_fortran_init_flag():
output.write("static int fortran_init = 0;\n")
once(write_fortran_init_flag)
else:
fn_scope["callfn"] = c_call
def write_body(out):
for child in children:
child.evaluate(out, fn_scope)
out.write("/* ================== C Wrappers for %s ================== */\n" % fn_name)
write_c_wrapper(out, fn, return_val, write_body)
if output_fortran_wrappers:
out.write("/* =============== Fortran Wrappers for %s =============== */\n" % fn_name)