call custom cuda device function from the generated code
if you have highly optimized cuda® code for certain subfunctions that you want to incorporate into your generated code, gpu coder™ extends the functionality to help you achieve this goal.
the external cuda function must use the __device__
qualifier to execute the
function on the gpu device. these device functions are different from global functions
(kernels) in that they can only be called from other device or global functions. therefore the
coder.ceval
calls to the device functions must be from within a loop that
gets mapped to a kernel. for information on integrating cuda kernels with the generated code, see .
note
code generation fails if the loop containing the coder.ceval
calls
cannot be mapped to a kernel. see the troubleshooting topic in the gpu coder documentation to check for issues preventing kernel creation and their
suggested workarounds. if your matlab® code section contains unsupported functions, then you must remove the
coder.ceval
calls from such sections.
call __usad4_wrap
cuda device function
the stereo disparity example measures the distance between two corresponding points in
the left and the right image of a stereo pair. the
stereodisparity_cuda_sample
entry-point function calls the
__usad4_wrap
external device function by using the
coder.ceval
function.
%% modified algorithm for stereo disparity block matching % in this implementation instead of finding shifted image ,indices are mapped % accordingly to save memory and some processing rgba column major packed % data is used as input for compatibility with cuda intrinsics. convolution % is performed using separable filters (horizontal and then vertical) function [out_disp] = stereodisparity_cuda_sample(img0,img1) coder.cinclude('cuda_intrinsic.h'); % gpu code generation pragma coder.gpu.kernelfun; %% stereo disparity parameters % win_rad is the radius of the window to be operated,min_disparity is the % minimum disparity level the search continues for, max_disparity is the maximum % disparity level the search continues for. win_rad = 8; min_disparity = -16; max_disparity = 0; %% image dimensions for loop control % the number of channels packed are 4 (rgba) so as nchannels are 4 [imgheight,imgwidth]=size(img0); nchannels = 4; imgheight = imgheight/nchannels; %% to store the raw differences diff_img = zeros([imgheight 2*win_rad,imgwidth 2*win_rad],'int32'); %to store the minimum cost min_cost = zeros([imgheight,imgwidth],'int32'); min_cost(:,:) = 99999999; % store the final disparity out_disp = zeros([imgheight,imgwidth],'int16'); %% filters for aggregating the differences % filter_h is the horizontal filter used in separable convolution % filter_v is the vertical filter used in separable convolution which % operates on the output of the row convolution filt_h = ones([1 17],'int32'); filt_v = ones([17 1],'int32'); %% main loop that runs for all the disparity levels. this loop is currently % expected to run on cpu. for d=min_disparity:max_disparity % find the difference matrix for the current disparity level. expect % this to generate a kernel function. coder.gpu.kernel; for colidx=1:imgwidth 2*win_rad coder.gpu.kernel; for rowidx=1:imgheight 2*win_rad % row index calculation ind_h = rowidx - win_rad; % column indices calculation for left image ind_w1 = colidx - win_rad; % row indices calculation for right image ind_w2 = colidx d - win_rad; % border clamping for row indices if ind_h <= 0 ind_h = 1; end if ind_h > imgheight ind_h = imgheight; end % border clamping for column indices for left image if ind_w1 <= 0 ind_w1 = 1; end if ind_w1 > imgwidth ind_w1 = imgwidth; end % border clamping for column indices for right image if ind_w2 <= 0 ind_w2 = 1; end if ind_w2 > imgwidth ind_w2 = imgwidth; end % in this step, sum of absolute differences is performed % across four channels. this piece of code is suitable % for replacement with sad intrinsics tdiff = int32(0); tdiff = coder.ceval('-gpudevicefcn', '__usad4_wrap', coder.rref(img0((ind_h-1)*(nchannels) 1,ind_w1)), coder.rref(img1((ind_h-1)*(nchannels) 1,ind_w2))); %store the sad cost into a matrix diff_img(rowidx,colidx) = tdiff; end end % aggregating the differences using separable convolution. expect this % to generate two kernel using shared memory.the first kernel is the % convolution with the horizontal kernel and second kernel operates on % its output the column wise convolution. cost_v = conv2(diff_img,filt_h,'valid'); cost = conv2(cost_v,filt_v,'valid'); % this part updates the min_cost matrix with by comparing the values % with current disparity level. expect to generate a kernel for this. for ll=1:imgwidth for kk=1:imgheight % load the cost temp_cost = int32(cost(kk,ll)); % compare against the minimum cost available and store the % disparity value if min_cost(kk,ll) > temp_cost min_cost(kk,ll) = temp_cost; out_disp(kk,ll) = abs(d) 8; end end end end end
the definition for the __usad4_wrap
is written in an external file
cuda_intrinsic.h
. the file is located in the same folder as the
entry-point function.
__device__ unsigned int __usad4(unsigned int a, unsigned int b, unsigned int c=0) { unsigned int result; #if (__cuda_arch__ >= 300) // kepler (sm 3.x) supports a 4 vector sad simd asm("vabsdiff4.u32.u32.u32.add" " %0, %1, %2, %3;": "=r"(result):"r"(a), "r"(b), "r"(c)); #else // sm 2.0 // fermi (sm 2.x) supports only 1 sad simd, // so there are 4 instructions asm("vabsdiff.u32.u32.u32.add" " %0, %1.b0, %2.b0, %3;": "=r"(result):"r"(a), "r"(b), "r"(c)); asm("vabsdiff.u32.u32.u32.add" " %0, %1.b1, %2.b1, %3;": "=r"(result):"r"(a), "r"(b), "r"(result)); asm("vabsdiff.u32.u32.u32.add" " %0, %1.b2, %2.b2, %3;": "=r"(result):"r"(a), "r"(b), "r"(result)); asm("vabsdiff.u32.u32.u32.add" " %0, %1.b3, %2.b3, %3;": "=r"(result):"r"(a), "r"(b), "r"(result)); #endif return result; } __device__ unsigned int packbytes(const uint8_t *inbytes) { unsigned int packed = inbytes[0] | (inbytes[1] << 8) | (inbytes[2] << 16) | (inbytes[3] << 24); return packed; } __device__ unsigned int __usad4_wrap(const uint8_t *a, const uint8_t *b) { unsigned int x = packbytes(a); unsigned int y = packbytes(b); return __usad4(x, y); }
generate cuda code
generate cuda code by creating a code configuration object. specify the location of the
custom c files by setting custom code properties (custominclude
) on
configuration objects. the following is an example code generation script that points to the
location of cuda_intrinsic.h
file.
cfg = coder.gpuconfig('mex'); cfg.custominclude = pwd; codegen -config cfg -args {imgrgb0, imgrgb1} stereodisparity_cuda_sample_intrinsic;
generated code
gpu coder creates four kernels. the following is a snippet of the generated cuda code.
e_stereodisparity_cuda_sample_i<<>> (gpu_img1, gpu_img0, d, gpu_diff_img);*/ /* aggregating the differences using separable convolution.*/ /* expect this to generate two kernel using shared memory.*/ /* the first kernel is the convolution with the horizontal kernel and*/ /* second kernel operates on its output the column wise convolution. */ f_stereodisparity_cuda_sample_i<< >> (gpu_diff_img, gpu_a); g_stereodisparity_cuda_sample_i<< >> (gpu_a, gpu_cost_v); h_stereodisparity_cuda_sample_i<< >> (gpu_a, gpu_cost_v); /* this part updates the min_cost matrix with by comparing the values */ /* with current disparity level. expect to generate a kernel for this. */ i_stereodisparity_cuda_sample_i<< >> (d, gpu_cost, gpu_out_disp, gpu_min_cost);
the e_stereodisparity_cuda_sample_i
kernel is the one that calls the
__usad4_wrap
device function. the following is a snippet of
e_stereodisparity_cuda_sample_i
kernel code.
static __global__ __launch_bounds__(512, 1) void e_stereodisparity_cuda_sample_i (const uint8_t *img1, const uint8_t *img0, int32_t d, int32_t *diff_img) { ... /* in this step, sum of absolute differences is performed */ /* across four channels. this piece of code is suitable */ /* for replacement with sad intrinsics */ temp_cost = __usad4_wrap(&img0[((ind_h - 1) << 2) 2132 * (ind_w1 - 1)], &img1[((ind_h - 1) << 2) 2132 * (temp_cost - 1)]); /* store the sad cost into a matrix */ diff_img[rowidx 549 * colidx] = temp_cost; } }
see also
functions
codegen
| | | | |
objects
- | | |