How to convert an array to a CuArray efficiently

Please forgive me as I am taking my first step in using GPU’s in Julia. My use case is I want to do some processioning of electron microscope image stacks from a TEM. So I have everything setup and I have some micrographs that are in TIFF format (gray scale). I used the Images package to load my test images. Now according to work flow I need to convert / port these to the CuArray type.

Just using the below naively

cuImgGray = CuArray(imgGray)

warns that this is a scalar operation and is slow. If I set allowscalar() to false then the operation fails.
Now there must be a way to do this efficiently otherwise converting my images to the GPU compatible type would be absurdly long and would be no use at all. Especially when my end goal is process hundreds of Gigs to 10’s Terabytes of data. The file I have here is only 16 meg and it takes on the order of a minute to convert and load into GPU memory.

1 Like

What type is imgGray?

Hi bcmichael

Thank you for the quick response. typeof(imGray) yields

imgGray = Gray.(load("data/TEMSA630kx.tif")
typeof(imgGray) -> Matrix{Gray{Normed{UInt8,8}}}

So in the space before you answered I converted the array to Float32. After I did this I got much better performance using


Then copying to the GPU with the constructor


Perhaps there is even a better way?
Now I am getting into using the CUFFT lib. I am not a stranger to FFTW but I have not used the CUDA libs so I am sure I’m in for a pretty steep learning curve.

Yeah converting to Float32 on the CPU first seems pretty reasonable. Using CUFFT should be pretty simple. I think you should be able to use basically the same interface for FFT on a CuArray as a normal Array.