Skip to content

Commit ad54bae

Browse files
committed
io/adios2: support scalar attributes
1 parent 8278cc2 commit ad54bae

7 files changed

Lines changed: 62 additions & 1 deletion

File tree

src/kg/include/kg/io/Engine.inl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ inline void Engine::putVariable(const T* data, const Mode launch,
9393
template <typename T>
9494
inline void Engine::putAttribute(const T& datum)
9595
{
96-
file_.putAttribute(prefix(), &datum, 1);
96+
file_.putAttribute(prefix(), datum);
9797
}
9898

9999
template <typename T>

src/kg/include/kg/io/File.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ class File
4141
template <typename T>
4242
void getAttribute(const std::string& name, T* data);
4343

44+
template <typename T>
45+
void putAttribute(const std::string& name, const T& datum);
46+
4447
template <typename T>
4548
void putAttribute(const std::string& name, const T* data, size_t size);
4649

src/kg/include/kg/io/File.inl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,14 @@ inline void File::getAttribute(const std::string& name, T* data)
6161
impl_->getAttribute(name, dataVar);
6262
}
6363

64+
template <typename T>
65+
inline void File::putAttribute(const std::string& name, const T& datum)
66+
{
67+
assert(impl_);
68+
FileBase::Type datumVar = datum;
69+
impl_->putAttribute(name, datumVar);
70+
}
71+
6472
template <typename T>
6573
inline void File::putAttribute(const std::string& name, const T* data,
6674
size_t size)

src/kg/include/kg/io/FileAdios2.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class FileAdios2 : public FileBase
3535
Dims shapeVariable(const std::string& name) const override;
3636

3737
void getAttribute(const std::string& name, TypePointer data) override;
38+
void putAttribute(const std::string& name, const Type& datum) override;
3839
void putAttribute(const std::string& name, TypeConstPointer data,
3940
size_t size) override;
4041
size_t sizeAttribute(const std::string& name) const override;
@@ -44,6 +45,7 @@ class FileAdios2 : public FileBase
4445
struct GetVariable;
4546
struct GetAttribute;
4647
struct PutAttribute;
48+
struct PutAttributeScalar;
4749

4850
template <typename T>
4951
void putVariable(const std::string& name, const T* data, Mode launch,
@@ -57,6 +59,9 @@ class FileAdios2 : public FileBase
5759
template <typename T>
5860
void getAttribute(const std::string& name, T* data);
5961

62+
template <typename T>
63+
void putAttribute(const std::string& name, const T& datum);
64+
6065
template <typename T>
6166
void putAttribute(const std::string& name, const T* data, size_t size);
6267

src/kg/include/kg/io/FileAdios2.inl

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ inline FileAdios2::FileAdios2(adios2::ADIOS& ad, const std::string& name,
2121
io_name_ = io_name;
2222
}
2323
io_ = ad.DeclareIO(io_name_);
24+
// io_.SetEngine("BP4");
2425
adios2::Mode adios2_mode;
2526
if (mode == Mode::Read) {
2627
adios2_mode = adios2::Mode::Read;
@@ -179,6 +180,21 @@ inline Dims FileAdios2::shapeVariable(const std::string& name) const
179180
std::abort();
180181
}
181182

183+
template <typename T>
184+
inline void FileAdios2::putAttribute(const std::string& name, const T& datum)
185+
{
186+
// if (mpiRank() != 0) { // FIXME, should we do this?
187+
// return;
188+
// }
189+
auto attr = io_.InquireAttribute<T>(name);
190+
if (attr) {
191+
mprintf("attr '%s' already exists -- ignoring it!\n", name.c_str());
192+
} else {
193+
// FIXME? we're never using "single value", just an array of size 1
194+
io_.DefineAttribute<T>(name, datum);
195+
}
196+
}
197+
182198
template <typename T>
183199
inline void FileAdios2::putAttribute(const std::string& name, const T* data,
184200
size_t size)
@@ -224,6 +240,27 @@ inline void FileAdios2::getAttribute(const std::string& name, TypePointer data)
224240
mpark::visit(GetAttribute{*this, name}, data);
225241
}
226242

243+
struct FileAdios2::PutAttributeScalar
244+
{
245+
PutAttributeScalar(FileAdios2& self, const std::string& name)
246+
: self{self}, name{name}
247+
{}
248+
249+
template <typename T>
250+
void operator()(const T& datum)
251+
{
252+
self.putAttribute(name, datum);
253+
}
254+
255+
FileAdios2& self;
256+
const std::string& name;
257+
};
258+
259+
inline void FileAdios2::putAttribute(const std::string& name, const Type& datum)
260+
{
261+
mpark::visit(PutAttributeScalar{*this, name}, datum);
262+
}
263+
227264
struct FileAdios2::PutAttribute
228265
{
229266
PutAttribute(FileAdios2& self, const std::string& name, size_t size)

src/kg/include/kg/io/FileBase.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ namespace io
1414
class FileBase
1515
{
1616
public:
17+
using Type = mpark::variant<int, unsigned int, unsigned long,
18+
unsigned long long, float, double, std::string>;
1719
using TypePointer =
1820
mpark::variant<int*, unsigned int*, unsigned long*, unsigned long long*,
1921
float*, double*, std::string*>;
@@ -41,6 +43,7 @@ class FileBase
4143
virtual Dims shapeVariable(const std::string& name) const = 0;
4244

4345
virtual void getAttribute(const std::string& name, TypePointer data) = 0;
46+
virtual void putAttribute(const std::string& name, const Type& datum) = 0;
4447
virtual void putAttribute(const std::string& name, TypeConstPointer data,
4548
size_t size) = 0;
4649
virtual size_t sizeAttribute(const std::string& name) const = 0;

src/kg/testing/io/TestIOAdios2.cxx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ TEST(IOAdios2, FilePutGetAttribute)
7676
file.beginStep(kg::io::StepMode::Append);
7777
auto dbl = std::vector<double>{1., 2., 3., 4., 5.};
7878
file.putAttribute("attr_dbl", dbl.data(), dbl.size());
79+
double val = 99.;
80+
file.putAttribute("attr_dbl_scalar", val);
7981
file.endStep();
8082
}
8183
{
@@ -84,7 +86,10 @@ TEST(IOAdios2, FilePutGetAttribute)
8486
auto size = file.sizeAttribute("attr_dbl");
8587
auto dbl = std::vector<double>(size);
8688
file.getAttribute("attr_dbl", dbl.data());
89+
double val;
90+
file.getAttribute("attr_dbl_scalar", &val);
8791
EXPECT_EQ(dbl, (std::vector<double>{1., 2., 3., 4., 5.}));
92+
EXPECT_EQ(val, 99.);
8893
file.endStep();
8994
}
9095
}

0 commit comments

Comments
 (0)