]> SALOME platform Git repositories - tools/medcoupling.git/blob - src/ParaMEDMEM/CommInterface.cxx
Salome HOME
ParaUMesh.redistributeCells implementation.
[tools/medcoupling.git] / src / ParaMEDMEM / CommInterface.cxx
1 // Copyright (C) 2007-2020  CEA/DEN, EDF R&D
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 "CommInterface.hxx"
21
22 namespace MEDCoupling
23 {
24   MPI_Datatype ParaTraits<double>::MPIDataType = MPI_DOUBLE;
25
26   MPI_Datatype ParaTraits<Int32>::MPIDataType = MPI_INT;
27
28   MPI_Datatype ParaTraits<Int64>::MPIDataType = MPI_LONG;
29
30   /*! \anchor CommInterface-det
31      \class CommInterface
32
33     The class \a CommInterface is the gateway to the MPI library.
34     It is a wrapper around all MPI calls, thus trying to abstract the rest of the code from using the direct MPI API
35     (but this is not strictly respected overall in practice ...). It is used in all
36     the \ref parallel "DEC related classes".
37
38     It is typically instantiated after the MPI_Init() call in a program and is afterwards passed as a
39     parameter to the constructors of various \ref parallel "parallel objects" so that they access the
40     MPI library via this common interface.
41
42     As an example, the following code excerpt initializes a processor group made of the zero processor.
43
44     \verbatim
45     #include "CommInterface.hxx"
46     #include "ProcessorGroup.hxx"
47
48     int main(int argc, char** argv)
49     {
50     //initialization
51     MPI_Init(&argc, &argv);
52     MEDCoupling::CommInterface comm_interface;
53
54     //setting up a processor group with proc 0
55     set<int> procs;
56     procs.insert(0);
57     MEDCoupling::ProcessorGroup group(procs, comm_interface);
58
59     //cleanup
60     MPI_Finalize();
61     }
62     \endverbatim
63   */
64
65   /*!
66    * Generalized AllGather collective communication.
67    * This method send input \a array to all procs.
68    */
69   void CommInterface::allGatherArrays(MPI_Comm comm, const DataArrayIdType *array, std::vector< MCAuto<DataArrayIdType> >& arraysOut) const
70   {
71     std::unique_ptr<mcIdType[]> result, resultIndex;
72     int size(this->allGatherArrays(comm,array,result,resultIndex));
73     arraysOut.resize(size);
74     for(int i = 0 ; i < size ; ++i)
75     {
76       arraysOut[i] = DataArrayIdType::New();
77       mcIdType nbOfEltPack(resultIndex[i+1]-resultIndex[i]);
78       arraysOut[i]->alloc(nbOfEltPack,1);
79       std::copy(result.get()+resultIndex[i],result.get()+resultIndex[i+1],arraysOut[i]->getPointer());
80     }
81   }
82
83   /*!
84    * Generalized AllGather collective communication.
85    * This method send input \a array to all procs.
86    */
87   int CommInterface::allGatherArrays(MPI_Comm comm, const DataArrayIdType *array, std::unique_ptr<mcIdType[]>& result, std::unique_ptr<mcIdType[]>& resultIndex) const
88   {
89     int size;
90     this->commSize(comm,&size);
91     std::unique_ptr<mcIdType[]> nbOfElems(new mcIdType[size]);
92     mcIdType nbOfCellsRequested(array->getNumberOfTuples());
93     this->allGather(&nbOfCellsRequested,1,MPI_ID_TYPE,nbOfElems.get(),1,MPI_ID_TYPE,comm);
94     mcIdType nbOfCellIdsSum(std::accumulate(nbOfElems.get(),nbOfElems.get()+size,0));
95     result.reset(new mcIdType[nbOfCellIdsSum]);
96     std::unique_ptr<int[]> nbOfElemsInt( CommInterface::ToIntArray<mcIdType>(nbOfElems,size) );
97     std::unique_ptr<int[]> offsetsIn( CommInterface::ComputeOffset(nbOfElemsInt,size) );
98     this->allGatherV(array->begin(),nbOfCellsRequested,MPI_ID_TYPE,result.get(),nbOfElemsInt.get(),offsetsIn.get(),MPI_ID_TYPE,comm);
99     resultIndex = ComputeOffsetFull<mcIdType>(nbOfElems,size);
100     return size;
101   }
102
103   void CommInterface::allToAllArrays(MPI_Comm comm, const std::vector< MCAuto<DataArrayDouble> >& arrays, std::vector< MCAuto<DataArrayDouble> >& arraysOut) const
104   {
105     this->allToAllArraysT<double>(comm,arrays,arraysOut);
106   }
107
108   void CommInterface::allToAllArrays(MPI_Comm comm, const std::vector< MCAuto<DataArrayDouble> >& arrays, MCAuto<DataArrayDouble>& arraysOut) const
109   {
110     std::unique_ptr<mcIdType[]> notUsed1;
111     mcIdType notUsed2;
112     this->allToAllArraysT2<double>(comm,arrays,arraysOut,notUsed1,notUsed2);
113   }
114
115   /*!
116   * Generalized AllToAll collective communication.
117   */
118   void CommInterface::allToAllArrays(MPI_Comm comm, const std::vector< MCAuto<DataArrayIdType> >& arrays, std::vector< MCAuto<DataArrayIdType> >& arraysOut) const
119   {
120     this->allToAllArraysT<mcIdType>(comm,arrays,arraysOut);
121   }
122
123 }