Loading...
Searching...
No Matches
IndexMap.h
// Copyright (C) 2015 Chris Richardson
//
// This file is part of DOLFIN.
//
// DOLFIN is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// DOLFIN 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 Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with DOLFIN. If not, see <http://www.gnu.org/licenses/>.
#ifndef __INDEX_MAP_H
#define __INDEX_MAP_H
#include <utility>
#include <vector>
#include <dolfin/common/MPI.h>
namespace dolfin
{
class IndexMap
{
public:
enum class MapSize : int32_t { ALL = 0,
OWNED = 1,
UNOWNED = 2,
GLOBAL = 3 };
IndexMap();
explicit IndexMap(MPI_Comm mpi_comm);
IndexMap(MPI_Comm mpi_comm, std::size_t local_size, std::size_t block_size);
~IndexMap();
void init(std::size_t local_size, std::size_t block_size);
std::pair<std::size_t, std::size_t> local_range() const;
std::size_t size(MapSize type) const;
const std::vector<std::size_t>& local_to_global_unowned() const;
std::size_t local_to_global(std::size_t i) const;
void set_local_to_global(const std::vector<std::size_t>& indices);
const std::vector<int>& off_process_owner() const;
int global_index_owner(std::size_t index) const;
int block_size() const;
MPI_Comm mpi_comm() const;
private:
// MPI Communicator
dolfin::MPI::Comm _mpi_comm;
// Cache rank of mpi_comm (otherwise calls to MPI_Comm_rank can be
// excessive)
unsigned int _rank;
// Range of ownership of index for all processes
std::vector<std::size_t> _all_ranges;
// Local to global map for off-process entries
std::vector<std::size_t> _local_to_global;
// Off process owner cache
std::vector<int> _off_process_owner;
// Block size
int _block_size;
};
// Function which may appear in a hot loop
inline std::size_t IndexMap::local_to_global(std::size_t i) const
{
// These two calls get hopefully optimized out of hot loops due to
// inlining
const std::size_t local_size = size(IndexMap::MapSize::OWNED);
const std::size_t global_offset = local_range().first;
if (i < local_size)
return (i + global_offset);
else
{
const std::div_t div = std::div((i - local_size), _block_size);
const int component = div.rem;
const int index = div.quot;
dolfin_assert((std::size_t) index < _local_to_global.size());
return _block_size*_local_to_global[index] + component;
}
}
}
#endif
dolfin::IndexMap::local_to_global
std::size_t local_to_global(std::size_t i) const
Get global index of local index i.
Definition IndexMap.h:118
dolfin::IndexMap::init
void init(std::size_t local_size, std::size_t block_size)
Definition IndexMap.cpp:49
dolfin::IndexMap::~IndexMap
~IndexMap()
Destructor.
Definition IndexMap.cpp:44
dolfin::IndexMap::set_local_to_global
void set_local_to_global(const std::vector< std::size_t > &indices)
Definition IndexMap.cpp:112
dolfin::IndexMap::local_to_global_unowned
const std::vector< std::size_t > & local_to_global_unowned() const
Definition IndexMap.cpp:107
dolfin::IndexMap::block_size
int block_size() const
Get block size.
Definition IndexMap.cpp:137
dolfin::IndexMap::IndexMap
IndexMap()
Constructor.
Definition IndexMap.cpp:25
dolfin::IndexMap::global_index_owner
int global_index_owner(std::size_t index) const
Get process owner of any global index.
Definition IndexMap.cpp:124
dolfin::IndexMap::MapSize
MapSize
Definition IndexMap.h:42
dolfin::IndexMap::size
std::size_t size(MapSize type) const
Definition IndexMap.cpp:77
dolfin::IndexMap::off_process_owner
const std::vector< int > & off_process_owner() const
Get off process owner for unowned indices.
Definition IndexMap.cpp:132
dolfin::IndexMap::local_range
std::pair< std::size_t, std::size_t > local_range() const
Local range of indices.
Definition IndexMap.cpp:63
dolfin::IndexMap::mpi_comm
MPI_Comm mpi_comm() const
Return MPI communicator.
Definition IndexMap.cpp:142
dolfin::MPI::Comm
Definition MPI.h:77
dolfin
Definition adapt.h:30