1 // Copyright (C) 2007-2023 CEA, EDF
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "ByStringMPIProcessorGroup.hxx"
33 \class ByStringMPIProcessorGroup
35 The ByStringMPIProcessorGroup implements a derived version of MPIProcessorGroup.
37 Groups are formed from MPI ranks with the same simCodeTag. Two trivial cases:
38 - All simCodeTag are equal, then one group is formed from all mpi ranks in the communicator
39 - All simCodeTag are different, then n-MPI groups are formed.
43 /*! Build and return a map between different simCodeTag and the set of MPI ranks ids (based on the passed communicator) grouped by the same identifier.
44 \param interface CommInterface object giving access to the MPI communication layer
45 \param simCodeTag the string identifiying the tag for the group.
46 \param world_comm mpi communicator
47 \return Map relating unique simCodeTag and the group of MPI ranks ids belowing to that group
49 static std::map<std::string,std::set<int>> DefineSetIdByStringName( const CommInterface& interface, const std::string& simCodeTag, const MPI_Comm& world_comm )
53 interface.commSize(world_comm,&size_world);
54 interface.commRank(world_comm,&rank_world);
56 std::map<std::string,std::set<int>> myRanksSet;
58 std::vector<int> displacement(size_world, 0 );
59 std::vector<int> words_size(size_world);
61 int stringSize = (int) simCodeTag.size();
62 interface.allGather( &stringSize, 1, MPI_INT, words_size.data(), 1, MPI_INT, world_comm );
64 for (size_t rank = 1; rank < words_size.size(); rank++)
65 displacement[ rank ] = words_size[ rank - 1 ] + displacement[ rank - 1 ];
67 char globalnames[displacement[size_world-1]];
69 interface.allGatherV( simCodeTag.c_str(), stringSize, MPI_CHAR, &globalnames,
70 words_size.data(), displacement.data(), MPI_CHAR, world_comm );
72 for (size_t rank = 0; rank < size_world; rank++)
74 std::string strByRank( &globalnames[displacement[ rank ]], words_size[ rank ] );
75 myRanksSet[ strByRank ].insert( (int)rank );
81 * Creates a processor group that is based on all the
82 processors of MPI_COMM_WORLD .This routine must be called by all processors in MPI_COMM_WORLD.
83 \param interface CommInterface object giving access to the MPI
86 ByStringMPIProcessorGroup::ByStringMPIProcessorGroup(const CommInterface& interface):
87 MPIProcessorGroup(interface)
91 /*! Creates a processor group based in the simCodeTag passed.
93 \param interface CommInterface object giving access to the MPI
95 \param simCodeTag the string identifiying the tag for the group.
96 \param world_comm mpi communicator
98 ByStringMPIProcessorGroup::ByStringMPIProcessorGroup(const CommInterface& interface, const std::string& simCodeTag, const MPI_Comm& world_comm ):
99 MPIProcessorGroup(interface, DefineSetIdByStringName( interface, simCodeTag, world_comm ), simCodeTag, world_comm )
103 ByStringMPIProcessorGroup::ByStringMPIProcessorGroup(const ByStringMPIProcessorGroup& other):
104 MPIProcessorGroup(other)
108 ByStringMPIProcessorGroup::~ByStringMPIProcessorGroup()
112 ByStringMPIProcessorGroup *ByStringMPIProcessorGroup::deepCopy() const
114 return new ByStringMPIProcessorGroup(*this);