Casting a struct full of GDOUBLEs to DOUBLEs

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

Moderator: pavanky

Casting a struct full of GDOUBLEs to DOUBLEs

Postby melonakos » Mon Aug 23, 2010 4:33 pm

One Jacket programmer recently asked the following:

I could not find it in the docs, but is there a single function to convert a garray structure consisting of gdouble variables to an equivalent structure to doubles without working with each substructure element?


The answer is, YES, it is simple to convert a struct full of GPU variables (e.g. GDOUBLE) to DOUBLES. You can do that as shown in the following snippet:

Code: Select all
mystruct_gpu.a = grand(3);
mystruct_gpu.b = grand(3);
mystruct_gpu.c = grand(3)

mystruct_cpu = structfun(@(x)double(x),mystruct_gpu,'UniformOutput',false)

% check results
mystruct_gpu.a
mystruct_cpu.a


Running this code produces the following output:

Code: Select all
>> jacketNstructfun

mystruct_gpu =

    a: [3x3 garray]
    b: [3x3 garray]
    c: [3x3 garray]


mystruct_cpu =

    a: [3x3 double]
    b: [3x3 double]
    c: [3x3 double]

 
ans =
 
    0.6337    0.0120    0.0158
    0.7606    0.3713    0.7297
    0.0095    0.6250    0.1693


ans =

    0.6337    0.0120    0.0158
    0.7606    0.3713    0.7297
    0.0095    0.6250    0.1693

>>
John Melonakos (john@arrayfire.com)
User avatar
melonakos
 
Posts: 503
Joined: Tue Jun 10, 2008 9:49 am

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

cron