forked from npshub/mantid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EventWorkspaceMRU.cpp
168 lines (156 loc) · 5.64 KB
/
EventWorkspaceMRU.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
// NScD Oak Ridge National Laboratory, European Spallation Source,
// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
// SPDX - License - Identifier: GPL - 3.0 +
#include "MantidDataObjects/EventWorkspaceMRU.h"
#include "MantidKernel/System.h"
namespace Mantid::DataObjects {
EventWorkspaceMRU::~EventWorkspaceMRU() {
// Make sure you free up the memory in the MRUs
{
Poco::ScopedWriteRWLock _lock(m_changeMruListsMutexY);
for (auto &data : m_bufferedDataY) {
if (data) {
data->clear();
}
}
}
Poco::ScopedWriteRWLock _lock2(m_changeMruListsMutexE);
for (auto &data : m_bufferedDataE) {
if (data) {
data->clear();
}
}
}
//---------------------------------------------------------------------------
/** This function makes sure that there are enough data
* buffers (MRU's) for E for the number of threads requested.
* @param thread_num :: thread number that wants a MRU buffer
*/
void EventWorkspaceMRU::ensureEnoughBuffersE(size_t thread_num) const {
Poco::ScopedWriteRWLock _lock(m_changeMruListsMutexE);
if (m_bufferedDataE.size() <= thread_num) {
m_bufferedDataE.resize(thread_num + 1);
for (auto &data : m_bufferedDataE) {
if (!data)
data = std::make_unique<mru_listE>(50); // Create a MRU list with this many entries.
}
}
}
//---------------------------------------------------------------------------
/** This function makes sure that there are enough data
* buffers (MRU's) for Y for the number of threads requested.
* @param thread_num :: thread number that wants a MRU buffer
*/
void EventWorkspaceMRU::ensureEnoughBuffersY(size_t thread_num) const {
Poco::ScopedWriteRWLock _lock(m_changeMruListsMutexY);
if (m_bufferedDataY.size() <= thread_num) {
m_bufferedDataY.resize(thread_num + 1);
for (auto &data : m_bufferedDataY) {
if (!data)
data = std::make_unique<mru_listY>(50); // Create a MRU list with this many entries.
}
}
}
//---------------------------------------------------------------------------
/// Clear all the data in the MRU buffers
void EventWorkspaceMRU::clear() {
{
// Make sure you free up the memory in the MRUs
Poco::ScopedWriteRWLock _lock(m_changeMruListsMutexY);
for (auto &data : m_bufferedDataY) {
if (data) {
data->clear();
}
}
}
Poco::ScopedWriteRWLock _lock(m_changeMruListsMutexE);
for (auto &data : m_bufferedDataE) {
if (data) {
data->clear();
}
}
}
//---------------------------------------------------------------------------
/** Find a Y histogram in the MRU
*
* @param thread_num :: number of the thread in which this is run
* @param index :: index of the data to return
* @return pointer to the TypeWithMarker that has the data; NULL if not found.
*/
Kernel::cow_ptr<HistogramData::HistogramY> EventWorkspaceMRU::findY(size_t thread_num, const EventList *index) {
Poco::ScopedReadRWLock _lock(m_changeMruListsMutexY);
auto result = m_bufferedDataY[thread_num]->find(reinterpret_cast<std::uintptr_t>(index));
if (result)
return result->m_data;
return YType(nullptr);
}
/** Find a Y histogram in the MRU
*
* @param thread_num :: number of the thread in which this is run
* @param index :: index of the data to return
* @return pointer to the TypeWithMarker that has the data; NULL if not found.
*/
Kernel::cow_ptr<HistogramData::HistogramE> EventWorkspaceMRU::findE(size_t thread_num, const EventList *index) {
Poco::ScopedReadRWLock _lock(m_changeMruListsMutexE);
auto result = m_bufferedDataE[thread_num]->find(reinterpret_cast<std::uintptr_t>(index));
if (result)
return result->m_data;
return EType(nullptr);
}
/** Insert a new histogram into the MRU
*
* @param thread_num :: thread being accessed
* @param data :: the new data
* @param index :: index of the data to insert
*/
void EventWorkspaceMRU::insertY(size_t thread_num, YType data, const EventList *index) {
Poco::ScopedReadRWLock _lock(m_changeMruListsMutexY);
auto yWithMarker = std::make_shared<TypeWithMarker<YType>>(reinterpret_cast<std::uintptr_t>(index));
yWithMarker->m_data = std::move(data);
m_bufferedDataY[thread_num]->insert(yWithMarker);
// the memory is cleared automatically due to being a smart_ptr
}
/** Insert a new histogram into the MRU
*
* @param thread_num :: thread being accessed
* @param data :: the new data
* @param index :: index of the data to insert
*/
void EventWorkspaceMRU::insertE(size_t thread_num, EType data, const EventList *index) {
Poco::ScopedReadRWLock _lock(m_changeMruListsMutexE);
auto eWithMarker = std::make_shared<TypeWithMarker<EType>>(reinterpret_cast<std::uintptr_t>(index));
eWithMarker->m_data = std::move(data);
auto oldData = m_bufferedDataE[thread_num]->insert(eWithMarker);
// And clear up the memory of the old one, if it is dropping out.
}
/** Delete any entries in the MRU at the given index
*
* @param index :: index to delete.
*/
void EventWorkspaceMRU::deleteIndex(const EventList *index) {
{
Poco::ScopedReadRWLock _lock1(m_changeMruListsMutexE);
for (auto &data : m_bufferedDataE) {
if (data) {
data->deleteIndex(reinterpret_cast<std::uintptr_t>(index));
}
}
}
Poco::ScopedReadRWLock _lock2(m_changeMruListsMutexY);
for (auto &data : m_bufferedDataY) {
if (data) {
data->deleteIndex(reinterpret_cast<std::uintptr_t>(index));
}
}
}
size_t EventWorkspaceMRU::MRUSize() const {
if (m_bufferedDataY.empty()) {
return 0;
} else {
return this->m_bufferedDataY.front()->size();
}
}
} // namespace Mantid::DataObjects