/******************************************************************************* * * TRIQS: a Toolbox for Research in Interacting Quantum Systems * * Copyright (C) 2011 by O. Parcollet * * TRIQS 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. * * TRIQS 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 * TRIQS. If not, see . * ******************************************************************************/ #ifndef TRIQS_ARRAYS_H5_COMMON_H #define TRIQS_ARRAYS_H5_COMMON_H #include #include "hdf5_hl.h" #include "../cache.hpp" #include #include template std::string get_triqs_hdf5_data_scheme(T const & ) { return "";} namespace triqs { namespace arrays { namespace h5 { using namespace H5; // conversion of C type to HDF5 native inline PredType native_type_from_C(char) { return PredType::NATIVE_CHAR; } inline PredType native_type_from_C(signed char) { return PredType::NATIVE_SCHAR; } inline PredType native_type_from_C(unsigned char) { return PredType::NATIVE_UCHAR; } inline PredType native_type_from_C(short) { return PredType::NATIVE_SHORT; } inline PredType native_type_from_C(unsigned short) { return PredType::NATIVE_USHORT; } inline PredType native_type_from_C(int) { return PredType::NATIVE_INT; } inline PredType native_type_from_C(unsigned) { return PredType::NATIVE_UINT; } inline PredType native_type_from_C(long) { return PredType::NATIVE_LONG; } inline PredType native_type_from_C(unsigned long) { return PredType::NATIVE_ULONG; } inline PredType native_type_from_C(long long) { return PredType::NATIVE_LLONG; } inline PredType native_type_from_C(unsigned long long) { return PredType::NATIVE_ULLONG; } inline PredType native_type_from_C(float) { return PredType::NATIVE_FLOAT; } inline PredType native_type_from_C(double) { return PredType::NATIVE_DOUBLE; } inline PredType native_type_from_C(long double) { return PredType::NATIVE_LDOUBLE; } inline PredType native_type_from_C(bool) { return PredType::NATIVE_SCHAR; } inline PredType native_type_from_C(std::string) { return PredType::C_S1; } // conversion of C type to HDF5 native // NEED TO CHANGE THIS ? It is not standard... We should fix a standard or have a trait inline PredType h5_type_from_C(char) { return PredType::NATIVE_CHAR; } inline PredType h5_type_from_C(signed char) { return PredType::NATIVE_SCHAR; } inline PredType h5_type_from_C(unsigned char) { return PredType::NATIVE_UCHAR; } inline PredType h5_type_from_C(short) { return PredType::NATIVE_SHORT; } inline PredType h5_type_from_C(unsigned short) { return PredType::NATIVE_USHORT; } inline PredType h5_type_from_C(int) { return PredType::NATIVE_INT; } inline PredType h5_type_from_C(unsigned) { return PredType::NATIVE_UINT; } inline PredType h5_type_from_C(long) { return PredType::NATIVE_LONG; } inline PredType h5_type_from_C(unsigned long) { return PredType::NATIVE_ULONG; } inline PredType h5_type_from_C(long long) { return PredType::NATIVE_LLONG; } inline PredType h5_type_from_C(unsigned long long) { return PredType::NATIVE_ULLONG; } inline PredType h5_type_from_C(float) { return PredType::NATIVE_FLOAT; } inline PredType h5_type_from_C(double) { return PredType::NATIVE_DOUBLE; } inline PredType h5_type_from_C(long double) { return PredType::NATIVE_LDOUBLE; } inline PredType h5_type_from_C(bool) { return PredType::NATIVE_SCHAR; } inline PredType h5_type_from_C(std::string) { return PredType::C_S1; } // If it is complex return T else T template struct remove_complex { typedef T type;}; template struct remove_complex > { typedef T type;}; template PredType data_type_mem ( ArrayType const & A) { return native_type_from_C(typename remove_complex::type()); } template PredType data_type_mem_scalar ( S const & A) { return native_type_from_C(typename remove_complex::type()); } // the type of data to put in the file_or_group template PredType data_type_file ( V const & ) { return h5_type_from_C(typename remove_complex::type());} // the pointer on the start of data template TYPE_DISABLE_IF ( void *, boost::is_complex ) data ( ArrayType const & A) { return &(A.storage()[0]);} template TYPE_ENABLE_IF ( void *, boost::is_complex ) data ( ArrayType const & A) { typedef typename ArrayType::value_type::value_type T; std::complex * p = &(A.storage()[0]); return reinterpret_cast(p); } // dataspace from lengths and strides. Correct for the complex. strides must be >0 template H5::DataSpace dataspace_from_LS ( mini_vector const & Ltot, mini_vector const & L, mini_vector const & S, mini_vector const & offset = mini_vector() ) { #ifdef TRIQS_ARRAYS_DEBUG_H5_SLICE std::cerr << "total lens in dataspace_from_LS"<< Ltot.to_string() << std::endl ; std::cerr << "lens in dataspace_from_LS"<< L.to_string() << std::endl ; std::cerr << "Strides in dataspace_from_LS "<< S.to_string() << std::endl ; std::cerr << "offset in dataspace_from_LS"<< offset.to_string() << std::endl ; #endif static const unsigned int rank = R + (IsComplex ? 1 : 0); hsize_t totdimsf[rank], dimsf [rank], stridesf[rank], offsetf[rank]; // dataset dimensions for (size_t u=0; u H5::DataSpace data_space ( ArrayType const & A) { static const unsigned int R = ArrayType::rank; mini_vector S; mini_vector const & S1 ( A.indexmap().strides() ); for (size_t u=0; u::value; return dataspace_from_LS ( A.indexmap().domain().lengths(),A.indexmap().domain().lengths(), S); } /******************** resize or check the size ****************************************************/ template ENABLE_IF(is_amv_value_class) resize_or_check ( A & a, mini_vector const & dimsf ) { a.resize( indexmaps::cuboid::domain_t( dimsf)); } template ENABLE_IF(is_amv_view_class) resize_or_check ( A const & a, mini_vector const & dimsf ) { if (a.indexmap().domain().lengths() != dimsf) TRIQS_RUNTIME_ERROR<<"Dimension error : the view can not be resized : " << "\n in file : "<< dimsf.to_string() << "\n in view : "<createAttribute(name.c_str(), strdatatype, attr_dataspace); //myatt_in.write(strdatatype, strwritebuf); myatt_in.write(strdatatype, (void *)(value.c_str())); } /****************** Read string attribute *********************************************/ /// Return the attribute name of obj, and "" if the attribute does not exist. inline std::string read_string_attribute (H5::H5Object const * obj, std::string name ) { std::string value =""; Attribute attr; if (H5LTfind_attribute(obj -> getId(), name.c_str() )==0) return value;// not present // can not find how to get the size with hl. Using full interface //herr_t err2 = H5LTget_attribute_string(gr.getId(), x.c_str(), name.c_str() , &(buf.front()) ) ; //if (err2 < 0) TRIQS_RUNTIME_ERROR << "Reading a string attribute and got rank !=0"; //value.append( &(buf.front()) ); try { attr= obj->openAttribute(name.c_str());} catch (H5::AttributeIException) { return value;} try { DataSpace dataspace = attr.getSpace(); int rank = dataspace.getSimpleExtentNdims(); if (rank != 0) TRIQS_RUNTIME_ERROR << "Reading a string attribute and got rank !=0"; size_t size = attr.getStorageSize(); StrType strdatatype(PredType::C_S1, size); std::vector buf(size+1, 0x00); attr.read(strdatatype, (void *)(&buf[0])); value.append( &(buf.front()) ); } TRIQS_ARRAYS_H5_CATCH_EXCEPTION; return value; } /****************** Read/Write the special TRIQS_HDF5_data_scheme attribute *********************************************/ template inline void write_triqs_hdf5_data_scheme( H5::Group g, T const & obj) { write_string_attribute( &g, "TRIQS_HDF5_data_scheme" , get_triqs_hdf5_data_scheme(obj).c_str() ) ; // herr_t err = H5LTset_attribute_string(F.getId(), subgroup_name.c_str(), "TRIQS_HDF5_data_scheme" , get_triqs_hdf5_data_scheme(p).c_str() ) ; } inline std::string read_triqs_hdf5_data_scheme( H5::Group g) { return read_string_attribute( &g, "TRIQS_HDF5_data_scheme") ; } /****************** WRITE attribute *********************************************/ //inline void write_triqs_data_scheme ( H5::H5Object const & grp, std::string obj_name, std::string attr_name, std::string triqs_data_scheme ) { // herr_t err = H5LTset_attribute_string(grp.getId(),obj_name.c_str(),attr_name.c_str(), , value.c_str() ) ; // if (err<0) TRIQS_RUNTIME_ERROR << "Error in setting attribute "<< name << " to " << value; // } }}} #endif