• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

openmc-dev / openmc / 12776996362

14 Jan 2025 09:49PM UTC coverage: 84.938% (+0.2%) from 84.729%
12776996362

Pull #3133

github

web-flow
Merge 0495246d9 into 549cc0973
Pull Request #3133: Kinetics parameters using Iterated Fission Probability

318 of 330 new or added lines in 10 files covered. (96.36%)

1658 existing lines in 66 files now uncovered.

50402 of 59340 relevant lines covered (84.94%)

33987813.96 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

77.78
/include/openmc/shared_array.h
1
#ifndef OPENMC_SHARED_ARRAY_H
2
#define OPENMC_SHARED_ARRAY_H
3

4
//! \file shared_array.h
5
//! \brief Shared array data structure
6

7
#include "openmc/memory.h"
8

9
namespace openmc {
10

11
//==============================================================================
12
// Class declarations
13
//==============================================================================
14

15
// This container is an array that is capable of being appended to in an
16
// thread safe manner by use of atomics. It only provides protection for the
17
// use cases currently present in OpenMC. Namely, it covers the scenario where
18
// multiple threads are appending to an array, but no threads are reading from
19
// or operating on it in any other way at the same time. Multiple threads can
20
// call the thread_safe_append() function concurrently and store data to the
21
// object at the index returned from thread_safe_append() safely, but no other
22
// operations are protected.
23
template<typename T>
24
class SharedArray {
25

26
public:
27
  //==========================================================================
28
  // Constructors
29

30
  //! Default constructor.
31
  SharedArray() = default;
32

33
  //! Construct a zero size container with space to hold capacity number of
34
  //! elements.
35
  //
36
  //! \param capacity The number of elements for the container to allocate
37
  //! space for
38
  SharedArray(int64_t capacity) : capacity_(capacity)
39
  {
40
    data_ = make_unique<T[]>(capacity);
41
  }
42

43
  //==========================================================================
44
  // Methods and Accessors
45

46
  //! Return a reference to the element at specified location i. No bounds
47
  //! checking is performed.
48
  T& operator[](int64_t i) { return data_[i]; }
49
  const T& operator[](int64_t i) const { return data_[i]; }
50

51
  //! Allocate space in the container for the specified number of elements.
52
  //! reserve() does not change the size of the container.
53
  //
54
  //! \param capacity The number of elements to allocate in the container
55
  void reserve(int64_t capacity)
56
  {
57
    data_ = make_unique<T[]>(capacity);
58
    capacity_ = capacity;
59
  }
60

61
  //! Increase the size of the container by one and append value to the
62
  //! array. Returns an index to the element of the array written to. Also
63
  //! tests to enforce that the append operation does not read off the end
64
  //! of the array. In the event that this does happen, set the size to be
65
  //! equal to the capacity and return -1.
66
  //
67
  //! \value The value of the element to append
68
  //! \return The index in the array written to. In the event that this
69
  //! index would be greater than what was allocated for the container,
70
  //! return -1.
71
  int64_t thread_safe_append(const T& value)
148,626,787✔
72
  {
73
    // Atomically capture the index we want to write to
74
    int64_t idx;
75
#pragma omp atomic capture seq_cst
74,743,790✔
76
    idx = size_++;
148,626,787✔
77

78
    // Check that we haven't written off the end of the array
79
    if (idx >= capacity_) {
148,626,787✔
80
#pragma omp atomic write seq_cst
UNCOV
81
      size_ = capacity_;
×
UNCOV
82
      return -1;
×
83
    }
84

85
    // Copy element value to the array
86
    data_[idx] = value;
148,626,787✔
87

88
    return idx;
148,626,787✔
89
  }
90

91
  //! Free any space that was allocated for the container. Set the
92
  //! container's size and capacity to 0.
93
  void clear()
94
  {
95
    data_.reset();
96
    size_ = 0;
97
    capacity_ = 0;
98
  }
99

100
  //! Return the number of elements in the container
101
  int64_t size() { return size_; }
102

103
  //! Resize the container to contain a specified number of elements. This is
104
  //! useful in cases where the container is written to in a non-thread safe
105
  //! manner, where the internal size of the array needs to be manually updated.
106
  //
107
  //! \param size The new size of the container
108
  void resize(int64_t size) { size_ = size; }
109

110
  //! Return whether the array is full
111
  bool full() const { return size_ == capacity_; }
1,145,833,439✔
112

113
  //! Return the number of elements that the container has currently allocated
114
  //! space for.
115
  int64_t capacity() { return capacity_; }
116

117
  //! Return pointer to the underlying array serving as element storage.
118
  T* data() { return data_.get(); }
119
  const T* data() const { return data_.get(); }
120

121
  //! Classic iterators
122
  T* begin() { return data_.get(); }
123
  const T* cbegin() const { return data_.get(); }
124
  T* end() { return data_.get() + size_; }
125
  const T* cend() const { return data_.get() + size_; }
126

127
private:
128
  //==========================================================================
129
  // Data members
130

131
  unique_ptr<T[]> data_; //!< An RAII handle to the elements
132
  int64_t size_ {0};     //!< The current number of elements
133
  int64_t capacity_ {0}; //!< The total space allocated for elements
134
};
135

136
} // namespace openmc
137

138
#endif // OPENMC_SHARED_ARRAY_H
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc