def build_data(nx, ny, nz): input = StencilGrid([nx, ny, nz]) input.set_neighborhood(2, input.moore_neighborhood(include_origin=True)) # input.set_neighborhood(2, [ # (0, 0, 0), # (1, 0, 0), (0, 1, 0), (0, 0, 1), # (-1, -1, 0), (-1, 1, 0), (-1, 0, -1), (-1, 0, 1), # (0, -1, -1), (0, -1, 1), (0, 1, -1), (0, 1, 1), # (-1, -1, -1), (-1, -1, 1), (-1, 1, -1), (-1, 1, 1), # (1, -1, -1), (1, -1, 1), (1, 1, -1), (1, 1, 1), # ]) for x in input.interior_points(): input[x] = random.random() coefficients = StencilGrid([4]) coefficients[0] = 1.0 coefficients[1] = 0.5 coefficients[2] = 0.25 coefficients[3] = 0.125 for n in input.neighbor_definition: print(n) output = StencilGrid([nx,ny,nz]) return input, coefficients, output
def test_von_neuman_neighborhood(self): stencil_grid = StencilGrid([2, 2]) neighborhood = stencil_grid.von_neuman_neighborhood() self._are_lists_equal( neighborhood, [(-1, 0), (0, -1), (0, 1), (1, 0)] ) self._are_lists_unequal( neighborhood, [(-1, 0), (0, -1), (0, 0), (0, 1), (1, 0)] ) stencil_grid = StencilGrid([2, 2, 2]) neighborhood = stencil_grid.von_neuman_neighborhood() self._are_lists_equal( neighborhood, [ (-1, 0, 0), (0, -1, 0), (0, 0, -1), (0, 0, 1), (0, 1, 0), (1, 0, 0), ] )
def build_data(nx, ny, nz): input = StencilGrid([nx, ny, nz]) input.set_neighborhood(2, input.moore_neighborhood(include_origin=True)) # input.set_neighborhood(2, [ # (0, 0, 0), # (1, 0, 0), (0, 1, 0), (0, 0, 1), # (-1, -1, 0), (-1, 1, 0), (-1, 0, -1), (-1, 0, 1), # (0, -1, -1), (0, -1, 1), (0, 1, -1), (0, 1, 1), # (-1, -1, -1), (-1, -1, 1), (-1, 1, -1), (-1, 1, 1), # (1, -1, -1), (1, -1, 1), (1, 1, -1), (1, 1, 1), # ]) for x in input.interior_points(): input[x] = random.random() coefficients = StencilGrid([4]) coefficients[0] = 1.0 coefficients[1] = 0.5 coefficients[2] = 0.25 coefficients[3] = 0.125 for n in input.neighbor_definition: print(n) output = StencilGrid([nx, ny, nz]) return input, coefficients, output
def test_border_points(self): stencil_grid = StencilGrid([3, 3]) border = [x for x in stencil_grid.border_points()] # self._are_lists_equal(border, [(0, 1), (2, 1), (1, 0), (1, 2), (0, 0), (0, 2), (2, 0), (2, 2)]) # print "border" # for point in stencil_grid.border_points(): # print(point) stencil_grid = StencilGrid([3, 3, 3]) border = sorted(list(stencil_grid.border_points())) # print "corners %s" % sorted(list(stencil_grid.corner_points())) # print "edges %s" % sorted(list(stencil_grid.edge_points())) border2 = sorted(list(stencil_grid.boundary_points())) # print border2 # print("len border %d len border2 %d" % (len(border), len(border2))) # print("len border %d len border2 %d" % (len(set(border)), len(set(border2)))) # print("sorted border %s" % border) # print("sorted border2 %s" % border2) self._are_lists_equal(border, border2) stencil_grid = StencilGrid([100, 99, 8, 7, 6]) border1 = sorted(list(stencil_grid.border_points())) border2 = sorted(list(stencil_grid.boundary_points())) # print("len border1 %d len border2 %d" % (len(border1), len(border2))) # print("len border1 %d len border2 %d" % (len(set(border1)), len(set(border2)))) # print("sorted border1 %s" % border1) # print("sorted border2 %s" % border2) self._are_lists_equal(list(set(border1)), border2)
def gaussian(stdev, length): result = StencilGrid([length]) scale = 1.0/(stdev*math.sqrt(2.0*math.pi)) divisor = -1.0 / (2.0 * stdev * stdev) for x in range(length): result[x] = scale * math.exp(float(x) * float(x) * divisor) return result
def test_corner_points(self): stencil_grid = StencilGrid([3, 3]) corners = [x for x in stencil_grid.corner_points()] self._are_lists_equal(corners, [(0, 0), (0, 2), (2, 0), (2, 2)]) # print "corners" # for point in stencil_grid.corner_points(): # print(point) import itertools stencil_grid = StencilGrid([10, 9, 8, 7, 6]) corners = [x for x in stencil_grid.corner_points()] ends = [[0, 9], [0, 8], [0, 7], [0, 6], [0, 5]] self._are_lists_equal(corners, itertools.product(*ends))
def test_moore_neighborhood(self): stencil_grid = StencilGrid([2, 2]) neighborhood = stencil_grid.moore_neighborhood() self._are_lists_equal(neighborhood, [(-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1)]) self._are_lists_unequal(neighborhood, [(-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 0), (0, 1), (1, -1), (1, 0), (1, 1)]) neighborhood = stencil_grid.moore_neighborhood(include_origin=True) self._are_lists_equal(neighborhood, [(-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 0), (0, 1), (1, -1), (1, 0), (1, 1)]) stencil_grid = StencilGrid([2, 2, 2]) neighborhood = stencil_grid.moore_neighborhood() self._are_lists_equal(neighborhood, [(-1, -1, -1), (-1, -1, 0), (-1, -1, 1), (-1, 0, -1), (-1, 0, 0), (-1, 0, 1), (-1, 1, -1), (-1, 1, 0), (-1, 1, 1), (0, -1, -1), (0, -1, 0), (0, -1, 1), (0, 0, -1), (0, 0, 1), (0, 1, -1), (0, 1, 0), (0, 1, 1), (1, -1, -1), (1, -1, 0), (1, -1, 1), (1, 0, -1), (1, 0, 0), (1, 0, 1), (1, 1, -1), (1, 1, 0), (1, 1, 1)]) neighborhood = stencil_grid.moore_neighborhood(include_origin=True) self._are_lists_equal(neighborhood, [(-1, -1, -1), (-1, -1, 0), (-1, -1, 1), (-1, 0, -1), (-1, 0, 0), (-1, 0, 1), (-1, 1, -1), (-1, 1, 0), (-1, 1, 1), (0, -1, -1), (0, -1, 0), (0, -1, 1), (0, 0, -1), (0, 0, 0), (0, 0, 1), (0, 1, -1), (0, 1, 0), (0, 1, 1), (1, -1, -1), (1, -1, 0), (1, -1, 1), (1, 0, -1), (1, 0, 0), (1, 0, 1), (1, 1, -1), (1, 1, 0), (1, 1, 1)])
def test_moore_neighborhood(self): stencil_grid = StencilGrid([2, 2]) neighborhood = stencil_grid.moore_neighborhood() self._are_lists_equal( neighborhood, [(-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1)] ) self._are_lists_unequal( neighborhood, [(-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 0), (0, 1), (1, -1), (1, 0), (1, 1)] ) neighborhood = stencil_grid.moore_neighborhood(include_origin=True) self._are_lists_equal( neighborhood, [(-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 0), (0, 1), (1, -1), (1, 0), (1, 1)] ) stencil_grid = StencilGrid([2, 2, 2]) neighborhood = stencil_grid.moore_neighborhood() self._are_lists_equal( neighborhood, [ (-1, -1, -1), (-1, -1, 0), (-1, -1, 1), (-1, 0, -1), (-1, 0, 0), (-1, 0, 1), (-1, 1, -1), (-1, 1, 0), (-1, 1, 1), (0, -1, -1), (0, -1, 0), (0, -1, 1), (0, 0, -1), (0, 0, 1), (0, 1, -1), (0, 1, 0), (0, 1, 1), (1, -1, -1), (1, -1, 0), (1, -1, 1), (1, 0, -1), (1, 0, 0), (1, 0, 1), (1, 1, -1), (1, 1, 0), (1, 1, 1) ] ) neighborhood = stencil_grid.moore_neighborhood(include_origin=True) self._are_lists_equal( neighborhood, [ (-1, -1, -1), (-1, -1, 0), (-1, -1, 1), (-1, 0, -1), (-1, 0, 0), (-1, 0, 1), (-1, 1, -1), (-1, 1, 0), (-1, 1, 1), (0, -1, -1), (0, -1, 0), (0, -1, 1), (0, 0, -1), (0, 0, 0), (0, 0, 1), (0, 1, -1), (0, 1, 0), (0, 1, 1), (1, -1, -1), (1, -1, 0), (1, -1, 1), (1, 0, -1), (1, 0, 0), (1, 0, 1), (1, 1, -1), (1, 1, 0), (1, 1, 1) ] )
def test_von_neuman_neighborhood(self): stencil_grid = StencilGrid([2, 2]) neighborhood = stencil_grid.von_neuman_neighborhood() self._are_lists_equal(neighborhood, [(-1, 0), (0, -1), (0, 1), (1, 0)]) self._are_lists_unequal(neighborhood, [(-1, 0), (0, -1), (0, 0), (0, 1), (1, 0)]) stencil_grid = StencilGrid([2, 2, 2]) neighborhood = stencil_grid.von_neuman_neighborhood() self._are_lists_equal(neighborhood, [ (-1, 0, 0), (0, -1, 0), (0, 0, -1), (0, 0, 1), (0, 1, 0), (1, 0, 0), ])
def test_edge_points(self): stencil_grid = StencilGrid([3, 3]) edges = [x for x in stencil_grid.edge_points()] self._are_lists_equal(edges, [(0, 1), (2, 1), (1, 0), (1, 2)])
height = width image = np.random.rand(width, height) # print("Print numpy image: ", image) stencil = np.array([[0, 1, 0], [1, -4, 1], [0, 1, 0]]) class Kernel(StencilKernel): def kernel(self, in_grid, out_grid): for x in in_grid.interior_points(): out_grid[x] = -4 * in_grid[x] for y in in_grid.neighbors(x, 1): out_grid[x] += in_grid[y] in_grid = StencilGrid([width, height]) for i in range(width): for j in range(height): in_grid[(i, j)] = image[(i, j)] # print("StencilGrid in_grid:", in_grid) iterations = 10 total = 0.0 for _ in range(iterations): with Timer() as t: out_image = convolve(image, stencil, mode='constant', cval=0.0) total += t.interval print("Numpy convolve avg: {0}".format(total / iterations)) total = 0.0 for _ in range(iterations):
time_steps = 18 class Kernel(StencilKernel): def kernel(self, in_img, out_img): for x in out_img.interior_points(): out_img[x] = in_img[x] for y in in_img.neighbors(x, 0): out_img[x] += 0.125 * in_img[y] for z in in_img.neighbors(x, 1): out_img[x] -= 0.125 * 2.0 * in_img[z] kernel = Kernel(backend='ocl') kernel.should_unroll = False out_grid = StencilGrid([time_steps, width, height]) out_grid.ghost_depth = 1 in_grid = StencilGrid([time_steps, width, height]) in_grid.ghost_depth = 1 base = 1024 r = random.seed() for i in range(width): for j in range(height): in_grid.data[(0, i, j)] = random.randrange(1024) * 1.0 in_grid.neighbor_definition[0] = [(-1, 1, 0), (-1, -1, 0), (-1, 0, 1), (-1, 0, -1)] in_grid.neighbor_definition[1] = [(-1, 0, 0), (-1, 0, 0)]
height = 130 time_steps = 18 class Kernel(StencilKernel): def kernel(self, in_img, out_img): for x in out_img.interior_points(): out_img[x] = in_img[x] for y in in_img.neighbors(x, 0): out_img[x] += 0.125 * in_img[y] for z in in_img.neighbors(x, 1): out_img[x] -= 0.125 * 2.0 * in_img[z] kernel = Kernel(backend='ocl') kernel.should_unroll = False out_grid = StencilGrid([time_steps, width, height]) out_grid.ghost_depth = 1 in_grid = StencilGrid([time_steps, width, height]) in_grid.ghost_depth = 1 base = 1024 r = random.seed() for i in range(width): for j in range(height): in_grid.data[(0, i, j)] = random.randrange(1024) * 1.0 in_grid.neighbor_definition[0] = [(-1, 1, 0), (-1, -1, 0), (-1, 0, 1), (-1, 0, -1)] in_grid.neighbor_definition[1] = [(-1, 0, 0), (-1, 0, 0)]
stdev_d = 3 stdev_s = 70 radius = 1 width = 64 + radius * 2 class Kernel(StencilKernel): def kernel(self, in_grid, out_grid): for x in out_grid.interior_points(): for y in in_grid.neighbors(x, 1): out_grid[x] += in_grid[y] kernel = Kernel() kernel.should_unroll = False out_grid1 = StencilGrid([width, width]) out_grid1.ghost_depth = radius out_grid2 = StencilGrid([width, width]) out_grid2.ghost_depth = radius in_grid = StencilGrid([width, width]) in_grid.ghost_depth = radius for x in range(0, width): for y in range(0, width): in_grid.data[(x, y)] = 1.0 # for x in range(-radius, radius+1): # for y in range(-radius, radius+1): # in_grid.neighbor_definition[1].append((x, y))
scale = 1.0/(stdev*math.sqrt(2.0*math.pi)) divisor = -1.0 / (2.0 * stdev * stdev) for x in range(length): result[x] = scale * math.exp(float(x) * float(x) * divisor) return result def distance(x, y): return math.sqrt(sum([(x[i]-y[i])**2 for i in range(0, len(x))])) pixels = map(ord, list(image_in.read(width * height))) # Read in grayscale values intensity = float(sum(pixels))/len(pixels) kernel = Kernel() kernel.should_unroll = False out_grid = StencilGrid([width, height]) out_grid.ghost_depth = radius in_grid = StencilGrid([width, height]) in_grid.ghost_depth = radius for x in range(-radius, radius+1): for y in range(-radius, radius+1): in_grid.neighbor_definition[1].append((x, y)) for x in range(0, width): for y in range(0, height): in_grid.data[(x, y)] = pixels[y * width + x] gaussian1 = gaussian(stdev_d, radius*2) gaussian2 = gaussian(stdev_s, 256) kernel.kernel(in_grid, gaussian1, gaussian2, out_grid)
height = 50 stdev_d = 3 stdev_s = 70 radius = 1 width = 64 + radius*2 class Kernel(StencilKernel): def kernel(self, in_grid, out_grid): for x in out_grid.interior_points(): for y in in_grid.neighbors(x, 1): out_grid[x] += in_grid[y] kernel = Kernel() kernel.should_unroll = False out_grid1 = StencilGrid([width, width]) out_grid1.ghost_depth = radius out_grid2 = StencilGrid([width, width]) out_grid2.ghost_depth = radius in_grid = StencilGrid([width, width]) in_grid.ghost_depth = radius for x in range(0, width): for y in range(0, width): in_grid.data[(x, y)] = 1.0 # for x in range(-radius, radius+1): # for y in range(-radius, radius+1): # in_grid.neighbor_definition[1].append((x, y))
for x in in_grid.interior_points(): out_grid[x] = -4 * in_grid[x] for y in in_grid.neighbors(x, 1): out_grid[x] += in_grid[y] x = [] iterations = 10 results = [[] for _ in range(7)] totals = [0.0 for _ in range(7)] for width in (2**x + 4 for x in range(10, 14)): height = width image = np.random.rand(width, height) in_grid = StencilGrid([width, height]) for i in range(width): for j in range(height): in_grid[(i, j)] = image[(i, j)] in_grid.neighbor_definition[1] = [ (-2, 2), (-1, 2), (0, 2), (1, 2), (2, 2), (-2, 1), (-1, 1), (0, 1), (1, 1), (2, 1), (-2, 0), (-1, 0), (1, 0), (2, 0), (-2, -1), (-1, -1), (0, -1), (1, -1), (2, -1), (-2, -2), (-1, -2), (0, -2), (1, -2), (2, -2) ] in_grid.ghost_depth = 2 out_grid = StencilGrid([width, height]) out_grid.ghost_depth = 2
out_grid[x] = -4 * in_grid[x] for y in in_grid.neighbors(x, 1): out_grid[x] += in_grid[y] x = [] iterations = 10 results = [[] for _ in range(7)] totals = [0.0 for _ in range(7)] for width in (2**x + 4 for x in range(10, 14)): height = width image = np.random.rand(width, height) in_grid = StencilGrid([width, height]) for i in range(width): for j in range(height): in_grid[(i, j)] = image[(i, j)] in_grid.neighbor_definition[1] = [(-2, 2), (-1, 2), (0, 2), (1, 2), (2, 2), (-2, 1), (-1, 1), (0, 1), (1, 1), (2, 1), (-2, 0), (-1, 0), (1, 0), (2, 0), (-2, -1), (-1, -1), (0, -1), (1, -1), (2, -1), (-2, -2), (-1, -2), (0, -2), (1, -2), (2, -2)] in_grid.ghost_depth = 2 out_grid = StencilGrid([width, height]) out_grid.ghost_depth = 2 # print("StencilGrid in_grid:", in_grid)
def __init__(self, alpha, beta): super(LaplacianKernel, self).__init__(backend='c') self.constants = {'alpha': alpha, 'beta': beta} def kernel(self, in_grid, out_grid): for x in in_grid.interior_points(): out_grid[x] = alpha * in_grid[x] for y in in_grid.neighbors(x, 1): out_grid[x] += beta * in_grid[y] # nx = int(sys.argv[1]) # ny = int(sys.argv[2]) # nz = int(sys.argv[3]) nx = 1026 ny = 1026 nz = 34 input_grid = StencilGrid([nx, ny, nz]) output_grid = StencilGrid([nx, ny, nz]) # input_grid.data = ones([nx, ny, nz], dtype=float32) # for x in input_grid.interior_points(): # input_grid[x] = random.randint(nx * ny * nz) laplacian = LaplacianKernel(alpha, beta) laplacian.kernel(input_grid, output_grid) # for i in range(50): # for x in input_grid.interior_points(): # input_grid[x] = random.randint(nx * ny * nz) # laplacian.kernel(input_grid, output_grid)