[657] | 1 | /*! |
---|
| 2 | \file domain_algorithm_interpolate_from_file.cpp |
---|
| 3 | \author Ha NGUYEN |
---|
| 4 | \since 09 Jul 2015 |
---|
[689] | 5 | \date 15 Sep 2015 |
---|
[657] | 6 | |
---|
| 7 | \brief Algorithm for interpolation on a domain. |
---|
| 8 | */ |
---|
[689] | 9 | #include "domain_algorithm_interpolate.hpp" |
---|
[657] | 10 | #include <boost/unordered_map.hpp> |
---|
| 11 | #include "context.hpp" |
---|
| 12 | #include "context_client.hpp" |
---|
| 13 | #include "distribution_client.hpp" |
---|
| 14 | #include "client_server_mapping_distributed.hpp" |
---|
[660] | 15 | #include "netcdf.hpp" |
---|
[688] | 16 | #include "mapper.hpp" |
---|
[821] | 17 | #include "mpi_tag.hpp" |
---|
[657] | 18 | |
---|
| 19 | namespace xios { |
---|
| 20 | |
---|
[689] | 21 | CDomainAlgorithmInterpolate::CDomainAlgorithmInterpolate(CDomain* domainDestination, CDomain* domainSource, CInterpolateDomain* interpDomain) |
---|
[657] | 22 | : CDomainAlgorithmTransformation(domainDestination, domainSource), interpDomain_(interpDomain) |
---|
| 23 | { |
---|
| 24 | interpDomain_->checkValid(domainSource); |
---|
| 25 | } |
---|
| 26 | |
---|
[689] | 27 | /*! |
---|
| 28 | Compute remap with integrated remap calculation module |
---|
| 29 | */ |
---|
| 30 | void CDomainAlgorithmInterpolate::computeRemap() |
---|
[688] | 31 | { |
---|
| 32 | using namespace sphereRemap; |
---|
| 33 | |
---|
| 34 | CContext* context = CContext::getCurrent(); |
---|
| 35 | CContextClient* client=context->client; |
---|
| 36 | int clientRank = client->clientRank; |
---|
| 37 | int i, j, k, idx; |
---|
[689] | 38 | std::vector<double> srcPole(3,0), dstPole(3,0); |
---|
| 39 | int orderInterp = interpDomain_->order.getValue(); |
---|
[846] | 40 | bool renormalize ; |
---|
[688] | 41 | |
---|
[846] | 42 | if (interpDomain_->renormalize.isEmpty()) renormalize=true; |
---|
| 43 | else renormalize = interpDomain_->renormalize; |
---|
| 44 | |
---|
[743] | 45 | const double poleValue = 90.0; |
---|
| 46 | const int constNVertex = 4; // Value by default number of vertex for rectangular domain |
---|
[688] | 47 | int nVertexSrc, nVertexDest; |
---|
| 48 | nVertexSrc = nVertexDest = constNVertex; |
---|
| 49 | |
---|
| 50 | // First of all, try to retrieve the boundary values of domain source and domain destination |
---|
| 51 | int localDomainSrcSize = domainSrc_->i_index.numElements(); |
---|
| 52 | int niSrc = domainSrc_->ni.getValue(), njSrc = domainSrc_->nj.getValue(); |
---|
| 53 | bool hasBoundSrc = domainSrc_->hasBounds; |
---|
| 54 | if (hasBoundSrc) nVertexSrc = domainSrc_->nvertex.getValue(); |
---|
| 55 | CArray<double,2> boundsLonSrc(nVertexSrc,localDomainSrcSize); |
---|
| 56 | CArray<double,2> boundsLatSrc(nVertexSrc,localDomainSrcSize); |
---|
| 57 | |
---|
[689] | 58 | if (CDomain::type_attr::rectilinear == domainSrc_->type) srcPole[2] = 1; |
---|
[688] | 59 | if (hasBoundSrc) // Suppose that domain source is curvilinear or unstructured |
---|
| 60 | { |
---|
| 61 | if (!domainSrc_->bounds_lon_2d.isEmpty()) |
---|
| 62 | { |
---|
| 63 | for (j = 0; j < njSrc; ++j) |
---|
| 64 | for (i = 0; i < niSrc; ++i) |
---|
| 65 | { |
---|
| 66 | k=j*niSrc+i; |
---|
| 67 | for(int n=0;n<nVertexSrc;++n) |
---|
| 68 | { |
---|
| 69 | boundsLonSrc(n,k) = domainSrc_->bounds_lon_2d(n,i,j); |
---|
| 70 | boundsLatSrc(n,k) = domainSrc_->bounds_lat_2d(n,i,j); |
---|
| 71 | } |
---|
| 72 | } |
---|
| 73 | } |
---|
| 74 | else |
---|
| 75 | { |
---|
| 76 | boundsLonSrc = domainSrc_->bounds_lon_1d; |
---|
| 77 | boundsLatSrc = domainSrc_->bounds_lat_1d; |
---|
| 78 | } |
---|
| 79 | } |
---|
| 80 | else // if domain source is rectilinear, not do anything now |
---|
| 81 | { |
---|
[753] | 82 | bool isNorthPole = false; |
---|
| 83 | bool isSouthPole = false; |
---|
[809] | 84 | CArray<double,1> lon_g ; |
---|
| 85 | CArray<double,1> lat_g ; |
---|
[753] | 86 | |
---|
[809] | 87 | if (!domainSrc_->lonvalue_1d.isEmpty() && !domainSrc_->latvalue_1d.isEmpty()) |
---|
[808] | 88 | { |
---|
[809] | 89 | domainSrc_->AllgatherRectilinearLonLat(domainSrc_->lonvalue_1d,domainSrc_->latvalue_1d, lon_g,lat_g) ; |
---|
[846] | 90 | } |
---|
| 91 | else if (! domainSrc_->latvalue_rectilinear_read_from_file.isEmpty() && ! domainSrc_->lonvalue_rectilinear_read_from_file.isEmpty() ) |
---|
[808] | 92 | { |
---|
[809] | 93 | lat_g=domainSrc_->latvalue_rectilinear_read_from_file ; |
---|
| 94 | lon_g=domainSrc_->lonvalue_rectilinear_read_from_file ; |
---|
[846] | 95 | } |
---|
| 96 | else if (!domainSrc_->lon_start.isEmpty() && !domainSrc_->lon_end.isEmpty() && |
---|
[809] | 97 | !domainSrc_->lat_start.isEmpty() && !domainSrc_->lat_end.isEmpty()) |
---|
[846] | 98 | { |
---|
| 99 | double step=(domainSrc_->lon_end-domainSrc_->lon_start)/domainSrc_->ni_glo ; |
---|
| 100 | for(int i=0; i<domainSrc_->ni_glo; ++i) lon_g(i)=domainSrc_->lon_start+i*step ; |
---|
| 101 | step=(domainSrc_->lat_end-domainSrc_->lat_start)/domainSrc_->nj_glo ; |
---|
| 102 | for(int i=0; i<domainSrc_->ni_glo; ++i) lat_g(i)=domainSrc_->lat_start+i*step ; |
---|
| 103 | } |
---|
| 104 | else ERROR("void CDomainAlgorithmInterpolate::computeRemap()",<<"Cannot compute bounds for rectilinear domain") ; |
---|
[808] | 105 | |
---|
[688] | 106 | nVertexSrc = constNVertex; |
---|
[809] | 107 | domainSrc_->fillInRectilinearBoundLonLat(lon_g,lat_g, boundsLonSrc, boundsLatSrc); |
---|
[688] | 108 | } |
---|
| 109 | |
---|
[743] | 110 | std::map<int,std::vector<std::pair<int,double> > > interpMapValueNorthPole; |
---|
| 111 | std::map<int,std::vector<std::pair<int,double> > > interpMapValueSouthPole; |
---|
| 112 | |
---|
[688] | 113 | int localDomainDestSize = domainDest_->i_index.numElements(); |
---|
| 114 | int niDest = domainDest_->ni.getValue(), njDest = domainDest_->nj.getValue(); |
---|
| 115 | bool hasBoundDest = domainDest_->hasBounds; |
---|
| 116 | if (hasBoundDest) nVertexDest = domainDest_->nvertex.getValue(); |
---|
| 117 | CArray<double,2> boundsLonDest(nVertexDest,localDomainDestSize); |
---|
| 118 | CArray<double,2> boundsLatDest(nVertexDest,localDomainDestSize); |
---|
| 119 | |
---|
[689] | 120 | if (CDomain::type_attr::rectilinear == domainDest_->type) dstPole[2] = 1; |
---|
[688] | 121 | if (hasBoundDest) |
---|
| 122 | { |
---|
| 123 | if (!domainDest_->bounds_lon_2d.isEmpty()) |
---|
| 124 | { |
---|
| 125 | for (j = 0; j < njDest; ++j) |
---|
| 126 | for (i = 0; i < niDest; ++i) |
---|
| 127 | { |
---|
| 128 | k=j*niDest+i; |
---|
| 129 | for(int n=0;n<nVertexDest;++n) |
---|
| 130 | { |
---|
| 131 | boundsLonDest(n,k) = domainDest_->bounds_lon_2d(n,i,j); |
---|
| 132 | boundsLatDest(n,k) = domainDest_->bounds_lat_2d(n,i,j); |
---|
| 133 | } |
---|
| 134 | } |
---|
| 135 | } |
---|
| 136 | else |
---|
| 137 | { |
---|
| 138 | boundsLonDest = domainDest_->bounds_lon_1d; |
---|
| 139 | boundsLatDest = domainDest_->bounds_lat_1d; |
---|
| 140 | } |
---|
| 141 | } |
---|
| 142 | else |
---|
| 143 | { |
---|
[753] | 144 | bool isNorthPole = false; |
---|
| 145 | bool isSouthPole = false; |
---|
[775] | 146 | if (std::abs(poleValue - std::abs(domainDest_->lat_start)) < NumTraits<double>::epsilon()) isNorthPole = true; |
---|
| 147 | if (std::abs(poleValue - std::abs(domainDest_->lat_end)) < NumTraits<double>::epsilon()) isSouthPole = true; |
---|
[809] | 148 | |
---|
| 149 | CArray<double,1> lon_g ; |
---|
| 150 | CArray<double,1> lat_g ; |
---|
| 151 | |
---|
| 152 | if (!domainDest_->lonvalue_1d.isEmpty() && !domainDest_->latvalue_1d.isEmpty()) |
---|
| 153 | { |
---|
| 154 | domainDest_->AllgatherRectilinearLonLat(domainDest_->lonvalue_1d,domainDest_->latvalue_1d, lon_g,lat_g) ; |
---|
| 155 | } |
---|
| 156 | else if (! domainDest_->latvalue_rectilinear_read_from_file.isEmpty() && ! domainDest_->lonvalue_rectilinear_read_from_file.isEmpty() ) |
---|
| 157 | { |
---|
| 158 | lat_g=domainDest_->latvalue_rectilinear_read_from_file ; |
---|
| 159 | lon_g=domainDest_->lonvalue_rectilinear_read_from_file ; |
---|
| 160 | } |
---|
| 161 | else if (!domainDest_->lon_start.isEmpty() && !domainDest_->lon_end.isEmpty() && |
---|
| 162 | !domainDest_->lat_start.isEmpty() && !domainDest_->lat_end.isEmpty()) |
---|
| 163 | { |
---|
| 164 | double step=(domainDest_->lon_end-domainDest_->lon_start)/domainDest_->ni_glo ; |
---|
| 165 | for(int i=0; i<domainDest_->ni_glo; ++i) lon_g(i)=domainDest_->lon_start+i*step ; |
---|
[821] | 166 | step=(domainDest_->lat_end-domainDest_->lat_start)/domainDest_->nj_glo ; |
---|
[809] | 167 | for(int i=0; i<domainDest_->ni_glo; ++i) lat_g(i)=domainDest_->lat_start+i*step ; |
---|
| 168 | } |
---|
| 169 | else ERROR("void CDomainAlgorithmInterpolate::computeRemap()",<<"Cannot compute bounds for rectilinear domain") ; |
---|
| 170 | if (std::abs(poleValue - std::abs(lat_g(0))) < NumTraits<double>::epsilon()) isNorthPole = true; |
---|
| 171 | if (std::abs(poleValue - std::abs(lat_g(domainDest_->nj_glo-1))) < NumTraits<double>::epsilon()) isSouthPole = true; |
---|
| 172 | |
---|
[821] | 173 | |
---|
| 174 | |
---|
| 175 | |
---|
[743] | 176 | if (isNorthPole && (0 == domainDest_->jbegin.getValue())) |
---|
| 177 | { |
---|
| 178 | int ibegin = domainDest_->ibegin.getValue(); |
---|
| 179 | for (i = 0; i < niDest; ++i) |
---|
| 180 | { |
---|
| 181 | interpMapValueNorthPole[i+ibegin]; |
---|
| 182 | } |
---|
| 183 | } |
---|
| 184 | |
---|
| 185 | if (isSouthPole && (domainDest_->nj_glo.getValue() == (domainDest_->jbegin.getValue() + njDest))) |
---|
| 186 | { |
---|
| 187 | int ibegin = domainDest_->ibegin.getValue(); |
---|
| 188 | int njGlo = domainDest_->nj_glo.getValue(); |
---|
| 189 | int niGlo = domainDest_->ni_glo.getValue(); |
---|
| 190 | for (i = 0; i < niDest; ++i) |
---|
| 191 | { |
---|
| 192 | k = (njGlo - 1)*niGlo + i + ibegin; |
---|
| 193 | interpMapValueSouthPole[k]; |
---|
| 194 | } |
---|
| 195 | } |
---|
| 196 | |
---|
[688] | 197 | // Ok, fill in boundary values for rectangular domain |
---|
[689] | 198 | nVertexDest = constNVertex; |
---|
[809] | 199 | domainDest_->fillInRectilinearBoundLonLat(lon_g,lat_g, boundsLonDest, boundsLatDest); |
---|
[688] | 200 | } |
---|
| 201 | |
---|
[709] | 202 | |
---|
| 203 | |
---|
[688] | 204 | // Ok, now use mapper to calculate |
---|
| 205 | int nSrcLocal = domainSrc_->i_index.numElements(); |
---|
| 206 | int nDstLocal = domainDest_->i_index.numElements(); |
---|
[709] | 207 | long int * globalSrc = new long int [nSrcLocal]; |
---|
| 208 | long int * globalDst = new long int [nDstLocal]; |
---|
| 209 | |
---|
| 210 | long int globalIndex; |
---|
| 211 | int i_ind, j_ind; |
---|
| 212 | for (int idx = 0; idx < nSrcLocal; ++idx) |
---|
| 213 | { |
---|
| 214 | i_ind=domainSrc_->i_index(idx) ; |
---|
| 215 | j_ind=domainSrc_->j_index(idx) ; |
---|
| 216 | |
---|
| 217 | globalIndex = i_ind + j_ind * domainSrc_->ni_glo; |
---|
| 218 | globalSrc[idx] = globalIndex; |
---|
| 219 | } |
---|
| 220 | |
---|
| 221 | for (int idx = 0; idx < nDstLocal; ++idx) |
---|
| 222 | { |
---|
| 223 | i_ind=domainDest_->i_index(idx) ; |
---|
| 224 | j_ind=domainDest_->j_index(idx) ; |
---|
| 225 | |
---|
| 226 | globalIndex = i_ind + j_ind * domainDest_->ni_glo; |
---|
| 227 | globalDst[idx] = globalIndex; |
---|
| 228 | } |
---|
| 229 | |
---|
| 230 | |
---|
| 231 | // Calculate weight index |
---|
[688] | 232 | Mapper mapper(client->intraComm); |
---|
| 233 | mapper.setVerbosity(PROGRESS) ; |
---|
| 234 | |
---|
[880] | 235 | |
---|
[846] | 236 | // supress masked data for the source |
---|
| 237 | int nSrcLocalUnmasked = 0 ; |
---|
[893] | 238 | for (int idx=0 ; idx < nSrcLocal; idx++) if (domainSrc_->localMask(idx)) ++nSrcLocalUnmasked ; |
---|
[846] | 239 | |
---|
[880] | 240 | |
---|
[846] | 241 | CArray<double,2> boundsLonSrcUnmasked(nVertexSrc,nSrcLocalUnmasked); |
---|
| 242 | CArray<double,2> boundsLatSrcUnmasked(nVertexSrc,nSrcLocalUnmasked); |
---|
| 243 | long int * globalSrcUnmasked = new long int [nSrcLocalUnmasked]; |
---|
| 244 | |
---|
| 245 | nSrcLocalUnmasked=0 ; |
---|
| 246 | for (int idx=0 ; idx < nSrcLocal; idx++) |
---|
| 247 | { |
---|
[893] | 248 | if (domainSrc_->localMask(idx)) |
---|
[846] | 249 | { |
---|
| 250 | for(int n=0;n<nVertexSrc;++n) |
---|
| 251 | { |
---|
| 252 | boundsLonSrcUnmasked(n,nSrcLocalUnmasked) = boundsLonSrc(n,idx) ; |
---|
| 253 | boundsLatSrcUnmasked(n,nSrcLocalUnmasked) = boundsLatSrc(n,idx) ; |
---|
| 254 | } |
---|
| 255 | globalSrcUnmasked[nSrcLocalUnmasked]=globalSrc[idx] ; |
---|
| 256 | ++nSrcLocalUnmasked ; |
---|
| 257 | } |
---|
| 258 | } |
---|
| 259 | |
---|
[893] | 260 | |
---|
[846] | 261 | int nDstLocalUnmasked = 0 ; |
---|
[893] | 262 | for (int idx=0 ; idx < nDstLocal; idx++) if (domainDest_->localMask(idx)) ++nDstLocalUnmasked ; |
---|
[846] | 263 | |
---|
| 264 | CArray<double,2> boundsLonDestUnmasked(nVertexDest,nDstLocalUnmasked); |
---|
| 265 | CArray<double,2> boundsLatDestUnmasked(nVertexDest,nDstLocalUnmasked); |
---|
| 266 | long int * globalDstUnmasked = new long int [nDstLocalUnmasked]; |
---|
| 267 | |
---|
| 268 | nDstLocalUnmasked=0 ; |
---|
| 269 | for (int idx=0 ; idx < nDstLocal; idx++) |
---|
| 270 | { |
---|
[893] | 271 | if (domainDest_->localMask(idx)) |
---|
[846] | 272 | { |
---|
| 273 | for(int n=0;n<nVertexDest;++n) |
---|
| 274 | { |
---|
| 275 | boundsLonDestUnmasked(n,nDstLocalUnmasked) = boundsLonDest(n,idx) ; |
---|
| 276 | boundsLatDestUnmasked(n,nDstLocalUnmasked) = boundsLatDest(n,idx) ; |
---|
| 277 | } |
---|
| 278 | globalDstUnmasked[nDstLocalUnmasked]=globalDst[idx] ; |
---|
| 279 | ++nDstLocalUnmasked ; |
---|
| 280 | } |
---|
| 281 | } |
---|
[856] | 282 | |
---|
[846] | 283 | mapper.setSourceMesh(boundsLonSrcUnmasked.dataFirst(), boundsLatSrcUnmasked.dataFirst(), nVertexSrc, nSrcLocalUnmasked, &srcPole[0], globalSrcUnmasked); |
---|
| 284 | mapper.setTargetMesh(boundsLonDestUnmasked.dataFirst(), boundsLatDestUnmasked.dataFirst(), nVertexDest, nDstLocalUnmasked, &dstPole[0], globalDstUnmasked); |
---|
| 285 | |
---|
| 286 | std::vector<double> timings = mapper.computeWeights(orderInterp,renormalize); |
---|
| 287 | |
---|
[709] | 288 | std::map<int,std::vector<std::pair<int,double> > > interpMapValue; |
---|
[743] | 289 | std::map<int,std::vector<std::pair<int,double> > >::const_iterator iteNorthPole = interpMapValueNorthPole.end(), |
---|
| 290 | iteSouthPole = interpMapValueSouthPole.end(); |
---|
[688] | 291 | for (int idx = 0; idx < mapper.nWeights; ++idx) |
---|
| 292 | { |
---|
[709] | 293 | interpMapValue[mapper.targetWeightId[idx]].push_back(make_pair(mapper.sourceWeightId[idx],mapper.remapMatrix[idx])); |
---|
[743] | 294 | if (iteNorthPole != interpMapValueNorthPole.find(mapper.targetWeightId[idx])) |
---|
| 295 | { |
---|
| 296 | interpMapValueNorthPole[mapper.targetWeightId[idx]].push_back(make_pair(mapper.sourceWeightId[idx],mapper.remapMatrix[idx])); |
---|
| 297 | } |
---|
| 298 | |
---|
| 299 | if (iteSouthPole != interpMapValueSouthPole.find(mapper.targetWeightId[idx])) |
---|
| 300 | { |
---|
| 301 | interpMapValueSouthPole[mapper.targetWeightId[idx]].push_back(make_pair(mapper.sourceWeightId[idx],mapper.remapMatrix[idx])); |
---|
| 302 | } |
---|
[688] | 303 | } |
---|
[743] | 304 | int niGloDst = domainDest_->ni_glo.getValue(); |
---|
| 305 | processPole(interpMapValueNorthPole, niGloDst); |
---|
| 306 | processPole(interpMapValueSouthPole, niGloDst); |
---|
| 307 | |
---|
| 308 | if (!interpMapValueNorthPole.empty()) |
---|
| 309 | { |
---|
| 310 | std::map<int,std::vector<std::pair<int,double> > >::iterator itNorthPole = interpMapValueNorthPole.begin(); |
---|
| 311 | for (; itNorthPole != iteNorthPole; ++itNorthPole) |
---|
| 312 | { |
---|
| 313 | if (!(itNorthPole->second.empty())) |
---|
| 314 | itNorthPole->second.swap(interpMapValue[itNorthPole->first]); |
---|
| 315 | } |
---|
| 316 | } |
---|
| 317 | |
---|
| 318 | if (!interpMapValueSouthPole.empty()) |
---|
| 319 | { |
---|
| 320 | std::map<int,std::vector<std::pair<int,double> > >::iterator itSouthPole = interpMapValueSouthPole.begin(); |
---|
| 321 | for (; itSouthPole != iteSouthPole; ++itSouthPole) |
---|
| 322 | { |
---|
| 323 | if (!(itSouthPole->second.empty())) |
---|
| 324 | itSouthPole->second.swap(interpMapValue[itSouthPole->first]); |
---|
| 325 | } |
---|
| 326 | } |
---|
| 327 | |
---|
[709] | 328 | exchangeRemapInfo(interpMapValue); |
---|
| 329 | |
---|
| 330 | delete [] globalSrc; |
---|
[846] | 331 | delete [] globalSrcUnmasked; |
---|
[709] | 332 | delete [] globalDst; |
---|
[846] | 333 | delete [] globalDstUnmasked; |
---|
| 334 | |
---|
[688] | 335 | } |
---|
| 336 | |
---|
[743] | 337 | void CDomainAlgorithmInterpolate::processPole(std::map<int,std::vector<std::pair<int,double> > >& interMapValuePole, |
---|
| 338 | int nbGlobalPointOnPole) |
---|
| 339 | { |
---|
| 340 | CContext* context = CContext::getCurrent(); |
---|
| 341 | CContextClient* client=context->client; |
---|
| 342 | |
---|
| 343 | MPI_Comm poleComme(MPI_COMM_NULL); |
---|
| 344 | MPI_Comm_split(client->intraComm, interMapValuePole.empty() ? MPI_UNDEFINED : 1, 0, &poleComme); |
---|
| 345 | if (MPI_COMM_NULL != poleComme) |
---|
| 346 | { |
---|
| 347 | int nbClientPole; |
---|
| 348 | MPI_Comm_size(poleComme, &nbClientPole); |
---|
| 349 | |
---|
| 350 | std::map<int,std::vector<std::pair<int,double> > >::iterator itePole = interMapValuePole.end(), itPole, |
---|
| 351 | itbPole = interMapValuePole.begin(); |
---|
| 352 | |
---|
| 353 | int nbWeight = 0; |
---|
| 354 | for (itPole = itbPole; itPole != itePole; ++itPole) |
---|
| 355 | nbWeight += itPole->second.size(); |
---|
| 356 | |
---|
| 357 | std::vector<int> recvCount(nbClientPole,0); |
---|
| 358 | std::vector<int> displ(nbClientPole,0); |
---|
| 359 | MPI_Allgather(&nbWeight,1,MPI_INT,&recvCount[0],1,MPI_INT,poleComme) ; |
---|
| 360 | |
---|
| 361 | displ[0]=0; |
---|
| 362 | for(int n=1;n<nbClientPole;++n) displ[n]=displ[n-1]+recvCount[n-1] ; |
---|
| 363 | int recvSize=displ[nbClientPole-1]+recvCount[nbClientPole-1] ; |
---|
| 364 | |
---|
| 365 | std::vector<int> sendSourceIndexBuff(nbWeight); |
---|
| 366 | std::vector<double> sendSourceWeightBuff(nbWeight); |
---|
| 367 | int k = 0; |
---|
| 368 | for (itPole = itbPole; itPole != itePole; ++itPole) |
---|
| 369 | { |
---|
| 370 | for (int idx = 0; idx < itPole->second.size(); ++idx) |
---|
| 371 | { |
---|
| 372 | sendSourceIndexBuff[k] = (itPole->second)[idx].first; |
---|
| 373 | sendSourceWeightBuff[k] = (itPole->second)[idx].second; |
---|
| 374 | ++k; |
---|
| 375 | } |
---|
| 376 | } |
---|
| 377 | |
---|
| 378 | std::vector<int> recvSourceIndexBuff(recvSize); |
---|
| 379 | std::vector<double> recvSourceWeightBuff(recvSize); |
---|
| 380 | |
---|
| 381 | // Gather all index and weight for pole |
---|
| 382 | MPI_Allgatherv(&sendSourceIndexBuff[0],nbWeight,MPI_INT,&recvSourceIndexBuff[0],&recvCount[0],&displ[0],MPI_INT,poleComme); |
---|
| 383 | MPI_Allgatherv(&sendSourceWeightBuff[0],nbWeight,MPI_DOUBLE,&recvSourceWeightBuff[0],&recvCount[0],&displ[0],MPI_DOUBLE,poleComme); |
---|
| 384 | |
---|
| 385 | std::map<int,double> recvTemp; |
---|
| 386 | for (int idx = 0; idx < recvSize; ++idx) |
---|
| 387 | { |
---|
| 388 | if (recvTemp.end() != recvTemp.find(recvSourceIndexBuff[idx])) |
---|
| 389 | recvTemp[recvSourceIndexBuff[idx]] += recvSourceWeightBuff[idx]/nbGlobalPointOnPole; |
---|
| 390 | else |
---|
| 391 | recvTemp[recvSourceIndexBuff[idx]] = 0.0; |
---|
| 392 | } |
---|
| 393 | |
---|
| 394 | std::map<int,double>::const_iterator itRecvTemp, itbRecvTemp = recvTemp.begin(), iteRecvTemp = recvTemp.end(); |
---|
| 395 | |
---|
| 396 | for (itPole = itbPole; itPole != itePole; ++itPole) |
---|
| 397 | { |
---|
| 398 | itPole->second.clear(); |
---|
| 399 | for (itRecvTemp = itbRecvTemp; itRecvTemp != iteRecvTemp; ++itRecvTemp) |
---|
| 400 | itPole->second.push_back(make_pair(itRecvTemp->first, itRecvTemp->second)); |
---|
| 401 | } |
---|
| 402 | } |
---|
| 403 | |
---|
| 404 | } |
---|
| 405 | |
---|
[689] | 406 | /*! |
---|
| 407 | Compute the index mapping between domain on grid source and one on grid destination |
---|
| 408 | */ |
---|
[827] | 409 | void CDomainAlgorithmInterpolate::computeIndexSourceMapping_(const std::vector<CArray<double,1>* >& dataAuxInputs) |
---|
[688] | 410 | { |
---|
[689] | 411 | if (!interpDomain_->file.isEmpty()) |
---|
| 412 | readRemapInfo(); |
---|
| 413 | else |
---|
| 414 | computeRemap(); |
---|
[688] | 415 | } |
---|
| 416 | |
---|
[689] | 417 | void CDomainAlgorithmInterpolate::readRemapInfo() |
---|
[657] | 418 | { |
---|
| 419 | CContext* context = CContext::getCurrent(); |
---|
| 420 | CContextClient* client=context->client; |
---|
| 421 | int clientRank = client->clientRank; |
---|
| 422 | |
---|
| 423 | std::string filename = interpDomain_->file.getValue(); |
---|
| 424 | std::map<int,std::vector<std::pair<int,double> > > interpMapValue; |
---|
[660] | 425 | readInterpolationInfo(filename, interpMapValue); |
---|
[657] | 426 | |
---|
[709] | 427 | exchangeRemapInfo(interpMapValue); |
---|
| 428 | } |
---|
| 429 | |
---|
| 430 | |
---|
| 431 | /*! |
---|
| 432 | Read remap information from file then distribute it among clients |
---|
| 433 | */ |
---|
[856] | 434 | void CDomainAlgorithmInterpolate::exchangeRemapInfo(std::map<int,std::vector<std::pair<int,double> > >& interpMapValue) |
---|
[709] | 435 | { |
---|
| 436 | CContext* context = CContext::getCurrent(); |
---|
| 437 | CContextClient* client=context->client; |
---|
| 438 | int clientRank = client->clientRank; |
---|
| 439 | |
---|
[827] | 440 | this->transformationMapping_.resize(1); |
---|
| 441 | this->transformationWeight_.resize(1); |
---|
| 442 | |
---|
[833] | 443 | TransformationIndexMap& transMap = this->transformationMapping_[0]; |
---|
| 444 | TransformationWeightMap& transWeight = this->transformationWeight_[0]; |
---|
[827] | 445 | |
---|
[657] | 446 | boost::unordered_map<size_t,int> globalIndexOfDomainDest; |
---|
| 447 | int ni = domainDest_->ni.getValue(); |
---|
| 448 | int nj = domainDest_->nj.getValue(); |
---|
| 449 | int ni_glo = domainDest_->ni_glo.getValue(); |
---|
| 450 | size_t globalIndex; |
---|
| 451 | int nIndexSize = domainDest_->i_index.numElements(), i_ind, j_ind; |
---|
| 452 | for (int idx = 0; idx < nIndexSize; ++idx) |
---|
| 453 | { |
---|
| 454 | i_ind=domainDest_->i_index(idx) ; |
---|
| 455 | j_ind=domainDest_->j_index(idx) ; |
---|
| 456 | |
---|
| 457 | globalIndex = i_ind + j_ind * ni_glo; |
---|
| 458 | globalIndexOfDomainDest[globalIndex] = clientRank; |
---|
| 459 | } |
---|
| 460 | |
---|
| 461 | CClientServerMappingDistributed domainIndexClientClientMapping(globalIndexOfDomainDest, |
---|
| 462 | client->intraComm, |
---|
| 463 | true); |
---|
| 464 | CArray<size_t,1> globalIndexInterp(interpMapValue.size()); |
---|
| 465 | std::map<int,std::vector<std::pair<int,double> > >::const_iterator itb = interpMapValue.begin(), it, |
---|
| 466 | ite = interpMapValue.end(); |
---|
| 467 | size_t globalIndexCount = 0; |
---|
| 468 | for (it = itb; it != ite; ++it) |
---|
| 469 | { |
---|
| 470 | globalIndexInterp(globalIndexCount) = it->first; |
---|
| 471 | ++globalIndexCount; |
---|
| 472 | } |
---|
| 473 | |
---|
| 474 | domainIndexClientClientMapping.computeServerIndexMapping(globalIndexInterp); |
---|
[829] | 475 | const CClientServerMapping::GlobalIndexMap& globalIndexInterpSendToClient = domainIndexClientClientMapping.getGlobalIndexOnServer(); |
---|
[657] | 476 | |
---|
| 477 | //Inform each client number of index they will receive |
---|
| 478 | int nbClient = client->clientSize; |
---|
| 479 | int* sendBuff = new int[nbClient]; |
---|
| 480 | int* recvBuff = new int[nbClient]; |
---|
[709] | 481 | for (int i = 0; i < nbClient; ++i) |
---|
| 482 | { |
---|
| 483 | sendBuff[i] = 0; |
---|
| 484 | recvBuff[i] = 0; |
---|
| 485 | } |
---|
[657] | 486 | int sendBuffSize = 0; |
---|
[829] | 487 | CClientServerMapping::GlobalIndexMap::const_iterator itbMap = globalIndexInterpSendToClient.begin(), itMap, |
---|
| 488 | iteMap = globalIndexInterpSendToClient.end(); |
---|
[657] | 489 | for (itMap = itbMap; itMap != iteMap; ++itMap) |
---|
| 490 | { |
---|
[709] | 491 | const std::vector<size_t>& tmp = itMap->second; |
---|
[657] | 492 | int sizeIndex = 0, mapSize = (itMap->second).size(); |
---|
| 493 | for (int idx = 0; idx < mapSize; ++idx) |
---|
| 494 | { |
---|
[856] | 495 | // sizeIndex += interpMapValue.at((itMap->second)[idx]).size(); |
---|
| 496 | sizeIndex += (interpMapValue[(int)(itMap->second)[idx]]).size(); |
---|
[657] | 497 | } |
---|
| 498 | sendBuff[itMap->first] = sizeIndex; |
---|
| 499 | sendBuffSize += sizeIndex; |
---|
| 500 | } |
---|
| 501 | |
---|
[709] | 502 | |
---|
[657] | 503 | MPI_Allreduce(sendBuff, recvBuff, nbClient, MPI_INT, MPI_SUM, client->intraComm); |
---|
| 504 | |
---|
| 505 | int* sendIndexDestBuff = new int [sendBuffSize]; |
---|
| 506 | int* sendIndexSrcBuff = new int [sendBuffSize]; |
---|
| 507 | double* sendWeightBuff = new double [sendBuffSize]; |
---|
| 508 | |
---|
| 509 | std::vector<MPI_Request> sendRequest; |
---|
[709] | 510 | |
---|
| 511 | int sendOffSet = 0, l = 0; |
---|
[657] | 512 | for (itMap = itbMap; itMap != iteMap; ++itMap) |
---|
| 513 | { |
---|
[709] | 514 | const std::vector<size_t>& indexToSend = itMap->second; |
---|
| 515 | int mapSize = indexToSend.size(); |
---|
[657] | 516 | int k = 0; |
---|
| 517 | for (int idx = 0; idx < mapSize; ++idx) |
---|
| 518 | { |
---|
[856] | 519 | std::vector<std::pair<int,double> >& interpMap = interpMapValue[(int)indexToSend[idx]]; //interpMapValue.at(indexToSend[idx]); |
---|
[657] | 520 | for (int i = 0; i < interpMap.size(); ++i) |
---|
| 521 | { |
---|
[709] | 522 | sendIndexDestBuff[l] = indexToSend[idx]; |
---|
| 523 | sendIndexSrcBuff[l] = interpMap[i].first; |
---|
| 524 | sendWeightBuff[l] = interpMap[i].second; |
---|
[657] | 525 | ++k; |
---|
[709] | 526 | ++l; |
---|
[657] | 527 | } |
---|
| 528 | } |
---|
| 529 | |
---|
| 530 | sendRequest.push_back(MPI_Request()); |
---|
| 531 | MPI_Isend(sendIndexDestBuff + sendOffSet, |
---|
| 532 | k, |
---|
| 533 | MPI_INT, |
---|
| 534 | itMap->first, |
---|
[821] | 535 | MPI_DOMAIN_INTERPOLATION_DEST_INDEX, |
---|
[657] | 536 | client->intraComm, |
---|
| 537 | &sendRequest.back()); |
---|
| 538 | sendRequest.push_back(MPI_Request()); |
---|
| 539 | MPI_Isend(sendIndexSrcBuff + sendOffSet, |
---|
| 540 | k, |
---|
| 541 | MPI_INT, |
---|
| 542 | itMap->first, |
---|
[821] | 543 | MPI_DOMAIN_INTERPOLATION_SRC_INDEX, |
---|
[657] | 544 | client->intraComm, |
---|
| 545 | &sendRequest.back()); |
---|
| 546 | sendRequest.push_back(MPI_Request()); |
---|
| 547 | MPI_Isend(sendWeightBuff + sendOffSet, |
---|
| 548 | k, |
---|
| 549 | MPI_DOUBLE, |
---|
| 550 | itMap->first, |
---|
[821] | 551 | MPI_DOMAIN_INTERPOLATION_WEIGHT, |
---|
[657] | 552 | client->intraComm, |
---|
| 553 | &sendRequest.back()); |
---|
| 554 | sendOffSet += k; |
---|
| 555 | } |
---|
| 556 | |
---|
| 557 | int recvBuffSize = recvBuff[clientRank]; |
---|
| 558 | int* recvIndexDestBuff = new int [recvBuffSize]; |
---|
| 559 | int* recvIndexSrcBuff = new int [recvBuffSize]; |
---|
| 560 | double* recvWeightBuff = new double [recvBuffSize]; |
---|
| 561 | int receivedSize = 0; |
---|
| 562 | int clientSrcRank; |
---|
| 563 | while (receivedSize < recvBuffSize) |
---|
| 564 | { |
---|
| 565 | MPI_Status recvStatus; |
---|
| 566 | MPI_Recv((recvIndexDestBuff + receivedSize), |
---|
| 567 | recvBuffSize, |
---|
| 568 | MPI_INT, |
---|
| 569 | MPI_ANY_SOURCE, |
---|
[821] | 570 | MPI_DOMAIN_INTERPOLATION_DEST_INDEX, |
---|
[657] | 571 | client->intraComm, |
---|
| 572 | &recvStatus); |
---|
| 573 | |
---|
| 574 | int countBuff = 0; |
---|
| 575 | MPI_Get_count(&recvStatus, MPI_INT, &countBuff); |
---|
| 576 | clientSrcRank = recvStatus.MPI_SOURCE; |
---|
| 577 | |
---|
| 578 | MPI_Recv((recvIndexSrcBuff + receivedSize), |
---|
| 579 | recvBuffSize, |
---|
| 580 | MPI_INT, |
---|
| 581 | clientSrcRank, |
---|
[821] | 582 | MPI_DOMAIN_INTERPOLATION_SRC_INDEX, |
---|
[657] | 583 | client->intraComm, |
---|
| 584 | &recvStatus); |
---|
| 585 | |
---|
| 586 | MPI_Recv((recvWeightBuff + receivedSize), |
---|
| 587 | recvBuffSize, |
---|
| 588 | MPI_DOUBLE, |
---|
| 589 | clientSrcRank, |
---|
[821] | 590 | MPI_DOMAIN_INTERPOLATION_WEIGHT, |
---|
[657] | 591 | client->intraComm, |
---|
| 592 | &recvStatus); |
---|
| 593 | |
---|
| 594 | for (int idx = 0; idx < countBuff; ++idx) |
---|
| 595 | { |
---|
[827] | 596 | transMap[*(recvIndexDestBuff + receivedSize + idx)].push_back(*(recvIndexSrcBuff + receivedSize + idx)); |
---|
| 597 | transWeight[*(recvIndexDestBuff + receivedSize + idx)].push_back(*(recvWeightBuff + receivedSize + idx)); |
---|
[657] | 598 | } |
---|
| 599 | receivedSize += countBuff; |
---|
| 600 | } |
---|
| 601 | |
---|
| 602 | std::vector<MPI_Status> requestStatus(sendRequest.size()); |
---|
[709] | 603 | MPI_Waitall(sendRequest.size(), &sendRequest[0], MPI_STATUS_IGNORE); |
---|
[657] | 604 | |
---|
| 605 | delete [] sendIndexDestBuff; |
---|
| 606 | delete [] sendIndexSrcBuff; |
---|
| 607 | delete [] sendWeightBuff; |
---|
| 608 | delete [] recvIndexDestBuff; |
---|
| 609 | delete [] recvIndexSrcBuff; |
---|
| 610 | delete [] recvWeightBuff; |
---|
| 611 | delete [] sendBuff; |
---|
| 612 | delete [] recvBuff; |
---|
| 613 | } |
---|
| 614 | |
---|
| 615 | /*! |
---|
| 616 | Read interpolation information from a file |
---|
| 617 | \param [in] filename interpolation file |
---|
| 618 | \param [in/out] interpMapValue Mapping between (global) index of domain on grid destination and |
---|
| 619 | corresponding global index of domain and associated weight value on grid source |
---|
| 620 | */ |
---|
[689] | 621 | void CDomainAlgorithmInterpolate::readInterpolationInfo(std::string& filename, |
---|
[709] | 622 | std::map<int,std::vector<std::pair<int,double> > >& interpMapValue) |
---|
[657] | 623 | { |
---|
[660] | 624 | int ncid ; |
---|
| 625 | int weightDimId ; |
---|
| 626 | size_t nbWeightGlo ; |
---|
[657] | 627 | |
---|
[660] | 628 | CContext* context = CContext::getCurrent(); |
---|
| 629 | CContextClient* client=context->client; |
---|
| 630 | int clientRank = client->clientRank; |
---|
| 631 | int clientSize = client->clientSize; |
---|
[663] | 632 | |
---|
[660] | 633 | nc_open(filename.c_str(),NC_NOWRITE, &ncid) ; |
---|
| 634 | nc_inq_dimid(ncid,"n_weight",&weightDimId) ; |
---|
| 635 | nc_inq_dimlen(ncid,weightDimId,&nbWeightGlo) ; |
---|
| 636 | |
---|
| 637 | size_t nbWeight ; |
---|
| 638 | size_t start ; |
---|
| 639 | size_t div = nbWeightGlo/clientSize ; |
---|
| 640 | size_t mod = nbWeightGlo%clientSize ; |
---|
| 641 | if (clientRank < mod) |
---|
| 642 | { |
---|
| 643 | nbWeight=div+1 ; |
---|
| 644 | start=clientRank*(div+1) ; |
---|
| 645 | } |
---|
| 646 | else |
---|
| 647 | { |
---|
| 648 | nbWeight=div ; |
---|
| 649 | start= mod * (div+1) + (clientRank-mod) * div ; |
---|
| 650 | } |
---|
| 651 | |
---|
| 652 | double* weight=new double[nbWeight] ; |
---|
| 653 | int weightId ; |
---|
| 654 | nc_inq_varid (ncid, "weight", &weightId) ; |
---|
| 655 | nc_get_vara_double(ncid, weightId, &start, &nbWeight, weight) ; |
---|
| 656 | |
---|
[663] | 657 | long* srcIndex=new long[nbWeight] ; |
---|
[660] | 658 | int srcIndexId ; |
---|
| 659 | nc_inq_varid (ncid, "src_idx", &srcIndexId) ; |
---|
| 660 | nc_get_vara_long(ncid, srcIndexId, &start, &nbWeight, srcIndex) ; |
---|
| 661 | |
---|
[663] | 662 | long* dstIndex=new long[nbWeight] ; |
---|
[660] | 663 | int dstIndexId ; |
---|
| 664 | nc_inq_varid (ncid, "dst_idx", &dstIndexId) ; |
---|
| 665 | nc_get_vara_long(ncid, dstIndexId, &start, &nbWeight, dstIndex) ; |
---|
[663] | 666 | |
---|
[660] | 667 | for(size_t ind=0; ind<nbWeight;++ind) |
---|
[663] | 668 | interpMapValue[dstIndex[ind]-1].push_back(make_pair(srcIndex[ind]-1,weight[ind])); |
---|
[657] | 669 | } |
---|
| 670 | |
---|
| 671 | } |
---|