Salome HOME
Updated copyright comment
[tools/medcoupling.git] / src / ParaMEDMEM / ParaDataArray.txx
1 // Copyright (C) 2020-2024  CEA, EDF
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 "ParaDataArray.hxx"
24 #include "CommInterface.hxx"
25 #include "MEDCouplingMemArray.txx"
26
27 #include <sstream>
28
29 namespace MEDCoupling
30 {
31   template<class T>
32   ParaDataArrayTemplate<T>::ParaDataArrayTemplate(typename Traits<T>::ArrayType *seqDa)
33   {
34     this->_seq_da.takeRef(seqDa);
35   }
36
37   template<class T>
38   std::size_t ParaDataArrayTemplate<T>::getHeapMemorySizeWithoutChildren() const
39   {
40     return 0;
41   }
42
43   template<class T>
44   std::vector<const BigMemoryObject *> ParaDataArrayTemplate<T>::getDirectChildrenWithNull() const
45   {
46     return { this->_seq_da };
47   }
48   
49   template<class T>
50   void ParaDataArrayTemplate<T>::checkOKOneComponent(const std::string& msg) const
51   {
52     if(this->_seq_da.isNull())
53     {
54       std::ostringstream oss; oss << msg << " : nullptr internal pointer !";
55       throw INTERP_KERNEL::Exception(oss.str());
56     }
57     this->_seq_da->checkAllocated();
58     if( this->_seq_da->getNumberOfComponents()!=1 )
59     {
60       std::ostringstream oss; oss << msg << " : internal seq dataarray does not contain one component as expected !";
61       throw INTERP_KERNEL::Exception(oss.str());
62     }
63   }
64
65   /*!
66     Parallel version of DataArrayInt::buildComplement. Returns result on proc 0. Not allocated DataArrayT is returned for all procs.
67    */
68   template<class T>
69   DataArrayIdType *ParaDataArrayDiscrete<T>::buildComplement(T nbOfElems) const
70   {
71     using DataArrayT = typename Traits<T>::ArrayType;
72     this->checkOKOneComponent("ParaDataArray::buildComplement");
73     MPI_Comm comm(MPI_COMM_WORLD);
74     CommInterface ci;
75     int size;
76     ci.commSize(comm,&size);
77     std::vector< MCAuto<DataArrayT> > idsCaptured(size);
78     for(int curRk = 0 ; curRk < size ; ++curRk)
79     {
80       T curStart(0),curEnd(0);
81       DataArrayTools<T>::GetSlice(0,nbOfElems,1,ToIdType(curRk),ToIdType(size),curStart,curEnd);
82       MCAuto<DataArrayIdType> idsInGlobalIds(this->_seq_da->findIdsInRange(curStart,curEnd));
83       idsCaptured[curRk] = this->_seq_da->selectByTupleIdSafe(idsInGlobalIds->begin(),idsInGlobalIds->end());
84     }
85     // communication : 1 arrays are going to be all2allized : ids
86     MCAuto<DataArrayT> aggregatedIds;
87     {
88       std::vector< MCAuto<DataArrayT> > myRkIdsCaptured;
89       ci.allToAllArraysT<T>(comm,idsCaptured,myRkIdsCaptured);
90       aggregatedIds = DataArrayT::Aggregate(FromVecAutoToVecOfConst<DataArrayT>(myRkIdsCaptured));
91     }
92     aggregatedIds->sort();
93     aggregatedIds = aggregatedIds->buildUnique();
94     int rank(-1);
95     ci.commRank(comm,&rank);
96     T vmin(std::numeric_limits<T>::max()),vmax(-std::numeric_limits<T>::max());
97     DataArrayTools<T>::GetSlice(0,nbOfElems,1,ToIdType(rank),ToIdType(size),vmin,vmax);
98     aggregatedIds->applyLin(1,-vmin);
99     MCAuto<DataArrayIdType> seqComp(aggregatedIds->buildComplement(ToIdType(vmax-vmin)));
100     seqComp->applyLin(1,ToIdType(vmin));
101     //
102     std::vector< MCAuto<DataArrayIdType> > arraysOut;
103     ci.gatherArrays(comm,0,seqComp,arraysOut);
104     MCAuto<DataArrayIdType> ret(DataArrayIdType::Aggregate(FromVecAutoToVecOfConst<DataArrayIdType>(arraysOut)));
105     return ret.retn();
106   }
107
108 }