main content

stencil processing on gpu -凯发k8网页登录

this example shows how to generate cuda® kernels for stencil type operations by implementing "game of life" by john h. conway.

"game of life" is a zero-player cellular automaton game that consists of a collection of cells (population) in a rectangular grid (universe). the cells evolve at discrete time steps known as generations. a set of mathematical rules applied to the cells and its neighbors control their life, death,and reproduction. this "game of life" implementation is based on the example provided in the e-book by cleve moler. the implementation follows these rules:

  • cells are arranged in a 2-d grid.

  • at each step, the vitality of the eight nearest neighbors of each cell determines its fate.

  • any cell with exactly three live neighbors comes to life at the next step.

  • a live cell with exactly two live neighbors remains alive at the next step.

  • all other cells (including those with more than three neighbors) die at the next step or remain empty.

here are some examples of how a cell is updated.

many array operations can be expressed as a stencil operation, where each element of the output array depends on a small region of the input array. the stencil in this example is the 3-by-3 region around each cell. finite differences, convolution, median filtering, and finite-element methods are examples of other operations that stencil processing can perform.

third-party prerequisites

required

this example generates cuda mex and has the following third-party requirements.

  • cuda enabled nvidia® gpu and compatible driver.

optional

for non-mex builds such as static, dynamic libraries or executables, this example has the following additional requirements.

  • nvidia toolkit.

  • environment variables for the compilers and libraries. for more information, see third-party hardware and .

verify gpu environment

to verify that the compilers and libraries necessary for running this example are set up correctly, use the function.

envcfg = coder.gpuenvconfig('host');
envcfg.basiccodegen = 1;
envcfg.quiet = 1;
coder.checkgpuinstall(envcfg);

generate a random initial population

being that the game is zero-player, the evolution of the game is determined by its initial state. for this example, an initial population of cells is created on a two-dimensional grid with approximately 25% of the locations being alive.

gridsize = 500;
numgenerations = 100;
initialgrid = (rand(gridsize,gridsize) > .75);
% draw the initial grid
imagesc(initialgrid);
colormap([1 1 1;0 0.5 0]);
title('initial grid');

play the game of life

the gameoflife_orig.m function is a fully vectorized implementation of "game of life". the function updates all cells on the grid in one pass per their generation.

type gameoflife_orig
%% matlab vectorized implementation
function grid = gameoflife_orig(initialgrid)
% 凯发官网入口首页 copyright 2016-2019 the mathworks, inc. 
numgenerations = 100;
grid = initialgrid;
[gridsize,~] = size(initialgrid);
% loop through each generation updating the grid and displaying it.
for generation = 1:numgenerations
    grid = updategrid(grid, gridsize);
    
    imagesc(grid);
    colormap([1 1 1;0 0.5 0]);
    title(['grid at iteration ',num2str(generation)]);
    drawnow;
end
    function x = updategrid(x, n)
        % index vectors increase or decrease the centered index by one
        % thereby accessing neighbors to the left,right,up, and down.
        p = [1 1:n-1];
        q = [2:n n];
        % count how many of the eight neighbors are alive.
        neighbors = x(:,p)   x(:,q)   x(p,:)   x(q,:)   ...
            x(p,p)   x(q,q)   x(p,q)   x(q,p);
        % a live cell with two live neighbors, or any cell with
        % three live neighbors, is alive at the next step.
        x = (x & (neighbors == 2)) | (neighbors == 3);
    end
end

play the game by calling the gameoflife_orig function with an initial population. the game iterates through 100 generations and displays the population at each generation.

gameoflife_orig(initialgrid);

convert the game of life for gpu code generation

looking at the calculations in the updategrid function, it is apparent that the same operations are applied at each grid location independently. however, each cell must know about its eight neighbors. the modified gameoflife_stencil.m function uses the pragma to compute a 3-by-3 region around each cell. the gpu coder™ implementation of the stencil kernel computes one element of the grid in each thread and uses shared memory to improve memory bandwidth and data locality.

type gameoflife_stencil
function grid = gameoflife_stencil(initialgrid) %#codegen
% 凯发官网入口首页 copyright 2016-2019 the mathworks, inc.
numgenerations = 100;
grid = initialgrid;
% loop through each generation updating the grid.
for generation = 1:numgenerations
    grid = stencilfun(@updateelem, grid, [3,3], shape='same');
end
end
function x = updateelem(window)
neighbors = window(1,1)   window(1,2)   window(1,3) ...
      window(2,1)   window(2,3) ...
      window(3,1)   window(3,2)   window(3,3);
x = (window(2,2) & (neighbors == 2)) | (neighbors == 3);
end

generate cuda mex for the function

to generate cuda mex for the gameoflife_stencil function, create a gpu code configuration object, and then use the codegen command.

cfg = coder.gpuconfig('mex');
codegen -config cfg -args {initialgrid}  gameoflife_stencil
code generation successful.

run the mex function

run the generated gameoflife_stencil_mex with the random initial population.

gridgpu = gameoflife_stencil_mex(initialgrid);
% draw the grid after 100 generations
imagesc(gridgpu);
colormap([1 1 1;0 0.5 0]);
title('final grid - cuda mex');

see also

functions

objects

  • | | |

related topics

网站地图