fread directly to GPU memory

4 views (last 30 days)
John
John on 29 Feb 2012
Hi Everybody,
I'm new to GPU processing with matlab, and I'm wondering if anybody has solved this problem before. I have a binary file that I read through using the fread function. Currently I read in a range of data, fft it, and dump it to another file. I just got a new NVIDIA GPU and would like to process the fft on the GPU. I can easily do this if I read it into the cpu memory and then create a gpuArray.
The caveat is that I don't want to read it to the workspace first. since there is significant overhead with that, I would like to import the data directly to the RAM on the GPU without putting it into the memory of the workspace.
Any help or links are greatly appreciated.

Accepted Answer

Edric Ellis
Edric Ellis on 29 Feb 2012
Unfortunately, there's no way to do that. The nearest you can get is to pre-allocate the gpuArray, fread chunks of data, and then assign into the gpuArray. The only benefit here is that you don't need the whole array in host memory at any one time.
% Create a test file
a = rand(100);
fh = fopen( 'test.bin', 'wb' );
fwrite( fh, a, 'double' );
fclose( fh );
% Read the test file into a gpuArray one column at a time
g = parallel.gpu.GPUArray.zeros(100);
fh = fopen( 'test.bin', 'rb' );
for ii = 1:100
col = fread( fh, 100, 'double' );
g(:,ii) = col;
end
assert( isequal( a, g ) );
You'd probably want to read bigger chunks than that to get good performance. Also, note that gpuArrays can be saved into MAT files.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!