def init_X(fiat_cell):
        vertices = numpy.array(fiat_cell.get_vertices())
        X = numpy.average(vertices, axis=0)

        formatter = ArrayInit(X, precision=parameters["precision"])._formatter
        return "\n".join("%s = %s;" % ("X[%d]" % i, formatter(v))
                         for i, v in enumerate(X))
Example #2
0
def init_X(fiat_cell, parameters):
    vertices = numpy.array(fiat_cell.get_vertices())
    X = numpy.average(vertices, axis=0)

    formatter = ArrayInit(X,
                          precision=numpy.finfo(
                              parameters["scalar_type"]).resolution)._formatter
    return "\n".join("%s = %s;" % ("X[%d]" % i, formatter(v))
                     for i, v in enumerate(X))
Example #3
0
    def visit_Writer(self, o, *args, **kwargs):
        lvalue = o.children[0]
        writes = [l for l in kwargs["loop_nest"] if l.dim in lvalue.rank]

        # Evaluate the expression for each point in in the n-dimensional space
        # represented by /writes/
        dims = tuple(l.dim for l in writes)
        shape = tuple(l.size for l in writes)
        values, precision = np.zeros(shape), None
        for i in itertools.product(*[range(j) for j in shape]):
            point = {d: v for d, v in zip(dims, i)}
            expr_values, precision = self.visit(o.children[1],
                                                point=point,
                                                *args,
                                                **kwargs)
            # The sum takes into account reductions
            values[i] = np.sum(expr_values)

        # If values is not expected to be block-sparse, just return
        if not self.track_zeros:
            return {lvalue: ArrayInit(values)}

        # Sniff the values to check for the presence of zero-valued blocks: ...
        # ... set default nonzero patten
        nonzero = [[(i, 0)] for i in shape]
        # ... track nonzeros in each dimension
        nonzeros_bydim = values.nonzero()
        mapper = []
        for nz_dim in nonzeros_bydim:
            mapper_dim = defaultdict(set)
            for i, nz in enumerate(nz_dim):
                point = []
                # ... handle outer dimensions
                for j in nonzeros_bydim[:-1]:
                    if j is not nz_dim:
                        point.append((j[i], ))
                # ... handle the innermost dimension, which is treated "specially"
                # to retain data alignment
                for j in nonzeros_bydim[-1:]:
                    if j is not nz_dim:
                        point.append(
                            tuple(range(self.down(j[i]), self.up(j[i] + 1))))
                mapper_dim[nz].add(tuple(point))
            mapper.append(mapper_dim)
        for i, dim in enumerate(mapper[:-1]):
            # Group indices iff contiguous /and/ same codomain
            def grouper(arg):
                m, n = arg
                return m - n, dim[n]

            ranges = []
            for k, g in itertools.groupby(enumerate(sorted(dim.keys())),
                                          grouper):
                group = list(map(operator.itemgetter(1), g))
                ranges.append((group[-1] - group[0] + 1, group[0]))
            nonzero[i] = ranges or nonzero[i]
        # Group indices in the innermost dimension iff within vector length size
        ranges, grouper = [], lambda n: self.down(n)
        for k, g in itertools.groupby(sorted(mapper[-1].keys()), grouper):
            group = list(g)
            ranges.append((group[-1] - group[0] + 1, group[0]))
        nonzero[-1] = self.make_itspace(mode=1).merge(ranges or nonzero[-1],
                                                      within=-1)

        return {lvalue: SparseArrayInit(values, precision, tuple(nonzero))}