using feval() to launch CUDA kernels from Matlab
11 views (last 30 days)
Show older comments
I'm using feval() for rapid prototyping of tests for my CUDA kernels, written in CUDA C. Works like a charm, but I have two questions:
1. The explanation of the treatment of input/output kernel arguments in the feval() function documentation leaves me wondering is there any data copying overhead involved? Namely, let's say that I have following kernel implemented in file addAssign.cu:
_global__ void addAssign(int const n, float* __restrict__ y, float const* __restrict__ x)
{
/* do y[i] += x[i] here */
}
and that I call it from Matlab as follows:
n = 10000;
x = gpuArray(single(rand(n, 1)));
y = gpuArray(single(rand(n, 1)));
kernel = parallel.gpu.CUDAKernel('addAssign.ptx', 'addAssign.cu');
kernel.ThreadBlockSize = [128, 1, 1];
kernel.GridSize = [ceil(n / 128), 1, 1];
y = feval(kernel, n, y, x);
So, is Matlab runtime going to do any data copying for assignment in the last statement, or it is going to recognize that 'y' appears both as input and output, and that the kernel will properly update it? I'm asking because I've noticed that if I omit the assignment, values of 'y' won't get updated.
2. How to use gputimeit() with feval() run as above? It seems like gputimeit() would discard return values from function passed as argument.
Thanks.
0 Comments
Answers (1)
Edric Ellis
on 14 Dec 2017
1. Input/output variables to CUDAKernel.feval are operated on in-place in the usual way for MATLAB data. See this blog entry for more details. In other words, providing the type of y matches exactly the prototype of your kernel (i.e. it doesn't need to be cast to a different numeric type), and that it appears on the left-hand side too, then it is eligible for in-place operation.
2. You could use the wait method of GPUDevice rather than gputimeit to ensure you're timing exactly what you expect. (Behind the scenes, gputimeit uses the wait method to ensure execution has completed).
See Also
Categories
Find more on GPU Computing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!