graphicalmodel_hdf5.hxx

Go to the documentation of this file.
00001 #pragma once
00002 #ifndef OPENGM_GRAPHICALMODEL_HDF5_HXX
00003 #define OPENGM_GRAPHICALMODEL_HDF5_HXX
00004 
00005 #include <string>
00006 #include <iostream>
00007 #include <sstream>
00008 #include <typeinfo>
00009 
00010 #include "opengm/opengm.hxx"
00011 #include "opengm/utilities/metaprogramming.hxx"
00012 #include "opengm/datastructures/marray/marray.hxx"
00013 #include "opengm/datastructures/marray/marray_hdf5.hxx"
00014 
00015 #include "opengm/graphicalmodel/graphicalmodel_factor.hxx"
00016 #include "opengm/graphicalmodel/graphicalmodel.hxx"
00017 #include "opengm/functions/sparsemarray.hxx"
00018 #include "opengm/functions/potts.hxx"
00019 #include "opengm/functions/pottsn.hxx"
00020 #include "opengm/functions/absolute_difference.hxx"
00021 #include "opengm/functions/squared_difference.hxx"
00022 #include "opengm/functions/truncated_absolute_difference.hxx"
00023 #include "opengm/functions/truncated_squared_difference.hxx"
00024 
00025 namespace opengm {
00026 
00028 namespace hdf5 {
00029 
00031 template<class T>
00032 struct IsValidTypeForHdf5Save {
00033    typedef  opengm::meta::Bool<
00034       opengm::meta::Not<
00035          opengm::meta::IsInvalidType<T>::value
00036       >::value
00037    > tmpBoolAType;
00038    typedef opengm::meta::Bool<
00039       std::numeric_limits<T>::is_specialized
00040    > tmpBoolBType;
00041    enum Values {
00042       value=meta::And<tmpBoolAType::value,tmpBoolBType::value>::value
00043    };
00044 };
00045 
00046 struct StoredValueTypeInfo{
00047    enum Values{
00048       AsFloat=0,
00049       AsDouble=1,
00050       AsUInt=2,
00051       AsInt=3
00052    };
00053 };
00054 
00055 template<class GM, size_t IX, size_t DX, bool END>
00056 struct GetFunctionRegistration;
00057 
00058 template<class GM, size_t IX, size_t DX>
00059 struct GetFunctionRegistration<GM, IX, DX, false> {
00060    static size_t get(const size_t functionIndex) {
00061       if(IX == functionIndex) {
00062          typedef typename meta::TypeAtTypeList<typename GM::FunctionTypeList, IX>::type TypeAtIX ;
00063          return FunctionRegistration< TypeAtIX >::Id;
00064       }
00065       else {
00066          return GetFunctionRegistration
00067          <
00068             GM,
00069             meta::Increment<IX>::value,
00070             DX,
00071             meta::EqualNumber<meta::Increment<IX>::value, DX>::value
00072          >::get(functionIndex);
00073       }
00074    }
00075 };
00076 
00077 template<class GM, size_t IX, size_t DX>
00078 struct GetFunctionRegistration<GM, IX, DX, true>{
00079    static size_t get(const size_t functionIndex) {
00080       throw RuntimeError("Incorrect function type id.");
00081    }
00082 };
00083 
00084 template<class GM, size_t IX, size_t DX, bool END>
00085 struct SaveAndLoadFunctions;
00086 
00087 template<class GM, size_t IX, size_t DX>
00088 struct SaveAndLoadFunctions<GM, IX, DX, false>
00089 {
00090    template<class HDF5_HANDLE>
00091    static void save
00092    (
00093       HDF5_HANDLE handle,
00094       const GM& gm,
00095       const opengm::UInt64Type storeValueTypeAs
00096    ) {
00097       if(meta::FieldAccess::template byIndex<IX>(gm.functionDataField_).functionData_.functions_.size() != 0)
00098       {
00099          // create group
00100          std::stringstream ss;
00101          typedef typename meta::TypeAtTypeList<typename GM::FunctionTypeList, IX>::type TypeAtIX;
00102          ss << "function-id-" << (FunctionRegistration<TypeAtIX>::Id);
00103          hid_t group = marray::hdf5::createGroup(handle, ss.str());
00104 
00105          // loop over all functions of this type
00106          size_t indexCounter = 0;
00107          size_t valueCounter = 0;
00108          for(size_t i=0; i<meta::FieldAccess::template byIndex<IX>(gm.functionDataField_).functionData_.functions_.size(); ++i) {
00109             indexCounter += FunctionSerialization<TypeAtIX>::indexSequenceSize(
00110                meta::FieldAccess::template byIndex<IX> (gm.functionDataField_).functionData_.functions_[i]);
00111             valueCounter += FunctionSerialization<TypeAtIX>::valueSequenceSize(
00112                meta::FieldAccess::template byIndex<IX> (gm.functionDataField_).functionData_.functions_[i]);
00113          }
00114          marray::Vector<typename GM::ValueType> valueVector(valueCounter);
00115          marray::Vector<opengm::UInt64Type> indexVector(indexCounter);
00116          typename marray::Vector<typename GM::ValueType>::iterator valueIter = valueVector.begin();
00117          typename marray::Vector<opengm::UInt64Type>::iterator indexIter = indexVector.begin();
00118          for(size_t i=0; i<meta::FieldAccess::template byIndex<IX>(gm.functionDataField_).functionData_.functions_.size(); ++i) {
00119             FunctionSerialization<TypeAtIX>::serialize(
00120                meta::FieldAccess::template byIndex<IX> (gm.functionDataField_).functionData_.functions_[i], indexIter, valueIter);
00121             indexIter+=FunctionSerialization<TypeAtIX>::indexSequenceSize(
00122                meta::FieldAccess::template byIndex<IX> (gm.functionDataField_).functionData_.functions_[i]);
00123             valueIter+=FunctionSerialization<TypeAtIX>::valueSequenceSize(
00124                meta::FieldAccess::template byIndex<IX> (gm.functionDataField_).functionData_.functions_[i]);
00125          }
00126          marray::hdf5::save(group, std::string("indices"), indexVector);
00127          OPENGM_ASSERT(storeValueTypeAs<4);
00128          typedef typename GM::ValueType GmValueType;
00129          if(storeValueTypeAs==static_cast<opengm::UInt64Type>(StoredValueTypeInfo::AsFloat)) {
00130             typedef opengm::detail_types::Float StorageType;
00131             if(opengm::meta::Compare<GmValueType,StorageType>::value==true) {
00132                marray::hdf5::save(group, std::string("values"), valueVector);
00133             }
00134             else{
00135                marray::Vector<StorageType> tmpValueVector=valueVector;
00136                marray::hdf5::save(group, std::string("values"), tmpValueVector);
00137             }
00138          }
00139          else if(storeValueTypeAs==static_cast<opengm::UInt64Type>(StoredValueTypeInfo::AsDouble)) {
00140             typedef opengm::detail_types::Double StorageType;
00141             if(opengm::meta::Compare<GmValueType,StorageType>::value==true) {
00142                marray::hdf5::save(group, std::string("values"), valueVector);
00143             }
00144             else{
00145                marray::Vector<StorageType> tmpValueVector=valueVector;
00146                marray::hdf5::save(group, std::string("values"), tmpValueVector);
00147             }
00148          }
00149          else if(storeValueTypeAs==static_cast<opengm::UInt64Type>(StoredValueTypeInfo::AsUInt)) {
00150             typedef opengm::detail_types::UInt64Type StorageType;
00151             if(opengm::meta::Compare<GmValueType,StorageType>::value==true) {
00152                marray::hdf5::save(group, std::string("values"), valueVector);
00153             }
00154             else{
00155                marray::Vector<StorageType> tmpValueVector=valueVector;
00156                marray::hdf5::save(group, std::string("values"), tmpValueVector);
00157             }
00158          }
00159          else if (storeValueTypeAs==static_cast<opengm::UInt64Type>(StoredValueTypeInfo::AsInt)) {
00160             typedef opengm::detail_types::Int64Type StorageType;
00161             if(opengm::meta::Compare<GmValueType,StorageType>::value==true) {
00162                marray::hdf5::save(group, std::string("values"), valueVector);
00163             }
00164             else{
00165                marray::Vector<StorageType> tmpValueVector=valueVector;
00166                marray::hdf5::save(group, std::string("values"), tmpValueVector);
00167             }
00168          }
00169          marray::hdf5::closeGroup(group);
00170       }
00171 
00172       // save functions of the next type in the typelist
00173       typedef typename opengm::meta::Increment<IX>::type NewIX;
00174       SaveAndLoadFunctions<GM, NewIX::value, DX, opengm::meta::EqualNumber<NewIX::value, DX>::value >::save(handle, gm,storeValueTypeAs);
00175    }
00176 
00177    template<class HDF5_HANDLE>
00178    static void load
00179    (
00180       HDF5_HANDLE handle,
00181       GM& gm,
00182       const  std::vector<opengm::UInt64Type>& numberOfFunctions,
00183       const  std::vector<opengm::UInt64Type>& functionIndexLookup,
00184       const  std::vector<bool> & useFunction,
00185       const opengm::UInt64Type loadValueTypeAs,
00186       bool oldFormat=false
00187    ) {
00188       if(useFunction[IX]==true) {
00189          size_t mappedIndex;
00190          bool foundIndex=false;
00191          for(size_t i=0;i<functionIndexLookup.size();++i) {
00192             if(functionIndexLookup[i]==IX ) {
00193                mappedIndex=i;
00194                foundIndex=true;
00195                break;
00196             }
00197          }
00198          if(!foundIndex) {
00199             throw RuntimeError("Could not load function.");
00200          }
00201 
00202          if(numberOfFunctions[mappedIndex] != 0) {
00203             // create subgroup
00204             std::stringstream ss;
00205             typedef typename meta::TypeAtTypeList<typename GM::FunctionTypeList, IX>::type TypeAtIX ;
00206             ss << "function-id-" << (FunctionRegistration<TypeAtIX>::Id);
00207             hid_t group = marray::hdf5::openGroup(handle, ss.str());
00208             marray::Vector<typename GM::ValueType> serializationValues;
00209             marray::Vector<opengm::UInt64Type> serializationIndicies;
00210             {
00211                std::string subDatasetName("indices");
00212                marray::hdf5::load(group, subDatasetName, serializationIndicies);
00213             }
00214             {
00215                std::string subDatasetName("values");
00216                OPENGM_ASSERT(loadValueTypeAs<4);
00217                typedef typename GM::ValueType GmValueType;
00218                if(oldFormat==false) {
00219                   if(loadValueTypeAs==static_cast<opengm::UInt64Type>(StoredValueTypeInfo::AsFloat)) {
00220                      typedef opengm::detail_types::Float StorageType;
00221                      if(opengm::meta::Compare<GmValueType,StorageType>::value==true) {
00222                         marray::hdf5::load(group, subDatasetName, serializationValues);
00223                      }
00224                      else{
00225                         marray::Vector<StorageType> tmpSerializationValues;
00226                         marray::hdf5::load(group, subDatasetName, tmpSerializationValues);
00227                         serializationValues=tmpSerializationValues;
00228                      }
00229                   }
00230                   else if(loadValueTypeAs==static_cast<opengm::UInt64Type>(StoredValueTypeInfo::AsDouble)) {
00231                      typedef opengm::detail_types::Double StorageType;
00232                      if(opengm::meta::Compare<GmValueType,StorageType>::value==true) {
00233                         marray::hdf5::load(group, subDatasetName, serializationValues);
00234                      }
00235                      else{
00236                         marray::Vector<StorageType> tmpSerializationValues;
00237                         marray::hdf5::load(group, subDatasetName, tmpSerializationValues);
00238                         serializationValues=tmpSerializationValues;
00239                      }
00240                   }
00241                   else if(loadValueTypeAs==static_cast<opengm::UInt64Type>(StoredValueTypeInfo::AsUInt)) {
00242                      typedef opengm::detail_types::UInt64Type StorageType;
00243                      if(opengm::meta::Compare<GmValueType,StorageType>::value==true) {
00244                         marray::hdf5::load(group, subDatasetName, serializationValues);
00245                      }
00246                      else{
00247                         marray::Vector<StorageType> tmpSerializationValues;
00248                         marray::hdf5::load(group, subDatasetName, tmpSerializationValues);
00249                         serializationValues=tmpSerializationValues;
00250                      }
00251                   }
00252                   else if (loadValueTypeAs==static_cast<opengm::UInt64Type>(StoredValueTypeInfo::AsInt)) {
00253                      typedef opengm::detail_types::Int64Type StorageType;
00254                      if(opengm::meta::Compare<GmValueType,StorageType>::value==true) {
00255                         marray::hdf5::load(group, subDatasetName, serializationValues);
00256                      }
00257                      else{
00258                         marray::Vector<StorageType> tmpSerializationValues;
00259                         marray::hdf5::load(group, subDatasetName, tmpSerializationValues);
00260                         serializationValues=tmpSerializationValues;
00261                      }
00262                   }
00263                }
00264                else{
00265                   marray::hdf5::load(group, subDatasetName, serializationValues);
00266                }
00267 
00268             }
00269             // resize function
00270             gm.template functions<IX>().resize(numberOfFunctions[mappedIndex]);
00271             typename marray::Vector<opengm::UInt64Type>::const_iterator indexIter=serializationIndicies.begin();
00272             typename marray::Vector<typename GM::ValueType>::const_iterator valueIter=serializationValues.begin();
00273             // fill function with data
00274             for(size_t i=0; i<meta::FieldAccess::template byIndex<IX> (gm.functionDataField_).functionData_.functions_.size(); ++i) {
00275                FunctionSerialization<TypeAtIX>::deserialize(
00276                   indexIter, valueIter, meta::FieldAccess::template byIndex<IX> (gm.functionDataField_).functionData_.functions_[i]);
00277                indexIter += FunctionSerialization<TypeAtIX>::indexSequenceSize(
00278                   meta::FieldAccess::template byIndex<IX> (gm.functionDataField_).functionData_.functions_[i]);
00279                valueIter+=FunctionSerialization<TypeAtIX>::valueSequenceSize(
00280                   meta::FieldAccess::template byIndex<IX> (gm.functionDataField_).functionData_.functions_[i]);
00281             }
00282             marray::hdf5::closeGroup(group);
00283          }
00284       }
00285 
00286       // load functions of the next type in the typelist
00287       typedef typename opengm::meta::Increment<IX>::type NewIX;
00288       SaveAndLoadFunctions<GM, NewIX::value, DX, opengm::meta::EqualNumber<NewIX::value, DX>::value >::load
00289       (handle, gm, numberOfFunctions,functionIndexLookup,useFunction,loadValueTypeAs,oldFormat);
00290    }
00291 };
00292 
00293 template<class GM, size_t IX, size_t DX>
00294 struct SaveAndLoadFunctions<GM, IX, DX, true> {
00295    template<class HDF5_HANDLE>
00296    static void save
00297    (
00298       HDF5_HANDLE handle,
00299       const GM& gm,
00300       const opengm::UInt64Type storeValueTypeAs
00301    )
00302    {
00303 
00304    }
00305 
00306    template<class HDF5_HANDLE>
00307    static void load
00308    (
00309       HDF5_HANDLE handle,
00310       GM& gm,
00311       const  std::vector<opengm::UInt64Type>& numberOfFunctions,
00312       const  std::vector<opengm::UInt64Type>& functionIndexLookup,
00313       const  std::vector<bool> & useFunction ,
00314       const opengm::UInt64Type loadValueTypeAs,
00315       bool oldFormat=false
00316    )
00317    {
00318 
00319    }
00320 };
00322 
00327 template<class GM>
00328 void save
00329 (
00330    const GM& gm,
00331    const std::string& filepath,
00332    const std::string& datasetName
00333 )
00334 {
00335    typedef typename GM::ValueType ValueType;
00336    typedef typename GM::FactorType FactorType;
00337 
00338    if(IsValidTypeForHdf5Save<typename GM::ValueType>::value==false) {
00339       throw opengm::RuntimeError( std::string("ValueType  has no support for hdf5 export") );
00340    }
00341    hid_t file = marray::hdf5::createFile(filepath, marray::hdf5::DEFAULT_HDF5_VERSION);
00342    hid_t group = marray::hdf5::createGroup(file, datasetName);
00343    std::vector<UInt64Type> serializationIndicies;
00344    opengm::UInt64Type storeValueTypeAs;
00345    // float
00346    if(opengm::meta::Compare<opengm::detail_types::Float,ValueType>::value==true) {
00347       storeValueTypeAs=static_cast<opengm::UInt64Type>(StoredValueTypeInfo::AsFloat);
00348    }
00349    //double
00350    else if(opengm::meta::Compare<opengm::detail_types::Double,ValueType>::value==true) {
00351       storeValueTypeAs=static_cast<opengm::UInt64Type>(StoredValueTypeInfo::AsDouble);
00352    }
00353    // long double
00354    else if(opengm::meta::Compare<opengm::detail_types::LongDouble,ValueType>::value==true) {
00355       throw RuntimeError(std::string("ValueType \" long double\" has no support for hdf5 export"));
00356    }
00357    // bool
00358    else if(opengm::meta::Compare<opengm::detail_types::Bool,ValueType>::value==true) {
00359       storeValueTypeAs = static_cast<opengm::UInt64Type> (StoredValueTypeInfo::AsUInt);
00360    }
00361    // unsigned integers
00362    else if(std::numeric_limits<ValueType>::is_integer==true && std::numeric_limits<ValueType>::is_signed==false) {
00363       storeValueTypeAs = static_cast<opengm::UInt64Type> (StoredValueTypeInfo::AsUInt);
00364    }
00365    // signed integers
00366    else if(std::numeric_limits<ValueType>::is_integer==true && std::numeric_limits<ValueType>::is_signed==true) {
00367       storeValueTypeAs = static_cast<opengm::UInt64Type> (StoredValueTypeInfo::AsInt);
00368    }
00369    else{
00370        throw RuntimeError(std::string("ValueType has no support for hdf5 export"));
00371    }
00372    //opengm::UInt64Type
00373    // save meta data
00374    {
00375       std::string subDatasetName("header");
00376       serializationIndicies.push_back(VERSION_MAJOR);
00377       serializationIndicies.push_back(VERSION_MINOR);
00378       serializationIndicies.push_back(gm.numberOfVariables());
00379       serializationIndicies.push_back(gm.factors_.size());
00380       serializationIndicies.push_back(GM::NrOfFunctionTypes);
00381       for(size_t i=0; i<GM::NrOfFunctionTypes; ++i) {
00382          const size_t fRegId=GetFunctionRegistration
00383          <
00384             GM,
00385             0,
00386             GM::NrOfFunctionTypes,
00387             meta::EqualNumber<GM::NrOfFunctionTypes, 0>::value
00388          >::get(i);
00389          serializationIndicies.push_back(fRegId);
00390          serializationIndicies.push_back(gm.numberOfFunctions(i));
00391       }
00392       serializationIndicies.push_back(storeValueTypeAs);
00393       marray::hdf5::save(group, subDatasetName, serializationIndicies);
00394    }
00395 
00396    // save numbers of states
00397    {
00398       std::string subDatasetName("numbers-of-states");
00399       serializationIndicies.resize(gm.numberOfVariables());
00400       for(size_t i=0;i<gm.numberOfVariables();++i) {
00401          serializationIndicies[i]=
00402             static_cast<opengm::UInt64Type>(gm.numberOfLabels(i));
00403       }
00404       marray::hdf5::save(group, subDatasetName, serializationIndicies);
00405    }
00406    serializationIndicies.clear();
00407 
00408    // save all functions
00409    SaveAndLoadFunctions<GM, 0, GM::NrOfFunctionTypes, opengm::meta::EqualNumber<GM::NrOfFunctionTypes, 0>::value >::save(group, gm,storeValueTypeAs);
00410 
00411    // save all factors
00412    {
00413       std::string subDatasetName("factors");
00414       for(size_t i = 0; i < gm.factors_.size(); ++i) {
00415          serializationIndicies.push_back(static_cast<opengm::UInt64Type>(gm.factors_[i].functionIndex_));
00416          serializationIndicies.push_back(static_cast<opengm::UInt64Type>(gm.factors_[i].functionTypeId_));
00417          serializationIndicies.push_back(static_cast<opengm::UInt64Type>(gm.factors_[i].variableIndices_.size()));
00418          for(size_t j = 0; j < gm.factors_[i].variableIndices_.size(); ++j) {
00419             serializationIndicies.push_back(static_cast<opengm::UInt64Type> (gm.factors_[i].variableIndices_[j]));
00420          }
00421       }
00422       if(serializationIndicies.size() != 0)marray::hdf5::save(group, subDatasetName, serializationIndicies);
00423    }
00424    marray::hdf5::closeGroup(group);
00425    marray::hdf5::closeFile(file);
00426 }
00427 
00428 template<class GM>
00429 void load
00430 (
00431    GM& gm,
00432    const std::string& filepath,
00433    const std::string& datasetName
00434 )
00435 {
00436    typedef typename GM::ValueType ValueType;
00437    typedef typename GM::FactorType FactorType;
00438    hid_t file = marray::hdf5::openFile(filepath, marray::hdf5::READ_ONLY, marray::hdf5::DEFAULT_HDF5_VERSION);
00439    hid_t group =marray::hdf5::openGroup(file, datasetName);
00440    marray::Vector<opengm::UInt64Type> serializationIndicies;
00441    std::vector<opengm::UInt64Type> numberOfFunctions;
00442    std::vector<opengm::UInt64Type> functionIndexLookup;
00443    std::vector<bool> useFunction(GM::NrOfFunctionTypes,false);
00444    marray::Vector<opengm::UInt64Type> typeRegisterId;
00445    //size_t numberOfVariables;
00446    bool oldFormat=false;
00447    opengm::UInt64Type loadValueTypeAs=0;
00448    {
00449       std::string subDatasetName("header");
00450       marray::hdf5::load(group, subDatasetName, serializationIndicies);
00451       OPENGM_CHECK_OP(serializationIndicies.size() ,>, 5," ")
00452       //OPENGM_CHECK_OP(serializationIndicies.size() ,<=, 5 + 2 * GM::NrOfFunctionTypes+1," ")
00453       //OPENGM_ASSERT( serializationIndicies.size() > 5 && serializationIndicies.size() <= 5 + 2 * GM::NrOfFunctionTypes+1);
00454       if(!(serializationIndicies.size() > 5 && serializationIndicies.size() <= 5 + 2 * GM::NrOfFunctionTypes)) {
00455       }
00456       if(serializationIndicies[0] != 2 || serializationIndicies[1] != 0) {
00457          throw RuntimeError("This version of the HDF5 file format is not supported by this version of OpenGM.");
00458       }
00459       //numberOfVariables=serializationIndicies[2];
00460       //gm.numbersOfStates_.resize(serializationIndicies[2]);
00461       gm.factors_.resize(serializationIndicies[3], FactorType(&gm));
00462       numberOfFunctions.resize(serializationIndicies[4]);
00463       functionIndexLookup.resize(serializationIndicies[4]);
00464       typeRegisterId.resize(serializationIndicies[4]);
00465       for(size_t i=0; i<numberOfFunctions.size(); ++i) {
00466          //const size_t fRegId=GetFunctionRegistration<GM, 0, GM::NrOfFunctionTypes, meta::EqualNumber<GM::NrOfFunctionTypes, 0>::value>::get(i);
00467          typeRegisterId[i]=serializationIndicies[5 + 2 * i];
00468          numberOfFunctions[i]=serializationIndicies[5 + 2*i + 1];
00469       }
00470 
00471       if(serializationIndicies.size()!=5+2*numberOfFunctions.size()+1) {
00472          if(serializationIndicies.size()==5+2*numberOfFunctions.size()) {
00473             oldFormat=true;
00474          }
00475          else{
00476             throw RuntimeError(std::string("error in hdf5 file"));
00477          }
00478       }
00479       else{
00480          loadValueTypeAs=serializationIndicies[serializationIndicies.size()-1];
00481          OPENGM_ASSERT(loadValueTypeAs<4);
00482       }
00483       // check if saved function (type list) is a subset of the typelist of the
00484       // gm in which we want to load
00485       for(size_t i=0; i<numberOfFunctions.size(); ++i) {
00486          opengm::UInt64Type regIdToFind=typeRegisterId[i];
00487          bool foundId=false;
00488          for(size_t j=0; j<GM::NrOfFunctionTypes; ++j) {
00489             opengm::UInt64Type regIdInList=GetFunctionRegistration<GM, 0, GM::NrOfFunctionTypes, meta::EqualNumber<GM::NrOfFunctionTypes, 0>::value>::get(j);
00490             if(regIdToFind==regIdInList ) {
00491                foundId=true;
00492                functionIndexLookup[i]=j;
00493                useFunction[j]=true;
00494                break;
00495             }
00496          }
00497          if(foundId==false && numberOfFunctions[i]!=0) {
00498              std::stringstream ss;
00499              ss << "The HDF5 file contains the function type "
00500                 << regIdToFind
00501                 << " which is not contained in the type list in the C++ code.";
00502             throw RuntimeError(ss.str());
00503          }
00504       }
00505    }
00506    //if(numberOfVariables != 0) {
00507    std::string subDatasetName("numbers-of-states");
00508    marray::hdf5::load(group, subDatasetName, serializationIndicies);
00509    gm.space_.assignDense(serializationIndicies.begin(), serializationIndicies.end());
00510    OPENGM_ASSERT(serializationIndicies.size() == gm.numberOfVariables());
00511    //}
00512    SaveAndLoadFunctions<GM, 0, GM::NrOfFunctionTypes, opengm::meta::EqualNumber<GM::NrOfFunctionTypes, 0>::value >::load
00513    (group, gm, numberOfFunctions,functionIndexLookup,useFunction,loadValueTypeAs,oldFormat);
00514    if(gm.factors_.size() != 0) {
00515 
00516       std::string subDatasetName("factors");
00517       marray::hdf5::load(group, subDatasetName, serializationIndicies);
00518       size_t sIndex = 0;
00519       for(size_t i = 0; i < gm.factors_.size(); ++i) {
00520          gm.factors_[i].functionIndex_ = static_cast<opengm::UInt64Type> (serializationIndicies[sIndex]);
00521          sIndex++;
00522          gm.factors_[i].functionTypeId_ =
00523             functionIndexLookup[static_cast<opengm::UInt64Type> (serializationIndicies[sIndex])];
00524          sIndex++;
00525          gm.factors_[i].variableIndices_.resize(static_cast<opengm::UInt64Type> (serializationIndicies[sIndex]));
00526          sIndex++;
00527          for(size_t j = 0; j < gm.factors_[i].variableIndices_.size(); ++j) {
00528             gm.factors_[i].variableIndices_[j] = static_cast<opengm::UInt64Type> (serializationIndicies[sIndex]);
00529             sIndex++;
00530          }
00531       }
00532    }
00533 
00534    marray::hdf5::closeGroup(group);
00535    marray::hdf5::closeFile(file);
00536    gm.variableFactorAdjaceny_.resize(gm.numberOfVariables());
00537    // adjacenies
00538 
00539    for(size_t i=0; i<gm.numberOfFactors(); ++i) {
00540       for(size_t vi=0;vi<gm[i].numberOfVariables(); ++vi) {
00541          gm.variableFactorAdjaceny_[ gm[i].variableIndex(vi) ].insert(i);
00542       }
00543       //typedef functionwrapper::AddFactorUpdateAdjacencyWrapper<GM::NrOfFunctionTypes> WrapperType;
00544       //WrapperType::op(gm.functionDataField_, gm[i].functionIndex(), i, gm[i].functionType());
00545    }
00546    gm.initializeFactorFunctionAdjacency();
00547 }
00548       
00549 } // namespace hdf5
00550 } // namespace opengm
00551 
00552 #endif // #ifndef OPENGM_GRAPHICALMODEL_HDF5_HXX
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Generated on Mon Jun 17 16:31:03 2013 for OpenGM by  doxygen 1.6.3