Compare two array element with If statement

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

Moderator: pavanky

Compare two array element with If statement

Postby danielpengzuo » Mon Feb 17, 2014 2:31 pm

I ran into a problem with comparing two single-element array in a If statement.
I was trying to do something like:

If (A(0,0) > B(0,0)) {
...
} else {
...
}

Apparently, if does not recognize af:array type. Anyways to work around this issue without transferring the data to the host-side.
Many thanks.

Daniel
danielpengzuo
 
Posts: 10
Joined: Mon Jan 27, 2014 10:43 pm

Re: Compare two array element with If statement

Postby pavanky » Mon Feb 17, 2014 2:36 pm

Hi Daniel,

if (..) statements can not be overloaded using Array data type.

One solution would be to get the result back on to the CPU (like below) but this will involve costly memory transfers.

Code: Select all
bool cond = (A(0, 0) < B(0, 0)).scalar<bool>();
if (cond) {
 // something
} else {
 // something else
}


If you can give us more details about the code itself we can see if your code can be vectorized.
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: Compare two array element with If statement

Postby danielpengzuo » Mon Feb 17, 2014 2:39 pm

Code: Select all
               
                do
                {
                    niter++;
                    dL=-FFOCLI(pc, uc,  jc0,  Nc, LIL(iz, im),  omega,  gzc,  phi,  a,  lambda,  psi, bb, gam, theta)/FFOCLIp(pc, uc, jc0,  Nc, LIL(iz, im),  omega,  gzc,  phi,  a,  lambda,  psi, bb, gam, theta);
                    rtn = LIL(iz, im) + dL;
                    if(rtn<=xL)
                    {
                        LIL(iz, im)=xL;
                    }
                    else if(rtn>=xH)
                    {
                        LIL(iz, im)=xH;
                    }
                    else
                    {
                        LIL(iz, im)=rtn;
                    }
                    FFTMP = FFOCLI(pc, uc,  jc0,  Nc, LIL(iz, im),  omega,  gzc,  phi,  a,  lambda,  psi, bb, gam, theta);
                    if (af::abs(FFTMP) < TolX|| af::abs(dL) < TolX)
                    {
                        niter=103;
                    }
                }
                while (niter < 100);


This is the code I am working on. There is an IF statement near the bottom that changes the counter to break out of the loop when the conditions are met.
My question is related to that IF statement. Is pulling back to main memory the only way to compare two array element?

Thanks.
danielpengzuo
 
Posts: 10
Joined: Mon Jan 27, 2014 10:43 pm

Re: Compare two array element with If statement

Postby pavanky » Mon Feb 17, 2014 2:41 pm

What are the sizes of FFTMP and dL ?
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: Compare two array element with If statement

Postby danielpengzuo » Mon Feb 17, 2014 2:43 pm

They would just be some number, in array terms, 1-by-1.
danielpengzuo
 
Posts: 10
Joined: Mon Jan 27, 2014 10:43 pm

Re: Compare two array element with If statement

Postby pavanky » Mon Feb 17, 2014 2:52 pm

Hi Daniel,

in case you are doing something like sum(..) (or max, or min or other reductions) when returning from FFOCLI, change it to sum<float>(..). This way you can do the comparison on the CPU instead of doing af::abs(..)

If FFOCLI is using other functions that return 1x1 elements, just do something like

Code: Select all
float hdL = dL.scalar<float>();
float hFFTMP = FFTMP.scalar<float>();
if (abs(hdL) < TollX || abs(hFFTMP) < TollX) {
niter = 103;
}
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: Compare two array element with If statement

Postby danielpengzuo » Mon Feb 17, 2014 2:56 pm

Thank you very much! It seems that I have to do a memory transfer here for the IF statement no matter what.

Daniel
danielpengzuo
 
Posts: 10
Joined: Mon Jan 27, 2014 10:43 pm

Re: Compare two array element with If statement

Postby pavanky » Mon Feb 17, 2014 3:38 pm

Hi Daniel,

Doing sum<float> , max<float> etc is more efficient than doing .scalar<float>. If you are ok with sharing FFOCLI function, we can see if there is a more optimal solution.
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


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

cron