]> SALOME platform Git repositories - tools/medcoupling.git/blob - src/ParaMEDMEM/ByStringMPIProcessorGroup.cxx
Salome HOME
[bos #38048] [EDF] (2023-T3) PARAMEDMEM Ergonomy.
[tools/medcoupling.git] / src / ParaMEDMEM / ByStringMPIProcessorGroup.cxx
1 // Copyright (C) 2007-2023  CEA, EDF
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "ByStringMPIProcessorGroup.hxx"
21
22 #include <iostream>
23 #include <set>
24 #include <algorithm>
25 #include "mpi.h"
26
27 using namespace std;
28
29
30 namespace MEDCoupling
31 {
32   /*!
33    \class ByStringMPIProcessorGroup
34
35    The ByStringMPIProcessorGroup implements a derived version of MPIProcessorGroup. 
36
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.
40   */
41
42
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
48   */
49   static std::map<std::string,std::set<int>> DefineSetIdByStringName( const CommInterface& interface, const std::string& simCodeTag, const MPI_Comm& world_comm )
50   {
51     int size_world;
52     int rank_world;
53     interface.commSize(world_comm,&size_world);
54     interface.commRank(world_comm,&rank_world);
55
56     std::map<std::string,std::set<int>> myRanksSet;
57
58     std::vector<int> displacement(size_world, 0 );
59     std::vector<int> words_size(size_world);
60
61     int stringSize = (int) simCodeTag.size();
62     interface.allGather( &stringSize, 1, MPI_INT, words_size.data(), 1, MPI_INT, world_comm );
63
64     for (size_t rank = 1; rank < words_size.size(); rank++)
65       displacement[ rank ] = words_size[ rank - 1 ] + displacement[ rank - 1 ];
66
67     char globalnames[displacement[size_world-1]];
68
69     interface.allGatherV( simCodeTag.c_str(), stringSize, MPI_CHAR, &globalnames, 
70                           words_size.data(), displacement.data(), MPI_CHAR, world_comm );
71
72     for (size_t rank = 0; rank < size_world; rank++)
73     {
74       std::string strByRank( &globalnames[displacement[ rank ]], words_size[ rank ] );
75       myRanksSet[ strByRank ].insert( (int)rank );
76     }
77     return myRanksSet;
78   }
79
80   /*! 
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
84    communication layer
85   */
86   ByStringMPIProcessorGroup::ByStringMPIProcessorGroup(const CommInterface& interface):
87     MPIProcessorGroup(interface)
88   {
89   }
90
91   /*! Creates a processor group based in the simCodeTag passed.
92
93     \param interface CommInterface object giving access to the MPI
94     communication layer
95     \param simCodeTag the string identifiying the tag for the group.
96     \param world_comm mpi communicator
97   */
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 )
100   {
101   } 
102
103   ByStringMPIProcessorGroup::ByStringMPIProcessorGroup(const ByStringMPIProcessorGroup& other):
104     MPIProcessorGroup(other)
105   {
106   }
107
108   ByStringMPIProcessorGroup::~ByStringMPIProcessorGroup()
109   {
110   }
111
112   ByStringMPIProcessorGroup *ByStringMPIProcessorGroup::deepCopy() const
113   {
114     return new ByStringMPIProcessorGroup(*this);
115   }
116
117 }