SGObject.cpp

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-2009 Soeren Sonnenburg
00008  * Copyright (C) 2008-2009 Fraunhofer Institute FIRST and Max Planck Society
00009  */
00010 
00011 #include "base/SGObject.h"
00012 #include "lib/io.h"
00013 #include "base/Parallel.h"
00014 #include "base/init.h"
00015 #include "base/Version.h"
00016 #include "base/Parameter.h"
00017 
00018 #include <stdlib.h>
00019 #include <stdio.h>
00020 
00021 
00022 namespace shogun
00023 {
00024     class CMath;
00025     class Parallel;
00026     class IO;
00027     class Version;
00028 
00029     extern CMath* sg_math;
00030     extern Parallel* sg_parallel;
00031     extern IO* sg_io;
00032     extern Version* sg_version;
00033 
00034     template<> void CSGObject::set_generic<bool>()
00035     {
00036         m_generic = PT_BOOL;
00037     }
00038 
00039     template<> void CSGObject::set_generic<char>()
00040     {
00041         m_generic = PT_CHAR;
00042     }
00043 
00044     template<> void CSGObject::set_generic<int8_t>()
00045     {
00046         m_generic = PT_INT8;
00047     }
00048 
00049     template<> void CSGObject::set_generic<uint8_t>()
00050     {
00051         m_generic = PT_UINT8;
00052     }
00053 
00054     template<> void CSGObject::set_generic<int16_t>()
00055     {
00056         m_generic = PT_INT16;
00057     }
00058 
00059     template<> void CSGObject::set_generic<uint16_t>()
00060     {
00061         m_generic = PT_UINT16;
00062     }
00063 
00064     template<> void CSGObject::set_generic<int32_t>()
00065     {
00066         m_generic = PT_INT32;
00067     }
00068 
00069     template<> void CSGObject::set_generic<uint32_t>()
00070     {
00071         m_generic = PT_UINT32;
00072     }
00073 
00074     template<> void CSGObject::set_generic<int64_t>()
00075     {
00076         m_generic = PT_INT64;
00077     }
00078 
00079     template<> void CSGObject::set_generic<uint64_t>()
00080     {
00081         m_generic = PT_UINT64;
00082     }
00083 
00084     template<> void CSGObject::set_generic<float32_t>()
00085     {
00086         m_generic = PT_FLOAT32;
00087     }
00088 
00089     template<> void CSGObject::set_generic<float64_t>()
00090     {
00091         m_generic = PT_FLOAT64;
00092     }
00093 
00094     template<> void CSGObject::set_generic<floatmax_t>()
00095     {
00096         m_generic = PT_FLOATMAX;
00097     }
00098 
00099 } /* namespace shogun  */
00100 
00101 using namespace shogun;
00102 
00103 CSGObject::CSGObject()
00104 {
00105     init();
00106     set_global_objects();
00107     pthread_mutex_init(&m_ref_mutex, NULL);
00108 
00109     SG_GCDEBUG("SGObject created (%p)\n", this);
00110 }
00111 
00112 CSGObject::CSGObject(const CSGObject& orig)
00113 :io(orig.io), parallel(orig.parallel), version(orig.version)
00114 {
00115     init();
00116     set_global_objects();
00117 }
00118 
00119 CSGObject::~CSGObject()
00120 {
00121     SG_GCDEBUG("SGObject destroyed (%p)\n", this);
00122 
00123     pthread_mutex_destroy(&m_ref_mutex);
00124     unset_global_objects();
00125     delete m_parameters;
00126 }
00127 
00128 void CSGObject::set_global_objects(void)
00129 {
00130     if (!sg_io || !sg_parallel || !sg_version)
00131     {
00132         fprintf(stderr, "call init_shogun() before using the library, dying.\n");
00133         exit(1);
00134     }
00135 
00136     SG_REF(sg_io);
00137     SG_REF(sg_parallel);
00138     SG_REF(sg_version);
00139 
00140     io=sg_io;
00141     parallel=sg_parallel;
00142     version=sg_version;
00143 }
00144 
00145 void CSGObject::unset_global_objects(void)
00146 {
00147     SG_UNREF(version);
00148     SG_UNREF(parallel);
00149     SG_UNREF(io);
00150 }
00151 
00152 void CSGObject::set_io(IO* new_io)
00153 {
00154     SG_UNREF(sg_io);
00155     sg_io=new_io;
00156     SG_REF(sg_io);
00157 }
00158 
00159 IO* CSGObject::get_io()
00160 {
00161     SG_REF(sg_io);
00162     return sg_io;
00163 }
00164 
00165 void CSGObject::set_parallel(Parallel* new_parallel)
00166 {
00167     SG_UNREF(sg_parallel);
00168     sg_parallel=new_parallel;
00169     SG_REF(sg_parallel);
00170 }
00171 
00172 Parallel* CSGObject::get_parallel()
00173 {
00174     SG_REF(sg_parallel);
00175     return sg_parallel;
00176 }
00177 
00178 void CSGObject::set_version(Version* new_version)
00179 {
00180     SG_UNREF(sg_version);
00181     sg_version=new_version;
00182     SG_REF(sg_version);
00183 }
00184 
00185 Version* CSGObject::get_version()
00186 {
00187     SG_REF(sg_version);
00188     return sg_version;
00189 }
00190 
00191 bool CSGObject::is_generic(EPrimitiveType* generic) const
00192 {
00193     *generic = m_generic;
00194 
00195     return m_generic != PT_NOT_GENERIC;
00196 }
00197 
00198 void CSGObject::unset_generic()
00199 {
00200     m_generic = PT_NOT_GENERIC;
00201 }
00202 
00203 void CSGObject::print_serializable(const char* prefix)
00204 {
00205     SG_PRINT("\n%s\n================================================================================\n", get_name());
00206     m_parameters->print(prefix);
00207 }
00208 
00209 bool CSGObject::save_serializable(CSerializableFile* file,
00210                                    const char* prefix)
00211 {
00212     SG_DEBUG("START SAVING CSGObject '%s'\n", get_name());
00213     try
00214     {
00215         save_serializable_pre();
00216     }
00217     catch (ShogunException e)
00218     {
00219         SG_SWARNING("%s%s::save_serializable_pre(): ShogunException: "
00220                    "%s\n", prefix, get_name(),
00221                    e.get_exception_string());
00222         return false;
00223     }
00224     if (!m_save_pre_called)
00225     {
00226         SG_SWARNING("%s%s::save_serializable_pre(): Implementation "
00227                    "error: BASE_CLASS::LOAD_SERIALIZABLE_PRE() not "
00228                    "called!\n", prefix, get_name());
00229         return false;
00230     }
00231 
00232     if (!m_parameters->save(file, prefix))
00233         return false;
00234 
00235     try
00236     {
00237         save_serializable_post();
00238     }
00239     catch (ShogunException e)
00240     {
00241         SG_SWARNING("%s%s::save_serializable_post(): ShogunException: "
00242                    "%s\n", prefix, get_name(),
00243                    e.get_exception_string());
00244         return false;
00245     }
00246 
00247     if (!m_save_post_called)
00248     {
00249         SG_SWARNING("%s%s::save_serializable_post(): Implementation "
00250                    "error: BASE_CLASS::LOAD_SERIALIZABLE_POST() not "
00251                    "called!\n", prefix, get_name());
00252         return false;
00253     }
00254 
00255     if (prefix == NULL || *prefix == '\0')
00256         file->close();
00257 
00258     SG_DEBUG("DONE SAVING CSGObject '%s'\n", get_name());
00259 
00260     return true;;
00261 }
00262 
00263 bool CSGObject::load_serializable(CSerializableFile* file,
00264                                    const char* prefix)
00265 {
00266     SG_DEBUG("START LOADING CSGObject '%s'\n", get_name());
00267     try
00268     {
00269         load_serializable_pre();
00270     }
00271     catch (ShogunException e)
00272     {
00273         SG_SWARNING("%s%s::load_serializable_pre(): ShogunException: "
00274                    "%s\n", prefix, get_name(),
00275                    e.get_exception_string());
00276         return false;
00277     }
00278     if (!m_load_pre_called)
00279     {
00280         SG_SWARNING("%s%s::load_serializable_pre(): Implementation "
00281                    "error: BASE_CLASS::LOAD_SERIALIZABLE_PRE() not "
00282                    "called!\n", prefix, get_name());
00283         return false;
00284     }
00285 
00286     if (!m_parameters->load(file, prefix))
00287         return false;
00288 
00289     try
00290     {
00291         load_serializable_post();
00292     }
00293     catch (ShogunException e)
00294     {
00295         SG_SWARNING("%s%s::load_serializable_post(): ShogunException: "
00296                    "%s\n", prefix, get_name(),
00297                    e.get_exception_string());
00298         return false;
00299     }
00300 
00301     if (!m_load_post_called)
00302     {
00303         SG_SWARNING("%s%s::load_serializable_post(): Implementation "
00304                    "error: BASE_CLASS::LOAD_SERIALIZABLE_POST() not "
00305                    "called!\n", prefix, get_name());
00306         return false;
00307     }
00308     SG_DEBUG("DONE LOADING CSGObject '%s'\n", get_name());
00309 
00310     return true;
00311 }
00312 
00313 void CSGObject::load_serializable_pre() throw (ShogunException)
00314 {
00315     m_load_pre_called = true;
00316 }
00317 
00318 void CSGObject::load_serializable_post() throw (ShogunException)
00319 {
00320     m_load_post_called = true;
00321 }
00322 
00323 void CSGObject::save_serializable_pre() throw (ShogunException)
00324 {
00325     m_save_pre_called = true;
00326 }
00327 
00328 void CSGObject::save_serializable_post() throw (ShogunException)
00329 {
00330     m_save_post_called = true;
00331 }
00332 
00333 void CSGObject::init()
00334 {
00335     m_refcount = 0;
00336     io = NULL;
00337     parallel = NULL;
00338     version = NULL;
00339     m_parameters = new Parameter();
00340     m_generic = PT_NOT_GENERIC;
00341     m_load_pre_called = false;
00342     m_load_post_called = false;
00343 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

SHOGUN Machine Learning Toolbox - Documentation