94 template<
class TContainerType >
96 const TContainerType& rContainer,
97 const std::vector<int>& rIdList,
103 std::unordered_map< int, GPType > global_pointers_list;
104 const int current_rank = rDataCommunicator.
Rank();
105 const int world_size = rDataCommunicator.
Size();
107 std::vector<int> remote_ids;
112 for(
const int id : rIdList ) {
113 const auto it = rContainer.find(
id);
115 if( it != rContainer.end()) {
116 if(ObjectIsLocal(*it, current_rank)) {
118 global_pointers_list.emplace(
id,GPType(&*it, current_rank));
122 remote_ids.push_back(
id);
126 remote_ids.push_back(
id);
131 for(
const int id : rIdList ) {
132 const auto it = rContainer.find(
id);
133 if( it != rContainer.end()) {
135 global_pointers_list.emplace(
id,GPType(&*it, current_rank));
143 std::vector<int> all_remote_ids;
144 std::vector< std::vector<int> > collected_remote_ids(world_size);
145 std::unordered_map< int, GPType > all_non_local_gp_map;
149 for(
int i=0;
i<world_size; ++
i) {
150 if(
i != master_rank) {
151 if(current_rank == master_rank) {
152 rDataCommunicator.
Recv(collected_remote_ids[
i],
i);
153 }
else if(current_rank ==
i) {
154 rDataCommunicator.
Send(remote_ids,master_rank);
157 if(current_rank == master_rank)
158 collected_remote_ids[
i] = remote_ids;
161 if(current_rank == master_rank) {
162 for(
const int id : collected_remote_ids[
i])
163 all_remote_ids.push_back(
id );
185 if(current_rank == master_rank) {
186 std::sort(all_remote_ids.begin(), all_remote_ids.end());
187 auto last = std::unique(all_remote_ids.begin(), all_remote_ids.end());
188 all_remote_ids.erase(last, all_remote_ids.end());
192 int number_of_all_remote_ids = all_remote_ids.size();
193 rDataCommunicator.
Broadcast(number_of_all_remote_ids,master_rank);
195 if(current_rank != master_rank)
196 all_remote_ids.resize(number_of_all_remote_ids);
199 rDataCommunicator.
Broadcast(all_remote_ids,master_rank);
203 for(
int i=0;
i<world_size; ++
i) {
204 if(
i != master_rank) {
205 if(current_rank == master_rank) {
206 std::unordered_map< int, GPType > recv_gps;
207 rDataCommunicator.
Recv(recv_gps,
i);
209 for(
auto& it : recv_gps)
210 all_non_local_gp_map.emplace(it.first, it.second);
211 }
else if(current_rank ==
i) {
212 auto non_local_gp_map = ComputeGpMap(rContainer, all_remote_ids, rDataCommunicator);
213 rDataCommunicator.
Send(non_local_gp_map,master_rank);
216 auto recv_gps = ComputeGpMap(rContainer, all_remote_ids, rDataCommunicator);
218 for(
auto& it : recv_gps)
219 all_non_local_gp_map.emplace(it.first, it.second);
225 for(
int i=0;
i<world_size; ++
i) {
226 if(
i != master_rank) {
227 if(current_rank == master_rank) {
228 auto gp_list = ExtractById(all_non_local_gp_map,collected_remote_ids[
i]);
231 rDataCommunicator.
Send(gp_list,
i);
232 }
else if(current_rank ==
i) {
233 std::unordered_map< int, GPType > gp_list;
234 rDataCommunicator.
Recv(gp_list, master_rank);
236 for(
auto& it : gp_list)
237 global_pointers_list.emplace(it.first, it.second);
240 auto gp_list = ExtractById(all_non_local_gp_map,collected_remote_ids[
i]);
242 for(
auto& it : gp_list)
243 global_pointers_list.emplace(it.first, it.second);
247 return global_pointers_list;
256 template<
class TContainerType >
258 const TContainerType& rContainer,
263 std::vector<int> local_id_list;
264 local_id_list.reserve(rContainer.size());
265 for (
const auto& r_entity : rContainer) {
266 local_id_list.push_back(r_entity.Id());
279 template<
class TContainerType >
281 const TContainerType& rContainer,
286 const int world_size = rDataCommunicator.
Size();
289 const std::size_t number_of_entities = rContainer.size();
292 std::vector<int> number_of_entities_per_partition(world_size);
293 std::vector<int> send_number_of_entities_per_partition(1, number_of_entities);
294 rDataCommunicator.AllGather(send_number_of_entities_per_partition, number_of_entities_per_partition);
297 std::vector<int> global_id_list, local_id_list;
298 local_id_list.reserve(number_of_entities);
299 for (
const auto& r_entity : rContainer) {
300 local_id_list.push_back(r_entity.Id());
304 std::vector<int> recv_sizes(number_of_entities_per_partition);
305 int message_size = 0;
306 std::vector<int> recv_offsets(world_size, 0);
307 for (
int i_rank = 0; i_rank < world_size; i_rank++) {
308 recv_offsets[i_rank] = message_size;
309 message_size += recv_sizes[i_rank];
311 global_id_list.resize(message_size);
314 rDataCommunicator.AllGatherv(local_id_list, global_id_list, recv_sizes, recv_offsets);
328 template<
class TContainerType >
330 const TContainerType& rContainer,
331 const std::vector<int>& rIdList,
337 const int current_rank = rDataCommunicator.
Rank();
341 result.
reserve(rIdList.size());
342 for(
unsigned int i=0;
i<rIdList.size(); ++
i) {
343 auto it = global_pointers_list.find(rIdList[
i]);
344 if(it != global_pointers_list.end())
347 KRATOS_ERROR <<
"The id " << rIdList[
i] <<
" was not found for processor " << current_rank << std::endl;
375 virtual std::string
Info()
const
377 std::stringstream buffer;
378 buffer <<
"GlobalPointerUtilities" ;
385 rOStream <<
"GlobalPointerUtilities";
439 static bool ObjectIsLocal(
const GeometricalObject& rGeometricalObject,
const int CurrentRank)
450 static bool ObjectIsLocal(
const Node& rNode,
const int CurrentRank)
452 return rNode.FastGetSolutionStepValue(PARTITION_INDEX) == CurrentRank;
465 template<
class GPType >
466 static std::unordered_map< int, GPType > ExtractById(
467 std::unordered_map< int, GPType >& rGPList,
468 const std::vector<int>& rIds)
470 std::unordered_map< int, GPType > extracted_list;
472 auto gp = rGPList[id];
473 extracted_list[id] = gp;
475 return extracted_list;
485 template<
class TContainerType >
486 static std::unordered_map< int, GlobalPointer<typename TContainerType::value_type> > ComputeGpMap(
487 const TContainerType& rContainer,
488 const std::vector<int>& rIds,
489 const DataCommunicator& rDataCommunicator)
491 const int current_rank = rDataCommunicator.Rank();
492 std::unordered_map< int, GlobalPointer<typename TContainerType::value_type> > extracted_list;
494 if(rDataCommunicator.IsDistributed()) {
497 for(
auto id : rIds) {
498 const auto it = rContainer.find(
id);
500 if( it != rContainer.end()) {
502 if(ObjectIsLocal(*it, current_rank)){
503 extracted_list.emplace(
id, GlobalPointer<typename TContainerType::value_type>(&*it, current_rank));
509 for(
auto id : rIds) {
510 const auto it = rContainer.find(
id);
512 if( it != rContainer.end()) {
514 extracted_list.emplace(
id, GlobalPointer<typename TContainerType::value_type>(&*it, current_rank));
518 return extracted_list;
568 rOStream << std::endl;
Serial (do-nothing) version of a wrapper class for MPI communication.
Definition: data_communicator.h:318
void Recv(TObject &rRecvObject, const int RecvSource, const int RecvTag=0) const
Exchange data with other ranks.
Definition: data_communicator.h:573
virtual int Size() const
Get the parallel size of this DataCommunicator.
Definition: data_communicator.h:597
virtual int Rank() const
Get the parallel rank for this DataCommunicator.
Definition: data_communicator.h:587
void Broadcast(TObject &rBroadcastObject, const int SourceRank) const
Synchronize a buffer to the value held by the broadcasting rank.
Definition: data_communicator.h:473
void Send(const TObject &rSendValues, const int SendDestination, const int SendTag=0) const
Exchange data with other ranks.
Definition: data_communicator.h:559
virtual bool IsDistributed() const
Check whether this DataCommunicator is aware of parallelism.
Definition: data_communicator.h:606
This defines the geometrical object, base definition of the element and condition entities.
Definition: geometrical_object.h:58
This class is a wrapper for a pointer to a data that is located in a different rank.
Definition: global_pointer.h:44
This class is used to manage global pointers. Overall, the GlobalPointerUtilities class provides a us...
Definition: global_pointer_utilities.h:67
virtual void PrintInfo(std::ostream &rOStream) const
Print information about this object.
Definition: global_pointer_utilities.h:383
KRATOS_CLASS_POINTER_DEFINITION(GlobalPointerUtilities)
Pointer definition of GlobalPointerUtilities.
virtual void PrintData(std::ostream &rOStream) const
Print object's data.
Definition: global_pointer_utilities.h:389
virtual std::string Info() const
Turn back information as a string.
Definition: global_pointer_utilities.h:375
static GlobalPointersVector< typename TContainerType::value_type > GlobalRetrieveGlobalPointers(const TContainerType &rContainer, const DataCommunicator &rDataCommunicator)
Retrieve global pointers for entities in container, given a data communicator. All entities are retri...
Definition: global_pointer_utilities.h:280
virtual ~GlobalPointerUtilities()
Destructor.
Definition: global_pointer_utilities.h:84
static GlobalPointersVector< typename TContainerType::value_type > LocalRetrieveGlobalPointers(const TContainerType &rContainer, const DataCommunicator &rDataCommunicator)
Retrieve global pointers for entities in container, given a data communicator. Only local entities ar...
Definition: global_pointer_utilities.h:257
static std::unordered_map< int, GlobalPointer< typename TContainerType::value_type > > RetrieveGlobalIndexedPointersMap(const TContainerType &rContainer, const std::vector< int > &rIdList, const DataCommunicator &rDataCommunicator)
Retrieves a map of global pointers corresponding to the given entity ids, where the global pointers p...
Definition: global_pointer_utilities.h:95
static GlobalPointersVector< typename TContainerType::value_type > RetrieveGlobalIndexedPointers(const TContainerType &rContainer, const std::vector< int > &rIdList, const DataCommunicator &rDataCommunicator)
Retrieve global indexed pointers from container and data communicator.
Definition: global_pointer_utilities.h:329
GlobalPointerUtilities()
Default constructor.
Definition: global_pointer_utilities.h:80
This class is a vector which stores global pointers.
Definition: global_pointers_vector.h:61
void push_back(TPointerType x)
Definition: global_pointers_vector.h:322
void reserve(int dim)
Definition: global_pointers_vector.h:375
#define KRATOS_ERROR
Definition: exception.h:161
REF: G. R. Cowper, GAUSSIAN QUADRATURE FORMULAS FOR TRIANGLES.
Definition: mesh_condition.cpp:21
std::istream & operator>>(std::istream &rIStream, LinearMasterSlaveConstraint &rThis)
input stream function
std::ostream & operator<<(std::ostream &rOStream, const LinearMasterSlaveConstraint &rThis)
output stream function
Definition: linear_master_slave_constraint.h:432
integer i
Definition: TensorModule.f:17