Mantid
Loading...
Searching...
No Matches
EventWorkspaceMRU.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
8
9#include <algorithm>
10
11namespace Mantid::DataObjects {
12
14 // Make sure you free up the memory in the MRUs
15 {
16 Poco::ScopedWriteRWLock _lock(m_changeMruListsMutexY);
17 for (auto &data : m_bufferedDataY) {
18 if (data) {
19 data->clear();
20 }
21 }
22 }
23 Poco::ScopedWriteRWLock _lock2(m_changeMruListsMutexE);
24 for (auto &data : m_bufferedDataE) {
25 if (data) {
26 data->clear();
27 }
28 }
29}
30
31//---------------------------------------------------------------------------
36void EventWorkspaceMRU::ensureEnoughBuffersE(size_t thread_num) const {
37 Poco::ScopedWriteRWLock _lock(m_changeMruListsMutexE);
38 if (m_bufferedDataE.size() <= thread_num) {
39 m_bufferedDataE.resize(thread_num + 1);
40 // Replaces empty data with an MRU list with this many entries.
41 std::transform(m_bufferedDataE.begin(), m_bufferedDataE.end(), m_bufferedDataE.begin(),
42 [](auto &data) { return data ? std::move(data) : std::move(std::make_unique<mru_listE>(50)); });
43 }
44}
45//---------------------------------------------------------------------------
50void EventWorkspaceMRU::ensureEnoughBuffersY(size_t thread_num) const {
51 Poco::ScopedWriteRWLock _lock(m_changeMruListsMutexY);
52 if (m_bufferedDataY.size() <= thread_num) {
53 m_bufferedDataY.resize(thread_num + 1);
54 // Replaces empty data with an MRU list with this many entries.
55 std::transform(m_bufferedDataY.begin(), m_bufferedDataY.end(), m_bufferedDataY.begin(),
56 [](auto &data) { return data ? std::move(data) : std::move(std::make_unique<mru_listY>(50)); });
57 }
58}
59
60//---------------------------------------------------------------------------
63 {
64 // Make sure you free up the memory in the MRUs
65 Poco::ScopedWriteRWLock _lock(m_changeMruListsMutexY);
66 for (auto &data : m_bufferedDataY) {
67 if (data) {
68 data->clear();
69 }
70 }
71 }
72
73 Poco::ScopedWriteRWLock _lock(m_changeMruListsMutexE);
74 for (auto &data : m_bufferedDataE) {
75 if (data) {
76 data->clear();
77 }
78 }
79}
80
81//---------------------------------------------------------------------------
89 Poco::ScopedReadRWLock _lock(m_changeMruListsMutexY);
90 auto result = m_bufferedDataY[thread_num]->find(reinterpret_cast<std::uintptr_t>(index));
91 if (result)
92 return result->m_data;
93 return YType(nullptr);
94}
95
103 Poco::ScopedReadRWLock _lock(m_changeMruListsMutexE);
104 auto result = m_bufferedDataE[thread_num]->find(reinterpret_cast<std::uintptr_t>(index));
105 if (result)
106 return result->m_data;
107 return EType(nullptr);
108}
109
116void EventWorkspaceMRU::insertY(size_t thread_num, YType data, const EventList *index) {
117 Poco::ScopedReadRWLock _lock(m_changeMruListsMutexY);
118 auto yWithMarker = std::make_shared<TypeWithMarker<YType>>(reinterpret_cast<std::uintptr_t>(index));
119 yWithMarker->m_data = std::move(data);
120 m_bufferedDataY[thread_num]->insert(yWithMarker);
121 // the memory is cleared automatically due to being a smart_ptr
122}
123
130void EventWorkspaceMRU::insertE(size_t thread_num, EType data, const EventList *index) {
131 Poco::ScopedReadRWLock _lock(m_changeMruListsMutexE);
132 auto eWithMarker = std::make_shared<TypeWithMarker<EType>>(reinterpret_cast<std::uintptr_t>(index));
133 eWithMarker->m_data = std::move(data);
134 m_bufferedDataE[thread_num]->insert(eWithMarker);
135 // And clear up the memory of the old one, if it is dropping out.
136}
137
143 // acquire lock if the buffer isn't empty
144 if (!m_bufferedDataE.empty()) {
145 Poco::ScopedReadRWLock _lock1(m_changeMruListsMutexE);
146 for (auto &data : m_bufferedDataE) {
147 if (data) {
148 data->deleteIndex(reinterpret_cast<std::uintptr_t>(index));
149 }
150 }
151 }
152 // acquire lock if the buffer isn't empty
153 if ((!m_bufferedDataY.empty())) {
154 Poco::ScopedReadRWLock _lock2(m_changeMruListsMutexY);
155 for (auto &data : m_bufferedDataY) {
156 if (data) {
157 data->deleteIndex(reinterpret_cast<std::uintptr_t>(index));
158 }
159 }
160 }
161}
162
164 if (m_bufferedDataY.empty()) {
165 return 0;
166 } else {
167 return this->m_bufferedDataY.front()->size();
168 }
169}
170
171} // namespace Mantid::DataObjects
std::map< DeltaEMode::Type, std::string > index
A class for holding :
Definition EventList.h:57
Poco::RWLock m_changeMruListsMutexE
Mutex when adding entries in the MRU list.
std::vector< std::unique_ptr< mru_listE > > m_bufferedDataE
The most-recently-used list of dataE histograms.
std::vector< std::unique_ptr< mru_listY > > m_bufferedDataY
The most-recently-used list of dataY histograms.
void insertY(size_t thread_num, YType data, const EventList *index)
Insert a new histogram into the MRU.
Kernel::cow_ptr< HistogramData::HistogramY > YType
EType findE(size_t thread_num, const EventList *index)
Find a Y histogram in the MRU.
void insertE(size_t thread_num, EType data, const EventList *index)
Insert a new histogram into the MRU.
Kernel::cow_ptr< HistogramData::HistogramE > EType
void ensureEnoughBuffersE(size_t thread_num) const
This function makes sure that there are enough data buffers (MRU's) for E for the number of threads r...
YType findY(size_t thread_num, const EventList *index)
Find a Y histogram in the MRU.
size_t MRUSize() const
Return how many entries in the Y MRU list are used.
void deleteIndex(const EventList *index)
Delete any entries in the MRU at the given index.
void clear()
Clear all the data in the MRU buffers.
void ensureEnoughBuffersY(size_t thread_num) const
This function makes sure that there are enough data buffers (MRU's) for Y for the number of threads r...
Implements a copy on write data template.
Definition cow_ptr.h:41