opm-simulators/ebos/collecttoiorank.hh

594 lines
21 KiB
C++
Raw Normal View History

// -*- 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.
*/
#ifndef EWOMS_COLLECT_TO_IO_RANK_HH
#define EWOMS_COLLECT_TO_IO_RANK_HH
#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>
2018-02-08 05:20:17 -06:00
#include <opm/grid/common/p2pcommunicator.hh>
#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>
#include <opm/material/common/Exceptions.hpp>
#include <opm/material/common/Unused.hpp>
#include <dune/grid/common/mcmgmapper.hh>
#include <stdexcept>
namespace Ewoms {
template <class Vanguard>
2018-07-12 03:16:20 -05:00
class CollectDataToIORank
{
public:
typedef typename Vanguard::Grid Grid;
typedef typename Grid::CollectiveCommunication CollectiveCommunication;
2018-07-12 03:16:20 -05:00
// global id
class GlobalCellIndex
{
2018-07-12 03:16:20 -05:00
int globalId_;
int localIndex_;
bool isInterior_;
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
};
typedef typename Dune::PersistentContainer<Grid, GlobalCellIndex> GlobalIndexContainer;
static const int dimension = Grid::dimension;
typedef typename Grid::LeafGridView GridView;
typedef GridView AllGridView;
typedef Dune::Point2PointCommunicator<Dune::SimpleMessageBuffer> P2PCommunicatorType;
typedef typename P2PCommunicatorType::MessageBufferType MessageBufferType;
typedef std::vector<GlobalCellIndex> LocalIndexMapType;
2018-07-12 03:16:20 -05:00
typedef std::vector<int> IndexMapType;
typedef std::vector<IndexMapType> IndexMapStorageType;
2018-07-12 03:16:20 -05:00
class DistributeIndexMapping : public P2PCommunicatorType::DataHandleInterface
{
protected:
const std::vector<int>& distributedGlobalIndex_;
IndexMapType& localIndexMap_;
IndexMapStorageType& indexMaps_;
std::map<int, int> globalPosition_;
2018-07-12 03:16:20 -05:00
std::vector<int>& ranks_;
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,
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
{
size_t size = globalIndex.size();
2018-07-12 03:16:20 -05:00
// create mapping globalIndex --> localIndex
for (size_t index = 0; index < size; ++index)
globalPosition_.insert(std::make_pair(globalIndex[index], index));
2018-07-12 03:16:20 -05:00
// we need to create a mapping from local to global
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();
size_t localSize = localIndexMap_.size();
indexMap.resize(localSize);
for (size_t i=0; i<localSize; ++i)
{
int id = distributedGlobalIndex_[localIndexMap_[i]];
indexMap[i] = globalPosition_[id];
ranks_[indexMap[i]] = ioRank;
}
}
2018-07-12 03:16:20 -05:00
}
void pack(int link, MessageBufferType& buffer)
2018-07-12 03:16:20 -05:00
{
// we should only get one link
if (link != 0)
2018-07-12 03:16:20 -05:00
throw std::logic_error("link in method pack is not 0 as execpted");
2018-07-12 03:16:20 -05:00
// pack all interior global cell id's
int size = localIndexMap_.size();
buffer.write(size);
for (int index = 0; index < size; ++index) {
int globalIdx = distributedGlobalIndex_[localIndexMap_[index]];
buffer.write(globalIdx);
}
2018-07-12 03:16:20 -05:00
}
void unpack(int link, MessageBufferType& buffer)
2018-07-12 03:16:20 -05:00
{
// get index map for current link
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;
buffer.read(numCells);
indexMap.resize(numCells);
for (int index = 0; index < numCells; ++index) {
2018-07-12 03:16:20 -05:00
int globalId = -1;
buffer.read(globalId);
assert(globalPosition_.find(globalId) != globalPosition_.end());
indexMap[index] = globalPosition_[globalId];
ranks_[indexMap[index]] = link + 1;
}
2018-07-12 03:16:20 -05:00
}
};
2018-07-12 03:16:20 -05:00
enum { ioRank = 0 };
static const bool needsReordering =
!std::is_same<typename Vanguard::Grid, typename Vanguard::EquilGrid>::value;
CollectDataToIORank(const Vanguard& vanguard)
: toIORankComm_()
2018-07-12 03:16:20 -05:00
{
// index maps only have to be build when reordering is needed
if (!needsReordering && !isParallel())
return;
2018-07-12 03:16:20 -05:00
const CollectiveCommunication& comm = vanguard.grid().comm();
2018-07-12 03:16:20 -05:00
{
std::set<int> send, recv;
2018-07-12 03:16:20 -05:00
typedef typename Vanguard::EquilGrid::LeafGridView EquilGridView;
const EquilGridView equilGridView = vanguard.equilGrid().leafGridView();
#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());
#else
2018-07-12 03:16:20 -05:00
typedef Dune::MultipleCodimMultipleGeomTypeMapper<EquilGridView, Dune::MCMGElementLayout> EquilElementMapper;
EquilElementMapper equilElemMapper(equilGridView);
#endif
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
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) {
int elemIdx = equilElemMapper.index(*elemIt);
2018-07-12 03:16:20 -05:00
int cartElemIdx = vanguard.equilCartesianIndexMapper().cartesianIndex(elemIdx);
globalCartesianIndex_[elemIdx] = cartElemIdx;
}
2018-07-12 03:16:20 -05:00
// the I/O rank receives from all other ranks
if (isIORank()) {
for (int i = 0; i < comm.size(); ++i) {
if (i != ioRank)
recv.insert(i);
}
2018-07-12 03:16:20 -05:00
}
else // all other simply send to the I/O rank
send.insert(ioRank);
2018-07-12 03:16:20 -05:00
localIndexMap_.clear();
const size_t gridSize = vanguard.grid().size(0);
localIndexMap_.reserve(gridSize);
2018-07-12 03:16:20 -05:00
// store the local Cartesian index
IndexMapType distributedCartesianIndex;
distributedCartesianIndex.resize(gridSize, -1);
2018-07-12 03:16:20 -05:00
typedef typename Vanguard::GridView LocalGridView;
const LocalGridView localGridView = vanguard.gridView();
#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());
#else
2018-07-12 03:16:20 -05:00
typedef Dune::MultipleCodimMultipleGeomTypeMapper<LocalGridView, Dune::MCMGElementLayout> ElementMapper;
ElementMapper elemMapper(localGridView);
#endif
2018-07-12 03:16:20 -05:00
// A mapping for the whole grid (including the ghosts) is needed for restarts
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);
2018-07-12 03:16:20 -05:00
// only store interior element for collection
//assert(element.partitionType() == Dune::InteriorEntity);
localIndexMap_.push_back(elemIdx);
2018-07-12 03:16:20 -05:00
}
2018-07-12 03:16:20 -05:00
// insert send and recv linkage to communicator
toIORankComm_.insertRequest(send, recv);
2018-07-12 03:16:20 -05:00
// need an index map for each rank
indexMaps_.clear();
indexMaps_.resize(comm.size());
2018-07-12 03:16:20 -05:00
// distribute global id's to io rank for later association of dof's
DistributeIndexMapping distIndexMapping(globalCartesianIndex_,
distributedCartesianIndex,
localIndexMap_,
indexMaps_,
globalRanks_);
toIORankComm_.exchange(distIndexMapping);
}
2018-07-12 03:16:20 -05: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:
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
{
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;
std::size_t containerSize = globalSize;
2018-07-12 03:16:20 -05:00
auto OPM_OPTIM_UNUSED ret = globalCellData_.insert(key, pair.second.dim,
std::vector<double>(containerSize),
2018-07-12 03:16:20 -05:00
pair.second.target);
assert(ret.second);
}
2018-07-12 03:16:20 -05:00
MessageBufferType buffer;
pack(0, buffer);
2018-07-12 03:16:20 -05:00
// the last index map is the local one
doUnpack(indexMaps.back(), buffer);
2018-07-12 03:16:20 -05:00
}
}
2018-07-12 03:16:20 -05:00
// pack all data associated with link
void pack(int link, MessageBufferType& buffer)
2018-07-12 03:16:20 -05:00
{
// we should only get one link
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:16:20 -05:00
// write all cell data registered in local state
for (const auto& pair : localCellData_) {
const auto& data = pair.second.data;
2018-07-12 03:16:20 -05:00
// write all data from local data to buffer
write(buffer, localIndexMap_, data);
}
2018-07-12 03:16:20 -05:00
}
void doUnpack(const IndexMapType& indexMap, MessageBufferType& buffer)
2018-07-12 03:16:20 -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
read(buffer, indexMap, data);
}
2018-07-12 03:16:20 -05:00
}
// unpack all data associated with link
void unpack(int link, MessageBufferType& buffer)
{ doUnpack(indexMaps_[link], buffer); }
2018-07-12 03:16:20 -05:00
protected:
template <class Vector>
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();
buffer.write(size);
assert(vector.size() >= stride * size);
for (unsigned int i=0; i<size; ++i)
{
unsigned int index = localIndexMap[i] * stride + offset;
assert(index < vector.size());
buffer.write(vector[index]);
}
2018-07-12 03:16:20 -05:00
}
2018-07-12 03:16:20 -05:00
template <class Vector>
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;
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]);
}
2018-07-12 03:16:20 -05: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,
bool isIORank)
: localWellData_(localWellData)
, globalWellData_(globalWellData)
{
if (isIORank) {
2018-07-12 03:16:20 -05:00
MessageBufferType buffer;
pack(0, buffer);
2018-07-12 03:16:20 -05:00
// pass a dummy_link to satisfy virtual class
int dummyLink = -1;
unpack(dummyLink, buffer);
}
2018-07-12 03:16:20 -05:00
}
2018-07-12 03:16:20 -05:00
// pack all data associated with link
void pack(int link, MessageBufferType& buffer)
2018-07-12 03:16:20 -05:00
{
// we should only get one link
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:16:20 -05:00
localWellData_.write(buffer);
}
2018-07-12 03:16:20 -05:00
// unpack all data associated with link
void unpack(int /*link*/, MessageBufferType& buffer)
{ globalWellData_.read(buffer); }
2018-07-12 03:16:20 -05: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:
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
{
if (isIORank) {
2018-07-12 03:16:20 -05:00
MessageBufferType buffer;
pack(0, buffer);
// pass a dummyLink to satisfy virtual class
int dummyLink = -1;
unpack(dummyLink, buffer);
}
2018-07-12 03:16:20 -05:00
}
2018-07-12 03:16:20 -05:00
// pack all data associated with link
void pack(int link, MessageBufferType& buffer)
2018-07-12 03:16:20 -05:00
{
// we should only get one link
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:16:20 -05:00
// write all block data
unsigned int size = localBlockData_.size();
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-07-12 03:16:20 -05:00
}
2018-07-12 03:16:20 -05:00
// unpack all data associated with link
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;
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-07-12 03:16:20 -05:00
}
2018-07-12 03:16:20 -05:00
};
2018-07-12 03:16:20 -05:00
// gather solution to rank 0 for EclipseWriter
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
if(!needsReordering && !isParallel())
return;
2018-07-12 03:16:20 -05:00
// this also packs and unpacks the local buffers one ioRank
PackUnPackCellData
packUnpackCellData(localCellData,
2018-07-12 03:16:20 -05:00
globalCellData_,
localIndexMap_,
indexMaps_,
numCells(),
isIORank());
if (!isParallel())
2018-07-12 03:16:20 -05:00
// no need to collect anything.
return;
2018-07-12 03:16:20 -05:00
PackUnPackWellData
packUnpackWellData(localWellData,
globalWellData_,
isIORank());
2018-07-12 03:16:20 -05:00
PackUnPackBlockData
packUnpackBlockData(localBlockData,
globalBlockData_,
isIORank());
toIORankComm_.exchange(packUnpackCellData);
toIORankComm_.exchange(packUnpackWellData);
toIORankComm_.exchange(packUnpackBlockData);
#ifndef NDEBUG
2018-07-12 03:16:20 -05:00
// mkae sure every process is on the same page
toIORankComm_.barrier();
#endif
2018-07-12 03:16:20 -05:00
}
2018-07-12 03:16:20 -05:00
const std::map<std::pair<std::string, int>, double>& globalBlockData() const
{ return globalBlockData_; }
2018-01-19 08:45:42 -06:00
2018-07-12 03:16:20 -05:00
const Opm::data::Solution& globalCellData() const
{ return globalCellData_; }
2018-07-12 03:16:20 -05:00
const Opm::data::Wells& globalWellData() const
{ return globalWellData_; }
2018-02-09 02:22:38 -06:00
2018-07-12 03:16:20 -05:00
bool isIORank() const
{ return toIORankComm_.rank() == ioRank; }
2018-07-12 03:16:20 -05:00
bool isParallel() const
{ return toIORankComm_.size() > 1; }
int localIdxToGlobalIdx(unsigned localIdx) const
2018-07-12 03:16:20 -05:00
{
if (!isParallel())
2018-07-12 03:16:20 -05:00
return localIdx;
2018-07-12 03:16:20 -05:00
// the last indexMap is the local one
const IndexMapType& indexMap = indexMaps_.back();
if (indexMap.empty())
2018-07-12 03:16:20 -05:00
throw std::logic_error("index map is not created on this rank");
2018-07-12 03:16:20 -05:00
if (localIdx > indexMap.size())
throw std::logic_error("local index is outside map range");
2018-07-12 03:16:20 -05:00
return indexMap[localIdx];
}
size_t numCells () const
{ return globalCartesianIndex_.size(); }
2018-07-12 03:16:20 -05:00
const std::vector<int>& globalRanks() const
{ return globalRanks_; }
bool isGlobalIdxOnThisRank(unsigned globalIdx) const
2018-07-12 03:16:20 -05:00
{
if (!isParallel())
2018-07-12 03:16:20 -05:00
return true;
2018-07-12 03:16:20 -05:00
// the last indexMap is the local one
const IndexMapType& indexMap = indexMaps_.back();
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:
P2PCommunicatorType toIORankComm_;
IndexMapType globalCartesianIndex_;
IndexMapType localIndexMap_;
IndexMapStorageType indexMaps_;
2018-07-31 08:35:15 -05:00
std::vector<int> globalRanks_;
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_;
};
} // end namespace Ewoms
#endif