def _cgen(self): decl = cgen.Value(self.carray.typ.__str__(), self.carray.name) if not self.flat: for dim in self.carray.dims: decl = cgen.ArrayOf(decl, dim) else: total_size = self.carray.get_total_size() decl = cgen.ArrayOf(decl, total_size) return decl
def visit_Assign(self, node): self.visit(node.targets[0]) self.visit(node.value) if isinstance(node.value, ast.List): # Detect in-place initialisation of multi-dimensional arrays tmp_node = node.value decl = c.Value('float', node.targets[0].id) while isinstance(tmp_node, ast.List): decl = c.ArrayOf(decl, len(tmp_node.elts)) if isinstance(tmp_node.elts[0], ast.List): # Check type and dimension are the same if not all(isinstance(e, ast.List) for e in tmp_node.elts): raise TypeError( "Non-list element discovered in array declaration") if not all( len(e.elts) == len(tmp_node.elts[0].elts) for e in tmp_node.elts): raise TypeError( "Irregular array length not allowed in array declaration" ) tmp_node = tmp_node.elts[0] node.ccode = c.Initializer(decl, node.value.ccode) self.array_vars += [node.targets[0].id] else: node.ccode = c.Assign(node.targets[0].ccode, node.value.ccode)
def get_declarations(self): scope = self.scope_stack[-1] result = [] pre_func_decl = [] def gen_shape(start_end): return ":".join(self.gen_expr(s) for s in start_end) for name in sorted(scope.known_names()): shape = scope.dim_map.get(name) if shape is not None: dim_stmt = cgen.Statement( "dimension \"fortran\" %s[%s]" % (scope.translate_var_name(name), ", ".join( gen_shape(s) for s in shape))) # cannot omit 'dimension' decl even for rank-1 args: result.append(dim_stmt) if name in scope.data: assert name not in scope.arg_names data = scope.data[name] if shape is None: assert len(data) == 1 result.append( cgen.Initializer(self.get_declarator(name), self.gen_expr(data[0]))) else: from cgen.opencl import CLConstant pre_func_decl.append( cgen.Initializer( CLConstant( cgen.ArrayOf( self.get_declarator( "%s_%s" % (scope.subprogram_name, name)))), "{ %s }" % ",\n".join(self.gen_expr(x) for x in data))) else: if name not in scope.arg_names: if shape is not None: result.append( cgen.Statement( "%s %s[nitemsof(%s)]" % (dtype_to_ctype( scope.get_type(name)), name, name))) else: result.append(self.get_declarator(name)) return pre_func_decl, result
def declaration(self, pos): """Generates the declarative instructions for the optimizations over sites nr. `pos` :param pos: The local tensor to copy (should be `< len(X)`) :returns: List containing cgen Statements """ max_ltens_size = max(self._ltens_sizes) max_left_size = 1 if pos == 0 else max(self._ranks[:pos]) max_right_size = 1 if pos == self._sites - 1 else max( self._ranks[pos:]) max_tmat_size = max(self._ranks[i] * self._ranks[i + 1] for i in range(self._sites - 2)) init_statements = [ c.LineComment( "Define the row number the current thread is operating on"), c.Initializer(c.Const(c.POD(np.int32, 'mid')), 'threadIdx.x + blockIdx.x * blockDim.x'), c.LineComment("Allocate shared memory for the local tensors"), ccu.CudaShared( c.ArrayOf(c.POD(self._dtype, 'x_shared'), max_ltens_size)), c.LineComment( "Allocate the left-, right-, and transfer contractions"), c.ArrayOf(c.POD(self._dtype, 'left_c'), max_left_size), c.ArrayOf(c.POD(self._dtype, 'right_c'), max_right_size), c.ArrayOf(c.POD(self._dtype, 'tmat_c'), max_tmat_size), c.ArrayOf(c.POD(self._dtype, 'buf_c'), max(max_right_size, max_left_size)), c.LineComment("Shortcut for current row of design matrix"), c.LineComment("Carefull, current_row might be out of bounds!"), ConstPointerToConst(self._dtype, 'current_row', 'A + (mid * %i)' % sum(self._dims)) ] return init_statements