FFmmAlgorithmStarpuGroup.hpp 74.4 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
// ===================================================================================
// Logiciel initial: ScalFmm Version 0.5
// Co-auteurs : Olivier Coulaud, Bérenger Bramas.
// Propriétaires : INRIA.
// Copyright © 2011-2012, diffusé sous les termes et conditions d’une licence propriétaire.
// Initial software: ScalFmm Version 0.5
// Co-authors: Olivier Coulaud, Bérenger Bramas.
// Owners: INRIA.
// Copyright © 2011-2012, spread under the terms and conditions of a proprietary license.
// ===================================================================================
#ifndef FFMMALGORITHMSTARPUGROUP_HPP
#define FFMMALGORITHMSTARPUGROUP_HPP

#include "../Utils/FAssertable.hpp"
#include "../Utils/FDebug.hpp"
#include "../Utils/FTrace.hpp"
#include "../Utils/FTic.hpp"
#include "../Utils/FGlobal.hpp"
#include "../Utils/FMemUtils.hpp"

#include "../Containers/FOctree.hpp"
#include "../Containers/FBoolArray.hpp"


#include "../Extensions/FExtendCoordinate.hpp"
#include "../Extensions/FExtendMortonIndex.hpp"

28 29
#include <starpu.h>

30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
/*
TODO:
scinder multipole/local
*/


/**
* @author Berenger Bramas (berenger.bramas@inria.fr)
* @class FFmmAlgorithmStarpuGroup
* @brief
* Please read the license
*/
template<class OctreeClass, class ParticleClass, class CellClass, class ContainerClass, class KernelClass, class LeafClass>
class FFmmAlgorithmStarpuGroup : protected FAssertable{
    /////////////////////////////////////////////////////////////
    // Utils classes
    /////////////////////////////////////////////////////////////

48

49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
    struct MortonContainer : public FExtendMortonIndex, public FExtendCoordinate {
        ContainerClass container;
    };

    /** This structure holds the data properties needed
      * by a cell/leaf to finish its computation
      */
    struct TransferProperties{
        explicit TransferProperties(const int inIndex = 0, const int inPosition = 0, const int inDataPos = 0)
            : indexWhoNeedsData(inIndex), positionInComputationArray(inPosition), positionInDataArray(inDataPos) {
        }
        // In the group destination, who need the data?
        int indexWhoNeedsData;
        // where to put the data in the array
        int positionInComputationArray;
        // Where to read the data from?
        int positionInDataArray;
    };

    /** The transfer buffer holds many properties
      * it has enough information to create a copy task and
      * a process task
      */
    struct TransferBuffer {
73
        TransferBuffer() : groupDestination(0) {
74 75 76 77 78 79 80
        }
        // the group who need the data
        int groupDestination;
        // position in the original group
        FVector<int> originalIndexPosition;
        // transfer properties
        FVector<TransferProperties> compuationProperties;
81 82
        // where data will be copied
        int indexToStarCopying;
83 84 85 86 87 88
    };

    /** A group contains several cells
      * and some properties
      */
    struct Group {
89 90
        Group() : cellArray(0), needOther(0), leavesArray(0), transferBufferCell(0),
            nbCellToReceive(0), transferBufferLeaf(0), nbLeafToReceive(0) {
91 92
            handleCellArrayUp = 0;
            handleCellArrayDown = 0;
93 94 95 96
            handleLeafArray = 0;
            handleLeafArrayRead = 0;
            handleTransferCell = 0;
            handleTransferLeaf = 0;
97 98 99
        }
        ~Group(){
            delete[] cellArray;
100
            delete[] needOther;
101 102 103 104
            delete[] leavesArray;
            for(int idx = 0 ; idx < dataToSend.getSize() ; ++idx){
                delete dataToSend[idx];
            }
105
            delete[] transferBufferCell;
106
            delete[] transferBufferLeaf;
107

108 109
            if( handleCellArrayUp != starpu_data_handle_t(0)) starpu_data_unregister(handleCellArrayUp);
            if( handleCellArrayDown != starpu_data_handle_t(0)) starpu_data_unregister(handleCellArrayDown);
110 111 112 113
            if( handleLeafArray != starpu_data_handle_t(0)) starpu_data_unregister(handleLeafArray);
            if( handleLeafArrayRead != starpu_data_handle_t(0)) starpu_data_unregister(handleLeafArrayRead);
            if( handleTransferCell != starpu_data_handle_t(0)) starpu_data_unregister(handleTransferCell);
            if( handleTransferLeaf != starpu_data_handle_t(0)) starpu_data_unregister(handleTransferLeaf);
114 115 116 117 118 119 120 121 122
        }

        // Morton index the group start at
        MortonIndex beginIndex;
        // Morton index the group end at
        MortonIndex endIndex;
        // Number of elements in the group, usually GroupSize
        int nbElements;
        // The data of the group
123
        CellClass* FRestrict cellArray;
124
        bool* needOther;
125 126 127 128 129 130 131
        // Or the leaves data
        MortonContainer* FRestrict leavesArray;
        // Information needed to compute parent child operations
        int indexOfStartInLowerGroups;
        FVector<Group*> lowerGroups;
        // Information needed in case of transfering data needed
        FVector<TransferBuffer*> dataToSend;
132 133 134 135

        // memory to copy before compute remotly
        CellClass* FRestrict transferBufferCell;
        int nbCellToReceive;
136 137 138 139

        // memory to copy before compute remotly
        MortonContainer* FRestrict transferBufferLeaf;
        int nbLeafToReceive;
140 141

        // Starpu data
142 143
        starpu_data_handle_t handleCellArrayUp;
        starpu_data_handle_t handleCellArrayDown;
144 145 146 147
        starpu_data_handle_t handleLeafArray;
        starpu_data_handle_t handleLeafArrayRead;
        starpu_data_handle_t handleTransferCell;
        starpu_data_handle_t handleTransferLeaf;
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
    //////////////////////////////////////////////////////////////////
    // Init Kernels
    //////////////////////////////////////////////////////////////////

    // Init the fmm kernel (1 per thread)
    void initKernels(){
        globalKernels = new KernelClass*[starpu_worker_get_count()];
        memset(globalKernels, 0, sizeof(KernelClass*) * starpu_worker_get_count());

        for(unsigned int workerid = 0; workerid < starpu_worker_get_count(); ++workerid){
            if( starpu_worker_get_type(workerid) == STARPU_CPU_WORKER ){
                globalKernels[workerid] = new KernelClass(*kernel);
            }
        }
    }

    // Delete kernels
    void releaseKernels(){
        for(unsigned int workerid = 0; workerid < starpu_worker_get_count(); ++workerid){
            delete globalKernels[workerid];
        }
        delete[] globalKernels;
    }

174 175 176 177 178 179 180 181 182 183
    /////////////////////////////////////////////////////////////
    // Attributes
    /////////////////////////////////////////////////////////////

    OctreeClass* const tree;      //< The octree to work on
    const int OctreeHeight;       //< Height of the tree
    const int BlockSize;          //< Size of the block
    Group**const blockedTree;     //< Current block tree
    int*const blockedPerLevel;    //< Number of block per level
    KernelClass* const kernel;    //< The kernel
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
    const bool useStarpuPerfModel;//< to know if perf model has to be used

    static const int MaxChild = 9;

    starpu_codelet p2m_cl;
    starpu_codelet p2p_cl;
    starpu_codelet p2p_restore_cl;
    starpu_codelet m2m_cl[MaxChild];
    starpu_codelet m2l_cl;
    starpu_codelet m2l_other_cl;
    starpu_codelet m2l_copy_cl;
    starpu_codelet l2l_cl[MaxChild];
    starpu_codelet l2p_cl;

    starpu_perfmodel p2p_model;
    starpu_perfmodel p2p_restore_model;
    starpu_perfmodel p2m_model;
    starpu_perfmodel m2m_model;
    starpu_perfmodel m2l_model;
    starpu_perfmodel m2l_other_model;
    starpu_perfmodel m2l_copy_model;
    starpu_perfmodel l2l_model;
    starpu_perfmodel l2p_model;

    void initCodelet(){
        // init perf model
        memset(&p2p_model, 0, sizeof(p2p_model));
        p2p_model.type = STARPU_HISTORY_BASED;
        p2p_model.symbol = "P2P";
        memset(&p2p_restore_model, 0, sizeof(p2p_restore_model));
        p2p_restore_model.type = STARPU_HISTORY_BASED;
        p2p_restore_model.symbol = "P2P Restore";
        memset(&p2m_model, 0, sizeof(p2m_model));
        p2m_model.type = STARPU_HISTORY_BASED;
        p2m_model.symbol = "P2M";
        memset(&m2l_model, 0, sizeof(m2l_model));
        m2l_model.type = STARPU_HISTORY_BASED;
        m2l_model.symbol = "M2L";
        memset(&m2l_other_model, 0, sizeof(m2l_other_model));
        m2l_other_model.type = STARPU_HISTORY_BASED;
        m2l_other_model.symbol = "M2L Other";
        memset(&m2l_copy_model, 0, sizeof(m2l_model));
        m2l_copy_model.type = STARPU_HISTORY_BASED;
        m2l_copy_model.symbol = "M2L Copy";
        memset(&l2p_model, 0, sizeof(l2p_model));
        l2p_model.type = STARPU_HISTORY_BASED;
        l2p_model.symbol = "L2P";
        memset(&l2l_model, 0, sizeof(l2l_model));
        l2l_model.type = STARPU_HISTORY_BASED;
        l2l_model.symbol = "L2L";
        memset(&m2m_model, 0, sizeof(m2m_model));
        m2m_model.type = STARPU_HISTORY_BASED;
        m2m_model.symbol = "M2M";

        // P2M
        memset(&p2m_cl, 0, sizeof(p2m_cl));
        p2m_cl.where = STARPU_CPU;
        p2m_cl.cpu_funcs[0] = p2m_cpu;
        p2m_cl.nbuffers = 2;
        p2m_cl.modes[0] = STARPU_W;
        p2m_cl.modes[1] = STARPU_R;
        if(useStarpuPerfModel) p2m_cl.model = &p2m_model;
        // P2P
        memset(&p2p_cl, 0, sizeof(starpu_codelet) );
        p2p_cl.where = STARPU_CPU;
        p2p_cl.cpu_funcs[0] = p2p_cpu;
        p2p_cl.nbuffers = 2;
        p2p_cl.modes[0] = STARPU_RW;
        p2p_cl.modes[1] = STARPU_RW;
        if( useStarpuPerfModel ) p2p_cl.model = &p2p_model;
        // P2P restore
        memset(&p2p_restore_cl, 0, sizeof(starpu_codelet) );
        p2p_restore_cl.where = STARPU_CPU;
        p2p_restore_cl.cpu_funcs[0] = p2p_restore_cpu;
        p2p_restore_cl.nbuffers = 2;
        p2p_restore_cl.modes[0] = STARPU_RW;
        p2p_restore_cl.modes[1] = STARPU_R;
        if( useStarpuPerfModel ) p2p_restore_cl.model = &p2p_restore_model;

        // L2P
        memset(&l2p_cl, 0, sizeof(l2p_cl));
        l2p_cl.where = STARPU_CPU;
        l2p_cl.cpu_funcs[0] = l2p_cpu;
        l2p_cl.nbuffers = 2;
        l2p_cl.modes[0] = STARPU_R;
        l2p_cl.modes[1] = STARPU_RW;
        if(useStarpuPerfModel)  l2p_cl.model = &l2p_model;
        // M2L
        memset(&m2l_cl, 0, sizeof(starpu_codelet) );
        m2l_cl.where = STARPU_CPU;
        m2l_cl.cpu_funcs[0] = m2l_cpu;
275
        m2l_cl.nbuffers = 2;
276
        m2l_cl.modes[0] = STARPU_RW;
277
        m2l_cl.modes[1] = STARPU_R;
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
        if( useStarpuPerfModel ) m2l_cl.model = &m2l_model;
        // M2L other
        memset(&m2l_other_cl, 0, sizeof(starpu_codelet) );
        m2l_other_cl.where = STARPU_CPU;
        m2l_other_cl.cpu_funcs[0] = m2l_other_cpu;
        m2l_other_cl.nbuffers = 2;
        m2l_other_cl.modes[0] = STARPU_RW;
        m2l_other_cl.modes[1] = STARPU_R;
        if( useStarpuPerfModel ) m2l_other_cl.model = &m2l_other_model;
        // M2L copy
        memset(&m2l_copy_cl, 0, sizeof(starpu_codelet) );
        m2l_copy_cl.where = STARPU_CPU;
        m2l_copy_cl.cpu_funcs[0] = m2l_copy_cpu;
        m2l_copy_cl.nbuffers = 2;
        m2l_copy_cl.modes[0] = STARPU_RW;
        m2l_copy_cl.modes[1] = STARPU_R;
        if( useStarpuPerfModel ) m2l_copy_cl.model = &m2l_copy_model;
        // M2M & L2L
        memset(m2m_cl, 0, sizeof(starpu_codelet) * MaxChild);
        memset(l2l_cl, 0, sizeof(starpu_codelet) * MaxChild);
        for( int idxChild = 0 ; idxChild < MaxChild ; ++idxChild){
            m2m_cl[idxChild].where = STARPU_CPU;
            m2m_cl[idxChild].cpu_funcs[0] = m2m_cpu;
            m2m_cl[idxChild].nbuffers = idxChild + 2;
            m2m_cl[idxChild].modes[0] = STARPU_W;
            if( useStarpuPerfModel) m2m_cl[idxChild].model = &m2m_model;

            l2l_cl[idxChild].where = STARPU_CPU;
            l2l_cl[idxChild].cpu_funcs[0] = l2l_cpu;
            l2l_cl[idxChild].nbuffers = idxChild + 2;
            l2l_cl[idxChild].modes[0] = STARPU_R;
            if( useStarpuPerfModel) l2l_cl[idxChild].model = &l2l_model;

            for( int idxMode = 0 ; idxMode <= idxChild ; ++idxMode){
                m2m_cl[idxChild].modes[idxMode+1] = STARPU_R;
                l2l_cl[idxChild].modes[idxMode+1] = STARPU_RW;
            }
        }
    }
317 318 319 320 321 322 323 324

public:
    /** The constructor need the octree and the kernel used for computation
      * @param inTree the octree to work on
      * @param inKernels the kernel to call
      * An assert is launched if one of the arguments is null
      */
    FFmmAlgorithmStarpuGroup(OctreeClass* const inTree, KernelClass* const inKernel,
325
                             const int inBlockedSize = 250, const bool inUseStarpuPerfModel = false)
326 327 328 329
        : tree(inTree), OctreeHeight(tree->getHeight()),
          BlockSize(inBlockedSize),
          blockedTree(new Group*[OctreeHeight + 1]) ,
          blockedPerLevel(new int[OctreeHeight + 1]),
330 331
          kernel(inKernel),
          useStarpuPerfModel(inUseStarpuPerfModel) {
332 333 334 335 336 337 338

        fassert(tree, "tree cannot be null", __LINE__, __FILE__);
        fassert(kernel, "kernel cannot be null", __LINE__, __FILE__);

        memset(blockedTree, 0, sizeof(Group*) * (OctreeHeight + 1));
        memset(blockedPerLevel, 0, (OctreeHeight + 1) * sizeof(int));

339
        FDEBUG(FDebug::Controller << "FFmmAlgorithmStarpuGroup (Block size = " << BlockSize <<")\n");
340 341 342 343 344 345 346 347 348 349 350 351 352 353
    }

    /** Default destructor */
    virtual ~FFmmAlgorithmStarpuGroup(){
        delete[] blockedTree;
        delete[] blockedPerLevel;
    }

    /////////////////////////////////////////////////////////////
    // Tree to group functions
    /////////////////////////////////////////////////////////////

    /**
      */
354
    void buildGroups(const int nbThreads = -1){
355 356
        FTRACE( FTrace::FFunction functionTrace(__FUNCTION__, "Fmm" , __FILE__ , __LINE__) );

357 358 359 360 361
        starpu_conf setup;
        starpu_conf_init(&setup);
        setup.ncpus = nbThreads;
        // Run starpu
        starpu_init(&setup);
362 363 364 365 366 367 368 369
        FDEBUG(FDebug::Controller << "Start starpu runtime, Nb Workers = " << starpu_worker_get_count() << "\n");

        // create codelet
        initCodelet();

        // create kernel for all thread
        initKernels();

370 371 372 373 374 375 376 377 378 379 380 381
        // Count leaf to allocate and big array
        typename OctreeClass::Iterator* iterArray = 0;
        {
            int leafsNumber = 0;
            typename OctreeClass::Iterator octreeIterator(tree);
            octreeIterator.gotoBottomLeft();
            do{
                ++leafsNumber;
            } while(octreeIterator.moveRight());
            iterArray = new typename OctreeClass::Iterator[leafsNumber];
            fassert(iterArray, "iterArray bad alloc", __LINE__, __FILE__);
        }
382
        FDEBUG( FDebug::Controller << "\tCopy the tree\n"; );
383 384 385 386 387 388 389 390 391 392 393 394 395 396
        // Then we start creating the block
        {
            typename OctreeClass::Iterator octreeIterator(tree);
            typename OctreeClass::Iterator avoidGotLeftIterator(octreeIterator);
            for(int idxLevel = 1; idxLevel < OctreeHeight; ++idxLevel){
                // put every thing in the array
                int counterAtLevel = 0;
                do{
                    iterArray[counterAtLevel++] = octreeIterator;
                } while(octreeIterator.moveRight());
                avoidGotLeftIterator.moveDown();
                octreeIterator = avoidGotLeftIterator;
                // find the number of groups
                const int NbGroups = (counterAtLevel + BlockSize - 1) / BlockSize;
397
                FDEBUG( FDebug::Controller << "\t\tAt level " << idxLevel << " there are " << NbGroups << " groups\n"; );
398 399 400 401 402 403 404 405 406 407
                blockedPerLevel[idxLevel] = NbGroups;
                blockedTree[idxLevel] = new Group[NbGroups];

                // copy data to group
                int copyIndex = 0;
                for( int idxGroup = 0 ; idxGroup < NbGroups ; ++idxGroup ){
                    const int cellsInThisGroup = FMath::Min(BlockSize, counterAtLevel-copyIndex);

                    blockedTree[idxLevel][idxGroup].nbElements = cellsInThisGroup;
                    blockedTree[idxLevel][idxGroup].cellArray = new CellClass[cellsInThisGroup];
408
                    blockedTree[idxLevel][idxGroup].needOther = new bool[cellsInThisGroup];
409

410
                    // starpu
411 412 413 414
                    starpu_vector_data_register(&blockedTree[idxLevel][idxGroup].handleCellArrayUp, 0,
                                                (uintptr_t)blockedTree[idxLevel][idxGroup].cellArray,
                                                blockedTree[idxLevel][idxGroup].nbElements, sizeof(CellClass));
                    starpu_vector_data_register(&blockedTree[idxLevel][idxGroup].handleCellArrayDown, 0,
415 416 417
                                                (uintptr_t)blockedTree[idxLevel][idxGroup].cellArray,
                                                blockedTree[idxLevel][idxGroup].nbElements, sizeof(CellClass));

418 419 420
                    for(int idxCell = 0 ; idxCell < cellsInThisGroup ; ++idxCell, ++copyIndex){
                        blockedTree[idxLevel][idxGroup].cellArray[idxCell].setMortonIndex( iterArray[copyIndex].getCurrentGlobalIndex() );
                        blockedTree[idxLevel][idxGroup].cellArray[idxCell].setCoordinate( iterArray[copyIndex].getCurrentGlobalCoordinate() );
421
                        blockedTree[idxLevel][idxGroup].needOther[idxCell] = false;
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
                        blockedTree[idxLevel][idxGroup].cellArray[idxCell].intialCopy( iterArray[copyIndex].getCurrentCell() );
                    }

                    blockedTree[idxLevel][idxGroup].beginIndex = blockedTree[idxLevel][idxGroup].cellArray[0].getMortonIndex();
                    blockedTree[idxLevel][idxGroup].endIndex = blockedTree[idxLevel][idxGroup].cellArray[cellsInThisGroup-1].getMortonIndex();
                }
            }
            // leaf level will have the same groups has head cell level
            const int NbGroups = blockedPerLevel[OctreeHeight-1];
            blockedPerLevel[OctreeHeight] = NbGroups;
            blockedTree[OctreeHeight] = new Group[NbGroups];
            int copyIndex = 0;
            for( int idxGroup = 0 ; idxGroup < NbGroups ; ++idxGroup ){
                blockedTree[OctreeHeight][idxGroup].nbElements = blockedTree[OctreeHeight-1][idxGroup].nbElements;
                blockedTree[OctreeHeight][idxGroup].beginIndex = blockedTree[OctreeHeight-1][idxGroup].beginIndex;
                blockedTree[OctreeHeight][idxGroup].endIndex = blockedTree[OctreeHeight-1][idxGroup].endIndex;

                const int NbLeaves = blockedTree[OctreeHeight][idxGroup].nbElements;
                blockedTree[OctreeHeight][idxGroup].leavesArray = new MortonContainer[NbLeaves];
441 442 443 444 445 446 447 448 449

                // starpu
                starpu_vector_data_register(&blockedTree[OctreeHeight][idxGroup].handleLeafArray, 0,
                                            (uintptr_t)blockedTree[OctreeHeight][idxGroup].leavesArray,
                                            NbLeaves, sizeof(MortonContainer));
                starpu_vector_data_register(&blockedTree[OctreeHeight][idxGroup].handleLeafArrayRead, 0,
                                            (uintptr_t)blockedTree[OctreeHeight][idxGroup].leavesArray,
                                            NbLeaves, sizeof(MortonContainer));

450 451 452 453 454 455 456 457 458
                for(int idxLeaf = 0 ; idxLeaf < NbLeaves ; ++idxLeaf, ++copyIndex){
                    blockedTree[OctreeHeight][idxGroup].leavesArray[idxLeaf].container = *iterArray[copyIndex].getCurrentListSrc();
                    blockedTree[OctreeHeight][idxGroup].leavesArray[idxLeaf].setMortonIndex(iterArray[copyIndex].getCurrentGlobalIndex());
                    blockedTree[OctreeHeight][idxGroup].leavesArray[idxLeaf].setCoordinate(iterArray[copyIndex].getCurrentGlobalCoordinate());
                }
            }
        }
        delete[] iterArray;
        iterArray = 0;
459
        FDEBUG( FDebug::Controller << "\tPrepare child parent relations\n"; );
460 461 462 463
        // All block has been created, now find the parent-child relations
        {
            for(int idxLevel = 1; idxLevel < OctreeHeight - 1; ++idxLevel){
                int currentLowerGroup = 0;
464
                FDEBUG( FReal totalDependencies = 0 );
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
                // find the number of groups
                const int NbGroups = blockedPerLevel[idxLevel];
                for( int idxGroup = 0 ; idxGroup < NbGroups ; ++idxGroup ){
                    // copy current group
                    blockedTree[idxLevel][idxGroup].lowerGroups.push( &blockedTree[idxLevel+1][currentLowerGroup] );
                    {
                        int startIndex = 0;
                        while( (blockedTree[idxLevel+1][currentLowerGroup].cellArray[startIndex].getMortonIndex()>>3) != blockedTree[idxLevel][idxGroup].beginIndex){
                            ++startIndex;
                        }
                        blockedTree[idxLevel][idxGroup].indexOfStartInLowerGroups = startIndex;
                    }
                    if((blockedTree[idxLevel+1][currentLowerGroup].endIndex>>3) <= blockedTree[idxLevel][idxGroup].endIndex){
                        ++currentLowerGroup;
                    }
                    // copy until too much on the right
                    while(currentLowerGroup < blockedPerLevel[idxLevel+1] && (blockedTree[idxLevel+1][currentLowerGroup].endIndex>>3) <= blockedTree[idxLevel][idxGroup].endIndex) {
                        blockedTree[idxLevel][idxGroup].lowerGroups.push( &blockedTree[idxLevel+1][currentLowerGroup] );
                        ++currentLowerGroup;
                    }
                    // do we have to move backward for the next group
                    if(currentLowerGroup < blockedPerLevel[idxLevel+1] &&
                            (blockedTree[idxLevel+1][currentLowerGroup].beginIndex>>3) <= blockedTree[idxLevel][idxGroup].endIndex){
                        blockedTree[idxLevel][idxGroup].lowerGroups.push( &blockedTree[idxLevel+1][currentLowerGroup] );
                    }
490
                    FDEBUG( totalDependencies += blockedTree[idxLevel][idxGroup].lowerGroups.getSize()/FReal(NbGroups) );
491
                }
492
                FDEBUG( FDebug::Controller << "\t\tAt level " << idxLevel << " average parent-child dependencies " << totalDependencies << "\n"; );
493 494
            }
        }
495
        FDEBUG( FDebug::Controller << "\tPrepare M2L\n"; );
496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564
        // We have the parent-child relation, prepare the Multipole copy
        {
            MortonIndex potentialInteraction[189];
            int potentialPosition[189];
            for(int idxLevel = 1; idxLevel < OctreeHeight; ++idxLevel){
                // for all groups
                const int NbGroups = blockedPerLevel[idxLevel];

                for( int idxGroup = 0 ; idxGroup < NbGroups ; ++idxGroup ){
                    const int NbCells = blockedTree[idxLevel][idxGroup].nbElements;
                    // for all cells
                    for( int idxCell = 0 ; idxCell < NbCells ; ++idxCell ){
                        // get interaction
                        const int nbInteraction = getInteractionsFromPosition( blockedTree[idxLevel][idxGroup].cellArray[idxCell].getCoordinate(), idxLevel, potentialInteraction, potentialPosition);
                        // test each interaction
                        for( int idxInteraction = 0 ; idxInteraction < nbInteraction ; ++idxInteraction ){
                            // is there an interaction out of the block?
                            if(potentialInteraction[idxInteraction] < blockedTree[idxLevel][idxGroup].beginIndex
                                    || blockedTree[idxLevel][idxGroup].endIndex < potentialInteraction[idxInteraction]){
                                // find the destinatary
                                const int UNFOUND_GROUP = -1;
                                int receiver = idxGroup;
                                if(potentialInteraction[idxInteraction] < blockedTree[idxLevel][idxGroup].beginIndex){
                                    do {
                                        --receiver;
                                    } while(receiver != -1 && potentialInteraction[idxInteraction] < blockedTree[idxLevel][receiver].beginIndex);
                                    // Does someone hold this index?
                                    if( receiver == -1 || blockedTree[idxLevel][receiver].endIndex < potentialInteraction[idxInteraction]){
                                        receiver = UNFOUND_GROUP;
                                    }
                                }
                                else{
                                    do {
                                        ++receiver;
                                    } while(receiver != NbGroups && blockedTree[idxLevel][receiver].endIndex < potentialInteraction[idxInteraction]);
                                    // Does someone hold this index?
                                    if( receiver == NbGroups || potentialInteraction[idxInteraction] < blockedTree[idxLevel][receiver].beginIndex){
                                        receiver = UNFOUND_GROUP;
                                    }
                                }
                                // we found the group
                                if( receiver != UNFOUND_GROUP){
                                    // does he really need it? TODO move to binary search
                                    const int idxInReceiver = findNeigh(blockedTree[idxLevel][receiver].cellArray,
                                                                        blockedTree[idxLevel][receiver].nbElements,
                                                                        potentialInteraction[idxInteraction]);

                                    const bool targetExistInOtherGroup = (idxInReceiver != -1);
                                    if( targetExistInOtherGroup ){
                                        // then prepare for sending data
                                        // find the right transferBuffer if exists
                                        int transferBufferToSend = 0;
                                        while( transferBufferToSend != blockedTree[idxLevel][idxGroup].dataToSend.getSize()
                                               && receiver != blockedTree[idxLevel][idxGroup].dataToSend[transferBufferToSend]->groupDestination ){
                                            ++transferBufferToSend;
                                        }
                                        // if not data are needed from now
                                        if( transferBufferToSend == blockedTree[idxLevel][idxGroup].dataToSend.getSize()){
                                            blockedTree[idxLevel][idxGroup].dataToSend.push( new TransferBuffer );
                                            blockedTree[idxLevel][idxGroup].dataToSend[transferBufferToSend]->groupDestination = receiver;
                                            blockedTree[idxLevel][idxGroup].dataToSend[transferBufferToSend]->originalIndexPosition.push(idxCell);
                                        }
                                        else if(blockedTree[idxLevel][idxGroup].dataToSend[transferBufferToSend]->originalIndexPosition.getSize() &&
                                                blockedTree[idxLevel][idxGroup].dataToSend[transferBufferToSend]->originalIndexPosition.head() != idxCell) {
                                            blockedTree[idxLevel][idxGroup].dataToSend[transferBufferToSend]->originalIndexPosition.push(idxCell);
                                        }
                                        // add to the sending transferBuffer
                                        blockedTree[idxLevel][idxGroup].dataToSend[transferBufferToSend]->compuationProperties.push(TransferProperties(idxInReceiver,342-potentialPosition[idxInteraction],
                                                                                                                  blockedTree[idxLevel][idxGroup].dataToSend[transferBufferToSend]->originalIndexPosition.getSize()-1));
565 566
                                        // need other data
                                        blockedTree[idxLevel][idxGroup].needOther[idxCell] = true;
567 568 569 570 571
                                    }
                                }
                            }
                        }
                    }
572 573 574 575 576 577 578 579 580 581 582 583 584 585
                }
                // pre-compte buffer to copy data
                for( int idxGroup = 0 ; idxGroup < NbGroups ; ++idxGroup ){
                    // Copy transferBuffer to send
                    const int nbRemoteInteractions = blockedTree[idxLevel][idxGroup].dataToSend.getSize();
                    for(int idxRemote = 0 ; idxRemote < nbRemoteInteractions ; ++idxRemote){
                        const int cellToCopy = blockedTree[idxLevel][idxGroup].dataToSend[idxRemote]->originalIndexPosition.getSize();
                        const int receiver = blockedTree[idxLevel][idxGroup].dataToSend[idxRemote]->groupDestination;
                        blockedTree[idxLevel][idxGroup].dataToSend[idxRemote]->indexToStarCopying = blockedTree[idxLevel][receiver].nbCellToReceive;
                        // increase index
                        blockedTree[idxLevel][receiver].nbCellToReceive += cellToCopy;
                    }
                }
                // allocate
586
                FDEBUG( FReal totalNeeded = 0);
587
                for( int idxGroup = 0 ; idxGroup < NbGroups ; ++idxGroup ){
588
                    FDEBUG( totalNeeded += blockedTree[idxLevel][idxGroup].nbCellToReceive/FReal(NbGroups); );
589
                    blockedTree[idxLevel][idxGroup].transferBufferCell = new CellClass[blockedTree[idxLevel][idxGroup].nbCellToReceive];
590 591 592 593
                    // starpu
                    starpu_vector_data_register(&blockedTree[idxLevel][idxGroup].handleTransferCell, 0,
                                                (uintptr_t)blockedTree[idxLevel][idxGroup].transferBufferCell,
                                                blockedTree[idxLevel][idxGroup].nbCellToReceive, sizeof(CellClass));
594
                }
595
                FDEBUG( FDebug::Controller << "\t\tAverage cells needed by each group at level " << idxLevel << " = " << totalNeeded << "\n"; );
596 597
                // init with right index
                for( int idxGroup = 0 ; idxGroup < NbGroups ; ++idxGroup ){
598 599 600 601
                    // Copy transferBuffer to send
                    const int nbRemoteInteractions = blockedTree[idxLevel][idxGroup].dataToSend.getSize();
                    for(int idxRemote = 0 ; idxRemote < nbRemoteInteractions ; ++idxRemote){
                        const int cellToCopy = blockedTree[idxLevel][idxGroup].dataToSend[idxRemote]->originalIndexPosition.getSize();
602 603
                        const int receiver = blockedTree[idxLevel][idxGroup].dataToSend[idxRemote]->groupDestination;
                        const int offset = blockedTree[idxLevel][idxGroup].dataToSend[idxRemote]->indexToStarCopying;
604 605
                        for(int idxCell = 0 ; idxCell < cellToCopy ; ++idxCell){
                            const int cellPosition = blockedTree[idxLevel][idxGroup].dataToSend[idxRemote]->originalIndexPosition[idxCell];
606 607
                            blockedTree[idxLevel][receiver].transferBufferCell[idxCell + offset].setMortonIndex( blockedTree[idxLevel][idxGroup].cellArray[cellPosition].getMortonIndex() );
                            blockedTree[idxLevel][receiver].transferBufferCell[idxCell + offset].setCoordinate( blockedTree[idxLevel][idxGroup].cellArray[cellPosition].getCoordinate() );
608 609 610 611 612
                        }
                    }
                }
            }
        }
613
        FDEBUG( FDebug::Controller << "\tPrepare P2P\n"; );
614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686
        // Prepare sending receiving for the P2P
        {
            MortonIndex potentialInteraction[26];
            int potentialPosition[26];

            const int NbGroups = blockedPerLevel[OctreeHeight];

            for( int idxGroup = 0 ; idxGroup < NbGroups ; ++idxGroup ){
                const int NbCells = blockedTree[OctreeHeight][idxGroup].nbElements;
                // for all cells
                for( int idxCell = 0 ; idxCell < NbCells ; ++idxCell ){
                    // get interaction
                    const int nbInteraction = getNeighborsFromPosition( blockedTree[OctreeHeight][idxGroup].leavesArray[idxCell].getCoordinate(), OctreeHeight, potentialInteraction, potentialPosition);
                    // test each interaction
                    for( int idxInteraction = 0 ; idxInteraction < nbInteraction ; ++idxInteraction ){
                        // is there an interaction out of the block?
                        if(potentialInteraction[idxInteraction] < blockedTree[OctreeHeight][idxGroup].beginIndex
                                || blockedTree[OctreeHeight][idxGroup].endIndex < potentialInteraction[idxInteraction]){
                            // find the destinatary
                            const int UNFOUND_GROUP = -1;
                            int receiver = idxGroup;
                            if(potentialInteraction[idxInteraction] < blockedTree[OctreeHeight][idxGroup].beginIndex){
                                do {
                                    --receiver;
                                } while(receiver != -1 && potentialInteraction[idxInteraction] < blockedTree[OctreeHeight][receiver].beginIndex);
                                // Does someone hold this index?
                                if( receiver == -1 || blockedTree[OctreeHeight][receiver].endIndex < potentialInteraction[idxInteraction]){
                                    receiver = UNFOUND_GROUP;
                                }
                            }
                            else{
                                do {
                                    ++receiver;
                                } while(receiver != NbGroups && blockedTree[OctreeHeight][receiver].endIndex < potentialInteraction[idxInteraction]);
                                // Does someone hold this index?
                                if( receiver == NbGroups || potentialInteraction[idxInteraction] < blockedTree[OctreeHeight][receiver].beginIndex){
                                    receiver = UNFOUND_GROUP;
                                }
                            }
                            // we found the group
                            if( receiver != UNFOUND_GROUP ){
                                // does he really need it? TODO move to binary search
                                const int idxInReceiver = findNeigh( blockedTree[OctreeHeight][receiver].leavesArray,
                                                               blockedTree[OctreeHeight][receiver].nbElements,
                                                               potentialInteraction[idxInteraction]);

                                const bool targetExistInOtherGroup = (idxInReceiver != -1);
                                if( targetExistInOtherGroup ){
                                    // then prepare for sending data
                                    // find the right transferBuffer if exists
                                    int transferBufferToSend = 0;
                                    while( transferBufferToSend != blockedTree[OctreeHeight][idxGroup].dataToSend.getSize()
                                           && receiver != blockedTree[OctreeHeight][idxGroup].dataToSend[transferBufferToSend]->groupDestination ){
                                        ++transferBufferToSend;
                                    }
                                    // if not data are needed from now
                                    if( transferBufferToSend == blockedTree[OctreeHeight][idxGroup].dataToSend.getSize()){
                                        blockedTree[OctreeHeight][idxGroup].dataToSend.push( new TransferBuffer );
                                        blockedTree[OctreeHeight][idxGroup].dataToSend[transferBufferToSend]->groupDestination = receiver;
                                        blockedTree[OctreeHeight][idxGroup].dataToSend[transferBufferToSend]->originalIndexPosition.push(idxCell);
                                    }
                                    else if( blockedTree[OctreeHeight][idxGroup].dataToSend[transferBufferToSend]->originalIndexPosition.getSize() &&
                                             blockedTree[OctreeHeight][idxGroup].dataToSend[transferBufferToSend]->originalIndexPosition.head() != idxCell ){
                                        blockedTree[OctreeHeight][idxGroup].dataToSend[transferBufferToSend]->originalIndexPosition.push(idxCell);
                                    }
                                    // add to the sending transferBuffer
                                    blockedTree[OctreeHeight][idxGroup].dataToSend[transferBufferToSend]->compuationProperties.push(TransferProperties(idxInReceiver,26-potentialPosition[idxInteraction],
                                                                                                                                                       blockedTree[OctreeHeight][idxGroup].dataToSend[transferBufferToSend]->originalIndexPosition.getSize()-1));
                                }
                            }
                        }
                    }
                }
687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704
            }
            // pre-compte buffer to copy data
            for( int idxGroup = 0 ; idxGroup < NbGroups ; ++idxGroup ){
                // Copy transferBuffer to send
                const int nbRemoteInteractions = blockedTree[OctreeHeight][idxGroup].dataToSend.getSize();
                for(int idxRemote = 0 ; idxRemote < nbRemoteInteractions ; ++idxRemote){
                    const int leafToCopy = blockedTree[OctreeHeight][idxGroup].dataToSend[idxRemote]->originalIndexPosition.getSize();
                    const int receiver = blockedTree[OctreeHeight][idxGroup].dataToSend[idxRemote]->groupDestination;
                    blockedTree[OctreeHeight][idxGroup].dataToSend[idxRemote]->indexToStarCopying = blockedTree[OctreeHeight][receiver].nbLeafToReceive;
                    // increase index
                    blockedTree[OctreeHeight][receiver].nbLeafToReceive += leafToCopy;
                }
            }
            // allocate needed memory
            FDEBUG( FReal totalNeeded = 0 );
            for( int idxGroup = 0 ; idxGroup < NbGroups ; ++idxGroup ){
                FDEBUG( totalNeeded += blockedTree[OctreeHeight][idxGroup].nbLeafToReceive/FReal(NbGroups); );
                blockedTree[OctreeHeight][idxGroup].transferBufferLeaf = new MortonContainer[blockedTree[OctreeHeight][idxGroup].nbLeafToReceive];
705 706 707 708
                // starpu
                starpu_vector_data_register(&blockedTree[OctreeHeight][idxGroup].handleTransferLeaf, 0,
                                            (uintptr_t)blockedTree[OctreeHeight][idxGroup].transferBufferLeaf,
                                            blockedTree[OctreeHeight][idxGroup].nbLeafToReceive, sizeof(MortonContainer));
709 710 711 712
            }
            FDEBUG( FDebug::Controller << "\t\tAverage leaves needed by each group = " << totalNeeded << "\n"; );
            // copy data
            for( int idxGroup = 0 ; idxGroup < NbGroups ; ++idxGroup ){
713 714 715 716
                // Copy transferBuffer to send
                const int nbRemoteInteractions = blockedTree[OctreeHeight][idxGroup].dataToSend.getSize();
                for(int idxRemote = 0 ; idxRemote < nbRemoteInteractions ; ++idxRemote){
                    const int leafToCopy = blockedTree[OctreeHeight][idxGroup].dataToSend[idxRemote]->originalIndexPosition.getSize();
717 718
                    const int receiver = blockedTree[OctreeHeight][idxGroup].dataToSend[idxRemote]->groupDestination;
                    const int offset = blockedTree[OctreeHeight][idxGroup].dataToSend[idxRemote]->indexToStarCopying;
719 720
                    for(int idxLeaf = 0 ; idxLeaf < leafToCopy ; ++idxLeaf){
                        const int leafPosition = blockedTree[OctreeHeight][idxGroup].dataToSend[idxRemote]->originalIndexPosition[idxLeaf];
721 722 723
                        blockedTree[OctreeHeight][receiver].transferBufferLeaf[idxLeaf + offset].setMortonIndex( blockedTree[OctreeHeight][idxGroup].leavesArray[leafPosition].getMortonIndex() );
                        blockedTree[OctreeHeight][receiver].transferBufferLeaf[idxLeaf + offset].setCoordinate( blockedTree[OctreeHeight][idxGroup].leavesArray[leafPosition].getCoordinate() );
                        blockedTree[OctreeHeight][receiver].transferBufferLeaf[idxLeaf + offset].container = blockedTree[OctreeHeight][idxGroup].leavesArray[leafPosition].container;
724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767
                    }
                }
            }
        }
    }

    void releaseGroups(){
        // copy from group to octree
        {
            typename OctreeClass::Iterator octreeIterator(tree);
            typename OctreeClass::Iterator avoidGotLeftIterator(octreeIterator);
            for(int idxLevel = 1; idxLevel < OctreeHeight - 1; ++idxLevel){
                const int NbGroups = blockedPerLevel[idxLevel];
                for( int idxGroup = 0 ; idxGroup < NbGroups ; ++idxGroup ){
                    const int NbCells = blockedTree[idxLevel][idxGroup].nbElements;
                    for( int idxCell = 0 ; idxCell < NbCells ; ++idxCell ){
                        octreeIterator.getCurrentCell()->restoreCopy( &blockedTree[idxLevel][idxGroup].cellArray[idxCell] );
                        octreeIterator.moveRight();
                    }
                }

                avoidGotLeftIterator.moveDown();
                octreeIterator = avoidGotLeftIterator;
            }
            // restore leaf
            const int NbGroups = blockedPerLevel[OctreeHeight-1];
            for( int idxGroup = 0 ; idxGroup < NbGroups ; ++idxGroup ){
                const int NbCells = blockedTree[OctreeHeight-1][idxGroup].nbElements;
                for( int idxCell = 0 ; idxCell < NbCells ; ++idxCell ){
                    // restore cell
                    octreeIterator.getCurrentCell()->restoreCopy( &blockedTree[OctreeHeight-1][idxGroup].cellArray[idxCell] );
                    // restore particles
                    *octreeIterator.getCurrentListTargets() = blockedTree[OctreeHeight][idxGroup].leavesArray[idxCell].container;
                    // goto right
                    octreeIterator.moveRight();
                }
            }
        }
        // release memory
        for(int idxLevel = 0; idxLevel <= OctreeHeight; ++idxLevel){
            delete[] blockedTree[idxLevel];
        }
        memset(blockedTree, 0, sizeof(Group*) * (OctreeHeight + 1));
        memset(blockedPerLevel, 0, (OctreeHeight + 1) * sizeof(int));
768 769 770 771 772 773 774

        // release kernels
        releaseKernels();

        // shutdown starpu
        FDEBUG( FDebug::Controller << "Shutdown starpu\n" );
        starpu_shutdown();
775 776 777 778 779 780 781
    }

    /////////////////////////////////////////////////////////////
    // Execute functions
    /////////////////////////////////////////////////////////////

    void execute(){
782
        P2P_P2M();
783 784 785 786 787 788

        M2M_M2L();

        L2L();

        L2P();
789 790 791

        FDEBUG( FDebug::Controller << "Wait task to be finished...\n" );
        starpu_task_wait_for_all();
792 793 794 795 796
    }

    /////////////////////////////////////////////////////////////

    // The P2P
797
    void P2P_P2M(){
798
        FTRACE( FTrace::FFunction functionTrace(__FUNCTION__, "Fmm" , __FILE__ , __LINE__) );
799 800
        FDEBUG( FDebug::Controller.write("\tStart Bottom Pass (P2P + P2M)\n").write(FDebug::Flush); );
        FDEBUG(FTic counterTimeP2PP2M);
801
        FDEBUG(FTic counterTime);
802
        FDEBUG(FTic counterTimeCopy);
803 804 805 806

        // P2P inside leaves
        const int NbGroups = blockedPerLevel[OctreeHeight];
        for( int idxGroup = 0 ; idxGroup < NbGroups ; ++idxGroup ){
807
            starpu_insert_task( &p2m_cl, STARPU_W, blockedTree[OctreeHeight-1][idxGroup].handleCellArrayUp,
808 809 810 811 812
                                STARPU_R, blockedTree[OctreeHeight][idxGroup].handleLeafArrayRead, 0);

            starpu_insert_task( &p2p_cl, STARPU_VALUE, &OctreeHeight, sizeof(OctreeHeight),
                                STARPU_RW, blockedTree[OctreeHeight][idxGroup].handleLeafArray,
                                STARPU_RW, blockedTree[OctreeHeight][idxGroup].handleTransferLeaf, 0);
813 814
        }

815
        FDEBUG(counterTimeP2PP2M.tac());
816
        FDEBUG(counterTimeCopy.tic());
817

818
        // P2P restore
819 820 821
        for( int idxGroup = 0 ; idxGroup < NbGroups ; ++idxGroup ){
            const int nbRemoteInteraction = blockedTree[OctreeHeight][idxGroup].dataToSend.getSize();
            for( int idxRemote = 0 ; idxRemote < nbRemoteInteraction ; ++idxRemote ){
822 823 824
                const int receiver = blockedTree[OctreeHeight][idxGroup].dataToSend[idxRemote]->groupDestination;
                const int offset = blockedTree[OctreeHeight][idxGroup].dataToSend[idxRemote]->indexToStarCopying;

825 826 827 828 829 830
                FVector<int>* indexPosition = &blockedTree[OctreeHeight][idxGroup].dataToSend[idxRemote]->originalIndexPosition;
                starpu_insert_task( &p2p_restore_cl,
                                    STARPU_VALUE, &indexPosition, sizeof(indexPosition),
                                    STARPU_VALUE, &offset, sizeof(offset),
                                    STARPU_RW, blockedTree[OctreeHeight][idxGroup].handleLeafArray,
                                    STARPU_R, blockedTree[OctreeHeight][receiver].handleTransferLeaf, 0);
831 832 833
            }
        }

834 835 836
        FDEBUG( FDebug::Controller << "\tFinished (@Bottom Pass (P2M + P2P + Copy P2P) = "  << counterTime.tacAndElapsed() << "s)\n" );
        FDEBUG( FDebug::Controller << "\t\tP2P + P2M = "  << counterTimeP2PP2M.elapsed() << "s)\n" );
        FDEBUG( FDebug::Controller << "\t\tRestore P2P leaves only = "  << counterTimeCopy.tacAndElapsed() << "s)\n" );
837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854
    }


    /////////////////////////////////////////////////////////////

    void M2M_M2L(){
        FTRACE( FTrace::FFunction functionTrace(__FUNCTION__, "Fmm" , __FILE__ , __LINE__) );
        FDEBUG( FDebug::Controller.write("\tStart Upward and Transfer pass\n").write(FDebug::Flush); );
        FDEBUG(FTic counterTime);
        FDEBUG(FTic counterTimeM2M);
        FDEBUG(FTic counterTimeM2L);
        FDEBUG(FTic counterTimeM2LRemote);

        for(int idxLevel = OctreeHeight - 2 ; idxLevel > 1 ; --idxLevel){
            FDEBUG(counterTimeM2M.tic());
            {
                const int NbGroups = blockedPerLevel[idxLevel];
                for( int idxGroup = 0 ; idxGroup < NbGroups ; ++idxGroup ){
855

856 857
                    struct starpu_task* const task = starpu_task_create();
                    // buffer 0 is current leaf
858
                    task->handles[0] = blockedTree[idxLevel][idxGroup].handleCellArrayUp;
859 860 861

                    const int nbLowerGroups = blockedTree[idxLevel][idxGroup].lowerGroups.getSize();
                    for(int idxLower = 0 ; idxLower < nbLowerGroups ; ++idxLower){
862
                        task->handles[idxLower + 1] = blockedTree[idxLevel][idxGroup].lowerGroups[idxLower]->handleCellArrayUp;
863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878
                    }
                    // put the right codelet
                    task->cl = &m2m_cl[nbLowerGroups-1];
                    // put args values
                    char *arg_buffer;
                    size_t arg_buffer_size;
                    starpu_codelet_pack_args(&arg_buffer, &arg_buffer_size,
                            STARPU_VALUE, &idxLevel, sizeof(idxLevel),
                            STARPU_VALUE, &blockedTree[idxLevel][idxGroup].indexOfStartInLowerGroups, sizeof(blockedTree[idxLevel][idxGroup].indexOfStartInLowerGroups),
                            STARPU_VALUE, &nbLowerGroups, sizeof(nbLowerGroups),
                            0);
                    task->cl_arg = arg_buffer;
                    task->cl_arg_size = arg_buffer_size;

                    // submit task
                    starpu_task_submit(task);
879 880 881
                }
            }
            FDEBUG(counterTimeM2M.tac());
882

883 884 885 886 887 888
            FDEBUG(counterTimeM2L.tic());
            {
                const int lowerLevel = idxLevel + 1;
                const int NbGroups = blockedPerLevel[lowerLevel];

                for( int idxGroup = 0 ; idxGroup < NbGroups ; ++idxGroup ){
889

890 891 892 893 894 895 896 897
                    const MortonIndex begin = blockedTree[lowerLevel][idxGroup].beginIndex;
                    const MortonIndex end = blockedTree[lowerLevel][idxGroup].endIndex;
                    bool*const needOther = blockedTree[lowerLevel][idxGroup].needOther;
                    starpu_insert_task( &m2l_cl,
                                        STARPU_VALUE, &needOther, sizeof(needOther),
                                        STARPU_VALUE, &lowerLevel, sizeof(lowerLevel),
                                        STARPU_VALUE, &begin, sizeof(begin),
                                        STARPU_VALUE, &end, sizeof(end),
898 899
                                        STARPU_RW, blockedTree[lowerLevel][idxGroup].handleCellArrayDown,
                                        STARPU_R, blockedTree[lowerLevel][idxGroup].handleCellArrayUp, 0);
900 901 902 903 904


                    const int nbRemoteInteraction = blockedTree[lowerLevel][idxGroup].dataToSend.getSize();
                    for( int idxRemote = 0 ; idxRemote < nbRemoteInteraction ; ++idxRemote ){
                        // copy
905
                        const int receiver = blockedTree[lowerLevel][idxGroup].dataToSend[idxRemote]->groupDestination;
906 907
                        const int indexStart = blockedTree[lowerLevel][idxGroup].dataToSend[idxRemote]->indexToStarCopying;
                        FVector<int>* originalPosition = &blockedTree[lowerLevel][idxGroup].dataToSend[idxRemote]->originalIndexPosition;
908

909 910 911 912
                        starpu_insert_task( &m2l_copy_cl,
                                            STARPU_VALUE, &indexStart, sizeof(indexStart),
                                            STARPU_VALUE, &originalPosition, sizeof(originalPosition),
                                            STARPU_RW, blockedTree[lowerLevel][receiver].handleTransferCell,
913
                                            STARPU_R, blockedTree[lowerLevel][idxGroup].handleCellArrayUp, 0);
914 915
                    }
                }
916 917 918 919

                FDEBUG(counterTimeM2LRemote.tic());
                for( int idxGroup = 0 ; idxGroup < NbGroups ; ++idxGroup ){
                        // remote M2L
920 921 922 923 924 925 926 927
                    const MortonIndex begin = blockedTree[lowerLevel][idxGroup].beginIndex;
                    const MortonIndex end = blockedTree[lowerLevel][idxGroup].endIndex;
                    bool*const needOther = blockedTree[lowerLevel][idxGroup].needOther;
                    starpu_insert_task( &m2l_other_cl,
                                        STARPU_VALUE, &needOther, sizeof(needOther),
                                        STARPU_VALUE, &lowerLevel, sizeof(lowerLevel),
                                        STARPU_VALUE, &begin, sizeof(begin),
                                        STARPU_VALUE, &end, sizeof(end),
928
                                        STARPU_RW, blockedTree[lowerLevel][idxGroup].handleCellArrayDown,
929
                                        STARPU_R, blockedTree[lowerLevel][idxGroup].handleTransferCell, 0);
930 931
                }                
                FDEBUG(counterTimeM2LRemote.tac());
932 933 934 935 936 937 938 939 940 941
            }
            FDEBUG(counterTimeM2L.tac());
        }
        {
            FDEBUG(counterTimeM2L.tic());

            const int lowerLevel = 2;
            const int NbGroups = blockedPerLevel[lowerLevel];

            for( int idxGroup = 0 ; idxGroup < NbGroups ; ++idxGroup ){
942 943 944 945 946 947 948 949 950

                const MortonIndex begin = blockedTree[lowerLevel][idxGroup].beginIndex;
                const MortonIndex end = blockedTree[lowerLevel][idxGroup].endIndex;
                bool*const needOther = blockedTree[lowerLevel][idxGroup].needOther;
                starpu_insert_task( &m2l_cl,
                                    STARPU_VALUE, &needOther, sizeof(needOther),
                                    STARPU_VALUE, &lowerLevel, sizeof(lowerLevel),
                                    STARPU_VALUE, &begin, sizeof(begin),
                                    STARPU_VALUE, &end, sizeof(end),
951 952
                                    STARPU_RW, blockedTree[lowerLevel][idxGroup].handleCellArrayDown,
                                    STARPU_R, blockedTree[lowerLevel][idxGroup].handleCellArrayUp, 0);
953 954 955 956 957


                const int nbRemoteInteraction = blockedTree[lowerLevel][idxGroup].dataToSend.getSize();
                for( int idxRemote = 0 ; idxRemote < nbRemoteInteraction ; ++idxRemote ){
                    // copy
958
                    const int receiver = blockedTree[lowerLevel][idxGroup].dataToSend[idxRemote]->groupDestination;
959 960
                    const int indexStart = blockedTree[lowerLevel][idxGroup].dataToSend[idxRemote]->indexToStarCopying;
                    FVector<int>* originalPosition = &blockedTree[lowerLevel][idxGroup].dataToSend[idxRemote]->originalIndexPosition;
961

962 963 964 965
                    starpu_insert_task( &m2l_copy_cl,
                                        STARPU_VALUE, &indexStart, sizeof(indexStart),
                                        STARPU_VALUE, &originalPosition, sizeof(originalPosition),
                                        STARPU_RW, blockedTree[lowerLevel][receiver].handleTransferCell,
966
                                        STARPU_R, blockedTree[lowerLevel][idxGroup].handleCellArrayUp, 0);
967
                }
968

969
            }
970

971 972 973
            FDEBUG(counterTimeM2LRemote.tic());
            for( int idxGroup = 0 ; idxGroup < NbGroups ; ++idxGroup ){
                    // remote M2L
974 975 976 977 978 979 980 981
                    const MortonIndex begin = blockedTree[lowerLevel][idxGroup].beginIndex;
                    const MortonIndex end = blockedTree[lowerLevel][idxGroup].endIndex;
                    bool*const needOther = blockedTree[lowerLevel][idxGroup].needOther;
                    starpu_insert_task( &m2l_other_cl,
                                        STARPU_VALUE, &needOther, sizeof(needOther),
                                        STARPU_VALUE, &lowerLevel, sizeof(lowerLevel),
                                        STARPU_VALUE, &begin, sizeof(begin),
                                        STARPU_VALUE, &end, sizeof(end),
982
                                        STARPU_RW, blockedTree[lowerLevel][idxGroup].handleCellArrayDown,
983
                                        STARPU_R, blockedTree[lowerLevel][idxGroup].handleTransferCell, 0);
984
            }
985
            FDEBUG(counterTimeM2LRemote.tac());
986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005

            FDEBUG(counterTimeM2L.tac());
        }

        FDEBUG( FDebug::Controller << "\tFinished (@Upward and transfer (M2M + M2L) = "  << counterTime.tacAndElapsed() << "s)\n" );
        FDEBUG( FDebug::Controller << "\t\tM2M only = "  << counterTimeM2M.cumulated() << "s)\n" );
        FDEBUG( FDebug::Controller << "\t\tM2L all = "  << counterTimeM2L.cumulated() << "s)\n" );
        FDEBUG( FDebug::Controller << "\t\tM2L Remote only = "  << counterTimeM2LRemote.cumulated() << "s)\n" );
    }

    /////////////////////////////////////////////////////////////

    void L2L(){
        FTRACE( FTrace::FFunction functionTrace(__FUNCTION__, "Fmm" , __FILE__ , __LINE__) );
        FDEBUG( FDebug::Controller.write("\tStart Downard pass\n").write(FDebug::Flush); );
        FDEBUG(FTic counterTime);

        for(int idxLevel = 2 ; idxLevel < OctreeHeight - 1 ; ++idxLevel){
            const int NbGroups = blockedPerLevel[idxLevel];
            for( int idxGroup = 0 ; idxGroup < NbGroups ; ++idxGroup ){
1006 1007
                struct starpu_task* const task = starpu_task_create();
                // buffer 0 is current leaf
1008
                task->handles[0] = blockedTree[idxLevel][idxGroup].handleCellArrayDown;
1009 1010 1011

                const int nbLowerGroups = blockedTree[idxLevel][idxGroup].lowerGroups.getSize();
                for(int idxLower = 0 ; idxLower < nbLowerGroups ; ++idxLower){
1012
                    task->handles[idxLower + 1] = blockedTree[idxLevel][idxGroup].lowerGroups[idxLower]->handleCellArrayDown;
1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030
                }
                // put the right codelet
                task->cl = &l2l_cl[nbLowerGroups-1];

                // put args values
                char *arg_buffer;
                size_t arg_buffer_size;
                const int indexOfStart = blockedTree[idxLevel][idxGroup].indexOfStartInLowerGroups;
                starpu_codelet_pack_args(&arg_buffer, &arg_buffer_size,
                        STARPU_VALUE, &idxLevel, sizeof(idxLevel),
                        STARPU_VALUE, &indexOfStart, sizeof(indexOfStart),
                        STARPU_VALUE, &nbLowerGroups, sizeof(nbLowerGroups),
                        0);
                task->cl_arg = arg_buffer;
                task->cl_arg_size = arg_buffer_size;

                // submit task
                starpu_task_submit(task);
1031
            }
1032

1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046
        }

        FDEBUG( FDebug::Controller << "\tFinished (@Downard (L2L) = "  << counterTime.tacAndElapsed() << "s)\n" );
    }

    /////////////////////////////////////////////////////////////

    void L2P(){
        FTRACE( FTrace::FFunction functionTrace(__FUNCTION__, "Fmm" , __FILE__ , __LINE__) );
        FDEBUG( FDebug::Controller.write("\tL2P\n").write(FDebug::Flush); );
        FDEBUG(FTic counterTime);

        const int NbGroups = blockedPerLevel[OctreeHeight-1];
        for( int idxGroup = 0 ; idxGroup < NbGroups ; ++idxGroup ){
1047
            starpu_insert_task( &l2p_cl, STARPU_R, blockedTree[OctreeHeight-1][idxGroup].handleCellArrayDown,
1048
                                STARPU_RW, blockedTree[OctreeHeight][idxGroup].handleLeafArray, 0);
1049 1050 1051 1052 1053 1054 1055 1056 1057 1058
        }

        FDEBUG( FDebug::Controller << "\tFinished (@L2P (L2P) = "  << counterTime.tacAndElapsed() << "s)\n" );
    }

    /////////////////////////////////////////////////////////////
    // Utils functions
    /////////////////////////////////////////////////////////////

    template <class Object>
1059
    static int findNeigh(const Object leafs[], const int size, const MortonIndex indexToFound) {
1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081

        int idxLeft = 0;
        int idxRight = size - 1;

        while( idxLeft <= idxRight ){
            const int idxMiddle = (idxRight + idxLeft) / 2;
            const MortonIndex mortonMiddle = leafs[idxMiddle].getMortonIndex();

            if( mortonMiddle == indexToFound){
                return idxMiddle;
            }

            if( indexToFound < mortonMiddle ){
                idxRight = idxMiddle - 1;
            }
            else{
                idxLeft = idxMiddle + 1;
            }
        }
        return -1;
    }

1082
    static int getInteractionsFromPosition(const FTreeCoordinate& workingCell,const int inLevel, MortonIndex inNeighbors[189], int inNeighborsPosition[189]) {
1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127

        // Then take each child of the parent's neighbors if not in directNeighbors
        // Father coordinate
        const FTreeCoordinate parentCell(workingCell.getX()>>1,workingCell.getY()>>1,workingCell.getZ()>>1);

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

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

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

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

                    // if we are not on the current cell
                    if( idxX || idxY || idxZ ){
                        const FTreeCoordinate otherParent(parentCell.getX() + idxX,parentCell.getY() + idxY,parentCell.getZ() + idxZ);
                        const MortonIndex mortonOther = otherParent.getMortonIndex(inLevel-1);

                        // For each child
                        for(int idxCousin = 0 ; idxCousin < 8 ; ++idxCousin){
                            const int xdiff  = ((otherParent.getX()<<1) | ( (idxCousin>>2) & 1)) - workingCell.getX();
                            const int ydiff  = ((otherParent.getY()<<1) | ( (idxCousin>>1) & 1)) - workingCell.getY();
                            const int zdiff  = ((otherParent.getZ()<<1) | (idxCousin&1)) - workingCell.getZ();

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

        return idxNeighbors;
    }

1128
    static int getNeighborsFromPosition(const FTreeCoordinate& center, const int inLevel, MortonIndex indexes[26], int indexInArray[26]) {
1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144

        const int limite = FMath::pow2(inLevel-1);
        int idxNeig = 0;
        // We test all cells around
        for(int idxX = -1 ; idxX <= 1 ; ++idxX){
            if(!FMath::Between(center.getX() + idxX,0, limite)) continue;

            for(int idxY = -1 ; idxY <= 1 ; ++idxY){
                if(!FMath::Between(center.getY() + idxY,0, limite)) continue;

                for(int idxZ = -1 ; idxZ <= 1 ; ++idxZ){
                    if(!FMath::Between(center.getZ() + idxZ,0, limite)) continue;

                    // if we are not on the current cell
                    if( idxX || idxY || idxZ ){
                        const FTreeCoordinate other(center.getX() + idxX,center.getY() + idxY,center.getZ() + idxZ);
1145
                        indexes[ idxNeig ] = other.getMortonIndex(inLevel-1);
1146 1147 1148 1149 1150 1151 1152 1153
                        indexInArray[ idxNeig ] = ((idxX+1)*3 + (idxY+1)) * 3 + (idxZ+1);
                        ++idxNeig;
                    }
                }
            }
        }
        return idxNeig;
    }
1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453

    /////////////////////////////////////////////////////////////////////////////
    // Callback
    /////////////////////////////////////////////////////////////////////////////
    // The current solution uses a global array of kernel
    static KernelClass** globalKernels;

    // P2M
    static void p2m_cpu(void *descr[], void *){
        CellClass*const multipole = (CellClass*)STARPU_VECTOR_GET_PTR(descr[0]);
        const MortonContainer*const particles = (const MortonContainer*)STARPU_VECTOR_GET_PTR(descr[1]);
        const int size = STARPU_VECTOR_GET_NX(descr[0]);

        for( int idxLeaf = 0 ; idxLeaf < size ; ++idxLeaf ){
            globalKernels[starpu_worker_get_id()]->P2M( &multipole[idxLeaf], &particles[idxLeaf].container);
        }
    }

    // M2M
    static void m2m_cpu(void *descr[], void *cl_arg){
        int level;
        int indexOfStart;
        int nbLowerGroup;
        starpu_codelet_unpack_args(cl_arg, &level, &indexOfStart, &nbLowerGroup);

        CellClass*const multipole = (CellClass*)STARPU_VECTOR_GET_PTR(descr[0]);
        const int size = STARPU_VECTOR_GET_NX(descr[0]);

        const CellClass* child_multipole = (CellClass*)STARPU_VECTOR_GET_PTR(descr[1]);
        int child_size = STARPU_VECTOR_GET_NX(descr[1]);

        int childIndex = indexOfStart;
        int childGroup = 0;

        const CellClass* children[8];
        for( int idxCell = 0 ; idxCell < size ; ++idxCell ){
            const MortonIndex currentIndex = multipole[idxCell].getMortonIndex();
            memset(children , 0, 8 * sizeof(CellClass*));

            while( currentIndex == (child_multipole[childIndex].getMortonIndex() >> 3) ){
                children[child_multipole[childIndex].getMortonIndex() & 7] = &child_multipole[childIndex];
                ++childIndex;
                if(childIndex == child_size){
                    childIndex = 0;
                    ++childGroup;
                    if( childGroup == nbLowerGroup ){
                        break;
                    }
                    child_multipole = (const CellClass*)STARPU_VECTOR_GET_PTR(descr[childGroup+1]);
                    child_size = STARPU_VECTOR_GET_NX(descr[childGroup+1]);
                }
            }
            globalKernels[starpu_worker_get_id()]->M2M(&multipole[idxCell], children, level);
        }
    }

    // M2L
    static void m2l_cpu(void *descr[], void *cl_arg)
    {
        bool* needOther;
        int level;
        MortonIndex begin;
        MortonIndex end;
        starpu_codelet_unpack_args(cl_arg, &needOther, &level, &begin, &end);

        CellClass*const multipole_local = (CellClass*)STARPU_VECTOR_GET_PTR(descr[0]);
        const int size = STARPU_VECTOR_GET_NX(descr[0]);


        const CellClass* neighbors[343];
        memset(neighbors, 0, sizeof(CellClass*) * 343);

        MortonIndex potentialInteraction[189];
        int potentialPosition[189];

        for( int idxCell = 0 ; idxCell < size ; ++idxCell ){
            if( !needOther[idxCell] ){
                const int nbInteraction = getInteractionsFromPosition( multipole_local[idxCell].getCoordinate(), level, potentialInteraction, potentialPosition);
                int counter = 0;

                for(int idxInteraction = 0 ; idxInteraction < nbInteraction ; ++idxInteraction){
                    if( begin <= potentialInteraction[idxInteraction] && potentialInteraction[idxInteraction] <= end){
                        int neighPosition = -1;
                        int offset = 0;
                        if( potentialInteraction[idxInteraction] < multipole_local[idxCell].getMortonIndex()){
                            neighPosition = findNeigh(multipole_local, idxCell, potentialInteraction[idxInteraction]);
                        }
                        else {
                            neighPosition = findNeigh(multipole_local + idxCell + 1, size - idxCell - 1, potentialInteraction[idxInteraction]);
                            offset = idxCell + 1;
                        }
                        if( neighPosition != -1 ){
                            neighbors[ potentialPosition[idxInteraction] ] = &multipole_local[neighPosition + offset];
                            ++counter;
                        }
                    }
                }

                if(counter){
                    globalKernels[starpu_worker_get_id()]->M2L( &multipole_local[idxCell] , neighbors, counter, level);
                    memset(neighbors, 0, sizeof(CellClass*) * 343);
                }
            }
        }
    }

    // M2L other
    static void m2l_other_cpu(void *descr[], void *cl_arg)
    {

        bool* needOther;
        int level;
        MortonIndex begin;
        MortonIndex end;
        starpu_codelet_unpack_args(cl_arg, &needOther, &level, &begin, &end);

        CellClass*const multipole_local = (CellClass*)STARPU_VECTOR_GET_PTR(descr[0]);
        const int size = STARPU_VECTOR_GET_NX(descr[0]);

        const CellClass*const transferBuffer = (CellClass*)STARPU_VECTOR_GET_PTR(descr[1]);
        const int sizeBuffer = STARPU_VECTOR_GET_NX(descr[1]);

        const CellClass* neighbors[343];
        memset(neighbors, 0, sizeof(CellClass*) * 343);

        MortonIndex potentialInteraction[189];
        int potentialPosition[189];

        for( int idxCell = 0 ; idxCell < size ; ++idxCell ){
            if( needOther[idxCell] ){
                const int nbInteraction = getInteractionsFromPosition( multipole_local[idxCell].getCoordinate(), level, potentialInteraction, potentialPosition);
                int counter = 0;

                for(int idxInteraction = 0 ; idxInteraction < nbInteraction ; ++idxInteraction){
                    if( begin <= potentialInteraction[idxInteraction] && potentialInteraction[idxInteraction] <= end){
                        int neighPosition = -1;
                        int offset = 0;
                        if( potentialInteraction[idxInteraction] < multipole_local[idxCell].getMortonIndex()){
                            neighPosition = findNeigh(multipole_local, idxCell, potentialInteraction[idxInteraction]);
                        }
                        else {
                            neighPosition = findNeigh(multipole_local + idxCell + 1, size - idxCell - 1, potentialInteraction[idxInteraction]);
                            offset = idxCell + 1;
                        }
                        if( neighPosition != -1 ){
                            neighbors[ potentialPosition[idxInteraction] ] = &multipole_local[neighPosition + offset];
                            ++counter;
                        }
                    }
                    else {
                        const int neighPosition = findNeigh(transferBuffer, sizeBuffer, potentialInteraction[idxInteraction]);
                        if( neighPosition != -1 ){
                            neighbors[ potentialPosition[idxInteraction] ] = &transferBuffer[neighPosition];
                            ++counter;
                        }
                    }
                }

                if(counter){
                    globalKernels[starpu_worker_get_id()]->M2L( &multipole_local[idxCell] , neighbors, counter, level);
                    memset(neighbors, 0, sizeof(CellClass*) * 343);
                }
            }
        }
    }

    // M2L copy
    static void m2l_copy_cpu(void *descr[], void *cl_arg)
    {
        int indexOfStart;
        FVector<int>* originalIndexPosition;
        starpu_codelet_unpack_args(cl_arg, &indexOfStart, &originalIndexPosition);

        CellClass*const transferBuffer = (CellClass*)STARPU_VECTOR_GET_PTR(descr[0]);
        const CellClass*const multipole_local = ((const CellClass*)STARPU_VECTOR_GET_PTR(descr[1]));

        for(int idxLeaf = 0 ; idxLeaf < (*originalIndexPosition).getSize() ; ++idxLeaf){
            const