# Use ArBB types in C + + statements

## Use ArBB types in C + + statements

Hi,

I am trying to parallel a function and this is the code (I only include map function):

```void mymap(f32 x, f32& density, boolean exist){

using namespace arbb;

array coord;

position(coord);

_if(exist){

i32 ck = coord.at(2);

i32 cj = coord.at(1);

i32 cioriginal = coord.at(0);

i32 ci = cioriginal/16;

i32 index;

index = (ck*ny + cj)*nx + ci)*16+cioriginal%16;
_for(i32 C = 0, C < 27, C++){
i32 kk = ck + ((C%9)%3)-1;

i32 jj = cj + ((C%9)/3)-1;

i32 ii = ci + (C/9)-1;
_if(ii >= 0 && ii < nx && jj >= 0 && jj < ny && kk >= 0 && kk < nz){

i32 cellNeigh = ((kk*ny + jj)*nx + ii)*16;S

_for(i32 particleNeigh=ii*16, particleNeigh<16+ii*16 && neighbor(exist,particleNeigh-cioriginal,jj-cj,kk-ck), particleNeigh++){

_if(cellNeigh+(particleNeigh-ii*16) < index){

f32 dispX = (x - neighbor(x,particleNeigh-cioriginal,jj-cj,kk-ck));

f32 distSq =  dispX * dispX;

density+=distSq;

}_end_if

}_end_for
}_end_if

}_end_for
}_end_if

}```

Using
the function position I need to use ArBBtypes (it cannot be used
the function value) and the statements _if and _for . However, the
operations are independent ones to each other so therefore, it would be
better to introduce statements for and if. With the code shown I obtain worse results than the serial version, any suggestion?

Thanks

4 posts / novo 0
Para obter mais informações sobre otimizações de compiladores, consulte Aviso sobre otimizações.

Hi, have you taken a look at our knowledge base? We have a few articles about how to use the map() function properly. The reason why your code is slower than serial right now is that your ArBB function is not really expressing any parallelism.Any ArBB command that has an underscore _ such as _if and _for are evaluated as serial single core. But don't worry. We have an excellent article which I will link you to hereabouthow to do "for loop style" computations in ArBB step by step with a code example.After reading through that article and the other relevant KBs, let me know if there's anything else I can do to help you.

Thanks,

Noah

Thanks for your replying. I read the suggested articles but I have a question. How could I parallelize this sample?

```do_main(){
float* in,out;

bool* validPosition;

int max = 100;
//...

//initialize arrays

//...
for(int i = 0; i < length; i++){  //each position of array
if(validPosition[i] == true){
for(int neigh = -radius; neigh <= radius && validPosition[neigh] == true; neigh++){
float result = in[i] * in[neigh];
if(result < max){
out[i] += result;
}
}

}

}

}
```

Before I can set the result, I need to check the position of array, the position of the neighbor and the result obtained .

Thanks.

> [...] it would be
better to introduce statements for and if

Yes, your code looks like a 25-point 3d stencil ("27") where a cross pattern ("stencil") is completely pre-determined. The term "pre-determined" actually helps you to rely on a C++ for loop to generate the neighbor's index positions. This leads to a great advantage of run-time code generation - the stencil pattern can be hard-coded into the elemental function's code but still rely on your applications advice ("generate a 25-point stencil").

What I suggest you is to cleanup the neighbor index calculation. Use something like:
// pseudo code below!
// result = x
for (int d = 0; d < 3; ++d) {
for (int r = 1; r <= 4; ++r) {
// a[d] = my_position[d] - r
// b[d] = my_position[d] + r
// left = neighbor(a)
// right = neighbor(b)
// result += left + right
}
}

Try to minimize the amount of ArBB operations inside the C++ double-for to only the necessary operations. Using _if inside the double-for will essentially replicate these branches 3x4 times. On the other hand the _if (in contrast to "cond") is able to avoid the neighbor look-up if the index is out of bounds (OOB). Usually it is even possible to avoid these _if statements by processing the inner part of the grid separately. Also, dealing with OOB values or shift_sticky is a try. Anyhow, you need the _if statements anyways since you check against a grid of Booleans that determine an arbitrary shape.

## Deixar um comentário

Faça login para adicionar um comentário. Não é membro?