SHOGUN  v2.1.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/lib/common.h>
17 #include <shogun/lib/DataType.h>
20 
21 #include <shogun/base/Parallel.h>
22 #include <shogun/base/Version.h>
23 
24 #include <shogun/io/SGIO.h>
25 
26 
27 #ifdef HAVE_PTHREAD
28 #include <pthread.h>
29 #endif //HAVE_PTHREAD
30 
34 namespace shogun
35 {
36 class IO;
37 class Parallel;
38 class Version;
39 class Parameter;
40 class ParameterMap;
41 class SGParamInfo;
42 class CSerializableFile;
43 
44 template <class T, class K> class CMap;
45 
46 struct TParameter;
47 template <class T> class DynArray;
48 
49 // define reference counter macros
50 //
51 #ifdef USE_REFERENCE_COUNTING
52 #define SG_REF(x) { if (x) (x)->ref(); }
53 #define SG_UNREF(x) { if (x) { if ((x)->unref()==0) (x)=NULL; } }
54 #define SG_UNREF_NO_NULL(x) { if (x) { (x)->unref(); } }
55 #else
56 #define SG_REF(x)
57 #define SG_UNREF(x)
58 #define SG_UNREF_NO_NULL(x)
59 #endif
60 
61 /*******************************************************************************
62  * Macros for registering parameters/model selection parameters
63  ******************************************************************************/
64 #define SG_ADD(param, name, description, ms_available) {\
65  m_parameters->add(param, name, description);\
66  if (ms_available)\
67  m_model_selection_parameters->add(param, name, description);\
68 }
69 /*******************************************************************************
70  * End of macros for registering parameters/model selection parameters
71  ******************************************************************************/
72 
76 };
77 
88 class CSGObject
89 {
90 public:
92  CSGObject();
93 
95  CSGObject(const CSGObject& orig);
96 
98  virtual ~CSGObject();
99 
100 #ifdef USE_REFERENCE_COUNTING
101 
105  int32_t ref();
106 
111  int32_t ref_count();
112 
118  int32_t unref();
119 #endif //USE_REFERENCE_COUNTING
120 
124  virtual CSGObject *shallow_copy() const
125  {
127  return NULL;
128  }
129 
133  virtual CSGObject *deep_copy() const
134  {
136  return NULL;
137  }
138 
144  virtual const char* get_name() const = 0;
145 
154  virtual bool is_generic(EPrimitiveType* generic) const;
155 
158  template<class T> void set_generic();
159 
164  void unset_generic();
165 
170  virtual void print_serializable(const char* prefix="");
171 
181  virtual bool save_serializable(CSerializableFile* file,
182  const char* prefix="", int32_t param_version=VERSION_PARAMETER);
183 
195  virtual bool load_serializable(CSerializableFile* file,
196  const char* prefix="", int32_t param_version=VERSION_PARAMETER);
197 
212  int32_t file_version, CSerializableFile* file,
213  const char* prefix="");
214 
227  DynArray<TParameter*>* load_all_file_parameters(int32_t file_version,
228  int32_t current_version,
229  CSerializableFile* file, const char* prefix="");
230 
245  void map_parameters(DynArray<TParameter*>* param_base,
246  int32_t& base_version,
247  DynArray<const SGParamInfo*>* target_param_infos);
248 
253  void set_global_io(SGIO* io);
254 
259  SGIO* get_global_io();
260 
266 
272 
278 
284 
288 
290  void print_modsel_params();
291 
298  char* get_modsel_param_descr(const char* param_name);
299 
306  index_t get_modsel_param_index(const char* param_name);
307 
316 
317 #ifdef TRACE_MEMORY_ALLOCS
318  static void list_memory_allocs()
319  {
320  shogun::list_memory_allocs();
321  }
322 #endif
323 
324 protected:
344  virtual TParameter* migrate(DynArray<TParameter*>* param_base,
345  const SGParamInfo* target);
346 
369  virtual void one_to_one_migration_prepare(DynArray<TParameter*>* param_base,
370  const SGParamInfo* target, TParameter*& replacement,
371  TParameter*& to_migrate, char* old_name=NULL);
372 
381  virtual void load_serializable_pre() throw (ShogunException);
382 
391  virtual void load_serializable_post() throw (ShogunException);
392 
401  virtual void save_serializable_pre() throw (ShogunException);
402 
411  virtual void save_serializable_post() throw (ShogunException);
412 
413 public:
419  virtual bool update_parameter_hash();
420 
421 private:
422  void set_global_objects();
423  void unset_global_objects();
424  void init();
425 
431  bool is_param_new(const SGParamInfo param_info) const;
432 
441  bool save_parameter_version(CSerializableFile* file, const char* prefix="",
442  int32_t param_version=VERSION_PARAMETER);
443 
447  int32_t load_parameter_version(CSerializableFile* file,
448  const char* prefix="");
449 
450  /*Gets an incremental hash of all parameters as well as the parameters
451  * of CSGObject children of the current object's parameters.
452  *
453  * @param param Parameter to hash
454  * @param current hash
455  * @param carry value for Murmur3 incremental hash
456  * @param total_length total byte length of all hashed
457  * parameters so far. Byte length of parameters will be added
458  * to the total length
459  */
460  void get_parameter_incremental_hash(Parameter* param,
461  uint32_t& hash, uint32_t& carry, uint32_t& total_length);
462 
463 public:
466 
469 
472 
475 
478 
481 
483  uint32_t m_hash;
484 
485 private:
486 
487  EPrimitiveType m_generic;
488  bool m_load_pre_called;
489  bool m_load_post_called;
490  bool m_save_pre_called;
491  bool m_save_post_called;
492 
493  int32_t m_refcount;
494 
495 #ifdef HAVE_PTHREAD
496  PTHREAD_LOCK_T m_ref_lock;
497 #endif //HAVE_PTHREAD
498 };
499 }
500 #endif // __SGOBJECT_H__

SHOGUN Machine Learning Toolbox - Documentation