Skip to content

Commit

Permalink
Added compilation support without HDF5 library
Browse files Browse the repository at this point in the history
  • Loading branch information
Rohit-Kakodkar committed Oct 11, 2024
1 parent d4939e6 commit 8c7459e
Show file tree
Hide file tree
Showing 11 changed files with 156 additions and 17 deletions.
36 changes: 22 additions & 14 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ cmake_minimum_required(VERSION 3.17.5)
project(specfem2d_kokkos VERSION 0.1.0)

set(CMAKE_CXX_STANDARD 17)
option(HDF5_CXX_BUILD "Build HDF5 C++" ON)
option(MPI_PARALLEL "MPI enabled" OFF)
option(BUILD_TESTS "Tests included" OFF)
option(BUILD_EXAMPLES "Examples included" OFF)
Expand Down Expand Up @@ -62,13 +63,8 @@ endif()
find_package(HDF5 COMPONENTS CXX)

if (NOT ${HDF5_FOUND})
message("-- HDF5 not found. Installing HDF5.")
include(FetchContent)
FetchContent_Declare(
hdf5
URL https://support.hdfgroup.org/ftp/HDF5/releases/hdf5-1.12/hdf5-1.12.0/src/hdf5-1.12.0.tar.gz
)
FetchContent_MakeAvailable(hdf5)
message("-- HDF5 not found. Building without HDF5.")
set(HDF5_CXX_BUILD OFF)
else ()
message(STATUS " LIB: ${HDF5_LIBRARIES}")
message(STATUS " INC: ${HDF5_INCLUDE_DIRS}")
Expand Down Expand Up @@ -114,12 +110,25 @@ add_library(
src/IO/ASCII/native_type.cpp
)

target_link_libraries(
IO
Boost::boost
Kokkos::kokkos
${HDF5_LIBRARIES}
)
if (NOT HDF5_CXX_BUILD)
target_compile_definitions(
IO
PUBLIC -DNO_HDF5
)

target_link_libraries(
IO
Boost::boost
Kokkos::kokkos
)
else()
target_link_libraries(
IO
${HDF5_LIBRARIES}
Boost::boost
Kokkos::kokkos
)
endif()

add_library(
point
Expand Down Expand Up @@ -490,7 +499,6 @@ target_link_libraries(
kernels
solver
Boost::program_options
${HDF5_LIBRARIES}
)

# Include tests
Expand Down
2 changes: 1 addition & 1 deletion docs/cookbooks/example_03.rst
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ Now that we have the mesh database, we can run the forward simulation. Lets set
There are several few critical parameters within the input file that we need to pay attention to:

1. Saving the forward wavefield: Computing frechet derivatives at time @:math:`\tau` requires the adjoint wavefield at time @:math:`\tau` and the forward wavefield at time @:math:`T-\tau`. This would require saving the forward wavefield at every time step during the forward run. However, this can be memory intensive and slow down the simulation. To avoid this, we reconstruct the forward wavefield during the adjoint simulation. This is done by saving the wavefield at the last time step of the forward simulation and running the solver in reverse during the adjoint simulation. The combination of forward and adjoint simulations is called combined simulation within SPECFEM++.
1. Saving the forward wavefield: Computing frechet derivatives at time :math:`\tau` requires the adjoint wavefield at time :math:`\tau` and the forward wavefield at time :math:`T-\tau`. This would require saving the forward wavefield at every time step during the forward run. However, this can be memory intensive and slow down the simulation. To avoid this, we reconstruct the forward wavefield during the adjoint simulation. This is done by saving the wavefield at the last time step of the forward simulation and running the solver in reverse during the adjoint simulation. The combination of forward and adjoint simulations is called combined simulation within SPECFEM++.

To store the wavefield at the last time step, we need to set the following parameters in the input file:

Expand Down
4 changes: 2 additions & 2 deletions docs/getting_started/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Compiler Versions

.. note::

The following compilers are supported and tested by Kokkos. In theory, SPECFEM++ should work with any of these compiler versions. However we have not tested all of them and cannot guarantee the same. If you have issues compiling with a compiler versions listed below, please create an `issue on GitHub <https://github.com/PrincetonUniversity/specfem2d_kokkos/issues/new?assignees=&labels=&projects=&template=bug_report.md&title=>`_`.
The following compilers are supported and tested by Kokkos. In theory, SPECFEM++ should work with any of these compiler versions. However we have not tested all of them and cannot guarantee the same. If you have issues compiling with a compiler versions listed below, please create an `issue on GitHub <https://github.com/PrincetonUniversity/specfem2d_kokkos/issues/new?assignees=&labels=&projects=&template=bug_report.md&title=>`_.

.. list-table::
:widths: 30 35 35
Expand Down Expand Up @@ -67,7 +67,7 @@ Dependencies
* Kokkos: required
* Boost: required
* YAML-CPP: required
* HDF5: required
* HDF5: optional

Download SPECFEM++
------------------
Expand Down
3 changes: 3 additions & 0 deletions include/IO/HDF5/HDF5.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#pragma once

#ifndef NO_HDF5
#include "H5Cpp.h"
#endif

#include "IO/HDF5/impl/dataset.hpp"
#include "IO/HDF5/impl/dataset.tpp"
#include "IO/HDF5/impl/file.hpp"
Expand Down
25 changes: 25 additions & 0 deletions include/IO/HDF5/impl/dataset.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
#ifndef SPECFEM_IO_HDF5_IMPL_DATASET_HPP
#define SPECFEM_IO_HDF5_IMPL_DATASET_HPP

#ifndef NO_HDF5
#include "H5Cpp.h"
#endif

#include "datasetbase.hpp"
#include <memory>
#include <string>
Expand All @@ -11,6 +14,27 @@ namespace IO {
namespace impl {
namespace HDF5 {

#ifdef NO_HDF5
// Error message if HDF5 is not available
template <typename ViewType, typename OpType> class Dataset {
public:
using value_type = typename ViewType::non_const_value_type;
using MemSpace = typename ViewType::memory_space;
using native_type = void;

template <typename... Args> Dataset(Args &&...args) {
throw std::runtime_error("SPECFEM++ was not compiled with HDF5 support");
}

void write() {
throw std::runtime_error("SPECFEM++ was not compiled with HDF5 support");
}

void read() {
throw std::runtime_error("SPECFEM++ was not compiled with HDF5 support");
}
};
#else
// Forward declaration
template <typename OpType> class Group;
template <typename OpType> class File;
Expand Down Expand Up @@ -86,6 +110,7 @@ class Dataset : public DatasetBase<OpType> {
private:
ViewType data; ///< Data to be written/read
};
#endif
} // namespace HDF5
} // namespace impl
} // namespace IO
Expand Down
5 changes: 5 additions & 0 deletions include/IO/HDF5/impl/dataset.tpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
#ifndef SPECFEM_IO_HDF5_IMPL_DATASET_TPP
#define SPECFEM_IO_HDF5_IMPL_DATASET_TPP

#ifndef NO_HDF5
#include "H5Cpp.h"
#endif

#include "IO/operators.hpp"
#include "dataset.hpp"
#include "datasetbase.hpp"
Expand All @@ -11,6 +14,7 @@
#include <string>
#include <type_traits>

#ifndef NO_HDF5
template <typename ViewType, typename OpType>
specfem::IO::impl::HDF5::Dataset<ViewType, OpType>::Dataset(
std::unique_ptr<H5::H5File> &file, const std::string &name,
Expand Down Expand Up @@ -68,5 +72,6 @@ void specfem::IO::impl::HDF5::Dataset<ViewType, OpType>::read() {
throw std::runtime_error("Unknown memory space");
}
}
#endif

#endif
6 changes: 6 additions & 0 deletions include/IO/HDF5/impl/datasetbase.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
#ifndef SPECFEM_IO_HDF5_IMPL_DATASETBASE_HPP
#define SPECFEM_IO_HDF5_IMPL_DATASETBASE_HPP

#ifndef NO_HDF5
#include "H5Cpp.h"
#endif

#include "IO/operators.hpp"
#include "native_type.hpp"
#include <memory>
Expand All @@ -11,6 +14,8 @@ namespace specfem {
namespace IO {
namespace impl {
namespace HDF5 {

#ifndef NO_HDF5
template <typename OpType> class DatasetBase;

template <> class DatasetBase<specfem::IO::write> {
Expand Down Expand Up @@ -103,6 +108,7 @@ template <> class DatasetBase<specfem::IO::read> {
std::unique_ptr<H5::DataSet> dataset;
std::unique_ptr<H5::DataSpace> dataspace;
};
#endif
} // namespace HDF5
} // namespace impl
} // namespace IO
Expand Down
39 changes: 39 additions & 0 deletions include/IO/HDF5/impl/file.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
#ifndef SPECFEM_IO_HDF5_IMPL_FILE_HPP
#define SPECFEM_IO_HDF5_IMPL_FILE_HPP

#ifndef NO_HDF5
#include "H5Cpp.h"
#endif

#include "IO/operators.hpp"
#include "dataset.hpp"
#include "group.hpp"
Expand All @@ -13,6 +16,39 @@ namespace IO {
namespace impl {
namespace HDF5 {

#ifdef NO_HDF5
template <typename OpType> class File {
public:
File(const std::string &name) {
throw std::runtime_error("SPECFEM++ was not compiled with HDF5 support");
}
File(const char *name) {
throw std::runtime_error("SPECFEM++ was not compiled with HDF5 support");
}

template <typename ViewType>
specfem::IO::impl::HDF5::Dataset<ViewType, OpType>
createDataset(const std::string &name, const ViewType data) {
throw std::runtime_error("SPECFEM++ was not compiled with HDF5 support");
}

specfem::IO::impl::HDF5::Group<OpType> createGroup(const std::string &name) {
throw std::runtime_error("SPECFEM++ was not compiled with HDF5 support");
}

template <typename ViewType>
specfem::IO::impl::HDF5::Dataset<ViewType, OpType>
openDataset(const std::string &name, const ViewType data) {
throw std::runtime_error("SPECFEM++ was not compiled with HDF5 support");
}

specfem::IO::impl::HDF5::Group<OpType> openGroup(const std::string &name) {
throw std::runtime_error("SPECFEM++ was not compiled with HDF5 support");
}
};

#else

// Forward declaration
template <typename OpType> class Group;
template <typename ViewType, typename OpType> class Dataset;
Expand Down Expand Up @@ -142,6 +178,9 @@ template <> class File<specfem::IO::read> {
private:
std::unique_ptr<H5::H5File> file; ///< pointer to HDF5 file object
};

#endif

} // namespace HDF5
} // namespace impl
} // namespace IO
Expand Down
38 changes: 38 additions & 0 deletions include/IO/HDF5/impl/group.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
#ifndef SPECFEM_IO_HDF5_IMPL_GROUP_HPP
#define SPECFEM_IO_HDF5_IMPL_GROUP_HPP

#ifndef NO_HDF5
#include "H5Cpp.h"
#endif

#include "IO/operators.hpp"
#include "dataset.hpp"
#include <memory>
Expand All @@ -12,6 +15,39 @@ namespace IO {
namespace impl {
namespace HDF5 {

#ifdef NO_HDF5
template <typename OpType> class Group {
public:
Group(std::unique_ptr<H5::H5File> &file, const std::string &name) {
throw std::runtime_error("SPECFEM++ was not compiled with HDF5 support");
}
Group(std::unique_ptr<H5::Group> &group, const std::string &name) {
throw std::runtime_error("SPECFEM++ was not compiled with HDF5 support");
}

template <typename ViewType>
specfem::IO::impl::HDF5::Dataset<ViewType, OpType>
createDataset(const std::string &name, const ViewType data) {
throw std::runtime_error("SPECFEM++ was not compiled with HDF5 support");
}

specfem::IO::impl::HDF5::Group<OpType> createGroup(const std::string &name) {
throw std::runtime_error("SPECFEM++ was not compiled with HDF5 support");
}

template <typename ViewType>
specfem::IO::impl::HDF5::Dataset<ViewType, OpType>
openDataset(const std::string &name, const ViewType data) {
throw std::runtime_error("SPECFEM++ was not compiled with HDF5 support");
}

specfem::IO::impl::HDF5::Group<OpType> openGroup(const std::string &name) {
throw std::runtime_error("SPECFEM++ was not compiled with HDF5 support");
}
};

#else

// Forward declaration
template <typename ViewType, typename OpType> class Dataset;
template <typename OpType> class File;
Expand Down Expand Up @@ -152,6 +188,8 @@ template <> class Group<specfem::IO::read> {
std::unique_ptr<H5::Group> group; ///< pointer to HDF5 group object
};

#endif

} // namespace HDF5
} // namespace impl
} // namespace IO
Expand Down
10 changes: 10 additions & 0 deletions include/IO/HDF5/impl/native_type.hpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
#ifndef SPECFEM_IO_HDF5_IMPL_NATIVE_TYPE_HPP
#define SPECFEM_IO_HDF5_IMPL_NATIVE_TYPE_HPP

#ifndef NO_HDF5
#include "H5Cpp.h"
#endif

namespace specfem {
namespace IO {
namespace impl {
namespace HDF5 {

#ifdef NO_HDF5
template <typename T> struct native_type {
static T type() {
throw std::runtime_error("SPECFEM++ was not compiled with HDF5 support");
}
};
#else
template <typename T> struct native_type {};
#endif
} // namespace HDF5
} // namespace impl
} // namespace IO
Expand Down
5 changes: 5 additions & 0 deletions include/IO/HDF5/impl/native_type.tpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
#ifndef SPECFEM_IO_HDF5_IMPL_NATIVE_TYPE_TPP
#define SPECFEM_IO_HDF5_IMPL_NATIVE_TYPE_TPP

#ifndef NO_HDF5
#include "H5Cpp.h"
#endif

#include "native_type.hpp"
#include <iostream>

#ifndef NO_HDF5
template <> struct specfem::IO::impl::HDF5::native_type<int> {
static H5::IntType& type() {
static H5::IntType type(H5::PredType::NATIVE_INT);
Expand Down Expand Up @@ -108,5 +112,6 @@ template <> struct specfem::IO::impl::HDF5::native_type<bool> {
return type;
}
};
#endif

#endif

0 comments on commit 8c7459e

Please sign in to comment.