Hitmap 1.3
 All Data Structures Namespaces Files Functions Variables Typedefs Friends Macros Groups Pages
genTypeStruct.py
Go to the documentation of this file.
1 #!/usr/bin/python
2 
3 #
4 # Script to automatically generate macros to define new Hitmap communication
5 # derived data types for structs, with as many fields as needed.
6 #
7 # The result of this script is stored in: hit_comTypeStruct.h
8 #
9 # @version 1.0
10 # @author Javier Fresno Bausela
11 # @author Arturo Gonzalez-Escribano
12 # @date Mar 2013
13 # @date Jun 2013 Changed loop variables for something longer to avoid warnings.
14 # @date Ago 2015 Add the generation of the license tags
15 #
16 #
17 # This software is provided to enhance knowledge and encourage progress
18 # in the scientific community and are to be used only for research and
19 # educational purposes. Any reproduction or use for commercial purpose
20 # is prohibited without the prior express written permission.
21 #
22 # This software is provided "as is" and without any express or implied
23 # warranties, including, without limitation, the implied warranties of
24 # merchantability and fitness for a particular purpose.
25 #
26 # Copyright (c) 2013, Javier Fresno Bausela
27 # All Rights Reserved.
28 #
29 
30 
31 # Imports
32 import sys
33 import string
34 import itertools
35 
36 # Config
37 
38 # Number of generated macros, we are using the A,B,...,Z,AA,..ZY,ZZ
39 # names for the variables so the current limit is 26 + 26 * 26 = 702 macros.
40 NUM_MACROS = 100
41 
42 
43 ##############################################################
44 
45 # Generate the letters
46 one_letter_items = list(string.uppercase)
47 two_letter_items = [''.join(x) for x in
48  itertools.product(string.uppercase, repeat=2)]
49 letters = one_letter_items + two_letter_items
50 
51 # Print a comment
52 def comment(text):
53  sys.stdout.write("// ")
54  sys.stdout.write(text)
55  sys.stdout.write("\n")
56 
57 # Print a multiline comment
58 def big_comment(text):
59  sys.stdout.write("/**\n * ")
60  text = text.replace("\n","\n * ")
61  sys.stdout.write(text)
62  sys.stdout.write(" \n */\n")
63 
64 # Print a line with a tab and the continue sysmbol
65 def macro_line(text):
66  sys.stdout.write("\t" + text + " \\\n")
67 
68 # Prints a struct macro for the given number of variables
69 def print_struct_macro(nvars):
70 
71  comment("Macro for " + str(nvars) + " struct elements" )
72 
73  # Macro header
74  sys.stdout.write("#define hit_comTypeStruct"+str(nvars))
75  sys.stdout.write("(new_type, NATIVE_STRUCT")
76  for i in range(nvars):
77  sys.stdout.write(", NAME_"+letters[i]+", COUNT_"+letters[i]+", TYPE_"+letters[i])
78  sys.stdout.write(") \\\n")
79  sys.stdout.write("{ \\\n")
80 
81  # Native struct
82  macro_line("NATIVE_STRUCT cmdline;")
83 
84  # Block counts
85  sys.stdout.write("\tint blockcounts[" + str(nvars) + "] = {(COUNT_A)")
86  for i in range(1,nvars):
87  sys.stdout.write(", (COUNT_"+letters[i]+")")
88  sys.stdout.write("}; \\\n")
89 
90  # Datatypes
91  sys.stdout.write("\tMPI_Datatype types[" + str(nvars) + "] = {(TYPE_A)")
92  for i in range(1,nvars):
93  sys.stdout.write(", (TYPE_"+letters[i]+")")
94  sys.stdout.write("}; \\\n")
95 
96  # Displacements
97  macro_line("MPI_Aint displs[" + str(nvars) + "];")
98  for i in range(nvars):
99  macro_line("MPI_Get_address(&cmdline.NAME_" +letters[i] + ", &displs["+ str(i) +"]);")
100 
101  # Loop for fix displacements
102  macro_line("int ivarloop;")
103  macro_line("for(ivarloop=" + str(nvars-1) + "; ivarloop>=0; ivarloop--){")
104  macro_line("\tdispls[ivarloop] -= displs[0];")
105  macro_line("}")
106 
107  # MPI functions
108  macro_line("MPI_Type_create_struct(" + str(nvars) + ", blockcounts, displs, types, new_type);")
109  macro_line("MPI_Type_commit(new_type);")
110 
111  sys.stdout.write("} \n")
112  print
113 
114 ##############################################################
115 
116 # Change the output
117 sys.stdout = open('hit_comTypeStruct.h', 'w')
118 
119 # Comments
120 big_comment("Macros to create the MPI Struct Datatypes.\n"
121 "The script to generate this file is included at the end.\n\n"
122 "<license>\n"
123 "</license>\n")
124 print
125 
126 for i in range(1,NUM_MACROS+1):
128 
129 # Inlude the file in the .h
130 big_comment("Python script to generate this file")
131 print "#ifdef NOT_INCLUDE_THIS_BEACUSE_IS_THE_PYTHON_SCRIPT"
132 f = open(sys.argv[0])
133 lines = f.readlines()
134 for l in lines:
135  sys.stdout.write(l)
136 f.close()
137 print "#endif"
138 print
139 
140 
141 
142 
def print_struct_macro