Tutorials:HDF5Howto

From ALPS
Jump to: navigation, search

This is a HDF5 and ALPS short (very short) howto. Please feel free to extend it or to contact Emanuel or Lukas for any questions. The ALPS hdf5 interface is still experimental - we are very open to comments and suggestions.

Problem: ALPS XML files are human readable. However, for large observables they become too verbose and consume too much space. In addition to that, they take a long time to write.

HDF5 offers the possibility to write files in portable binary format. The ALPS hdf5 interface is based on the mocasito framework discussed during the winter 2008 workshop, and can read and write ALPS observables and observable sets.

The following text gives a quick introduction to the ALPS hdf5 interface.

To use the interface, compile your code with --with-hdf5=/path/to/hdf5. HDF5 libraries have to be compiled with the compiler that is used to compile the rest of the code - it seems that intel and gcc hdf5 - libraries are not compatible. Make sure you get a current version of hdf5, i.e. at least version 1.8. The interface has changed significantly between the two versions.

You can find the general mocasito hdf5 framework in alps/src/mocasito/io. It handles all the read / write commands and the gruesome hdf5 details. The interface to the observables is coded in src/alps/alea/hdf5.h.

A typical Monte Carlo code has an ALPS observable set, in which all the MonteCarlo observables reside. This code writes it to a hdf5 file:

#include <alps/osiris.h> //for the rank
#include <alps/alea/hdf5.h>

 ....

 ////write hdf5 dump
 int rank=alps::detail::local_id();
 std::stringstream dumpname;
 dumpname<<(parms["DUMP"])<<"_"<<rank<<".hdf5";
 try{
   measurements.write_hdf5(dumpname.str(),0,rank);
 }catch(std::exception &e){
   std::cerr<<"caught exception: "<<e.what()<<std::endl;
 } 

The location at which observables in this set are written is defined in our mocasito stylesheet (see workshop). In order to read the observables again, e.g. for evaluation, you need to call the 'read_hdf5' - funciton:

std::stringstream filename;
filename<<parms["DUMP"]<<"_"<<rank<<".hdf5";
if(! (std::ifstream(filename.str().c_str())).good()) break;
mocasito::io::container<mocasito::io::hdf5> container(filename.str().c_str());
std::stringstream path; path<<"/simulation/realizations/0/clones/"<<rank;

Greens.read_hdf5(filename.str().c_str(),0,rank); //  <-this invokes the HDF5 call, will throw exceptions if it fails.
density.read_hdf5(filename.str().c_str(),0,rank);
nn_corr.read_hdf5(filename.str().c_str(),0,rank);

Greens_obseval<<Greens;  //  <-Pipe it into an observable evaluator if you have multiple clones.
density_obseval<<density;
nn_obseval<<nn_corr;
std::cout<<"read rank: "<<rank<<std::endl;

Use the h5ls -r (for recursive) and h5dump tools to access the inner structure of a binary hdf5 file and examine its contents. These tools are part of your hdf5 distribution, you find them in HDF5_DIR/bin/.