Commit 41bf10ab authored by Alejandro Homs Puron's avatar Alejandro Homs Puron
Browse files

[PROC][FAI] Use HDF5 wrapper to read the kernel params from files

parent b81adab6
......@@ -16,7 +16,7 @@
#include <boost/compute/command_queue.hpp>
#include <boost/compute/algorithm/transform.hpp>
#include <hdf5.h>
#include <lima/io/h5/wrapper.hpp>
#include <lima/exceptions.hpp>
......@@ -77,60 +77,33 @@ namespace processing::fai
namespace detail
{
// clang-format off
template <typename T> struct deduct;
template <> struct deduct<unsigned char> { static const hid_t type() { return H5T_NATIVE_UINT8; } };
template <> struct deduct<char> { static const hid_t type() { return H5T_NATIVE_INT8; } };
template <> struct deduct<unsigned short> { static const hid_t type() { return H5T_NATIVE_UINT16; } };
template <> struct deduct<short> { static const hid_t type() { return H5T_NATIVE_INT16; } };
template <> struct deduct<unsigned int> { static const hid_t type() { return H5T_NATIVE_UINT32; } };
template <> struct deduct<int> { static const hid_t type() { return H5T_NATIVE_INT32; } };
template <> struct deduct<unsigned long long> { static const hid_t type() { return H5T_NATIVE_UINT64; } };
template <> struct deduct<long long> { static const hid_t type() { return H5T_NATIVE_INT64; } };
template <> struct deduct<float> { static const hid_t type() { return H5T_NATIVE_FLOAT; } };
template <> struct deduct<double> { static const hid_t type() { return H5T_NATIVE_DOUBLE; } };
template <> struct deduct<bool> { static const hid_t type() { return H5T_NATIVE_HBOOL; } };
// clang-format on
/// Read an HDF5 data into an OpenCL buffer
template <typename T>
void read_h5_dset(std::filesystem::path const& h5_path, vector<T>& out, std::string dataset_path,
boost::compute::command_queue& queue = boost::compute::system::default_queue())
{
herr_t status;
namespace h5 = lima::io::h5;
// Open an existing file
hid_t file_id = H5Fopen(h5_path.c_str(), H5F_ACC_RDONLY, H5P_DEFAULT);
if (file_id < 0)
LIMA_THROW_EXCEPTION(lima::hdf5_error("Failed to open HDF5 file") << boost::errinfo_file_name(h5_path));
auto file = h5::file::open(h5_path);
// Open an existing dataset
hid_t dataset_id = H5Dopen2(file_id, dataset_path.c_str(), H5P_DEFAULT);
if (dataset_id < 0)
LIMA_THROW_EXCEPTION(lima::hdf5_error("Failed to open HDF5 dataset"));
auto dataset = h5::dataset::open(file, dataset_path);
// Check the dimensions
hid_t dataspace_id = H5Dget_space(dataset_id);
const int ndims = H5Sget_simple_extent_ndims(dataspace_id);
auto dataspace = dataset.space();
const int ndims = dataspace.ndims();
hsize_t dims[ndims];
H5Sget_simple_extent_dims(dataspace_id, dims, NULL);
dataspace.dims(dims, nullptr);
hsize_t size = std::accumulate(dims, dims + ndims, 1, [](hsize_t lhs, hsize_t rhs) { return lhs * rhs; });
out.resize(size);
// Read the dataset
void* ptr = queue.enqueue_map_buffer(out.get_buffer(), CL_MAP_WRITE, 0, out.size());
status = H5Dread(dataset_id, deduct<T>::type(), H5S_ALL, H5S_ALL, H5P_DEFAULT, ptr);
auto dtype = h5::predef_datatype::create<T>();
dataset.read(ptr, dtype);
queue.enqueue_unmap_buffer(out.get_buffer(), ptr);
// Close the dataspace
status = H5Sclose(dataspace_id);
// Close the dataset
status = H5Dclose(dataset_id);
// Close the file
status = H5Fclose(file_id);
}
} // namespace detail
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment