OpenGM  2.3.x
Discrete Graphical Model Library
graphicalmodel_explicit_storage.hxx
Go to the documentation of this file.
1 #pragma once
2 #ifndef OPENGM_GRAPHICALMODEL_EXPLICIT_STORAGE_HXX
3 #define OPENGM_GRAPHICALMODEL_EXPLICIT_STORAGE_HXX
4 
5 #include <vector>
6 #include <algorithm>
7 #include <numeric>
8 #include <map>
9 
13 #include "opengm/opengm.hxx"
18 
19 namespace opengm{
20 
21 template<
22  class T,
23  class OPERATOR,
24  class FUNCTION_TYPE_LIST ,
25  class SPACE
26 >
27 class GraphicalModel;
28 
29 
30 template<unsigned int I,unsigned int D,bool END>
31 class FunctionIteratation;
32 
33 
62 
63 template<class GM>
65 
66  template<unsigned int I,unsigned int D,bool END>
67  friend class FunctionIteratation;
68  typedef GM GraphicalModelType;
69  typedef typename GraphicalModelType::LabelType LabelType;
70  typedef typename GraphicalModelType::IndexType IndexType;
71  typedef typename GraphicalModelType::ValueType ValueType;
72  typedef typename GraphicalModelType::OperatorType OperatorType;
73  typedef typename GraphicalModelType::FactorType FactorType;
74  typedef typename GraphicalModelType::IndependentFactorType IndependentFactorType;
75  typedef typename GraphicalModelType::FunctionIdentifier FunctionIdentifier;
76 
77 public:
78  ExplicitStorage(const GraphicalModelType & gm )
79  :gm_(gm),
80  functionSize_(GraphicalModelType::NrOfFunctionTypes),
81  functionTypeStart_(GraphicalModelType::NrOfFunctionTypes,0){
82  //some offset calculation to get a function index
83  // from the fid (=type + index)
84  size_t numFTotal=0;
85  for(size_t i=0;i<GraphicalModelType::NrOfFunctionTypes;++i){
86  functionSize_[i]=gm_.numberOfFunctions(i);
87  numFTotal+=functionSize_[i];
88  }
89  //
90  functionIndexToStart_.resize(numFTotal);
91  functionTypeStart_[0]=0;
92  for(size_t i=1;i<GraphicalModelType::NrOfFunctionTypes;++i){
93  for(size_t ii=0;ii<i;++ii){
94  functionTypeStart_[i]+=functionSize_[ii];
95  }
96  }
97 
98  // calculate how much storage is needed
99  size_t storageSize=0;
101 
102 
103  // allocate memory
104  data_ = new ValueType[storageSize];
105  dataSize_=storageSize;
106 
107  // write function into allocated memory
108  size_t currentOffset=0;
110  OPENGM_ASSERT(currentOffset==storageSize);
111  }
113  delete[] data_;
114  }
115 
116  ValueType const * operator[](const FactorType & factor)const{
117  const size_t scalarIndex=fidToIndex(factor.functionType(),factor.functionIndex());
118  return data_+functionIndexToStart_[scalarIndex];
119  }
120 
121 private:
122  size_t dataSize_;
123  const GraphicalModelType & gm_;
124  std::vector<size_t> functionSize_;
125  std::vector<size_t> functionTypeStart_;
126  std::vector<size_t> functionIndexToStart_;
127  ValueType * data_;
128 
129 
130  size_t fidToIndex(const size_t functionType,const size_t functionIndex)const{
131  return functionTypeStart_[functionType]+functionIndex;
132  }
133 
134  ValueType const * getFunction(size_t functionType,size_t functionIndex){
135 
136  }
137 
138 };
139 
151 template<class GM>
153 private:
154  typedef typename GM::ValueType GmValueType;
155  typedef typename GM::OperatorType GmOperatorTye;
156  typedef typename GM::FunctionIdentifier GmFunctionIdentifier;
157 public:
159 private:
161 public:
162  static void convert(const GM & gm,ExplicitGraphicalModelType & explicitGm){
163  DiscreteSpace< > space;
164  space.reserve(gm.numberOfVariables());
165  for(size_t v=0;v<gm.numberOfVariables();++v){
166  space.addVariable(gm.numberOfLabels(v));
167  }
168  explicitGm.assign(space);
169  std::map<GmFunctionIdentifier,FunctionIdentifier> fidMap;
170  //convert adds all the explicit functions
172  for(size_t f=0;f<gm.numberOfFactors();++f){
173  const typename GM::FactorType & factor=gm[f];
174  FunctionIdentifier explicitFid=fidMap[GmFunctionIdentifier(factor.functionIndex(),factor.functionType())];
175  explicitGm.addFactor(explicitFid,factor.variableIndicesBegin(),factor.variableIndicesEnd());
176  }
177  }
178 };
179 
180 
181 
182 template<unsigned int IX,unsigned int DX>
183 class FunctionIteratation<IX,DX,false>{
184  public:
185  template<class STORAGE>
186  static void size( STORAGE & storage ,size_t & neededStorage){
187 
188  const size_t numF=storage.functionSize_[IX];
189  for(size_t f=0;f<numF;++f){
190  neededStorage+=storage.gm_. template functions<IX>()[f].size();
191  }
192  FunctionIteratation<IX+1,DX,IX+1==DX >::size(storage,neededStorage);
193  }
194 
195  template<class STORAGE>
196  static void store( STORAGE & storage ,size_t & currentOffset){
197  // get function type
198  typedef typename STORAGE::GraphicalModelType::FunctionTypeList FTypeList;
199  typedef typename meta::TypeAtTypeList<FTypeList,IX>::type FunctionType;
200  typedef typename FunctionType::FunctionShapeIteratorType FunctionShapeIteratorType;
201  const size_t numF=storage.functionSize_[IX];
202  for(size_t f=0;f<numF;++f){
203  const FunctionType & function = storage.gm_. template functions<IX>()[f];
204  const size_t functionSize=function.size();
205  // compute function index (1. scalar index)
206  const size_t fIndex=storage.fidToIndex(IX,f);
207 
208  OPENGM_ASSERT(fIndex<storage.functionIndexToStart_.size());
209  // remember offset
210  storage.functionIndexToStart_[fIndex]=currentOffset;
211  // write function into memory
212  ShapeWalker< FunctionShapeIteratorType > walker(function.functionShapeBegin(),function.dimension());
213  for (size_t i = 0; i < functionSize; ++i) {
214  OPENGM_ASSERT(currentOffset+i<storage.dataSize_);
215  storage.data_[currentOffset+i]=function(walker.coordinateTuple().begin());
216  ++walker;
217  }
218  currentOffset+=functionSize;
219  }
220  FunctionIteratation<IX+1,DX,IX+1==DX >::store(storage,currentOffset);
221  }
222 
223  template<class GM,class GM_EXPLICIT,class FID_MAP>
224  static void convert(const GM & gm ,GM_EXPLICIT & gmExplicit , FID_MAP & fidMap){
225  // get function type
226  typedef typename GM::FunctionTypeList FTypeList;
227  typedef typename meta::TypeAtTypeList<FTypeList,IX>::type FunctionType;
228  typedef typename FunctionType::FunctionShapeIteratorType FunctionShapeIteratorType;
229  const size_t numF=gm. template functions<IX>().size();
230  // loop over all function of the type "FunctionType"
231  for(size_t f=0;f<numF;++f){
232  const FunctionType & function = gm. template functions<IX>()[f];
233  const size_t functionSize=function.size();
234 
235 
236  ExplicitFunction<typename GM_EXPLICIT::ValueType> explicitFunction(function.functionShapeBegin(),function.functionShapeEnd());
237  ShapeWalker< FunctionShapeIteratorType > walker(function.functionShapeBegin(),function.dimension());
238  for (size_t i = 0; i < functionSize; ++i) {
239  explicitFunction(i)=function(walker.coordinateTuple().begin());
240  ++walker;
241  }
242  // add function and "fid"-mapping
243  typename GM::FunctionIdentifier gmFid(f,IX);
244  fidMap[gmFid]=gmExplicit.addFunction(explicitFunction);
245  }
247  }
248 
249 
250 };
251 
252 
253 template<unsigned int IX,unsigned int DX>
255  public:
256  template<class STORAGE>
257  static void size( STORAGE & storage ,size_t & neededStorage){
258  // do nothing
259  }
260 
261  template<class STORAGE>
262  static void store( STORAGE & storage ,size_t & currentOffset){
263  // do nothing
264  }
265 
266  template<class GM,class GM_EXPLICIT,class FID_MAP>
267  static void convert(const GM & gm ,GM_EXPLICIT & gmExplicit , FID_MAP & fidMap){
268  // do nothing
269  }
270 };
271 
272 }
273 
274 #endif //OPENGM_GRAPHICALMODEL_EXPLICIT_STORAGE_HXX
IndexType addFactor(const FunctionIdentifier &, ITERATOR, ITERATOR)
add a factor to the graphical model
The OpenGM namespace.
Definition: config.hxx:43
Discrete space in which variables can have differently many labels.
static void size(STORAGE &storage, size_t &neededStorage)
ExplicitStorage (continous storage) of a graphical model function data.
static void store(STORAGE &storage, size_t &currentOffset)
static void store(STORAGE &storage, size_t &currentOffset)
Convert any graphical model into an explicit graphical model.
#define OPENGM_ASSERT(expression)
Definition: opengm.hxx:77
ValueType const * operator[](const FactorType &factor) const
GraphicalModel< GmValueType, GmOperatorTye, ExplicitFunction< GmValueType >, DiscreteSpace< > > ExplicitGraphicalModelType
static void convert(const GM &gm, GM_EXPLICIT &gmExplicit, FID_MAP &fidMap)
ExplicitStorage(const GraphicalModelType &gm)
Function encoded as a dense multi-dimensional array, marray::Marray.
IndexType addVariable(const LabelType)
add one more variable
static void convert(const GM &gm, ExplicitGraphicalModelType &explicitGm)
void assign(const SpaceType &)
clear the graphical model and construct a new one based on a label space
void reserve(const IndexType)
allocate memory for a fixed number of variables
static void convert(const GM &gm, GM_EXPLICIT &gmExplicit, FID_MAP &fidMap)
static void size(STORAGE &storage, size_t &neededStorage)