From 4ebf7c893d3c6320531b9129608c814a3f80d169 Mon Sep 17 00:00:00 2001 From: John Tramm Date: Thu, 23 Jan 2020 18:29:51 +0000 Subject: [PATCH] initial commit of sharedarray object. --- include/openmc/shared_array.h | 93 +++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 include/openmc/shared_array.h diff --git a/include/openmc/shared_array.h b/include/openmc/shared_array.h new file mode 100644 index 0000000000..d0deafec6e --- /dev/null +++ b/include/openmc/shared_array.h @@ -0,0 +1,93 @@ +#ifndef OPENMC_SHARED_ARRAY_H +#define OPENMC_SHARED_ARRAY_H + +//! \file shared_array.h +//! \brief Shared array data structure + +namespace openmc { + +//============================================================================== +// Class declarations +//============================================================================== + +template +class SharedArray { + +private: + //========================================================================== + // Data members + std::unique_ptr data_; + int64_t size_; + int64_t capacity_; + +public: + //========================================================================== + // Constructors + + SharedArray() + { + capacity_ = 0; + size_ = 0; + } + + SharedArray(int64_t capacity) : capacity_(capacity) + { + data_ = std::make_unique(capacity); + size_ = 0; + } + + //========================================================================== + // Methods and Accessors + + T& operator[](int64_t i) {return data_[i];} + + void reserve(int64_t capacity) + { + data_ = std::make_unique(capacity); + capacity_ = capacity; + } + + //! Increases the size of the SharedArray by one and returns an index to the + //! last element of the array. + int64_t append() + { + // Atomically capture the index we want to write to + int64_t idx; + #pragma omp atomic capture + idx = size_++; + + // Check that we haven't written off the end of the array + if (idx >= capacity_) { + #pragma omp atomic write + size_ = capacity_; + return -1; + } + + return idx; + } + + void clear() + { + data_.reset(); + size_ = 0; + capacity_ = 0; + } + + int64_t size() + { + int size; + #pragma omp atomic read + size = size_; + return size; + } + + int64_t capacity() + { + return capacity_; + } + +}; + +} // namespace openmc + +#endif // OPENMC_SHARED_ARRAY_H