Commit 46957c37 authored by gbalduzz's avatar gbalduzz
Browse files

Allow HDF5 datasets to be overwritten. Writer cleanup.

parent 51d00850
Loading
Loading
Loading
Loading
+68 −17
Original line number Diff line number Diff line
@@ -96,6 +96,12 @@ public:
  template <typename scalar_type>
  bool execute(std::string name, dca::linalg::Matrix<scalar_type, dca::linalg::CPU>& A);

  template <typename scalar_type>
  bool execute(std::string name, dca::linalg::Matrix<std::complex<scalar_type>, dca::linalg::CPU>& A);

  template <typename scalar_type>
  bool execute(dca::linalg::Matrix<scalar_type, dca::linalg::CPU>& A);

  bool execute(std::string name, io::Buffer& buff) {
    return execute(name, static_cast<io::Buffer::Container&>(buff));
  }
@@ -215,11 +221,10 @@ bool HDF5Reader::execute(std::string name, func::function<scalartype, domain_typ

template <typename scalar_type>
bool HDF5Reader::execute(std::string name, dca::linalg::Vector<scalar_type, dca::linalg::CPU>& V) {
  open_group(name);
  bool success = true;

  try {
    std::string full_name = get_path() + "/data";
    std::string full_name = get_path() + "/" + name;

    H5::DataSet dataset = my_file->openDataSet(full_name.c_str());

@@ -229,24 +234,24 @@ bool HDF5Reader::execute(std::string name, dca::linalg::Vector<scalar_type, dca:

    H5Dread(dataset.getId(), HDF5_TYPE<scalar_type>::get(), dataspace.getId(), H5S_ALL, H5P_DEFAULT,
            &V[0]);

    V.set_name(name);
  }
  catch (const H5::FileIException& err) {
    std::cout << "\n\n\t the vector (" + name + ") does not exist in path : " + get_path() + "\n\n";
    success = false;
  }

  close_group();
  return success;
}

template <typename scalar_type>
bool HDF5Reader::execute(std::string name,
                         dca::linalg::Vector<std::complex<scalar_type>, dca::linalg::CPU>& V) {
  open_group(name);
  bool success = true;

  try {
    std::string full_name = get_path() + "/data";
    std::string full_name = get_path() + "/" + name;

    H5::DataSet dataset = my_file->openDataSet(full_name.c_str());

@@ -256,37 +261,79 @@ bool HDF5Reader::execute(std::string name,

    H5Dread(dataset.getId(), HDF5_TYPE<scalar_type>::get(), dataspace.getId(), H5S_ALL, H5P_DEFAULT,
            &V[0]);

    V.set_name(name);
  }
  catch (const H5::FileIException& err) {
    std::cout << "\n\n\t the vector (" + name + ") does not exist in path : " + get_path() + "\n\n";
    success = false;
  }

  close_group();
  return success;
}

template <typename scalar_type>
bool HDF5Reader::execute(std::string name, dca::linalg::Matrix<scalar_type, dca::linalg::CPU>& A) {
  open_group(name);
  bool success = true;

  try {
    std::string full_name = get_path() + "/data";
    std::string full_name = get_path() + "/" + name;

    H5::DataSet dataset = my_file->openDataSet(full_name.c_str());
    H5::DataSpace dataspace = dataset.getSpace();

    std::array<hsize_t, 2> dims;
    dataspace.getSimpleExtentDims(dims.data(), nullptr);

    std::vector<scalar_type> linearized(dims[0] * dims[1]);
    dataset.read(linearized.data(), HDF5_TYPE<scalar_type>::get_PredType(), dataspace);

    // HDF5 is column major, while Matrix is not major.
    A.resizeNoCopy(std::make_pair(dims[0], dims[1]));
    unsigned linindex = 0;
    for (int i = 0; i < A.nrRows(); ++i) {
      for (int j = 0; j < A.nrCols(); ++j)
        A(i, j) = linearized[linindex++];
    }

    A.set_name(name);
  }
  catch (const H5::FileIException& err) {
    std::cout << "\n\n\t the function (" + name + ") does not exist in path : " + get_path() +
                     "\n\n";
    success = false;
  }

  return success;
}

template <typename scalar_type>
bool HDF5Reader::execute(std::string name,
                         dca::linalg::Matrix<std::complex<scalar_type>, dca::linalg::CPU>& A) {
  bool success = true;

  try {
    std::string full_name = get_path() + "/" + name;

    H5::DataSet dataset = my_file->openDataSet(full_name.c_str());
    H5::DataSpace dataspace = dataset.getSpace();

    // These 2 lines fix the bug of reading into a matrix which has been resized to a smaller size
    // hsize_t global_size[2] = {A.nrCols(), A.nrRows()}; // HDF5 use row
    // major data distribution
    // hsize_t global_size[2] = {A.capacity().second, A.capacity().first}; // HDF5 use
    // row major data distribution
    // dataspace.setExtentSimple(2, &global_size[0], NULL);
    std::array<hsize_t, 3> dims;
    dataspace.getSimpleExtentDims(dims.data(), nullptr);

    H5Dread(dataset.getId(), HDF5_TYPE<scalar_type>::get(), dataspace.getId(), H5S_ALL, H5P_DEFAULT,
            &A(0, 0));
    A.resizeNoCopy(std::make_pair(dims[1], dims[2]));
    std::vector<std::complex<scalar_type>> linearized(A.nrRows() * A.nrCols());

    dataset.read(linearized.data(), HDF5_TYPE<scalar_type>::get_PredType(), dataspace);

    // HDF5 is column major, while Matrix is not major.
    unsigned linindex = 0;
    for (int i = 0; i < A.nrRows(); ++i)
      for (int j = 0; j < A.nrCols(); ++j) {
        A(i, j) = linearized[linindex++];
      }

    A.set_name(name);
  }
  catch (const H5::FileIException& err) {
    std::cout << "\n\n\t the function (" + name + ") does not exist in path : " + get_path() +
@@ -294,10 +341,14 @@ bool HDF5Reader::execute(std::string name, dca::linalg::Matrix<scalar_type, dca:
    success = false;
  }

  close_group();
  return success;
}

template <typename scalar_type>
bool HDF5Reader::execute(dca::linalg::Matrix<scalar_type, dca::linalg::CPU>& A) {
  return execute(A.get_name(), A);
}

}  // namespace io
}  // namespace dca

+154 −368
Original line number Diff line number Diff line
@@ -6,6 +6,7 @@
// See CITATION.md for citation guidelines, if DCA++ is used for scientific publications.
//
// Author: Peter Staar (taa@zurich.ibm.com)
//         Giovanni Balduzzi (gbalduzz@itp.phys.ethz.ch)
//
// HDF5 writer.

@@ -14,6 +15,7 @@

#include <complex>
#include <memory>
#include <mutex>
#include <sstream>
#include <string>
#include <vector>
@@ -37,15 +39,14 @@ public:

public:
  // In: verbose. If true, the writer outputs a short log whenever it is executed.
  HDF5Writer(bool verbose = true)
      : my_file(NULL), file_id(-1), my_group(0), my_paths(0), verbose_(verbose) {}
  HDF5Writer(bool verbose = true) : file_id(-1), my_paths(0), verbose_(verbose) {}

  ~HDF5Writer();

  bool is_reader() {
  constexpr bool is_reader() {
    return false;
  }
  bool is_writer() {
  constexpr bool is_writer() {
    return true;
  }

@@ -57,6 +58,11 @@ public:

  std::string get_path();

  // Closes all open datasets. Overwriting them will no longer be possible.
  void flush() {
    datasets_.clear();
  }

  template <typename arbitrary_struct_t>
  static void to_file(const arbitrary_struct_t& arbitrary_struct, const std::string& file_name);

@@ -76,8 +82,11 @@ public:

  void execute(const std::string& name, const std::vector<std::string>& value);

  template <typename scalar_type>
  void execute(const std::string& name, const std::vector<std::vector<scalar_type>>& value);
  template <typename Scalar, std::size_t n>
  void execute(const std::string& name, const std::vector<std::array<Scalar, n>>& value);

  template <typename Scalar>
  void execute(const std::string& name, const std::vector<std::vector<Scalar>>& value);

  template <typename domain_type>
  void execute(const std::string& name, const func::dmn_0<domain_type>& dmn);
@@ -109,6 +118,11 @@ public:
  void execute(const std::string& name,
               const dca::linalg::Matrix<std::complex<scalar_type>, dca::linalg::CPU>& A);

  template <typename scalar_type>
  void execute(const dca::linalg::Matrix<scalar_type, dca::linalg::CPU>& A) {
    execute(A.get_name(), A);
  }

  template <class T>
  void execute(const std::string& name, const std::unique_ptr<T>& obj);

@@ -119,17 +133,41 @@ public:
    return execute(name, static_cast<io::Buffer::Container>(buffer));
  }

  bool groupExists(const std::string& path) const;

  // Returns -1 if datasets does not exist.
  std::array<hsize_t, 2> datasetId(const std::string& path);

  operator bool() const {
    return static_cast<bool>(file_);
  }

  void lock() {
    mutex_.lock();
  }

  void unlock() {
    mutex_.unlock();
  }

private:
  bool fexists(const char* filename);

  H5::H5File* my_file;
  void write(const std::string& name, const std::vector<hsize_t>& size, H5::PredType type,
             const void* data);

  std::unique_ptr<H5::H5File> file_;

  hid_t file_id;

  std::vector<H5::Group*> my_group;
  std::unordered_map<std::string, std::unique_ptr<H5::Group>> groups_;
  std::unordered_map<std::string, std::pair<std::unique_ptr<H5::DataSet>, std::unique_ptr<H5::DataSpace>>>
      datasets_;
  std::vector<std::string> my_paths;

  bool verbose_;

  std::mutex mutex_;
};

template <typename arbitrary_struct_t>
@@ -142,54 +180,18 @@ void HDF5Writer::to_file(const arbitrary_struct_t& arbitrary_struct, const std::

template <typename scalar_type>
void HDF5Writer::execute(const std::string& name, scalar_type value) {
  H5::H5File& file = (*my_file);
  std::string path = get_path();

  hsize_t dims[1];

  H5::DataSet* dataset = NULL;
  H5::DataSpace* dataspace = NULL;

  {
    dims[0] = 1;
    dataspace = new H5::DataSpace(1, dims);
  const std::string full_name = get_path() + "/" + name;
  std::vector<hsize_t> dims{1};

    std::string full_name = path + "/" + name;
    dataset = new H5::DataSet(
        file.createDataSet(full_name.c_str(), HDF5_TYPE<scalar_type>::get_PredType(), *dataspace));

    H5Dwrite(dataset->getId(), HDF5_TYPE<scalar_type>::get(), dataspace->getId(), H5S_ALL,
             H5P_DEFAULT, &value);
  }

  delete dataset;
  delete dataspace;
  write(full_name, dims, HDF5_TYPE<scalar_type>::get_PredType(), &value);
}

template <typename scalar_type>
void HDF5Writer::execute(const std::string& name, const std::pair<scalar_type, scalar_type>& value) {
  H5::H5File& file = (*my_file);
  std::string path = get_path();
  std::string full_name = get_path() + "/" + name;
  std::vector<hsize_t> dims{2};

  hsize_t dims[1];

  H5::DataSet* dataset = NULL;
  H5::DataSpace* dataspace = NULL;

  {
    dims[0] = 2;
    dataspace = new H5::DataSpace(1, dims);

    std::string full_name = path + "/" + name;
    dataset = new H5::DataSet(
        file.createDataSet(full_name.c_str(), HDF5_TYPE<scalar_type>::get_PredType(), *dataspace));

    H5Dwrite(dataset->getId(), HDF5_TYPE<scalar_type>::get(), dataspace->getId(), H5S_ALL,
             H5P_DEFAULT, &(value.first));
  }

  delete dataset;
  delete dataspace;
  write(full_name, dims, HDF5_TYPE<scalar_type>::get_PredType(), &value.first);
}

template <typename scalar_type>
@@ -197,150 +199,90 @@ void HDF5Writer::execute(const std::string& name,
                         const std::vector<scalar_type>& value)  //, H5File& file, std::string path)
{
  if (value.size() > 0) {
    H5::H5File& file = (*my_file);
    std::string path = get_path();

    hsize_t dims[1];

    H5::DataSet* dataset = NULL;
    H5::DataSpace* dataspace = NULL;

    {
      dims[0] = value.size();
      dataspace = new H5::DataSpace(1, dims);

      std::string full_name = path + "/" + name;
      dataset = new H5::DataSet(
          file.createDataSet(full_name.c_str(), HDF5_TYPE<scalar_type>::get_PredType(), *dataspace));

      H5Dwrite(dataset->getId(), HDF5_TYPE<scalar_type>::get(), dataspace->getId(), H5S_ALL,
               H5P_DEFAULT, &value[0]);
    }

    delete dataset;
    delete dataspace;
    std::string full_name = get_path() + "/" + name;
    std::vector<hsize_t> dims{value.size()};
    write(full_name, dims, HDF5_TYPE<scalar_type>::get_PredType(), value.data());
  }
}

template <typename scalar_type>
void HDF5Writer::execute(const std::string& name,
                         const std::vector<std::complex<scalar_type>>& value) {
  H5::H5File& file = (*my_file);
  std::string path = get_path();

  hsize_t dims[2];

  H5::DataSet* dataset = NULL;
  H5::DataSpace* dataspace = NULL;

  {
    dims[0] = 2;
    dims[1] = value.size();
    dataspace = new H5::DataSpace(2, dims);

    std::string full_name = path + "/" + name;
    dataset = new H5::DataSet(
        file.createDataSet(full_name.c_str(), HDF5_TYPE<scalar_type>::get_PredType(), *dataspace));

    H5Dwrite(dataset->getId(), HDF5_TYPE<scalar_type>::get(), dataspace->getId(), H5S_ALL,
             H5P_DEFAULT, &value[0]);
  if (value.size() > 0) {
    std::string full_name = get_path() + "/" + name;
    std::vector<hsize_t> dims{2, value.size()};
    write(full_name, dims, HDF5_TYPE<scalar_type>::get_PredType(), value.data());
  }

  delete dataset;
  delete dataspace;
}

template <typename scalar_type>
void HDF5Writer::execute(const std::string& name, const std::vector<std::vector<scalar_type>>& value) {
  if (value.size() > 0) {
    H5::H5File& file = (*my_file);
    std::string full_name = get_path() + "/" + name;

    bool all_the_same_size = true;

    std::vector<size_t> dim(2, 0);
    {
      dim[0] = value.size();
      dim[1] = value[0].size();

      for (size_t l = 0; l < dim[0]; l++)
        if (dim[1] != value[l].size())
    const std::size_t cols = value[0].size();
    for (auto& v : value) {
      if (v.size() != cols) {
        all_the_same_size = false;
        break;
      }
    }

    open_group(name);
    if (all_the_same_size) {
      std::vector<hsize_t> dims{value.size(), cols};
      std::vector<scalar_type> linearized(dims[0] * dims[1]);

    execute("equal-size", all_the_same_size);
      std::size_t linindex = 0;
      for (std::size_t i = 0; i < value.size(); ++i)
        for (std::size_t j = 0; j < cols; ++j, ++linindex)
          linearized[linindex] = value[i][j];

    H5::DataSet* dataset = NULL;
    H5::DataSpace* dataspace = NULL;
      write(full_name, dims, HDF5_TYPE<scalar_type>::get_PredType(), value.data());
    }
    else {
      open_group(full_name);
      std::vector<hsize_t> dims(1);
      for (std::size_t i = 0; i < value.size(); ++i) {
        const std::string new_name = full_name + "/row_" + std::to_string(i);
        dims[0] = value[i].size();
        write(new_name, dims, HDF5_TYPE<scalar_type>::get_PredType(), value[i].data());
      }
      close_group();
    }
  }
}

    if (all_the_same_size) {
      execute("size", dim);
template <typename Scalar, std::size_t n>
void HDF5Writer::execute(const std::string& name, const std::vector<std::array<Scalar, n>>& value) {
  if (value.size() == 0)
    return;

      hsize_t dims[2];
  std::array<hsize_t, 2> dims{value.size(), n};

      {
        dims[0] = value.size();
        dims[1] = value[0].size();
  H5::DataSet* dataset = nullptr;
  H5::DataSpace* dataspace = nullptr;

        dataspace = new H5::DataSpace(2, dims);
  std::string full_name = get_path() + "/" + name;

        std::string full_name = get_path() + "/data";
        dataset = new H5::DataSet(file.createDataSet(
            full_name.c_str(), HDF5_TYPE<scalar_type>::get_PredType(), *dataspace));
  dataspace = new H5::DataSpace(2, dims.data());

        scalar_type* tmp = new scalar_type[dims[0] * dims[1]];
  dataset = new H5::DataSet(
      file_->createDataSet(full_name.c_str(), HDF5_TYPE<Scalar>::get_PredType(), *dataspace));

        /*
          for(int i=0; i<dims[0]; i++)
          for(int j=0; j<dims[1]; j++)
          tmp[i+j*dims[0]] = value[i][j];
        */
  Scalar* tmp = new Scalar[dims[0] * dims[1]];

  // hdf5 has row-major ordering!
        for (hsize_t i = 0; i < dims[0]; i++)
          for (hsize_t j = 0; j < dims[1]; j++)
            tmp[i * dims[1] + j] = value[i][j];
  //  for (hsize_t i = 0; i < dims[0]; i++)
  //    for (hsize_t j = 0; j < dims[1]; j++)
  //      tmp[i * dims[1] + j] = value[i][j];

        H5Dwrite(dataset->getId(), HDF5_TYPE<scalar_type>::get(), dataspace->getId(), H5S_ALL,
                 H5P_DEFAULT, tmp);
  H5Dwrite(dataset->getId(), HDF5_TYPE<Scalar>::get(), dataspace->getId(), H5S_ALL, H5P_DEFAULT,
           value[0].data());

  delete[] tmp;
      }
    }
    else {
      execute("size_i", dim[0]);

      dim.resize(0);
      for (size_t l = 0; l < value.size(); l++)
        dim.push_back(value[l].size());

      execute("size_j", dim);

      hsize_t dims[1];

      open_group("data");
      for (size_t l = 0; l < value.size(); l++) {
        dims[0] = value[l].size();
        dataspace = new H5::DataSpace(1, dims);

        std::stringstream ss;
        ss << get_path() << "/" << l;

        dataset = new H5::DataSet(file.createDataSet(
            ss.str().c_str(), HDF5_TYPE<scalar_type>::get_PredType(), *dataspace));

        H5Dwrite(dataset->getId(), HDF5_TYPE<scalar_type>::get(), dataspace->getId(), H5S_ALL,
                 H5P_DEFAULT, &value[l]);
      }
      close_group();
    }

  delete dataset;
  delete dataspace;

    close_group();
  }
}

template <typename domain_type>
@@ -380,54 +322,27 @@ void HDF5Writer::execute(const std::string& name, const func::function<scalar_ty
  if (f.size() == 0)
    return;

  H5::H5File& file = (*my_file);

  open_group(name);

  std::string new_path = get_path();

  {
  execute("name", f.get_name());

    {
      std::vector<int> vec(0);
  std::vector<hsize_t> dims(0);

      for (int l = 0; l < f.signature(); l++)
        vec.push_back(f[l]);
  int n_dims = f.signature();
  for (int l = 0; l < n_dims; l++)
    dims.push_back(f[l]);

      execute("domain-sizes", vec);
    }

    {
      int N_dmns = f.signature();

      std::vector<hsize_t> dims(N_dmns);  // hsize_t dims[N_dmns];

      //      for(int l=0; l<N_dmns; l++)
      //        dims[l] = f[l];
  execute("domain-sizes", dims);

  // be carefull --> HDF5 is by default row-major, while the function-class is column-major !
      for (int l = 0; l < N_dmns; l++)
        dims[N_dmns - 1 - l] = f[l];

      H5::DataSet* dataset = NULL;
      H5::DataSpace* dataspace = NULL;

      {
        dataspace = new H5::DataSpace(N_dmns, &dims[0]);
  for (int l = 0; l < n_dims; l++)
    dims[n_dims - 1 - l] = f[l];

  std::string full_name = new_path + "/data";
        dataset = new H5::DataSet(file.createDataSet(
            full_name.c_str(), HDF5_TYPE<scalar_type>::get_PredType(), *dataspace));

        H5Dwrite(dataset->getId(), HDF5_TYPE<scalar_type>::get(), dataspace->getId(), H5S_ALL,
                 H5P_DEFAULT, &f(0));
      }

      delete dataset;
      delete dataspace;
    }
  }
  write(full_name, dims, HDF5_TYPE<scalar_type>::get_PredType(), f.values());

  close_group();
}
@@ -438,50 +353,29 @@ void HDF5Writer::execute(const std::string& name,
  if (f.size() == 0)
    return;

  H5::H5File& file = (*my_file);

  open_group(name);

  {
    execute("name", f.get_name());

    {
      std::vector<int> vec(0);
  std::string new_path = get_path();

      for (int l = 0; l < f.signature(); l++)
        vec.push_back(f[l]);
  execute("name", f.get_name());

      execute("domain-sizes", vec);
    }
  std::vector<hsize_t> dims(0);

    {
      int N_dmns = f.signature();
  int n_dims = f.signature();
  for (int l = 0; l < n_dims; l++)
    dims.push_back(f[l]);

      std::vector<hsize_t> dims(N_dmns + 1);
  execute("domain-sizes", dims);

  dims.resize(n_dims + 1);
  // be carefull --> HDF5 is by default row-major, while the function-class is column-major !
      dims[N_dmns] = 2;
      for (int l = 0; l < N_dmns; l++)
        dims[N_dmns - 1 - l] = f[l];

      H5::DataSet* dataset = NULL;
      H5::DataSpace* dataspace = NULL;
  for (int l = 0; l < n_dims; l++)
    dims[n_dims - l] = f[l];

      {
        dataspace = new H5::DataSpace(N_dmns + 1, &dims[0]);
  dims[0] = 2;

  std::string full_name = get_path() + "/data";
        dataset = new H5::DataSet(file.createDataSet(
            full_name.c_str(), HDF5_TYPE<scalar_type>::get_PredType(), *dataspace));

        H5Dwrite(dataset->getId(), HDF5_TYPE<scalar_type>::get(), dataspace->getId(), H5S_ALL,
                 H5P_DEFAULT, &f(0));
      }

      delete dataset;
      delete dataspace;
    }
  }
  write(full_name, dims, HDF5_TYPE<scalar_type>::get_PredType(), f.values());

  close_group();
}
@@ -489,156 +383,48 @@ void HDF5Writer::execute(const std::string& name,
template <typename scalar_type>
void HDF5Writer::execute(const std::string& name,
                         const dca::linalg::Vector<scalar_type, dca::linalg::CPU>& V) {
  H5::H5File& file = (*my_file);

  open_group(name);

  execute("name", V.get_name());
  execute("size", V.get_size());

  hsize_t dims[1];

  dims[0] = V.size();

  H5::DataSet* dataset = NULL;
  H5::DataSpace* dataspace = NULL;

  {
    dataspace = new H5::DataSpace(1, dims);

    std::string full_name = get_path() + "/data";
    dataset = new H5::DataSet(
        file.createDataSet(full_name.c_str(), HDF5_TYPE<scalar_type>::get_PredType(), *dataspace));

    H5Dwrite(dataset->getId(), HDF5_TYPE<scalar_type>::get(), dataspace->getId(), H5S_ALL,
             H5P_DEFAULT, &V[0]);
  }

  delete dataset;
  delete dataspace;

  close_group();
  std::string full_name = get_path() + "/" + name;
  write(full_name, std::vector<hsize_t>{V.size()}, HDF5_TYPE<scalar_type>::get_PredType(), V.ptr());
}

template <typename scalar_type>
void HDF5Writer::execute(const std::string& name,
                         const dca::linalg::Vector<std::complex<scalar_type>, dca::linalg::CPU>& V) {
  H5::H5File& file = (*my_file);

  open_group(name);

  execute("name", V.get_name());
  execute("size", V.size());

  hsize_t dims[2];

  dims[0] = 2;
  dims[1] = V.size();

  H5::DataSet* dataset = NULL;
  H5::DataSpace* dataspace = NULL;

  {
    dataspace = new H5::DataSpace(2, dims);

    std::string full_name = get_path() + "/data";
    dataset = new H5::DataSet(
        file.createDataSet(full_name.c_str(), HDF5_TYPE<scalar_type>::get_PredType(), *dataspace));

    H5Dwrite(dataset->getId(), HDF5_TYPE<scalar_type>::get(), dataspace->getId(), H5S_ALL,
             H5P_DEFAULT, &V[0]);
  }

  delete dataset;
  delete dataspace;

  close_group();
  std::string full_name = get_path() + "/" + name;
  write(full_name, std::vector<hsize_t>{2, V.size()}, HDF5_TYPE<scalar_type>::get_PredType(),
        V.ptr());
}

template <typename scalar_type>
void HDF5Writer::execute(const std::string& name,
                         const dca::linalg::Matrix<scalar_type, dca::linalg::CPU>& A) {
  H5::H5File& file = (*my_file);

  open_group(name);

  {
    execute("name", A.get_name());

    execute("current-size", A.size());
    execute("global-size", A.capacity());
  }

  hsize_t dims[2];

  dims[0] = A.capacity().first;
  dims[1] = A.capacity().second;

  H5::DataSet* dataset = NULL;
  H5::DataSpace* dataspace = NULL;

  {
    dataspace = new H5::DataSpace(2, dims);

    std::string full_name = get_path() + "/data";
    dataset = new H5::DataSet(
        file.createDataSet(full_name.c_str(), HDF5_TYPE<scalar_type>::get_PredType(), *dataspace));

    H5Dwrite(dataset->getId(), HDF5_TYPE<scalar_type>::get(), dataspace->getId(), H5S_ALL,
             H5P_DEFAULT, &A(0, 0));
  }
  std::vector<hsize_t> dims{hsize_t(A.nrRows()), hsize_t(A.nrCols())};
  std::vector<scalar_type> linearized(dims[0] * dims[1]);

  delete dataset;
  delete dataspace;
  int linindex = 0;
  // Note: Matrices are row major, while HDF5 is column major
  for (int i = 0; i < A.nrRows(); ++i)
    for (int j = 0; j < A.nrCols(); ++j)
      linearized[linindex++] = A(i, j);

  close_group();
  std::string full_name = get_path() + "/" + name;
  write(full_name, dims, HDF5_TYPE<scalar_type>::get_PredType(), linearized.data());
}

template <typename scalar_type>
void HDF5Writer::execute(const std::string& name,
                         const dca::linalg::Matrix<std::complex<scalar_type>, dca::linalg::CPU>& A) {
  H5::H5File& file = (*my_file);

  open_group(name);

  {
    execute("name", A.get_name());

    std::vector<int> vec(2, 0);

    vec[0] = A.size().first;
    vec[1] = A.size().second;
    execute("current-size", vec);

    vec[0] = A.capacity().first;
    vec[1] = A.capacity().second;
    execute("global-size", vec);
  }

  hsize_t dims[3];
  std::vector<hsize_t> dims{hsize_t(2), hsize_t(A.nrRows()), hsize_t(A.nrCols())};
  std::vector<std::complex<scalar_type>> linearized(A.nrRows() * A.nrCols());

  dims[0] = 2;
  dims[1] = A.capacity().first;
  dims[2] = A.capacity().second;

  H5::DataSet* dataset = NULL;
  H5::DataSpace* dataspace = NULL;

  {
    dataspace = new H5::DataSpace(3, dims);

    std::string full_name = get_path() + "/data";
    dataset = new H5::DataSet(
        file.createDataSet(full_name.c_str(), HDF5_TYPE<scalar_type>::get_PredType(), *dataspace));
  int linindex = 0;
  // Note: Matrices are row major, while HDF5 is column major
  for (int i = 0; i < A.nrRows(); ++i)
    for (int j = 0; j < A.nrCols(); ++j)
      linearized[linindex++] = A(i, j);

    H5Dwrite(dataset->getId(), HDF5_TYPE<scalar_type>::get(), dataspace->getId(), H5S_ALL,
             H5P_DEFAULT, &A(0, 0));
  }

  delete dataset;
  delete dataspace;

  close_group();
  std::string full_name = get_path() + "/" + name;
  write(full_name, dims, HDF5_TYPE<scalar_type>::get_PredType(), linearized.data());
}

template <class T>
+1 −0
Original line number Diff line number Diff line
@@ -39,6 +39,7 @@ void HDF5Reader::open_file(std::string file_name) {
}

void HDF5Reader::close_file() {
  my_file->close();
  delete my_file;
  my_file = NULL;
}
+40 −28

File changed.

Preview size limit exceeded, changes collapsed.

+127 −1

File changed.

Preview size limit exceeded, changes collapsed.