Commit 1c50d7a7 authored by Cristian Lalescu's avatar Cristian Lalescu
Browse files

merge io_tools with hdf5_tools

parent d2739af6
Pipeline #12900 passed with stage
in 5 minutes and 55 seconds
...@@ -123,7 +123,7 @@ class _base(object): ...@@ -123,7 +123,7 @@ class _base(object):
template_par = 'int' template_par = 'int'
elif parameters[key[i]].dtype == np.float64: elif parameters[key[i]].dtype == np.float64:
template_par = 'double' template_par = 'double'
src_txt += '{0} = read_vector<{1}>(parameter_file, "/{2}/{0}");\n'.format( src_txt += '{0} = hdf5_tools::read_vector<{1}>(parameter_file, "/{2}/{0}");\n'.format(
key_prefix + key[i], template_par, file_group) key_prefix + key[i], template_par, file_group)
else: else:
src_txt += 'H5Dread(dset, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT, &{0});\n'.format(key_prefix + key[i]) src_txt += 'H5Dread(dset, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT, &{0});\n'.format(key_prefix + key[i])
......
...@@ -29,7 +29,7 @@ ...@@ -29,7 +29,7 @@
#include <stdarg.h> #include <stdarg.h>
#include <iostream> #include <iostream>
#include <typeinfo> #include <typeinfo>
#include "io_tools.hpp" #include "hdf5_tools.hpp"
#ifndef BASE #ifndef BASE
......
...@@ -50,3 +50,93 @@ int hdf5_tools::grow_file_datasets( ...@@ -50,3 +50,93 @@ int hdf5_tools::grow_file_datasets(
return file_problems; return file_problems;
} }
template <typename number>
std::vector<number> hdf5_tools::read_vector(
const hid_t group,
const std::string dset_name)
{
std::vector<number> result;
hsize_t vector_length;
// first, read size of array
hid_t dset, dspace;
hid_t mem_dtype;
if (typeid(number) == typeid(int))
mem_dtype = H5Tcopy(H5T_NATIVE_INT);
else if (typeid(number) == typeid(double))
mem_dtype = H5Tcopy(H5T_NATIVE_DOUBLE);
dset = H5Dopen(group, dset_name.c_str(), H5P_DEFAULT);
dspace = H5Dget_space(dset);
assert(H5Sget_simple_extent_ndims(dspace) == 1);
H5Sget_simple_extent_dims(dspace, &vector_length, NULL);
result.resize(vector_length);
H5Dread(dset, mem_dtype, H5S_ALL, H5S_ALL, H5P_DEFAULT, &result.front());
H5Sclose(dspace);
H5Dclose(dset);
H5Tclose(mem_dtype);
return result;
}
template <typename dtype>
std::vector<dtype> hdf5_tools::read_vector_with_single_rank(
const int myrank,
const int rank_to_use,
const MPI_Comm COMM,
const hid_t file_id,
const std::string dset_name)
{
std::vector<dtype> data;
int vector_size;
if (myrank == rank_to_use)
{
data = hdf5_tools::read_vector<dtype>(
file_id,
dset_name);
vector_size = data.size();
}
MPI_Bcast(
&vector_size,
1,
MPI_INT,
rank_to_use,
COMM);
if (myrank != rank_to_use)
data.resize(vector_size);
MPI_Bcast(
&data.front(),
vector_size,
(typeid(dtype) == typeid(int)) ? MPI_INT : MPI_DOUBLE,
rank_to_use,
COMM);
return data;
}
namespace hdf5_tools
{
template <>
std::vector<int> read_vector(
const hid_t,
const std::string);
template <>
std::vector<double> read_vector(
const hid_t,
const std::string);
template <>
std::vector<int> read_vector_with_single_rank(
const int myrank,
const int rank_to_use,
const MPI_Comm COMM,
const hid_t file_id,
const std::string dset_name);
template <>
std::vector<double> read_vector_with_single_rank(
const int myrank,
const int rank_to_use,
const MPI_Comm COMM,
const hid_t file_id,
const std::string dset_name);
}
...@@ -27,6 +27,8 @@ ...@@ -27,6 +27,8 @@
#ifndef HDF5_TOOLS_HPP #ifndef HDF5_TOOLS_HPP
#define HDF5_TOOLS_HPP #define HDF5_TOOLS_HPP
#include <vector>
#include <hdf5.h>
#include "base.hpp" #include "base.hpp"
namespace hdf5_tools namespace hdf5_tools
...@@ -45,6 +47,19 @@ namespace hdf5_tools ...@@ -45,6 +47,19 @@ namespace hdf5_tools
const hid_t stat_file, const hid_t stat_file,
const std::string group_name, const std::string group_name,
int tincrement); int tincrement);
template <typename number>
std::vector<number> read_vector(
const hid_t group,
const std::string dset_name);
template <typename number>
std::vector<number> read_vector_with_single_rank(
const int myrank,
const int rank_to_use,
const MPI_Comm COMM,
const hid_t group,
const std::string dset_name);
} }
#endif//HDF5_TOOLS_HPP #endif//HDF5_TOOLS_HPP
......
/**********************************************************************
* *
* Copyright 2015 Max Planck Institute *
* for Dynamics and Self-Organization *
* *
* This file is part of bfps. *
* *
* bfps is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published *
* by the Free Software Foundation, either version 3 of the License, *
* or (at your option) any later version. *
* *
* bfps is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with bfps. If not, see <http://www.gnu.org/licenses/> *
* *
* Contact: Cristian.Lalescu@ds.mpg.de *
* *
**********************************************************************/
#include <typeinfo>
#include <cassert>
#include "io_tools.hpp"
template <typename number>
std::vector<number> read_vector(
hid_t group,
std::string dset_name)
{
std::vector<number> result;
hsize_t vector_length;
// first, read size of array
hid_t dset, dspace;
hid_t mem_dtype;
if (typeid(number) == typeid(int))
mem_dtype = H5Tcopy(H5T_NATIVE_INT);
else if (typeid(number) == typeid(double))
mem_dtype = H5Tcopy(H5T_NATIVE_DOUBLE);
dset = H5Dopen(group, dset_name.c_str(), H5P_DEFAULT);
dspace = H5Dget_space(dset);
assert(H5Sget_simple_extent_ndims(dspace) == 1);
H5Sget_simple_extent_dims(dspace, &vector_length, NULL);
result.resize(vector_length);
H5Dread(dset, mem_dtype, H5S_ALL, H5S_ALL, H5P_DEFAULT, &result.front());
H5Sclose(dspace);
H5Dclose(dset);
H5Tclose(mem_dtype);
return result;
}
template std::vector<int> read_vector(
hid_t, std::string);
template std::vector<double> read_vector(
hid_t, std::string);
/**********************************************************************
* *
* Copyright 2015 Max Planck Institute *
* for Dynamics and Self-Organization *
* *
* This file is part of bfps. *
* *
* bfps is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published *
* by the Free Software Foundation, either version 3 of the License, *
* or (at your option) any later version. *
* *
* bfps is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with bfps. If not, see <http://www.gnu.org/licenses/> *
* *
* Contact: Cristian.Lalescu@ds.mpg.de *
* *
**********************************************************************/
#include <hdf5.h>
#include <vector>
#include <string>
#ifndef IO_TOOLS
#define IO_TOOLS
template <typename number>
std::vector<number> read_vector(
hid_t group,
std::string dset_name);
#endif//IO_TOOLS
...@@ -88,7 +88,7 @@ print('This is bfps version ' + VERSION) ...@@ -88,7 +88,7 @@ print('This is bfps version ' + VERSION)
### lists of files and MANIFEST.in ### lists of files and MANIFEST.in
src_file_list = ['full_code/hdf5_tools', src_file_list = ['hdf5_tools',
'full_code/code_base', 'full_code/code_base',
'full_code/direct_numerical_simulation', 'full_code/direct_numerical_simulation',
'full_code/NSVE', 'full_code/NSVE',
...@@ -108,7 +108,6 @@ src_file_list = ['full_code/hdf5_tools', ...@@ -108,7 +108,6 @@ src_file_list = ['full_code/hdf5_tools',
'interpolator_base', 'interpolator_base',
'fluid_solver', 'fluid_solver',
'fluid_solver_base', 'fluid_solver_base',
'io_tools',
'fftw_tools', 'fftw_tools',
'spline_n1', 'spline_n1',
'spline_n2', 'spline_n2',
......
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