dimanche 28 juin 2015

Can anyone modify the following code to avoid the usage of "if"?

I am working on cuda currently and I got stuck on the code below. The code was originally written in matlab and I am trying to re-write it using cuda:

Pv = 0; Nv =0;
[LOOP]
v1 = l(li);
v2 = l(li+1);
if((v1>0) && (v2>0))
    Pv = Pv + 1;
elseif((v1<0) && (v2<0))
    Nv = Nv +1;
elseif((v1>0) && (v2<0))
    r = v1/(v1-v2);
    Pv = Pv + r;
    Nv = Nv + 1 - r;
elseif((v1<0) && (v2>0))
    r = v2/(v2-v1);
    Pv = Pv + r;
    Nv = Nv + 1 - r;
end
[LOOP END]

But, in cuda architecture, "if" expression is sometimes expensive, and I believe there is some way to avoid the usage of it, although I cannot figure it out now.

The main purpose of the code is to calculate the ratio of positive interval of negative interval and add them up respectively. In most of the situation , v1 and v2 have the same sign, but once they have different sign, I have to use a bunch of "if" or even perhaps "abs()" to handle the situation.

So, can anyone help me to re-write the code using C while using as few "if " as possible?

Aucun commentaire:

Enregistrer un commentaire