Code covered by the BSD License  

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from
3D trilinear interpolation using GPU

5.0 | 2 ratings Rate this file 21 Downloads (last 30 days) File Size: 4.85 KB File ID: #40044 Version: 1.4
image thumbnail

3D trilinear interpolation using GPU


Jun Tan (view profile)


28 Jan 2013 (Updated )

3D trilinear interpolation using GPU. 20 times faster.

| Watch this File

File Information

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

    (Same as Matlab interp3's: VI = interp3(X,Y,Z,V,XI,YI,ZI)

    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.

(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]);
    shading flat;
    daspect([1 1 1]);

    Can be 20 times faster than Matlab interp3.

    (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 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 release MATLAB 7.14 (R2012a)
Other requirements CUDA Tools
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (4)
02 Jan 2015 Eli Duenisch  
04 Aug 2014 Philip

Philip (view profile)

22 Feb 2013 Jun Tan

Jun Tan (view profile)

Hi, Raphael:

Thanks for your comments.

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.

I hope I have answered your question.

Good luck,

Comment only
22 Feb 2013 Raphael

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 1.1

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 1.2

Added screenshot and example, and input format explanation.

19 Aug 2014 1.3

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

19 Aug 2014 1.4

Updated description and testing example.

Contact us