[687] | 1 | /*! |
---|
| 2 | \file domain_algorithm_generate_rectilinear.cpp |
---|
| 3 | \author Ha NGUYEN |
---|
| 4 | \since 31 Aug 2015 |
---|
| 5 | \date 31 Aug 2015 |
---|
| 6 | |
---|
| 7 | \brief Algorithm for automatic generation of rectilinear domain. |
---|
| 8 | */ |
---|
| 9 | #include "domain_algorithm_generate_rectilinear.hpp" |
---|
| 10 | #include "grid.hpp" |
---|
| 11 | #include "domain.hpp" |
---|
| 12 | #include "context.hpp" |
---|
| 13 | #include "context_client.hpp" |
---|
| 14 | #include "generate_rectilinear_domain.hpp" |
---|
| 15 | |
---|
| 16 | namespace xios { |
---|
| 17 | |
---|
| 18 | CDomainAlgorithmGenerateRectilinear::CDomainAlgorithmGenerateRectilinear(CDomain* domainDestination, CDomain* domainSource, |
---|
[775] | 19 | CGrid* gridDest, CGrid* gridSource, |
---|
| 20 | CGenerateRectilinearDomain* genRectDomain) |
---|
| 21 | : CDomainAlgorithmTransformation(domainDestination, domainSource), nbDomainDistributedPart_(0) |
---|
[1642] | 22 | TRY |
---|
[687] | 23 | { |
---|
[933] | 24 | type_ = ELEMENT_GENERATION; |
---|
[727] | 25 | genRectDomain->checkValid(domainDestination); |
---|
[775] | 26 | if (0 != gridSource) computeDistributionGridSource(gridSource); |
---|
| 27 | else |
---|
| 28 | { |
---|
| 29 | computeDistributionGridDestination(gridDest); |
---|
| 30 | } |
---|
[687] | 31 | fillInAttributesDomainDestination(); |
---|
| 32 | } |
---|
[1642] | 33 | CATCH |
---|
[687] | 34 | |
---|
| 35 | /*! |
---|
| 36 | Compute the index mapping between domain on grid source and one on grid destination |
---|
| 37 | */ |
---|
[827] | 38 | void CDomainAlgorithmGenerateRectilinear::computeIndexSourceMapping_(const std::vector<CArray<double,1>* >& dataAuxInputs) |
---|
[687] | 39 | { |
---|
| 40 | /* Nothing to do */ |
---|
| 41 | } |
---|
| 42 | |
---|
| 43 | /*! |
---|
| 44 | Calculate the number of distributed parts on domain source |
---|
| 45 | */ |
---|
[775] | 46 | void CDomainAlgorithmGenerateRectilinear::computeDistributionGridSource(CGrid* gridSrc) |
---|
[1642] | 47 | TRY |
---|
[687] | 48 | { |
---|
| 49 | CContext* context = CContext::getCurrent(); |
---|
| 50 | CContextClient* client = context->client; |
---|
| 51 | |
---|
[775] | 52 | std::vector<CDomain*> domListSrcP = gridSrc->getDomains(); |
---|
| 53 | std::vector<CAxis*> axisListSrcP = gridSrc->getAxis(); |
---|
[687] | 54 | |
---|
| 55 | for (int i = 0; i < domListSrcP.size(); ++i) // support we have only domain, more than one, for now, dont know how to process |
---|
| 56 | { |
---|
| 57 | // First, find (roundly) distribution of associated axis (if any) |
---|
| 58 | if (axisListSrcP.empty()) nbDomainDistributedPart_ = client->clientSize; |
---|
| 59 | else |
---|
| 60 | { |
---|
[775] | 61 | gridSrc->solveAxisRef(false); |
---|
[687] | 62 | int nbAxis = axisListSrcP.size(); |
---|
| 63 | std::vector<int> nbLocalAxis(nbAxis, 0); |
---|
| 64 | for (int j = 0; j < nbAxis; ++j) |
---|
| 65 | { |
---|
| 66 | std::vector<int> globalAxisIndex(axisListSrcP[j]->n); |
---|
| 67 | for (int idx = 0; idx < axisListSrcP[j]->n; ++idx) |
---|
| 68 | globalAxisIndex[idx] = axisListSrcP[j]->begin + idx; |
---|
| 69 | HashXIOS<int> hashFunc; |
---|
| 70 | StdSize hashValue = hashFunc.hashVec(globalAxisIndex); |
---|
| 71 | std::vector<StdSize> recvBuff(client->clientSize); |
---|
[1642] | 72 | ep_lib::MPI_Gather(&hashValue, 1, EP_UNSIGNED_LONG, |
---|
| 73 | &recvBuff[0], 1, EP_UNSIGNED_LONG, |
---|
[687] | 74 | 0, |
---|
| 75 | client->intraComm); |
---|
| 76 | if (0 == client->clientRank) |
---|
| 77 | { |
---|
| 78 | std::set<StdSize> setTmp; |
---|
| 79 | for (int k = 0; k < recvBuff.size(); ++k) |
---|
| 80 | { |
---|
| 81 | if (setTmp.end() == setTmp.find(recvBuff[k])) |
---|
| 82 | { |
---|
| 83 | ++nbLocalAxis[j]; |
---|
| 84 | setTmp.insert(recvBuff[k]); |
---|
| 85 | } |
---|
| 86 | } |
---|
| 87 | } |
---|
| 88 | |
---|
[1642] | 89 | ep_lib::MPI_Bcast(&nbLocalAxis[0], nbAxis, EP_INT, |
---|
[687] | 90 | 0, client->intraComm); |
---|
| 91 | } |
---|
| 92 | |
---|
| 93 | int nbAxisDistributedPart = 1; |
---|
| 94 | for (int j = 0; j < nbAxis; ++j) nbAxisDistributedPart *= nbLocalAxis[j]; |
---|
| 95 | nbDomainDistributedPart_ = client->clientSize/nbAxisDistributedPart; |
---|
| 96 | } |
---|
| 97 | } |
---|
| 98 | } |
---|
[1642] | 99 | CATCH |
---|
[687] | 100 | |
---|
| 101 | /*! |
---|
[775] | 102 | Compute the distribution of the domain destination by using available information provided by user such as n_distributed_partition of axis |
---|
| 103 | */ |
---|
| 104 | void CDomainAlgorithmGenerateRectilinear::computeDistributionGridDestination(CGrid* gridDest) |
---|
[1642] | 105 | TRY |
---|
[775] | 106 | { |
---|
| 107 | // For now, just suppose that the grid contains only one domain |
---|
| 108 | std::vector<CAxis*> axisListDestP = gridDest->getAxis(); |
---|
| 109 | int nbPartition = 1, idx = 0; |
---|
| 110 | for (int i = 0; i < gridDest->axis_domain_order.numElements(); ++i) |
---|
| 111 | { |
---|
| 112 | if (false == (gridDest->axis_domain_order)(i)) |
---|
| 113 | { |
---|
| 114 | nbPartition *= (axisListDestP[idx]->n_distributed_partition.isEmpty()) ? 1: (axisListDestP[idx]->n_distributed_partition.getValue()); |
---|
| 115 | ++idx; |
---|
| 116 | } |
---|
| 117 | } |
---|
| 118 | |
---|
| 119 | CContext* context = CContext::getCurrent(); |
---|
| 120 | CContextClient* client = context->client; |
---|
| 121 | int modPart = (client->clientSize) % nbPartition; |
---|
| 122 | if (0 != modPart) |
---|
| 123 | ERROR("CDomainAlgorithmGenerateRectilinear::computeDistributionGridDestination(CGrid* gridDest)", |
---|
| 124 | << "The grid " <<gridDest->getId() << " is not well-distributed. There is an incompatibility between distribution of axis and domain."); |
---|
| 125 | nbDomainDistributedPart_ = client->clientSize/nbPartition; |
---|
| 126 | |
---|
| 127 | } |
---|
[1642] | 128 | CATCH |
---|
[775] | 129 | |
---|
| 130 | /*! |
---|
[687] | 131 | Fill in all necessary attributes of domain destination and their values |
---|
| 132 | */ |
---|
| 133 | void CDomainAlgorithmGenerateRectilinear::fillInAttributesDomainDestination() |
---|
[1642] | 134 | TRY |
---|
[687] | 135 | { |
---|
[1064] | 136 | if (!domainDest_->distributionAttributesHaveValue()) |
---|
| 137 | domainDest_->redistribute(nbDomainDistributedPart_); |
---|
| 138 | domainDest_->fillInLonLat(); |
---|
[687] | 139 | } |
---|
[1642] | 140 | CATCH |
---|
[687] | 141 | } |
---|