Line data Source code
1 : //* This file is part of the MOOSE framework 2 : //* https://mooseframework.inl.gov 3 : //* 4 : //* All rights reserved, see COPYRIGHT for full restrictions 5 : //* https://github.com/idaholab/moose/blob/master/COPYRIGHT 6 : //* 7 : //* Licensed under LGPL 2.1, please see LICENSE for details 8 : //* https://www.gnu.org/licenses/lgpl-2.1.html 9 : 10 : #pragma once 11 : 12 : #include "libmesh/parallel_object.h" 13 : #include "libmesh/parallel.h" 14 : #include "MooseTypes.h" 15 : 16 : namespace StochasticTools 17 : { 18 : 19 : /** 20 : * Templated class that specifies a distributed storage of a vector of given 21 : * objects. It has a helper vector that contains global IDs for every stored item. 22 : */ 23 : template <typename T> 24 : class DistributedData : public libMesh::ParallelObject 25 : { 26 : public: 27 : DistributedData(const libMesh::Parallel::Communicator & comm_in); 28 : 29 : /// Initialize the container with a given number of samples. this partitions 30 : /// the samples using linearPartitioning. 31 : void initializeContainer(unsigned int num_global_entries); 32 : 33 : /// Changing a sample with a global index if it is owned locally. 34 : void changeEntry(unsigned int glob_i, const T & sample); 35 : 36 : /// Adding a new sample locally with a global index. 37 : void addNewEntry(unsigned int glob_i, const T & sample); 38 : 39 : /// Closes the container meaning that no new samples can be added or the 40 : /// already existing samples canot be changed. 41 0 : void closeContainer() { _closed = true; }; 42 : 43 : /// Checking of sample with global ID is locally owned ot not. 44 : bool hasGlobalEntry(unsigned int glob_i) const; 45 : 46 : /// Getting an itertor to the beginning of the local samples. 47 0 : typename std::vector<T>::iterator localEntryBegin() { return _local_entries.begin(); }; 48 : 49 : /// Getting an iterator to the end of the locally owned samples. 50 0 : typename std::vector<T>::iterator localEntryEnd() { return _local_entries.end(); }; 51 : 52 : /// Getting an iterator to the beginning of the locally owned sample IDs. 53 0 : typename std::vector<unsigned int>::iterator localEntryIDBegin() 54 : { 55 0 : return _local_entry_ids.begin(); 56 : }; 57 : 58 : /// Getting an iterator to the end of the locally owned sample IDs. 59 0 : typename std::vector<unsigned int>::iterator localEntryIDEnd() { return _local_entry_ids.end(); }; 60 : 61 : /// Getting a sample using its global index. 62 : const T & getGlobalEntry(unsigned int glob_i) const; 63 : 64 : /// Getting a sample using its local index. 65 : const T & getLocalEntry(unsigned int loc_i) const; 66 : 67 : /// Getting all of the locally owned samples. 68 0 : const std::vector<T> & getLocalEntries() const { return _local_entries; }; 69 : 70 : /// Getting the vector of sample IDs for locally owned samples. 71 0 : const std::vector<unsigned int> & getLocalEntryIDs() const { return _local_entry_ids; }; 72 : 73 : /// Getting the number of global samples. 74 : unsigned int getNumberOfGlobalEntries() const; 75 : 76 : /// Getting the number of locally owned samples. 77 320 : unsigned int getNumberOfLocalEntries() const { return _n_local_entries; }; 78 : 79 : /// Getting the local index of a global sample if locally owned. 80 : unsigned int getLocalIndex(unsigned int glob_i) const; 81 : 82 : /// Getting the global index of a locally owned sample. 83 : unsigned int getGlobalIndex(unsigned int loc_i) const; 84 : 85 : protected: 86 : /// The vector where the samples are stored. 87 : std::vector<T> _local_entries; 88 : 89 : /// The vector where the global sample IDs are stored. 90 : std::vector<unsigned int> _local_entry_ids; 91 : 92 : /// Flag which shows if the container is closed or not. 93 : bool _closed; 94 : 95 : /// Number of local samples. 96 : unsigned int _n_local_entries; 97 : }; 98 : 99 : } // StochasticTools namespace