FUserKernelEngine.hpp 20.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
// ===================================================================================
// Copyright ScalFmm 2014 I
// This software is a computer program whose purpose is to compute the FMM.
//
// This software is governed by the CeCILL-C and LGPL licenses and
// abiding by the rules of distribution of free software.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public and CeCILL-C Licenses for more details.
// "http://www.cecill.info".
// "http://www.gnu.org/licenses".
// ===================================================================================


/**
 * @file This file contains a class that inherits from FScalFMMEngine,
 * and will implement the API functions for a user defined kernel.
 */
#ifndef FUSERKERNELENGINE_HPP
#define FUSERKERNELENGINE_HPP

#include "FScalFMMEngine.hpp"


/**
 * @brief CoreCell : Cell used to store User datas
 */
class CoreCell : public FBasicCell {
    // Mutable in order to work with the API
    mutable void* userData;

34 35 36
    //Static members to be initialised before octree creation
    static Scalfmm_Cell_Descriptor user_cell_descriptor;

37
public:
38 39 40 41 42 43 44 45 46 47 48 49
    static void Init(Scalfmm_Cell_Descriptor cell_descriptor){
        user_cell_descriptor=cell_descriptor;
    }

    static Callback_init_cell GetInit(){
        return user_cell_descriptor.user_init_cell;
    }

    static Callback_free_cell GetFree(){
        return user_cell_descriptor.user_free_cell;
    }

50 51 52
    CoreCell() : userData(nullptr) {
    }

53
    //We free the cells here
54
    ~CoreCell(){
55 56 57
        if(userData){
            this->user_cell_descriptor.user_free_cell(userData);
        }
58
    }
59

60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
    /**
     * @brief setContainer store the ptr to the user data inside our
     * struct
     */
    void setContainer(void* inContainer) const {
        userData = inContainer;
    }

    /**
     * @brief getContainer : return the user datas (in order to give
     * it back to the user defined kernel function)
     */
    void* getContainer() const {
        return userData;
    }
75

76 77
};

78 79 80 81
/**
 * Define here static member
 */
Scalfmm_Cell_Descriptor CoreCell::user_cell_descriptor;
82 83 84 85 86 87 88 89

/**
 * This class simply call the function pointers from Scalfmm_Kernel_Descriptor.
 * If not pointer is set the calls are skipped.
 * The userData is given at any calls.
 */
template< class CellClass, class ContainerClass>
class CoreKernel : public FAbstractKernels<CellClass,ContainerClass> {
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
    Scalfmm_Kernel_Descriptor kernel;
    void* userData;

public:
    CoreKernel(Scalfmm_Kernel_Descriptor inKernel, void* inUserData) : kernel(inKernel) , userData(inUserData){
    }

    /** Default destructor */
    virtual ~CoreKernel(){
    }

    /** Do nothing */
    virtual void P2M(CellClass* const cell, const ContainerClass* const container) {
        if(kernel.p2m) kernel.p2m(cell->getContainer(), container->getNbParticles(), container->getIndexes().data(), userData);
    }

    /** Do nothing */
    virtual void M2M(CellClass* const FRestrict cell, const CellClass*const FRestrict *const FRestrict children, const int level) {
        if(kernel.m2m){
            for(int idx = 0 ; idx < 8 ; ++idx){
                if( children[idx] ){
                    kernel.m2m(level, cell->getContainer(), idx, children[idx]->getContainer(), userData);
                }
            }
        }
    }

    /** Do nothing */
    virtual void M2L(CellClass* const FRestrict cell, const CellClass* interactions[], const int , const int level) {
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
        if(kernel.m2l_full){//all 343 interactions will be computed directly
            //First, copy the fmm cell inside an array of user cells
            void * userCellArray[343];
            for(int i=0 ; i<343 ; ++i){
                if(interactions[i] != nullptr){
                    userCellArray[i] = interactions[i]->getContainer();
                }
                else{
                    userCellArray[i] = nullptr;
                }
            }
            kernel.m2l_full(level,cell->getContainer(),userCellArray,userData);
        }
        else{
            if(kernel.m2l){
                for(int idx = 0 ; idx < 343 ; ++idx){
                    if( interactions[idx] ){
                        kernel.m2l(level, cell->getContainer(), idx, interactions[idx]->getContainer(), userData);
                    }
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
                }
            }
        }
    }

    /** Do nothing */
    virtual void L2L(const CellClass* const FRestrict cell, CellClass* FRestrict *const FRestrict children, const int level) {
        if(kernel.l2l){
            for(int idx = 0 ; idx < 8 ; ++idx){
                if( children[idx] ){
                    kernel.l2l(level, cell->getContainer(), idx, children[idx]->getContainer(), userData);
                }
            }
        }
    }

    /** Do nothing */
    virtual void L2P(const CellClass* const cell, ContainerClass* const container){
        if(kernel.l2p) kernel.l2p(cell->getContainer(), container->getNbParticles(), container->getIndexes().data(), userData);
    }


    /** Do nothing */
    virtual void P2P(const FTreeCoordinate& ,
                     ContainerClass* const FRestrict targets, const ContainerClass* const FRestrict /*sources*/,
                     ContainerClass* const neighbors[27], const int ){
        if(kernel.p2pinner) kernel.p2pinner(targets->getNbParticles(), targets->getIndexes().data(), userData);

167 168
        if(kernel.p2p_full){
            //Create the arrays of size and indexes
169 170
            FSize nbPartPerNeighbors[27];
            const FSize * indicesPerNeighbors[27];
171 172 173 174 175 176 177 178 179 180 181 182
            for(int idx=0 ; idx<27 ; ++idx){
                if(neighbors[idx]){
                    nbPartPerNeighbors[idx] = neighbors[idx]->getNbParticles();
                    indicesPerNeighbors[idx] = neighbors[idx]->getIndexes().data();
                }
                else{
                    nbPartPerNeighbors[idx] = 0;
                    indicesPerNeighbors[idx] = nullptr;
                }
            }
            kernel.p2p_full(targets->getNbParticles(),targets->getIndexes().data(),indicesPerNeighbors,nbPartPerNeighbors,userData);
        }
183 184 185 186
        if(kernel.p2p){
            for(int idx = 0 ; idx < 27 ; ++idx){
                if( neighbors[idx] ){
                    kernel.p2p(targets->getNbParticles(), targets->getIndexes().data(),
187
                               neighbors[idx]->getNbParticles(), neighbors[idx]->getIndexes().data(), userData);
188 189 190 191 192 193 194 195 196 197 198 199 200
                }
            }
        }
    }

    /** Do nothing */
    virtual void P2PRemote(const FTreeCoordinate& ,
                     ContainerClass* const FRestrict , const ContainerClass* const FRestrict ,
                     ContainerClass* const [27], const int ){
    }

};

201
template<class FReal>
202 203 204 205 206
class FUserKernelEngine : public FScalFMMEngine{

private:

    //Typedefs :
207 208
    typedef FP2PParticleContainerIndexed<FReal>           ContainerClass;
    typedef FSimpleLeaf<FReal, ContainerClass>                   LeafClass;
209
    typedef FOctree<FReal,CoreCell,ContainerClass,LeafClass>  OctreeClass;
210
    typedef CoreKernel<CoreCell,ContainerClass>     CoreKernelClass;
211

212
    //For arranger classes
213
    typedef FBasicParticleContainerIndexedMover<FReal, OctreeClass, ContainerClass> MoverClass;
214
    typedef FOctreeArranger<FReal,OctreeClass, ContainerClass, MoverClass> ArrangerClass;
215
    typedef FArrangerPeriodic<FReal,OctreeClass, ContainerClass, MoverClass> ArrangerClassPeriodic;
216 217 218 219 220 221 222


    //Attributes
    OctreeClass * octree;
    CoreKernelClass * kernel;
    ArrangerClass * arranger;

223

224
public:
225
    FUserKernelEngine(/*int TreeHeight, double BoxWidth , double * BoxCenter, */scalfmm_kernel_type KernelType) :
226
        octree(nullptr), kernel(nullptr), arranger(nullptr){
227
        //        octree = new OctreeClass(TreeHeight,FMath::Min(3,TreeHeight-1),BoxWidth,FPoint<FReal>(BoxCenter));
228 229 230 231 232
        kernelType = KernelType;
        //Kernel is not set now because the user must provide a
        //Scalfmm_Kernel_descriptor
    }

233

234 235
    ~FUserKernelEngine(){
        delete octree;
236
        octree=nullptr;
237 238
        if(arranger){
            delete arranger;
239
            arranger=nullptr;
240 241 242
        }
        if(kernel){
            delete kernel;
243
            kernel=nullptr;
244 245
        }
    }
246 247

    void user_kernel_config( Scalfmm_Kernel_Descriptor userKernel, void * userDatas){
248 249 250
        if(!kernel){
            kernel = new CoreKernelClass(userKernel,userDatas);
        }
251 252
    }

253 254
    void build_tree(int TreeHeight,double BoxWidth,double* BoxCenter,Scalfmm_Cell_Descriptor user_cell_descriptor){
        CoreCell::Init(user_cell_descriptor);
255
        this->octree = new OctreeClass(TreeHeight,FMath::Min(3,TreeHeight-1),BoxWidth,FPoint<FReal>(BoxCenter));
256
    }
257 258 259

    void tree_insert_particles( int NbPositions, double * arrayX, double * arrayY, double * arrayZ){
        for(int idPart = 0; idPart<NbPositions ; ++idPart){
260
            octree->insert(FPoint<FReal>(arrayX[idPart],arrayY[idPart],arrayZ[idPart]),idPart);
261 262
        }
        nbPart += NbPositions;
263
        this->init_cell();
264 265 266 267
    }

    void tree_insert_particles_xyz( int NbPositions, double * XYZ){
        for(int idPart = 0; idPart<NbPositions ; ++idPart){
268
            octree->insert(FPoint<FReal>(&XYZ[3*idPart]),idPart);
269 270
        }
        nbPart += NbPositions;
271
        this->init_cell();
272 273
    }

274 275 276 277 278 279
    /**
     * To retrieve the positions, in order to move the parts
     */
    void get_positions_xyz(int NbPositions, double * positionsToFill){
        octree->forEachLeaf([&](LeafClass* leaf){
                ContainerClass * sources = leaf->getSrc();
280
                const FVector<FSize>& indexes = leaf->getTargets()->getIndexes();
281
                FSize nbPartThere = sources->getNbParticles();
282
                for(FSize idxPart = 0 ; idxPart<nbPartThere ; ++idxPart){
283 284 285 286 287 288 289 290 291 292
                    positionsToFill[indexes[idxPart]*3+0] = sources->getPositions()[0][idxPart];
                    positionsToFill[indexes[idxPart]*3+1] = sources->getPositions()[1][idxPart];
                    positionsToFill[indexes[idxPart]*3+2] = sources->getPositions()[2][idxPart];
                }
            });
    }

    void get_positions_xyz_npart(int NbPositions, int * idxOfParticles, double * positionsToFill){
        octree->forEachLeaf([&](LeafClass* leaf){
                ContainerClass * sources = leaf->getSrc();
293
                const FVector<FSize>& indexes = leaf->getTargets()->getIndexes();
294
                FSize nbPartThere = sources->getNbParticles();
295
                for(FSize idxPart = 0 ; idxPart<nbPartThere ; ++idxPart){
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
                    int iterPart = 0;
                    bool notFoundYet = true;
                    while(iterPart < NbPositions && notFoundYet){
                        if(indexes[idxPart] == idxOfParticles[iterPart]){
                            positionsToFill[indexes[idxPart]*3+0] =  sources->getPositions()[0][idxPart];
                            positionsToFill[indexes[idxPart]*3+1] =  sources->getPositions()[1][idxPart];
                            positionsToFill[indexes[idxPart]*3+2] =  sources->getPositions()[2][idxPart];
                            notFoundYet = false;
                        }
                        else{
                            ++iterPart;
                        }
                    }
                }
            });
    }

    void get_positions( int NbPositions, double * X, double * Y , double * Z){
        octree->forEachLeaf([&](LeafClass* leaf){
                ContainerClass * sources = leaf->getSrc();
316
                const FVector<FSize>& indexes = leaf->getTargets()->getIndexes();
317
                FSize nbPartThere = sources->getNbParticles();
318
                for(FSize idxPart = 0 ; idxPart<nbPartThere ; ++idxPart){
319 320 321 322 323 324 325 326 327 328
                    X[indexes[idxPart]] = sources->getPositions()[0][idxPart];
                    Y[indexes[idxPart]] = sources->getPositions()[1][idxPart];
                    Z[indexes[idxPart]] = sources->getPositions()[2][idxPart];
                }
            });
    }

    void get_positions_npart(int NbPositions, int * idxOfParticles,double * X, double * Y , double * Z){
        octree->forEachLeaf([&](LeafClass* leaf){
                ContainerClass * sources = leaf->getSrc();
329
                const FVector<FSize>& indexes = leaf->getTargets()->getIndexes();
330
                FSize nbPartThere = sources->getNbParticles();
331
                for(FSize idxPart = 0 ; idxPart<nbPartThere ; ++idxPart){
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
                    int iterPart = 0;
                    bool notFoundYet = true;
                    while(iterPart < NbPositions && notFoundYet){
                        if(indexes[idxPart] == idxOfParticles[iterPart]){
                            X[indexes[idxPart]] =  sources->getPositions()[0][idxPart];
                            Y[indexes[idxPart]] =  sources->getPositions()[1][idxPart];
                            Z[indexes[idxPart]] =  sources->getPositions()[2][idxPart];
                            notFoundYet = false;
                        }
                        else{
                            ++iterPart;
                        }
                    }
                }
            });
    }



    //Arranger parts : following function provide a way to move parts
    //inside the tree
    void add_to_positions_xyz(int NbPositions,double * updatedXYZ){
        octree->forEachLeaf([&](LeafClass* leaf){
                ContainerClass * sources = leaf->getSrc();
356
                const FVector<FSize>& indexes = leaf->getTargets()->getIndexes();
357
                FSize nbPartThere = sources->getNbParticles();
358
                for(FSize idxPart = 0 ; idxPart<nbPartThere ; ++idxPart){
359 360 361 362 363 364 365 366 367 368
                    sources->getWPositions()[0][idxPart] += updatedXYZ[indexes[idxPart]*3+0];
                    sources->getWPositions()[1][idxPart] += updatedXYZ[indexes[idxPart]*3+1];
                    sources->getWPositions()[2][idxPart] += updatedXYZ[indexes[idxPart]*3+2];
                }
            });
    }

    void add_to_positions(int NbPositions,double * X, double * Y , double * Z){
        octree->forEachLeaf([&](LeafClass* leaf){
                ContainerClass * sources = leaf->getSrc();
369
                const FVector<FSize>& indexes = leaf->getTargets()->getIndexes();
370
                FSize nbPartThere = sources->getNbParticles();
371
                for(FSize idxPart = 0 ; idxPart<nbPartThere ; ++idxPart){
372 373 374 375 376 377 378 379 380 381 382
                    sources->getWPositions()[0][idxPart] += X[indexes[idxPart]];
                    sources->getWPositions()[1][idxPart] += Y[indexes[idxPart]];
                    sources->getWPositions()[2][idxPart] += Z[indexes[idxPart]];
                }
            });
    }


    void set_positions_xyz(int NbPositions, double * updatedXYZ){
        octree->forEachLeaf([&](LeafClass* leaf){
                ContainerClass * sources = leaf->getSrc();
383
                const FVector<FSize>& indexes = leaf->getTargets()->getIndexes();
384
                FSize nbPartThere = sources->getNbParticles();
385
                for(FSize idxPart = 0 ; idxPart<nbPartThere ; ++idxPart){
386 387 388 389 390 391 392 393 394 395
                    sources->getWPositions()[0][idxPart] = updatedXYZ[indexes[idxPart]*3+0];
                    sources->getWPositions()[1][idxPart] = updatedXYZ[indexes[idxPart]*3+1];
                    sources->getWPositions()[2][idxPart] = updatedXYZ[indexes[idxPart]*3+2];
                }
            });
    }

    void set_positions(int NbPositions, double * X, double * Y, double * Z){
        octree->forEachLeaf([&](LeafClass* leaf){
                ContainerClass * sources = leaf->getSrc();
396
                const FVector<FSize>& indexes = leaf->getTargets()->getIndexes();
397
                FSize nbPartThere = sources->getNbParticles();
398
                for(FSize idxPart = 0 ; idxPart<nbPartThere ; ++idxPart){
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
                    sources->getWPositions()[0][idxPart] = X[indexes[idxPart]];
                    sources->getWPositions()[1][idxPart] = Y[indexes[idxPart]];
                    sources->getWPositions()[2][idxPart] = Z[indexes[idxPart]];
                }
            });
    }


    void update_tree(){
        if(arranger){
            arranger->rearrange();
            //then, we need to re-allocate cells user data for the
            //cells created during the process and free user datas for
            //the cells removed during the process
            init_cell();
        }
        else{
            if(Algorithm == 2){ //case in wich the periodic algorithm is used
                arranger = new ArrangerClassPeriodic(octree);
                arranger->rearrange();
            }
            else{
                arranger = new ArrangerClass(octree);
                arranger->rearrange();
                init_cell();
            }
        }
    }

428 429 430
    /*
     * Call the user allocator on userDatas member field of each cell
     */
431 432 433
    void init_cell(){

        double boxwidth = octree->getBoxWidth();
434
        FPoint<FReal> BoxCenter = octree->getBoxCenter();
435 436 437 438 439 440 441
        double boxCorner[3];
        boxCorner[0] = BoxCenter.getX() - boxwidth/2.0;
        boxCorner[1] = BoxCenter.getY() - boxwidth/2.0;
        boxCorner[2] = BoxCenter.getZ() - boxwidth/2.0;
        //apply user function on each cell
        octree->forEachCellWithLevel([&](CoreCell * currCell,const int currLevel){
                if(!(currCell->getContainer())){
442 443 444 445 446 447 448
                    FTreeCoordinate currCoord = currCell->getCoordinate();
                    int arrayCoord[3] = {currCoord.getX(),currCoord.getY(),currCoord.getZ()};
                    MortonIndex    currMorton = currCoord.getMortonIndex(currLevel);
                    double position[3];
                    position[0] = boxCorner[0] + currCoord.getX()*boxwidth/double(1<<currLevel);
                    position[1] = boxCorner[1] + currCoord.getY()*boxwidth/double(1<<currLevel);
                    position[2] = boxCorner[2] + currCoord.getZ()*boxwidth/double(1<<currLevel);
449 450 451
                    currCell->setContainer(CoreCell::GetInit()(currLevel,currMorton,arrayCoord,position));
                }
            });
452 453
    }

454

455 456
    void free_cell(Callback_free_cell user_cell_deallocator){
        octree->forEachCell([&](CoreCell * currCell){
PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
457 458
                if(currCell->getContainer()){
                    user_cell_deallocator(currCell->getContainer());
459
                    currCell->setContainer(nullptr);
PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
460
                }
461 462 463 464
            });
    }

    void execute_fmm(){
465
        FAssertLF(kernel,"No kernel set, please use scalfmm_user_kernel_config before calling the execute routine ... Exiting \n");
466 467 468
        switch(Algorithm){
        case 0:
            {
469
                typedef FFmmAlgorithm<OctreeClass,CoreCell,ContainerClass,CoreKernelClass,LeafClass> AlgoClassSeq;
470 471 472 473 474 475 476 477 478 479 480 481 482
                AlgoClassSeq algoSeq(octree,kernel);
                algoSeq.execute();
                break;
            }
        case 1:
            {
                typedef FFmmAlgorithmThread<OctreeClass,CoreCell,ContainerClass,CoreKernelClass,LeafClass> AlgoClassThread;
                AlgoClassThread algoThread(octree,kernel);
                algoThread.execute();
                break;
            }
        case 2:
            {
483
                typedef FFmmAlgorithmPeriodic<FReal,OctreeClass,CoreCell,ContainerClass,CoreKernelClass,LeafClass> AlgoClassPeriodic;
484 485 486 487 488 489 490 491 492 493
                AlgoClassPeriodic algoPeriod(octree,2);
                algoPeriod.setKernel(kernel);
                algoPeriod.execute();
                break;
            }
        default :
            std::cout<< "No algorithm found (probably for strange reasons) : "<< Algorithm <<" exiting" << std::endl;
        }

    }
PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
494

495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
    void reset_tree(Callback_reset_cell cellReset){
        double boxwidth = octree->getBoxWidth();
        FPoint<FReal> BoxCenter = octree->getBoxCenter();
        double boxCorner[3];
        boxCorner[0] = BoxCenter.getX() - boxwidth/2.0;
        boxCorner[1] = BoxCenter.getY() - boxwidth/2.0;
        boxCorner[2] = BoxCenter.getZ() - boxwidth/2.0;
        //apply user function reset on each user's cell
        octree->forEachCellWithLevel([&](CoreCell * currCell,const int currLevel){
                if(currCell->getContainer()){
                    FTreeCoordinate currCoord = currCell->getCoordinate();
                    int arrayCoord[3] = {currCoord.getX(),currCoord.getY(),currCoord.getZ()};
                    MortonIndex    currMorton = currCoord.getMortonIndex(currLevel);
                    double position[3];
                    position[0] = boxCorner[0] + currCoord.getX()*boxwidth/double(1<<currLevel);
                    position[1] = boxCorner[1] + currCoord.getY()*boxwidth/double(1<<currLevel);
                    position[2] = boxCorner[2] + currCoord.getZ()*boxwidth/double(1<<currLevel);
                    cellReset(currLevel,currMorton,arrayCoord,position,currCell->getContainer());
                }
            });
    }

517
    void intern_dealloc_handle(Callback_free_cell userDeallocator){
PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
518
        free_cell(userDeallocator);
519
    }
520 521 522 523
};


#endif //FUSERKERNELENGINE_HPP