SHOGUN  v3.0.0
 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 class CLock;
41 
42 template <class T, class K> class CMap;
43 
44 struct TParameter;
45 template <class T> class DynArray;
46 
47 // define reference counter macros
48 //
49 #ifdef USE_REFERENCE_COUNTING
50 #define SG_REF(x) { if (x) (x)->ref(); }
51 #define SG_UNREF(x) { if (x) { if ((x)->unref()==0) (x)=NULL; } }
52 #define SG_UNREF_NO_NULL(x) { if (x) { (x)->unref(); } }
53 #else
54 #define SG_REF(x)
55 #define SG_UNREF(x)
56 #define SG_UNREF_NO_NULL(x)
57 #endif
58 
59 /*******************************************************************************
60  * Macros for registering parameters/model selection parameters
61  ******************************************************************************/
62 
63 #define VA_NARGS_IMPL(_1, _2, _3, _4, _5, N, ...) N
64 #define VA_NARGS(...) VA_NARGS_IMPL(__VA_ARGS__, 5, 4, 3, 2, 1)
65 
66 #define VARARG_IMPL2(base, count, ...) base##count(__VA_ARGS__)
67 #define VARARG_IMPL(base, count, ...) VARARG_IMPL2(base, count, __VA_ARGS__)
68 #define VARARG(base, ...) VARARG_IMPL(base, VA_NARGS(__VA_ARGS__), __VA_ARGS__)
69 
70 #define SG_ADD4(param, name, description, ms_available) {\
71  m_parameters->add(param, name, description);\
72  if (ms_available)\
73  m_model_selection_parameters->add(param, name, description);\
74 }
75 
76 #define SG_ADD5(param, name, description, ms_available, gradient_available) {\
77  m_parameters->add(param, name, description);\
78  if (ms_available)\
79  m_model_selection_parameters->add(param, name, description);\
80  if (gradient_available)\
81  m_gradient_parameters->add(param, name, description);\
82 }
83 
84 #define SG_ADD(...) VARARG(SG_ADD, __VA_ARGS__)
85 
86 /*******************************************************************************
87  * End of macros for registering parameters/model selection parameters
88  ******************************************************************************/
89 
94 };
95 
98 {
101 };
102 
116 {
117 public:
119  CSGObject();
120 
122  CSGObject(const CSGObject& orig);
123 
125  virtual ~CSGObject();
126 
127 #ifdef USE_REFERENCE_COUNTING
128 
132  int32_t ref();
133 
138  int32_t ref_count();
139 
145  int32_t unref();
146 #endif //USE_REFERENCE_COUNTING
147 
151  virtual CSGObject *shallow_copy() const
152  {
154  return NULL;
155  }
156 
160  virtual CSGObject *deep_copy() const
161  {
163  return NULL;
164  }
165 
171  virtual const char* get_name() const = 0;
172 
181  virtual bool is_generic(EPrimitiveType* generic) const;
182 
185  template<class T> void set_generic();
186 
191  void unset_generic();
192 
197  virtual void print_serializable(const char* prefix="");
198 
208  virtual bool save_serializable(CSerializableFile* file,
209  const char* prefix="", int32_t param_version=Version::get_version_parameter());
210 
222  virtual bool load_serializable(CSerializableFile* file,
223  const char* prefix="", int32_t param_version=Version::get_version_parameter());
224 
239  int32_t file_version, CSerializableFile* file,
240  const char* prefix="");
241 
254  DynArray<TParameter*>* load_all_file_parameters(int32_t file_version,
255  int32_t current_version,
256  CSerializableFile* file, const char* prefix="");
257 
272  void map_parameters(DynArray<TParameter*>* param_base,
273  int32_t& base_version,
274  DynArray<const SGParamInfo*>* target_param_infos);
275 
280  void set_global_io(SGIO* io);
281 
286  SGIO* get_global_io();
287 
293 
299 
305 
311 
315 
317  void print_modsel_params();
318 
325  char* get_modsel_param_descr(const char* param_name);
326 
333  index_t get_modsel_param_index(const char* param_name);
334 
342 
343 #ifdef TRACE_MEMORY_ALLOCS
344  static void list_memory_allocs()
345  {
346  shogun::list_memory_allocs();
347  }
348 #endif
349 
350 protected:
370  virtual TParameter* migrate(DynArray<TParameter*>* param_base,
371  const SGParamInfo* target);
372 
395  virtual void one_to_one_migration_prepare(DynArray<TParameter*>* param_base,
396  const SGParamInfo* target, TParameter*& replacement,
397  TParameter*& to_migrate, char* old_name=NULL);
398 
407  virtual void load_serializable_pre() throw (ShogunException);
408 
417  virtual void load_serializable_post() throw (ShogunException);
418 
427  virtual void save_serializable_pre() throw (ShogunException);
428 
437  virtual void save_serializable_post() throw (ShogunException);
438 
439 public:
445  virtual bool update_parameter_hash();
446 
458  virtual bool equals(CSGObject* other, float64_t accuracy=0.0);
459 
468  virtual CSGObject* clone();
469 
470 private:
471  void set_global_objects();
472  void unset_global_objects();
473  void init();
474 
480  bool is_param_new(const SGParamInfo param_info) const;
481 
490  bool save_parameter_version(CSerializableFile* file, const char* prefix="",
491  int32_t param_version=Version::get_version_parameter());
492 
496  int32_t load_parameter_version(CSerializableFile* file,
497  const char* prefix="");
498 
499  /*Gets an incremental hash of all parameters as well as the parameters
500  * of CSGObject children of the current object's parameters.
501  *
502  * @param param Parameter to hash
503  * @param current hash
504  * @param carry value for Murmur3 incremental hash
505  * @param total_length total byte length of all hashed
506  * parameters so far. Byte length of parameters will be added
507  * to the total length
508  */
509  void get_parameter_incremental_hash(Parameter* param,
510  uint32_t& hash, uint32_t& carry, uint32_t& total_length);
511 
512 public:
515 
518 
521 
524 
527 
530 
533 
535  uint32_t m_hash;
536 
537 private:
538 
539  EPrimitiveType m_generic;
540  bool m_load_pre_called;
541  bool m_load_post_called;
542  bool m_save_pre_called;
543  bool m_save_post_called;
544 
545  RefCount* m_refcount;
546 };
547 }
548 #endif // __SGOBJECT_H__

SHOGUN Machine Learning Toolbox - Documentation