Skip to content

Commit 6d6acea

Browse files
authored
Merge pull request #406 from germasch/pr/io-adios2-scalar
support writing of ADIOS2 scalars
2 parents bbbbff5 + ad54bae commit 6d6acea

7 files changed

Lines changed: 74 additions & 46 deletions

File tree

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

Lines changed: 8 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,9 @@ inline Engine::Engine(File&& file, MPI_Comm comm) : file_{std::move(file)}
1515
MPI_Comm_size(comm, &mpi_size_);
1616
}
1717

18-
inline void Engine::beginStep(StepMode mode)
19-
{
20-
file_.beginStep(mode);
21-
}
18+
inline void Engine::beginStep(StepMode mode) { file_.beginStep(mode); }
2219

23-
inline void Engine::endStep()
24-
{
25-
file_.endStep();
26-
}
20+
inline void Engine::endStep() { file_.endStep(); }
2721

2822
// ----------------------------------------------------------------------
2923
// put
@@ -78,18 +72,12 @@ inline void Engine::get(const std::string& pfx, T& datum, Args&&... args)
7872
// ----------------------------------------------------------------------
7973
// performPuts
8074

81-
inline void Engine::performPuts()
82-
{
83-
file_.performPuts();
84-
}
75+
inline void Engine::performPuts() { file_.performPuts(); }
8576

8677
// ----------------------------------------------------------------------
8778
// performGets
8879

89-
inline void Engine::performGets()
90-
{
91-
file_.performGets();
92-
}
80+
inline void Engine::performGets() { file_.performGets(); }
9381

9482
// ----------------------------------------------------------------------
9583
// internal
@@ -105,7 +93,7 @@ inline void Engine::putVariable(const T* data, const Mode launch,
10593
template <typename T>
10694
inline void Engine::putAttribute(const T& datum)
10795
{
108-
file_.putAttribute(prefix(), &datum, 1);
96+
file_.putAttribute(prefix(), datum);
10997
}
11098

11199
template <typename T>
@@ -150,20 +138,11 @@ inline Dims Engine::variableShape()
150138
// ----------------------------------------------------------------------
151139
// close
152140

153-
inline void Engine::close()
154-
{
155-
file_.close();
156-
}
141+
inline void Engine::close() { file_.close(); }
157142

158-
inline int Engine::mpiRank() const
159-
{
160-
return mpi_rank_;
161-
}
143+
inline int Engine::mpiRank() const { return mpi_rank_; }
162144

163-
inline int Engine::mpiSize() const
164-
{
165-
return mpi_size_;
166-
}
145+
inline int Engine::mpiSize() const { return mpi_size_; }
167146

168147
} // namespace io
169148
} // namespace kg

‎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: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,13 @@ namespace io
66

77
inline File::File(FileBase* impl) : impl_{impl} {}
88

9-
inline File::~File()
10-
{
11-
close();
12-
}
9+
inline File::~File() { close(); }
1310

14-
inline void File::close()
15-
{
16-
impl_.reset();
17-
}
11+
inline void File::close() { impl_.reset(); }
1812

19-
inline void File::beginStep(StepMode mode)
20-
{
21-
impl_->beginStep(mode);
22-
}
13+
inline void File::beginStep(StepMode mode) { impl_->beginStep(mode); }
2314

24-
inline void File::endStep()
25-
{
26-
impl_->endStep();
27-
}
15+
inline void File::endStep() { impl_->endStep(); }
2816

2917
inline void File::performPuts()
3018
{
@@ -73,6 +61,14 @@ inline void File::getAttribute(const std::string& name, T* data)
7361
impl_->getAttribute(name, dataVar);
7462
}
7563

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+
7672
template <typename T>
7773
inline void File::putAttribute(const std::string& name, const T* data,
7874
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: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ TEST(IOAdios2, CtorDtor) { auto io = kg::io::IOAdios2{}; }
88
TEST(IOAdios2, OpenWrite)
99
{
1010
auto io = kg::io::IOAdios2{};
11-
auto file = io.openFile("test1", kg::io::Mode::Write);
11+
auto file = io.openFile("test1.bp", kg::io::Mode::Write);
1212
}
1313

1414
TEST(IOAdios2, OpenReadMissingFile)
@@ -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)