h5cpp icon indicating copy to clipboard operation
h5cpp copied to clipboard

How to add custom drivers non-intrusively?

Open FlyingSamson opened this issue 1 year ago • 1 comments

For the project I'm currently working on, I need to write a custom driver.

Inheriting from hdf5::file::Driver works per se, but to overload the pure virtual function id() I need to return a hdf5::file::DriverID, which currently is limited to Posix, Direct, Memory, and MPI.

I could not find any reference to hdf5::file::DriverID other than the once in the existing drivers id() functions, so I'm not sure what the intended use id() is.

Would it be feasible to add another choice (e.g., Custom) to the hdf5::file::DriverID enum?

Alternatively, if the ID should remain unique even when multiple custom drivers are to be used, one might also consider some form of registration, that hands out unique integer ids for each driver. In any case I believe driver should be implemented as an open set, since hdf5 itself implements it that way. The implementation using the enum class makes this difficult, as it inherently makes it a closed set.

FlyingSamson avatar May 22 '24 09:05 FlyingSamson

Yes, it would be nice to support H5Pset_driver and H5Pget_driver . I would think about a solution which is backward compatible

jkotan avatar May 23 '24 13:05 jkotan

Hello @FlyingSamson,

I've just created PR which adds DriverID::Custom=0 (https://github.com/ess-dmsc/h5cpp/pull/665) Does it solve your issue? Do you have any simple example for your use-case we could add to our tests?

jkotan avatar Jan 20 '25 13:01 jkotan

Yes, that solves my issue.

For an (currently) internal project I'm developing a Virtual File Driver for hdf5, which I would like to use with h5cpp.

I don't think that this offers much testing potential, but maybe one could add an example.

What I currently do to add the VFD to h5cpp is (merged into one header instead of definitions in a separate CU for simplicity):

class MyDriver : public hdf5::file::Driver {
 public:
  MyDriver(<some type> parameters_for_file_access) : _parameters_for_file_access{parameters_for_file_access} {}
  MyDriver(const MyDriver&) = default;

  virtual void operator()(const hdf5::property::FileAccessList& fapl) const override {
    if (H5Pset_fapl_my_vfd(static_cast<hid_t>(fapl), _parameters_for_file_access) < 0) {
      hdf5::error::Singleton::instance().throw_with_stack("Failure setting my_vfd driver!");
    }
  }

  [[nodiscard]] virtual hdf5::file::DriverID id() const noexcept override  {
    return hdf5::file::DriverID::Custom;
  }

 private:
  <some type> _parameters_for_file_access;
};

H5Pset_fapl_my_vfd basically follows the tutorials by the HDF5 group and the hdf5 internal implementations of the existing VFDs.

And then I call on a hdf5::property::FileAccessList object

_fapl.driver(MyDriver());

and use that _fapl in a hdf5::file::open call.

FlyingSamson avatar Jan 21 '25 12:01 FlyingSamson