]> SALOME platform Git repositories - tools/medcoupling.git/blob - src/ParaMEDMEM/ParaUMesh.hxx
Salome HOME
Make it templated
[tools/medcoupling.git] / src / ParaMEDMEM / ParaUMesh.hxx
1 // Copyright (C) 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 // Author : Anthony Geay (EDF R&D)
20
21 #pragma once
22
23 #include "MEDCouplingUMesh.hxx"
24 #include "ProcessorGroup.hxx"
25 #include "MEDCouplingMemArray.hxx"
26
27 #include <string>
28 #include <vector>
29
30 namespace MEDCoupling
31 {
32   /*!
33    * Parallel representation of an unstructured mesh.
34    *
35    * This class is very specific to the requirement of parallel code computations.
36    */
37   class ParaUMesh : public RefCountObject
38   {
39   public:
40     static ParaUMesh *New(MEDCouplingUMesh *mesh, DataArrayIdType *globalCellIds, DataArrayIdType *globalNodeIds);
41     MCAuto<DataArrayIdType> getCellIdsLyingOnNodes(const DataArrayIdType *globalNodeIds, bool fullyIn) const;
42     ParaUMesh *redistributeCells(const DataArrayIdType *globalCellIds) const;
43     DataArrayDouble *redistributeCellField(const DataArrayIdType *globalCellIds, const DataArrayDouble *fieldValueToRed) const;
44     DataArrayIdType *redistributeCellField(const DataArrayIdType *globalCellIds, const DataArrayIdType *fieldValueToRed) const;
45     DataArrayDouble *redistributeNodeField(const DataArrayIdType *globalCellIds, const DataArrayDouble *fieldValueToRed) const;
46     DataArrayIdType *redistributeNodeField(const DataArrayIdType *globalCellIds, const DataArrayIdType *fieldValueToRed) const;
47     MEDCouplingUMesh *getMesh() { return _mesh; }
48     DataArrayIdType *getGlobalCellIds() { return _cell_global; }
49     DataArrayIdType *getGlobalNodeIds() { return _node_global; }
50   protected:
51     virtual ~ParaUMesh() { }
52     ParaUMesh(MEDCouplingUMesh *mesh, DataArrayIdType *globalCellIds, DataArrayIdType *globalNodeIds);
53     std::string getClassName() const override { return "ParaUMesh"; }
54     std::size_t getHeapMemorySizeWithoutChildren() const override;
55     std::vector<const BigMemoryObject *> getDirectChildrenWithNull() const override;
56   private:
57     MCAuto<MEDCouplingUMesh> _mesh;
58     MCAuto<DataArrayIdType> _cell_global;
59     MCAuto<DataArrayIdType> _node_global;
60   private:
61     template<class T>
62     typename Traits<T>::ArrayType *redistributeCellFieldT(const DataArrayIdType *globalCellIds, const typename Traits<T>::ArrayType *fieldValueToRed) const
63     {
64       using DataArrayT = typename Traits<T>::ArrayType;
65       MPI_Comm comm(MPI_COMM_WORLD);
66       CommInterface ci;
67       std::unique_ptr<mcIdType[]> allGlobalCellIds,allGlobalCellIdsIndex;
68       int size(ci.allGatherArrays(comm,globalCellIds,allGlobalCellIds,allGlobalCellIdsIndex));
69       // Prepare ParaUMesh parts to be sent : compute for each proc the contribution of current rank.
70       std::vector< MCAuto<DataArrayIdType> > globalCellIdsToBeSent(size);
71       std::vector< MCAuto<DataArrayT> > fieldToBeSent(size);
72       for(int curRk = 0 ; curRk < size ; ++curRk)
73       {
74         mcIdType offset(allGlobalCellIdsIndex[curRk]);
75         MCAuto<DataArrayIdType> globalCellIdsOfCurProc(DataArrayIdType::New());
76         globalCellIdsOfCurProc->useArray(allGlobalCellIds.get()+offset,false,DeallocType::CPP_DEALLOC,allGlobalCellIdsIndex[curRk+1]-offset,1);
77         // the key call is here : compute for rank curRk the cells to be sent
78         MCAuto<DataArrayIdType> globalCellIdsCaptured(_cell_global->buildIntersection(globalCellIdsOfCurProc));// OK for the global cellIds
79         MCAuto<DataArrayIdType> localCellIdsCaptured(_cell_global->findIdForEach(globalCellIdsCaptured->begin(),globalCellIdsCaptured->end()));
80         globalCellIdsToBeSent[curRk] = globalCellIdsCaptured;
81         fieldToBeSent[curRk] = fieldValueToRed->selectByTupleIdSafe(localCellIdsCaptured->begin(),localCellIdsCaptured->end());
82       }
83       // Receive
84       std::vector< MCAuto<DataArrayIdType> > globalCellIdsReceived;
85       ci.allToAllArrays(comm,globalCellIdsToBeSent,globalCellIdsReceived);
86       std::vector< MCAuto<DataArrayT> > fieldValueReceived;
87       ci.allToAllArrays(comm,fieldToBeSent,fieldValueReceived);
88       // use globalCellIdsReceived to reorganize everything
89       MCAuto<DataArrayIdType> aggregatedCellIds( DataArrayIdType::Aggregate(FromVecAutoToVecOfConst<DataArrayIdType>(globalCellIdsReceived)) );
90       MCAuto<DataArrayIdType> aggregatedCellIdsSorted(aggregatedCellIds->copySorted());
91       MCAuto<DataArrayIdType> idsIntoAggregatedIds(DataArrayIdType::FindPermutationFromFirstToSecondDuplicate(aggregatedCellIdsSorted,aggregatedCellIds));
92       MCAuto<DataArrayIdType> cellIdsOfSameNodeIds(aggregatedCellIdsSorted->indexOfSameConsecutiveValueGroups());
93       MCAuto<DataArrayIdType> n2o_cells(idsIntoAggregatedIds->selectByTupleIdSafe(cellIdsOfSameNodeIds->begin(),cellIdsOfSameNodeIds->end()-1));//new == new ordering so that global cell ids are sorted . old == coarse ordering implied by the aggregation
94       //
95       MCAuto<DataArrayT> fieldAggregated(DataArrayT::Aggregate(FromVecAutoToVecOfConst<DataArrayT>(fieldValueReceived)));
96       MCAuto<DataArrayT> ret(fieldAggregated->selectByTupleIdSafe(n2o_cells->begin(),n2o_cells->end()));
97       return ret.retn();
98     }
99     
100     template<class T>
101     typename Traits<T>::ArrayType *redistributeNodeFieldT(const DataArrayIdType *globalCellIds, const typename Traits<T>::ArrayType *fieldValueToRed) const
102     {
103       using DataArrayT = typename Traits<T>::ArrayType;
104       MPI_Comm comm(MPI_COMM_WORLD);
105       CommInterface ci;
106       std::unique_ptr<mcIdType[]> allGlobalCellIds,allGlobalCellIdsIndex;
107       int size(ci.allGatherArrays(comm,globalCellIds,allGlobalCellIds,allGlobalCellIdsIndex));
108       // Prepare ParaUMesh parts to be sent : compute for each proc the contribution of current rank.
109       std::vector< MCAuto<DataArrayIdType> > globalNodeIdsToBeSent(size);
110       std::vector< MCAuto<DataArrayT> > fieldToBeSent(size);
111       for(int curRk = 0 ; curRk < size ; ++curRk)
112       {
113         mcIdType offset(allGlobalCellIdsIndex[curRk]);
114         MCAuto<DataArrayIdType> globalCellIdsOfCurProc(DataArrayIdType::New());
115         globalCellIdsOfCurProc->useArray(allGlobalCellIds.get()+offset,false,DeallocType::CPP_DEALLOC,allGlobalCellIdsIndex[curRk+1]-offset,1);
116         // the key call is here : compute for rank curRk the cells to be sent
117         MCAuto<DataArrayIdType> globalCellIdsCaptured(_cell_global->buildIntersection(globalCellIdsOfCurProc));// OK for the global cellIds
118         MCAuto<DataArrayIdType> localCellIdsCaptured(_cell_global->findIdForEach(globalCellIdsCaptured->begin(),globalCellIdsCaptured->end()));
119         MCAuto<MEDCouplingUMesh> meshPart(_mesh->buildPartOfMySelf(localCellIdsCaptured->begin(),localCellIdsCaptured->end(),true));
120         MCAuto<DataArrayIdType> o2n(meshPart->zipCoordsTraducer());// OK for the mesh
121         MCAuto<DataArrayIdType> n2o(o2n->invertArrayO2N2N2O(meshPart->getNumberOfNodes()));
122         MCAuto<DataArrayIdType> globalNodeIdsPart(_node_global->selectByTupleIdSafe(n2o->begin(),n2o->end())); // OK for the global nodeIds
123         globalNodeIdsToBeSent[curRk] = globalNodeIdsPart;
124         fieldToBeSent[curRk] = fieldValueToRed->selectByTupleIdSafe(n2o->begin(),n2o->end());
125       }
126       // Receive
127       std::vector< MCAuto<DataArrayIdType> > globalNodeIdsReceived;
128       ci.allToAllArrays(comm,globalNodeIdsToBeSent,globalNodeIdsReceived);
129       std::vector< MCAuto<DataArrayT> > fieldValueReceived;
130       ci.allToAllArrays(comm,fieldToBeSent,fieldValueReceived);
131       // firstly deal with nodes.
132       MCAuto<DataArrayIdType> aggregatedNodeIds( DataArrayIdType::Aggregate(FromVecAutoToVecOfConst<DataArrayIdType>(globalNodeIdsReceived)) );
133       MCAuto<DataArrayIdType> aggregatedNodeIdsSorted(aggregatedNodeIds->copySorted());
134       MCAuto<DataArrayIdType> nodeIdsIntoAggregatedIds(DataArrayIdType::FindPermutationFromFirstToSecondDuplicate(aggregatedNodeIdsSorted,aggregatedNodeIds));
135       MCAuto<DataArrayIdType> idxOfSameNodeIds(aggregatedNodeIdsSorted->indexOfSameConsecutiveValueGroups());
136       MCAuto<DataArrayIdType> n2o_nodes(nodeIdsIntoAggregatedIds->selectByTupleIdSafe(idxOfSameNodeIds->begin(),idxOfSameNodeIds->end()-1));//new == new ordering so that global node ids are sorted . old == coarse ordering implied by the aggregation
137       //
138       MCAuto<DataArrayT> fieldAggregated(DataArrayT::Aggregate(FromVecAutoToVecOfConst<DataArrayT>(fieldValueReceived)));
139       MCAuto<DataArrayT> ret(fieldAggregated->selectByTupleIdSafe(n2o_nodes->begin(),n2o_nodes->end()));
140       //
141       return ret.retn();
142     }
143   };
144 }