1 // Copyright (C) 2017-2024 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
22 %template() std::set<int>;
25 #include "CommInterface.hxx"
26 #include "ProcessorGroup.hxx"
27 #include "Topology.hxx"
28 #include "MPIProcessorGroup.hxx"
29 #include "ByStringMPIProcessorGroup.hxx"
31 #include "InterpKernelDEC.hxx"
32 #include "NonCoincidentDEC.hxx"
33 #include "StructuredCoincidentDEC.hxx"
34 #include "OverlapDEC.hxx"
35 #include "ParaMESH.hxx"
36 #include "ParaFIELD.hxx"
37 #include "ICoCoMEDDoubleField.hxx"
38 #include "ICoCoMEDIntField.hxx"
39 #include "ComponentTopology.hxx"
40 #include "ParaUMesh.hxx"
41 #include "ParaSkyLineArray.hxx"
42 #include "ParaDataArray.hxx"
44 using namespace INTERP_KERNEL;
45 using namespace MEDCoupling;
46 using namespace ICoCo;
49 %include "InterpolationOptions.hxx"
50 %include "ProcessorGroup.hxx"
51 %include "DECOptions.hxx"
52 %include "ParaMESH.hxx"
53 %include "ParaFIELD.hxx"
54 %include "MPIProcessorGroup.hxx"
55 %include "ByStringMPIProcessorGroup.hxx"
56 %include "ComponentTopology.hxx"
58 %include "DisjointDEC.hxx"
59 %include "StructuredCoincidentDEC.hxx"
61 %newobject MEDCoupling::ParaUMesh::New;
62 %newobject MEDCoupling::ParaUMesh::getMesh;
63 %newobject MEDCoupling::ParaUMesh::getGlobalCellIds;
64 %newobject MEDCoupling::ParaUMesh::getGlobalNodeIds;
65 %newobject MEDCoupling::ParaUMesh::getCellIdsLyingOnNodes;
66 %newobject MEDCoupling::ParaUMesh::redistributeCells;
67 %newobject MEDCoupling::ParaUMesh::redistributeCellField;
68 %newobject MEDCoupling::ParaUMesh::redistributeNodeField;
69 %newobject MEDCoupling::ParaDataArrayInt32::New;
70 %newobject MEDCoupling::ParaDataArrayInt32::buildComplement;
71 %newobject MEDCoupling::ParaDataArrayInt64::New;
72 %newobject MEDCoupling::ParaDataArrayInt64::buildComplement;
73 %newobject MEDCoupling::ParaSkyLineArray::New;
74 %newobject MEDCoupling::ParaSkyLineArray::equiRedistribute;
75 %newobject MEDCoupling::ParaSkyLineArray::getSkyLineArray;
76 %newobject MEDCoupling::ParaSkyLineArray::getGlobalIdsArray;
78 %newobject MEDCoupling::InterpKernelDEC::_NewWithPG_internal;
79 %newobject MEDCoupling::InterpKernelDEC::_NewWithComm_internal;
80 %newobject MEDCoupling::InterpKernelDEC::retrieveNonFetchedIds;
81 %newobject MEDCoupling::OverlapDEC::_NewWithComm_internal;
83 %feature("unref") ParaSkyLineArray "$this->decrRef();"
84 %feature("unref") ParaUMesh "$this->decrRef();"
85 %feature("unref") ParaDataArrayInt32 "$this->decrRef();"
86 %feature("unref") ParaDataArrayInt64 "$this->decrRef();"
96 virtual ~CommInterface();
97 int worldSize() const;
98 int commSize(MPI_Comm comm, int* size) const;
99 int commRank(MPI_Comm comm, int* rank) const;
100 int commGroup(MPI_Comm comm, MPI_Group* group) const;
101 int groupIncl(MPI_Group group, int size, int* ranks, MPI_Group* group_output) const;
102 int commCreate(MPI_Comm comm, MPI_Group group, MPI_Comm* comm_output) const;
103 int groupFree(MPI_Group* group) const;
104 int commFree(MPI_Comm* comm) const;
106 int send(void* buffer, int count, MPI_Datatype datatype, int target, int tag, MPI_Comm comm) const;
107 int recv(void* buffer, int count, MPI_Datatype datatype, int source, int tag, MPI_Comm comm, MPI_Status* status) const;
108 int sendRecv(void* sendbuf, int sendcount, MPI_Datatype sendtype,
109 int dest, int sendtag, void* recvbuf, int recvcount,
110 MPI_Datatype recvtype, int source, int recvtag, MPI_Comm comm,
113 int Isend(void* buffer, int count, MPI_Datatype datatype, int target,
114 int tag, MPI_Comm comm, MPI_Request *request) const;
115 int Irecv(void* buffer, int count, MPI_Datatype datatype, int source,
116 int tag, MPI_Comm comm, MPI_Request* request) const;
118 int wait(MPI_Request *request, MPI_Status *status) const;
119 int test(MPI_Request *request, int *flag, MPI_Status *status) const;
120 int requestFree(MPI_Request *request) const;
121 int waitany(int count, MPI_Request *array_of_requests, int *index, MPI_Status *status) const;
122 int testany(int count, MPI_Request *array_of_requests, int *index, int *flag, MPI_Status *status) const;
123 int waitall(int count, MPI_Request *array_of_requests, MPI_Status *array_of_status) const { return MPI_Waitall(count, array_of_requests, array_of_status); }
124 int testall(int count, MPI_Request *array_of_requests, int *flag, MPI_Status *array_of_status) const;
125 int waitsome(int incount, MPI_Request *array_of_requests,int *outcount, int *array_of_indices, MPI_Status *array_of_status) const;
126 int testsome(int incount, MPI_Request *array_of_requests, int *outcount,
127 int *array_of_indices, MPI_Status *array_of_status) const;
128 int probe(int source, int tag, MPI_Comm comm, MPI_Status *status) const;
129 int Iprobe(int source, int tag, MPI_Comm comm, int *flag, MPI_Status *status) const;
130 int cancel(MPI_Request *request) const;
131 int testCancelled(MPI_Status *status, int *flag) const;
132 int barrier(MPI_Comm comm) const;
133 int errorString(int errorcode, char *string, int *resultlen) const;
134 int getCount(MPI_Status *status, MPI_Datatype datatype, int *count) const;
136 int broadcast(void* buffer, int count, MPI_Datatype datatype, int root, MPI_Comm comm) const;
137 int allGather(void* sendbuf, int sendcount, MPI_Datatype sendtype,
138 void* recvbuf, int recvcount, MPI_Datatype recvtype,
139 MPI_Comm comm) const;
140 int allToAll(void* sendbuf, int sendcount, MPI_Datatype sendtype,
141 void* recvbuf, int recvcount, MPI_Datatype recvtype,
142 MPI_Comm comm) const;
143 int allToAllV(void* sendbuf, int* sendcounts, int* senddispls,
144 MPI_Datatype sendtype, void* recvbuf, int* recvcounts,
145 int* recvdispls, MPI_Datatype recvtype,
146 MPI_Comm comm) const;
148 int reduce(void* sendbuf, void* recvbuf, int count, MPI_Datatype datatype, MPI_Op op, int root, MPI_Comm comm) const;
149 int allReduce(void* sendbuf, void* recvbuf, int count, MPI_Datatype datatype, MPI_Op op, MPI_Comm comm) const;
152 PyObject *allGatherArrays(const DataArrayIdType *array) const
154 std::vector< MCAuto<DataArrayIdType> > ret;
155 self->allGatherArrays(MPI_COMM_WORLD,array,ret);
156 return convertFromVectorAutoObjToPyObj<DataArrayIdType>(ret,SWIGTITraits<mcIdType>::TI);
159 PyObject *allToAllArrays(PyObject *arrays) const
161 std::vector< DataArrayIdType * > arraysIn;
162 std::vector< MCAuto<DataArrayIdType> > arrayOut;
163 convertFromPyObjVectorOfObj<MEDCoupling::DataArrayIdType*>(arrays,SWIGTITraits<mcIdType>::TI,"DataArrayIdType",arraysIn);
164 std::vector< MCAuto<DataArrayIdType> > arraysIn2(FromVecToVecAuto<DataArrayIdType>(arraysIn));
165 self->allToAllArrays(MPI_COMM_WORLD,arraysIn2,arrayOut);
166 return convertFromVectorAutoObjToPyObj<DataArrayIdType>(arrayOut,SWIGTITraits<mcIdType>::TI);
171 class ParaUMesh : public RefCountObject
174 static ParaUMesh *New(MEDCouplingUMesh *mesh, DataArrayIdType *globalCellIds, DataArrayIdType *globalNodeIds);
175 ParaUMesh *redistributeCells(const DataArrayIdType *globalCellIds) const;
176 DataArrayIdType *redistributeCellField(const DataArrayIdType *globalCellIds, const DataArrayIdType *fieldValueToRed) const;
177 DataArrayDouble *redistributeCellField(const DataArrayIdType *globalCellIds, const DataArrayDouble *fieldValueToRed) const;
178 DataArrayIdType *redistributeNodeField(const DataArrayIdType *globalCellIds, const DataArrayIdType *fieldValueToRed) const;
179 DataArrayDouble *redistributeNodeField(const DataArrayIdType *globalCellIds, const DataArrayDouble *fieldValueToRed) const;
182 ParaUMesh(MEDCouplingUMesh *mesh, DataArrayIdType *globalCellIds, DataArrayIdType *globalNodeIds)
184 return ParaUMesh::New(mesh,globalCellIds,globalNodeIds);
187 MEDCouplingUMesh *getMesh()
189 MEDCouplingUMesh *ret(self->getMesh());
190 if(ret) ret->incrRef();
194 DataArrayIdType *getGlobalCellIds()
196 DataArrayIdType *ret(self->getGlobalCellIds());
197 if(ret) ret->incrRef();
201 DataArrayIdType *getGlobalNodeIds()
203 DataArrayIdType *ret(self->getGlobalNodeIds());
204 if(ret) ret->incrRef();
208 DataArrayIdType *getCellIdsLyingOnNodes(const DataArrayIdType *globalNodeIds, bool fullyIn) const
210 MCAuto<DataArrayIdType> ret(self->getCellIdsLyingOnNodes(globalNodeIds,fullyIn));
216 class ParaDataArray : public RefCountObject
220 class ParaDataArrayInt32 : public ParaDataArray
223 static ParaDataArrayInt32 *New(DataArrayInt32 *seqDa);
224 DataArrayIdType *buildComplement(int nbOfElems) const;
227 ParaDataArrayInt32(DataArrayInt32 *seqDa)
229 return ParaDataArrayInt32::New(seqDa);
234 class ParaDataArrayInt64 : public ParaDataArray
237 static ParaDataArrayInt64 *New(DataArrayInt64 *seqDa);
238 DataArrayIdType *buildComplement(long nbOfElems) const;
241 ParaDataArrayInt64(DataArrayInt64 *seqDa)
243 return ParaDataArrayInt64::New(seqDa);
248 class ParaSkyLineArray : public RefCountObject
251 static ParaSkyLineArray *New(MEDCouplingSkyLineArray *ska, DataArrayIdType *globalIds);
254 ParaSkyLineArray(MEDCouplingSkyLineArray *ska, DataArrayIdType *globalIds)
256 return ParaSkyLineArray::New(ska,globalIds);
259 ParaSkyLineArray *equiRedistribute(mcIdType nbOfEntities) const
261 MCAuto<ParaSkyLineArray> ret(self->equiRedistribute(nbOfEntities));
265 MEDCouplingSkyLineArray *getSkyLineArray() const
267 MEDCouplingSkyLineArray *ret(self->getSkyLineArray());
273 DataArrayIdType *getGlobalIdsArray() const
275 DataArrayIdType *ret(self->getGlobalIdsArray());
283 /* This object can be used only if MED_ENABLE_FVM is defined*/
284 #ifdef MED_ENABLE_FVM
285 class NonCoincidentDEC : public DEC
288 NonCoincidentDEC(ProcessorGroup& source, ProcessorGroup& target);
292 class InterpKernelDEC : public DisjointDEC, public INTERP_KERNEL::InterpolationOptions
296 InterpKernelDEC(ProcessorGroup& source_group, ProcessorGroup& target_group);
297 InterpKernelDEC(const std::set<int>& src_ids, const std::set<int>& trg_ids); // hide last optional parameter!
298 InterpKernelDEC(ProcessorGroup& generic_group, const std::string& source_group, const std::string& target_group);
299 InterpKernelDEC(ProcessorGroup& generic_group, const std::string& interaction_group);
300 virtual ~InterpKernelDEC();
304 void synchronizeWithDefaultValue(double val);
306 void recvData(double time);
308 void sendData(double time , double deltatime);
309 void prepareSourceDE();
310 void prepareTargetDE();
313 // Provides a direct ctor for which the communicator can be passed with "MPI._addressof(the_com)":
314 InterpKernelDEC(const std::set<int>& src_ids, const std::set<int>& trg_ids, long long comm_ptr)
316 return new InterpKernelDEC(src_ids, trg_ids, *((MPI_Comm*)comm_ptr));
319 // This one should really not be called directly by the user since it still has an interface with a pointer to MPI_Comm
320 // which Swig doesn't handle nicely.
321 // It is just here to provide a constructor taking a **pointer** to a comm - See pythoncode below.
322 static InterpKernelDEC* _NewWithPG_internal(ProcessorGroup& source_group, ProcessorGroup& target_group)
324 return new InterpKernelDEC(source_group,target_group);
327 static InterpKernelDEC* _NewWithComm_internal(const std::set<int>& src_ids, const std::set<int>& trg_ids, long long another_comm)
329 return new InterpKernelDEC(src_ids,trg_ids, *(MPI_Comm*)another_comm); // I know, ugly cast ...
332 DataArrayIdType *retrieveNonFetchedIds() const
334 MCAuto<DataArrayIdType> ret = self->retrieveNonFetchedIds();
340 class OverlapDEC : public DEC, public INTERP_KERNEL::InterpolationOptions
343 OverlapDEC(const std::set<int>& procIds); // hide optional param comm
344 virtual ~OverlapDEC();
347 void sendRecvData(bool way=true);
351 void attachSourceLocalField(ParaFIELD *field, bool ownPt=false);
352 void attachTargetLocalField(ParaFIELD *field, bool ownPt=false);
353 void attachSourceLocalField(MEDCouplingFieldDouble *field);
354 void attachTargetLocalField(MEDCouplingFieldDouble *field);
355 void attachSourceLocalField(ICoCo::MEDDoubleField *field);
356 void attachTargetLocalField(ICoCo::MEDDoubleField *field);
357 ProcessorGroup *getGroup();
358 bool isInGroup() const;
360 void setDefaultValue(double val);
361 void setWorkSharingAlgo(int method);
363 void debugPrintWorkSharing(std::ostream & ostr) const;
366 OverlapDEC(const std::set<int>& ids, long long comm_ptr)
368 return new OverlapDEC(ids, *((MPI_Comm*)comm_ptr));
371 // This one should really not be called directly by the user since it still has an interface with a pointer to MPI_Comm
372 // which Swig doesn't handle nicely.
373 // It is just here to provide a constructor taking a **pointer** to a comm - See pythoncode below.
374 static OverlapDEC* _NewWithComm_internal(const std::set<int>& ids, long long another_comm)
376 return new OverlapDEC(ids, *(MPI_Comm*)another_comm); // I know, ugly cast ...
381 } // end namespace MEDCoupling
383 %extend MEDCoupling::ParaMESH
385 PyObject *getGlobalNumberingCell2() const
387 const mcIdType *tmp=self->getGlobalNumberingCell();
388 mcIdType size=self->getCellMesh()->getNumberOfCells();
389 PyObject *ret=PyList_New(size);
390 for(mcIdType i=0;i<size;i++)
391 PyList_SetItem(ret,i,PyInt_FromLong(tmp[i]));
395 PyObject *getGlobalNumberingFace2() const
397 const mcIdType *tmp=self->getGlobalNumberingFace();
398 mcIdType size=self->getFaceMesh()->getNumberOfCells();
399 PyObject *ret=PyList_New(size);
400 for(mcIdType i=0;i<size;i++)
401 PyList_SetItem(ret,i,PyInt_FromLong(tmp[i]));
405 PyObject *getGlobalNumberingNode2() const
407 const mcIdType *tmp=self->getGlobalNumberingNode();
408 mcIdType size=self->getCellMesh()->getNumberOfNodes();
409 PyObject *ret=PyList_New(size);
410 for(mcIdType i=0;i<size;i++)
411 PyList_SetItem(ret,i,PyInt_FromLong(tmp[i]));
417 if MEDCouplingUse64BitIDs():
418 ParaDataArrayInt = ParaDataArrayInt64
420 ParaDataArrayInt = ParaDataArrayInt32
425 # And here we use mpi4py ability to provide its internal (C++) pointer to the communicator:
426 # NB: doing a proper typemap from MPI_Comm from Python to C++ requires the inclusion of mpi4py headers and .i file ... an extra dependency ...
427 def _IKDEC_WithComm_internal(src_procs, tgt_procs, mpicomm=None):
428 from mpi4py import MPI
431 s, t = [el for el in src_procs], [el for el in tgt_procs]
434 msg = "InterpKernelDEC: invalid type in ctor arguments! Possible signatures are:\n"
435 msg += " - InterpKernelDEC(ProcessorGroup, ProcessorGroup)\n"
436 msg += " - InterpKernelDEC(<iterable>, <iterable>)\n"
437 msg += " - InterpKernelDEC(<iterable>, <iterable>, MPI_Comm*) : WARNING here the address of the communicator should be passed with MPI._addressof(the_com)\n"
438 msg += " - InterpKernelDEC.New(ProcessorGroup, ProcessorGroup)\n"
439 msg += " - InterpKernelDEC.New(<iterable>, <iterable>)\n"
440 msg += " - InterpKernelDEC.New(<iterable>, <iterable>, MPI_Comm)\n"
442 if isinstance(src_procs, ProcessorGroup) and isinstance(tgt_procs, ProcessorGroup):
443 return InterpKernelDEC._NewWithPG_internal(src_procs, tgt_procs)
444 elif not s is None: # iterable
445 return InterpKernelDEC._NewWithComm_internal(s, t, MPI._addressof(MPI.COMM_WORLD))
447 raise InterpKernelException(msg)
449 if s is None: raise InterpKernelException(msg) # must be iterable
450 return InterpKernelDEC._NewWithComm_internal(s, t, MPI._addressof(mpicomm))
452 def _ODEC_WithComm_internal(procs, mpicomm=None):
453 from mpi4py import MPI
456 g = [el for el in procs]
458 msg = "OverlapDEC: invalid type in ctor arguments! Possible signatures are:\n"
459 msg += " - OverlapDEC.New(<iterable>)\n"
460 msg += " - OverlapDEC.New(<iterable>, MPI_Comm)\n"
461 msg += " - OverlapDEC(<iterable>)\n"
462 msg += " - OverlapDEC(<iterable>, MPI_Comm*) : WARNING here the address of the communicator should be passed with MPI._addressof(the_com)\n"
463 raise InterpKernelException(msg)
467 return OverlapDEC._NewWithComm_internal(g, MPI._addressof(mpicomm))
469 InterpKernelDEC.New = _IKDEC_WithComm_internal
470 OverlapDEC.New = _ODEC_WithComm_internal