SGObject.h

Go to the documentation of this file.
00001 /*
00002  * This program is free software; you can redistribute it and/or modify
00003  * it under the terms of the GNU General Public License as published by
00004  * the Free Software Foundation; either version 3 of the License, or
00005  * (at your option) any later version.
00006  *
00007  * Written (W) 2008-2010 Soeren Sonnenburg
00008  * Written (W) 2011-2012 Heiko Strathmann
00009  * Copyright (C) 2008-2010 Fraunhofer Institute FIRST and Max Planck Society
00010  */
00011 
00012 #ifndef __SGOBJECT_H__
00013 #define __SGOBJECT_H__
00014 
00015 #include <shogun/lib/config.h>
00016 #include <shogun/io/SGIO.h>
00017 #include <shogun/lib/DataType.h>
00018 #include <shogun/lib/SGStringList.h>
00019 #include <shogun/lib/ShogunException.h>
00020 #include <shogun/lib/memory.h>
00021 #include <shogun/base/Parallel.h>
00022 #include <shogun/base/Version.h>
00023 
00024 
00025 #ifdef HAVE_PTHREAD
00026 #include <pthread.h>
00027 #endif //HAVE_PTHREAD
00028 
00032 namespace shogun
00033 {
00034 class IO;
00035 class Parallel;
00036 class Version;
00037 class Parameter;
00038 class ParameterMap;
00039 class SGParamInfo;
00040 class CSerializableFile;
00041 
00042 template <class T, class K> class CMap;
00043 
00044 struct TParameter;
00045 template <class T> class DynArray;
00046 
00047 // define reference counter macros
00048 //
00049 #ifdef USE_REFERENCE_COUNTING
00050 #define SG_REF(x) { if (x) (x)->ref(); }
00051 #define SG_UNREF(x) { if (x) { if ((x)->unref()==0) (x)=NULL; } }
00052 #define SG_UNREF_NO_NULL(x) { if (x) { (x)->unref(); } }
00053 #else
00054 #define SG_REF(x)
00055 #define SG_UNREF(x)
00056 #define SG_UNREF_NO_NULL(x)
00057 #endif
00058 
00059 /*******************************************************************************
00060  * Macros for registering parameters/model selection parameters
00061  ******************************************************************************/
00062 #define SG_ADD(param, name, description, ms_available) {\
00063         m_parameters->add(param, name, description);\
00064         if (ms_available)\
00065             m_model_selection_parameters->add(param, name, description);\
00066 }
00067 /*******************************************************************************
00068  * End of macros for registering parameters/model selection parameters
00069  ******************************************************************************/
00070 
00072 enum EModelSelectionAvailability {
00073     MS_NOT_AVAILABLE=0, MS_AVAILABLE
00074 };
00075 
00086 class CSGObject
00087 {
00088 public:
00090     CSGObject();
00091 
00093     CSGObject(const CSGObject& orig);
00094 
00096     virtual ~CSGObject();
00097 
00098 #ifdef USE_REFERENCE_COUNTING
00099 
00103     int32_t ref();
00104 
00109     int32_t ref_count();
00110 
00116     int32_t unref();
00117 #endif //USE_REFERENCE_COUNTING
00118 
00122     virtual CSGObject *shallow_copy() const
00123     {
00124         SG_NOTIMPLEMENTED;
00125         return NULL;
00126     }
00127 
00131     virtual CSGObject *deep_copy() const
00132     {
00133         SG_NOTIMPLEMENTED;
00134         return NULL;
00135     }
00136 
00142     virtual const char* get_name() const = 0;
00143 
00152     virtual bool is_generic(EPrimitiveType* generic) const;
00153 
00156     template<class T> void set_generic();
00157 
00162     void unset_generic();
00163 
00168     virtual void print_serializable(const char* prefix="");
00169 
00179     virtual bool save_serializable(CSerializableFile* file,
00180             const char* prefix="", int32_t param_version=VERSION_PARAMETER);
00181 
00193     virtual bool load_serializable(CSerializableFile* file,
00194             const char* prefix="", int32_t param_version=VERSION_PARAMETER);
00195 
00209     DynArray<TParameter*>* load_file_parameters(const SGParamInfo* param_info,
00210             int32_t file_version, CSerializableFile* file,
00211             const char* prefix="");
00212 
00225     DynArray<TParameter*>* load_all_file_parameters(int32_t file_version,
00226             int32_t current_version,
00227             CSerializableFile* file, const char* prefix="");
00228 
00243     void map_parameters(DynArray<TParameter*>* param_base,
00244             int32_t& base_version,
00245             DynArray<const SGParamInfo*>* target_param_infos);
00246 
00251     void set_global_io(SGIO* io);
00252 
00257     SGIO* get_global_io();
00258 
00263     void set_global_parallel(Parallel* parallel);
00264 
00269     Parallel* get_global_parallel();
00270 
00275     void set_global_version(Version* version);
00276 
00281     Version* get_global_version();
00282 
00285     SGStringList<char> get_modelsel_names();
00286 
00288     void print_modsel_params();
00289 
00296     char* get_modsel_param_descr(const char* param_name);
00297 
00304     index_t get_modsel_param_index(const char* param_name);
00305 
00313     void build_parameter_dictionary(CMap<TParameter*, CSGObject*>& dict);
00314 
00315 #ifdef TRACE_MEMORY_ALLOCS
00316     static void list_memory_allocs()
00317     {
00318             ::list_memory_allocs();
00319     }
00320 #endif
00321 
00322 protected:
00342     virtual TParameter* migrate(DynArray<TParameter*>* param_base,
00343             const SGParamInfo* target);
00344 
00367     virtual void one_to_one_migration_prepare(DynArray<TParameter*>* param_base,
00368             const SGParamInfo* target, TParameter*& replacement,
00369             TParameter*& to_migrate, char* old_name=NULL);
00370 
00379     virtual void load_serializable_pre() throw (ShogunException);
00380 
00389     virtual void load_serializable_post() throw (ShogunException);
00390 
00399     virtual void save_serializable_pre() throw (ShogunException);
00400 
00409     virtual void save_serializable_post() throw (ShogunException);
00410 
00416     virtual bool update_parameter_hash();
00417 
00418 private:
00419     void set_global_objects();
00420     void unset_global_objects();
00421     void init();
00422 
00428     bool is_param_new(const SGParamInfo param_info) const;
00429 
00438     bool save_parameter_version(CSerializableFile* file, const char* prefix="",
00439             int32_t param_version=VERSION_PARAMETER);
00440 
00444     int32_t load_parameter_version(CSerializableFile* file,
00445             const char* prefix="");
00446 
00447     /*Gets an incremental hash of all parameters as well as the parameters
00448      * of CSGObject children of the current object's parameters.
00449      *
00450      * @param param Parameter to hash
00451      * @param current hash
00452      * @param carry value for Murmur3 incremental hash
00453      * @param total_length total byte length of all hashed
00454      * parameters so far. Byte length of parameters will be added
00455      * to the total length
00456      */
00457     void get_parameter_incremental_hash(Parameter* param,
00458             uint32_t& hash, uint32_t& carry, uint32_t& total_length);
00459 
00460 public:
00462     SGIO* io;
00463 
00465     Parallel* parallel;
00466 
00468     Version* version;
00469 
00471     Parameter* m_parameters;
00472 
00474     Parameter* m_model_selection_parameters;
00475 
00477     ParameterMap* m_parameter_map;
00478 
00480     uint32_t m_hash;
00481 
00482 private:
00483 
00484     EPrimitiveType m_generic;
00485     bool m_load_pre_called;
00486     bool m_load_post_called;
00487     bool m_save_pre_called;
00488     bool m_save_post_called;
00489 
00490     int32_t m_refcount;
00491 
00492 #ifdef HAVE_PTHREAD
00493     PTHREAD_LOCK_T m_ref_lock;
00494 #endif //HAVE_PTHREAD
00495 };
00496 }
00497 #endif // __SGOBJECT_H__
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

SHOGUN Machine Learning Toolbox - Documentation