1
0
mirror of https://github.com/paboyle/Grid.git synced 2025-04-11 22:50:45 +01:00

HDF5: better complex number support

This commit is contained in:
Antonin Portelli 2017-01-20 12:10:41 -08:00
parent 6b5259cc10
commit afa095d33d
5 changed files with 51 additions and 21 deletions

View File

@ -60,12 +60,12 @@ Author: paboyle <paboyle@ph.ed.ac.uk>
// Grid headers // Grid headers
/////////////////// ///////////////////
#include "Config.h" #include "Config.h"
#include <Grid/serialisation/Serialisation.h>
#include <Grid/Timer.h> #include <Grid/Timer.h>
#include <Grid/PerfCount.h> #include <Grid/PerfCount.h>
#include <Grid/Log.h> #include <Grid/Log.h>
#include <Grid/AlignedAllocator.h> #include <Grid/AlignedAllocator.h>
#include <Grid/Simd.h> #include <Grid/Simd.h>
#include <Grid/serialisation/Serialisation.h>
#include <Grid/Threads.h> #include <Grid/Threads.h>
#include <Grid/Lexicographic.h> #include <Grid/Lexicographic.h>
#include <Grid/Init.h> #include <Grid/Init.h>

View File

@ -68,20 +68,21 @@ namespace Grid {
return os; return os;
} }
// Vector element trait ////////////////////////////////////////////////////// // Vector element trait //////////////////////////////////////////////////////
template <typename T> template <typename T>
struct element struct element
{ {
typedef T type; typedef T type;
static constexpr bool is_arithmetic = false; static constexpr bool is_number = false;
}; };
template <typename T> template <typename T>
struct element<std::vector<T>> struct element<std::vector<T>>
{ {
typedef typename element<T>::type type; typedef typename element<T>::type type;
static constexpr bool is_arithmetic = std::is_arithmetic<T>::value static constexpr bool is_number = std::is_arithmetic<T>::value
or element<T>::is_arithmetic; or is_complex<T>::value
or element<T>::is_number;
}; };
// Vector flatening utility class //////////////////////////////////////////// // Vector flatening utility class ////////////////////////////////////////////

View File

@ -32,10 +32,10 @@ namespace Grid
template <typename U> template <typename U>
void writeDefault(const std::string &s, const U &x); void writeDefault(const std::string &s, const U &x);
template <typename U> template <typename U>
typename std::enable_if<element<std::vector<U>>::is_arithmetic, void>::type typename std::enable_if<element<std::vector<U>>::is_number, void>::type
writeDefault(const std::string &s, const std::vector<U> &x); writeDefault(const std::string &s, const std::vector<U> &x);
template <typename U> template <typename U>
typename std::enable_if<!element<std::vector<U>>::is_arithmetic, void>::type typename std::enable_if<!element<std::vector<U>>::is_number, void>::type
writeDefault(const std::string &s, const std::vector<U> &x); writeDefault(const std::string &s, const std::vector<U> &x);
private: private:
template <typename U> template <typename U>
@ -59,10 +59,10 @@ namespace Grid
template <typename U> template <typename U>
void readDefault(const std::string &s, U &output); void readDefault(const std::string &s, U &output);
template <typename U> template <typename U>
typename std::enable_if<element<std::vector<U>>::is_arithmetic, void>::type typename std::enable_if<element<std::vector<U>>::is_number, void>::type
readDefault(const std::string &s, std::vector<U> &x); readDefault(const std::string &s, std::vector<U> &x);
template <typename U> template <typename U>
typename std::enable_if<!element<std::vector<U>>::is_arithmetic, void>::type typename std::enable_if<!element<std::vector<U>>::is_number, void>::type
readDefault(const std::string &s, std::vector<U> &x); readDefault(const std::string &s, std::vector<U> &x);
private: private:
template <typename U> template <typename U>
@ -99,7 +99,7 @@ namespace Grid
void Hdf5Writer::writeDefault(const std::string &s, const std::string &x); void Hdf5Writer::writeDefault(const std::string &s, const std::string &x);
template <typename U> template <typename U>
typename std::enable_if<element<std::vector<U>>::is_arithmetic, void>::type typename std::enable_if<element<std::vector<U>>::is_number, void>::type
Hdf5Writer::writeDefault(const std::string &s, const std::vector<U> &x) Hdf5Writer::writeDefault(const std::string &s, const std::vector<U> &x)
{ {
// alias to element type // alias to element type
@ -135,7 +135,7 @@ namespace Grid
} }
template <typename U> template <typename U>
typename std::enable_if<!element<std::vector<U>>::is_arithmetic, void>::type typename std::enable_if<!element<std::vector<U>>::is_number, void>::type
Hdf5Writer::writeDefault(const std::string &s, const std::vector<U> &x) Hdf5Writer::writeDefault(const std::string &s, const std::vector<U> &x)
{ {
push(s); push(s);
@ -169,7 +169,7 @@ namespace Grid
void Hdf5Reader::readDefault(const std::string &s, std::string &x); void Hdf5Reader::readDefault(const std::string &s, std::string &x);
template <typename U> template <typename U>
typename std::enable_if<element<std::vector<U>>::is_arithmetic, void>::type typename std::enable_if<element<std::vector<U>>::is_number, void>::type
Hdf5Reader::readDefault(const std::string &s, std::vector<U> &x) Hdf5Reader::readDefault(const std::string &s, std::vector<U> &x)
{ {
// alias to element type // alias to element type
@ -222,7 +222,7 @@ namespace Grid
} }
template <typename U> template <typename U>
typename std::enable_if<!element<std::vector<U>>::is_arithmetic, void>::type typename std::enable_if<!element<std::vector<U>>::is_number, void>::type
Hdf5Reader::readDefault(const std::string &s, std::vector<U> &x) Hdf5Reader::readDefault(const std::string &s, std::vector<U> &x)
{ {
uint64_t size; uint64_t size;

View File

@ -10,13 +10,14 @@
#define HDF5_NATIVE_TYPE(predType, cType)\ #define HDF5_NATIVE_TYPE(predType, cType)\
template <>\ template <>\
struct Hdf5Type<cType>\ class Hdf5Type<cType>\
{\ {\
static inline const H5NS::PredType & type(void)\ public:\
{\ static inline const H5NS::DataType & type(void)\
return H5NS::PredType::predType;\ {\
}\ return H5NS::PredType::predType;\
static constexpr bool isNative = true;\ }\
static constexpr bool isNative = true;\
}; };
#define DEFINE_HDF5_NATIVE_TYPES \ #define DEFINE_HDF5_NATIVE_TYPES \
@ -38,12 +39,36 @@ HDF5_NATIVE_TYPE(NATIVE_LDOUBLE, long double);
namespace Grid namespace Grid
{ {
template <typename T> struct Hdf5Type template <typename T> class Hdf5Type
{ {
public:
static constexpr bool isNative = false; static constexpr bool isNative = false;
}; };
DEFINE_HDF5_NATIVE_TYPES; DEFINE_HDF5_NATIVE_TYPES;
template <typename R>
class Hdf5Type<std::complex<R>>
{
public:
static inline const H5NS::DataType & type(void)
{
if (typePtr_ == nullptr)
{
typePtr_.reset(new H5NS::CompType(sizeof(std::complex<R>)));
typePtr_->insertMember("re", 0, Hdf5Type<R>::type());
typePtr_->insertMember("im", sizeof(R), Hdf5Type<R>::type());
}
return *typePtr_;
}
static constexpr bool isNative = false;
private:
static std::unique_ptr<H5NS::CompType> typePtr_;
};
template <typename R>
std::unique_ptr<H5NS::CompType> Hdf5Type<std::complex<R>>::typePtr_ = nullptr;
} }
#undef HDF5_NATIVE_TYPE #undef HDF5_NATIVE_TYPE

View File

@ -43,10 +43,14 @@ public:
bool , b, bool , b,
std::vector<double>, array, std::vector<double>, array,
std::vector<std::vector<double>>, twodimarray, std::vector<std::vector<double>>, twodimarray,
std::vector<std::vector<std::vector<Complex>>>, cmplx3darray
); );
myclass() {} myclass() {}
myclass(int i) myclass(int i)
: array(4,5.1), twodimarray(3,std::vector<double>(2,1.23456)), ve(2, myenum::blue) : array(4,5.1)
, twodimarray(3,std::vector<double>(5, 1.23456))
, cmplx3darray(3,std::vector<std::vector<Complex>>(5, std::vector<Complex>(7, Complex(1.2, 3.4))))
, ve(2, myenum::blue)
{ {
e=myenum::red; e=myenum::red;
x=i; x=i;