Commit 6db00800 authored by Cristian Lalescu's avatar Cristian Lalescu
Browse files

uses std::vector instead of std::set for collisions

parent 442d04a3
Pipeline #89711 passed with stages
in 18 minutes and 50 seconds
......@@ -24,35 +24,10 @@
#define P2P_GHOST_COLLISIONS_HPP
#include <cstring>
#include <set>
#include <utility>
#include <vector>
#include <cmath>
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);
for(int i=0; i < int(v.size())/2; i++)
{
//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)
......@@ -62,17 +37,6 @@ void print_pair_vec(std::vector<partsize_t> vec)
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_collisions
......@@ -91,17 +55,39 @@ class p2p_ghost_collisions
// description for disks:
double disk_width;
bool isActive;
void add_colliding_pair(partsize_t idx_part1, partsize_t idx_part2)
{
//DEBUG_MSG("thread %d, compute_interaction %ld, computer %ld, with collision_pairs_local size %d\n",
// omp_get_thread_num(),
// &this->collision_pairs_local,
// this,
// this->collision_pairs_local.size());
if (idx_part1 < idx_part2) // only store unique pairs of colliding particles
{
this->collision_pairs_local.push_back(idx_part1);
this->collision_pairs_local.push_back(idx_part2);
}
//DEBUG_MSG("inside compute interaction idx_part1 = %ld and idx_part2 = %ld\n", idx_part1, idx_part2);
assert(idx_part1!=idx_part2);
}
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;
std::vector <partsize_t> collision_pairs_local;
std::vector <partsize_t> collision_pairs_global;
public:
p2p_ghost_collisions(): current_particle_shape(SPHERE), cylinder_width(1.0), cylinder_length(1.0), disk_width(1.0), isActive(true), synchronisation(false) {}
// Copy constructor use a counter set to zero
p2p_ghost_collisions(const p2p_ghost_collisions&): synchronisation(false) {}
p2p_ghost_collisions(const p2p_ghost_collisions& src)
{
this->current_particle_shape = src.current_particle_shape;
this->cylinder_width = src.cylinder_width;
this->cylinder_length = src.cylinder_length;
this->disk_width = src.disk_width;
this->isActive = src.isActive;
this->synchronisation = src.synchronisation;
}
template <int size_particle_rhs>
void init_result_array(real_number /*rhs*/[], const partsize_t /*nbParticles*/) const{
......@@ -112,23 +98,22 @@ public:
}
void merge(const p2p_ghost_collisions& 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;
//std::vector <std::array <partsize_t, 2>> 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;
collision_pairs_local.insert(
collision_pairs_local.end(),
other.collision_pairs_local.begin(),
other.collision_pairs_local.end());
}
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());
}
DEBUG_MSG("p2p_ghost_collisions::MPI_merge\n");
int * recvcounts = NULL;
int vec_len_local = collision_pairs_vec_local.size();
int vec_len_local = collision_pairs_local.size();
int * displ = NULL;
if (myrank == 0)
{
......@@ -148,13 +133,13 @@ public:
displ[i]=displ[i-1]+recvcounts[i-1];
}
nbElements = displ[nprocs-1] + recvcounts[nprocs-1];
this->collision_pairs_vec_global.resize(nbElements);
this->collision_pairs_global.resize(nbElements);
DEBUG_MSG(("nbElements: "+std::to_string(nbElements)+"\n").c_str());
}
MPI_Gatherv(&collision_pairs_vec_local.front(),
MPI_Gatherv(&collision_pairs_local.front(),
vec_len_local,
MPI_LONG_LONG_INT,
&this->collision_pairs_vec_global.front(),
&this->collision_pairs_global.front(),
recvcounts,
displ,
MPI_LONG_LONG_INT,
......@@ -162,8 +147,6 @@ public:
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;
......@@ -185,7 +168,7 @@ public:
}
//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) {
std::vector <partsize_t> get_collision_pairs(MPI_Comm comm, int myrank, int nprocs) {
if(synchronisation==false)
{
MPI_merge(comm, myrank, nprocs);
......@@ -193,22 +176,14 @@ public:
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;
}
template <int size_particle_positions, int size_particle_rhs>
void compute_interaction(const partsize_t idx_part1,
const real_number pos_part1[],
......@@ -225,10 +200,7 @@ public:
{
case SPHERE:
{
std::pair <partsize_t, partsize_t> single_collision_pair(idx_part1, idx_part2);
this->collision_pairs_local.insert(single_collision_pair);
//DEBUG_MSG("inside compute interaction idx_part1 = %ld and idx_part2 = %ld\n", idx_part1, idx_part2);
assert(idx_part1!=idx_part2);
this->add_colliding_pair(idx_part1, idx_part2);
}
break;
case CYLINDER:
......@@ -299,12 +271,7 @@ public:
}
/* If cylinders overlap count it as a collision */
if( min_distance<=this->cylinder_width )
{
std::pair <partsize_t, partsize_t> single_collision_pair(idx_part1, idx_part2);
this->collision_pairs_local.insert(single_collision_pair);
//DEBUG_MSG("inside compute interaction idx_part1 = %ld and idx_part2 = %ld\n", idx_part1, idx_part2);
assert(idx_part1!=idx_part2);
}
this->add_colliding_pair(idx_part1, idx_part2);
}
}
break;
......@@ -378,9 +345,7 @@ public:
if (( t1[1]>=t2[0] ) and
( t2[1]>=t1[0] ))
{
std::pair <partsize_t, partsize_t> single_collision_pair(idx_part1, idx_part2);
this->collision_pairs_local.insert(single_collision_pair);
assert(idx_part1!=idx_part2);
this->add_colliding_pair(idx_part1, idx_part2);
return;
}
}
......
......@@ -64,7 +64,11 @@ class tracer_with_collision_counter_rhs: public tracer_rhs<rnumber, be, tt>
pset.copy_state_tofrom(
additional_data[additional_data.size()-1].get(),
result);
pset.template applyP2PKernel<3, p2p_ghost_collisions<abstract_particle_set::particle_rnumber, abstract_particle_set::partsize_t>>(
this->p2p_gc.reset_collision_pairs();
pset.template applyP2PKernel<
3,
p2p_ghost_collisions<abstract_particle_set::particle_rnumber,
abstract_particle_set::partsize_t>>(
this->p2p_gc,
additional_data);
// copy shuffled rhs values
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment