MAJ terminée. Nous sommes passés en version 14.6.2 . Pour consulter les "releases notes" associées c'est ici :

https://about.gitlab.com/releases/2022/01/11/security-release-gitlab-14-6-2-released/
https://about.gitlab.com/releases/2022/01/04/gitlab-14-6-1-released/

FAdaptiveTask.hpp 52.1 KB
Newer Older
Quentin Khan's avatar
Quentin Khan committed
1
2
3
4
5
6
7
8
#ifndef SCALFMM_ADAPTIVE_TASK_ALGO_HPP_
#define SCALFMM_ADAPTIVE_TASK_ALGO_HPP_

#include <algorithm>
#include <numeric>
#include <cmath> // Used to round box differences
#include <functional>
#include <map>
9
#include <vector>
Quentin Khan's avatar
Quentin Khan committed
10
11
12
13
14
15
16
17
18
19
20
21
#include <list>
#include <array>
#include <type_traits>
#include <memory>
#include <sstream>

#include <omp.h>

#include <unistd.h>

#include "Core/FCoreCommon.hpp"
#include "Containers/FTreeCoordinate.hpp"
22
#include "Utils/FAlgorithmTimers.hpp"
Quentin Khan's avatar
Quentin Khan committed
23

24
25
26
#include "Kernels/FKernelConcepts.hpp"

template<class _Tree, class _Kernel,
27
28
29
30
31
32
33
34
35
         typename std::enable_if<scalfmm::sfinae::has_P2M<_Tree, _Kernel>::value>::type* = nullptr,
         typename std::enable_if<scalfmm::sfinae::has_M2M<_Tree, _Kernel>::value>::type* = nullptr,
         typename std::enable_if<scalfmm::sfinae::has_M2L<_Tree, _Kernel>::value>::type* = nullptr,
         typename std::enable_if<scalfmm::sfinae::has_L2L<_Tree, _Kernel>::value>::type* = nullptr,
         typename std::enable_if<scalfmm::sfinae::has_L2P<_Tree, _Kernel>::value>::type* = nullptr,
         typename std::enable_if<scalfmm::sfinae::has_partial_P2P<_Tree, _Kernel>::value>::type* = nullptr,
         typename std::enable_if<scalfmm::sfinae::has_M2P<_Tree, _Kernel>::value>::type* = nullptr,
         typename std::enable_if<scalfmm::sfinae::has_P2L<_Tree, _Kernel>::value>::type* = nullptr
         >
36
class FAdaptiveTask : public FAlgorithmInterface, public FAlgorithmTimers {
Quentin Khan's avatar
Quentin Khan committed
37
38
39
40
41
42
43
44
public:
    using tree_t = _Tree;
    using kernel_t = _Kernel;
    using FReal = typename tree_t::FReal;

    using node_t = typename tree_t::node_t;

protected:
45
    /// Tree
Quentin Khan's avatar
Quentin Khan committed
46
    tree_t&   _tree;
47
    /// Vector of kernels, one per thread
Quentin Khan's avatar
Quentin Khan committed
48
49
    std::vector<std::unique_ptr<kernel_t>> _kernels;

50
51

    /** \brief SFINAE shortcut to check `K.setup(Args...)` existence */
Quentin Khan's avatar
Quentin Khan committed
52
    template<typename K, typename Ret, typename... Args>
53
    using has_setup_member = typename std::enable_if<
Quentin Khan's avatar
Quentin Khan committed
54
55
56
        std::is_same<Ret, decltype(std::declval<K>().setup(std::declval<Args>()...))>::value
        >::type*;

57
58
59
60
    /** \brief Calls K.setup(tree_t) if it exists, otherwise deleted by SFINAE */
    template<typename K, has_setup_member<K, void, tree_t> = nullptr>
    void setup_kernel(K* kernel) {
        kernel->setup(_tree);
61
62
    }

63
64
65
66
    /** \brief Calls K.setup() if it exists, otherwise deleted by SFINAE */
    template<typename K, has_setup_member<K, void> = nullptr>
    void setup_kernel(K* kernel) {
        kernel->setup();
Quentin Khan's avatar
Quentin Khan committed
67
68
    }

69
    /** \brief Fallback if both other overloads are deleted by SFINAE */
Quentin Khan's avatar
Quentin Khan committed
70
71
    void setup_kernel(...) {}

72
73
74
75
76
77
78
79
    /**
     * \brief Data pool for mock dependency
     *
     * Generates addresses that can be used as OpenMP task dependencies. To do
     * so, a list of char arrays (buckets) is created. Each char is a potential
     * dependency. When an array is exhausted, an new one is appended to the
     * list.
     */
Quentin Khan's avatar
Quentin Khan committed
80
    struct mock_dependency {
81
82
83
        /// Size of a bucket
        enum : std::size_t {bucket_size = 512};
        /// List of buckets. A bucket is implented as an array
84
        std::list<std::array<char, bucket_size>> pool;
85
        /// Index of the next dependency to hand out in the current bucket
Quentin Khan's avatar
Quentin Khan committed
86
87
        std::size_t mock_dependency_index = bucket_size;

88
        /// Yield a new dependency
89
        char* next() {
90
91
92
93
94
            // When the bucket is empty, add a new one
            if(bucket_size == mock_dependency_index) {
                pool.emplace_back();
                mock_dependency_index = 0;
            }
95
            return (char*) (&(this->pool.back()[mock_dependency_index++]));
Quentin Khan's avatar
Quentin Khan committed
96
97
98
        }
    } mock_dep;

99
100
101
102
103
104
105
106
107
108
    /**
     * \brief Dependency type names structure
     *
     * Each member of the enum identifies a dependency type name.
     */
    enum class dep_t {
        M,   ///< Multipole
        L,   ///< Local expansion
        P_s, ///< Source particle
        P_t, ///< Target particle
109
110
    };

111

112
113
    /**
     * \brief Get pointer to node ressource handling given dependency type
114
115
116
117
118
119
120
121
122
123
124
125
126
127
     *
     * OpenMP task dependencies do not need to be correlated with real data,
     * they only need to point to the same area in memory. This method maps a
     * dependency type to an address inside the given node object, cast to
     * `const char`.
     *
     * \tparam I Dependency type
     *
     * \param node Node to which the dependency applies
     *
     * \return A pointer to memory inside the node.
     */
    template<dep_t I>
    const char* get_dependency(node_t* node) const {
128
        if(I == dep_t::M) {
129
            return (const char*) node->getData();
130
        } else if(I == dep_t::L) {
131
132
            using type = typename std::decay<decltype((node->getDepth()))>::type;
            return (const char*) const_cast<type*>(&(node->getDepth()));
133
        } else if(I == dep_t::P_s) {
134
            return (const char*) node;
135
        } else if(I == dep_t::P_t) {
136
            return (const char*) node->getParticleContainer();
137
        }
138
        return nullptr;
139
140
141
    }


Quentin Khan's avatar
Quentin Khan committed
142
143
public:

Quentin Khan's avatar
Quentin Khan committed
144
145
146
147
148
149
    /**
     * \brief Constructor
     *
     * \param tree The tree to run over
     * \param kernel The kernel to use
     */
Quentin Khan's avatar
Quentin Khan committed
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
    FAdaptiveTask(tree_t& tree, kernel_t& kernel) :
        _tree(tree)
    {
        // Place kernel objects near their threads in memory to avoid NUMA
        // latency
        #pragma omp parallel
        {
            for(int i = 0; i < omp_get_num_threads(); ++i) {
                if(i == omp_get_thread_num()) {
                    this->_kernels.emplace_back(new kernel_t(kernel));
                }
                #pragma omp barrier
            }
        }
    }

166
167
    std::string name() const override {
        return "Task adaptive algorithm";
Quentin Khan's avatar
Quentin Khan committed
168
169
    }

170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
    std::string description() const override {
        int threads = 1;
        #pragma omp parallel shared(threads)
        {
            #pragma omp single nowait
            {
                threads = omp_get_max_threads();
            }
        }
        return std::string("threads: ") + std::to_string(threads);
    }

    using FAlgorithmInterface::execute;

    /**
     * \brief Run specific steps of the algorithm
     *
     * \param operations Specifies the algorithm operations to run, see
     * FFmmOperations.
     */
    void execute(const unsigned int operations) override {
Quentin Khan's avatar
Quentin Khan committed
191
192
193
        this->run(operations);
    }

194
    void run(int operations) {
Quentin Khan's avatar
Quentin Khan committed
195
196
197

        #pragma omp parallel
        {
198
            this->setup_kernel(this->_kernels[omp_get_thread_num()].get());
199
200
            #pragma omp barrier

Quentin Khan's avatar
Quentin Khan committed
201
202
203
204
            #pragma omp single nowait
            {
                if(operations & FFmmP2M) {
                    // 1. source to up, P2M
205
206
207
208
                    Timers["P2M"].tic();
                    this->source_to_up();;
                    Timers["P2M"].tac();
                    std::cout << "    P2M: " << Timers["P2M"].elapsed() << std::endl;
209
210
211
212
                }

                if(operations & FFmmP2L) {
                    // 3b X-list, P2L
213
214
215
216
                    Timers["P2L"].tic();
                    this->x_list_step();
                    Timers["P2L"].tac();
                    std::cout << "    P2L: " << Timers["P2L"].elapsed() << std::endl;
Quentin Khan's avatar
Quentin Khan committed
217
218
                }

219
220
                if(operations & FFmmP2P) {
                    // A. U-list, P2P
221
222
223
224
                    Timers["P2P"].tic();
                    this->u_list_step();
                    Timers["P2P"].tac();
                    std::cout << "    P2P: " << Timers["P2P"].elapsed() << std::endl;
225
226
                }

Quentin Khan's avatar
Quentin Khan committed
227
228
                if(operations & FFmmM2M) {
                    // 2. up to up, M2M
229
230
231
232
                    Timers["M2M"].tic();
                    this->up_to_up();
                    Timers["M2M"].tac();
                    std::cout << "    M2M: " << Timers["M2M"].elapsed() << std::endl;
Quentin Khan's avatar
Quentin Khan committed
233
234
                }

235
236
                if(operations & FFmmM2P) {
                    // 5a W-list, M2P
237
238
239
240
                    Timers["M2P"].tic();
                    this->w_list_step();
                    Timers["M2P"].tac();
                    std::cout << "    M2P: " << Timers["M2P"].elapsed() << std::endl;
241
242
                }

243
244
                if(operations & FFmmM2L) {
                    // 3a V-list, M2L
245
246
247
248
                    Timers["M2L"].tic();
                    this->v_list_step();
                    Timers["M2L"].tac();
                    std::cout << "    M2L: " << Timers["M2L"].elapsed() << std::endl;
Quentin Khan's avatar
Quentin Khan committed
249
250
251
252
                }

                if(operations & FFmmL2L) {
                    // 4. down to down, L2L
253
254
255
256
                    Timers["L2L"].tic();
                    this->down_to_down();
                    Timers["L2L"].tac();
                    std::cout << "    L2L: " << Timers["L2L"].elapsed() << std::endl;
Quentin Khan's avatar
Quentin Khan committed
257
258
259
                }

                if(operations & FFmmL2P) {
260
                    // 5b down to target, L2P
261
262
263
264
                    Timers["L2P"].tic();
                    this->down_to_target();
                    Timers["L2P"].tac();
                    std::cout << "    L2P: " << Timers["L2P"].elapsed() << std::endl;
Quentin Khan's avatar
Quentin Khan committed
265
266
                }

267
268


269
                std::cout << "    tasks creation: "
270
271
272
                          << std::accumulate(Timers.begin(), Timers.end(), 0.,
                                             [](double & res, const typename decltype(Timers)::value_type & t)
                                             {return res + t.second.cumulated();})
Quentin Khan's avatar
Quentin Khan committed
273
274
275
276
277
                          << '\n';
            }
        }
    }

278
    /** \brief Walk through leaves and queue P2M tasks */
Quentin Khan's avatar
Quentin Khan committed
279
280
    void source_to_up() {
        for(node_t* leaf : _tree.leaves()) {
281
282
283
284
285
286
287
288
289
290
291
            this->create_P2M_task(leaf);
        }
        // #pragma omp taskwait
    }

    /**
     * \brief Create and queue a P2M task
     *
     * \param leaf The P2M leaf
     */
    void create_P2M_task(node_t* leaf) {
292
293
            const char* ps_dep = get_dependency<dep_t::P_s>(leaf);(void)ps_dep;
            const char* m_dep = get_dependency<dep_t::M>(leaf);(void)m_dep;
294
295
296
297

            #pragma omp task firstprivate(leaf)                        \
                depend(in: ps_dep[:1])                                 \
                depend(inout: m_dep[:1])
Quentin Khan's avatar
Quentin Khan committed
298
299
            {
                const int thread_num = omp_get_thread_num();
300
                _kernels[thread_num]->P2M(leaf->getData(), leaf->getParticleContainer());
Quentin Khan's avatar
Quentin Khan committed
301
            }
302
303
304
305
306
307
308
309
310
    }


    /** \brief Walk through tree and queue M2M tasks */
    void up_to_up() {
        for(node_t& n : _tree.post_order_walk()) {
            if(! n.is_leaf()) {
                create_M2M_task(&n);
            }
Quentin Khan's avatar
Quentin Khan committed
311
        }
312
        // #pragma omp taskwait
Quentin Khan's avatar
Quentin Khan committed
313
314
    }

315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
    /**
     * \brief Create and queue an M2M task
     *
     * \param node An internal node
     *
     * \warning node is assumed not to be a leaf.
     */
    void create_M2M_task(node_t* node) {
        assert(! node->is_leaf());
        // Setup task dependencies
        // children data
        const char* children_dep[node_t::child_count] = {};
        for(node_t* child : node->getChildren()) {
            children_dep[child->getIndex() & (node_t::child_count-1)]
                = get_dependency<dep_t::M>(child);
        }
        // node data
        const char* parent_dep = get_dependency<dep_t::M>(node); (void) parent_dep;

        #pragma omp task                        \
            depend(in:                          \
                   children_dep[0][:1],         \
                   children_dep[1][:1],         \
                   children_dep[2][:1],         \
                   children_dep[3][:1],         \
                   children_dep[4][:1],         \
                   children_dep[5][:1],         \
                   children_dep[6][:1],         \
                   children_dep[7][:1])         \
            depend(out: parent_dep[:1])
        {
            const int thread_num = omp_get_thread_num();
Quentin Khan's avatar
Quentin Khan committed
347

348
349
350
351
352
353
            // Fill array of children data
            std::array<typename node_t::data_t*, node_t::child_count> child_data;
            for(node_t* child : node->getChildren()) {
                // Last bits of child index give the index relative to parent
                child_data[child->getIndex() & (node_t::child_count-1)] = child->getData();
            }
Quentin Khan's avatar
Quentin Khan committed
354

355
356
357
358
359
360
361
362
363
364
365
366
367
            // Call kernel module
            this->_kernels[thread_num]->M2M(node->getData(), child_data.data(),
                                            static_cast<int>(node->getDepth()));
        }
    }



    /** \brief Walk through tree and queue M2L tasks */
    void v_list_step() {
        for(node_t& n : _tree.in_order_walk()) {
            if(n.is_leaf() && n.getParticleContainer()->size() == 0) {
                continue;
Quentin Khan's avatar
Quentin Khan committed
368
            }
369
            create_M2L_task(&n);
Quentin Khan's avatar
Quentin Khan committed
370
371
372
373
374
        }
        // #pragma omp taskwait
    }


375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
    /**
     * \brief Create and queue an M2L task
     *
     * \param node A tree node
     */
    void create_M2L_task(node_t* node) {

        // Generate task dependencies
        // There cannot be more than 7^Dim cells involved in a M2L, in 3D, this is 343
        const char* task_deps[343];
        const char* data_dep = get_dependency<dep_t::L>(node);(void) data_dep;
        std::size_t idx_dep = 0;
        // Add existing dependencies
        for(node_t* v_item : node->V) {
            if(v_item->is_leaf()
               && v_item->getParticleContainer()->size() == 0) {
Quentin Khan's avatar
Quentin Khan committed
391
392
                continue;
            }
393
394
395
396
397
398
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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
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
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
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
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
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
            task_deps[idx_dep] = get_dependency<dep_t::M>(v_item);
            ++idx_dep;
        }
        // Add mock dependencies, these are generated on the fly and used
        // only once, that way they can never stop a task from starting
        while(idx_dep < 343) {
            task_deps[idx_dep] = this->mock_dep.next();
            ++idx_dep;
        }

        #pragma omp task                                                \
            depend(in:                                                  \
                   task_deps[0][:1],                                    \
                   task_deps[1][:1],                                    \
                   task_deps[2][:1],                                    \
                   task_deps[3][:1],                                    \
                   task_deps[4][:1],                                    \
                   task_deps[5][:1],                                    \
                   task_deps[6][:1],                                    \
                   task_deps[7][:1],                                    \
                   task_deps[8][:1],                                    \
                   task_deps[9][:1],                                    \
                   task_deps[10][:1],                                   \
                   task_deps[11][:1],                                   \
                   task_deps[12][:1],                                   \
                   task_deps[13][:1],                                   \
                   task_deps[14][:1],                                   \
                   task_deps[15][:1],                                   \
                   task_deps[16][:1],                                   \
                   task_deps[17][:1],                                   \
                   task_deps[18][:1],                                   \
                   task_deps[19][:1],                                   \
                   task_deps[20][:1],                                   \
                   task_deps[21][:1],                                   \
                   task_deps[22][:1],                                   \
                   task_deps[23][:1],                                   \
                   task_deps[24][:1],                                   \
                   task_deps[25][:1],                                   \
                   task_deps[26][:1],                                   \
                   task_deps[27][:1],                                   \
                   task_deps[28][:1],                                   \
                   task_deps[29][:1],                                   \
                   task_deps[30][:1],                                   \
                   task_deps[31][:1],                                   \
                   task_deps[32][:1],                                   \
                   task_deps[33][:1],                                   \
                   task_deps[34][:1],                                   \
                   task_deps[35][:1],                                   \
                   task_deps[36][:1],                                   \
                   task_deps[37][:1],                                   \
                   task_deps[38][:1],                                   \
                   task_deps[39][:1],                                   \
                   task_deps[40][:1],                                   \
                   task_deps[41][:1],                                   \
                   task_deps[42][:1],                                   \
                   task_deps[43][:1],                                   \
                   task_deps[44][:1],                                   \
                   task_deps[45][:1],                                   \
                   task_deps[46][:1],                                   \
                   task_deps[47][:1],                                   \
                   task_deps[48][:1],                                   \
                   task_deps[49][:1],                                   \
                   task_deps[50][:1],                                   \
                   task_deps[51][:1],                                   \
                   task_deps[52][:1],                                   \
                   task_deps[53][:1],                                   \
                   task_deps[54][:1],                                   \
                   task_deps[55][:1],                                   \
                   task_deps[56][:1],                                   \
                   task_deps[57][:1],                                   \
                   task_deps[58][:1],                                   \
                   task_deps[59][:1],                                   \
                   task_deps[60][:1],                                   \
                   task_deps[61][:1],                                   \
                   task_deps[62][:1],                                   \
                   task_deps[63][:1],                                   \
                   task_deps[64][:1],                                   \
                   task_deps[65][:1],                                   \
                   task_deps[66][:1],                                   \
                   task_deps[67][:1],                                   \
                   task_deps[68][:1],                                   \
                   task_deps[69][:1],                                   \
                   task_deps[70][:1],                                   \
                   task_deps[71][:1],                                   \
                   task_deps[72][:1],                                   \
                   task_deps[73][:1],                                   \
                   task_deps[74][:1],                                   \
                   task_deps[75][:1],                                   \
                   task_deps[76][:1],                                   \
                   task_deps[77][:1],                                   \
                   task_deps[78][:1],                                   \
                   task_deps[79][:1],                                   \
                   task_deps[80][:1],                                   \
                   task_deps[81][:1],                                   \
                   task_deps[82][:1],                                   \
                   task_deps[83][:1],                                   \
                   task_deps[84][:1],                                   \
                   task_deps[85][:1],                                   \
                   task_deps[86][:1],                                   \
                   task_deps[87][:1],                                   \
                   task_deps[88][:1],                                   \
                   task_deps[89][:1],                                   \
                   task_deps[90][:1],                                   \
                   task_deps[91][:1],                                   \
                   task_deps[92][:1],                                   \
                   task_deps[93][:1],                                   \
                   task_deps[94][:1],                                   \
                   task_deps[95][:1],                                   \
                   task_deps[96][:1],                                   \
                   task_deps[97][:1],                                   \
                   task_deps[98][:1],                                   \
                   task_deps[99][:1],                                   \
                   task_deps[100][:1],                                  \
                   task_deps[101][:1],                                  \
                   task_deps[102][:1],                                  \
                   task_deps[103][:1],                                  \
                   task_deps[104][:1],                                  \
                   task_deps[105][:1],                                  \
                   task_deps[106][:1],                                  \
                   task_deps[107][:1],                                  \
                   task_deps[108][:1],                                  \
                   task_deps[109][:1],                                  \
                   task_deps[110][:1],                                  \
                   task_deps[111][:1],                                  \
                   task_deps[112][:1],                                  \
                   task_deps[113][:1],                                  \
                   task_deps[114][:1],                                  \
                   task_deps[115][:1],                                  \
                   task_deps[116][:1],                                  \
                   task_deps[117][:1],                                  \
                   task_deps[118][:1],                                  \
                   task_deps[119][:1],                                  \
                   task_deps[120][:1],                                  \
                   task_deps[121][:1],                                  \
                   task_deps[122][:1],                                  \
                   task_deps[123][:1],                                  \
                   task_deps[124][:1],                                  \
                   task_deps[125][:1],                                  \
                   task_deps[126][:1],                                  \
                   task_deps[127][:1],                                  \
                   task_deps[128][:1],                                  \
                   task_deps[129][:1],                                  \
                   task_deps[130][:1],                                  \
                   task_deps[131][:1],                                  \
                   task_deps[132][:1],                                  \
                   task_deps[133][:1],                                  \
                   task_deps[134][:1],                                  \
                   task_deps[135][:1],                                  \
                   task_deps[136][:1],                                  \
                   task_deps[137][:1],                                  \
                   task_deps[138][:1],                                  \
                   task_deps[139][:1],                                  \
                   task_deps[140][:1],                                  \
                   task_deps[141][:1],                                  \
                   task_deps[142][:1],                                  \
                   task_deps[143][:1],                                  \
                   task_deps[144][:1],                                  \
                   task_deps[145][:1],                                  \
                   task_deps[146][:1],                                  \
                   task_deps[147][:1],                                  \
                   task_deps[148][:1],                                  \
                   task_deps[149][:1],                                  \
                   task_deps[150][:1],                                  \
                   task_deps[151][:1],                                  \
                   task_deps[152][:1],                                  \
                   task_deps[153][:1],                                  \
                   task_deps[154][:1],                                  \
                   task_deps[155][:1],                                  \
                   task_deps[156][:1],                                  \
                   task_deps[157][:1],                                  \
                   task_deps[158][:1],                                  \
                   task_deps[159][:1],                                  \
                   task_deps[160][:1],                                  \
                   task_deps[161][:1],                                  \
                   task_deps[162][:1],                                  \
                   task_deps[163][:1],                                  \
                   task_deps[164][:1],                                  \
                   task_deps[165][:1],                                  \
                   task_deps[166][:1],                                  \
                   task_deps[167][:1],                                  \
                   task_deps[168][:1],                                  \
                   task_deps[169][:1],                                  \
                   task_deps[170][:1],                                  \
                   task_deps[171][:1],                                  \
                   task_deps[172][:1],                                  \
                   task_deps[173][:1],                                  \
                   task_deps[174][:1],                                  \
                   task_deps[175][:1],                                  \
                   task_deps[176][:1],                                  \
                   task_deps[177][:1],                                  \
                   task_deps[178][:1],                                  \
                   task_deps[179][:1],                                  \
                   task_deps[180][:1],                                  \
                   task_deps[181][:1],                                  \
                   task_deps[182][:1],                                  \
                   task_deps[183][:1],                                  \
                   task_deps[184][:1],                                  \
                   task_deps[185][:1],                                  \
                   task_deps[186][:1],                                  \
                   task_deps[187][:1],                                  \
                   task_deps[188][:1],                                  \
                   task_deps[189][:1],                                  \
                   task_deps[190][:1],                                  \
                   task_deps[191][:1],                                  \
                   task_deps[192][:1],                                  \
                   task_deps[193][:1],                                  \
                   task_deps[194][:1],                                  \
                   task_deps[195][:1],                                  \
                   task_deps[196][:1],                                  \
                   task_deps[197][:1],                                  \
                   task_deps[198][:1],                                  \
                   task_deps[199][:1],                                  \
                   task_deps[200][:1],                                  \
                   task_deps[201][:1],                                  \
                   task_deps[202][:1],                                  \
                   task_deps[203][:1],                                  \
                   task_deps[204][:1],                                  \
                   task_deps[205][:1],                                  \
                   task_deps[206][:1],                                  \
                   task_deps[207][:1],                                  \
                   task_deps[208][:1],                                  \
                   task_deps[209][:1],                                  \
                   task_deps[210][:1],                                  \
                   task_deps[211][:1],                                  \
                   task_deps[212][:1],                                  \
                   task_deps[213][:1],                                  \
                   task_deps[214][:1],                                  \
                   task_deps[215][:1],                                  \
                   task_deps[216][:1],                                  \
                   task_deps[217][:1],                                  \
                   task_deps[218][:1],                                  \
                   task_deps[219][:1],                                  \
                   task_deps[220][:1],                                  \
                   task_deps[221][:1],                                  \
                   task_deps[222][:1],                                  \
                   task_deps[223][:1],                                  \
                   task_deps[224][:1],                                  \
                   task_deps[225][:1],                                  \
                   task_deps[226][:1],                                  \
                   task_deps[227][:1],                                  \
                   task_deps[228][:1],                                  \
                   task_deps[229][:1],                                  \
                   task_deps[230][:1],                                  \
                   task_deps[231][:1],                                  \
                   task_deps[232][:1],                                  \
                   task_deps[233][:1],                                  \
                   task_deps[234][:1],                                  \
                   task_deps[235][:1],                                  \
                   task_deps[236][:1],                                  \
                   task_deps[237][:1],                                  \
                   task_deps[238][:1],                                  \
                   task_deps[239][:1],                                  \
                   task_deps[240][:1],                                  \
                   task_deps[241][:1],                                  \
                   task_deps[242][:1],                                  \
                   task_deps[243][:1],                                  \
                   task_deps[244][:1],                                  \
                   task_deps[245][:1],                                  \
                   task_deps[246][:1],                                  \
                   task_deps[247][:1],                                  \
                   task_deps[248][:1],                                  \
                   task_deps[249][:1],                                  \
                   task_deps[250][:1],                                  \
                   task_deps[251][:1],                                  \
                   task_deps[252][:1],                                  \
                   task_deps[253][:1],                                  \
                   task_deps[254][:1],                                  \
                   task_deps[255][:1],                                  \
                   task_deps[256][:1],                                  \
                   task_deps[257][:1],                                  \
                   task_deps[258][:1],                                  \
                   task_deps[259][:1],                                  \
                   task_deps[260][:1],                                  \
                   task_deps[261][:1],                                  \
                   task_deps[262][:1],                                  \
                   task_deps[263][:1],                                  \
                   task_deps[264][:1],                                  \
                   task_deps[265][:1],                                  \
                   task_deps[266][:1],                                  \
                   task_deps[267][:1],                                  \
                   task_deps[268][:1],                                  \
                   task_deps[269][:1],                                  \
                   task_deps[270][:1],                                  \
                   task_deps[271][:1],                                  \
                   task_deps[272][:1],                                  \
                   task_deps[273][:1],                                  \
                   task_deps[274][:1],                                  \
                   task_deps[275][:1],                                  \
                   task_deps[276][:1],                                  \
                   task_deps[277][:1],                                  \
                   task_deps[278][:1],                                  \
                   task_deps[279][:1],                                  \
                   task_deps[280][:1],                                  \
                   task_deps[281][:1],                                  \
                   task_deps[282][:1],                                  \
                   task_deps[283][:1],                                  \
                   task_deps[284][:1],                                  \
                   task_deps[285][:1],                                  \
                   task_deps[286][:1],                                  \
                   task_deps[287][:1],                                  \
                   task_deps[288][:1],                                  \
                   task_deps[289][:1],                                  \
                   task_deps[290][:1],                                  \
                   task_deps[291][:1],                                  \
                   task_deps[292][:1],                                  \
                   task_deps[293][:1],                                  \
                   task_deps[294][:1],                                  \
                   task_deps[295][:1],                                  \
                   task_deps[296][:1],                                  \
                   task_deps[297][:1],                                  \
                   task_deps[298][:1],                                  \
                   task_deps[299][:1],                                  \
                   task_deps[300][:1],                                  \
                   task_deps[301][:1],                                  \
                   task_deps[302][:1],                                  \
                   task_deps[303][:1],                                  \
                   task_deps[304][:1],                                  \
                   task_deps[305][:1],                                  \
                   task_deps[306][:1],                                  \
                   task_deps[307][:1],                                  \
                   task_deps[308][:1],                                  \
                   task_deps[309][:1],                                  \
                   task_deps[310][:1],                                  \
                   task_deps[311][:1],                                  \
                   task_deps[312][:1],                                  \
                   task_deps[313][:1],                                  \
                   task_deps[314][:1],                                  \
                   task_deps[315][:1],                                  \
                   task_deps[316][:1],                                  \
                   task_deps[317][:1],                                  \
                   task_deps[318][:1],                                  \
                   task_deps[319][:1],                                  \
                   task_deps[320][:1],                                  \
                   task_deps[321][:1],                                  \
                   task_deps[322][:1],                                  \
                   task_deps[323][:1],                                  \
                   task_deps[324][:1],                                  \
                   task_deps[325][:1],                                  \
                   task_deps[326][:1],                                  \
                   task_deps[327][:1],                                  \
                   task_deps[328][:1],                                  \
                   task_deps[329][:1],                                  \
                   task_deps[330][:1],                                  \
                   task_deps[331][:1],                                  \
                   task_deps[332][:1],                                  \
                   task_deps[333][:1],                                  \
                   task_deps[334][:1],                                  \
                   task_deps[335][:1],                                  \
                   task_deps[336][:1],                                  \
                   task_deps[337][:1],                                  \
                   task_deps[338][:1],                                  \
                   task_deps[339][:1],                                  \
                   task_deps[340][:1],                                  \
                   task_deps[341][:1],                                  \
                   task_deps[342][:1]                                   \
                ) depend(inout: data_dep[:1]) firstprivate(node)
        {
Quentin Khan's avatar
Quentin Khan committed
750

751
            const int thread_num = omp_get_thread_num();
Quentin Khan's avatar
Quentin Khan committed
752

753
754
755
            std::vector<decltype(std::declval<const node_t>().getData())> v_item_data_list;
            std::vector<int> v_item_indices;
            // Needed to compute offset between boxes
Quentin Khan's avatar
Quentin Khan committed
756
757
758
759
760
            for(node_t* v_item : node->V) {
                if(v_item->is_leaf()
                   && v_item->getParticleContainer()->size() == 0) {
                    continue;
                }
761
762
                v_item_indices.push_back(compute_box_offset_index(node, v_item, 3));
                v_item_data_list.push_back(v_item->getData());
Quentin Khan's avatar
Quentin Khan committed
763
764
            }

765
766
            // Call kernel M2L operator
            this->_kernels[thread_num]->M2L(node->getData(), v_item_data_list.data(), v_item_indices.data(), static_cast<int>(v_item_data_list.size()), static_cast<int>(node->getDepth()));
Quentin Khan's avatar
Quentin Khan committed
767
768
769
        }
    }

770
    /** \brief Walk through leaves and queue P2L tasks */
Quentin Khan's avatar
Quentin Khan committed
771
772
773
774
775
776
777
778
    void x_list_step() {
        /* NOTE: the X list and W list are complementary: if A is in X(B) then B
         * is in W(A).
         *
         * We loop over the leaves first to detect the empty ones early on.
         */
        for(node_t* leaf : _tree.leaves()) {
            if(leaf->getParticleContainer()->size() > 0) {
779
780
781
782
783
                this->create_P2L_task(leaf);
            }
        }
        // #pragma omp taskwait
    }
784
785


786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
    /**
     * \brief Create and queue a P2L task
     *
     * \param leaf A tree leaf
     */
    void create_P2L_task(node_t* leaf) {
        for(node_t* w_item : leaf->W) {
            if(w_item->is_leaf() && w_item->getParticleContainer()->size() == 0) {
                continue;
            }

            const char* w_dep = get_dependency<dep_t::L>(w_item);(void)w_dep;
            const char* ps_dep = get_dependency<dep_t::P_s>(leaf);(void)ps_dep;

            #pragma omp task firstprivate(leaf, w_item) depend(in: ps_dep[:1]) depend(inout: w_dep[:1])
            {
                const int thread_num = omp_get_thread_num();
                this->_kernels[thread_num]->P2L(w_item->getData(), leaf->getParticleContainer());
Quentin Khan's avatar
Quentin Khan committed
804
805
806
807
            }
        }
    }

808
    /** Walk through tree and queue L2L tasks */
Quentin Khan's avatar
Quentin Khan committed
809
810
    void down_to_down() {
        for(node_t& n : _tree.pre_order_walk()) {
811
812
            if(! n.is_leaf()) {
                this->create_L2L_task(&n);
Quentin Khan's avatar
Quentin Khan committed
813
814
815
816
817
            }
        }
        // #pragma omp taskwait
    }

818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860

    /**
     * \brief Create and queue an L2L task
     *
     * \param node An internal tree node
     *
     * \node node is assumed not to be a leaf
     */
    void create_L2L_task(node_t* node) {
        assert(! node->is_leaf());

        const char* parent_dep = get_dependency<dep_t::L>(node);(void)parent_dep;
        const char* children_dep[node_t::child_count];
        for(std::size_t i = 0; i < node_t::child_count; ++i) {
            children_dep[i] = get_dependency<dep_t::L>(node->getChild(i));
        }

        #pragma omp task                        \
            depend(in: parent_dep[:1])          \
            depend(inout:                       \
                   children_dep[0][:1],         \
                   children_dep[1][:1],         \
                   children_dep[2][:1],         \
                   children_dep[3][:1],         \
                   children_dep[4][:1],         \
                   children_dep[5][:1],         \
                   children_dep[6][:1],         \
                   children_dep[7][:1]          \
                )
        {
            const int thread_num = omp_get_thread_num();

            typename node_t::data_t* child_data[node_t::child_count];
            for(std::size_t i = 0; i < node_t::child_count; ++i) {
                child_data[i] = node->getChild(i)->getData();
            }
            this->_kernels[thread_num]->L2L(node->getData(), child_data, static_cast<int>(node->getDepth()));
        }
    }



    /** \brief Walk through the leaves an queue M2P tasks */
Quentin Khan's avatar
Quentin Khan committed
861
862
863
    void w_list_step() {
        for(node_t* leaf : _tree.leaves()) {
            if(leaf->getParticleContainer()->size() > 0) {
864
865
866
867
868
                create_M2P_task(leaf);
            }
        }
        // #pragma omp taskwait
    }
869

870
871
872
873
874
875
876
877
878
879
    /**
     * \brief Create and queue an M2P task
     *
     * \param leaf A leaf
     */
    void create_M2P_task(node_t* leaf) {
        for(node_t* w_item : leaf->W) {
            if(w_item->is_leaf() && w_item->getParticleContainer()->size() == 0) {
                continue;
            }
880

881
882
883
884
885
886
887
            const char* w_dep = get_dependency<dep_t::M>(w_item); (void)w_dep;
            const char* pt_dep = get_dependency<dep_t::P_t>(leaf); (void)pt_dep;

            #pragma omp task depend(inout: pt_dep[:1]) depend(in: w_dep[:1])
            {
                const int thread_num = omp_get_thread_num();
                this->_kernels[thread_num]->M2P(w_item->getData(), leaf->getParticleContainer());
Quentin Khan's avatar
Quentin Khan committed
888
889
890
891
            }
        }
    }

892
893

    /** \brief Walk through the leaves and queue L2P tasks */
Quentin Khan's avatar
Quentin Khan committed
894
895
    void down_to_target() {
        for(node_t* leaf : _tree.leaves()) {
896
            if(leaf->getParticleContainer()->size() != 0) {
897
                this->create_L2P_task(leaf);
Quentin Khan's avatar
Quentin Khan committed
898
899
900
901
902
            }
        }
        // #pragma omp taskwait
    }

903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
    /**
     * \brief Create and queue an L2P task
     *
     * \param leaf A leaf
     */
    void create_L2P_task(node_t* leaf) {
        const char* data_dep = get_dependency<dep_t::L>(leaf); (void)data_dep;
        const char* pt_dep = get_dependency<dep_t::P_t>(leaf); (void)pt_dep;
        #pragma omp task depend(inout: pt_dep[:1]) depend(in: data_dep[:1])
        {
            const int thread_num = omp_get_thread_num();
            this->_kernels[thread_num]->L2P(leaf->getData(), leaf->getParticleContainer());
        }
    }


    /** \brief Walk through the leaves and queue P2P tasks */
    void u_list_step() {
        for(node_t* leaf : _tree.leaves()) {
            this->create_P2P_task(leaf);
        }
        // #pragma omp taskwait
    }

Quentin Khan's avatar
Quentin Khan committed
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953

    /** \brief Direct computation step (P2P)
     *
     * For each tree leaf, a direct computation is done with the adjacent
     * leaves.
     *
     * The symmetric computation kernels expect to receive the list of adjacent
     * leaves, stored in a leaf's U list, sorted by offset index.
     *
     * The offset index is of a node compared to another follows the
     * numerotation of the adjacent nodes, on each axis. In 3D, this woud give,
     * with L the reference leaf:
     *
     *          x          x            x
     *        0 1 2  |   9 10 11  |  18 19 20
     *     y  3 4 5  |  12  L 14  |  21 22 23
     *        6 7 8  |  15 16 17  |  24 25 26
     *         z=0        z=1          z=2
     *
     * When two node that are to be compared are not on the same level, the
     * lowest one's ancestor that is at the highest one's level is used to
     * compute the index.
     *
     * To statisfy this condition, the leaves have to be sorted before being
     * given to the kernel P2P method.
     *
     */
954
    void create_P2P_task(node_t* leaf) {
Quentin Khan's avatar
Quentin Khan committed
955
956
        using container_t = typename node_t::particle_container_t;

957
958
959
960
        container_t* const leaf_source_particle_container =
            leaf->getParticleContainer();
        container_t* const leaf_target_particle_container =
            leaf->getParticleContainer();
Quentin Khan's avatar
Quentin Khan committed
961

962
963
964
965
966
        // Skip empty leaves
        if( leaf_source_particle_container->size() == 0
            && leaf_target_particle_container->size() == 0) {
            return;
        }
Quentin Khan's avatar
Quentin Khan committed
967

968
969
        auto it = leaf->U.begin();
        bool do_inner = true;
Quentin Khan's avatar
Quentin Khan committed
970

971
972
973
974
        while(it != leaf->U.end()) {
            constexpr const std::size_t max_task_size = 27;
            std::size_t i = 0;
            auto first = it;
Quentin Khan's avatar
Quentin Khan committed
975

976
977
978
            const char* task_deps[max_task_size];
            const char* leaf_s_dep = get_dependency<dep_t::P_s>(leaf);(void)leaf_s_dep;
            const char* leaf_t_dep = get_dependency<dep_t::P_t>(leaf);(void)leaf_t_dep;
Quentin Khan's avatar
Quentin Khan committed
979

980
981
982
983
984
            while((it != leaf->U.end()) && (i < max_task_size)) {
                if((*it) == leaf || (*it)->getParticleContainer()->size() == 0) {
                    ++it;
                    continue;
                }
Quentin Khan's avatar
Quentin Khan committed
985

986
987
988
989
                task_deps[i] = get_dependency<dep_t::P_t>(*it);
                ++i;
                ++it;
            }
990

991
992
993
994
            while(i < max_task_size) {
                task_deps[i] = this->mock_dep.next();
                ++i;
            }
995

996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
            #pragma omp task                                            \
                firstprivate(leaf, leaf_source_particle_container, leaf_target_particle_container, first, it, do_inner) \
                depend(inout:                                           \
                       leaf_s_dep[:1],                                  \
                       leaf_t_dep[:1],                                  \
                       task_deps[0][:1],                                \
                       task_deps[1][:1],                                \
                       task_deps[2][:1],                                \
                       task_deps[3][:1],                                \
                       task_deps[4][:1],                                \
                       task_deps[5][:1],                                \
                       task_deps[6][:1],                                \
                       task_deps[7][:1],                                \
                       task_deps[8][:1],                                \
                       task_deps[9][:1],                                \
                       task_deps[10][:1],                               \
                       task_deps[11][:1],                               \
                       task_deps[12][:1],                               \
                       task_deps[13][:1],                               \
                       task_deps[14][:1],                               \
                       task_deps[15][:1],                               \
                       task_deps[16][:1],                               \
                       task_deps[17][:1],                               \
                       task_deps[18][:1],                               \
                       task_deps[19][:1],                               \
                       task_deps[20][:1],                               \
                       task_deps[21][:1],                               \
                       task_deps[22][:1],                               \
                       task_deps[23][:1],                               \
                       task_deps[24][:1],                               \
                       task_deps[25][:1],                               \
                       task_deps[26][:1]                                \
                    )
            {
                const int thread_num = omp_get_thread_num();
                // Vectors to be filled after sort and passed to kernel P2P
                std::vector<container_t*> u_item_source_particle_containers;
                std::vector<int> u_item_indices;
Quentin Khan's avatar
Quentin Khan committed
1034

1035
                auto last = it;
Quentin Khan's avatar
Quentin Khan committed
1036

1037
1038
1039
                while(first != last) {
                    // Skip empty u_items
                    if((*first) == leaf || (*first)->getParticleContainer()->size() == 0) {
Quentin Khan's avatar
Quentin Khan committed
1040
                        ++first;
1041
                        continue;
Quentin Khan's avatar
Quentin Khan committed
1042
                    }
1043
1044
1045
                    u_item_source_particle_containers.push_back((*first)->getParticleContainer());
                    u_item_indices.push_back(compute_box_offset_index(leaf, *first, 1));
                    ++first;
Quentin Khan's avatar
Quentin Khan committed
1046
                }
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
                // Call P2P on vectors data
                this->_kernels[thread_num]
                    ->P2P(
                        FTreeCoordinate(MortonIndex(leaf->getIndex())),
                        leaf_target_particle_container,
                        leaf_source_particle_container,
                        u_item_source_particle_containers.data(),
                        u_item_indices.data(),
                        static_cast<int>(u_item_source_particle_containers.size()),
                        do_inner
                        );
Quentin Khan's avatar
Quentin Khan committed
1058
            }
1059
1060
            do_inner = false;

Quentin Khan's avatar
Quentin Khan committed
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
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
        }
    }


    /** Compute resulting index in pre-computation array.
     *
     * Kernels precompute some operators according to node offsets. This method
     * returns the `other_node` index in the precomputation array.
     *
     * The index has the following form, with d the space dimension:
     *
     * x+n * 7^d + y+n * 7^(d-1) + ... + z+n
     *
     * Because an `other_node` is at most `n` boxes away in every direction from a
     * `node`, in order to get a positive index, we add `n` to the offset in every
     * direction.
     *
     * \param node The target node
     * \param other_node The interacting node
     * \param n The longest possible distance between node and other_node in
     * terms of boxes on an axis
     *
     * \warning If the nodes are not at the same level, the lowest one's
     * ancestor at the highest one's level is used.
     */
    int compute_box_offset_index(node_t* node, node_t* other_node, const std::size_t n) {
        while(node->getDepth() > other_node->getDepth()) {
            node = node->getParent();
        }
        while(node->getDepth() < other_node->getDepth()) {
            other_node = other_node->getParent();
        }

        typename node_t::FReal box_width = (node->getBox().c2 - node->getBox().c1)[0];

        auto center_offset = other_node->getBox().center() - node->getBox().center();
        std::size_t other_node_index = 0;
        for(std::size_t i = 0; i < node_t::Dim; ++i) {
            other_node_index = other_node_index * (2 * n + 1)
                + static_cast<unsigned long>(std::lround(center_offset[i] / box_width))
                + n;
        }

        return static_cast<int>(other_node_index);
    }


};

#endif