FTestKernel.cl 45.6 KB
Newer Older
1
/** This file contains the prototype for a kernel in opencl */
2
// @SCALFMM_PRIVATE
3

BRAMAS Berenger's avatar
BRAMAS Berenger committed
4 5 6 7 8 9

/***************************************************************************/
/***************************************************************************/
/************************CHANGE THINGS HERE*********************************/
/***************************************************************************/

10
typedef ___FSize___ FSize;
11 12 13 14 15 16 17
typedef ___FReal___ FReal;
typedef ___FParticleValueClass___ FParticleValueClass;
typedef long long int MortonIndex;

#define FOpenCLGroupOfCellsCellIsEmptyFlag  ((MortonIndex)-1)

#define NbAttributesPerParticle ___NbAttributesPerParticle___
18
#define NbSymbAttributes ___NbSymbAttributes___
19

20
#define FOpenCLGroupOfParticlesMemoryAlignementBytes  ___FP2PDefaultAlignement___
21 22 23 24 25
#define FOpenCLGroupOfParticlesMemoryAlignementParticles (FOpenCLGroupOfParticlesMemoryAlignementBytes/sizeof(FReal))
#define FOpenCLGroupOfParticlesLeafIsEmptyFlag ((MortonIndex)-1)

#define NULLPTR (0)

BRAMAS Berenger's avatar
BRAMAS Berenger committed
26 27
#define DefaultStructAlign ___DefaultStructAlign___

28
struct FSymboleCellClass {
29 30 31 32
    MortonIndex mortonIndex;
    int coordinates[3];
} __attribute__ ((aligned (DefaultStructAlign)));

33 34 35 36 37 38 39
typedef long long FTestCellPODData;
typedef FTestCellPODData FPoleCellClass;
typedef FTestCellPODData FLocalCellClass;

struct FWrappeCell{
    __global struct FSymboleCellClass* symb;
    __global FPoleCellClass* up;
40
    __global FLocalCellClass* down;
41 42 43
};


44 45 46 47 48
/***************************************************************************/
/***************************************************************************/
/***************************************************************************/
/***************************************************************************/

49
struct OutOfBlockInteraction{
50 51
    MortonIndex outIndex;
    MortonIndex insideIndex;
BRAMAS Berenger's avatar
BRAMAS Berenger committed
52
    int relativeOutPosition;
53
    int insideIdxInBlock;
BRAMAS Berenger's avatar
BRAMAS Berenger committed
54
} __attribute__ ((aligned (DefaultStructAlign)));
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189

#define Between(inValue, inMin, inMax)  ( (inMin) <= (inValue) && (inValue) < (inMax) )
#define pow2(power)  (1 << (power))
#define Abs(inV) (inV < 0 ? -inV : inV)

int3 GetPositionFromMorton(MortonIndex inIndex, const int inLevel){
    MortonIndex mask = 0x1LL;

    int3 coord;
    coord.x = 0;
    coord.y = 0;
    coord.z = 0;

    for(int indexLevel = 0; indexLevel < inLevel ; ++indexLevel){
        coord.z |= (int)(inIndex & mask);
        inIndex >>= 1;
        coord.y |= (int)(inIndex & mask);
        inIndex >>= 1;
        coord.x |= (int)(inIndex & mask);

        mask <<= 1;
    }

    return coord;
}

MortonIndex GetMortonIndex(const int3 coord, const int inLevel) {
    MortonIndex index = 0x0LL;
    MortonIndex mask = 0x1LL;
    // the ordre is xyz.xyz...
    MortonIndex mx = coord.x << 2;
    MortonIndex my = coord.y << 1;
    MortonIndex mz = coord.z;

    for(int indexLevel = 0; indexLevel < inLevel ; ++indexLevel){
        index |= (mz & mask);
        mask <<= 1;
        index |= (my & mask);
        mask <<= 1;
        index |= (mx & mask);
        mask <<= 1;

        mz <<= 2;
        my <<= 2;
        mx <<= 2;
    }

    return index;
}

int GetNeighborsIndexes(const int3 coord, const int OctreeHeight, MortonIndex indexes[26], int indexInArray[26]) {
    int idxNeig = 0;
    int limite = 1 << (OctreeHeight - 1);
    // We test all cells around
    for(int idxX = -1 ; idxX <= 1 ; ++idxX){
        if(!Between(coord.x + idxX,0, limite)) continue;

        for(int idxY = -1 ; idxY <= 1 ; ++idxY){
            if(!Between(coord.y + idxY,0, limite)) continue;

            for(int idxZ = -1 ; idxZ <= 1 ; ++idxZ){
                if(!Between(coord.z + idxZ,0, limite)) continue;

                // if we are not on the current cell
                if( idxX || idxY || idxZ ){
                    int3 other;

                    other.x = coord.x + idxX;
                    other.y = coord.y + idxY;
                    other.z = coord.z + idxZ;

                    indexes[ idxNeig ] = GetMortonIndex(other, OctreeHeight - 1);
                    indexInArray[ idxNeig ] = ((idxX+1)*3 + (idxY+1)) * 3 + (idxZ+1);
                    ++idxNeig;
                }
            }
        }
    }
    return idxNeig;
}

int GetInteractionNeighbors(const int3 coord, const int inLevel, MortonIndex inNeighbors[189], int inNeighborsPosition[189]) {
    // Then take each child of the parent's neighbors if not in directNeighbors
    // Father coordinate
    int3 parentCell;
    parentCell.x = coord.x>>1;
    parentCell.y = coord.y>>1;
    parentCell.z = coord.z>>1;

    // Limite at parent level number of box (split by 2 by level)
    const int limite = pow2(inLevel-1);

    int idxNeighbors = 0;
    // We test all cells around
    for(int idxX = -1 ; idxX <= 1 ; ++idxX){
        if(!Between(parentCell.x + idxX,0,limite)) continue;

        for(int idxY = -1 ; idxY <= 1 ; ++idxY){
            if(!Between(parentCell.y + idxY,0,limite)) continue;

            for(int idxZ = -1 ; idxZ <= 1 ; ++idxZ){
                if(!Between(parentCell.z + idxZ,0,limite)) continue;

                // if we are not on the current cell
                if( idxX || idxY || idxZ ){
                    int3 otherParent;

                    otherParent.x = parentCell.x + idxX;
                    otherParent.y = parentCell.y + idxY;
                    otherParent.z = parentCell.z + idxZ;

                    const MortonIndex mortonOther = GetMortonIndex(otherParent, inLevel-1);

                    // For each child
                    for(int idxCousin = 0 ; idxCousin < 8 ; ++idxCousin){
                        const int xdiff  = ((otherParent.x<<1) | ( (idxCousin>>2) & 1)) - coord.x;
                        const int ydiff  = ((otherParent.y<<1) | ( (idxCousin>>1) & 1)) - coord.y;
                        const int zdiff  = ((otherParent.z<<1) | (idxCousin&1)) - coord.z;

                        // Test if it is a direct neighbor
                        if(Abs(xdiff) > 1 || Abs(ydiff) > 1 || Abs(zdiff) > 1){
                            // add to neighbors
                            inNeighborsPosition[idxNeighbors] = ((( (xdiff+3) * 7) + (ydiff+3))) * 7 + zdiff + 3;
                            inNeighbors[idxNeighbors++] = (mortonOther << 3) | idxCousin;
                        }
                    }
                }
            }
        }
    }

    return idxNeighbors;
}


190
void FSetToNullptr343(struct FWrappeCell ptrs[343]){
191 192
    int idx;
    for( idx = 0 ; idx < 343 ; ++idx){
193
        ptrs[idx].symb = NULLPTR;
194 195 196 197 198 199 200 201 202 203 204
    }
}

/***************************************************************************/
/***************************************************************************/
/***************************************************************************/
/***************************************************************************/


struct FOpenCLGroupAttachedLeaf {
    //< Nb of particles in the current leaf
205
    FSize nbParticles;
206 207 208
    //< Pointers to the positions of the particles
    __global FReal* positionsPointers[3];
    //< Pointers to the attributes of the particles
209
    __global FParticleValueClass* attributes[NbSymbAttributes+NbAttributesPerParticle];
210 211
};

212
struct FOpenCLGroupAttachedLeaf BuildFOpenCLGroupAttachedLeaf(const FSize inNbParticles, __global FReal* inPositionBuffer, const size_t inLeadingPosition,
213
                                                              __global FParticleValueClass* inAttributesBuffer, const size_t inLeadingAttributes){
214 215 216 217 218 219 220
    struct FOpenCLGroupAttachedLeaf leaf;
    leaf.nbParticles = (inNbParticles);
    // Redirect pointers to position
    leaf.positionsPointers[0] = inPositionBuffer;
    leaf.positionsPointers[1] = (__global FReal*)(((__global unsigned char*)inPositionBuffer) + inLeadingPosition);
    leaf.positionsPointers[2] = (__global FReal*)(((__global unsigned char*)inPositionBuffer) + inLeadingPosition*2);

221
    for(unsigned idxAttribute = 0 ; idxAttribute < NbSymbAttributes ; ++idxAttribute){
222
        leaf.attributes[idxAttribute] =  (__global FParticleValueClass*)(((__global unsigned char*)inPositionBuffer) + inLeadingPosition*(idxAttribute+3));
223 224
    }

225
    // Redirect pointers to data
226 227 228 229 230 231 232 233 234
    if(inAttributesBuffer){
        for(unsigned idxAttribute = 0 ; idxAttribute < NbAttributesPerParticle ; ++idxAttribute){
            leaf.attributes[idxAttribute+NbSymbAttributes] = (__global FParticleValueClass*)(((__global unsigned char*)inAttributesBuffer) + idxAttribute*inLeadingAttributes);
        }
    }
    else{
        for(unsigned idxAttribute = 0 ; idxAttribute < NbAttributesPerParticle ; ++idxAttribute){
            leaf.attributes[idxAttribute+NbSymbAttributes] = NULLPTR;
        }
235 236 237 238 239 240 241 242 243 244 245 246 247
    }
    return leaf;
}

struct FOpenCLGroupAttachedLeaf EmptyFOpenCLGroupAttachedLeaf(){
    struct FOpenCLGroupAttachedLeaf leaf;
    leaf.nbParticles = -1;
    // Redirect pointers to position
    leaf.positionsPointers[0] = NULLPTR;
    leaf.positionsPointers[1] = NULLPTR;
    leaf.positionsPointers[2] = NULLPTR;

    // Redirect pointers to data
248
    for(unsigned idxAttribute = 0 ; idxAttribute < NbSymbAttributes+NbAttributesPerParticle ; ++idxAttribute){
249 250 251 252 253 254 255 256
        leaf.attributes[idxAttribute] = NULLPTR;
    }
    return leaf;
}

bool FOpenCLGroupAttachedLeaf_isAttachedToSomething(const struct FOpenCLGroupAttachedLeaf* group){
    return (group->nbParticles != -1);
}
BRAMAS Berenger's avatar
BRAMAS Berenger committed
257 258 259
bool FOpenCLGroupAttachedLeaf_getNbParticles(const struct FOpenCLGroupAttachedLeaf* group){
    return (group->nbParticles);
}
260 261 262 263 264 265 266 267 268


/** One header is allocated at the beginning of each block */
struct FOpenCLGroupOfParticlesBlockHeader{
    MortonIndex startingIndex;
    MortonIndex endingIndex;
    int numberOfLeavesInBlock;

    //< The real number of particles allocated
269
    FSize nbParticlesAllocatedInGroup;
270 271
    //< Starting point of position
    size_t offsetPosition;
272
    //< Bytes difference/offset between position
273
    size_t positionsLeadingDim;
274
    //< Bytes difference/offset between attributes
275
    size_t attributeLeadingDim;
276
    //< The total number of particles in the group
277
    FSize nbParticlesInGroup;
BRAMAS Berenger's avatar
BRAMAS Berenger committed
278
}__attribute__ ((aligned (DefaultStructAlign)));
BRAMAS Berenger's avatar
BRAMAS Berenger committed
279

280 281
/** Information about a leaf */
struct FOpenCLGroupOfParticlesLeafHeader {
282
    MortonIndex mindex;
283
    FSize nbParticles;
284
    size_t offSet;
BRAMAS Berenger's avatar
BRAMAS Berenger committed
285
}__attribute__ ((aligned (DefaultStructAlign)));
286 287 288 289


struct FOpenCLGroupOfParticles {
    //< The size of memoryBuffer in byte
290
    size_t allocatedMemoryInByte;
291 292 293 294 295 296 297 298
    //< Pointer to a block memory
    __global unsigned char* memoryBuffer;

    //< Pointer to the header inside the block memory
    __global struct FOpenCLGroupOfParticlesBlockHeader*    blockHeader;
    //< Pointer to leaves information
    __global struct FOpenCLGroupOfParticlesLeafHeader*     leafHeader;
    //< The total number of particles in the group
299
    const FSize nbParticlesInGroup;
300 301 302 303 304

    //< Pointers to particle position x, y, z
    __global FReal* particlePosition[3];

    //< Pointers to the particles data inside the block memory
305 306
    __global FParticleValueClass*      attributesBuffer;
    __global FParticleValueClass*      particleAttributes[NbSymbAttributes+NbAttributesPerParticle];
307 308
};

309 310
struct FOpenCLGroupOfParticles BuildFOpenCLGroupOfParticles(__global unsigned char* inBuffer, const size_t inAllocatedMemoryInByte,
                                                            __global unsigned char* inAttributeBuffer){
311 312 313 314 315
    struct FOpenCLGroupOfParticles group;
    group.allocatedMemoryInByte = (inAllocatedMemoryInByte);
    group.memoryBuffer = (inBuffer);

    // Move the pointers to the correct position
316 317 318
    group.blockHeader         = ((__global struct FOpenCLGroupOfParticlesBlockHeader*)inBuffer);
    inBuffer += sizeof(struct FOpenCLGroupOfParticlesBlockHeader);
    group.leafHeader          = ((__global struct FOpenCLGroupOfParticlesLeafHeader*)inBuffer);
319 320

    // Init particle pointers
321
    // Assert group.blockHeader->positionsLeadingDim == (sizeof(FReal) * group.blockHeader->nbParticlesAllocatedInGroup);
322
    group.particlePosition[0] = (__global FReal*) (group.memoryBuffer + group.blockHeader->offsetPosition);
323 324 325 326
    group.particlePosition[1] = (group.particlePosition[0] + group.blockHeader->nbParticlesAllocatedInGroup);
    group.particlePosition[2] = (group.particlePosition[1] + group.blockHeader->nbParticlesAllocatedInGroup);

    // Redirect pointer to data
327
    // Assert group.blockHeader->attributeLeadingDim == (sizeof(FParticleValueClass) * group.blockHeader->nbParticlesAllocatedInGroup);
BRAMAS Berenger's avatar
BRAMAS Berenger committed
328
    __global unsigned char* previousPointer = ((__global unsigned char*)(group.particlePosition[2] + group.blockHeader->nbParticlesAllocatedInGroup));
329
    for(unsigned idxAttribute = 0 ; idxAttribute < NbSymbAttributes ; ++idxAttribute){
330 331 332
        group.particleAttributes[idxAttribute] = ((__global FParticleValueClass*)previousPointer);
        previousPointer += sizeof(FParticleValueClass)*group.blockHeader->nbParticlesAllocatedInGroup;
    }
333 334
    
    if(inAttributeBuffer){
335
        group.attributesBuffer = (__global FParticleValueClass*)inAttributeBuffer;
336 337 338 339 340 341 342 343 344
        for(unsigned idxAttribute = 0 ; idxAttribute < NbAttributesPerParticle ; ++idxAttribute){
            group.particleAttributes[idxAttribute+NbSymbAttributes] = ((__global FParticleValueClass*)inAttributeBuffer);
            inAttributeBuffer += sizeof(FParticleValueClass)*group.blockHeader->nbParticlesAllocatedInGroup;
        }
    }
    else{
        group.attributesBuffer = NULLPTR;
        for(unsigned idxAttribute = 0 ; idxAttribute < NbAttributesPerParticle ; ++idxAttribute){
            group.particleAttributes[idxAttribute+NbSymbAttributes] = NULLPTR;
345
        }
346 347
    }
    
348 349 350 351 352 353 354 355
    return group;
}
MortonIndex FOpenCLGroupOfParticles_getStartingIndex(const struct FOpenCLGroupOfParticles* group) {
    return group->blockHeader->startingIndex;
}
MortonIndex FOpenCLGroupOfParticles_getEndingIndex(const struct FOpenCLGroupOfParticles* group) {
    return group->blockHeader->endingIndex;
}
BRAMAS Berenger's avatar
BRAMAS Berenger committed
356 357 358
int FOpenCLGroupOfParticles_getNumberOfLeaves(const struct FOpenCLGroupOfParticles* group) {
    return group->blockHeader->numberOfLeavesInBlock;
}
359 360 361
bool FOpenCLGroupOfParticles_isInside(const struct FOpenCLGroupOfParticles* group, const MortonIndex inIndex) {
    return group->blockHeader->startingIndex <= inIndex && inIndex < group->blockHeader->endingIndex;
}
362 363 364 365 366


/** Return the idx in array of the cell */
MortonIndex FOpenCLGroupOfParticles_getLeafMortonIndex(const struct FOpenCLGroupOfParticles* group, const int id){
    return group->leafHeader[id].mindex;
367
}
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383

/** Check if a cell exist (by binary search) and return it index */
int FOpenCLGroupOfParticles_getLeafIndex(const struct FOpenCLGroupOfParticles* group, const MortonIndex leafIdx){
    int idxLeft = 0;
    int idxRight = group->blockHeader->numberOfLeavesInBlock-1;
    while(idxLeft <= idxRight){
        const int idxMiddle = (idxLeft+idxRight)/2;
        if(group->leafHeader[idxMiddle].mindex == leafIdx){
            return idxMiddle;
        }
        if(leafIdx < group->leafHeader[idxMiddle].mindex){
            idxRight = idxMiddle-1;
        }
        else{
            idxLeft = idxMiddle+1;
        }
384
    }
385 386 387 388 389 390 391 392 393 394 395 396 397
    return -1;
}


bool FOpenCLGroupOfParticles_exists(const struct FOpenCLGroupOfParticles* group, const MortonIndex inIndex) {
    return FOpenCLGroupOfParticles_isInside(group, inIndex) && (FOpenCLGroupOfParticles_getLeafIndex(group, inIndex) != -1);
}
struct FOpenCLGroupAttachedLeaf FOpenCLGroupOfParticles_getLeaf(struct FOpenCLGroupOfParticles* group, const int id){
    return BuildFOpenCLGroupAttachedLeaf(group->leafHeader[id].nbParticles,
                                         group->particlePosition[0] + group->leafHeader[id].offSet,
            group->blockHeader->positionsLeadingDim,
            (group->attributesBuffer?group->particleAttributes[NbSymbAttributes] + group->leafHeader[id].offSet:NULLPTR),
            group->blockHeader->attributeLeadingDim);
398 399 400 401 402 403 404
}


struct FOpenCLGroupOfCellsBlockHeader{
    MortonIndex startingIndex;
    MortonIndex endingIndex;
    int numberOfCellsInBlock;
BRAMAS Berenger's avatar
BRAMAS Berenger committed
405
} __attribute__ ((aligned (DefaultStructAlign)));
406 407 408 409


struct FOpenCLGroupOfCells {
    //< The size of the memoryBuffer
410
    size_t allocatedMemoryInByte;
411 412 413 414 415 416
    //< Pointer to a block memory
    __global unsigned char* memoryBuffer;

    //< Pointer to the header inside the block memory
    __global struct FOpenCLGroupOfCellsBlockHeader*    blockHeader;
    //< Pointer to the indexes table inside the block memory
417
    __global MortonIndex*    cellIndexes;
418
    //< Pointer to the cells inside the block memory
419 420 421 422 423 424
    __global struct FSymboleCellClass*      blockCells;
    
    //< The multipole data
    __global FPoleCellClass* cellMultipoles;
    //< The local data
    __global FLocalCellClass* cellLocals;
425 426
};

427
struct FOpenCLGroupOfCells BuildFOpenCLGroupOfCells(__global unsigned char* inBuffer, const size_t inAllocatedMemoryInByte,
428 429 430 431
                                                    __global unsigned char* inCellMultipoles, __global unsigned char* inCellLocals){
    struct FOpenCLGroupOfCells group;
    group.memoryBuffer = (inBuffer);
    group.allocatedMemoryInByte = (inAllocatedMemoryInByte);
432 433

    // Move the pointers to the correct position
434 435
    group.blockHeader         = (__global struct FOpenCLGroupOfCellsBlockHeader*)(inBuffer);
    inBuffer += sizeof(struct FOpenCLGroupOfCellsBlockHeader);
436 437
    group.cellIndexes   = (__global MortonIndex*)(inBuffer);
    inBuffer += (group.blockHeader->numberOfCellsInBlock*sizeof(MortonIndex));
438 439 440 441
    group.blockCells          = (__global struct FSymboleCellClass*)(inBuffer);
    inBuffer += (group.blockHeader->numberOfCellsInBlock*sizeof(struct FSymboleCellClass));
    // Assert(((size_t)(inBuffer-group.memoryBuffer) == inAllocatedMemoryInByte);

442 443
    group.cellMultipoles = (__global FPoleCellClass*)inCellMultipoles;
    group.cellLocals = (__global FLocalCellClass*)inCellLocals;
444 445 446 447 448 449 450 451 452 453 454 455
    return group;
}
MortonIndex FOpenCLGroupOfCells_getStartingIndex(const struct FOpenCLGroupOfCells* group) {
    return group->blockHeader->startingIndex;
}
MortonIndex FOpenCLGroupOfCells_getEndingIndex(const struct FOpenCLGroupOfCells* group) {
    return group->blockHeader->endingIndex;
}
int FOpenCLGroupOfCells_getNumberOfCellsInBlock(const struct FOpenCLGroupOfCells* group) {
    return group->blockHeader->numberOfCellsInBlock;
}
int FOpenCLGroupOfCells_getSizeOfInterval(const struct FOpenCLGroupOfCells* group) {
456
    return group->blockHeader->endingIndex - group->blockHeader->startingIndex;
457 458 459 460
}
bool FOpenCLGroupOfCells_isInside(const struct FOpenCLGroupOfCells* group, const MortonIndex inIndex){
    return group->blockHeader->startingIndex <= inIndex && inIndex < group->blockHeader->endingIndex;
}
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505

MortonIndex FOpenCLGroupOfCells_getCellMortonIndex(const struct FOpenCLGroupOfCells* group,const int cellPos){
    return group->cellIndexes[cellPos];
}

int FOpenCLGroupOfCells_getCellIndex(const struct FOpenCLGroupOfCells* group,const MortonIndex cellIdx){
    int idxLeft = 0;
    int idxRight = group->blockHeader->numberOfCellsInBlock-1;
    while(idxLeft <= idxRight){
        const int idxMiddle = (idxLeft+idxRight)/2;
        if(group->cellIndexes[idxMiddle] == cellIdx){
            return idxMiddle;
        }
        if(cellIdx < group->cellIndexes[idxMiddle]){
            idxRight = idxMiddle-1;
        }
        else{
            idxLeft = idxMiddle+1;
        }
    }
    return -1;
}

int FOpenCLGroupOfCells_getFistChildIdx(const struct FOpenCLGroupOfCells* group, const MortonIndex parentIdx) {
    int idxLeft = 0;
    int idxRight = group->blockHeader->numberOfCellsInBlock-1;
    while(idxLeft <= idxRight){
        int idxMiddle = (idxLeft+idxRight)/2;
        if((group->cellIndexes[idxMiddle]>>3) == parentIdx){
            while(0 < idxMiddle && (group->cellIndexes[idxMiddle-1]>>3) == parentIdx){
                idxMiddle -= 1;
            }
            return idxMiddle;
        }
        if(parentIdx < (group->cellIndexes[idxMiddle]>>3)){
            idxRight = idxMiddle-1;
        }
        else{
            idxLeft = idxMiddle+1;
        }
    }
    return -1;
}


506
bool FOpenCLGroupOfCells_exists(const struct FOpenCLGroupOfCells* group, const MortonIndex inIndex) {
507
    return FOpenCLGroupOfCells_isInside(group, inIndex) && FOpenCLGroupOfCells_getCellIndex(group, inIndex) != -1;
508
}
509
struct FWrappeCell FOpenCLGroupOfCells_getCompleteCell(struct FOpenCLGroupOfCells* group, const int cellPos){
510
    struct FWrappeCell cell;
511 512 513
    cell.symb = &group->blockCells[cellPos];
    cell.up = &group->cellMultipoles[cellPos];
    cell.down = &group->cellLocals[cellPos];
514 515 516
    return cell;
}

517
struct FWrappeCell FOpenCLGroupOfCells_getUpCell(struct FOpenCLGroupOfCells* group, const int cellPos){
518
    struct FWrappeCell cell;
519 520 521
    cell.symb = &group->blockCells[cellPos];
    cell.up = &group->cellMultipoles[cellPos];
    cell.down = NULLPTR;
522 523 524
    return cell;
}

525 526 527
struct FWrappeCell FOpenCLGroupOfCells_getDownCell(struct FOpenCLGroupOfCells* group, const int cellPos){
    struct FWrappeCell cell;
    cell.symb = &group->blockCells[cellPos];
528
    cell.up = NULLPTR;
529
    cell.down =&group->cellLocals[cellPos];
530
    return cell;
531 532 533 534
}

struct Uptr9{
    __global unsigned char* ptrs[9];
BRAMAS Berenger's avatar
BRAMAS Berenger committed
535
} __attribute__ ((aligned (DefaultStructAlign)));
536 537 538

struct size_t9{
    size_t v[9];
BRAMAS Berenger's avatar
BRAMAS Berenger committed
539
} __attribute__ ((aligned (DefaultStructAlign)));
540 541 542 543 544

struct Uptr343{
    __global unsigned char* ptrs[343];
};

BRAMAS Berenger's avatar
BRAMAS Berenger committed
545 546 547 548 549 550
/***************************************************************************/
/***************************************************************************/
/************************CHANGE THINGS HERE*********************************/
/***************************************************************************/


551 552
void P2M(struct FWrappeCell pole, const struct FOpenCLGroupAttachedLeaf particles, __global void* user_data) {
    *pole.up = particles.nbParticles;
553 554
}

555
void M2M(struct FWrappeCell  pole, struct FWrappeCell child[8], const int level, __global void* user_data) {
556
    for(int idxChild = 0 ; idxChild < 8 ; ++idxChild){
557 558
        if(child[idxChild].symb){
            *pole.up += *child[idxChild].up;
559 560
        }
    }
561 562
}

563
void M2L(struct FWrappeCell const pole, const struct FWrappeCell distantNeighbors[343],
564
const int size, const int level, __global void* user_data) {
565
    for(int idxNeigh = 0 ; idxNeigh < 343 ; ++idxNeigh){
566 567
        if(distantNeighbors[idxNeigh].symb){
            *pole.down += *distantNeighbors[idxNeigh].up;
568 569
        }
    }
570 571
}

572
void L2L(const struct FWrappeCell localCell, struct FWrappeCell child[8], const int level, __global void* user_data) {
573
    for(int idxChild = 0 ; idxChild < 8 ; ++idxChild){
574 575
        if(child[idxChild].symb){
            *child[idxChild].down += *localCell.down;
576 577
        }
    }
578 579
}

580
void L2P(const struct FWrappeCell localCell, struct FOpenCLGroupAttachedLeaf particles, __global void* user_data){
581
    __global long long* partdown = particles.attributes[0];
582
    for(FSize idxPart = 0 ; idxPart < particles.nbParticles ; ++idxPart){
583
        partdown[idxPart] += *localCell.down;
584
    }
585 586 587
}

void P2P(const int3 pos,
588 589
         struct FOpenCLGroupAttachedLeaf  targets, const struct FOpenCLGroupAttachedLeaf sources,
         struct FOpenCLGroupAttachedLeaf directNeighborsParticles[27], int directNeighborsPositions[27], const int counter, __global void* user_data){
590 591
    long long cumul = sources.nbParticles-1;
    
592
    for(int idxNeigh = 0 ; idxNeigh < counter ; ++idxNeigh){
593 594 595 596 597 598
        if(FOpenCLGroupAttachedLeaf_isAttachedToSomething(&directNeighborsParticles[idxNeigh])){
            cumul += directNeighborsParticles[idxNeigh].nbParticles;
        }
    }
    
    __global long long* partdown = targets.attributes[0];
599
    for(FSize idxPart = 0 ; idxPart < targets.nbParticles ; ++idxPart){
600 601
        partdown[idxPart] += cumul;
    }
602 603 604
}

void P2PRemote(const int3 pos,
605 606
               struct FOpenCLGroupAttachedLeaf  targets, const struct FOpenCLGroupAttachedLeaf  sources,
               struct FOpenCLGroupAttachedLeaf directNeighborsParticles, const int position, __global void* user_data){
607
    __global long long* partdown = targets.attributes[0];
608
    for(FSize idxPart = 0 ; idxPart < targets.nbParticles ; ++idxPart){
609 610
        partdown[idxPart] += directNeighborsParticles.nbParticles;
    }
611 612
}

613
int3 getCoordinate(const struct FWrappeCell cell) {
614
    int3 coord;
615 616 617
    coord.x = cell.symb->coordinates[0];
    coord.y = cell.symb->coordinates[1];
    coord.z = cell.symb->coordinates[2];
618
    return coord;
619 620 621 622 623 624 625 626 627 628
}


/***************************************************************************/
/***************************************************************************/
/***************************************************************************/
/***************************************************************************/

#define FOpenCLCheck( test ) { FOpenCLCheckCore((test), __FILE__, __LINE__); }
#define FOpenCLCheckAfterCall() { FOpenCLCheckCore((cudaGetLastError()), __FILE__, __LINE__); }
629
#define FOpenCLAssertLF(ARGS) if(!(ARGS)){ *((char*)0x09) = 'e'; }
630 631 632 633 634 635 636 637 638
//#define FOpenCLAssertLF(ARGS) ARGS;

#define FMGetOppositeNeighIndex(index) (27-(index)-1)
#define FMGetOppositeInterIndex(index) (343-(index)-1)

#define FOpenCLMax(x,y) ((x)<(y) ? (y) : (x))
#define FOpenCLMin(x,y) ((x)>(y) ? (y) : (x))


639
__kernel void FOpenCL__bottomPassPerform(__global unsigned char* leafCellsPtr, size_t leafCellsSize,__global unsigned char* leafCellsUpPtr,
640
                                         __global unsigned char* containersPtr, size_t containersSize,
641 642 643
                                         __global void* userkernel ){
    struct FOpenCLGroupOfCells leafCells = BuildFOpenCLGroupOfCells(leafCellsPtr, leafCellsSize, leafCellsUpPtr, NULLPTR);
    struct FOpenCLGroupOfParticles containers = BuildFOpenCLGroupOfParticles(containersPtr, containersSize, NULLPTR);
644

645
    const int nbLeaves = FOpenCLGroupOfCells_getNumberOfCellsInBlock(&leafCells);
BRAMAS Berenger's avatar
BRAMAS Berenger committed
646
    
647 648 649 650 651 652
    for(int idxLeaf = 0 ; idxLeaf < nbLeaves ; ++idxLeaf){
        struct FWrappeCell cell = FOpenCLGroupOfCells_getUpCell(&leafCells, idxLeaf);
        FOpenCLAssertLF(cell.symb->mortonIndex == FOpenCLGroupOfCells_getCellMortonIndex(&leafCells, idxLeaf));
        struct FOpenCLGroupAttachedLeaf particles = FOpenCLGroupOfParticles_getLeaf(&containers, idxLeaf);
        FOpenCLAssertLF(FOpenCLGroupOfParticles_getLeafMortonIndex(&containers, idxLeaf) == FOpenCLGroupOfCells_getCellMortonIndex(&leafCells, idxLeaf));
        P2M(cell, particles, userkernel);
653 654 655 656 657 658 659 660
    }
}


/////////////////////////////////////////////////////////////////////////////////////
/// Upward Pass
/////////////////////////////////////////////////////////////////////////////////////

661
__kernel void FOpenCL__upwardPassPerform(__global unsigned char* currentCellsPtr, size_t currentCellsSize, __global unsigned char* currentCellsUpPtr,
662 663
                                         struct Uptr9 subCellGroupsPtr, struct size_t9 subCellGroupsSize, struct Uptr9 subCellGroupsUpPtr,
                                         int nbSubCellGroups, int idxLevel, __global void* userkernel){
664
    struct FOpenCLGroupOfCells currentCells = BuildFOpenCLGroupOfCells(currentCellsPtr, currentCellsSize, currentCellsUpPtr, NULLPTR);
665 666
    struct FOpenCLGroupOfCells subCellGroups[9];
    for(int idx = 0 ; idx < nbSubCellGroups ; ++idx){
667
        subCellGroups[idx] = BuildFOpenCLGroupOfCells(subCellGroupsPtr.ptrs[idx], subCellGroupsSize.v[idx], subCellGroupsUpPtr.ptrs[idx], NULLPTR);
668 669 670
    }

    FOpenCLAssertLF(nbSubCellGroups != 0);
671
    const int nbCells = FOpenCLGroupOfCells_getNumberOfCellsInBlock(&currentCells);
672
    int idxSubCellGroup = 0;
673
    int idxChildCell = FOpenCLGroupOfCells_getFistChildIdx(&subCellGroups[0], FOpenCLGroupOfCells_getCellIndex(&currentCells, 0));
674

675 676 677 678
    for(int idxCell = 0 ; idxCell < nbCells ; ++idxCell){
        struct FWrappeCell cell = FOpenCLGroupOfCells_getUpCell(&currentCells, idxCell);
        FOpenCLAssertLF(cell.symb->mortonIndex == FOpenCLGroupOfCells_getCellMortonIndex(&currentCells, idxCell));
        struct FWrappeCell child[8];
679

680 681 682 683 684
        FOpenCLAssertLF(idxSubCellGroup != nbSubCellGroups);

        for(int idxChild = 0 ; idxChild < 8 ; ++idxChild){
            child[idxChild].symb = NULLPTR;
        }
685

686 687 688 689 690 691 692 693 694 695 696
        while(idxSubCellGroup != nbSubCellGroups
              && (FOpenCLGroupOfCells_getCellMortonIndex(&subCellGroups[idxSubCellGroup], idxChildCell)>>3) == cell.symb->mortonIndex){
            const int idxChild = ((FOpenCLGroupOfCells_getCellMortonIndex(&subCellGroups[idxSubCellGroup], idxChildCell)) & 7);

            child[idxChild] = FOpenCLGroupOfCells_getUpCell(&subCellGroups[idxSubCellGroup], idxChildCell);
            idxChildCell += 1;

            if(idxChildCell == FOpenCLGroupOfCells_getNumberOfCellsInBlock(&subCellGroups[idxSubCellGroup])){
                idxChildCell = 0;
                idxSubCellGroup += 1;
            }
697
        }
698 699

        M2M(cell, child, idxLevel, userkernel);
700 701 702 703 704 705 706 707 708 709
    }
}



/////////////////////////////////////////////////////////////////////////////////////
/// Transfer Pass Mpi
/////////////////////////////////////////////////////////////////////////////////////


710
__kernel  void FOpenCL__transferInoutPassPerformMpi(__global unsigned char* currentCellsPtr, size_t currentCellsSize, __global unsigned char* currentCellsDownPtr,
711 712 713
                                                    __global unsigned char* externalCellsPtr, size_t externalCellsSize, __global unsigned char* externalCellsUpPtr,
                                                    int idxLevel, const __global struct OutOfBlockInteraction* outsideInteractions,
                                                    size_t nbOutsideInteractions, __global void* userkernel){
714 715
    struct FOpenCLGroupOfCells currentCells = BuildFOpenCLGroupOfCells(currentCellsPtr, currentCellsSize, NULLPTR, currentCellsDownPtr);
    struct FOpenCLGroupOfCells cellsOther = BuildFOpenCLGroupOfCells(externalCellsPtr, externalCellsSize, externalCellsUpPtr, NULLPTR);
716 717

    for(int outInterIdx = 0 ; outInterIdx < nbOutsideInteractions ; ++outInterIdx){
718 719 720 721
        const int cellPos = FOpenCLGroupOfCells_getCellIndex(&cellsOther, outsideInteractions[outInterIdx].outIndex);
        if(cellPos != -1){
            FOpenCLAssertLF(outsideInteractions[outInterIdx].outIndex == FOpenCLGroupOfCells_getCellMortonIndex(&cellsOther, outsideInteractions[outInterIdx].outIndex));
            struct FWrappeCell interCell = FOpenCLGroupOfCells_getUpCell(&cellsOther, cellPos);
722
            FOpenCLAssertLF(interCell.symb->mortonIndex == outsideInteractions[outInterIdx].outIndex);
723
            struct FWrappeCell cell = FOpenCLGroupOfCells_getDownCell(&currentCells, outsideInteractions[outInterIdx].insideIdxInBlock);
724 725 726
            FOpenCLAssertLF(cell.symb->mortonIndex == outsideInteractions[outInterIdx].insideIndex);

            struct FWrappeCell interactions[343];
727
            FSetToNullptr343(interactions);
BRAMAS Berenger's avatar
BRAMAS Berenger committed
728
            interactions[outsideInteractions[outInterIdx].relativeOutPosition] = interCell;
729 730 731 732 733 734 735 736 737 738 739 740 741 742
            const int counter = 1;
            M2L( cell , interactions, counter, idxLevel, userkernel);
        }
    }
}


/////////////////////////////////////////////////////////////////////////////////////
/// Transfer Pass
/////////////////////////////////////////////////////////////////////////////////////



__kernel  void FOpenCL__transferInPassPerform(__global unsigned char* currentCellsPtr, size_t currentCellsSize,
743 744
                                              __global unsigned char* currentCellsUpPtr, __global unsigned char* currentCellsDownPtr,
                                              int idxLevel, __global void* userkernel){
745
    struct FOpenCLGroupOfCells currentCells = BuildFOpenCLGroupOfCells(currentCellsPtr, currentCellsSize, currentCellsUpPtr, currentCellsDownPtr);
746 747 748 749

    const MortonIndex blockStartIdx = FOpenCLGroupOfCells_getStartingIndex(&currentCells);
    const MortonIndex blockEndIdx = FOpenCLGroupOfCells_getEndingIndex(&currentCells);

750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772
    const int nbCells = FOpenCLGroupOfCells_getNumberOfCellsInBlock(&currentCells);

    for(int idxCell = 0 ; idxCell < nbCells ; ++idxCell){
        struct FWrappeCell cell = FOpenCLGroupOfCells_getDownCell(&currentCells, idxCell);
        FOpenCLAssertLF(cell.symb->mortonIndex == FOpenCLGroupOfCells_getCellMortonIndex(&currentCells, idxCell));
        MortonIndex interactionsIndexes[189];
        int interactionsPosition[189];
        const int3 coord = (getCoordinate(cell));
        int counter = GetInteractionNeighbors(coord, idxLevel,interactionsIndexes,interactionsPosition);

        struct FWrappeCell interactions[343];
        FSetToNullptr343(interactions);
        int counterExistingCell = 0;

        for(int idxInter = 0 ; idxInter < counter ; ++idxInter){
            if( blockStartIdx <= interactionsIndexes[idxInter] && interactionsIndexes[idxInter] < blockEndIdx ){
                const int cellPos = FOpenCLGroupOfCells_getCellIndex(&currentCells, interactionsIndexes[idxInter]);
                if(cellPos != -1){
                    struct FWrappeCell interCell = FOpenCLGroupOfCells_getUpCell(&currentCells, cellPos);
                    FOpenCLAssertLF(interCell.symb->mortonIndex == interactionsIndexes[idxInter]);
                    FOpenCLAssertLF(interactions[interactionsPosition[idxInter]].symb == NULLPTR);
                    interactions[interactionsPosition[idxInter]] = interCell;
                    counterExistingCell += 1;
773 774 775
                }
            }
        }
776 777

        M2L( cell , interactions, counterExistingCell, idxLevel, userkernel);
778 779 780 781 782 783
    }
}



__kernel void FOpenCL__transferInoutPassPerform(__global unsigned char* currentCellsPtr, size_t currentCellsSize,
784 785 786 787 788
                                                __global unsigned char*  currentCellsUpPtr, __global unsigned char*  currentCellsDownPtr,
                                                __global unsigned char* externalCellsPtr, size_t externalCellsSize,
                                                __global unsigned char* externalCellsUpPtr, __global unsigned char* externalCellsDownPtr,
                                                int idxLevel, const __global struct OutOfBlockInteraction* outsideInteractions,
                                                size_t nbOutsideInteractions, __global void* userkernel){
789 790
    struct FOpenCLGroupOfCells currentCells = BuildFOpenCLGroupOfCells(currentCellsPtr, currentCellsSize, currentCellsUpPtr, currentCellsDownPtr);
    struct FOpenCLGroupOfCells cellsOther = BuildFOpenCLGroupOfCells(externalCellsPtr, externalCellsSize, externalCellsUpPtr, externalCellsDownPtr);
791 792

    for(int outInterIdx = 0 ; outInterIdx < nbOutsideInteractions ; ++outInterIdx){
793 794 795 796
        const int cellPos = FOpenCLGroupOfCells_getCellIndex(&cellsOther, outsideInteractions[outInterIdx].outIndex);
        if(cellPos != -1){
            FOpenCLAssertLF(outsideInteractions[outInterIdx].outIndex == FOpenCLGroupOfCells_getCellMortonIndex(&cellsOther, outsideInteractions[outInterIdx].outIndex));
            struct FWrappeCell interCell = FOpenCLGroupOfCells_getUpCell(&cellsOther, cellPos);
797
            FOpenCLAssertLF(interCell.symb->mortonIndex == outsideInteractions[outInterIdx].outIndex);
798
            struct FWrappeCell cell = FOpenCLGroupOfCells_getDownCell(&currentCells, outsideInteractions[outInterIdx].insideIdxInBlock);
799 800 801
            FOpenCLAssertLF(cell.symb->mortonIndex == outsideInteractions[outInterIdx].insideIndex);

            struct FWrappeCell interactions[343];
802
            FSetToNullptr343(interactions);
BRAMAS Berenger's avatar
BRAMAS Berenger committed
803
            interactions[outsideInteractions[outInterIdx].relativeOutPosition] = interCell;
804 805 806
            const int counter = 1;
            M2L( cell , interactions, counter, idxLevel, userkernel);

BRAMAS Berenger's avatar
BRAMAS Berenger committed
807 808
            interactions[outsideInteractions[outInterIdx].relativeOutPosition].symb = NULLPTR;
            interactions[FMGetOppositeInterIndex(outsideInteractions[outInterIdx].relativeOutPosition)] = cell;
809 810 811 812 813 814 815 816 817 818 819 820
            M2L( interCell , interactions, counter, idxLevel, userkernel);
        }
    }
}



/////////////////////////////////////////////////////////////////////////////////////
/// Downard Pass
/////////////////////////////////////////////////////////////////////////////////////


821
__kernel void FOpenCL__downardPassPerform(__global unsigned char* currentCellsPtr, size_t currentCellsSize, __global unsigned char* currentCellsDownPtr,
822 823
                                          struct Uptr9 subCellGroupsPtr, struct size_t9 subCellGroupsSize, struct Uptr9 subCellGroupsDownPtr,
                                          int nbSubCellGroups, int idxLevel, __global void* userkernel){
824
    FOpenCLAssertLF(nbSubCellGroups != 0);
825
    struct FOpenCLGroupOfCells currentCells = BuildFOpenCLGroupOfCells(currentCellsPtr, currentCellsSize, NULLPTR, currentCellsDownPtr);
826 827
    struct FOpenCLGroupOfCells subCellGroups[9];
    for(int idx = 0 ; idx < nbSubCellGroups ; ++idx){
828
        subCellGroups[idx] = BuildFOpenCLGroupOfCells(subCellGroupsPtr.ptrs[idx], subCellGroupsSize.v[idx], NULLPTR, subCellGroupsDownPtr.ptrs[idx]);
829 830 831
    }


832 833
    FOpenCLAssertLF(nbSubCellGroups != 0);
    const int nbCells = FOpenCLGroupOfCells_getNumberOfCellsInBlock(&currentCells);
834
    int idxSubCellGroup = 0;
835
    int idxChildCell = FOpenCLGroupOfCells_getFistChildIdx(&subCellGroups[0], FOpenCLGroupOfCells_getCellIndex(&currentCells, 0));
836

837 838 839 840
    for(int idxCell = 0 ; idxCell < nbCells ; ++idxCell){
        struct FWrappeCell cell = FOpenCLGroupOfCells_getDownCell(&currentCells, idxCell);
        FOpenCLAssertLF(cell.symb->mortonIndex == FOpenCLGroupOfCells_getCellMortonIndex(&currentCells, idxCell));
        struct FWrappeCell child[8];
841

842
        FOpenCLAssertLF(idxSubCellGroup != nbSubCellGroups);
843

844 845
        for(int idxChild = 0 ; idxChild < 8 ; ++idxChild){
            child[idxChild].symb = NULLPTR;
846
        }
847 848 849 850 851 852 853 854 855 856 857 858 859 860 861

        while(idxSubCellGroup != nbSubCellGroups
              && (FOpenCLGroupOfCells_getCellMortonIndex(&subCellGroups[idxSubCellGroup], idxChildCell)>>3) == cell.symb->mortonIndex){
            const int idxChild = ((FOpenCLGroupOfCells_getCellMortonIndex(&subCellGroups[idxSubCellGroup], idxChildCell)) & 7);

            child[idxChild] = FOpenCLGroupOfCells_getDownCell(&subCellGroups[idxSubCellGroup], idxChildCell);
            idxChildCell += 1;

            if(idxChildCell == FOpenCLGroupOfCells_getNumberOfCellsInBlock(&subCellGroups[idxSubCellGroup])){
                idxChildCell = 0;
                idxSubCellGroup += 1;
            }
        }

        L2L(cell, child, idxLevel, userkernel);
862 863 864 865 866 867 868 869 870 871
    }
}



/////////////////////////////////////////////////////////////////////////////////////
/// Direct Pass MPI
/////////////////////////////////////////////////////////////////////////////////////


872
__kernel void FOpenCL__directInoutPassPerformMpi(__global unsigned char* containersPtr, size_t containersSize, __global unsigned char* containersDownPtr,
873 874 875
                                                 __global unsigned char* externalContainersPtr, size_t externalContainersSize, __global unsigned char* outsideInteractionsCl,
                                                 const __global struct OutOfBlockInteraction* outsideInteractions,
                                                 size_t nbOutsideInteractions, const int treeHeight, __global void* userkernel){
876 877
    struct FOpenCLGroupOfParticles containers = BuildFOpenCLGroupOfParticles(containersPtr, containersSize, containersDownPtr);
    struct FOpenCLGroupOfParticles containersOther = BuildFOpenCLGroupOfParticles(externalContainersPtr, externalContainersSize, NULLPTR);
878 879

    for(int outInterIdx = 0 ; outInterIdx < nbOutsideInteractions ; ++outInterIdx){
880 881 882 883 884 885
        const int leafPos = FOpenCLGroupOfParticles_getLeafIndex(&containersOther, outsideInteractions[outInterIdx].outIndex);
        if(leafPos != -1){
            FOpenCLAssertLF(FOpenCLGroupOfParticles_getLeafMortonIndex(&containersOther, leafPos) == outsideInteractions[outInterIdx].outIndex);
            struct FOpenCLGroupAttachedLeaf interParticles = FOpenCLGroupOfParticles_getLeaf(&containersOther, leafPos);
            struct FOpenCLGroupAttachedLeaf particles = FOpenCLGroupOfParticles_getLeaf(&containers, outsideInteractions[outInterIdx].insideIdxInBlock);
            FOpenCLAssertLF(FOpenCLGroupOfParticles_getLeafMortonIndex(&containers, outsideInteractions[outInterIdx].insideIdxInBlock) == outsideInteractions[outInterIdx].insideIndex);
886

BRAMAS Berenger's avatar
BRAMAS Berenger committed
887
            P2PRemote( GetPositionFromMorton(outsideInteractions[outInterIdx].insideIndex, treeHeight-1), particles, particles , interParticles, outsideInteractions[outInterIdx].relativeOutPosition, userkernel);
888 889 890 891 892 893 894 895 896 897 898
        }
    }
}


/////////////////////////////////////////////////////////////////////////////////////
/// Direct Pass
/////////////////////////////////////////////////////////////////////////////////////



899
__kernel void FOpenCL__directInPassPerform(__global unsigned char* containersPtr, size_t containersSize, __global unsigned char* containersDownPtr,
900
                                           const int treeHeight, __global void* userkernel){
901
    struct FOpenCLGroupOfParticles containers = BuildFOpenCLGroupOfParticles(containersPtr, containersSize, containersDownPtr);
902 903 904 905

    const MortonIndex blockStartIdx = FOpenCLGroupOfParticles_getStartingIndex(&containers);
    const MortonIndex blockEndIdx = FOpenCLGroupOfParticles_getEndingIndex(&containers);

906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926
    const int nbLeaves = FOpenCLGroupOfParticles_getNumberOfLeaves(&containers);

    for(int idxLeaf = 0 ; idxLeaf < nbLeaves ; ++idxLeaf){
        struct FOpenCLGroupAttachedLeaf particles = FOpenCLGroupOfParticles_getLeaf(&containers, idxLeaf);
        MortonIndex interactionsIndexes[26];
        int interactionsPosition[26];
        const int3 coord = GetPositionFromMorton(FOpenCLGroupOfParticles_getLeafMortonIndex(&containers, idxLeaf), treeHeight-1);
        int counter = GetNeighborsIndexes(coord, treeHeight,interactionsIndexes,interactionsPosition);

        struct FOpenCLGroupAttachedLeaf interactionsObjects[27];
        int neighPosition[26];
        int counterExistingCell = 0;

        for(int idxInter = 0 ; idxInter < counter ; ++idxInter){
            if( blockStartIdx <= interactionsIndexes[idxInter] && interactionsIndexes[idxInter] < blockEndIdx ){
                const int leafPos = FOpenCLGroupOfParticles_getLeafIndex(&containers, interactionsIndexes[idxInter]);
                if(leafPos){
                    FOpenCLAssertLF(FOpenCLGroupOfParticles_getLeafMortonIndex(&containers, leafPos) == interactionsIndexes[idxInter]);
                    interactionsObjects[counterExistingCell] = FOpenCLGroupOfParticles_getLeaf(&containers, leafPos);
                    neighPosition[counterExistingCell] = interactionsPosition[idxInter];
                    counterExistingCell += 1;
927 928 929
                }
            }
        }
930 931

        P2P( coord, particles, particles , interactionsObjects, neighPosition, counterExistingCell, userkernel);
932 933 934 935 936
    }
}



937
__kernel void FOpenCL__directInoutPassPerform(__global unsigned char* containersPtr, size_t containersSize, __global unsigned char* containersDownPtr,
938 939 940
                                              __global unsigned char* externalContainersPtr, size_t externalContainersSize, __global unsigned char* externalContainersDownPtr,
                                              const __global struct OutOfBlockInteraction* outsideInteractions,
                                              size_t nbOutsideInteractions, const int treeHeight, __global void* userkernel){
941 942
    struct FOpenCLGroupOfParticles containers = BuildFOpenCLGroupOfParticles(containersPtr, containersSize, containersDownPtr);
    struct FOpenCLGroupOfParticles containersOther = BuildFOpenCLGroupOfParticles(externalContainersPtr, externalContainersSize, externalContainersDownPtr);
943 944

    for(int outInterIdx = 0 ; outInterIdx < nbOutsideInteractions ; ++outInterIdx){
945 946 947 948 949 950
        const int leafPos = FOpenCLGroupOfParticles_getLeafIndex(&containersOther, outsideInteractions[outInterIdx].outIndex);
        if(leafPos != -1){
            FOpenCLAssertLF(FOpenCLGroupOfParticles_getLeafMortonIndex(&containersOther, leafPos) == outsideInteractions[outInterIdx].outIndex);
            struct FOpenCLGroupAttachedLeaf interParticles = FOpenCLGroupOfParticles_getLeaf(&containersOther, leafPos);
            struct FOpenCLGroupAttachedLeaf particles = FOpenCLGroupOfParticles_getLeaf(&containers, outsideInteractions[outInterIdx].insideIdxInBlock);
            FOpenCLAssertLF(FOpenCLGroupOfParticles_getLeafMortonIndex(&containers, outsideInteractions[outInterIdx].insideIdxInBlock) == outsideInteractions[outInterIdx].insideIndex);
951 952
            FOpenCLAssertLF(particles.nbParticles);
            FOpenCLAssertLF(interParticles.nbParticles);
953

BRAMAS Berenger's avatar
BRAMAS Berenger committed
954
            P2PRemote( GetPositionFromMorton(outsideInteractions[outInterIdx].insideIndex, treeHeight-1), particles, particles , interParticles, outsideInteractions[outInterIdx].relativeOutPosition, userkernel );
955

BRAMAS Berenger's avatar
BRAMAS Berenger committed
956
            P2PRemote( GetPositionFromMorton(outsideInteractions[outInterIdx].outIndex, treeHeight-1), interParticles, interParticles , particles, FMGetOppositeNeighIndex(outsideInteractions[outInterIdx].relativeOutPosition), userkernel);
957 958 959 960 961 962 963 964 965 966 967 968
        }
    }
}



/////////////////////////////////////////////////////////////////////////////////////
/// Merge Pass
/////////////////////////////////////////////////////////////////////////////////////



969
__kernel void FOpenCL__mergePassPerform(__global unsigned char* leafCellsPtr, size_t leafCellsSize, __global unsigned char* leafCellsDownPtr,
970 971
                                        __global unsigned char* containersPtr, size_t containersSize, __global unsigned char* containersDownPtr,
                                        __global void* userkernel){
972 973
    struct FOpenCLGroupOfCells leafCells = BuildFOpenCLGroupOfCells(leafCellsPtr,leafCellsSize, NULLPTR, leafCellsDownPtr);
    struct FOpenCLGroupOfParticles containers = BuildFOpenCLGroupOfParticles(containersPtr,containersSize, containersDownPtr);
974

975
    const int nbLeaves = FOpenCLGroupOfCells_getNumberOfCellsInBlock(&leafCells);
976

977 978 979 980 981 982
    for(int idxLeaf = 0 ; idxLeaf < nbLeaves ; ++idxLeaf){
        struct FWrappeCell cell = FOpenCLGroupOfCells_getDownCell(&leafCells, idxLeaf);
        FOpenCLAssertLF(cell.symb->mortonIndex == FOpenCLGroupOfCells_getCellMortonIndex(&leafCells, idxLeaf));
        struct FOpenCLGroupAttachedLeaf particles = FOpenCLGroupOfParticles_getLeaf(&containers, idxLeaf);
        FOpenCLAssertLF(FOpenCLGroupOfParticles_getLeafMortonIndex(&containers, idxLeaf) == FOpenCLGroupOfCells_getCellMortonIndex(&leafCells, idxLeaf));
        L2P(cell, particles, userkernel);
983 984 985
    }
}