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 +
9
10#include <algorithm>
11
12namespace Mantid::DataObjects {
13
15 // Make sure you free up the memory in the MRUs
16 {
17 Poco::ScopedWriteRWLock _lock(m_changeMruListsMutexY);
18 for (auto &data : m_bufferedDataY) {
19 if (data) {
20 data->clear();
21 }
22 }
23 }
24 Poco::ScopedWriteRWLock _lock2(m_changeMruListsMutexE);
25 for (auto &data : m_bufferedDataE) {
26 if (data) {
27 data->clear();
28 }
29 }
30}
31
32//---------------------------------------------------------------------------
37void EventWorkspaceMRU::ensureEnoughBuffersE(size_t thread_num) const {
38 Poco::ScopedWriteRWLock _lock(m_changeMruListsMutexE);
39 if (m_bufferedDataE.size() <= thread_num) {
40 m_bufferedDataE.resize(thread_num + 1);
41 // Replaces empty data with an MRU list with this many entries.
42 std::transform(m_bufferedDataE.begin(), m_bufferedDataE.end(), m_bufferedDataE.begin(),
43 [](auto &data) { return data ? std::move(data) : std::move(std::make_unique<mru_listE>(50)); });
44 }
45}
46//---------------------------------------------------------------------------
51void EventWorkspaceMRU::ensureEnoughBuffersY(size_t thread_num) const {
52 Poco::ScopedWriteRWLock _lock(m_changeMruListsMutexY);
53 if (m_bufferedDataY.size() <= thread_num) {
54 m_bufferedDataY.resize(thread_num + 1);
55 // Replaces empty data with an MRU list with this many entries.
56 std::transform(m_bufferedDataY.begin(), m_bufferedDataY.end(), m_bufferedDataY.begin(),
57 [](auto &data) { return data ? std::move(data) : std::move(std::make_unique<mru_listY>(50)); });
58 }
59}
60
61//---------------------------------------------------------------------------
64 {
65 // Make sure you free up the memory in the MRUs
66 Poco::ScopedWriteRWLock _lock(m_changeMruListsMutexY);
67 for (auto &data : m_bufferedDataY) {
68 if (data) {
69 data->clear();
70 }
71 }
72 }
73
74 Poco::ScopedWriteRWLock _lock(m_changeMruListsMutexE);
75 for (auto &data : m_bufferedDataE) {
76 if (data) {
77 data->clear();
78 }
79 }
80}
81
82//---------------------------------------------------------------------------
90 Poco::ScopedReadRWLock _lock(m_changeMruListsMutexY);
91 auto result = m_bufferedDataY[thread_num]->find(reinterpret_cast<std::uintptr_t>(index));
92 if (result)
93 return result->m_data;
94 return YType(nullptr);
95}
96
104 Poco::ScopedReadRWLock _lock(m_changeMruListsMutexE);
105 auto result = m_bufferedDataE[thread_num]->find(reinterpret_cast<std::uintptr_t>(index));
106 if (result)
107 return result->m_data;
108 return EType(nullptr);
109}
110
117void EventWorkspaceMRU::insertY(size_t thread_num, YType data, const EventList *index) {
118 Poco::ScopedReadRWLock _lock(m_changeMruListsMutexY);
119 auto yWithMarker = std::make_shared<TypeWithMarker<YType>>(reinterpret_cast<std::uintptr_t>(index));
120 yWithMarker->m_data = std::move(data);
121 m_bufferedDataY[thread_num]->insert(yWithMarker);
122 // the memory is cleared automatically due to being a smart_ptr
123}
124
131void EventWorkspaceMRU::insertE(size_t thread_num, EType data, const EventList *index) {
132 Poco::ScopedReadRWLock _lock(m_changeMruListsMutexE);
133 auto eWithMarker = std::make_shared<TypeWithMarker<EType>>(reinterpret_cast<std::uintptr_t>(index));
134 eWithMarker->m_data = std::move(data);
135 m_bufferedDataE[thread_num]->insert(eWithMarker);
136 // And clear up the memory of the old one, if it is dropping out.
137}
138
144 {
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 Poco::ScopedReadRWLock _lock2(m_changeMruListsMutexY);
153 for (auto &data : m_bufferedDataY) {
154 if (data) {
155 data->deleteIndex(reinterpret_cast<std::uintptr_t>(index));
156 }
157 }
158}
159
161 if (m_bufferedDataY.empty()) {
162 return 0;
163 } else {
164 return this->m_bufferedDataY.front()->size();
165 }
166}
167
168} // namespace Mantid::DataObjects
std::map< DeltaEMode::Type, std::string > index
Definition: DeltaEMode.cpp:19
A class for holding :
Definition: EventList.h:56
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