2016-11-11 08:02:34 -06:00
|
|
|
// -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
|
|
|
// vi: set et ts=4 sw=4 sts=4:
|
|
|
|
/*
|
|
|
|
This file is part of the Open Porous Media project (OPM).
|
|
|
|
|
|
|
|
OPM is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
OPM is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with OPM. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
Consult the COPYING file in the top-level source directory of this
|
|
|
|
module for the precise wording of the license and the list of
|
|
|
|
copyright holders.
|
|
|
|
*/
|
2018-07-12 03:23:26 -05:00
|
|
|
#ifndef EWOMS_COLLECT_TO_IO_RANK_HH
|
|
|
|
#define EWOMS_COLLECT_TO_IO_RANK_HH
|
2016-11-11 08:02:34 -06:00
|
|
|
|
2017-12-07 05:38:48 -06:00
|
|
|
#include <opm/output/data/Cells.hpp>
|
|
|
|
#include <opm/output/data/Solution.hpp>
|
2018-02-09 02:22:38 -06:00
|
|
|
#include <opm/output/data/Wells.hpp>
|
2017-12-07 05:38:48 -06:00
|
|
|
|
2018-02-08 05:20:17 -06:00
|
|
|
#include <opm/grid/common/p2pcommunicator.hh>
|
2016-11-11 08:02:34 -06:00
|
|
|
#include <dune/grid/utility/persistentcontainer.hh>
|
|
|
|
#include <dune/grid/common/gridenums.hh>
|
|
|
|
|
|
|
|
#include <opm/parser/eclipse/EclipseState/EclipseState.hpp>
|
|
|
|
#include <opm/parser/eclipse/EclipseState/Grid/EclipseGrid.hpp>
|
|
|
|
|
2018-02-01 07:40:01 -06:00
|
|
|
#include <opm/material/common/Exceptions.hpp>
|
2018-03-12 08:31:36 -05:00
|
|
|
#include <opm/material/common/Unused.hpp>
|
2016-11-11 08:02:34 -06:00
|
|
|
|
|
|
|
#include <dune/grid/common/mcmgmapper.hh>
|
|
|
|
|
|
|
|
#include <stdexcept>
|
|
|
|
|
2018-07-12 03:36:48 -05:00
|
|
|
namespace Ewoms {
|
2018-07-12 03:26:11 -05:00
|
|
|
|
|
|
|
template <class Vanguard>
|
2018-07-12 03:16:20 -05:00
|
|
|
class CollectDataToIORank
|
|
|
|
{
|
|
|
|
public:
|
2018-07-12 03:26:11 -05:00
|
|
|
typedef typename Vanguard::Grid Grid;
|
|
|
|
typedef typename Grid::CollectiveCommunication CollectiveCommunication;
|
2018-07-12 03:16:20 -05:00
|
|
|
|
|
|
|
// global id
|
|
|
|
class GlobalCellIndex
|
2016-11-11 08:02:34 -06:00
|
|
|
{
|
2018-07-12 03:16:20 -05:00
|
|
|
int globalId_;
|
|
|
|
int localIndex_;
|
|
|
|
bool isInterior_;
|
2016-11-11 08:02:34 -06:00
|
|
|
|
2018-07-12 03:26:11 -05:00
|
|
|
public:
|
|
|
|
GlobalCellIndex()
|
|
|
|
: globalId_(-1)
|
|
|
|
, localIndex_(-1)
|
|
|
|
, isInterior_(true)
|
|
|
|
{}
|
|
|
|
void setGhost()
|
|
|
|
{ isInterior_ = false; }
|
|
|
|
|
|
|
|
void setId(int globalId)
|
|
|
|
{ globalId_ = globalId; }
|
|
|
|
void setIndex(int localIndex)
|
|
|
|
{ localIndex_ = localIndex; }
|
|
|
|
|
|
|
|
int localIndex () const
|
|
|
|
{ return localIndex_; }
|
|
|
|
int id () const
|
|
|
|
{ return globalId_; }
|
|
|
|
bool isInterior() const
|
|
|
|
{ return isInterior_; }
|
2018-07-12 03:16:20 -05:00
|
|
|
};
|
2016-11-11 08:02:34 -06:00
|
|
|
|
2018-07-12 03:26:11 -05:00
|
|
|
typedef typename Dune::PersistentContainer<Grid, GlobalCellIndex> GlobalIndexContainer;
|
2016-11-11 08:02:34 -06:00
|
|
|
|
2018-07-12 03:36:48 -05:00
|
|
|
static const int dimension = Grid::dimension;
|
2016-11-11 08:02:34 -06:00
|
|
|
|
2018-07-12 03:26:11 -05:00
|
|
|
typedef typename Grid::LeafGridView GridView;
|
|
|
|
typedef GridView AllGridView;
|
2016-11-11 08:02:34 -06:00
|
|
|
|
2018-07-12 03:26:11 -05:00
|
|
|
typedef Dune::Point2PointCommunicator<Dune::SimpleMessageBuffer> P2PCommunicatorType;
|
|
|
|
typedef typename P2PCommunicatorType::MessageBufferType MessageBufferType;
|
2016-11-11 08:02:34 -06:00
|
|
|
|
2018-07-12 03:26:11 -05:00
|
|
|
typedef std::vector<GlobalCellIndex> LocalIndexMapType;
|
2016-11-11 08:02:34 -06:00
|
|
|
|
2018-07-12 03:16:20 -05:00
|
|
|
typedef std::vector<int> IndexMapType;
|
2018-07-12 03:26:11 -05:00
|
|
|
typedef std::vector<IndexMapType> IndexMapStorageType;
|
2016-11-11 08:02:34 -06:00
|
|
|
|
2018-07-12 03:16:20 -05:00
|
|
|
class DistributeIndexMapping : public P2PCommunicatorType::DataHandleInterface
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
const std::vector<int>& distributedGlobalIndex_;
|
|
|
|
IndexMapType& localIndexMap_;
|
|
|
|
IndexMapStorageType& indexMaps_;
|
2018-07-12 03:26:11 -05:00
|
|
|
std::map<int, int> globalPosition_;
|
2018-07-12 03:16:20 -05:00
|
|
|
std::vector<int>& ranks_;
|
2016-11-11 08:02:34 -06:00
|
|
|
|
2018-07-12 03:16:20 -05:00
|
|
|
public:
|
2018-07-31 08:35:15 -05:00
|
|
|
DistributeIndexMapping(const std::vector<int>& globalIndex,
|
|
|
|
const std::vector<int>& distributedGlobalIndex,
|
2018-07-12 03:26:11 -05:00
|
|
|
IndexMapType& localIndexMap,
|
|
|
|
IndexMapStorageType& indexMaps,
|
|
|
|
std::vector<int>& ranks)
|
|
|
|
: distributedGlobalIndex_(distributedGlobalIndex)
|
|
|
|
, localIndexMap_(localIndexMap)
|
|
|
|
, indexMaps_(indexMaps)
|
|
|
|
, globalPosition_()
|
|
|
|
, ranks_(ranks)
|
2018-07-12 03:16:20 -05:00
|
|
|
{
|
2018-07-12 03:26:11 -05:00
|
|
|
size_t size = globalIndex.size();
|
2018-07-12 03:16:20 -05:00
|
|
|
// create mapping globalIndex --> localIndex
|
2018-07-12 03:26:11 -05:00
|
|
|
for (size_t index = 0; index < size; ++index)
|
|
|
|
globalPosition_.insert(std::make_pair(globalIndex[index], index));
|
2016-11-11 08:02:34 -06:00
|
|
|
|
2018-07-12 03:16:20 -05:00
|
|
|
// we need to create a mapping from local to global
|
2018-07-12 03:26:11 -05:00
|
|
|
if (!indexMaps_.empty()) {
|
|
|
|
ranks_.resize(size, -1);
|
2018-07-12 03:16:20 -05:00
|
|
|
// for the ioRank create a localIndex to index in global state map
|
|
|
|
IndexMapType& indexMap = indexMaps_.back();
|
2018-07-12 03:26:11 -05:00
|
|
|
size_t localSize = localIndexMap_.size();
|
|
|
|
indexMap.resize(localSize);
|
2018-07-12 03:36:48 -05:00
|
|
|
for (size_t i=0; i<localSize; ++i)
|
2016-11-11 08:02:34 -06:00
|
|
|
{
|
2018-07-12 03:26:11 -05:00
|
|
|
int id = distributedGlobalIndex_[localIndexMap_[i]];
|
2018-07-12 03:36:48 -05:00
|
|
|
indexMap[i] = globalPosition_[id];
|
2018-07-12 03:26:11 -05:00
|
|
|
ranks_[indexMap[i]] = ioRank;
|
2016-11-11 08:02:34 -06:00
|
|
|
}
|
|
|
|
}
|
2018-07-12 03:16:20 -05:00
|
|
|
}
|
2016-11-11 08:02:34 -06:00
|
|
|
|
2018-07-12 03:26:11 -05:00
|
|
|
void pack(int link, MessageBufferType& buffer)
|
2018-07-12 03:16:20 -05:00
|
|
|
{
|
|
|
|
// we should only get one link
|
2018-07-12 03:26:11 -05:00
|
|
|
if (link != 0)
|
2018-07-12 03:16:20 -05:00
|
|
|
throw std::logic_error("link in method pack is not 0 as execpted");
|
2016-11-11 08:02:34 -06:00
|
|
|
|
2018-07-12 03:16:20 -05:00
|
|
|
// pack all interior global cell id's
|
2018-07-12 03:26:11 -05:00
|
|
|
int size = localIndexMap_.size();
|
|
|
|
buffer.write(size);
|
2016-11-11 08:02:34 -06:00
|
|
|
|
2018-07-12 03:36:48 -05:00
|
|
|
for (int index = 0; index < size; ++index) {
|
2018-07-12 03:26:11 -05:00
|
|
|
int globalIdx = distributedGlobalIndex_[localIndexMap_[index]];
|
|
|
|
buffer.write(globalIdx);
|
2016-11-11 08:02:34 -06:00
|
|
|
}
|
2018-07-12 03:16:20 -05:00
|
|
|
}
|
2016-11-11 08:02:34 -06:00
|
|
|
|
2018-07-12 03:26:11 -05:00
|
|
|
void unpack(int link, MessageBufferType& buffer)
|
2018-07-12 03:16:20 -05:00
|
|
|
{
|
|
|
|
// get index map for current link
|
2018-07-12 03:26:11 -05:00
|
|
|
IndexMapType& indexMap = indexMaps_[link];
|
|
|
|
assert(!globalPosition_.empty());
|
2018-07-12 03:16:20 -05:00
|
|
|
|
|
|
|
// unpack all interior global cell id's
|
|
|
|
int numCells = 0;
|
2018-07-12 03:26:11 -05:00
|
|
|
buffer.read(numCells);
|
|
|
|
indexMap.resize(numCells);
|
2018-07-12 03:36:48 -05:00
|
|
|
for (int index = 0; index < numCells; ++index) {
|
2018-07-12 03:16:20 -05:00
|
|
|
int globalId = -1;
|
2018-07-12 03:26:11 -05:00
|
|
|
buffer.read(globalId);
|
|
|
|
assert(globalPosition_.find(globalId) != globalPosition_.end());
|
|
|
|
indexMap[index] = globalPosition_[globalId];
|
|
|
|
ranks_[indexMap[index]] = link + 1;
|
2016-11-11 08:02:34 -06:00
|
|
|
}
|
2018-07-12 03:16:20 -05:00
|
|
|
}
|
|
|
|
};
|
2016-11-11 08:02:34 -06:00
|
|
|
|
2018-07-12 03:16:20 -05:00
|
|
|
enum { ioRank = 0 };
|
2016-11-11 08:02:34 -06:00
|
|
|
|
2018-07-12 03:36:48 -05:00
|
|
|
static const bool needsReordering =
|
|
|
|
!std::is_same<typename Vanguard::Grid, typename Vanguard::EquilGrid>::value;
|
2017-06-23 09:02:09 -05:00
|
|
|
|
2018-07-12 03:26:11 -05:00
|
|
|
CollectDataToIORank(const Vanguard& vanguard)
|
|
|
|
: toIORankComm_()
|
2018-07-12 03:16:20 -05:00
|
|
|
{
|
|
|
|
// index maps only have to be build when reordering is needed
|
2018-07-12 03:36:48 -05:00
|
|
|
if (!needsReordering && !isParallel())
|
|
|
|
return;
|
2017-06-23 09:02:09 -05:00
|
|
|
|
2018-07-12 03:16:20 -05:00
|
|
|
const CollectiveCommunication& comm = vanguard.grid().comm();
|
2017-06-22 08:55:01 -05:00
|
|
|
|
2018-07-12 03:16:20 -05:00
|
|
|
{
|
2018-07-12 03:36:48 -05:00
|
|
|
std::set<int> send, recv;
|
2018-07-12 03:16:20 -05:00
|
|
|
typedef typename Vanguard::EquilGrid::LeafGridView EquilGridView;
|
2018-07-12 03:36:48 -05:00
|
|
|
const EquilGridView equilGridView = vanguard.equilGrid().leafGridView();
|
2017-06-22 08:55:01 -05:00
|
|
|
|
2018-01-08 05:23:57 -06:00
|
|
|
#if DUNE_VERSION_NEWER(DUNE_GRID, 2,6)
|
2018-07-12 03:16:20 -05:00
|
|
|
typedef Dune::MultipleCodimMultipleGeomTypeMapper<EquilGridView> EquilElementMapper;
|
|
|
|
EquilElementMapper equilElemMapper(equilGridView, Dune::mcmgElementLayout());
|
2018-01-08 05:23:57 -06:00
|
|
|
#else
|
2018-07-12 03:16:20 -05:00
|
|
|
typedef Dune::MultipleCodimMultipleGeomTypeMapper<EquilGridView, Dune::MCMGElementLayout> EquilElementMapper;
|
|
|
|
EquilElementMapper equilElemMapper(equilGridView);
|
2018-01-08 05:23:57 -06:00
|
|
|
#endif
|
2017-06-22 08:55:01 -05:00
|
|
|
|
2018-07-12 03:16:20 -05:00
|
|
|
// We need a mapping from local to global grid, here we
|
|
|
|
// use equilGrid which represents a view on the global grid
|
2018-07-12 03:36:48 -05:00
|
|
|
const size_t globalSize = vanguard.equilGrid().leafGridView().size(0);
|
2018-07-12 03:16:20 -05:00
|
|
|
// reserve memory
|
|
|
|
globalCartesianIndex_.resize(globalSize, -1);
|
|
|
|
|
|
|
|
// loop over all elements (global grid) and store Cartesian index
|
|
|
|
auto elemIt = vanguard.equilGrid().leafGridView().template begin<0>();
|
|
|
|
const auto& elemEndIt = vanguard.equilGrid().leafGridView().template end<0>();
|
|
|
|
for (; elemIt != elemEndIt; ++elemIt) {
|
2018-07-12 03:36:48 -05:00
|
|
|
int elemIdx = equilElemMapper.index(*elemIt);
|
2018-07-12 03:16:20 -05:00
|
|
|
int cartElemIdx = vanguard.equilCartesianIndexMapper().cartesianIndex(elemIdx);
|
|
|
|
globalCartesianIndex_[elemIdx] = cartElemIdx;
|
|
|
|
}
|
2017-06-22 08:55:01 -05:00
|
|
|
|
2018-07-12 03:16:20 -05:00
|
|
|
// the I/O rank receives from all other ranks
|
2018-07-12 03:36:48 -05:00
|
|
|
if (isIORank()) {
|
|
|
|
for (int i = 0; i < comm.size(); ++i) {
|
|
|
|
if (i != ioRank)
|
|
|
|
recv.insert(i);
|
2016-11-11 08:02:34 -06:00
|
|
|
}
|
2018-07-12 03:16:20 -05:00
|
|
|
}
|
|
|
|
else // all other simply send to the I/O rank
|
2018-07-12 03:36:48 -05:00
|
|
|
send.insert(ioRank);
|
2016-11-11 08:02:34 -06:00
|
|
|
|
2018-07-12 03:16:20 -05:00
|
|
|
localIndexMap_.clear();
|
2018-07-12 03:36:48 -05:00
|
|
|
const size_t gridSize = vanguard.grid().size(0);
|
|
|
|
localIndexMap_.reserve(gridSize);
|
2016-11-11 08:02:34 -06:00
|
|
|
|
2018-07-12 03:16:20 -05:00
|
|
|
// store the local Cartesian index
|
|
|
|
IndexMapType distributedCartesianIndex;
|
|
|
|
distributedCartesianIndex.resize(gridSize, -1);
|
2016-11-22 07:28:07 -06:00
|
|
|
|
2018-07-12 03:16:20 -05:00
|
|
|
typedef typename Vanguard::GridView LocalGridView;
|
2018-07-12 03:36:48 -05:00
|
|
|
const LocalGridView localGridView = vanguard.gridView();
|
2017-06-22 08:55:01 -05:00
|
|
|
|
2018-01-08 05:23:57 -06:00
|
|
|
#if DUNE_VERSION_NEWER(DUNE_GRID, 2,6)
|
2018-07-12 03:16:20 -05:00
|
|
|
typedef Dune::MultipleCodimMultipleGeomTypeMapper<LocalGridView> ElementMapper;
|
|
|
|
ElementMapper elemMapper(localGridView, Dune::mcmgElementLayout());
|
2018-01-08 05:23:57 -06:00
|
|
|
#else
|
2018-07-12 03:16:20 -05:00
|
|
|
typedef Dune::MultipleCodimMultipleGeomTypeMapper<LocalGridView, Dune::MCMGElementLayout> ElementMapper;
|
|
|
|
ElementMapper elemMapper(localGridView);
|
2018-01-08 05:23:57 -06:00
|
|
|
#endif
|
2017-06-22 08:55:01 -05:00
|
|
|
|
2018-07-12 03:16:20 -05:00
|
|
|
// A mapping for the whole grid (including the ghosts) is needed for restarts
|
2018-07-12 03:36:48 -05:00
|
|
|
auto eIt = localGridView.template begin<0>();
|
|
|
|
const auto& eEndIt = localGridView.template end<0>();
|
|
|
|
for (; eIt != eEndIt; ++eIt) {
|
|
|
|
const auto element = *eIt;
|
|
|
|
int elemIdx = elemMapper.index(element);
|
|
|
|
distributedCartesianIndex[elemIdx] = vanguard.cartesianIndex(elemIdx);
|
2016-11-22 07:28:07 -06:00
|
|
|
|
2018-07-12 03:16:20 -05:00
|
|
|
// only store interior element for collection
|
2018-07-12 03:36:48 -05:00
|
|
|
//assert(element.partitionType() == Dune::InteriorEntity);
|
2017-06-23 06:02:42 -05:00
|
|
|
|
2018-07-12 03:36:48 -05:00
|
|
|
localIndexMap_.push_back(elemIdx);
|
2018-07-12 03:16:20 -05:00
|
|
|
}
|
2016-11-11 08:02:34 -06:00
|
|
|
|
2018-07-12 03:16:20 -05:00
|
|
|
// insert send and recv linkage to communicator
|
2018-07-12 03:36:48 -05:00
|
|
|
toIORankComm_.insertRequest(send, recv);
|
2016-11-11 08:02:34 -06:00
|
|
|
|
2018-07-12 03:16:20 -05:00
|
|
|
// need an index map for each rank
|
|
|
|
indexMaps_.clear();
|
2018-07-12 03:36:48 -05:00
|
|
|
indexMaps_.resize(comm.size());
|
2016-11-11 08:02:34 -06:00
|
|
|
|
2018-07-12 03:16:20 -05:00
|
|
|
// distribute global id's to io rank for later association of dof's
|
2018-07-12 03:36:48 -05:00
|
|
|
DistributeIndexMapping distIndexMapping(globalCartesianIndex_,
|
|
|
|
distributedCartesianIndex,
|
|
|
|
localIndexMap_,
|
|
|
|
indexMaps_,
|
|
|
|
globalRanks_);
|
|
|
|
toIORankComm_.exchange(distIndexMapping);
|
2016-11-11 08:02:34 -06:00
|
|
|
}
|
2018-07-12 03:16:20 -05:00
|
|
|
}
|
2016-11-11 08:02:34 -06:00
|
|
|
|
2018-07-12 03:16:20 -05:00
|
|
|
class PackUnPackCellData : public P2PCommunicatorType::DataHandleInterface
|
|
|
|
{
|
|
|
|
const Opm::data::Solution& localCellData_;
|
|
|
|
Opm::data::Solution& globalCellData_;
|
|
|
|
|
|
|
|
const IndexMapType& localIndexMap_;
|
|
|
|
const IndexMapStorageType& indexMaps_;
|
|
|
|
|
|
|
|
public:
|
2018-07-12 03:26:11 -05:00
|
|
|
PackUnPackCellData(const Opm::data::Solution& localCellData,
|
|
|
|
Opm::data::Solution& globalCellData,
|
|
|
|
const IndexMapType& localIndexMap,
|
|
|
|
const IndexMapStorageType& indexMaps,
|
|
|
|
size_t globalSize,
|
|
|
|
bool isIORank)
|
|
|
|
: localCellData_(localCellData)
|
|
|
|
, globalCellData_(globalCellData)
|
|
|
|
, localIndexMap_(localIndexMap)
|
|
|
|
, indexMaps_(indexMaps)
|
2018-07-12 03:16:20 -05:00
|
|
|
{
|
2018-07-12 03:36:48 -05:00
|
|
|
if (isIORank) {
|
2018-07-12 03:16:20 -05:00
|
|
|
// add missing data to global cell data
|
|
|
|
for (const auto& pair : localCellData_) {
|
|
|
|
const std::string& key = pair.first;
|
2018-07-11 06:59:29 -05:00
|
|
|
std::size_t containerSize = globalSize;
|
2018-07-12 03:16:20 -05:00
|
|
|
auto OPM_OPTIM_UNUSED ret = globalCellData_.insert(key, pair.second.dim,
|
2018-07-11 06:59:29 -05:00
|
|
|
std::vector<double>(containerSize),
|
2018-07-12 03:16:20 -05:00
|
|
|
pair.second.target);
|
|
|
|
assert(ret.second);
|
2016-11-11 08:02:34 -06:00
|
|
|
}
|
|
|
|
|
2018-07-12 03:16:20 -05:00
|
|
|
MessageBufferType buffer;
|
2018-07-12 03:36:48 -05:00
|
|
|
pack(0, buffer);
|
2016-11-11 08:02:34 -06:00
|
|
|
|
2018-07-12 03:16:20 -05:00
|
|
|
// the last index map is the local one
|
2018-07-12 03:36:48 -05:00
|
|
|
doUnpack(indexMaps.back(), buffer);
|
2018-07-12 03:16:20 -05:00
|
|
|
}
|
|
|
|
}
|
2017-12-07 05:38:48 -06:00
|
|
|
|
2018-07-12 03:16:20 -05:00
|
|
|
// pack all data associated with link
|
2018-07-12 03:26:11 -05:00
|
|
|
void pack(int link, MessageBufferType& buffer)
|
2018-07-12 03:16:20 -05:00
|
|
|
{
|
|
|
|
// we should only get one link
|
2018-07-12 03:36:48 -05:00
|
|
|
if (link != 0)
|
2018-07-12 03:16:20 -05:00
|
|
|
throw std::logic_error("link in method pack is not 0 as expected");
|
2016-11-11 08:02:34 -06:00
|
|
|
|
2018-07-12 03:16:20 -05:00
|
|
|
// write all cell data registered in local state
|
|
|
|
for (const auto& pair : localCellData_) {
|
|
|
|
const auto& data = pair.second.data;
|
2017-12-07 05:38:48 -06:00
|
|
|
|
2018-07-12 03:16:20 -05:00
|
|
|
// write all data from local data to buffer
|
2018-07-12 03:36:48 -05:00
|
|
|
write(buffer, localIndexMap_, data);
|
2016-11-11 08:02:34 -06:00
|
|
|
}
|
2018-07-12 03:16:20 -05:00
|
|
|
}
|
2016-11-11 08:02:34 -06:00
|
|
|
|
2018-07-12 03:26:11 -05:00
|
|
|
void doUnpack(const IndexMapType& indexMap, MessageBufferType& buffer)
|
2018-07-12 03:16:20 -05:00
|
|
|
{
|
2018-07-12 03:26:11 -05:00
|
|
|
// we loop over the data as
|
2018-07-12 03:16:20 -05:00
|
|
|
// its order governs the order the data got received.
|
|
|
|
for (auto& pair : localCellData_) {
|
|
|
|
const std::string& key = pair.first;
|
|
|
|
auto& data = globalCellData_.data(key);
|
|
|
|
|
|
|
|
//write all data from local cell data to buffer
|
2018-07-12 03:36:48 -05:00
|
|
|
read(buffer, indexMap, data);
|
2016-11-11 08:02:34 -06:00
|
|
|
}
|
2018-07-12 03:16:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// unpack all data associated with link
|
2018-07-12 03:26:11 -05:00
|
|
|
void unpack(int link, MessageBufferType& buffer)
|
|
|
|
{ doUnpack(indexMaps_[link], buffer); }
|
2016-11-11 08:02:34 -06:00
|
|
|
|
2018-07-12 03:16:20 -05:00
|
|
|
protected:
|
|
|
|
template <class Vector>
|
2018-07-12 03:26:11 -05:00
|
|
|
void write(MessageBufferType& buffer,
|
|
|
|
const IndexMapType& localIndexMap,
|
|
|
|
const Vector& vector,
|
|
|
|
unsigned int offset = 0,
|
|
|
|
unsigned int stride = 1) const
|
2018-07-12 03:16:20 -05:00
|
|
|
{
|
|
|
|
unsigned int size = localIndexMap.size();
|
2018-07-12 03:36:48 -05:00
|
|
|
buffer.write(size);
|
|
|
|
assert(vector.size() >= stride * size);
|
|
|
|
for (unsigned int i=0; i<size; ++i)
|
2016-11-11 08:02:34 -06:00
|
|
|
{
|
2018-07-12 03:36:48 -05:00
|
|
|
unsigned int index = localIndexMap[i] * stride + offset;
|
|
|
|
assert(index < vector.size());
|
|
|
|
buffer.write(vector[index]);
|
2016-11-11 08:02:34 -06:00
|
|
|
}
|
2018-07-12 03:16:20 -05:00
|
|
|
}
|
2016-11-11 08:02:34 -06:00
|
|
|
|
2018-07-12 03:16:20 -05:00
|
|
|
template <class Vector>
|
2018-07-12 03:26:11 -05:00
|
|
|
void read(MessageBufferType& buffer,
|
|
|
|
const IndexMapType& indexMap,
|
|
|
|
Vector& vector,
|
|
|
|
unsigned int offset = 0,
|
|
|
|
unsigned int stride = 1) const
|
2018-07-12 03:16:20 -05:00
|
|
|
{
|
|
|
|
unsigned int size = 0;
|
2018-07-12 03:36:48 -05:00
|
|
|
buffer.read(size);
|
|
|
|
assert(size == indexMap.size());
|
|
|
|
for (unsigned int i=0; i<size; ++i) {
|
|
|
|
unsigned int index = indexMap[i] * stride + offset;
|
|
|
|
assert(index < vector.size());
|
|
|
|
buffer.read(vector[index]);
|
2016-11-11 08:02:34 -06:00
|
|
|
}
|
2018-07-12 03:16:20 -05:00
|
|
|
}
|
|
|
|
};
|
2016-11-11 08:02:34 -06:00
|
|
|
|
2018-07-12 03:16:20 -05:00
|
|
|
class PackUnPackWellData : public P2PCommunicatorType::DataHandleInterface
|
|
|
|
{
|
|
|
|
const Opm::data::Wells& localWellData_;
|
|
|
|
Opm::data::Wells& globalWellData_;
|
|
|
|
|
|
|
|
public:
|
|
|
|
PackUnPackWellData(const Opm::data::Wells& localWellData,
|
|
|
|
Opm::data::Wells& globalWellData,
|
2018-07-12 03:26:11 -05:00
|
|
|
bool isIORank)
|
|
|
|
: localWellData_(localWellData)
|
|
|
|
, globalWellData_(globalWellData)
|
2018-02-09 05:57:34 -06:00
|
|
|
{
|
2018-07-12 03:26:11 -05:00
|
|
|
if (isIORank) {
|
2018-07-12 03:16:20 -05:00
|
|
|
MessageBufferType buffer;
|
2018-07-12 03:26:11 -05:00
|
|
|
pack(0, buffer);
|
2018-02-09 05:57:34 -06:00
|
|
|
|
2018-07-12 03:16:20 -05:00
|
|
|
// pass a dummy_link to satisfy virtual class
|
2018-07-12 03:26:11 -05:00
|
|
|
int dummyLink = -1;
|
|
|
|
unpack(dummyLink, buffer);
|
2018-02-09 05:57:34 -06:00
|
|
|
}
|
2018-07-12 03:16:20 -05:00
|
|
|
}
|
2018-02-09 05:57:34 -06:00
|
|
|
|
2018-07-12 03:16:20 -05:00
|
|
|
// pack all data associated with link
|
2018-07-12 03:26:11 -05:00
|
|
|
void pack(int link, MessageBufferType& buffer)
|
2018-07-12 03:16:20 -05:00
|
|
|
{
|
|
|
|
// we should only get one link
|
2018-07-12 03:26:11 -05:00
|
|
|
if (link != 0)
|
2018-07-12 03:16:20 -05:00
|
|
|
throw std::logic_error("link in method pack is not 0 as expected");
|
2018-07-12 03:26:11 -05:00
|
|
|
|
2018-07-12 03:16:20 -05:00
|
|
|
localWellData_.write(buffer);
|
|
|
|
}
|
2018-02-09 05:57:34 -06:00
|
|
|
|
2018-07-12 03:16:20 -05:00
|
|
|
// unpack all data associated with link
|
2018-07-12 03:26:11 -05:00
|
|
|
void unpack(int /*link*/, MessageBufferType& buffer)
|
|
|
|
{ globalWellData_.read(buffer); }
|
2018-02-09 05:57:34 -06:00
|
|
|
|
2018-07-12 03:16:20 -05:00
|
|
|
};
|
2018-02-09 05:57:34 -06:00
|
|
|
|
2018-07-12 03:16:20 -05:00
|
|
|
class PackUnPackBlockData : public P2PCommunicatorType::DataHandleInterface
|
|
|
|
{
|
|
|
|
const std::map<std::pair<std::string, int>, double>& localBlockData_;
|
|
|
|
std::map<std::pair<std::string, int>, double>& globalBlockValues_;
|
|
|
|
|
|
|
|
public:
|
2018-07-12 03:26:11 -05:00
|
|
|
PackUnPackBlockData(const std::map<std::pair<std::string, int>, double>& localBlockData,
|
|
|
|
std::map<std::pair<std::string, int>, double>& globalBlockValues,
|
|
|
|
bool isIORank)
|
|
|
|
: localBlockData_(localBlockData)
|
|
|
|
, globalBlockValues_(globalBlockValues)
|
2018-07-12 03:16:20 -05:00
|
|
|
{
|
2018-07-12 03:36:48 -05:00
|
|
|
if (isIORank) {
|
2018-07-12 03:16:20 -05:00
|
|
|
MessageBufferType buffer;
|
2018-07-12 03:36:48 -05:00
|
|
|
pack(0, buffer);
|
2018-02-09 05:57:34 -06:00
|
|
|
|
2018-07-11 06:59:29 -05:00
|
|
|
// pass a dummyLink to satisfy virtual class
|
|
|
|
int dummyLink = -1;
|
|
|
|
unpack(dummyLink, buffer);
|
2018-02-09 05:57:34 -06:00
|
|
|
}
|
2018-07-12 03:16:20 -05:00
|
|
|
}
|
2018-02-09 05:57:34 -06:00
|
|
|
|
2018-07-12 03:16:20 -05:00
|
|
|
// pack all data associated with link
|
2018-07-12 03:26:11 -05:00
|
|
|
void pack(int link, MessageBufferType& buffer)
|
2018-07-12 03:16:20 -05:00
|
|
|
{
|
|
|
|
// we should only get one link
|
2018-07-12 03:36:48 -05:00
|
|
|
if (link != 0)
|
2018-07-12 03:16:20 -05:00
|
|
|
throw std::logic_error("link in method pack is not 0 as expected");
|
2018-02-09 05:57:34 -06:00
|
|
|
|
2018-07-12 03:16:20 -05:00
|
|
|
// write all block data
|
|
|
|
unsigned int size = localBlockData_.size();
|
2018-07-12 03:36:48 -05:00
|
|
|
buffer.write(size);
|
2018-07-12 03:16:20 -05:00
|
|
|
for (const auto& map : localBlockData_) {
|
|
|
|
buffer.write(map.first.first);
|
|
|
|
buffer.write(map.first.second);
|
|
|
|
buffer.write(map.second);
|
2018-02-09 05:57:34 -06:00
|
|
|
}
|
2018-07-12 03:16:20 -05:00
|
|
|
}
|
2018-02-09 05:57:34 -06:00
|
|
|
|
2018-07-12 03:16:20 -05:00
|
|
|
// unpack all data associated with link
|
2018-07-12 03:26:11 -05:00
|
|
|
void unpack(int /*link*/, MessageBufferType& buffer)
|
2018-07-12 03:16:20 -05:00
|
|
|
{
|
|
|
|
// read all block data
|
|
|
|
unsigned int size = 0;
|
|
|
|
buffer.read(size);
|
|
|
|
for (size_t i = 0; i < size; ++i) {
|
|
|
|
std::string name;
|
|
|
|
int idx;
|
|
|
|
double data;
|
2018-07-12 03:36:48 -05:00
|
|
|
buffer.read(name);
|
|
|
|
buffer.read(idx);
|
|
|
|
buffer.read(data);
|
2018-07-12 03:16:20 -05:00
|
|
|
globalBlockValues_[std::make_pair(name, idx)] = data;
|
2018-02-09 05:57:34 -06:00
|
|
|
}
|
2018-07-12 03:16:20 -05:00
|
|
|
}
|
2018-02-09 05:57:34 -06:00
|
|
|
|
2018-07-12 03:16:20 -05:00
|
|
|
};
|
2018-02-09 05:57:34 -06:00
|
|
|
|
2018-07-12 03:16:20 -05:00
|
|
|
// gather solution to rank 0 for EclipseWriter
|
2018-07-12 03:26:11 -05:00
|
|
|
void collect(const Opm::data::Solution& localCellData,
|
|
|
|
const std::map<std::pair<std::string, int>, double>& localBlockData,
|
|
|
|
const Opm::data::Wells& localWellData)
|
2018-07-12 03:16:20 -05:00
|
|
|
{
|
|
|
|
globalCellData_ = {};
|
|
|
|
globalBlockData_.clear();
|
|
|
|
globalWellData_.clear();
|
2018-02-09 02:22:38 -06:00
|
|
|
|
2018-07-12 03:16:20 -05:00
|
|
|
// index maps only have to be build when reordering is needed
|
2018-07-12 03:36:48 -05:00
|
|
|
if(!needsReordering && !isParallel())
|
|
|
|
return;
|
2017-06-23 09:02:09 -05:00
|
|
|
|
2018-07-12 03:16:20 -05:00
|
|
|
// this also packs and unpacks the local buffers one ioRank
|
|
|
|
PackUnPackCellData
|
2018-07-12 03:36:48 -05:00
|
|
|
packUnpackCellData(localCellData,
|
2018-07-12 03:16:20 -05:00
|
|
|
globalCellData_,
|
|
|
|
localIndexMap_,
|
|
|
|
indexMaps_,
|
|
|
|
numCells(),
|
2018-07-12 03:36:48 -05:00
|
|
|
isIORank());
|
2016-11-11 08:02:34 -06:00
|
|
|
|
2018-07-12 03:36:48 -05:00
|
|
|
if (!isParallel())
|
2018-07-12 03:16:20 -05:00
|
|
|
// no need to collect anything.
|
|
|
|
return;
|
2017-06-23 06:02:42 -05:00
|
|
|
|
2018-07-12 03:16:20 -05:00
|
|
|
PackUnPackWellData
|
2018-07-12 03:36:48 -05:00
|
|
|
packUnpackWellData(localWellData,
|
|
|
|
globalWellData_,
|
|
|
|
isIORank());
|
2018-02-09 05:57:34 -06:00
|
|
|
|
2018-07-12 03:16:20 -05:00
|
|
|
PackUnPackBlockData
|
2018-07-12 03:36:48 -05:00
|
|
|
packUnpackBlockData(localBlockData,
|
|
|
|
globalBlockData_,
|
|
|
|
isIORank());
|
2018-02-09 05:57:34 -06:00
|
|
|
|
2018-07-12 03:36:48 -05:00
|
|
|
toIORankComm_.exchange(packUnpackCellData);
|
|
|
|
toIORankComm_.exchange(packUnpackWellData);
|
|
|
|
toIORankComm_.exchange(packUnpackBlockData);
|
2018-02-09 05:57:34 -06:00
|
|
|
|
|
|
|
|
2017-06-23 06:02:42 -05:00
|
|
|
|
2016-11-11 08:02:34 -06:00
|
|
|
#ifndef NDEBUG
|
2018-07-12 03:16:20 -05:00
|
|
|
// mkae sure every process is on the same page
|
|
|
|
toIORankComm_.barrier();
|
2016-11-11 08:02:34 -06:00
|
|
|
#endif
|
2018-07-12 03:16:20 -05:00
|
|
|
}
|
2016-11-11 08:02:34 -06:00
|
|
|
|
2018-07-12 03:16:20 -05:00
|
|
|
const std::map<std::pair<std::string, int>, double>& globalBlockData() const
|
2018-07-12 03:26:11 -05:00
|
|
|
{ return globalBlockData_; }
|
2018-01-19 08:45:42 -06:00
|
|
|
|
2018-07-12 03:16:20 -05:00
|
|
|
const Opm::data::Solution& globalCellData() const
|
2018-07-12 03:26:11 -05:00
|
|
|
{ return globalCellData_; }
|
2017-12-07 05:38:48 -06:00
|
|
|
|
2018-07-12 03:16:20 -05:00
|
|
|
const Opm::data::Wells& globalWellData() const
|
2018-07-12 03:26:11 -05:00
|
|
|
{ return globalWellData_; }
|
2018-02-09 02:22:38 -06:00
|
|
|
|
2018-07-12 03:16:20 -05:00
|
|
|
bool isIORank() const
|
2018-07-12 03:26:11 -05:00
|
|
|
{ return toIORankComm_.rank() == ioRank; }
|
2016-11-11 08:02:34 -06:00
|
|
|
|
2018-07-12 03:16:20 -05:00
|
|
|
bool isParallel() const
|
2018-07-12 03:26:11 -05:00
|
|
|
{ return toIORankComm_.size() > 1; }
|
2016-11-11 08:02:34 -06:00
|
|
|
|
2018-07-12 03:26:11 -05:00
|
|
|
int localIdxToGlobalIdx(unsigned localIdx) const
|
2018-07-12 03:16:20 -05:00
|
|
|
{
|
2018-07-12 03:36:48 -05:00
|
|
|
if (!isParallel())
|
2018-07-12 03:16:20 -05:00
|
|
|
return localIdx;
|
2018-07-12 03:36:48 -05:00
|
|
|
|
2018-07-12 03:16:20 -05:00
|
|
|
// the last indexMap is the local one
|
|
|
|
const IndexMapType& indexMap = indexMaps_.back();
|
2018-07-12 03:36:48 -05:00
|
|
|
if (indexMap.empty())
|
2018-07-12 03:16:20 -05:00
|
|
|
throw std::logic_error("index map is not created on this rank");
|
2017-12-07 05:38:48 -06:00
|
|
|
|
2018-07-12 03:16:20 -05:00
|
|
|
if (localIdx > indexMap.size())
|
|
|
|
throw std::logic_error("local index is outside map range");
|
2017-12-07 05:38:48 -06:00
|
|
|
|
2018-07-12 03:16:20 -05:00
|
|
|
return indexMap[localIdx];
|
|
|
|
}
|
2017-12-07 05:38:48 -06:00
|
|
|
|
2018-07-12 03:26:11 -05:00
|
|
|
size_t numCells () const
|
|
|
|
{ return globalCartesianIndex_.size(); }
|
2016-11-11 08:02:34 -06:00
|
|
|
|
2018-07-12 03:16:20 -05:00
|
|
|
const std::vector<int>& globalRanks() const
|
2018-07-12 03:26:11 -05:00
|
|
|
{ return globalRanks_; }
|
2018-01-12 08:32:43 -06:00
|
|
|
|
2018-07-12 03:26:11 -05:00
|
|
|
bool isGlobalIdxOnThisRank(unsigned globalIdx) const
|
2018-07-12 03:16:20 -05:00
|
|
|
{
|
2018-07-12 03:26:11 -05:00
|
|
|
if (!isParallel())
|
2018-07-12 03:16:20 -05:00
|
|
|
return true;
|
2018-07-12 03:26:11 -05:00
|
|
|
|
2018-07-12 03:16:20 -05:00
|
|
|
// the last indexMap is the local one
|
|
|
|
const IndexMapType& indexMap = indexMaps_.back();
|
2018-07-12 03:26:11 -05:00
|
|
|
if (indexMap.empty())
|
2018-07-12 03:16:20 -05:00
|
|
|
throw std::logic_error("index map is not created on this rank");
|
|
|
|
|
|
|
|
return std::find(indexMap.begin(), indexMap.end(), globalIdx) != indexMap.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2018-07-12 03:26:11 -05:00
|
|
|
P2PCommunicatorType toIORankComm_;
|
|
|
|
IndexMapType globalCartesianIndex_;
|
|
|
|
IndexMapType localIndexMap_;
|
|
|
|
IndexMapStorageType indexMaps_;
|
2018-07-31 08:35:15 -05:00
|
|
|
std::vector<int> globalRanks_;
|
2018-07-12 03:26:11 -05:00
|
|
|
Opm::data::Solution globalCellData_;
|
2018-07-12 03:16:20 -05:00
|
|
|
std::map<std::pair<std::string, int>, double> globalBlockData_;
|
|
|
|
Opm::data::Wells globalWellData_;
|
|
|
|
};
|
2016-11-11 08:02:34 -06:00
|
|
|
|
2018-07-12 03:23:26 -05:00
|
|
|
} // end namespace Ewoms
|
|
|
|
|
2016-11-11 08:02:34 -06:00
|
|
|
#endif
|