Example #1
0
    def __init__(self, width, height, pure_python=False, should_unroll=False):
        self.width = width
        self.height = height

        self.kernel = IteratedConwayKernel(77)
        self.kernel.pure_python = pure_python
        self.kernel.should_unroll = should_unroll
        # kernel.pure_python = True

        # create a stencil grid for t+1
        self.current_grid = StencilGrid([height, width])
        all_neighbors = [(x, y) for x in range(-1, 2) for y in range(-1, 2)]
        all_neighbors.remove((0, 0))
        self.current_grid.neighbor_definition.append(all_neighbors)
        self.future_grid = copy.deepcopy(
            self.current_grid
        )  # this will be swapped to current after each iteration

        # Randomly initialize a quarter of the cells to 1
        for x in self.current_grid.interior_points():
            if np.random.random() > 0.75:
                self.current_grid[x] = 1

        self.new_state_map = StencilGrid([18])
        for index, new_state in enumerate(
            [0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0]):
            self.new_state_map[index] = new_state
Example #2
0
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
Example #3
0
class GameRunner(object):
    def __init__(self, width, height, pure_python=False, should_unroll=False):
        self.width = width
        self.height = height

        self.kernel = IteratedConwayKernel(77)
        self.kernel.pure_python = pure_python
        self.kernel.should_unroll = should_unroll
        # kernel.pure_python = True

        # create a stencil grid for t+1
        self.current_grid = StencilGrid([height, width])
        all_neighbors = [(x, y) for x in range(-1, 2) for y in range(-1, 2)]
        all_neighbors.remove((0, 0))
        self.current_grid.neighbor_definition.append(all_neighbors)
        self.future_grid = copy.deepcopy(self.current_grid)  # this will be swapped to current after each iteration

        # Randomly initialize a quarter of the cells to 1
        for x in self.current_grid.interior_points():
            if np.random.random() > 0.75:
                self.current_grid[x] = 1

        self.new_state_map = StencilGrid([18])
        for index, new_state in enumerate([0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0]):
            self.new_state_map[index] = new_state

    def set_pure_python(self, new_mode):
        self.kernel.pure_python = new_mode

    def run_game(self, generations=1):
        for generation in range(generations):
            self.kernel.kernel(self.current_grid, self.new_state_map, self.future_grid)
            self.current_grid, self.future_grid = self.future_grid, self.current_grid
            print("gen %s" % generation)

    def __call__(self, *args, **kwargs):
        self.run_game()

    def run(self):
        self.run_game()

    def render(self):
        GameRunner.render_grid(self.current_grid)

    @staticmethod
    def render_grid(sk, msg="---"):
        """
        Simplistic render of a life board
        """
        print(msg)
        for h in range(sk.shape[0]):
            for w in range(sk.shape[1]):
                if sk[h][w] > 0:
                    print('*', end='')
                else:
                    print(' ', end='')
            print('')
Example #4
0
    def __init__(self, width, height, pure_python=False, should_unroll=False):
        self.width = width
        self.height = height

        self.kernel = IteratedConwayKernel(77)
        self.kernel.pure_python = pure_python
        self.kernel.should_unroll = should_unroll
        # kernel.pure_python = True

        # create a stencil grid for t+1
        self.current_grid = StencilGrid([height, width])
        all_neighbors = [(x, y) for x in range(-1, 2) for y in range(-1, 2)]
        all_neighbors.remove((0, 0))
        self.current_grid.neighbor_definition.append(all_neighbors)
        self.future_grid = copy.deepcopy(self.current_grid)  # this will be swapped to current after each iteration

        # Randomly initialize a quarter of the cells to 1
        for x in self.current_grid.interior_points():
            if np.random.random() > 0.75:
                self.current_grid[x] = 1

        self.new_state_map = StencilGrid([18])
        for index, new_state in enumerate([0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0]):
            self.new_state_map[index] = new_state
Example #5
0
time_steps = 500


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()
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)]

Example #6
0
height = 1600
time_steps = 500


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()
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)]

Example #7
0
alpha = 0.5
beta = 1.0


class LaplacianKernel(StencilKernel):
    def __init__(self, alpha, beta):
        super(LaplacianKernel, self).__init__()
        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])
input_grid = StencilGrid([nx, ny, nz])
output_grid = StencilGrid([nx, ny, nz])

for x in input_grid.interior_points():
    input_grid[x] = random.randint(nx * ny * nz)

laplacian = LaplacianKernel(alpha, beta)
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)
Example #8
0
        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
# pixels = 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)

Example #9
0
class GameRunner(object):
    def __init__(self, width, height, pure_python=False, should_unroll=False):
        self.width = width
        self.height = height

        self.kernel = IteratedConwayKernel(77)
        self.kernel.pure_python = pure_python
        self.kernel.should_unroll = should_unroll
        # kernel.pure_python = True

        # create a stencil grid for t+1
        self.current_grid = StencilGrid([height, width])
        all_neighbors = [(x, y) for x in range(-1, 2) for y in range(-1, 2)]
        all_neighbors.remove((0, 0))
        self.current_grid.neighbor_definition.append(all_neighbors)
        self.future_grid = copy.deepcopy(
            self.current_grid
        )  # this will be swapped to current after each iteration

        # Randomly initialize a quarter of the cells to 1
        for x in self.current_grid.interior_points():
            if np.random.random() > 0.75:
                self.current_grid[x] = 1

        self.new_state_map = StencilGrid([18])
        for index, new_state in enumerate(
            [0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0]):
            self.new_state_map[index] = new_state

    def set_pure_python(self, new_mode):
        self.kernel.pure_python = new_mode

    def run_game(self, generations=1):
        for generation in range(generations):
            self.kernel.kernel(self.current_grid, self.new_state_map,
                               self.future_grid)
            self.current_grid, self.future_grid = self.future_grid, self.current_grid
            print("gen %s" % generation)

    def __call__(self, *args, **kwargs):
        self.run_game()

    def run(self):
        self.run_game()

    def render(self):
        GameRunner.render_grid(self.current_grid)

    @staticmethod
    def render_grid(sk, msg="---"):
        """
        Simplistic render of a life board
        """
        print(msg)
        for h in range(sk.shape[0]):
            for w in range(sk.shape[1]):
                if sk[h][w] > 0:
                    print('*', end='')
                else:
                    print(' ', end='')
            print('')
Example #10
0
    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
# pixels = 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)