-
Notifications
You must be signed in to change notification settings - Fork 157
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Writing containers of bools failes to compile #490
Comments
If I'm not mistaken this is the limitation of Other |
I was able to get a My program is working for now, so this isn't a bit deal; however, it could be a small enhancement to HighFive if y'all are interested. /**
* this->set_ is the HighFive::DataSet that has already been created.
* i_file_ keeps track of entry of the DataSet that should be read/written the next time the buffer is filled.
*/
// writing
if constexpr (std::is_same_v<AtomicType, bool>) {
// handle bool specialization
std::vector<short> buff;
buff.reserve(buffer_.size());
for (const auto& v : buffer_) buff.push_back(v);
this->set_.select({i_file_}, {buff.size()}).write(buff);
} else {
this->set_.select({i_file_}, {buffer_.size()}).write(buffer_);
}
// reading
if constexpr (std::is_same_v<AtomicType,bool>) {
// get around std::vector<bool> specialization
std::vector<short> buff;
this->set_.select({i_file_}, {request_len}).read(buff);
buffer_.reserve(buff.size());
for (const auto& v : buff) buffer_.push_back(v);
} else {
this->set_.select({i_file_}, {request_len}).read(buffer_);
} https://github.com/LDMX-Software/fire/blob/8ac44b8e027eb3ec5888022663a8875b9b3fde6c/include/fire/h5/Writer.h#L117-L125 |
Thanks. It seems that you copy to a temporary right? I'm not against having some SFINAE to catch this case (rather in favour: it simplifies things for the user). |
Yes, that is the hacky solution I've used and it works fine for our purposes. The other solution that I've played around with is to implement a translation to a boolean enum that is what is stored in the file (this is what h5py does); however, this still doesn't resolve the issue of "surprising" the user with a difference between on-disk and in-memory data types. I think from a HighFive point of view, it may make sense to leave out this auto translation and just document how to handle boolean vectors in order to make sure the user knows what is happening with their data. |
Update I've found a better method which is still a mostly hacky solution, but it persists the type that the user intends. The basic idea is to copy the Again, here are code snippets showing the relevant translation steps during read/write. /**
* this->set_ is the HighFive::DataSet that has already been created.
* i_file_ keeps track of entry of the DataSet that should be read/written the next time the buffer is filled.
*/
// writing
if constexpr (std::is_same_v<AtomicType, bool>) {
// handle bool specialization
auto buff = std::make_unique<bool[]>(buffer_.size());
for (std::size_t i{0}; i < buffer_.size(); i++)
buff[i] = buffer_.at(i);
this->set_.select({i_file_}, {buff.size()})
.write(buff.get(), HighFive::AtomicType<bool>());
} else {
this->set_.select({i_file_}, {buffer_.size()}).write(buffer_);
}
// reading
if constexpr (std::is_same_v<AtomicType,bool>) {
// get around std::vector<bool> specialization
auto buff = std::make_unique<bool[]>(request_len);
this->set_.select({i_file_}, {request_len}).read(buff.get(),HighFive::AtomicType<bool>());
buffer_.reserve(request_len);
for (std::size_t i{0}; i < request_len; i++)
buffer_.push_back(buff[i]);
} else {
this->set_.select({i_file_}, {request_len}).read(buffer_);
} |
I see. This is indeed better I think as it writes When I was just searching, I found lots of complaints about |
My understanding is that each compiler is free to implement a memory-efficient specialization of All of my research basically points to using |
Should be fixed by #654 |
Describe the bug
Suppose I generate a data set of
bool
s that I store instd::vector<bool>
because the program does not know at compile time how many entries there will be. I want to save this buffer in the same way that I store other one-dimensional buffers into the output file.To Reproduce
I have written a small
main
file (copied in full below) based off of the simple create dataset example.g++ main.cxx -lhdf5
std::vector<bool>
which modifies howstd::vector::data
behaves.Expected behavior
A perfect solution for this issue would be a modification of the template deduction within HighFive to handle the specialization of vectros of bools. A satisfactory solution would be suggestions on how to accomplish my goal of writing a dataset of boolean values whose size is not known at compile time.
Desktop (please complete the following information):
I am doing my developments within a container. The full docker build context can be found here.
Additional context
The reason I want to do this is because I would like to develop a buffering mechanism for the type of data my program works with. My data is very highly chunked and so it is much more efficient to only make occasional
write
s even long after a specific piece of data is chosen to be stored into the output file. This buffering mechanism would need to handle all so-called "atomic" types.Program Code
Compile Error Printout
The text was updated successfully, but these errors were encountered: