13#ifndef LOCALVECTORS_HH
14#define LOCALVECTORS_HH
29 template <
class Entry,
class SortedIdx>
35 : idx_(
idx), data(data_)
42 SortedIdx
idx()
const {
return idx_; }
93 template <
class Entry,
class SortedIdx,
class Vector>
105 LocalVectors(
Vector& globalVector, std::vector<std::mutex> & scatterMutexes_,
size_t maxStorage=256*1024)
106 : globalVector(&globalVector), scatterMutexes(&scatterMutexes_)
109 localData.reserve(maxStorage/
sizeof(Entry));
110 if(scatterMutexes_.empty())
144 size_t size = idx.size();
148 if (localData.capacity() < localData.size()+
size)
154 Entry* pos = &*localData.insert(localData.end(),
size,Entry(0));
155 localVectors.emplace_back(idx,pos);
173 size_t size()
const {
return localVectors.size(); }
184 if (localData.empty())
return;
188 throw DetailedException(
"No global vector given to scatter to",__FILE__,__LINE__);
192 unsigned n = scatterMutexes->size();
193 for (
unsigned ri=0; ri<n; ++ri)
196 std::lock_guard<std::mutex> lock((*scatterMutexes)[ri]);
199 for (
auto const&
vec : localVectors)
204 for (
auto const& glIdx :
vec.idx())
207 if (glIdx.first >= globalEndIdx)
break;
208 if (globalStartIdx <= glIdx.first)
210 (*globalVector)[glIdx.first] +=
vec[glIdx.second];
226 localVectors.clear();
234 return localData.size() *
sizeof(Entry);
241 std::vector<Entry> localData;
243 std::vector<value_type> localVectors;
245 Vector* globalVector =
nullptr;
246 std::vector<std::mutex>* scatterMutexes =
nullptr;
A wrapper class for conveniently providing exceptions with context information.
Providing a vector interface for contiguously stored entries.
size_t size() const
The number of entries stored.
Entry & operator[](int idx)
Access the matrix entries.
void relocate(Entry *newData)
Resets the data pointer.
LocalVector(SortedIdx const &idx, Entry *data_)
Entry const & operator[](int idx) const
SortedIdx idx() const
A sequence of indices, sorted ascendingly by global idx.
A structure for holding a sequence of several local vectors to be filled sequentially and to be scatt...
size_t size() const
The number of local vectors that are stored.
size_t storageSize() const
reports the size of the local vectors storage in bytes
value_type const & operator[](int n) const
value_type & back()
A reference to the last pushed local matrix.
void scatter()
Scatters the local vectors into the global one and removes them from the local container.
LocalVectors()
Default constructor.
value_type const & back() const
void push_back(SortedIdx const &idx)
Appends another (zero-initialized) local vector.
value_type & operator[](int n)
A reference to the n-th local matrix.
LocalVectors(Vector &globalVector, std::vector< std::mutex > &scatterMutexes_, size_t maxStorage=256 *1024)
void clear()
clears all the data, leaving an empty state
~LocalVectors()
Destructor.
Index uniformWeightRangeStart(BlockIndex i, BlockIndex n, Index m)
Computes partitioning points of ranges for uniform weight distributions.
Dune::FieldVector< Scalar, dim > Vector