#include <armadillo>
#include <h5cpp/all>
#include <chrono>
#include <functional>
#include <iostream>
auto t0 = clock::now();
body();
auto t1 = clock::now();
}
int main() {
auto fd =
h5::create(
"packet_batches.h5", H5F_ACC_TRUNC);
auto check = [](const char* label, bool ok) {
std::cout << (ok ?
"✔ ok " :
"✘ failed") <<
" " << label <<
"\n";
};
double t1_us;
{
h5::max_dims{H5S_UNLIMITED, ncols},
h5::chunk{1, ncols});
arma::mat M(nrows, ncols, arma::fill::zeros);
M(i, j) = static_cast<double>(j + i * 0.001);
t1_us = time_us([&]() {
h5::append(pt, M(i, j));
});
pt = h5::pt_t{};
check("variant 1: element-wise append shape (8 x 128k)",
back.size() == nrows * ncols);
}
double t2_us;
{
h5::max_dims{H5S_UNLIMITED, ncols},
h5::chunk{1, ncols});
arma::mat M(nrows, ncols);
M.row(i) = arma::randu<arma::rowvec>(ncols) + static_cast<double>(i);
t2_us = time_us([&]() {
arma::rowvec r = M.row(i);
const double* p = r.memptr();
}
});
pt = h5::pt_t{};
check("variant 2: row-wise append shape (8 x 128k)",
back.size() == nrows * ncols);
}
double t3_us;
{
h5::max_dims{H5S_UNLIMITED, ncols},
h5::chunk{1, ncols});
arma::mat M(nrows, ncols);
M.row(i) = arma::randu<arma::rowvec>(ncols) + static_cast<double>(i);
arma::mat T = M.t();
t3_us = time_us([&]() {
});
pt = h5::pt_t{};
check("variant 3: raw-pointer append shape (8 x 128k)",
back.size() == nrows * ncols);
}
std::cout <<
"\ntimings (microseconds, lower is better):\n"
<< " variant 1 element-wise : " << t1_us << "\n"
<< " variant 2 row-wise : " << t2_us << "\n"
<< " variant 3 raw-pointer : " << t3_us << "\n\n";
double t4_us;
{
h5::max_dims{H5S_UNLIMITED, height, width},
h5::chunk{1, height, width} | h5::gzip{6});
t4_us = time_us([&]() {
frame[y * width + x] =
static_cast<std::uint8_t>((x + y + f) & 0xff);
}
});
}
bool size_ok = movie.size() == nframes * frame_bytes;
bool first_ok = size_ok;
bool last_ok = size_ok;
first_ok = (movie[y * width + x] ==
static_cast<std::uint8_t>((x + y + 0) & 0xff));
const std::size_t last_off = (nframes - 1) * frame_bytes;
last_ok = (movie[last_off + y * width + x]
==
static_cast<std::uint8_t>((x + y + (nframes - 1)) & 0xff));
check("variant 4: movie clip 24 x 64 x 64 frames", size_ok && first_ok && last_ok);
std::cout <<
" variant 4 movie clip (write only): " << t4_us <<
" us\n";
return 0;
}
h5::at_t create(const hid_t &parent, const std::string &path, args_t &&... args)
Create a new attribute of element type T on a parent HDF5 object.
Definition H5Acreate.hpp:100
T aread(const hid_t &ds, const std::string &name, const h5::acpl_t &acpl=h5::default_acpl)
Read an attribute by name and return its value as type T.
Definition H5Aread.hpp:76