56 const std::string& dataset,
57 std::size_t expectedSamples,
62 H5E_auto2_t oldFunc =
nullptr;
63 void* oldData =
nullptr;
64 H5Eget_auto2(H5E_DEFAULT, &oldFunc, &oldData);
65 H5Eset_auto2(H5E_DEFAULT,
nullptr,
nullptr);
67 H5E_auto2_t f;
void* d;
68 ~ErrRestore() { H5Eset_auto2(H5E_DEFAULT, f, d); }
69 } errRestore{oldFunc, oldData};
73 hid_t file = H5Fopen(filename.c_str(), H5F_ACC_RDONLY, H5P_DEFAULT);
74 if (file < 0) { why =
"cannot open serially";
return false; }
76 hid_t dset = H5Dopen2(file, dataset.c_str(), H5P_DEFAULT);
77 if (dset < 0) { H5Fclose(file); why =
"no dataset " + dataset;
return false; }
80 haddr_t offset = HADDR_UNDEF;
82 hid_t dcpl = H5Dget_create_plist(dset);
83 if (dcpl < 0) { why =
"no create plist";
break; }
84 const H5D_layout_t layout = H5Pget_layout(dcpl);
85 const int nfilters = H5Pget_nfilters(dcpl);
87 if (layout != H5D_CONTIGUOUS) { why =
"not contiguous";
break; }
88 if (nfilters != 0) { why =
"filtered";
break; }
92 hid_t dtype = H5Dget_type(dset);
93 const htri_t sameType = H5Tequal(dtype, H5T_IEEE_F64LE);
95 if (sameType <= 0) { why =
"not little-endian float64";
break; }
97 if (H5Dget_storage_size(dset) != expectedSamples *
sizeof(
double)) {
98 why =
"storage size mismatch";
break;
101 offset = H5Dget_offset(dset);
102 if (offset == HADDR_UNDEF) { why =
"no file offset";
break; }
105 if (offset %
sizeof(
double) != 0) { why =
"offset not 8-byte aligned";
break; }
111 if (!ok)
return false;
113 const int fd = open(filename.c_str(), O_RDONLY);
114 if (fd < 0) { why =
"cannot open for mapping";
return false; }
116 const std::size_t pageSize =
static_cast<std::size_t
>(sysconf(_SC_PAGESIZE));
117 const off_t base =
static_cast<off_t
>((offset / pageSize) * pageSize);
118 const std::size_t delta =
static_cast<std::size_t
>(offset -
static_cast<haddr_t
>(base));
119 const std::size_t bytes = expectedSamples *
sizeof(double) + delta;
124 void* p = mmap(
nullptr, bytes, PROT_READ, MAP_SHARED, fd, base);
126 if (p == MAP_FAILED) { why =
"mmap failed";
return false; }
128 madvise(p, bytes, trackingAdvice);
133 data_m =
reinterpret_cast<const double*
>(
static_cast<const char*
>(p) + delta);
144 void* adviseBase =
nullptr;
145 std::size_t adviseBytes = 0;
149 const std::uintptr_t pageMask =
static_cast<std::uintptr_t
>(sysconf(_SC_PAGESIZE)) - 1;
150 const std::uintptr_t lo =
reinterpret_cast<std::uintptr_t
>(
data_m +
begin) & ~pageMask;
151 const std::uintptr_t hi =
reinterpret_cast<std::uintptr_t
>(
data_m +
end);
152 adviseBase =
reinterpret_cast<void*
>(lo);
153 adviseBytes = hi - lo;
154 madvise(adviseBase, adviseBytes, MADV_SEQUENTIAL);
159 constexpr std::size_t block = 4096;
161 for (std::size_t i =
begin; i <
end && !found; i += block) {
162 const std::size_t stop = std::min(i + block,
end);
163 for (std::size_t j = i; j < stop; ++j) {
164 found |= (
data_m[j] != 0.0);
168 if (adviseBase !=
nullptr) {
169 madvise(adviseBase, adviseBytes, trackingAdvice);