Casting ArrayFire array to host array

[Old posts from the commercial version of ArrayFire] Discussion of ArrayFire using CUDA or OpenCL.

Moderator: pavanky

Casting ArrayFire array to host array

Postby anishk25 » Wed Dec 18, 2013 9:17 pm

Hi,

I was wondering if ArrayFire allows to cast the array structure back to an array on the host or device. For example if I had something like the following

array A = randu(3,1);
float *a_f = (float*)A; // is this possible to do?

Is it possible to cast this back to a float[] array. The reason I am asking this is that I need to use OpenGL to visualize some data. I know arrayfire already has a graphics library but I need to do some more advance graphics rendering on the GPU. If I need the data to stay on the GPU, is it possible to access it in a way so that it can work with OpenGL?

Thanks
anishk25
 
Posts: 4
Joined: Wed Dec 18, 2013 9:00 pm

Re: Casting ArrayFire array to host array

Postby shehzan » Thu Dec 19, 2013 2:15 pm

Hi

By default, arrays are of type f32 or floats.
You can get arrays on the host side by using the host() function. You can read more about it here: http://www.accelereyes.com/arrayfire/c/group__array__func__cpp__host.htm
If you need to typecast an array to another datatype, you can use the .as() function.
You can read more about it in our documentation http://www.accelereyes.com/arrayfire/c/group__array__func__cpp__as.htm

If you want data to stay on GPU and be accessible by the OpenGL API, it is a little bit more complex and you will need to write interop code for that. CUDA-OpenGL interop and OpenCL-OpenGL interop are two ways to do it.
----
Shehzan
Developer
AccelerEyes
User avatar
shehzan
 
Posts: 121
Joined: Tue Feb 12, 2013 7:20 pm

Re: Casting ArrayFire array to host array

Postby pavanky » Thu Dec 19, 2013 2:37 pm

Hi Anishk,

If you want the data to stay on the GPU you will need to use the CUDA interop. But you will still likely need a device to device memory copy on the GPU which is much faster than GPU -> host -> GPU.

For getting the device pointer out from arrayfire, you can do the following.

Code: Select all
float *d_A = A.device<float>();


You will then need to map OpenGL buffer to a cuda device pointer. Then you can do a cudaMemcpy to pass the data from arrayfire to OpenGL.

You can see how to map opengl to cuda device pointers over at http://3dgep.com/?p=2082#Register_Resources_with_CUDA
Pavan Yalamanchili,
ArrayFire
--
~ If it is not broken, you have not tried hard enough ~
User avatar
pavanky
Site Admin
 
Posts: 1123
Joined: Mon Mar 15, 2010 7:39 pm
Location: Atlanta, GA

Re: Casting ArrayFire array to host array

Postby anishk25 » Thu Dec 19, 2013 6:51 pm

Thanks, pavanky. Thats what I exactly needed.
anishk25
 
Posts: 4
Joined: Wed Dec 18, 2013 9:00 pm


Return to [archive-commercial] Programming & Development with ArrayFire

cron