Peano
Loading...
Searching...
No Matches
BooleanArray.py
Go to the documentation of this file.
1# This file is part of the DaStGen2 project. For conditions of distribution and
2# use, please see the copyright notice at www.peano-framework.org
3from .Attribute import Attribute
4from .Boolean import Boolean
5from dastgen2.Utils import LLVMSymbol
6
7
9 """
10
11 Represent an array of boolean flags
12
13 Consult the constructor on details onto which C++ data these flags
14 are mapped.
15
16 """
17
19 self,
20 name,
21 cardinality,
22 compress=False,
23 ifdefs=[],
24 qualifier=Attribute.Qualifier.NONE,
25 initval=None,
26 ):
27 """
28
29 C++ offers the bitset to accommodate an array of boolean flags. While
30 a bitset provides a memory-efficient way to encode a set of booleans,
31 it is not efficient if we use it in combination with other booleans or
32 other bitsets, as the C++ compiler will insert empty bits to align
33 each attribute at least with a byte.
34
35 Our LLVM modification can eliminate these fill-in bits, but it does
36 not natively support bitsets (as we, in general, do not cover stuff
37 from the standard library). If you want to use the extension, you have
38 to model the bitset as an array of booleans natively.
39
40
41
42 ## Attributes
43
44 compress: Boolean
45 Without compression (default), map array onto bitet. Otherwise,
46 use native array of boolean.
47
48 """
49 if qualifier != Attribute.Qualifier.NONE:
50 raise NotImplementedError(
51 f"Attribute {name}: This data type can't work with qualifiers (yet)"
52 )
53 if initval is not None:
54 raise NotImplementedError(
55 f"Attribute {name}: Can't generate arrays with initial values (yet)."
56 )
57 Boolean.__init__(
58 self, name, ifdefs=ifdefs, qualifier=qualifier, initval=initval
59 )
60 self._cardinality = str(cardinality)
61 self.compresscompress = compress
62
63 def get_methods(self, _full_qualified_class_name, for_declaration=True):
64 accessor_name = self._name_name[:1].title() + self._name_name[1:]
65 return [
66 (
67 "get" + accessor_name + "() const",
68 "std::bitset<" + self._cardinality + ">",
69 ),
70 (
71 "set"
72 + accessor_name
73 + "(const std::bitset<"
74 + self._cardinality
75 + ">& value)",
76 "void",
77 ),
78 ("get" + accessor_name + "(int index) const", "bool"),
79 ("set" + accessor_name + "(int index, bool value)", "void"),
80 ("flip" + accessor_name + "(int index)", "void"),
81 ]
82
83 def get_plain_C_attributes(self, for_constructor=False):
84 if self.compresscompress:
85 return [
86 (
87 "_" + self._name_name + "[" + self._cardinality + "]",
88 "bool",
89 "[[clang::pack]]",
90 ["defined(" + LLVMSymbol + ")"],
91 ),
92 (
93 "_" + self._name_name,
94 "std::bitset<" + self._cardinality + ">",
95 "",
96 ["!defined(" + LLVMSymbol + ")"],
97 ),
98 ]
99 else:
100 return [("_" + self._name_name, "std::bitset<" + self._cardinality + ">")]
101
103 return [("_" + self._name_name, "bool")]
104
105 def get_method_body(self, signature):
106 if self.use_data_store:
107 name = " _dataStore._" + self._name_name
108 else:
109 name = " _" + self._name_name
110 if signature.startswith("flip") and "index" in signature and self.compresscompress:
111 return name + "[index] = not " + name + "[index];\n"
112 if signature.startswith("flip") and "index" in signature:
113 return name + ".flip(index);\n"
114 elif signature.startswith("get") and "index" in signature:
115 return " return " + name + "[index];\n"
116 elif signature.startswith("set") and "index" in signature:
117 return name + "[index] = value;\n"
118 elif signature.startswith("get") and self.compresscompress:
119 return """
120 std::bitset<{}> result;
121 for (int i=0; i<{}; i++) result[i] = {}[i];
122 return result;
123""".format(
124 self._cardinality, self._cardinality, name
125 )
126 elif signature.startswith("get"):
127 return " return " + name + ";\n"
128 elif signature.startswith("set") and self.compresscompress:
129 return (
130 """
131 for (int i=0; i<"""
132 + self._cardinality
133 + """; i++) """
134 + name
135 + """[i]=value[i];
136"""
137 )
138 elif signature.startswith("set"):
139 return name + " = value;\n"
140 else:
141 assert False
142 return ""
143
145 return [("MPI_UNSIGNED_LONG", 1)]
146
148 return [("_" + self._name_name, "std::bitset<" + self._cardinality + ">")]
149
151 return not self.compresscompress
152
153 def get_to_string(self):
154 """
155
156 Return string representation of attribute.
157
158 """
159 if self.compresscompress:
160 return "get" + self._name_name[0].upper() + self._name_name[1:] + "()"
161 elif self.use_data_store:
162 return "_dataStore._" + self._name_name
163 else:
164 return "_" + self._name_name
Represent an array of boolean flags.
get_to_string(self)
Return string representation of attribute.
get_first_plain_C_attribute(self)
For MPI for example, I need to know the first attribute.
get_method_body(self, signature)
I hand in the method signature (see get_methods()) and wanna get the whole implementation.
__init__(self, name, cardinality, compress=False, ifdefs=[], qualifier=Attribute.Qualifier.NONE, initval=None)
C++ offers the bitset to accommodate an array of boolean flags.
get_plain_C_attributes(self, for_constructor=False)
Return list of n-tuples.
get_native_MPI_type(self)
I originally wanted to map the booleans to MPI_CXX_BOOL.
get_constructor_arguments(self)
Return list of tuple of arguments for the constructor.
use_default_copy_constructor(self)
If this routine returns False, the generator will create a copy constructor copying each attribute ov...
get_methods(self, _full_qualified_class_name, for_declaration=True)
Return sequence of methods that are defined for this attribute.