p2p_ghost_collision_base.hpp 8.4 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/******************************************************************************
*                                                                             *
*  Copyright 2019 Max Planck Institute for Dynamics and Self-Organization     *
*                                                                             *
*  This file is part of bfps.                                                 *
*                                                                             *
*  bfps is free software: you can redistribute it and/or modify               *
*  it under the terms of the GNU General Public License as published          *
*  by the Free Software Foundation, either version 3 of the License,          *
*  or (at your option) any later version.                                     *
*                                                                             *
*  bfps is distributed in the hope that it will be useful,                    *
*  but WITHOUT ANY WARRANTY; without even the implied warranty of             *
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
*  GNU General Public License for more details.                               *
*                                                                             *
*  You should have received a copy of the GNU General Public License          *
*  along with bfps.  If not, see <http://www.gnu.org/licenses/>               *
*                                                                             *
* Contact: Cristian.Lalescu@ds.mpg.de                                         *
*                                                                             *
******************************************************************************/
#ifndef P2P_GHOST_COLLISION_BASE_HPP
#define P2P_GHOST_COLLISION_BASE_HPP

#include <cstring>
#include <set>
#include <utility>
#include <vector>
template < class partsize_t>
std::vector<partsize_t> pairs2vec(std::set <std::pair <partsize_t,partsize_t>> ID_pairs){
  std::vector<partsize_t> v(2*ID_pairs.size());
  int i = 0;
  for(auto p: ID_pairs)
  {
    v[2*i] = p.first;
    v[2*i+1] = p.second;
    i++;
  }
  return v;
}

template < class partsize_t>
std::set <std::pair <partsize_t,partsize_t>> vec2pairs(std::vector<partsize_t> v){
  std::set <std::pair <partsize_t,partsize_t>> ID_pairs;
  assert(v.size()%2 == 0);
47
  for(int i=0; i < int(v.size())/2; i++)
48
49
50
51
52
53
54
55
56
57
58
59
    {
      //DEBUG_MSG((std::to_string(v[2*i])+" "+std::to_string(v[2*i+1])+"\n").c_str());
      std::pair <partsize_t, partsize_t> single_collision_pair(v[2*i],v[2*i+1]);
      ID_pairs.insert(single_collision_pair);
    }
  return ID_pairs;
}

template <class partsize_t>
void print_pair_vec(std::vector<partsize_t> vec)
{
    assert(vec.size() % 2 == 0);
60
    for (int i = 0; i < int(vec.size())/2; i++)
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
        DEBUG_MSG("in print_pair_vec, pair %d is (%ld,%ld)\n", i, vec[2*i], vec[2*i+1]);
}

template <class partsize_t>
void print_pair_set(std::set<std::pair<partsize_t, partsize_t>> ID_pairs)
{
    int i = 0;
    for (auto p: ID_pairs)
    {
        DEBUG_MSG("in print_pair_set, pair %d is (%ld,%ld)\n", i, p.first, p.second);
        i++;
    }
}


template <class real_number, class partsize_t>
class p2p_ghost_collision_base
{
    protected:
        bool synchronisation;
        std::set <std::pair <partsize_t, partsize_t>> collision_pairs_local;
        std::set <std::pair <partsize_t, partsize_t>> collision_pairs_global;
        std::vector <partsize_t> collision_pairs_vec_global;

public:
    p2p_ghost_collision_base(): synchronisation(false) {}

    // Copy constructor use a counter set to zero
    p2p_ghost_collision_base(const p2p_ghost_collision_base&): synchronisation(false) {}

    template <int size_particle_rhs>
    void init_result_array(real_number /*rhs*/[], const partsize_t /*nbParticles*/) const{
    }

    template <int size_particle_rhs>
    void reduce_particles_rhs(real_number /*rhs_dst*/[], const real_number /*rhs_src*/[], const partsize_t /*nbParticles*/) const{
    }

    // the following declaration is illegal under the C++ standard,
    // but it should be there. therefore I'm leaving it here in spirit.
    //
    // template <int size_particle_positions, int size_particle_rhs>
    //virtual void compute_interaction(const partsize_t idx_part1,
    //                         const real_number /*pos_part1*/[],
    //        			     real_number /*rhs_part1*/[],
    //                         const partsize_t idx_part2,
    //                         const real_number /*pos_part2*/[],
	//		                 real_number /*rhs_part2*/[],
    //                         const real_number /*dist_pow2*/,
    //        			     const real_number /*cutoff*/,
    //                         const real_number /*xshift_coef*/,
	//		                 const real_number /*yshift_coef*/,
    //        			     const real_number /*zshift_coef*/) = 0;

    void merge(const p2p_ghost_collision_base& other){
        std::set <std::pair <partsize_t, partsize_t>> new_collision_pairs;
        std::set_union(collision_pairs_local.begin(), collision_pairs_local.end(),
                       other.collision_pairs_local.begin(), other.collision_pairs_local.end(),
                       std::inserter(new_collision_pairs, new_collision_pairs.begin()));
        collision_pairs_local = new_collision_pairs;
    }

    void MPI_merge(MPI_Comm comm, int myrank, int nprocs) {
        ///collect collision pairs
        std::vector <partsize_t> collision_pairs_vec_local =  pairs2vec(this->collision_pairs_local);
        DEBUG_MSG("transformed local collision pairs\n");
        if(this->collision_pairs_local.size()>0)
        {
            DEBUG_MSG(("local IDs of vec: "+std::to_string(collision_pairs_vec_local[0])+" "+std::to_string(collision_pairs_vec_local[1])+"\n").c_str());
        }
        int * recvcounts;
        int vec_len_local = collision_pairs_vec_local.size();
        int * displ;
        if (myrank == 0)
        {
            recvcounts = new int[nprocs];
        }
        MPI_Gather(&vec_len_local, 1, MPI_INT,
               recvcounts, 1, MPI_INT,
               0, comm);
        DEBUG_MSG("after gather\n");
        if (myrank == 0)
        {
            int nbElements;
            displ = new int[nprocs];
            displ[0]=0;
            for (int i=1; i<nprocs; i++)
            {
                displ[i]=displ[i-1]+recvcounts[i-1];
            }
            nbElements = displ[nprocs-1] + recvcounts[nprocs-1];
            this->collision_pairs_vec_global.resize(nbElements);
            DEBUG_MSG(("nbElements: "+std::to_string(nbElements)+"\n").c_str());
        }
        MPI_Gatherv(&collision_pairs_vec_local.front(),
                vec_len_local,
                MPI_LONG_LONG_INT,
                &this->collision_pairs_vec_global.front(),
                recvcounts,
                displ,
                MPI_LONG_LONG_INT,
                0,
                comm);
        if(myrank == 0)
        {
            this->collision_pairs_global = vec2pairs(this->collision_pairs_vec_global);
            print_pair_vec(this->collision_pairs_vec_global);
            print_pair_set(this->collision_pairs_global);
        }
        this->synchronisation = true;
        // free root rank memory
        if (myrank == 0)
        {
            delete[] recvcounts;
            delete[] displ;
        }
    }

    constexpr static bool isEnable() {
180
        return true;//interaction has to be turned off here by hand.
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
    }

    //get_collision_counter() will only give the correct result on rank 0
    long int get_collision_counter(MPI_Comm comm, int myrank, int nprocs) {
	if(synchronisation==false){
	   MPI_merge(comm, myrank, nprocs);
        }
	return this->collision_pairs_global.size();
    }

    //get_collision_pairs() will only give global pairs on rank 0
    std::set <std::pair <partsize_t,partsize_t>> get_collision_pairs(MPI_Comm comm, int myrank, int nprocs) {
        if(synchronisation==false){
	   MPI_merge(comm, myrank, nprocs);
        }
        return this->collision_pairs_global;
    }

    std::vector <partsize_t> get_collision_pairs_vec(MPI_Comm comm, int myrank, int nprocs) {
        if(synchronisation==false){
	   MPI_merge(comm, myrank, nprocs);
        }
        return this->collision_pairs_vec_global;
    }


    void reset_collision_pairs(){
        this->collision_pairs_local.clear();
        this->collision_pairs_global.clear();
	this->collision_pairs_vec_global.clear();
	this->synchronisation = false;
    }
};


#endif // P2P_GHOST_COLLISION_BASE_HPP