SHOGUN  v2.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-2012 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/io/SGIO.h>
17 #include <shogun/lib/DataType.h>
20 #include <shogun/lib/memory.h>
21 #include <shogun/base/Parallel.h>
22 #include <shogun/base/Version.h>
23 
24 
25 #ifdef HAVE_PTHREAD
26 #include <pthread.h>
27 #endif //HAVE_PTHREAD
28 
32 namespace shogun
33 {
34 class IO;
35 class Parallel;
36 class Version;
37 class Parameter;
38 class ParameterMap;
39 class SGParamInfo;
40 class CSerializableFile;
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 #define SG_ADD(param, name, description, ms_available) {\
63  m_parameters->add(param, name, description);\
64  if (ms_available)\
65  m_model_selection_parameters->add(param, name, description);\
66 }
67 /*******************************************************************************
68  * End of macros for registering parameters/model selection parameters
69  ******************************************************************************/
70 
74 };
75 
86 class CSGObject
87 {
88 public:
90  CSGObject();
91 
93  CSGObject(const CSGObject& orig);
94 
96  virtual ~CSGObject();
97 
98 #ifdef USE_REFERENCE_COUNTING
99 
103  int32_t ref();
104 
109  int32_t ref_count();
110 
116  int32_t unref();
117 #endif //USE_REFERENCE_COUNTING
118 
122  virtual CSGObject *shallow_copy() const
123  {
125  return NULL;
126  }
127 
131  virtual CSGObject *deep_copy() const
132  {
134  return NULL;
135  }
136 
142  virtual const char* get_name() const = 0;
143 
152  virtual bool is_generic(EPrimitiveType* generic) const;
153 
156  template<class T> void set_generic();
157 
162  void unset_generic();
163 
168  virtual void print_serializable(const char* prefix="");
169 
179  virtual bool save_serializable(CSerializableFile* file,
180  const char* prefix="", int32_t param_version=VERSION_PARAMETER);
181 
193  virtual bool load_serializable(CSerializableFile* file,
194  const char* prefix="", int32_t param_version=VERSION_PARAMETER);
195 
210  int32_t file_version, CSerializableFile* file,
211  const char* prefix="");
212 
225  DynArray<TParameter*>* load_all_file_parameters(int32_t file_version,
226  int32_t current_version,
227  CSerializableFile* file, const char* prefix="");
228 
243  void map_parameters(DynArray<TParameter*>* param_base,
244  int32_t& base_version,
245  DynArray<const SGParamInfo*>* target_param_infos);
246 
251  void set_global_io(SGIO* io);
252 
257  SGIO* get_global_io();
258 
264 
270 
276 
282 
286 
288  void print_modsel_params();
289 
296  char* get_modsel_param_descr(const char* param_name);
297 
304  index_t get_modsel_param_index(const char* param_name);
305 
314 
315 #ifdef TRACE_MEMORY_ALLOCS
316  static void list_memory_allocs()
317  {
318  ::list_memory_allocs();
319  }
320 #endif
321 
322 protected:
342  virtual TParameter* migrate(DynArray<TParameter*>* param_base,
343  const SGParamInfo* target);
344 
367  virtual void one_to_one_migration_prepare(DynArray<TParameter*>* param_base,
368  const SGParamInfo* target, TParameter*& replacement,
369  TParameter*& to_migrate, char* old_name=NULL);
370 
379  virtual void load_serializable_pre() throw (ShogunException);
380 
389  virtual void load_serializable_post() throw (ShogunException);
390 
399  virtual void save_serializable_pre() throw (ShogunException);
400 
409  virtual void save_serializable_post() throw (ShogunException);
410 
416  virtual bool update_parameter_hash();
417 
418 private:
419  void set_global_objects();
420  void unset_global_objects();
421  void init();
422 
428  bool is_param_new(const SGParamInfo param_info) const;
429 
438  bool save_parameter_version(CSerializableFile* file, const char* prefix="",
439  int32_t param_version=VERSION_PARAMETER);
440 
444  int32_t load_parameter_version(CSerializableFile* file,
445  const char* prefix="");
446 
447  /*Gets an incremental hash of all parameters as well as the parameters
448  * of CSGObject children of the current object's parameters.
449  *
450  * @param param Parameter to hash
451  * @param current hash
452  * @param carry value for Murmur3 incremental hash
453  * @param total_length total byte length of all hashed
454  * parameters so far. Byte length of parameters will be added
455  * to the total length
456  */
457  void get_parameter_incremental_hash(Parameter* param,
458  uint32_t& hash, uint32_t& carry, uint32_t& total_length);
459 
460 public:
463 
466 
469 
472 
475 
478 
480  uint32_t m_hash;
481 
482 private:
483 
484  EPrimitiveType m_generic;
485  bool m_load_pre_called;
486  bool m_load_post_called;
487  bool m_save_pre_called;
488  bool m_save_post_called;
489 
490  int32_t m_refcount;
491 
492 #ifdef HAVE_PTHREAD
493  PTHREAD_LOCK_T m_ref_lock;
494 #endif //HAVE_PTHREAD
495 };
496 }
497 #endif // __SGOBJECT_H__

SHOGUN Machine Learning Toolbox - Documentation