]> SALOME platform Git repositories - tools/medcoupling.git/blob - src/ParaMEDMEM/ParaUMesh.hxx
Salome HOME
Some helpers for debugging
[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       if( _cell_global->getNumberOfTuples() != fieldValueToRed->getNumberOfTuples() )
68         throw INTERP_KERNEL::Exception("PAraUMesh::redistributeCellFieldT : invalid input length of array !");
69       std::unique_ptr<mcIdType[]> allGlobalCellIds,allGlobalCellIdsIndex;
70       int size(ci.allGatherArrays(comm,globalCellIds,allGlobalCellIds,allGlobalCellIdsIndex));
71       // Prepare ParaUMesh parts to be sent : compute for each proc the contribution of current rank.
72       std::vector< MCAuto<DataArrayIdType> > globalCellIdsToBeSent(size);
73       std::vector< MCAuto<DataArrayT> > fieldToBeSent(size);
74       for(int curRk = 0 ; curRk < size ; ++curRk)
75       {
76         mcIdType offset(allGlobalCellIdsIndex[curRk]);
77         MCAuto<DataArrayIdType> globalCellIdsOfCurProc(DataArrayIdType::New());
78         globalCellIdsOfCurProc->useArray(allGlobalCellIds.get()+offset,false,DeallocType::CPP_DEALLOC,allGlobalCellIdsIndex[curRk+1]-offset,1);
79         // the key call is here : compute for rank curRk the cells to be sent
80         MCAuto<DataArrayIdType> globalCellIdsCaptured(_cell_global->buildIntersection(globalCellIdsOfCurProc));// OK for the global cellIds
81         MCAuto<DataArrayIdType> localCellIdsCaptured(_cell_global->findIdForEach(globalCellIdsCaptured->begin(),globalCellIdsCaptured->end()));
82         globalCellIdsToBeSent[curRk] = globalCellIdsCaptured;
83         fieldToBeSent[curRk] = fieldValueToRed->selectByTupleIdSafe(localCellIdsCaptured->begin(),localCellIdsCaptured->end());
84       }
85       // Receive
86       std::vector< MCAuto<DataArrayIdType> > globalCellIdsReceived;
87       ci.allToAllArrays(comm,globalCellIdsToBeSent,globalCellIdsReceived);
88       std::vector< MCAuto<DataArrayT> > fieldValueReceived;
89       ci.allToAllArrays(comm,fieldToBeSent,fieldValueReceived);
90       // use globalCellIdsReceived to reorganize everything
91       MCAuto<DataArrayIdType> aggregatedCellIds( DataArrayIdType::Aggregate(FromVecAutoToVecOfConst<DataArrayIdType>(globalCellIdsReceived)) );
92       MCAuto<DataArrayIdType> aggregatedCellIdsSorted(aggregatedCellIds->copySorted());
93       MCAuto<DataArrayIdType> idsIntoAggregatedIds(DataArrayIdType::FindPermutationFromFirstToSecondDuplicate(aggregatedCellIdsSorted,aggregatedCellIds));
94       MCAuto<DataArrayIdType> cellIdsOfSameNodeIds(aggregatedCellIdsSorted->indexOfSameConsecutiveValueGroups());
95       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
96       //
97       MCAuto<DataArrayT> fieldAggregated(DataArrayT::Aggregate(FromVecAutoToVecOfConst<DataArrayT>(fieldValueReceived)));
98       MCAuto<DataArrayT> ret(fieldAggregated->selectByTupleIdSafe(n2o_cells->begin(),n2o_cells->end()));
99       return ret.retn();
100     }
101     
102     template<class T>
103     typename Traits<T>::ArrayType *redistributeNodeFieldT(const DataArrayIdType *globalCellIds, const typename Traits<T>::ArrayType *fieldValueToRed) const
104     {
105       using DataArrayT = typename Traits<T>::ArrayType;
106       MPI_Comm comm(MPI_COMM_WORLD);
107       CommInterface ci;
108       if( _node_global->getNumberOfTuples() != fieldValueToRed->getNumberOfTuples() )
109         throw INTERP_KERNEL::Exception("PAraUMesh::redistributeNodeFieldT : invalid input length of array !");
110       std::unique_ptr<mcIdType[]> allGlobalCellIds,allGlobalCellIdsIndex;
111       int size(ci.allGatherArrays(comm,globalCellIds,allGlobalCellIds,allGlobalCellIdsIndex));
112       // Prepare ParaUMesh parts to be sent : compute for each proc the contribution of current rank.
113       std::vector< MCAuto<DataArrayIdType> > globalNodeIdsToBeSent(size);
114       std::vector< MCAuto<DataArrayT> > fieldToBeSent(size);
115       for(int curRk = 0 ; curRk < size ; ++curRk)
116       {
117         mcIdType offset(allGlobalCellIdsIndex[curRk]);
118         MCAuto<DataArrayIdType> globalCellIdsOfCurProc(DataArrayIdType::New());
119         globalCellIdsOfCurProc->useArray(allGlobalCellIds.get()+offset,false,DeallocType::CPP_DEALLOC,allGlobalCellIdsIndex[curRk+1]-offset,1);
120         // the key call is here : compute for rank curRk the cells to be sent
121         MCAuto<DataArrayIdType> globalCellIdsCaptured(_cell_global->buildIntersection(globalCellIdsOfCurProc));// OK for the global cellIds
122         MCAuto<DataArrayIdType> localCellIdsCaptured(_cell_global->findIdForEach(globalCellIdsCaptured->begin(),globalCellIdsCaptured->end()));
123         MCAuto<MEDCouplingUMesh> meshPart(_mesh->buildPartOfMySelf(localCellIdsCaptured->begin(),localCellIdsCaptured->end(),true));
124         MCAuto<DataArrayIdType> o2n(meshPart->zipCoordsTraducer());// OK for the mesh
125         MCAuto<DataArrayIdType> n2o(o2n->invertArrayO2N2N2O(meshPart->getNumberOfNodes()));
126         MCAuto<DataArrayIdType> globalNodeIdsPart(_node_global->selectByTupleIdSafe(n2o->begin(),n2o->end())); // OK for the global nodeIds
127         globalNodeIdsToBeSent[curRk] = globalNodeIdsPart;
128         fieldToBeSent[curRk] = fieldValueToRed->selectByTupleIdSafe(n2o->begin(),n2o->end());
129       }
130       // Receive
131       std::vector< MCAuto<DataArrayIdType> > globalNodeIdsReceived;
132       ci.allToAllArrays(comm,globalNodeIdsToBeSent,globalNodeIdsReceived);
133       std::vector< MCAuto<DataArrayT> > fieldValueReceived;
134       ci.allToAllArrays(comm,fieldToBeSent,fieldValueReceived);
135       // firstly deal with nodes.
136       MCAuto<DataArrayIdType> aggregatedNodeIds( DataArrayIdType::Aggregate(FromVecAutoToVecOfConst<DataArrayIdType>(globalNodeIdsReceived)) );
137       MCAuto<DataArrayIdType> aggregatedNodeIdsSorted(aggregatedNodeIds->copySorted());
138       MCAuto<DataArrayIdType> nodeIdsIntoAggregatedIds(DataArrayIdType::FindPermutationFromFirstToSecondDuplicate(aggregatedNodeIdsSorted,aggregatedNodeIds));
139       MCAuto<DataArrayIdType> idxOfSameNodeIds(aggregatedNodeIdsSorted->indexOfSameConsecutiveValueGroups());
140       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
141       //
142       MCAuto<DataArrayT> fieldAggregated(DataArrayT::Aggregate(FromVecAutoToVecOfConst<DataArrayT>(fieldValueReceived)));
143       MCAuto<DataArrayT> ret(fieldAggregated->selectByTupleIdSafe(n2o_nodes->begin(),n2o_nodes->end()));
144       //
145       return ret.retn();
146     }
147   };
148 }