Code covered by the BSD License

### Highlights from 3D trilinear interpolation using GPU

5.0
5.0 | 2 ratings Rate this file 29 Downloads (last 30 days) File Size: 4.85 KB File ID: #40044

# 3D trilinear interpolation using GPU

by

### Jun Tan (view profile)

28 Jan 2013 (Updated )

3D trilinear interpolation using GPU. 20 times faster.

File Information
Description

------------------------------------------------------------------------------------------------------------------------
3D trilinear interpolation using GPU
------------------------------------------------------------------------------------------------------------------------
Syntax:
vi = interp3_gpu(x, y, z, v, xi, yi, zi);

(Same as Matlab interp3's: VI = interp3(X,Y,Z,V,XI,YI,ZI)
see http://www.mathworks.com/help/matlab/ref/interp3.html)

Input x/y/z should be 1D array of the coordinate grid positions of the 3D matrix v, see the example, while xi/yi/zi don't have this requirement.

------------------------------------------------------------------------------------------------------------------------
Example:
(Similar to Matlab's example, with increased matrix sizes and finer colormap.)
[xFlow, yFlow, zFlow, vFlow] = flow(100);
[xInterp, yInterp, zInterp] = meshgrid(0.1 : 0.025 : 10, -3 : 0.025 : 3, -3 : 0.025 : 3);
vInterp = interp3_gpu(xFlow(1, :, 1), yFlow(:, 1, 1), zFlow(1, 1, :), vFlow, xInterp, yInterp, zInterp);
% Must pass 1D vector to arguments 1-3.
figure('color', [1 1 1]);
slice(xInterp, yInterp, zInterp, double(vInterp), [6 9.5], 2, [-2 0.2]);
daspect([1 1 1]);
colormap(jet(256));

------------------------------------------------------------------------------------------------------------------------
Result:
Can be 20 times faster than Matlab interp3.

------------------------------------------------------------------------------------------------------------------------
Notes:
(1) The first call to interp3_gpu is not so fast because it requires time to initialize GPU.
(2) If you want a 32-bit version, rebuild (nvcc -ptx interp3_cuda.cu). Make sure you have CUDA Tools installed. Also clear the persistent variable for the kernel object so it can be recreated. (Tip: To clear a persistent variable in a function, the easiest method is to type anything in the function code, press Ctrl-Z to undo the change, then press Ctrl-S to save the file.)
(3) In MATLAB R2013b,
g_vv = parallel.gpu.GPUArray.zeros(size(xi), 'single');
can be replaced by
g_vv = gpuArray.zeros(size(xi), 'single');

------------------------------------------------------------------------------------------------------------------------

Required Products Parallel Computing Toolbox
MATLAB
MATLAB release MATLAB 7.14 (R2012a)
Other requirements CUDA Tools
02 Jan 2015 Eli Duenisch

### Eli Duenisch (view profile)

04 Aug 2014 Philip

### Philip (view profile)

22 Feb 2013 Jun Tan

### Jun Tan (view profile)

Hi, Raphael:

My program uses a persistent variable "k_interp3" (the kernel) in file "interp3_gpu.m". This variable is created the first time it is used, and remains in memory until you clear the variable manually or you close your Matlab.

The reason that I use the persistent variable is that the kernel only needs to be created once in order to save the time spent in creating it. Matlab is actually slow in creating it. So I don't want to create it every time I need to call interp3_gpu.m.

Moreover, if you clear the variable, my program can recreate k_interp3 again when it notices that the variable doesn't exist. I believe the minor memory cost is not a problem, so I leave it up to the users to clear it when needed.

My test shows that when I run command "clear all" and "gpuDevice", the "FreeMemory" value returns to its original value.

Good luck,
Jun

Comment only
22 Feb 2013 Raphael

### Raphael (view profile)

Hello!
In my opinion you have some trouble with memory usage in your code.
If I check the gpu-Memory with gpu.FreeMemory before and after your program running with bigger matrices there is some memory blocked.

Up to now i could not find out how to solve the problem. I suppose there is a problem with the use of feval.
If i use the feval function with an empty kernel i have the same problem...
Greeting, Raphael

Comment only
28 Jan 2013

Updated description: The input x, y, and z should be 1D array of the x, y, and z coordinates of the 3D matrix v in ascending order.

28 Jan 2013

Added screenshot and example, and input format explanation.

19 Aug 2014

Improved MATLAB wrapper function.
"*.ptx" rebuilt for Windows x64. If you need it for other Windows, rebuild accordingly.

19 Aug 2014

Updated description and testing example.