Exemplo n.º 1
0
def create_component(ndim, dim):
    # each component is one dimension -> A[i] = B[i](A[i] - A[i-1]) + B[i+1](A[i+1] - A[i])
    lower_diff = StencilComponent("mesh",
                                  SparseWeightArray({Vector.zero_vector(ndim): 1, -Vector.unit_vector(dim, ndim): -1}))
    lower_diff *= StencilComponent("beta_{}".format(dim),
                                 SparseWeightArray({Vector.zero_vector(ndim): 1}))
    upper_diff = StencilComponent("mesh",
                                  SparseWeightArray({Vector.zero_vector(ndim): -1, Vector.unit_vector(dim, ndim): 1}))
    upper_diff *= StencilComponent("beta_{}".format(dim),
                                 SparseWeightArray({Vector.unit_vector(dim, ndim): 1}))

    return lower_diff + upper_diff
Exemplo n.º 2
0
def _stencil_conflict(data1, data2, shape_map):

    # shadows = get_shadow(stencil2)  # these are all of the offset vectors for each array
    shadows = data2.shadow
    if data1.output not in shadows:
        return False  # not read/writing from same mesh

    shade = shadows[data1.output]  # these are vectors that are used.

    shape = shape_map[data1.primary_mesh]

    if data1.output == data2.output:  # if they write to the same array
        shade |= {Vector.zero_vector(len(shape))}

    # Perform exhaustive search over pairs of iteration spaces?

    # TODO: Probably should make this smarter
    for write_domain in data1.iteration_space.reify(shape).domains:
        for read_domain in data2.iteration_space.reify(shape).domains:
            # in order to save time, compute the projections for each vector onto each dimension.
            collisions = [{} for _ in shape]
            for vector in shade:
                for dim, val in enumerate(vector):
                    collisions[dim][val] = False

            # using 1d slices of each domain
            for wd_1d, rd_1d, offsets in zip(
                zip(write_domain.lower, write_domain.upper, write_domain.stride),
                zip(read_domain.lower, read_domain.upper, read_domain.stride),
                collisions,
            ):
                for offset in offsets:
                    offsets[offset] = _has_conflict(wd_1d, rd_1d, offset)
            # print(collisions)
            for vector in shade:
                if all(
                    collisions[dim][val] for dim, val in enumerate(vector)
                ):  # some vector has a collision on all dimensions
                    return True
    return False
Exemplo n.º 3
0
def test_collision():
    """
    Test using Red Black. All of them should collide.
    """
    dimensions = 3
    # Red domains are formed by the origin plus all non-negative vectors with 1-norm 2.
    red_domain_starts = [Vector.zero_vector(dimensions)] + [i for i in Vector.von_neumann_vectors(dimensions, 2)
                                                            if all(coord >= 0 for coord in i)]
    red_domain = DomainUnion([
        RectangularDomain([(s, -1, 2) for s in start])
        for start in red_domain_starts
    ])


    # Black domains are formed by non-negative vectors with 1-norm 1.

    black_domain_starts = [i for i in Vector.von_neumann_vectors(dimensions, 1) if all(coord >= 0 for coord in i)]

    black_domain = DomainUnion([
        RectangularDomain([(s, -1, 2) for s in start])
        for start in black_domain_starts
    ])

    star_neighborhood = {Vector.unit_vector(d, dimensions): 1 for d in range(dimensions)}
    star_neighborhood.update(
        {-Vector.unit_vector(d, dimensions): 1 for d in range(dimensions)}
    )
    red = Stencil(
        StencilComponent(
            "mesh",
            SparseWeightArray(
                star_neighborhood
            )
        ),
        "mesh",
        red_domain
    )

    black = Stencil(
        StencilComponent(
            "mesh",
            SparseWeightArray(
                star_neighborhood
            )
        ),
        "mesh",
        black_domain
    )

    red_oop = Stencil(
        StencilComponent(
            "mesh",
            SparseWeightArray(
                star_neighborhood
            )
        ),
        "output",
        red_domain
    )

    black_oop = Stencil(
        StencilComponent(
            "mesh",
            SparseWeightArray(
                star_neighborhood
            )
        ),
        "output",
        black_domain
    )

    print("Testing with simulated 32^dimensions mesh")
    print("WRITE-READ", "has collision")
    print("RED - RED", stencil_conflict(red, red, {"mesh": (32,)*dimensions}))
    print("BLACK - BLACK", stencil_conflict(black, black, {"mesh": (32,)*dimensions}))
    print("RED - BLACK", stencil_conflict(red, black, {"mesh": (32,)*dimensions}))
    print("BLACK - RED", stencil_conflict(black, red, {"mesh": (32,)*dimensions}))
    print("REDOOP - BLACKOOP", stencil_conflict(red_oop, black_oop, {"mesh": (32,)*dimensions}))
    print("BLACKOOP - REDOOP", stencil_conflict(black_oop, red_oop, {"mesh": (32,)*dimensions}))
    print("INTRA-red is valid", validate_stencil(red))
    print("INTRA-black is valid", validate_stencil(black))