share variable between 2 cuda files.

hi

i’m new to CUDa… ive trying to access an array from one cuda file in another but with no luck. I’ve tried declaring it as extern and accessin it in the second file but with no luck. Could someone give a simple sample code as to how to do this ? It would be very encouraging for beginners like me… thanks

waiting for your replies.

i forgot to add one important detail… I want to share an array thats stored in the “GPU”, between the 2 files.

In CUDA ‘compilation unit’ is .cu file. and there is no linking stage.

I’m not sure what you’re trying to achieve, but if you have one kernel which is split across two or more files try #include’ing file where your array is defined into .cu file where you need to access it.

You should understand that if you have file1.cu where you define array and file2.cu where you #include “file1.cu” then those arrays will be different. I.e. if you want to access same array from different kernels you need to alocate it in device memory and pass pointer to it to each of kernels.

Hi AndreiB

thanks for the reply. let me add some detail to my problem.

Im tryin to apply a filter declared in file1.cu to an image in file2.cu.

So i want to keep the filter permanently in GPU till ive applied to all the images in file2.cu which is called repeatedly from matlab thru MEX function passing a different image each time.

I am calling file2.cu repeatedly from matlab thru a MeX function so I cannot use #include as it wil compile file1.cu many times unnecessarily.

Could you tell me how to use pointers to achieve this or any other possible solution ? Ive been breakin my head over this for a while now.

How do you store images in .cu file?

I’ve never wrote filters and never worked with Matlab, but I guess image filter is some kernel which takes one or more image (i.e. memory range) and produces resulting image.

So, I suppose it will look like global filter( void *in1, [void *in2,…,] void *out ) { … };

Next, you allocate memory on device for each image you’d like to transform and for results (this is done with cudaMalloc() ) and fill memory with source images.

Next, you call your kernel and pass pointer to images and read back results.

Just downloadad Matalb CUDA lugin – there’s Szeta.cu which I believe demonstrates all of the steps above.

This might be kind of a dumb answer, but why not have the function that creates the array pass the pointer back to the main() function (or at least to a level that’s “above” both kernel calls), and pass it back down when they are called? You’ll still need to make sure the compiler / linker can find the functions, though, by declaring them in a common header and placing them in extern “C” {} blocks, if the files are compiled separately.