Newer
Older
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*
* Distributed under the OSI-approved Apache License, Version 2.0. See
* accompanying file Copyright.txt for details.
*
* VariableBase.cpp
*
* Created on: May 11, 2017
* Author: William F Godoy godoywf@ornl.gov
*/
#include "VariableBase.h"
/// \cond EXCLUDE_FROM_DOXYGEN
#include <algorithm> //std::count
#include <stdexcept> //std::invalid_argument
/// \endcond
namespace adios
{
VariableBase::VariableBase(const std::string &name, const std::string type,
const size_t elementSize, const Dims shape,
const Dims start, const Dims count,
const bool constantShape, const bool debugMode)
: m_Name(name), m_Type(type), m_ElementSize(elementSize), m_Shape(shape),
m_Start(start), m_Count(count), m_ConstantShape(constantShape),
m_DebugMode(debugMode)
{
InitShapeType();
}
size_t VariableBase::PayLoadSize() const noexcept
{
return GetTotalSize(m_Count) * m_ElementSize;
}
size_t VariableBase::TotalSize() const noexcept
{
return GetTotalSize(m_Count);
}
void VariableBase::SetSelection(const Dims start, const Dims count)
{
if (m_DebugMode == true)
{
if (m_SingleValue)
{
throw std::invalid_argument(
"ERROR: selection is not valid for single value variable " +
m_Name + ", in call to SetSelection\n");
}
if (m_ConstantShape)
{
throw std::invalid_argument(
"ERROR: selection is not valid for constant shape variable " +
m_Name + ", in call to SetSelection\n");
}
if (m_ShapeID == ShapeID::GlobalArray &&
(m_Shape.size() != m_Count.size() ||
m_Shape.size() != m_Start.size()))
{
throw std::invalid_argument("ERROR: count and start must be the "
"same size as shape for variable " +
m_Name + ", in call to SetSelection\n");
}
if (m_ShapeID == ShapeID::LocalArray && !start.empty())
{
throw std::invalid_argument("ERROR: start argument must be empty "
"for local array variable " +
m_Name + ", in call to SetSelection\n");
}
if (m_ShapeID == ShapeID::JoinedArray && !start.empty())
{
throw std::invalid_argument("ERROR: start argument must be empty "
"for joined array variable " +
m_Name + ", in call to SetSelection\n");
}
}
m_Count = count;
m_Start = start;
}
void VariableBase::SetSelection(const SelectionBoundingBox &selection)
{
Dims start, count;
ConvertUint64VectorToSizetVector(selection.m_Start, start);
ConvertUint64VectorToSizetVector(selection.m_Count, count);
SetSelection(start, count);
}
void VariableBase::SetMemorySelection(const SelectionBoundingBox &selection)
{
if (m_DebugMode == true)
{
if (m_SingleValue)
{
throw std::invalid_argument("ERROR: memory selection is not valid "
"for single value variable " +
m_Name +
", in call to SetMemorySelection\n");
}
if (m_Shape.size() != selection.m_Count.size() ||
m_Shape.size() != selection.m_Start.size())
{
throw std::invalid_argument(
"ERROR: selection argument m_Count and m_Start sizes must be "
"the "
"same as variable " +
m_Name + " m_Shape, in call to SetMemorySelction\n");
}
}
ConvertUint64VectorToSizetVector(selection.m_Count, m_MemoryCount);
ConvertUint64VectorToSizetVector(selection.m_Start, m_MemoryStart);
}
void VariableBase::SetStepSelection(const unsigned int startStep,
const unsigned int countStep)
{
m_ReadFromStep = startStep;
m_ReadNSteps = countStep;
}
// transforms related functions
void VariableBase::ClearTransforms() { m_TransformsInfo.clear(); }
// PRIVATE
void VariableBase::InitShapeType()
{
if (!m_Shape.empty() && m_Start.empty() && m_Count.empty())
{
if (m_DebugMode == true)
{
if (m_ConstantShape == true)
{
throw std::invalid_argument(
"ERROR: isConstantShape (true) argument is invalid "
"with empty start and count "
"arguments\n");
}
}
m_ShapeID = ShapeID::GlobalArray;
}
else if (!m_Shape.empty() && m_Shape.size() == m_Start.size() &&
m_Shape.size() == m_Count.size())
{
if (m_DebugMode == true)
{
auto lf_LargerThanError = [&](const unsigned int i,
const std::string dims1,
const std::string dims2) {
const std::string iString(std::to_string(i));
throw std::invalid_argument(
"ERROR: " + dims1 + "[" + iString + "] > " + dims2 + "[" +
iString + "], in DefineVariable " + m_Name + "\n");
};
for (unsigned int i = 0; i < m_Shape.size(); ++i)
{
if (m_Count[i] > m_Shape[i])
{
lf_LargerThanError(i, "count", "shape");
}
if (m_Start[i] > m_Shape[i])
{
lf_LargerThanError(i, "start", "shape");
}
}
}
m_ShapeID = ShapeID::GlobalArray;
}
else if (m_Shape.empty() && m_Start.empty() && m_Count.empty())
{
m_ShapeID = ShapeID::GlobalValue;
m_SingleValue = true;
}
else if (m_Shape.empty() && m_Start.empty() && !m_Count.empty())
{
m_ShapeID = ShapeID::LocalArray;
}
else if (m_Shape.size() == 1 && m_Shape.front() == LocalValueDim)
{
m_ShapeID = ShapeID::LocalValue;
m_SingleValue = true;
}
else if (!m_Shape.empty() &&
std::count(m_Shape.begin(), m_Shape.end(), JoinedDim) == 1)
{
m_ShapeID = ShapeID::JoinedArray;
}
else if (!m_Shape.empty() &&
std::count(m_Shape.begin(), m_Shape.end(), JoinedDim) > 1)
{
throw std::invalid_argument("ERROR: variable can't have more than one "
"JoinedDim in shape argument, in call to "
"DefineVariable " +
m_Name + "\n");
}
else
{
throw std::invalid_argument("ERROR: the "
"combination of shape, start and count "
"arguments is inconsistent, in call to "
"DefineVariable " +
m_Name + "\n");
}
}
void VariableBase::CheckDims(const std::string hint) const
{
if (m_ShapeID == ShapeID::GlobalArray)
{
if (m_Start.empty() || m_Count.empty())
{
throw std::invalid_argument(
"ERROR: GlobalArray variable " + m_Name +
" start and count dimensions must be defined by either "
"DefineVariable or a Selection " +
hint + "\n");
}
}
// TODO need to think more exceptions here
}
} // end namespace adios