p2p_distr_mpi.hpp 62.1 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#ifndef P2P_DISTR_MPI_HPP
#define P2P_DISTR_MPI_HPP

#include <mpi.h>

#include <vector>
#include <memory>
#include <cassert>

#include <type_traits>
#include <omp.h>
#include <algorithm>

#include "scope_timer.hpp"
#include "particles_utils.hpp"
#include "p2p_tree.hpp"

template <class partsize_t, class real_number>
class p2p_distr_mpi {
protected:
    static const int MaxNbRhs = 100;

    enum MpiTag{
        TAG_NB_PARTICLES,
        TAG_POSITION_PARTICLES,
        TAG_RESULT_PARTICLES,
    };

    struct NeighborDescriptor{
        partsize_t nbParticlesToExchange;
        int destProc;
        int nbLevelsToExchange;
        bool isRecv;
34
        bool positionsReceived;
35
36
37

        std::unique_ptr<real_number[]> toRecvAndMerge;
        std::unique_ptr<real_number[]> toCompute;
38
        std::unique_ptr<real_number[]> toData;
39
40
41
42
43
44
45
        std::unique_ptr<real_number[]> results;
    };

    enum Action{
        NOTHING_TODO,
        RECV_PARTICLES,
        COMPUTE_PARTICLES,
46
        CHECK_PARTICLES,
47
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
73
74
75
76
77
        RELEASE_BUFFER_PARTICLES,
        MERGE_PARTICLES,

        RECV_MOVE_NB_LOW,
        RECV_MOVE_NB_UP,
        RECV_MOVE_LOW,
        RECV_MOVE_UP
    };

    MPI_Comm current_com;

    int my_rank;
    int nb_processes;
    int nb_processes_involved;

    const std::pair<int,int> current_partition_interval;
    const int current_partition_size;
    const std::array<size_t,3> field_grid_dim;

    std::unique_ptr<int[]> partition_interval_size_per_proc;
    std::unique_ptr<int[]> partition_interval_offset_per_proc;

    std::unique_ptr<partsize_t[]> current_offset_particles_for_partition;

    std::vector<std::pair<Action,int>> whatNext;
    std::vector<MPI_Request> mpiRequests;
    std::vector<NeighborDescriptor> neigDescriptors;

    std::array<real_number,3> spatial_box_width;
    std::array<real_number,3> spatial_box_offset;

78
    const real_number cutoff_radius_compute;
79
    const int nb_cells_factor;
80
81
82
    const real_number cutoff_radius;
    std::array<long int,3> nb_cell_levels;

83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
    template <class DataType, int sizeElement>
    static void permute_copy(const partsize_t offsetIdx, const partsize_t nbElements,
                             const std::pair<long int,partsize_t> permutation[],
                             DataType data[], std::vector<unsigned char>* buffer){
        buffer->resize(nbElements*sizeof(DataType)*sizeElement);
        DataType* dataBuffer = reinterpret_cast<DataType*>(buffer->data());

        // Permute
        for(partsize_t idxPart = 0 ; idxPart < nbElements ; ++idxPart){
            const partsize_t srcData = permutation[idxPart].second;
            const partsize_t destData = idxPart;
            for(int idxVal = 0 ; idxVal < sizeElement ; ++idxVal){
                dataBuffer[destData*sizeElement + idxVal]
                        = data[srcData*sizeElement + idxVal];
            }
        }

        // Copy back
        for(partsize_t idxPart = 0 ; idxPart < nbElements ; ++idxPart){
            const partsize_t srcData = idxPart;
            const partsize_t destData = idxPart+offsetIdx;
            for(int idxVal = 0 ; idxVal < sizeElement ; ++idxVal){
                data[destData*sizeElement + idxVal]
                        = dataBuffer[srcData*sizeElement + idxVal];
            }
        }
    }

111
    static int foundGridFactor(const real_number in_cutoff_radius, const std::array<real_number,3>& in_spatial_box_width){
112
113
114
115
        int idx_factor = 1;
        while(in_cutoff_radius <= in_spatial_box_width[IDX_Z]/real_number(idx_factor+1)){
            idx_factor += 1;
        }
116
        return idx_factor;
117
118
    }

119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
public:
    ////////////////////////////////////////////////////////////////////////////

    p2p_distr_mpi(MPI_Comm in_current_com,
                     const std::pair<int,int>& in_current_partitions,
                     const std::array<size_t,3>& in_field_grid_dim,
                     const std::array<real_number,3>& in_spatial_box_width,
                     const std::array<real_number,3>& in_spatial_box_offset,
                     const real_number in_cutoff_radius)
        : current_com(in_current_com),
            my_rank(-1), nb_processes(-1),nb_processes_involved(-1),
            current_partition_interval(in_current_partitions),
            current_partition_size(current_partition_interval.second-current_partition_interval.first),
            field_grid_dim(in_field_grid_dim),
            spatial_box_width(in_spatial_box_width), spatial_box_offset(in_spatial_box_offset),
134
            cutoff_radius_compute(in_cutoff_radius),
135
136
            nb_cells_factor(foundGridFactor(in_cutoff_radius, in_spatial_box_width)),
            cutoff_radius(in_spatial_box_width[IDX_Z]/real_number(nb_cells_factor)){
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165

        AssertMpi(MPI_Comm_rank(current_com, &my_rank));
        AssertMpi(MPI_Comm_size(current_com, &nb_processes));

        partition_interval_size_per_proc.reset(new int[nb_processes]);
        AssertMpi( MPI_Allgather( const_cast<int*>(&current_partition_size), 1, MPI_INT,
                                  partition_interval_size_per_proc.get(), 1, MPI_INT,
                                  current_com) );
        assert(partition_interval_size_per_proc[my_rank] == current_partition_size);

        partition_interval_offset_per_proc.reset(new int[nb_processes+1]);
        partition_interval_offset_per_proc[0] = 0;
        for(int idxProc = 0 ; idxProc < nb_processes ; ++idxProc){
            partition_interval_offset_per_proc[idxProc+1] = partition_interval_offset_per_proc[idxProc] + partition_interval_size_per_proc[idxProc];
        }

        current_offset_particles_for_partition.reset(new partsize_t[current_partition_size+1]);

        nb_processes_involved = nb_processes;
        while(nb_processes_involved != 0 && partition_interval_size_per_proc[nb_processes_involved-1] == 0){
            nb_processes_involved -= 1;
        }
        assert(nb_processes_involved != 0);
        for(int idx_proc_involved = 0 ; idx_proc_involved < nb_processes_involved ; ++idx_proc_involved){
            assert(partition_interval_size_per_proc[idx_proc_involved] != 0);
        }

        assert(int(field_grid_dim[IDX_Z]) == partition_interval_offset_per_proc[nb_processes_involved]);

166
167
168
        nb_cell_levels[IDX_X] = nb_cells_factor;
        nb_cell_levels[IDX_Y] = nb_cells_factor;
        nb_cell_levels[IDX_Z] = nb_cells_factor;
169
170
171
172
173
174
    }

    virtual ~p2p_distr_mpi(){}

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

175
176
177
178
179
180
181
182
    int getGridFactor() const{
        return nb_cells_factor;
    }

    real_number getGridCutoff() const{
        return cutoff_radius;
    }

183
184
185
186
187
    long int get_cell_coord_x_from_index(const long int index) const{
        return index % nb_cell_levels[IDX_X];
    }

    long int get_cell_coord_y_from_index(const long int index) const{
188
        return (index % (nb_cell_levels[IDX_X]*nb_cell_levels[IDX_Y]))
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
                / nb_cell_levels[IDX_X];
    }

    long int get_cell_coord_z_from_index(const long int index) const{
        return index / (nb_cell_levels[IDX_X]*nb_cell_levels[IDX_Y]);
    }

    long int first_cell_level_proc(const int dest_proc) const{
        const real_number field_section_width_z = spatial_box_width[IDX_Z]/real_number(field_grid_dim[IDX_Z]);
        return static_cast<long int>((field_section_width_z*real_number(partition_interval_offset_per_proc[dest_proc]))/cutoff_radius);
    }

    long int last_cell_level_proc(const int dest_proc) const{
        const real_number field_section_width_z = spatial_box_width[IDX_Z]/real_number(field_grid_dim[IDX_Z]);
        return static_cast<long int>((field_section_width_z*real_number(partition_interval_offset_per_proc[dest_proc+1])
                                     - std::numeric_limits<real_number>::epsilon())/cutoff_radius);
    }

    std::array<long int,3> get_cell_coordinate(const real_number pos_x, const real_number pos_y,
                                               const real_number pos_z) const {
        const real_number diff_x = pos_x - spatial_box_offset[IDX_X];
        const real_number diff_y = pos_y - spatial_box_offset[IDX_Y];
        const real_number diff_z = pos_z - spatial_box_offset[IDX_Z];
        std::array<long int,3> coord;
        coord[IDX_X] = static_cast<long int>(diff_x/cutoff_radius);
        coord[IDX_Y] = static_cast<long int>(diff_y/cutoff_radius);
        coord[IDX_Z] = static_cast<long int>(diff_z/cutoff_radius);
        return coord;
    }

    long int get_cell_idx(const real_number pos_x, const real_number pos_y,
                          const real_number pos_z) const {
        std::array<long int,3> coord = get_cell_coordinate(pos_x, pos_y, pos_z);
        return ((coord[IDX_Z]*nb_cell_levels[IDX_Y])+coord[IDX_Y])*nb_cell_levels[IDX_X]+coord[IDX_X];
    }

    real_number compute_distance_r2(const real_number x1, const real_number y1, const real_number z1,
Berenger Bramas's avatar
Berenger Bramas committed
226
227
228
229
                                    const real_number x2, const real_number y2, const real_number z2,
                                    const real_number xshift_coef, const real_number yshift_coef, const real_number zshift_coef) const {
        real_number diff_x = std::abs(x1-x2+xshift_coef*spatial_box_width[IDX_X]);
        assert(diff_x <= 2*cutoff_radius);
230

Berenger Bramas's avatar
Berenger Bramas committed
231
232
        real_number diff_y = std::abs(y1-y2+yshift_coef*spatial_box_width[IDX_Y]);
        assert(diff_y <= 2*cutoff_radius);
233

Berenger Bramas's avatar
Berenger Bramas committed
234
235
        real_number diff_z = std::abs(z1-z2+zshift_coef*spatial_box_width[IDX_Z]);
        assert(diff_z <= 2*cutoff_radius);
Berenger Bramas's avatar
Debug    
Berenger Bramas committed
236
237
238

        return (diff_x*diff_x) + (diff_y*diff_y) + (diff_z*diff_z);
    }
239

240
    template <class computer_class, int size_particle_positions, int size_particle_data, int size_particle_rhs>
241
242
243
    void compute_distr(computer_class& in_computer,
                       const partsize_t current_my_nb_particles_per_partition[],
                       real_number particles_positions[],
244
                       real_number particles_data[],
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
275
                       real_number particles_current_rhs[],
                       partsize_t inout_index_particles[]){
        TIMEZONE("compute_distr");

        // Some processes might not be involved
        if(nb_processes_involved <= my_rank){
            return;
        }

        const long int my_top_z_cell_level = last_cell_level_proc(my_rank);
        const long int my_down_z_cell_level = first_cell_level_proc(my_rank);
        const long int my_nb_cell_levels = 1+my_top_z_cell_level-my_down_z_cell_level;

        current_offset_particles_for_partition[0] = 0;
        partsize_t myTotalNbParticles = 0;
        for(int idxPartition = 0 ; idxPartition < current_partition_size ; ++idxPartition){
            myTotalNbParticles += current_my_nb_particles_per_partition[idxPartition];
            current_offset_particles_for_partition[idxPartition+1] = current_offset_particles_for_partition[idxPartition] + current_my_nb_particles_per_partition[idxPartition];
        }

        // Compute box idx for each particle
        std::unique_ptr<long int[]> particles_coord(new long int[current_offset_particles_for_partition[current_partition_size]]);

        {
            for(int idxPartition = 0 ; idxPartition < current_partition_size ; ++idxPartition){
                #pragma omp parallel for schedule(static)
                for(partsize_t idxPart = current_offset_particles_for_partition[idxPartition] ; idxPart < current_offset_particles_for_partition[idxPartition+1] ; ++idxPart ){
                    particles_coord[idxPart] = get_cell_idx(particles_positions[(idxPart)*size_particle_positions + IDX_X],
                                                                              particles_positions[(idxPart)*size_particle_positions + IDX_Y],
                                                                              particles_positions[(idxPart)*size_particle_positions + IDX_Z]);
                    assert(my_down_z_cell_level <= get_cell_coord_z_from_index(particles_coord[idxPart]));
276
277
278
279
280
281
282
283
284
285
286
                    if(!(get_cell_coord_z_from_index(particles_coord[idxPart]) <= my_top_z_cell_level)){// TODO
                        printf("Coord index %ld - %ld (tree index %ld)\n", idxPart, inout_index_particles[idxPart],particles_coord[idxPart]);
                        printf(">> Box index %ld - %ld - %ld\n", get_cell_coord_x_from_index(particles_coord[idxPart]),
                               get_cell_coord_y_from_index(particles_coord[idxPart]),
                               get_cell_coord_z_from_index(particles_coord[idxPart]));
                        printf(">> idxPartition %d\n", idxPartition);
                        printf(">> my_top_z_cell_level %ld\n", my_top_z_cell_level);
                        printf(">> position %e %e %e\n", particles_positions[(idxPart)*size_particle_positions + IDX_X],
                                particles_positions[(idxPart)*size_particle_positions + IDX_Y],
                                particles_positions[(idxPart)*size_particle_positions + IDX_Z]);
                    }
287
288
289
290
                    assert(get_cell_coord_z_from_index(particles_coord[idxPart]) <= my_top_z_cell_level);
                }
            }

291
            std::vector<std::pair<long int,partsize_t>> part_to_sort;
292
293
294
295
296
297
298

            // Sort each partition in cells
            for(int idxPartition = 0 ; idxPartition < current_partition_size ; ++idxPartition){
                part_to_sort.clear();

                for(partsize_t idxPart = current_offset_particles_for_partition[idxPartition] ; idxPart < current_offset_particles_for_partition[idxPartition+1] ; ++idxPart ){
                    part_to_sort.emplace_back();
299
300
                    part_to_sort.back().first = particles_coord[idxPart];
                    part_to_sort.back().second = idxPart;
301
302
                }

303
                assert(part_to_sort.size() == (current_my_nb_particles_per_partition[idxPartition]));
304
305

                std::sort(part_to_sort.begin(), part_to_sort.end(),
306
307
308
                          [](const std::pair<long int,partsize_t>& p1,
                             const std::pair<long int,partsize_t>& p2){
                    return p1.first < p2.first;
309
                });
310
311
312
313
314
315
316
317
318
319

//                for(partsize_t idxPart = 1 ; idxPart < (long int)part_to_sort.size() ; ++idxPart){// TODO
//                    assert(part_to_sort[idxPart-1].first <= part_to_sort[idxPart].first);
//                }

                // Permute array using buffer
                std::vector<unsigned char> buffer;
                permute_copy<real_number, size_particle_positions>(current_offset_particles_for_partition[idxPartition],
                                                                   current_my_nb_particles_per_partition[idxPartition],
                                                                   part_to_sort.data(), particles_positions, &buffer);
320
321
322
                permute_copy<real_number, size_particle_data>(current_offset_particles_for_partition[idxPartition],
                                                             current_my_nb_particles_per_partition[idxPartition],
                                                             part_to_sort.data(), particles_data, &buffer);
323
324
325
326
327
328
329
330
331
                permute_copy<real_number, size_particle_rhs>(current_offset_particles_for_partition[idxPartition],
                                                             current_my_nb_particles_per_partition[idxPartition],
                                                             part_to_sort.data(), particles_current_rhs, &buffer);
                permute_copy<partsize_t, 1>(current_offset_particles_for_partition[idxPartition],
                                            current_my_nb_particles_per_partition[idxPartition],
                                            part_to_sort.data(), inout_index_particles, &buffer);
                permute_copy<long int, 1>(current_offset_particles_for_partition[idxPartition],
                                            current_my_nb_particles_per_partition[idxPartition],
                                            part_to_sort.data(), particles_coord.get(), &buffer);
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
            }
        }

        // Build the tree
        p2p_tree<std::vector<std::pair<partsize_t,partsize_t>>> my_tree(nb_cell_levels);

        for(int idxPartition = 0 ; idxPartition < current_partition_size ; ++idxPartition){
            long int current_cell_idx = -1;
            partsize_t current_nb_particles_in_cell = 0;
            partsize_t current_cell_offset = 0;

            for(partsize_t idx_part = current_offset_particles_for_partition[idxPartition] ;
                            idx_part != current_offset_particles_for_partition[idxPartition+1]; ++idx_part){
                if(particles_coord[idx_part] != current_cell_idx){
                    if(current_nb_particles_in_cell){
                        my_tree.getCell(current_cell_idx).emplace_back(current_cell_offset,current_nb_particles_in_cell);
                    }
                    current_cell_idx = particles_coord[idx_part];
                    current_nb_particles_in_cell = 1;
                    current_cell_offset = idx_part;
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
//                    if(inout_index_particles[idx_part] == 547){// TODO
//                        printf("idxPartition %d\n", idxPartition);
//                        printf(">> Coord index %ld - %ld (tree index %ld)\n", idx_part, inout_index_particles[idx_part],particles_coord[idx_part]);
//                        printf(">> Box index %ld - %ld - %ld\n", get_cell_coord_x_from_index(particles_coord[idx_part]),
//                               get_cell_coord_y_from_index(particles_coord[idx_part]),
//                               get_cell_coord_z_from_index(particles_coord[idx_part]));
//                        printf(">> current_cell_offset %ld current_nb_particles_in_cell %ld\n", current_cell_offset, current_nb_particles_in_cell);
//                        printf(">> Position %e %e %e\n", particles_positions[idx_part*size_particle_positions + IDX_X],
//                                particles_positions[idx_part*size_particle_positions + IDX_Y],
//                                particles_positions[idx_part*size_particle_positions + IDX_Z]);
//                    }
//                    if(inout_index_particles[idx_part] == 356){// TODO
//                        printf("idxPartition %d\n", idxPartition);
//                        printf(">> Coord index %ld - %ld (tree index %ld)\n", idx_part, inout_index_particles[idx_part],particles_coord[idx_part]);
//                        printf(">> Box index %ld - %ld - %ld\n", get_cell_coord_x_from_index(particles_coord[idx_part]),
//                               get_cell_coord_y_from_index(particles_coord[idx_part]),
//                               get_cell_coord_z_from_index(particles_coord[idx_part]));
//                        printf(">> current_cell_offset %ld current_nb_particles_in_cell %ld\n", current_cell_offset, current_nb_particles_in_cell);
//                        printf(">> Position %e %e %e\n", particles_positions[idx_part*size_particle_positions + IDX_X],
//                                particles_positions[idx_part*size_particle_positions + IDX_Y],
//                                particles_positions[idx_part*size_particle_positions + IDX_Z]);
//                    }
                }
                else{
                    current_nb_particles_in_cell += 1;
377
378
379
380
381
382
383
384
                }
            }
            if(current_nb_particles_in_cell){
                my_tree.getCell(current_cell_idx).emplace_back(current_cell_offset,current_nb_particles_in_cell);

            }
        }

385
386
387
//        printf("[%d] go from cutoff level %ld to %ld\n",
//               my_rank, my_down_z_cell_level, my_top_z_cell_level); // TODO remove
//        fflush(stdout); // TODO
388
389

        // Offset per cell layers
Berenger Bramas's avatar
Debug    
Berenger Bramas committed
390
        long int previous_index = 0;
391
392
393
394
        std::unique_ptr<partsize_t[]> particles_offset_layers(new partsize_t[my_nb_cell_levels+1]());
        for(int idxPartition = 0 ; idxPartition < current_partition_size ; ++idxPartition){
            for(partsize_t idx_part = current_offset_particles_for_partition[idxPartition] ;
                            idx_part != current_offset_particles_for_partition[idxPartition+1]; ++idx_part){
Berenger Bramas's avatar
Debug    
Berenger Bramas committed
395
396
397
398
399
400
                const long int part_box_z_index = get_cell_coord_z_from_index(particles_coord[idx_part]);
                assert(my_down_z_cell_level <= part_box_z_index);
                assert(part_box_z_index <= my_top_z_cell_level);
                particles_offset_layers[part_box_z_index+1-my_down_z_cell_level] += 1;
                assert(previous_index <= part_box_z_index);
                previous_index = part_box_z_index;
401
402
            }
        }
403
404
405
406
        for(long int idx_layer = 0 ; idx_layer < my_nb_cell_levels ; ++idx_layer){
//            printf("[%d] nb particles in cutoff level %ld are %ld\n",
//                   my_rank, idx_layer, particles_offset_layers[idx_layer+1]); // TODO remove
//            fflush(stdout); // TODO
407
408
409
410
411
412
413
414
415
416
            particles_offset_layers[idx_layer+1] += particles_offset_layers[idx_layer];
        }

        // Reset vectors
        assert(whatNext.size() == 0);
        assert(mpiRequests.size() == 0);
        neigDescriptors.clear();

        // Find process with at least one neighbor
        {
417
418
419
            std::cout << my_rank << ">>  my_top_z_cell_level " << my_top_z_cell_level << std::endl;
            std::cout << my_rank << ">>  my_down_z_cell_level " << my_down_z_cell_level << std::endl;
            std::cout.flush();// TODO
420
421
422
423
424
425
426
427
428
429
430
431

            int dest_proc = (my_rank+1)%nb_processes_involved;
            while(dest_proc != my_rank
                  && (my_top_z_cell_level == first_cell_level_proc(dest_proc)
                      || (my_top_z_cell_level+1)%nb_cell_levels[IDX_Z] == first_cell_level_proc(dest_proc))){
                // Find if we have to send 1 or 2 cell levels
                int nb_levels_to_send = 1;
                if(my_nb_cell_levels > 1 // I have more than one level
                        && (my_top_z_cell_level-1+2)%nb_cell_levels[IDX_Z] <= last_cell_level_proc(dest_proc)){
                    nb_levels_to_send += 1;
                }

432
433
434
435
                std::cout << my_rank << " dest_proc " << dest_proc << std::endl;
                std::cout << my_rank << ">> first_cell_level_proc(dest_proc) " << first_cell_level_proc(dest_proc) << std::endl;
                std::cout << my_rank << ">> last_cell_level_proc(dest_proc) " << last_cell_level_proc(dest_proc) << std::endl;
                std::cout.flush();// TODO
436
437
438
439
440
441

                NeighborDescriptor descriptor;
                descriptor.destProc = dest_proc;
                descriptor.nbLevelsToExchange = nb_levels_to_send;
                descriptor.nbParticlesToExchange = particles_offset_layers[my_nb_cell_levels] - particles_offset_layers[my_nb_cell_levels-nb_levels_to_send];
                descriptor.isRecv = false;
442
                descriptor.positionsReceived = false;
443

444
445
446
447
448
449
450
                std::cout << my_rank << " SEND" << std::endl;
                std::cout << ">> descriptor.destProc " << descriptor.destProc << std::endl;
                std::cout << ">> descriptor.nbLevelsToExchange " << descriptor.nbLevelsToExchange << std::endl;
                std::cout << ">> descriptor.nbParticlesToExchange " << descriptor.nbParticlesToExchange << std::endl;
                std::cout << ">> descriptor.isRecv " << descriptor.isRecv << std::endl;
                std::cout << ">> neigDescriptors.size() " << neigDescriptors.size() << std::endl;
                std::cout.flush();// TODO
451
452
453
454
455

                neigDescriptors.emplace_back(std::move(descriptor));

                dest_proc = (dest_proc+1)%nb_processes_involved;
            }
456
457
458
            std::cout << my_rank << " NO dest_proc " << dest_proc << std::endl;
            std::cout << my_rank << " NO first_cell_level_proc(dest_proc) " << first_cell_level_proc(dest_proc) << std::endl;
            std::cout.flush();// TODO
459
460
461
462
463
464
465
466
467
468
469
470

            int src_proc = (my_rank-1+nb_processes_involved)%nb_processes_involved;
            while(src_proc != my_rank
                  && (last_cell_level_proc(src_proc) == my_down_z_cell_level
                      || (last_cell_level_proc(src_proc)+1)%nb_cell_levels[IDX_Z] == my_down_z_cell_level)){
                // Find if we have to send 1 or 2 cell levels
                int nb_levels_to_recv = 1;
                if(my_nb_cell_levels > 1 // I have more than one level
                        && first_cell_level_proc(src_proc) <= (my_down_z_cell_level-1+2)%nb_cell_levels[IDX_Z]){
                    nb_levels_to_recv += 1;
                }

471
472
473
                std::cout << my_rank << " src_proc " << src_proc << std::endl;
                std::cout << my_rank << " first_cell_level_proc(src_proc) " << first_cell_level_proc(src_proc) << std::endl;
                std::cout.flush();// TODO
474
475
476
477
478
479

                NeighborDescriptor descriptor;
                descriptor.destProc = src_proc;
                descriptor.nbLevelsToExchange = nb_levels_to_recv;
                descriptor.nbParticlesToExchange = -1;
                descriptor.isRecv = true;
480
                descriptor.positionsReceived = false;
481
482
483

                neigDescriptors.emplace_back(std::move(descriptor));

484
485
486
487
488
489
490
491
                std::cout << my_rank << "] RECV" << std::endl;
                std::cout << ">> descriptor.destProc " << descriptor.destProc << std::endl;
                std::cout << ">> descriptor.nbLevelsToExchange " << descriptor.nbLevelsToExchange << std::endl;
                std::cout << ">> descriptor.nbParticlesToExchange " << descriptor.nbParticlesToExchange << std::endl;
                std::cout << ">> descriptor.nbParticlesToExchange " << descriptor.nbParticlesToExchange << std::endl;
                std::cout << ">> descriptor.isRecv " << descriptor.isRecv << std::endl;
                std::cout << ">> neigDescriptors.size() " << neigDescriptors.size() << std::endl;
                std::cout.flush();// TODO
492
493
494

                src_proc = (src_proc-1+nb_processes_involved)%nb_processes_involved;
            }
495
496
497
            std::cout << my_rank << " NO src_proc " << src_proc << std::endl;
            std::cout << my_rank << " NO first_cell_level_proc(src_proc) " << first_cell_level_proc(src_proc) << std::endl;
            std::cout.flush();// TODO
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
        }

        //////////////////////////////////////////////////////////////////////
        /// Exchange the number of particles in each partition
        /// Could involve only here but I do not think it will be a problem
        //////////////////////////////////////////////////////////////////////

        assert(whatNext.size() == 0);
        assert(mpiRequests.size() == 0);


        for(int idxDescr = 0 ; idxDescr < int(neigDescriptors.size()) ; ++idxDescr){
            NeighborDescriptor& descriptor = neigDescriptors[idxDescr];

            if(descriptor.isRecv == false){
                whatNext.emplace_back(std::pair<Action,int>{NOTHING_TODO, -1});
                mpiRequests.emplace_back();
                AssertMpi(MPI_Isend(const_cast<partsize_t*>(&descriptor.nbParticlesToExchange),
                                    1, particles_utils::GetMpiType(partsize_t()),
                                    descriptor.destProc, TAG_NB_PARTICLES,
                                    current_com, &mpiRequests.back()));

                if(descriptor.nbParticlesToExchange){
521
522
523
524
525
//                    std::cout << my_rank << "] SEND_PARTICLES" << std::endl;
//                    std::cout << "descriptor.nbParticlesToExchange " << descriptor.nbParticlesToExchange << std::endl;
//                    std::cout << "descriptor.destProc " << descriptor.destProc << std::endl;
//                    std::cout << "idxDescr " << idxDescr << std::endl;
//                    std::cout << "send from part " << particles_offset_layers[my_nb_cell_levels-descriptor.nbLevelsToExchange] << std::endl;
526
527
528
529

                    whatNext.emplace_back(std::pair<Action,int>{NOTHING_TODO, -1});
                    mpiRequests.emplace_back();
                    assert(descriptor.nbParticlesToExchange*size_particle_positions < std::numeric_limits<int>::max());
Berenger Bramas's avatar
Debug    
Berenger Bramas committed
530
                    AssertMpi(MPI_Isend(const_cast<real_number*>(&particles_positions[particles_offset_layers[my_nb_cell_levels-descriptor.nbLevelsToExchange]*size_particle_positions]),
531
532
533
534
                              int(descriptor.nbParticlesToExchange*size_particle_positions), particles_utils::GetMpiType(real_number()),
                              descriptor.destProc, TAG_POSITION_PARTICLES,
                              current_com, &mpiRequests.back()));

535
536
537
538
539
540
541
542
                    whatNext.emplace_back(std::pair<Action,int>{NOTHING_TODO, -1});
                    mpiRequests.emplace_back();
                    assert(descriptor.nbParticlesToExchange*size_particle_data < std::numeric_limits<int>::max());
                    AssertMpi(MPI_Isend(const_cast<real_number*>(&particles_data[particles_offset_layers[my_nb_cell_levels-descriptor.nbLevelsToExchange]*size_particle_data]),
                              int(descriptor.nbParticlesToExchange*size_particle_data), particles_utils::GetMpiType(real_number()),
                              descriptor.destProc, TAG_POSITION_PARTICLES,
                              current_com, &mpiRequests.back()));

543
544
                    assert(descriptor.toRecvAndMerge == nullptr);
                    descriptor.toRecvAndMerge.reset(new real_number[descriptor.nbParticlesToExchange*size_particle_rhs]);
Berenger Bramas's avatar
Debug    
Berenger Bramas committed
545
                    whatNext.emplace_back(std::pair<Action,int>{MERGE_PARTICLES, idxDescr});
546
547
548
549
550
551
552
553
                    mpiRequests.emplace_back();
                    assert(descriptor.nbParticlesToExchange*size_particle_rhs < std::numeric_limits<int>::max());
                    AssertMpi(MPI_Irecv(descriptor.toRecvAndMerge.get(), int(descriptor.nbParticlesToExchange*size_particle_rhs),
                                        particles_utils::GetMpiType(real_number()), descriptor.destProc, TAG_RESULT_PARTICLES,
                                        current_com, &mpiRequests.back()));
                }
            }
            else{
554
555
//                std::cout << "RECV_PARTICLES " << RECV_PARTICLES << std::endl;
//                std::cout << "idxDescr " << idxDescr << std::endl;
556
557
558
559
560
561
562
563
564
565
566
567
568
569
                whatNext.emplace_back(std::pair<Action,int>{RECV_PARTICLES, idxDescr});
                mpiRequests.emplace_back();
                AssertMpi(MPI_Irecv(&descriptor.nbParticlesToExchange,
                      1, particles_utils::GetMpiType(partsize_t()), descriptor.destProc, TAG_NB_PARTICLES,
                      current_com, &mpiRequests.back()));
            }
        }

        TIMEZONE_OMP_INIT_PREPARALLEL(omp_get_max_threads())
        #pragma omp parallel default(shared)
        {
            #pragma omp master
            {
                while(mpiRequests.size()){
Berenger Bramas's avatar
Berenger Bramas committed
570
                    TIMEZONE("wait-loop");
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
                    assert(mpiRequests.size() == whatNext.size());

                    int idxDone = int(mpiRequests.size());
                    {
                        TIMEZONE("wait");
                        AssertMpi(MPI_Waitany(int(mpiRequests.size()), mpiRequests.data(), &idxDone, MPI_STATUSES_IGNORE));
                    }
                    const std::pair<Action, int> releasedAction = whatNext[idxDone];
                    std::swap(mpiRequests[idxDone], mpiRequests[mpiRequests.size()-1]);
                    std::swap(whatNext[idxDone], whatNext[mpiRequests.size()-1]);
                    mpiRequests.pop_back();
                    whatNext.pop_back();

                    //////////////////////////////////////////////////////////////////////
                    /// Data to exchange particles
                    //////////////////////////////////////////////////////////////////////
                    if(releasedAction.first == RECV_PARTICLES){
Berenger Bramas's avatar
Berenger Bramas committed
588
                        TIMEZONE("post-recv-particles");
589
                        NeighborDescriptor& descriptor = neigDescriptors[releasedAction.second];
Berenger Bramas's avatar
Debug    
Berenger Bramas committed
590
                        assert(descriptor.isRecv);
591
592
593
594
595
                        const int destProc = descriptor.destProc;
                        const partsize_t NbParticlesToReceive = descriptor.nbParticlesToExchange;
                        assert(NbParticlesToReceive != -1);
                        assert(descriptor.toCompute == nullptr);

596
597
598
599
//                        std::cout << my_rank << "] RECV_PARTICLES" << std::endl;
//                        std::cout << "descriptor.nbParticlesToExchange " << descriptor.nbParticlesToExchange << std::endl;
//                        std::cout << "descriptor.destProc " << descriptor.destProc << std::endl;
//                        std::cout << "releasedAction.second " << releasedAction.second << std::endl;
600
601

                        if(NbParticlesToReceive){
602
//                            std::cout << "MPI_Irecv " << std::endl;
603
                            descriptor.toCompute.reset(new real_number[NbParticlesToReceive*size_particle_positions]);
604
                            whatNext.emplace_back(std::pair<Action,int>{CHECK_PARTICLES, releasedAction.second});
605
606
607
608
609
                            mpiRequests.emplace_back();
                            assert(NbParticlesToReceive*size_particle_positions < std::numeric_limits<int>::max());
                            AssertMpi(MPI_Irecv(descriptor.toCompute.get(), int(NbParticlesToReceive*size_particle_positions),
                                                particles_utils::GetMpiType(real_number()), destProc, TAG_POSITION_PARTICLES,
                                                current_com, &mpiRequests.back()));
610
611
612
613
614
615
616
617
618


                            descriptor.toData.reset(new real_number[NbParticlesToReceive*size_particle_data]);
                            whatNext.emplace_back(std::pair<Action,int>{COMPUTE_PARTICLES, releasedAction.second});
                            mpiRequests.emplace_back();
                            assert(NbParticlesToReceive*size_particle_data < std::numeric_limits<int>::max());
                            AssertMpi(MPI_Irecv(descriptor.toData.get(), int(NbParticlesToReceive*size_particle_data),
                                                particles_utils::GetMpiType(real_number()), destProc, TAG_POSITION_PARTICLES,
                                                current_com, &mpiRequests.back()));
619
620
621
622
623
624
625
                        }
                    }

                    //////////////////////////////////////////////////////////////////////
                    /// Computation
                    //////////////////////////////////////////////////////////////////////
                    if(releasedAction.first == COMPUTE_PARTICLES){
Berenger Bramas's avatar
Berenger Bramas committed
626
                        TIMEZONE("compute-particles");
627
                        NeighborDescriptor& descriptor = neigDescriptors[releasedAction.second];
Berenger Bramas's avatar
Debug    
Berenger Bramas committed
628
                        assert(descriptor.isRecv);
629
630
631
                        const partsize_t NbParticlesToReceive = descriptor.nbParticlesToExchange;

                        assert(descriptor.toCompute != nullptr);
632
633
                        assert(descriptor.toData != nullptr);
                        assert(descriptor.positionsReceived == true);
634
                        descriptor.results.reset(new real_number[NbParticlesToReceive*size_particle_rhs]);
Berenger Bramas's avatar
Debug    
Berenger Bramas committed
635
                        in_computer.template init_result_array<size_particle_rhs>(descriptor.results.get(), NbParticlesToReceive);
636
637
638
639
640
641
642

                        // Compute
                        partsize_t idxPart = 0;
                        while(idxPart != NbParticlesToReceive){
                            const long int current_cell_idx = get_cell_idx(descriptor.toCompute[idxPart*size_particle_positions + IDX_X],
                                                                           descriptor.toCompute[idxPart*size_particle_positions + IDX_Y],
                                                                           descriptor.toCompute[idxPart*size_particle_positions + IDX_Z]);
Berenger Bramas's avatar
Debug    
Berenger Bramas committed
643
                            partsize_t nb_parts_in_cell = 1;
644
645
646
647
648
649
650
651
                            while(idxPart+nb_parts_in_cell != NbParticlesToReceive
                                  && current_cell_idx == get_cell_idx(descriptor.toCompute[(idxPart+nb_parts_in_cell)*size_particle_positions + IDX_X],
                                                                     descriptor.toCompute[(idxPart+nb_parts_in_cell)*size_particle_positions + IDX_Y],
                                                                     descriptor.toCompute[(idxPart+nb_parts_in_cell)*size_particle_positions + IDX_Z])){
                                nb_parts_in_cell += 1;
                            }

                            const std::vector<std::pair<partsize_t,partsize_t>>* neighbors[27];
652
                            long int neighbors_indexes[27];
Berenger Bramas's avatar
Berenger Bramas committed
653
654
                            std::array<real_number,3> shift[27];
                            const int nbNeighbors = my_tree.getNeighbors(current_cell_idx, neighbors, neighbors_indexes, shift, true);
655

656
657
658
659
660
661
662
663
664
665
666
667
//                            for(int idx_test = 0 ; idx_test < nb_parts_in_cell ; ++idx_test){ // TODO
//                                real_number totest[3] = {8.570442e-01, 7.173084e-02, 8.279754e-03};
//                                if(int(descriptor.toCompute[(idxPart+idx_test)*size_particle_positions + IDX_X]*1000) == int(totest[0]*1000)
//                                        && int(descriptor.toCompute[(idxPart+idx_test)*size_particle_positions + IDX_Y]*1000) == int(totest[1]*1000)
//                                        && int(descriptor.toCompute[(idxPart+idx_test)*size_particle_positions + IDX_Z]*1000) == int(totest[2]*1000)){
//                                    printf("Found a pos %ld\n", idxPart+idx_test);
//                                    printf("pos %e %e %e\n",
//                                           descriptor.toCompute[(idxPart+idx_test)*size_particle_positions + IDX_X],
//                                            descriptor.toCompute[(idxPart+idx_test)*size_particle_positions + IDX_Y],
//                                            descriptor.toCompute[(idxPart+idx_test)*size_particle_positions + IDX_Z]);
//                                }
//                            }
Berenger Bramas's avatar
Debug    
Berenger Bramas committed
668

669
670
671
672
673
674
675
676
677
678
                            // with other interval
                            for(size_t idx_neighbor = 0 ; idx_neighbor < nbNeighbors ; ++idx_neighbor){
                                for(size_t idx_2 = 0 ; idx_2 < (*neighbors[idx_neighbor]).size() ; ++idx_2){
                                    for(partsize_t idx_p1 = 0 ; idx_p1 < nb_parts_in_cell ; ++idx_p1){
                                        for(partsize_t idx_p2 = 0 ; idx_p2 < (*neighbors[idx_neighbor])[idx_2].second ; ++idx_p2){
                                            const real_number dist_r2 = compute_distance_r2(descriptor.toCompute[(idxPart+idx_p1)*size_particle_positions + IDX_X],
                                                                                            descriptor.toCompute[(idxPart+idx_p1)*size_particle_positions + IDX_Y],
                                                                                            descriptor.toCompute[(idxPart+idx_p1)*size_particle_positions + IDX_Z],
                                                                                            particles_positions[((*neighbors[idx_neighbor])[idx_2].first+idx_p2)*size_particle_positions + IDX_X],
                                                                                            particles_positions[((*neighbors[idx_neighbor])[idx_2].first+idx_p2)*size_particle_positions + IDX_Y],
Berenger Bramas's avatar
Berenger Bramas committed
679
680
                                                                                            particles_positions[((*neighbors[idx_neighbor])[idx_2].first+idx_p2)*size_particle_positions + IDX_Z],
                                                                                            shift[idx_neighbor][IDX_X], shift[idx_neighbor][IDX_Y], shift[idx_neighbor][IDX_Z]);
681
                                            if(dist_r2 < cutoff_radius_compute*cutoff_radius_compute){
682
                                                in_computer.template compute_interaction<size_particle_positions,size_particle_data, size_particle_rhs>(
Berenger Bramas's avatar
Debug    
Berenger Bramas committed
683
                                                                    &descriptor.toCompute[(idxPart+idx_p1)*size_particle_positions],
684
                                                                    &descriptor.toData[(idxPart+idx_p1)*size_particle_data],
Berenger Bramas's avatar
Debug    
Berenger Bramas committed
685
686
                                                                    &descriptor.results[(idxPart+idx_p1)*size_particle_rhs],
                                                                    &particles_positions[((*neighbors[idx_neighbor])[idx_2].first+idx_p2)*size_particle_positions],
687
                                                                    &particles_data[((*neighbors[idx_neighbor])[idx_2].first+idx_p2)*size_particle_data],
Berenger Bramas's avatar
Debug    
Berenger Bramas committed
688
                                                                    &particles_current_rhs[((*neighbors[idx_neighbor])[idx_2].first+idx_p2)*size_particle_rhs],
Berenger Bramas's avatar
Berenger Bramas committed
689
                                                                    dist_r2, shift[idx_neighbor][IDX_X], shift[idx_neighbor][IDX_Y], shift[idx_neighbor][IDX_Z]);
Berenger Bramas's avatar
Debug    
Berenger Bramas committed
690
691
                                            }

692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
//                                            if(inout_index_particles[(*neighbors[idx_neighbor])[idx_2].first+idx_p2] == 356){// TODO
//                                                printf("test interaction between :\n");
//                                                printf("index %ld (%ld) pos %e %e %e\n",
//                                                       (idxPart+idx_p1), -1L,
//                                                       descriptor.toCompute[(idxPart+idx_p1)*size_particle_positions + IDX_X],
//                                                       descriptor.toCompute[(idxPart+idx_p1)*size_particle_positions + IDX_Y],
//                                                       descriptor.toCompute[(idxPart+idx_p1)*size_particle_positions + IDX_Z]);
//                                                printf("index %ld (%ld) pos %e %e %e\n",
//                                                       ((*neighbors[idx_neighbor])[idx_2].first+idx_p2),
//                                                       inout_index_particles[((*neighbors[idx_neighbor])[idx_2].first+idx_p2)],
//                                                       particles_positions[((*neighbors[idx_neighbor])[idx_2].first+idx_p2)*size_particle_positions + IDX_X],
//                                                       particles_positions[((*neighbors[idx_neighbor])[idx_2].first+idx_p2)*size_particle_positions + IDX_Y],
//                                                       particles_positions[((*neighbors[idx_neighbor])[idx_2].first+idx_p2)*size_particle_positions + IDX_Z]);
//                                                printf("Radius = %e (%e)\n", sqrt(dist_r2), dist_r2);
//                                            }
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
                                        }
                                    }
                                }
                            }

                            idxPart += nb_parts_in_cell;
                        }

                        // Send back
                        const int destProc = descriptor.destProc;
                        whatNext.emplace_back(std::pair<Action,int>{RELEASE_BUFFER_PARTICLES, releasedAction.second});
                        mpiRequests.emplace_back();
                        assert(NbParticlesToReceive*size_particle_rhs < std::numeric_limits<int>::max());
                        AssertMpi(MPI_Isend(descriptor.results.get(), int(NbParticlesToReceive*size_particle_rhs),
                                            particles_utils::GetMpiType(real_number()), destProc, TAG_RESULT_PARTICLES,
                                            current_com, &mpiRequests.back()));
                    }
                    //////////////////////////////////////////////////////////////////////
                    /// Computation
                    //////////////////////////////////////////////////////////////////////
727
728
729
730
731
732
733
734
735
                    if(releasedAction.first == CHECK_PARTICLES){
                        NeighborDescriptor& descriptor = neigDescriptors[releasedAction.second];
                        assert(descriptor.toCompute != nullptr);
                        assert(descriptor.isRecv);
                        descriptor.positionsReceived = true;
                    }
                    //////////////////////////////////////////////////////////////////////
                    /// Computation
                    //////////////////////////////////////////////////////////////////////
736
737
738
                    if(releasedAction.first == RELEASE_BUFFER_PARTICLES){
                        NeighborDescriptor& descriptor = neigDescriptors[releasedAction.second];
                        assert(descriptor.toCompute != nullptr);
Berenger Bramas's avatar
Debug    
Berenger Bramas committed
739
                        assert(descriptor.isRecv);
740
741
742
743
744
                        descriptor.toCompute.release();
                    }
                    //////////////////////////////////////////////////////////////////////
                    /// Merge
                    //////////////////////////////////////////////////////////////////////
Berenger Bramas's avatar
Debug    
Berenger Bramas committed
745
                    if(releasedAction.first == MERGE_PARTICLES){
Berenger Bramas's avatar
Berenger Bramas committed
746
                        TIMEZONE("merge");
747
                        NeighborDescriptor& descriptor = neigDescriptors[releasedAction.second];
Berenger Bramas's avatar
Debug    
Berenger Bramas committed
748
                        assert(descriptor.isRecv == false);
749
                        assert(descriptor.toRecvAndMerge != nullptr);
Berenger Bramas's avatar
Debug    
Berenger Bramas committed
750
751
                        in_computer.template reduce_particles_rhs<size_particle_rhs>(&particles_current_rhs[particles_offset_layers[my_nb_cell_levels-descriptor.nbLevelsToExchange]*size_particle_rhs],
                                descriptor.toRecvAndMerge.get(), descriptor.nbParticlesToExchange);
752
753
754
755
756
757
758
759
760
761
762
                        descriptor.toRecvAndMerge.release();
                    }
                }
            }
        }

        assert(whatNext.size() == 0);
        assert(mpiRequests.size() == 0);

        // Compute self data
        for(const auto& iter_cell : my_tree){
Berenger Bramas's avatar
Berenger Bramas committed
763
            TIMEZONE("proceed-leaf");
764
765
766
767
768
            const std::vector<std::pair<partsize_t,partsize_t>>& intervals = iter_cell.second;

            for(size_t idx_1 = 0 ; idx_1 < intervals.size() ; ++idx_1){
                // self interval
                for(partsize_t idx_p1 = 0 ; idx_p1 < intervals[idx_1].second ; ++idx_p1){
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
//                    if(((inout_index_particles[(intervals[idx_1].first+idx_p1)] == 356))){// TODO
//                        printf("box %ld:\n", iter_cell.first);
//                        printf("intervals.size() %lu:\n", intervals.size());
//                        printf("intervals[idx_1].second %ld:\n", intervals[idx_1].second);
//                        printf("index %ld (%ld) pos %e %e %e\n",
//                               (intervals[idx_1].first+idx_p1), inout_index_particles[(intervals[idx_1].first+idx_p1)],
//                               particles_positions[(intervals[idx_1].first+idx_p1)*size_particle_positions + IDX_X],
//                               particles_positions[(intervals[idx_1].first+idx_p1)*size_particle_positions + IDX_Y],
//                               particles_positions[(intervals[idx_1].first+idx_p1)*size_particle_positions + IDX_Z]);
//                    }
//                    if(((inout_index_particles[(intervals[idx_1].first+idx_p1)] == 547))){// TODO
//                        printf("box %ld:\n", iter_cell.first);
//                        printf("intervals.size() %lu:\n", intervals.size());
//                        printf("intervals[idx_1].second %ld:\n", intervals[idx_1].second);
//                        printf("index %ld (%ld) pos %e %e %e\n",
//                               (intervals[idx_1].first+idx_p1), inout_index_particles[(intervals[idx_1].first+idx_p1)],
//                               particles_positions[(intervals[idx_1].first+idx_p1)*size_particle_positions + IDX_X],
//                               particles_positions[(intervals[idx_1].first+idx_p1)*size_particle_positions + IDX_Y],
//                               particles_positions[(intervals[idx_1].first+idx_p1)*size_particle_positions + IDX_Z]);
//                    }


791
792
793
794
795
796
                    for(partsize_t idx_p2 = idx_p1+1 ; idx_p2 < intervals[idx_1].second ; ++idx_p2){
                        const real_number dist_r2 = compute_distance_r2(particles_positions[(intervals[idx_1].first+idx_p1)*size_particle_positions + IDX_X],
                                                                        particles_positions[(intervals[idx_1].first+idx_p1)*size_particle_positions + IDX_Y],
                                                                        particles_positions[(intervals[idx_1].first+idx_p1)*size_particle_positions + IDX_Z],
                                                                        particles_positions[(intervals[idx_1].first+idx_p2)*size_particle_positions + IDX_X],
                                                                        particles_positions[(intervals[idx_1].first+idx_p2)*size_particle_positions + IDX_Y],
Berenger Bramas's avatar
Berenger Bramas committed
797
798
                                                                        particles_positions[(intervals[idx_1].first+idx_p2)*size_particle_positions + IDX_Z],
                                                                        0, 0, 0);
799
                        if(dist_r2 < cutoff_radius_compute*cutoff_radius_compute){
800
                            in_computer.template compute_interaction<size_particle_positions,size_particle_data,size_particle_rhs>(
801
                                                &particles_positions[(intervals[idx_1].first+idx_p1)*size_particle_positions],
802
                                                &particles_data[(intervals[idx_1].first+idx_p1)*size_particle_data],
803
804
                                                &particles_current_rhs[(intervals[idx_1].first+idx_p1)*size_particle_rhs],
                                                &particles_positions[(intervals[idx_1].first+idx_p2)*size_particle_positions],
805
                                                &particles_data[(intervals[idx_1].first+idx_p2)*size_particle_data],
806
                                                &particles_current_rhs[(intervals[idx_1].first+idx_p2)*size_particle_rhs],
Berenger Bramas's avatar
Berenger Bramas committed
807
                                                dist_r2, 0, 0, 0);
808
                        }
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829

//                        if(((inout_index_particles[(intervals[idx_1].first+idx_p1)] == 356)
//                                || inout_index_particles[(intervals[idx_1].first+idx_p2)] == 356)/*
//                                && ((inout_index_particles[(intervals[idx_1].first+idx_p1)] == 1832)
//                                    || inout_index_particles[(intervals[idx_1].first+idx_p2)] == 1832)
//                                && ((inout_index_particles[(intervals[idx_1].first+idx_p1)] == 547)
//                                    || inout_index_particles[(intervals[idx_1].first+idx_p2)] == 547)*/){// TODO
//                            printf("print between :\n");
//                            printf("index %ld (%ld) pos %e %e %e\n",
//                                   (intervals[idx_1].first+idx_p1), inout_index_particles[(intervals[idx_1].first+idx_p1)],
//                                   particles_positions[(intervals[idx_1].first+idx_p1)*size_particle_positions + IDX_X],
//                                   particles_positions[(intervals[idx_1].first+idx_p1)*size_particle_positions + IDX_Y],
//                                   particles_positions[(intervals[idx_1].first+idx_p1)*size_particle_positions + IDX_Z]);
//                            printf("index %ld (%ld) pos %e %e %e\n",
//                                   (intervals[idx_1].first+idx_p2),
//                                   inout_index_particles[(intervals[idx_1].first+idx_p2)],
//                                   particles_positions[(intervals[idx_1].first+idx_p2)*size_particle_positions + IDX_X],
//                                   particles_positions[(intervals[idx_1].first+idx_p2)*size_particle_positions + IDX_Y],
//                                   particles_positions[(intervals[idx_1].first+idx_p2)*size_particle_positions + IDX_Z]);
//                            printf("Radius = %e (%e)\n", sqrt(dist_r2), dist_r2);
//                        }
830
831
832
833
834
835
836
837
838
839
840
841
                    }
                }

                // with other interval
                for(size_t idx_2 = idx_1+1 ; idx_2 < intervals.size() ; ++idx_2){
                    for(partsize_t idx_p1 = 0 ; idx_p1 < intervals[idx_1].second ; ++idx_p1){
                        for(partsize_t idx_p2 = 0 ; idx_p2 < intervals[idx_2].second ; ++idx_p2){
                            const real_number dist_r2 = compute_distance_r2(particles_positions[(intervals[idx_1].first+idx_p1)*size_particle_positions + IDX_X],
                                                                            particles_positions[(intervals[idx_1].first+idx_p1)*size_particle_positions + IDX_Y],
                                                                            particles_positions[(intervals[idx_1].first+idx_p1)*size_particle_positions + IDX_Z],
                                                                            particles_positions[(intervals[idx_2].first+idx_p2)*size_particle_positions + IDX_X],
                                                                            particles_positions[(intervals[idx_2].first+idx_p2)*size_particle_positions + IDX_Y],
Berenger Bramas's avatar
Berenger Bramas committed
842
843
                                                                            particles_positions[(intervals[idx_2].first+idx_p2)*size_particle_positions + IDX_Z],
                                                                            0, 0, 0);
844
                            if(dist_r2 < cutoff_radius_compute*cutoff_radius_compute){
845
                                in_computer.template compute_interaction<size_particle_positions,size_particle_data,size_particle_rhs>(
846
                                                    &particles_positions[(intervals[idx_1].first+idx_p1)*size_particle_positions],
847
                                                    &particles_data[(intervals[idx_1].first+idx_p1)*size_particle_data],
848
849
                                                    &particles_current_rhs[(intervals[idx_1].first+idx_p1)*size_particle_rhs],
                                                    &particles_positions[(intervals[idx_2].first+idx_p2)*size_particle_positions],
850
                                                    &particles_data[(intervals[idx_2].first+idx_p2)*size_particle_data],
851
                                                    &particles_current_rhs[(intervals[idx_2].first+idx_p2)*size_particle_rhs],
Berenger Bramas's avatar
Berenger Bramas committed
852
                                                    dist_r2, 0, 0, 0);
853
                            }
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874

//                            if(((inout_index_particles[(intervals[idx_1].first+idx_p1)] == 356)
//                                    || inout_index_particles[(intervals[idx_2].first+idx_p2)] == 356)/*
//                                    && ((inout_index_particles[(intervals[idx_1].first+idx_p1)] == 547)
//                                        || inout_index_particles[(intervals[idx_2].first+idx_p2)] == 547)
//                                    && ((inout_index_particles[(intervals[idx_1].first+idx_p1)] == 1832)
//                                        || inout_index_particles[(intervals[idx_2].first+idx_p2)] == 1832)*/){// TODO
//                                printf("print between :\n");
//                                printf("index %ld (%ld) pos %e %e %e\n",
//                                       (intervals[idx_1].first+idx_p1), inout_index_particles[(intervals[idx_1].first+idx_p1)],
//                                       particles_positions[(intervals[idx_1].first+idx_p1)*size_particle_positions + IDX_X],
//                                       particles_positions[(intervals[idx_1].first+idx_p1)*size_particle_positions + IDX_Y],
//                                       particles_positions[(intervals[idx_1].first+idx_p1)*size_particle_positions + IDX_Z]);
//                                printf("index %ld (%ld) pos %e %e %e\n",
//                                       (intervals[idx_2].first+idx_p2),
//                                       inout_index_particles[(intervals[idx_2].first+idx_p2)],
//                                       particles_positions[(intervals[idx_2].first+idx_p2)*size_particle_positions + IDX_X],
//                                       particles_positions[(intervals[idx_2].first+idx_p2)*size_particle_positions + IDX_Y],
//                                       particles_positions[(intervals[idx_2].first+idx_p2)*size_particle_positions + IDX_Z]);
//                                printf("Radius = %e (%e)\n", sqrt(dist_r2), dist_r2);
//                            }
875
876
877
878
879
880
881
882
                        }
                    }
                }
            }


            const long int currenct_cell_idx = iter_cell.first;
            const std::vector<std::pair<partsize_t,partsize_t>>* neighbors[27];
883
            long int neighbors_indexes[27];
Berenger Bramas's avatar
Berenger Bramas committed
884
885
            std::array<real_number,3> shift[27];
            const int nbNeighbors = my_tree.getNeighbors(currenct_cell_idx, neighbors, neighbors_indexes, shift, false);
886

887
888
889
890
891
892
//            if(((currenct_cell_idx == 785))){// TODO
//                printf("box %ld:\n", iter_cell.first);
//                printf("intervals.size() %lu:\n", intervals.size());
//                printf("nbNeighbors %d\n",nbNeighbors);
//            }

893
894
895
            for(size_t idx_1 = 0 ; idx_1 < intervals.size() ; ++idx_1){
                // with other interval
                for(size_t idx_neighbor = 0 ; idx_neighbor < nbNeighbors ; ++idx_neighbor){
896
897
898
899
900
901
902
903
904
                    if(currenct_cell_idx < neighbors_indexes[idx_neighbor]){
                        for(size_t idx_2 = 0 ; idx_2 < (*neighbors[idx_neighbor]).size() ; ++idx_2){
                            for(partsize_t idx_p1 = 0 ; idx_p1 < intervals[idx_1].second ; ++idx_p1){
                                for(partsize_t idx_p2 = 0 ; idx_p2 < (*neighbors[idx_neighbor])[idx_2].second ; ++idx_p2){
                                    const real_number dist_r2 = compute_distance_r2(particles_positions[(intervals[idx_1].first+idx_p1)*size_particle_positions + IDX_X],
                                                                                    particles_positions[(intervals[idx_1].first+idx_p1)*size_particle_positions + IDX_Y],
                                                                                    particles_positions[(intervals[idx_1].first+idx_p1)*size_particle_positions + IDX_Z],
                                                                                    particles_positions[((*neighbors[idx_neighbor])[idx_2].first+idx_p2)*size_particle_positions + IDX_X],
                                                                                    particles_positions[((*neighbors[idx_neighbor])[idx_2].first+idx_p2)*size_particle_positions + IDX_Y],
Berenger Bramas's avatar
Berenger Bramas committed
905
906
                                                                                    particles_positions[((*neighbors[idx_neighbor])[idx_2].first+idx_p2)*size_particle_positions + IDX_Z],
                                                                                    shift[idx_neighbor][IDX_X], shift[idx_neighbor][IDX_Y], shift[idx_neighbor][IDX_Z]);
907
                                    if(dist_r2 < cutoff_radius_compute*cutoff_radius_compute){
908
                                        in_computer.template compute_interaction<size_particle_positions,size_particle_data,size_particle_rhs>(
909
                                                            &particles_positions[(intervals[idx_1].first+idx_p1)*size_particle_positions],
910
                                                            &particles_data[(intervals[idx_1].first+idx_p1)*size_particle_data],
911
912
                                                            &particles_current_rhs[(intervals[idx_1].first+idx_p1)*size_particle_rhs],
                                                            &particles_positions[((*neighbors[idx_neighbor])[idx_2].first+idx_p2)*size_particle_positions],
913
                                                            &particles_data[((*neighbors[idx_neighbor])[idx_2].first+idx_p2)*size_particle_data],
914
                                                            &particles_current_rhs[((*neighbors[idx_neighbor])[idx_2].first+idx_p2)*size_particle_rhs],
Berenger Bramas's avatar
Berenger Bramas committed
915
                                                            dist_r2, shift[idx_neighbor][IDX_X], shift[idx_neighbor][IDX_Y], shift[idx_neighbor][IDX_Z]);
916
                                    }
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937

//                                    if(((inout_index_particles[(intervals[idx_1].first+idx_p1)] == 356)
//                                            || inout_index_particles[((*neighbors[idx_neighbor])[idx_2].first+idx_p2)] == 356)/*
//                                        && (inout_index_particles[(intervals[idx_1].first+idx_p1)] == 547)
//                                            || inout_index_particles[((*neighbors[idx_neighbor])[idx_2].first+idx_p2)] == 547
//                                        && (inout_index_particles[(intervals[idx_1].first+idx_p1)] == 1832)
//                                            || inout_index_particles[((*neighbors[idx_neighbor])[idx_2].first+idx_p2)] == 1832*/){// TODO
//                                        printf("print between :\n");
//                                        printf("index %ld (%ld) pos %e %e %e\n",
//                                               (intervals[idx_1].first+idx_p1), inout_index_particles[(intervals[idx_1].first+idx_p1)],
//                                               particles_positions[(intervals[idx_1].first+idx_p1)*size_particle_positions + IDX_X],
//                                               particles_positions[(intervals[idx_1].first+idx_p1)*size_particle_positions + IDX_Y],
//                                               particles_positions[(intervals[idx_1].first+idx_p1)*size_particle_positions + IDX_Z]);
//                                        printf("index %ld (%ld) pos %e %e %e\n",
//                                               ((*neighbors[idx_neighbor])[idx_2].first+idx_p2),
//                                               inout_index_particles[((*neighbors[idx_neighbor])[idx_2].first+idx_p2)],
//                                               particles_positions[((*neighbors[idx_neighbor])[idx_2].first+idx_p2)*size_particle_positions + IDX_X],
//                                               particles_positions[((*neighbors[idx_neighbor])[idx_2].first+idx_p2)*size_particle_positions + IDX_Y],
//                                               particles_positions[((*neighbors[idx_neighbor])[idx_2].first+idx_p2)*size_particle_positions + IDX_Z]);
//                                        printf("Radius = %e (%e)\n", sqrt(dist_r2), dist_r2);
//                                    }
938
939
940
941
942
943
944
945
946
947
948
                                }
                            }
                        }
                    }
                }
            }
        }
    }
};

#endif