From 1c50d7a7947164b7209b4f8f790ff8c151ae99de Mon Sep 17 00:00:00 2001
From: Cristian C Lalescu <Cristian.Lalescu@ds.mpg.de>
Date: Wed, 24 May 2017 11:07:20 +0200
Subject: [PATCH] merge io_tools with hdf5_tools

---
 bfps/_base.py                           |   2 +-
 bfps/cpp/base.hpp                       |   2 +-
 bfps/cpp/full_code/hdf5_tools.cpp       |  52 ---------
 bfps/cpp/hdf5_tools.cpp                 | 142 ++++++++++++++++++++++++
 bfps/cpp/{full_code => }/hdf5_tools.hpp |  15 +++
 bfps/cpp/io_tools.cpp                   |  62 -----------
 bfps/cpp/io_tools.hpp                   |  41 -------
 setup.py                                |   3 +-
 8 files changed, 160 insertions(+), 159 deletions(-)
 delete mode 100644 bfps/cpp/full_code/hdf5_tools.cpp
 create mode 100644 bfps/cpp/hdf5_tools.cpp
 rename bfps/cpp/{full_code => }/hdf5_tools.hpp (83%)
 delete mode 100644 bfps/cpp/io_tools.cpp
 delete mode 100644 bfps/cpp/io_tools.hpp

diff --git a/bfps/_base.py b/bfps/_base.py
index 72f833da..03604896 100644
--- a/bfps/_base.py
+++ b/bfps/_base.py
@@ -123,7 +123,7 @@ class _base(object):
                     template_par = 'int'
                 elif parameters[key[i]].dtype == np.float64:
                     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)
             else:
                 src_txt += 'H5Dread(dset, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT, &{0});\n'.format(key_prefix + key[i])
diff --git a/bfps/cpp/base.hpp b/bfps/cpp/base.hpp
index 8fc5942f..61029c26 100644
--- a/bfps/cpp/base.hpp
+++ b/bfps/cpp/base.hpp
@@ -29,7 +29,7 @@
 #include <stdarg.h>
 #include <iostream>
 #include <typeinfo>
-#include "io_tools.hpp"
+#include "hdf5_tools.hpp"
 
 #ifndef BASE
 
diff --git a/bfps/cpp/full_code/hdf5_tools.cpp b/bfps/cpp/full_code/hdf5_tools.cpp
deleted file mode 100644
index 2262db98..00000000
--- a/bfps/cpp/full_code/hdf5_tools.cpp
+++ /dev/null
@@ -1,52 +0,0 @@
-#include "hdf5_tools.hpp"
-
-int hdf5_tools::grow_single_dataset(hid_t dset, int tincrement)
-{
-    int ndims;
-    hsize_t space;
-    space = H5Dget_space(dset);
-    ndims = H5Sget_simple_extent_ndims(space);
-    hsize_t *dims = new hsize_t[ndims];
-    H5Sget_simple_extent_dims(space, dims, NULL);
-    dims[0] += tincrement;
-    H5Dset_extent(dset, dims);
-    H5Sclose(space);
-    delete[] dims;
-    return EXIT_SUCCESS;
-}
-
-herr_t hdf5_tools::grow_dataset_visitor(
-    hid_t o_id,
-    const char *name,
-    const H5O_info_t *info,
-    void *op_data)
-{
-    if (info->type == H5O_TYPE_DATASET)
-    {
-        hsize_t dset = H5Dopen(o_id, name, H5P_DEFAULT);
-        grow_single_dataset(dset, *((int*)(op_data)));
-        H5Dclose(dset);
-    }
-    return EXIT_SUCCESS;
-}
-
-
-int hdf5_tools::grow_file_datasets(
-        const hid_t stat_file,
-        const std::string group_name,
-        int tincrement)
-{
-    int file_problems = 0;
-
-    hid_t group;
-    group = H5Gopen(stat_file, group_name.c_str(), H5P_DEFAULT);
-    H5Ovisit(
-            group,
-            H5_INDEX_NAME,
-            H5_ITER_NATIVE,
-            grow_dataset_visitor,
-            &tincrement);
-    H5Gclose(group);
-    return file_problems;
-}
-
diff --git a/bfps/cpp/hdf5_tools.cpp b/bfps/cpp/hdf5_tools.cpp
new file mode 100644
index 00000000..6158607d
--- /dev/null
+++ b/bfps/cpp/hdf5_tools.cpp
@@ -0,0 +1,142 @@
+#include "hdf5_tools.hpp"
+
+int hdf5_tools::grow_single_dataset(hid_t dset, int tincrement)
+{
+    int ndims;
+    hsize_t space;
+    space = H5Dget_space(dset);
+    ndims = H5Sget_simple_extent_ndims(space);
+    hsize_t *dims = new hsize_t[ndims];
+    H5Sget_simple_extent_dims(space, dims, NULL);
+    dims[0] += tincrement;
+    H5Dset_extent(dset, dims);
+    H5Sclose(space);
+    delete[] dims;
+    return EXIT_SUCCESS;
+}
+
+herr_t hdf5_tools::grow_dataset_visitor(
+    hid_t o_id,
+    const char *name,
+    const H5O_info_t *info,
+    void *op_data)
+{
+    if (info->type == H5O_TYPE_DATASET)
+    {
+        hsize_t dset = H5Dopen(o_id, name, H5P_DEFAULT);
+        grow_single_dataset(dset, *((int*)(op_data)));
+        H5Dclose(dset);
+    }
+    return EXIT_SUCCESS;
+}
+
+
+int hdf5_tools::grow_file_datasets(
+        const hid_t stat_file,
+        const std::string group_name,
+        int tincrement)
+{
+    int file_problems = 0;
+
+    hid_t group;
+    group = H5Gopen(stat_file, group_name.c_str(), H5P_DEFAULT);
+    H5Ovisit(
+            group,
+            H5_INDEX_NAME,
+            H5_ITER_NATIVE,
+            grow_dataset_visitor,
+            &tincrement);
+    H5Gclose(group);
+    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);
+}
+
diff --git a/bfps/cpp/full_code/hdf5_tools.hpp b/bfps/cpp/hdf5_tools.hpp
similarity index 83%
rename from bfps/cpp/full_code/hdf5_tools.hpp
rename to bfps/cpp/hdf5_tools.hpp
index aaa98b5d..0e3b38fe 100644
--- a/bfps/cpp/full_code/hdf5_tools.hpp
+++ b/bfps/cpp/hdf5_tools.hpp
@@ -27,6 +27,8 @@
 #ifndef HDF5_TOOLS_HPP
 #define HDF5_TOOLS_HPP
 
+#include <vector>
+#include <hdf5.h>
 #include "base.hpp"
 
 namespace hdf5_tools
@@ -45,6 +47,19 @@ namespace hdf5_tools
             const hid_t stat_file,
             const std::string group_name,
             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
diff --git a/bfps/cpp/io_tools.cpp b/bfps/cpp/io_tools.cpp
deleted file mode 100644
index 224803dc..00000000
--- a/bfps/cpp/io_tools.cpp
+++ /dev/null
@@ -1,62 +0,0 @@
-/**********************************************************************
-*                                                                     *
-*  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);
-
diff --git a/bfps/cpp/io_tools.hpp b/bfps/cpp/io_tools.hpp
deleted file mode 100644
index 69c0e8bb..00000000
--- a/bfps/cpp/io_tools.hpp
+++ /dev/null
@@ -1,41 +0,0 @@
-/**********************************************************************
-*                                                                     *
-*  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
-
diff --git a/setup.py b/setup.py
index 63941c1d..f9574c84 100644
--- a/setup.py
+++ b/setup.py
@@ -88,7 +88,7 @@ print('This is bfps version ' + VERSION)
 
 
 ### lists of files and MANIFEST.in
-src_file_list = ['full_code/hdf5_tools',
+src_file_list = ['hdf5_tools',
                  'full_code/code_base',
                  'full_code/direct_numerical_simulation',
                  'full_code/NSVE',
@@ -108,7 +108,6 @@ src_file_list = ['full_code/hdf5_tools',
                  'interpolator_base',
                  'fluid_solver',
                  'fluid_solver_base',
-                 'io_tools',
                  'fftw_tools',
                  'spline_n1',
                  'spline_n2',
-- 
GitLab