Conway's Game of Life Accelerated with PyCUDA

Luis Villasenor

lvillasen@gmail.com

Usage

python GameOfLife.py n n_iter

where n is the board size and n_iter the number of iterations

License of this example:

GPLv3

Date:

3/26/16

PyCUDA version:

   1 # Conway's Game of Life Accelerated with PyCUDA
   2 # Luis Villasenor
   3 # lvillasen@gmail.com
   4 # 3/26/2016
   5 # Licence: GPLv3
   6 # Usage: python GameOfLife.py n n_iter
   7 # where n is the board size and n_iter the number of iterations
   8 import pycuda.driver as cuda
   9 import pycuda.tools
  10 import pycuda.autoinit
  11 import pycuda.gpuarray as gpuarray
  12 from pycuda.compiler import SourceModule
  13 import sys
  14 import numpy as np
  15 from pylab import cm as cm
  16 import matplotlib.pyplot as plt
  17 n=int(sys.argv[1])
  18 n_iter=int(sys.argv[2])
  19 n_block=16
  20 n_grid=int(n/n_block);
  21 n=n_block*n_grid;
  22 def random_init(n):
  23     #np.random.seed(100)
  24     M=np.zeros((n,n)).astype(np.int32)
  25     for i in range(n):
  26         for j in range(n):
  27             M[j,i]=np.int32(np.random.randint(2))
  28     return M
  29 mod = SourceModule("""
  30 __global__ void step(int *C, int *M)
  31 {
  32   int count;
  33   int n_x = blockDim.x*gridDim.x;
  34   int i = threadIdx.x + blockDim.x*blockIdx.x;
  35   int j = threadIdx.y + blockDim.y*blockIdx.y;
  36   int threadId = j*n_x+i;
  37   int i_left; int i_right; int j_down; int j_up;
  38   if(i==0) {i_left=n_x-1;} else {i_left=i-1;}
  39   if(i==n_x-1) {i_right=0;} else {i_right=i+1;}
  40   if(j==0) {j_down=n_x-1;} else {j_down=j-1;}
  41   if(j==n_x-1) {j_up=0;} else {j_up=j+1;}
  42   count = C[j*n_x+i_left] + C[j_down*n_x+i]
  43     + C[j*n_x+i_right] + C[j_up*n_x+i] + C[j_up*n_x+i_left]
  44     + C[j_down*n_x+i_right] + C[j_down*n_x+i_left]
  45     + C[j_up*n_x+i_right];
  46 
  47 // Modify matrix M according to the rules B3/S23:
  48 //A cell is "Born" if it has exactly 3 neighbours,
  49 //A cell "Survives" if it has 2 or 3 living neighbours; it dies otherwise.
  50   if(count < 2 || count > 3) M[threadId] = 0; // cell dies
  51   if(count == 2) M[threadId] = C[threadId];// cell stays the same
  52   if(count == 3) M[threadId] = 1; // cell either stays alive, or is born
  53 }
  54 """)
  55 func = mod.get_function("step")
  56 C=random_init(n)
  57 M = np.empty_like(C)
  58 C_gpu = gpuarray.to_gpu( C )
  59 M_gpu = gpuarray.to_gpu( M )
  60 for k in range(n_iter):
  61   func(C_gpu,M_gpu,block=(n_block,n_block,1),grid=(n_grid,n_grid,1))
  62   C_gpu, M_gpu = M_gpu, C_gpu
  63 print("%d live cells after %d iterations" %(np.sum(C_gpu.get()),n_iter))
  64 fig = plt.figure(figsize=(12,12))
  65 ax = fig.add_subplot(111)
  66 fig.suptitle("Conway's Game of Life Accelerated with PyCUDA")
  67 ax.set_title('Number of Iterations = %d'%(n_iter))
  68 myobj =plt.imshow(C_gpu.get(),origin='lower',cmap='Greys',  interpolation='nearest',vmin=0, vmax=1)
  69 plt.pause(.01)
  70 plt.draw()
  71 m=n_iter
  72 while True:
  73     m+=1
  74     func(C_gpu,M_gpu,block=(n_block,n_block,1),grid=(n_grid,n_grid,1))
  75     C_gpu, M_gpu = M_gpu, C_gpu
  76     myobj.set_data(C_gpu.get())
  77     ax.set_title('Number of Iterations = %d'%(m))
  78     plt.pause(.01)
  79     plt.draw()

PyCuda/Examples/GameOfLife (last edited 2016-03-26 20:10:29 by dsl-187-193-8-33-dyn)