mirror of
https://github.com/openmc-dev/openmc.git
synced 2026-07-28 14:15:42 -04:00
initial commit of sharedarray object.
This commit is contained in:
parent
e450eb82d2
commit
4ebf7c893d
1 changed files with 93 additions and 0 deletions
93
include/openmc/shared_array.h
Normal file
93
include/openmc/shared_array.h
Normal file
|
|
@ -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 <typename T>
|
||||
class SharedArray {
|
||||
|
||||
private:
|
||||
//==========================================================================
|
||||
// Data members
|
||||
std::unique_ptr<T[]> data_;
|
||||
int64_t size_;
|
||||
int64_t capacity_;
|
||||
|
||||
public:
|
||||
//==========================================================================
|
||||
// Constructors
|
||||
|
||||
SharedArray()
|
||||
{
|
||||
capacity_ = 0;
|
||||
size_ = 0;
|
||||
}
|
||||
|
||||
SharedArray(int64_t capacity) : capacity_(capacity)
|
||||
{
|
||||
data_ = std::make_unique<T[]>(capacity);
|
||||
size_ = 0;
|
||||
}
|
||||
|
||||
//==========================================================================
|
||||
// Methods and Accessors
|
||||
|
||||
T& operator[](int64_t i) {return data_[i];}
|
||||
|
||||
void reserve(int64_t capacity)
|
||||
{
|
||||
data_ = std::make_unique<T[]>(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
|
||||
Loading…
Add table
Add a link
Reference in a new issue