gpu code generation: the mandelbrot set -凯发k8网页登录
this example shows how to generate cuda® code from a simple matlab® function by using gpu coder™. a mandelbrot set implementation by using standard matlab commands acts as the entry-point function. this example uses the codegen
command to generate a mex function that runs on the gpu. you can run the mex function to check for run-time errors.
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);
mandelbrot set
the mandelbrot set is the region in the complex plane consisting of the values for which the trajectories defined by
remain bounded at . the overall geometry of the mandelbrot set is shown in the figure. this view does not have the resolution to show the richly detailed structure of the fringe just outside the boundary of the set.
define input regions
pick a set of limits that specify a highly zoomed part of the mandelbrot set in the valley between the main cardioid and the bulb to its left. a 1000x1000
grid of and is created between these two limits. the mandelbrot algorithm is then iterated at each grid location. an iteration number of 500 is enough to render the image in full resolution.
maxiterations = 500; gridsize = 1000; xlim = [-0.748766713922161, -0.748766707771757]; ylim = [ 0.123640844894862, 0.123640851045266]; x = linspace( xlim(1), xlim(2), gridsize ); y = linspace( ylim(1), ylim(2), gridsize ); [xgrid,ygrid] = meshgrid( x, y );
the mandelbrot entry-point function
the mandelbrot_count.m
entry-point function contains a vectorized implementation of the mandelbrot set based on the code provided in the e-book by cleve moler. the %#codegen directive turns on matlab for code generation error checking. when gpu coder encounters the coder.gpu.kernelfun
pragma, it attempts to parallelize all the computation within this function, and then maps it to the gpu.
type mandelbrot_count
function count = mandelbrot_count(maxiterations, xgrid, ygrid) %#codegen % 凯发官网入口首页 copyright 2016-2019 the mathworks, inc. z0 = xgrid 1i*ygrid; count = ones(size(z0)); % map computation to gpu. coder.gpu.kernelfun; z = z0; for n = 0:maxiterations z = z.*z z0; inside = abs(z)<=2; count = count inside; end count = log(count);
test the functionality of mandelbrot_count
run the mandelbrot_count
function with the xgrid, ygrid values that were previously generated, and then plot the results.
count = mandelbrot_count(maxiterations, xgrid, ygrid); figure(2), imagesc( x, y, count ); colormap( [jet();flipud( jet() );0 0 0] ); title('mandelbrot set on matlab'); axis off
generate cuda mex for the function
to generate cuda mex for the mandelbrot_count
function, create a gpu code configuration object and run the codegen
command. because of architectural differences between the cpu and gpu, numeric verification does not always match. this scenario is true when using the single data type in your matlab code and performing accumulation operations on these single data type values. like this mandelbrot example even the double data types cause numeric errors. one reason for this mismatch is that the gpu floating-point units use fused floating-point multiply-add (fmad) instructions and the cpu does not use these instructions. the fmad=false
option that is passed to the nvcc
compiler turns off this fmad optimization.
cfg = coder.gpuconfig('mex'); cfg.gpuconfig.compilerflags = '--fmad=false'; codegen -config cfg -args {maxiterations,xgrid,ygrid} mandelbrot_count
code generation successful: to view the report, open('codegen/mex/mandelbrot_count/html/report.mldatx').
run the mex function
after generating a mex function, verify that it has the same functionality as the original matlab entry-point function. run the generated mandelbrot_count_mex
and plot the results.
countgpu = mandelbrot_count_mex(maxiterations, xgrid, ygrid); figure(2), imagesc( x, y, countgpu ); colormap( [jet();flipud( jet() );0 0 0] ); title('mandelbrot set on gpu'); axis off
see also
apps
functions
codegen
| | |
objects
- | |