Be the first to rate this file! 35 Downloads (last 30 days) File Size: 5.31 KB File ID: #40044
image thumbnail

3D trilinear interpolation using GPU



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" doesn't have this requirement.

(Same as Matlab's example, with minor change.)
[x,y,z,v] = flow(10);
[xi,yi,zi] = meshgrid(.1:.25:10, -3:.25:3, -3:.25:3);
vi = interp3_gpu(x(1,:,1),y(:,1,1),z(1,1,:),v,xi,yi,zi); % Get the correct x/y/z grid positions.
slice(xi,yi,zi,double(vi),[6 9.5],2,[-2 .2]), shading flat % Matlab slices function doesn't accept single precision. So convert to double before displaying.

Can be 20 times faster than Matlab interp3.

(1) The first call is not so fast because it requires time to build a kernel. All other calls are fast because the kernel exists as a persistent variable in the function.
(2) In case you want a 32-bit version, rebuild (nvcc -ptx Make sure you have CUDA installed. Also clear so the persistent variable for the kernel can be updated.

Required Products Parallel Computing Toolbox
MATLAB release MATLAB 7.14 (R2012a)
Other requirements CUDA
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (2)
22 Feb 2013 Jun Tan

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,

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

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.

Contact us