Thrust Example
With Thrust library support in GPU Coder™, you can take advantage of GPU-accelerated primitives such as sort to implement
complex high-performance parallel applications. When your
MATLAB® code uses gpucoder.sort
function instead of sort
, GPU Coder can generate calls to the Thrust sort primitives.
This example generates CUDA® code to sort the columns of a matrix in descending order. In one file, write an
entry-point function mySort
that accepts a matrix inputs
A
. Use the gpucoder.sort
function to sort the columns
of A
in descending order.
function B = mySort(A) B = gpucoder.sort(A, 1, 'descend'); end
Use the codegen
function to generate CUDA MEX
function.
codegen -config coder.gpuConfig('mex') -args {ones(1024,1024,'double')} -report mySort
Generated CUDA Code
The following is a snippet of the generated code. The Thrust library call is denoted by
thrustSortImpl
... cudaMalloc(&gpu_inDims, 8ULL); cudaMalloc(&gpu_B, 8388608ULL); cudaMalloc(&gpu_A, 8388608ULL); mySort_kernel1<<<dim3(1U, 1U, 1U), dim3(32U, 1U, 1U)>>>(*gpu_inDims); cudaMemcpy(gpu_A, (void *)&A[0], 8388608ULL, cudaMemcpyHostToDevice); mySort_kernel2<<<dim3(2048U, 1U, 1U), dim3(512U, 1U, 1U)>>>(*gpu_A, *gpu_B); cudaMemcpy(&inDims[0], gpu_inDims, 8ULL, cudaMemcpyDeviceToHost); thrustSortImpl(&(*gpu_B)[0], 2, &inDims[0], 1, 'd', false); cudaMemcpy(&B[0], gpu_B, 8388608ULL, cudaMemcpyDeviceToHost); ...