SHOGUN  v3.0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SGObject.h
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 3 of the License, or
5  * (at your option) any later version.
6  *
7  * Written (W) 2008-2010 Soeren Sonnenburg
8  * Written (W) 2011-2013 Heiko Strathmann
9  * Copyright (C) 2008-2010 Fraunhofer Institute FIRST and Max Planck Society
10  */
11 
12 #ifndef __SGOBJECT_H__
13 #define __SGOBJECT_H__
14 
15 #include <shogun/lib/config.h>
16 #include <shogun/lib/common.h>
17 #include <shogun/lib/DataType.h>
20 #include <shogun/lib/RefCount.h>
21 
22 #include <shogun/base/Parallel.h>
23 #include <shogun/base/Version.h>
24 
25 #include <shogun/io/SGIO.h>
26 
27 
31 namespace shogun
32 {
33 class IO;
34 class Parallel;
35 class Version;
36 class Parameter;
37 class ParameterMap;
38 class SGParamInfo;
39 class CSerializableFile;
40 
41 template <class T, class K> class CMap;
42 
43 struct TParameter;
44 template <class T> class DynArray;
45 
46 // define reference counter macros
47 //
48 #ifdef USE_REFERENCE_COUNTING
49 #define SG_REF(x) { if (x) (x)->ref(); }
50 #define SG_UNREF(x) { if (x) { if ((x)->unref()==0) (x)=NULL; } }
51 #define SG_UNREF_NO_NULL(x) { if (x) { (x)->unref(); } }
52 #else
53 #define SG_REF(x)
54 #define SG_UNREF(x)
55 #define SG_UNREF_NO_NULL(x)
56 #endif
57 
58 /*******************************************************************************
59  * Macros for registering parameters/model selection parameters
60  ******************************************************************************/
61 
62 #define VA_NARGS_IMPL(_1, _2, _3, _4, _5, N, ...) N
63 #define VA_NARGS(...) VA_NARGS_IMPL(__VA_ARGS__, 5, 4, 3, 2, 1)
64 
65 #define VARARG_IMPL2(base, count, ...) base##count(__VA_ARGS__)
66 #define VARARG_IMPL(base, count, ...) VARARG_IMPL2(base, count, __VA_ARGS__)
67 #define VARARG(base, ...) VARARG_IMPL(base, VA_NARGS(__VA_ARGS__), __VA_ARGS__)
68 
69 #define SG_ADD4(param, name, description, ms_available) {\
70  m_parameters->add(param, name, description);\
71  if (ms_available)\
72  m_model_selection_parameters->add(param, name, description);\
73 }
74 
75 #define SG_ADD5(param, name, description, ms_available, gradient_available) {\
76  m_parameters->add(param, name, description);\
77  if (ms_available)\
78  m_model_selection_parameters->add(param, name, description);\
79  if (gradient_available)\
80  m_gradient_parameters->add(param, name, description);\
81 }
82 
83 #define SG_ADD(...) VARARG(SG_ADD, __VA_ARGS__)
84 
85 /*******************************************************************************
86  * End of macros for registering parameters/model selection parameters
87  ******************************************************************************/
88 
93 };
94 
97 {
100 };
101 
115 {
116 public:
118  CSGObject();
119 
121  CSGObject(const CSGObject& orig);
122 
124  virtual ~CSGObject();
125 
126 #ifdef USE_REFERENCE_COUNTING
127 
131  int32_t ref();
132 
137  int32_t ref_count();
138 
144  int32_t unref();
145 #endif //USE_REFERENCE_COUNTING
146 
150  virtual CSGObject *shallow_copy() const
151  {
153  return NULL;
154  }
155 
159  virtual CSGObject *deep_copy() const
160  {
162  return NULL;
163  }
164 
170  virtual const char* get_name() const = 0;
171 
180  virtual bool is_generic(EPrimitiveType* generic) const;
181 
184  template<class T> void set_generic();
185 
190  void unset_generic();
191 
196  virtual void print_serializable(const char* prefix="");
197 
207  virtual bool save_serializable(CSerializableFile* file,
208  const char* prefix="", int32_t param_version=Version::get_version_parameter());
209 
221  virtual bool load_serializable(CSerializableFile* file,
222  const char* prefix="", int32_t param_version=Version::get_version_parameter());
223 
238  int32_t file_version, CSerializableFile* file,
239  const char* prefix="");
240 
253  DynArray<TParameter*>* load_all_file_parameters(int32_t file_version,
254  int32_t current_version,
255  CSerializableFile* file, const char* prefix="");
256 
271  void map_parameters(DynArray<TParameter*>* param_base,
272  int32_t& base_version,
273  DynArray<const SGParamInfo*>* target_param_infos);
274 
279  void set_global_io(SGIO* io);
280 
285  SGIO* get_global_io();
286 
292 
298 
304 
310 
314 
316  void print_modsel_params();
317 
324  char* get_modsel_param_descr(const char* param_name);
325 
332  index_t get_modsel_param_index(const char* param_name);
333 
341 
342 #ifdef TRACE_MEMORY_ALLOCS
343  static void list_memory_allocs()
344  {
345  shogun::list_memory_allocs();
346  }
347 #endif
348 
349 protected:
369  virtual TParameter* migrate(DynArray<TParameter*>* param_base,
370  const SGParamInfo* target);
371 
394  virtual void one_to_one_migration_prepare(DynArray<TParameter*>* param_base,
395  const SGParamInfo* target, TParameter*& replacement,
396  TParameter*& to_migrate, char* old_name=NULL);
397 
406  virtual void load_serializable_pre() throw (ShogunException);
407 
416  virtual void load_serializable_post() throw (ShogunException);
417 
426  virtual void save_serializable_pre() throw (ShogunException);
427 
436  virtual void save_serializable_post() throw (ShogunException);
437 
438 public:
444  virtual bool update_parameter_hash();
445 
457  virtual bool equals(CSGObject* other, float64_t accuracy=0.0);
458 
467  virtual CSGObject* clone();
468 
469 private:
470  void set_global_objects();
471  void unset_global_objects();
472  void init();
473 
479  bool is_param_new(const SGParamInfo param_info) const;
480 
489  bool save_parameter_version(CSerializableFile* file, const char* prefix="",
490  int32_t param_version=Version::get_version_parameter());
491 
495  int32_t load_parameter_version(CSerializableFile* file,
496  const char* prefix="");
497 
498  /*Gets an incremental hash of all parameters as well as the parameters
499  * of CSGObject children of the current object's parameters.
500  *
501  * @param param Parameter to hash
502  * @param current hash
503  * @param carry value for Murmur3 incremental hash
504  * @param total_length total byte length of all hashed
505  * parameters so far. Byte length of parameters will be added
506  * to the total length
507  */
508  void get_parameter_incremental_hash(Parameter* param,
509  uint32_t& hash, uint32_t& carry, uint32_t& total_length);
510 
511 public:
514 
517 
520 
523 
526 
529 
532 
534  uint32_t m_hash;
535 
536 private:
537 
538  EPrimitiveType m_generic;
539  bool m_load_pre_called;
540  bool m_load_post_called;
541  bool m_save_pre_called;
542  bool m_save_post_called;
543 
544  RefCount* m_refcount;
545 };
546 }
547 #endif // __SGOBJECT_H__
virtual const char * get_name() const =0
SGStringList< char > get_modelsel_names()
Definition: SGObject.cpp:1099
Parallel * get_global_parallel()
Definition: SGObject.cpp:248
virtual bool save_serializable(CSerializableFile *file, const char *prefix="", int32_t param_version=Version::get_version_parameter())
Definition: SGObject.cpp:285
Class that holds informations about a certain parameter of an CSGObject. Contains name...
Definition: ParameterMap.h:28
ParameterMap * m_parameter_map
Definition: SGObject.h:531
virtual TParameter * migrate(DynArray< TParameter * > *param_base, const SGParamInfo *target)
Definition: SGObject.cpp:917
int32_t index_t
Definition: common.h:60
virtual CSGObject * clone()
Definition: SGObject.cpp:1312
virtual CSGObject * deep_copy() const
Definition: SGObject.h:159
void unset_generic()
Definition: SGObject.cpp:274
Version * get_global_version()
Definition: SGObject.cpp:261
parameter struct
Definition: Parameter.h:26
virtual void save_serializable_pre()
Definition: SGObject.cpp:1033
virtual bool is_generic(EPrimitiveType *generic) const
Definition: SGObject.cpp:267
#define SG_NOTIMPLEMENTED
Definition: SGIO.h:141
EModelSelectionAvailability
Definition: SGObject.h:90
Parameter * m_parameters
Definition: SGObject.h:522
virtual CSGObject * shallow_copy() const
Definition: SGObject.h:150
static int32_t get_version_parameter()
Definition: Version.cpp:97
Parallel * parallel
Definition: SGObject.h:516
Implements a map of ParameterMapElement instances Maps one key to a set of values.
Definition: ParameterMap.h:161
virtual bool update_parameter_hash()
Definition: SGObject.cpp:226
char * get_modsel_param_descr(const char *param_name)
Definition: SGObject.cpp:1123
DynArray< TParameter * > * load_file_parameters(const SGParamInfo *param_info, int32_t file_version, CSerializableFile *file, const char *prefix="")
Definition: SGObject.cpp:513
Parameter class.
Definition: Parameter.h:216
Class SGObject is the base class of all shogun objects.
Definition: SGObject.h:114
virtual ~CSGObject()
Definition: SGObject.cpp:137
void build_gradient_parameter_dictionary(CMap< TParameter *, CSGObject * > *dict)
Definition: SGObject.cpp:1195
virtual bool equals(CSGObject *other, float64_t accuracy=0.0)
Definition: SGObject.cpp:1216
Template Dynamic array class that creates an array that can be used like a list or an array...
Definition: DynArray.h:30
double float64_t
Definition: common.h:48
virtual bool load_serializable(CSerializableFile *file, const char *prefix="", int32_t param_version=Version::get_version_parameter())
Definition: SGObject.cpp:344
DynArray< TParameter * > * load_all_file_parameters(int32_t file_version, int32_t current_version, CSerializableFile *file, const char *prefix="")
Definition: SGObject.cpp:672
Version * version
Definition: SGObject.h:519
EGradientAvailability
Definition: SGObject.h:96
the class CMap, a map based on the hash-table. w: http://en.wikipedia.org/wiki/Hash_table ...
Definition: SGObject.h:41
virtual void save_serializable_post()
Definition: SGObject.cpp:1038
void print_modsel_params()
Definition: SGObject.cpp:1075
Class Version provides version information.
Definition: Version.h:32
Parameter * m_model_selection_parameters
Definition: SGObject.h:525
void set_global_parallel(Parallel *parallel)
Definition: SGObject.cpp:219
virtual void load_serializable_pre()
Definition: SGObject.cpp:1023
virtual void load_serializable_post()
Definition: SGObject.cpp:1028
Class Parallel provides helper functions for multithreading.
Definition: Parallel.h:27
virtual void one_to_one_migration_prepare(DynArray< TParameter * > *param_base, const SGParamInfo *target, TParameter *&replacement, TParameter *&to_migrate, char *old_name=NULL)
Definition: SGObject.cpp:857
SGIO * get_global_io()
Definition: SGObject.cpp:213
void map_parameters(DynArray< TParameter * > *param_base, int32_t &base_version, DynArray< const SGParamInfo * > *target_param_infos)
Definition: SGObject.cpp:710
index_t get_modsel_param_index(const char *param_name)
Definition: SGObject.cpp:1136
void set_global_io(SGIO *io)
Definition: SGObject.cpp:206
uint32_t m_hash
Definition: SGObject.h:534
Class SGIO, used to do input output operations throughout shogun.
Definition: SGIO.h:245
Parameter * m_gradient_parameters
Definition: SGObject.h:528
virtual void print_serializable(const char *prefix="")
Definition: SGObject.cpp:279
void set_global_version(Version *version)
Definition: SGObject.cpp:254
template class SGStringList
Definition: SGStringList.h:25

SHOGUN Machine Learning Toolbox - Documentation