1 // Copyright (C) 2007-2020 CEA/DEN, EDF R&D
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 "CommInterface.hxx"
24 MPI_Datatype ParaTraits<double>::MPIDataType = MPI_DOUBLE;
26 MPI_Datatype ParaTraits<Int32>::MPIDataType = MPI_INT;
28 MPI_Datatype ParaTraits<Int64>::MPIDataType = MPI_LONG;
30 /*! \anchor CommInterface-det
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".
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.
42 As an example, the following code excerpt initializes a processor group made of the zero processor.
45 #include "CommInterface.hxx"
46 #include "ProcessorGroup.hxx"
48 int main(int argc, char** argv)
51 MPI_Init(&argc, &argv);
52 MEDCoupling::CommInterface comm_interface;
54 //setting up a processor group with proc 0
57 MEDCoupling::ProcessorGroup group(procs, comm_interface);
66 * Generalized AllGather collective communication.
67 * This method send input \a array to all procs.
69 void CommInterface::allGatherArrays(MPI_Comm comm, const DataArrayIdType *array, std::vector< MCAuto<DataArrayIdType> >& arraysOut) const
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)
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());
84 * Generalized AllGather collective communication.
85 * This method send input \a array to all procs.
87 int CommInterface::allGatherArrays(MPI_Comm comm, const DataArrayIdType *array, std::unique_ptr<mcIdType[]>& result, std::unique_ptr<mcIdType[]>& resultIndex) const
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);
103 void CommInterface::allToAllArrays(MPI_Comm comm, const std::vector< MCAuto<DataArrayDouble> >& arrays, std::vector< MCAuto<DataArrayDouble> >& arraysOut) const
105 this->allToAllArraysT<double>(comm,arrays,arraysOut);
108 void CommInterface::allToAllArrays(MPI_Comm comm, const std::vector< MCAuto<DataArrayDouble> >& arrays, MCAuto<DataArrayDouble>& arraysOut) const
110 std::unique_ptr<mcIdType[]> notUsed1;
112 this->allToAllArraysT2<double>(comm,arrays,arraysOut,notUsed1,notUsed2);
116 * Generalized AllToAll collective communication.
118 void CommInterface::allToAllArrays(MPI_Comm comm, const std::vector< MCAuto<DataArrayIdType> >& arrays, std::vector< MCAuto<DataArrayIdType> >& arraysOut) const
120 this->allToAllArraysT<mcIdType>(comm,arrays,arraysOut);