42 template<>
void CSGObject::set_generic<bool>()
47 template<>
void CSGObject::set_generic<char>()
52 template<>
void CSGObject::set_generic<int8_t>()
57 template<>
void CSGObject::set_generic<uint8_t>()
62 template<>
void CSGObject::set_generic<int16_t>()
67 template<>
void CSGObject::set_generic<uint16_t>()
69 m_generic = PT_UINT16;
72 template<>
void CSGObject::set_generic<int32_t>()
77 template<>
void CSGObject::set_generic<uint32_t>()
79 m_generic = PT_UINT32;
82 template<>
void CSGObject::set_generic<int64_t>()
87 template<>
void CSGObject::set_generic<uint64_t>()
89 m_generic = PT_UINT64;
92 template<>
void CSGObject::set_generic<float32_t>()
94 m_generic = PT_FLOAT32;
97 template<>
void CSGObject::set_generic<float64_t>()
99 m_generic = PT_FLOAT64;
102 template<>
void CSGObject::set_generic<floatmax_t>()
104 m_generic = PT_FLOATMAX;
107 template<>
void CSGObject::set_generic<CSGObject*>()
109 m_generic = PT_SGOBJECT;
112 template<>
void CSGObject::set_generic<complex128_t>()
114 m_generic = PT_COMPLEX128;
124 set_global_objects();
131 :io(orig.io), parallel(orig.parallel), version(orig.version)
134 set_global_objects();
144 unset_global_objects();
152 #ifdef USE_REFERENCE_COUNTING
153 int32_t CSGObject::ref()
155 int32_t count = m_refcount->
ref();
157 return m_refcount->ref_count();
164 return m_refcount->ref_count();
169 int32_t count = m_refcount->
unref();
172 SG_SGCDEBUG(
"unref() refcount %ld, obj %s (%p) destroying\n", count, this->
get_name(),
this)
179 return m_refcount->ref_count();
182 #endif //USE_REFERENCE_COUNTING
184 #ifdef TRACE_MEMORY_ALLOCS
188 void CSGObject::list_memory_allocs()
190 shogun::list_memory_allocs();
206 void CSGObject::set_global_objects()
210 fprintf(stderr,
"call init_shogun() before using the library, dying.\n");
223 void CSGObject::unset_global_objects()
258 get_parameter_incremental_hash(
m_hash, carry, length);
272 get_parameter_incremental_hash(hash, carry, length);
300 *
generic = m_generic;
312 SG_PRINT(
"\n%s\n================================================================================\n",
get_name())
317 const char* prefix, int32_t param_version)
326 SG_SWARNING(
"%s%s::save_serializable_pre(): ShogunException: "
332 if (!m_save_pre_called)
334 SG_SWARNING(
"%s%s::save_serializable_pre(): Implementation "
335 "error: BASE_CLASS::SAVE_SERIALIZABLE_PRE() not "
341 if (!save_parameter_version(file, prefix, param_version))
353 SG_SWARNING(
"%s%s::save_serializable_post(): ShogunException: "
359 if (!m_save_post_called)
361 SG_SWARNING(
"%s%s::save_serializable_post(): Implementation "
362 "error: BASE_CLASS::SAVE_SERIALIZABLE_POST() not "
367 if (prefix == NULL || *prefix ==
'\0')
376 const char* prefix, int32_t param_version)
378 REQUIRE(file != NULL,
"Serializable file object should be != NULL\n");
387 SG_SWARNING(
"%s%s::load_serializable_pre(): ShogunException: "
392 if (!m_load_pre_called)
394 SG_SWARNING(
"%s%s::load_serializable_pre(): Implementation "
395 "error: BASE_CLASS::LOAD_SERIALIZABLE_PRE() not "
401 int32_t file_version=load_parameter_version(file, prefix);
402 SG_DEBUG(
"file_version=%d, current_version=%d\n", file_version, param_version)
406 SG_WARNING(
"%s%s::load_serializable(): File contains no parameter "
407 "version. Seems like your file is from the days before this "
408 "was introduced. Ignore warning or serialize with this version "
409 "of shogun to get rid of above and this warnings.\n",
413 if (file_version>param_version)
417 SG_WARNING(
"%s%s::load_serializable(): parameter version of file "
418 "larger than the one of shogun. Try with a more recent"
419 "version of shogun.\n", prefix,
get_name());
423 SG_WARNING(
"%s%s::load_serializable(): parameter version of file "
424 "larger than the current. This is probably an implementation"
430 if (file_version==param_version)
441 if (is_param_new(
SGParamInfo(current, param_version)))
444 if (!current->
load(file, prefix))
452 param_version, file, prefix);
462 if (is_param_new(
SGParamInfo(current, param_version)))
483 SG_DEBUG(
"replacing parameter data by loaded/mapped values\n")
487 char* s=SG_MALLOC(
char, 200);
493 if (is_param_new(
SGParamInfo(current, param_version)))
507 SG_DEBUG(
"copying migrated data into parameter\n")
512 SG_DEBUG(
"deleting old parameter base\n")
528 SG_SWARNING(
"%s%s::load_serializable_post(): ShogunException: "
534 if (!m_load_post_called)
536 SG_SWARNING(
"%s%s::load_serializable_post(): Implementation "
537 "error: BASE_CLASS::LOAD_SERIALIZABLE_POST() not "
547 const SGParamInfo* param_info, int32_t file_version,
554 SG_SERROR(
"parameter version of \"%s\" in file (%d) is more recent than"
555 " provided %d!\n", param_info->
m_name, file_version,
563 SG_SDEBUG(
"try to get mapping for: %s\n", s)
567 bool free_mapped=
false;
578 if (file_version<param_info->m_param_version)
616 SG_SDEBUG(
"recursion stop, loading from file\n")
644 if (!loaded->
load(file, prefix))
647 SG_ERROR(
"Could not load %s. The reason for this might be wrong "
648 "parameter mappings\n", s);
652 SG_DEBUG(
"loaded lengths: y=%d, x=%d\n",
670 SG_SDEBUG(
"starting recursion over %s\n", s)
680 SG_SDEBUG(
"appending all results to current result\n")
685 delete recursion_array;
718 if (is_param_new(*info))
751 SG_DEBUG(
"no target parameter infos\n")
765 SG_DEBUG(
"trying to get parameter mapping for %s\n", s)
786 SG_DEBUG(
"no mapping found, using %s\n", s)
805 if (mapped_version>base_version)
817 ASSERT(base_version==mapped_version)
824 SG_DEBUG(
"migrating one step to target: %s\n", s)
831 SG_DEBUG(
"deleting parameters base version %d\n", base_version)
835 SG_DEBUG(
"replacing old parameter base\n")
836 *param_base=*new_base;
837 base_version=mapped_version+1;
849 SG_DEBUG(
"(%d:) \"%s\": sgobject \"%s\" at %p\n", i,
862 SG_DEBUG(
"(%d:) \"%s\": sgobject \"%s\" at %p\n", i,
870 char* s=SG_MALLOC(
char, 200);
894 SG_DEBUG(
"CSGObject::entering CSGObject::one_to_one_migration_prepare() for "
895 "\"%s\"\n", target->
m_name);
902 char* name=target->
m_name;
939 SG_DEBUG(
"copied and SG_REF sgobject pointer for \"%s\" at %p\n",
946 SG_DEBUG(
"CSGObject::leaving CSGObject::one_to_one_migration_prepare() for "
947 "\"%s\"\n", target->
m_name);
978 SG_ERROR(
"Name change for parameter that has to be mapped to \"%s\","
979 " and to no migration method available\n", target->
m_name);
987 char* s=SG_MALLOC(
char, 200);
989 SG_DEBUG(
"nothing changed, using old data: %s\n", s)
1019 SG_DEBUG(
"copying content of poitner for non-scalar data\n")
1026 SG_ERROR(
"No migration method available for %s!\n", s)
1036 const char* prefix, int32_t param_version)
1039 TParameter p(&t, ¶m_version,
"version_parameter",
1040 "Version of parameters of this object");
1041 return p.save(file, prefix);
1047 REQUIRE(file != NULL,
"Serializable file object should be != NULL");
1051 TParameter tp(&t, &v,
"version_parameter",
"");
1052 if (tp.load(file, prefix))
1060 m_load_pre_called =
true;
1065 m_load_post_called =
true;
1070 m_save_pre_called =
true;
1075 m_save_post_called =
true;
1078 #ifdef TRACE_MEMORY_ALLOCS
1083 void CSGObject::init()
1085 #ifdef TRACE_MEMORY_ALLOCS
1088 int32_t idx=sg_mallocs->
index_of(
this);
1105 m_load_pre_called =
false;
1106 m_load_post_called =
false;
1107 m_save_pre_called =
false;
1108 m_save_post_called =
false;
1121 for (
index_t i=0; i<num_param; i++)
1125 char* type=SG_MALLOC(
char, l);
1144 for (
index_t i=0; i<num_param; i++)
1151 if (len>max_string_length)
1152 max_string_length=len;
1166 SG_ERROR(
"There is no model selection parameter called \"%s\" for %s",
1184 if (!strcmp(param_name, current->
m_name))
1194 bool CSGObject::is_param_new(
const SGParamInfo param_info)
const
1203 void CSGObject::get_parameter_incremental_hash(uint32_t& hash, uint32_t& carry,
1204 uint32_t& total_length)
1220 child->get_parameter_incremental_hash(hash, carry,
1233 child[j]->get_parameter_incremental_hash(hash, carry,
1286 SG_INFO(
"leaving %s::equals(): name of other object differs\n",
get_name());
1294 SG_INFO(
"leaving %s::equals(): number of parameters of other object "
1301 SG_DEBUG(
"comparing parameter %d\n", i);
1308 if (!this_param && !other_param)
1311 if (!this_param && other_param)
1313 SG_DEBUG(
"leaving %s::equals(): parameter %d is NULL where other's "
1318 if (this_param && !other_param)
1320 SG_DEBUG(
"leaving %s::equals(): parameter %d is \"%s\" where other's "
1325 SG_DEBUG(
"comparing parameter \"%s\" to other's \"%s\"\n",
1329 if (!strcmp(
"DynamicObjectArray",
get_name()) &&
1330 !strcmp(this_param->
m_name,
"num_elements") &&
1331 !strcmp(other_param->
m_name,
"num_elements"))
1333 SG_DEBUG(
"Ignoring DynamicObjectArray::num_elements field\n");
1338 if (!strcmp(
"DynamicArray",
get_name()) &&
1339 !strcmp(this_param->
m_name,
"num_elements") &&
1340 !strcmp(other_param->
m_name,
"num_elements"))
1342 SG_DEBUG(
"Ignoring DynamicArray::num_elements field\n");
1347 if (!this_param->
equals(other_param, accuracy, tolerant))
1349 SG_INFO(
"leaving %s::equals(): parameters at position %d with name"
1350 " \"%s\" differs from other object parameter with name "
1370 REQUIRE(copy,
"Could not create empty instance of \"%s\". The reason for "
1371 "this usually is that get_name() of the class returns something "
1372 "wrong, or that a class has a wrongly set generic type.\n",
1377 SG_DEBUG(
"cloning parameter \"%s\" at index %d\n",
1382 SG_DEBUG(
"leaving %s::clone(): Clone failed. Returning NULL\n",
virtual const char * get_name() const =0
SGStringList< char > get_modelsel_names()
T get_element(int32_t index) const
Parallel * get_global_parallel()
virtual bool save_serializable(CSerializableFile *file, const char *prefix="", int32_t param_version=Version::get_version_parameter())
virtual void update_parameter_hash()
Class that holds informations about a certain parameter of an CSGObject. Contains name...
ParameterMap * m_parameter_map
virtual TParameter * migrate(DynArray< TParameter * > *param_base, const SGParamInfo *target)
bool append_element(T element)
virtual int32_t get_num_parameters()
virtual CSGObject * clone()
Class ShogunException defines an exception which is thrown whenever an error inside of shogun occurs...
virtual CSGObject * shallow_copy() const
static int32_t binary_search(T *output, int32_t size, T elem)
TParameter * get_parameter(int32_t idx)
DynArray< const SGParamInfo * > * get(const SGParamInfo) const
Version * get_global_version()
virtual void save_serializable_pre()
virtual bool is_generic(EPrimitiveType *generic) const
#define SG_NOTIMPLEMENTED
virtual void print(const char *prefix="")
static uint32_t FinalizeIncrementalMurmurHash3(uint32_t h, uint32_t carry, uint32_t total_length)
int32_t get_num_elements() const
static int32_t get_version_parameter()
SGParamInfo * duplicate() const
Implements a map of ParameterMapElement instances Maps one key to a set of values.
Datatypes that shogun supports.
static void qsort(T *output, int32_t size)
char * get_modsel_param_descr(const char *param_name)
bool equals(TParameter *other, float64_t accuracy=0.0, bool tolerant=false)
DynArray< TParameter * > * load_file_parameters(const SGParamInfo *param_info, int32_t file_version, CSerializableFile *file, const char *prefix="")
Class SGObject is the base class of all shogun objects.
void build_gradient_parameter_dictionary(CMap< TParameter *, CSGObject * > *dict)
Template Dynamic array class that creates an array that can be used like a list or an array...
virtual bool load_serializable(CSerializableFile *file, const char *prefix="", int32_t param_version=Version::get_version_parameter())
DynArray< TParameter * > * load_all_file_parameters(int32_t file_version, int32_t current_version, CSerializableFile *file, const char *prefix="")
virtual bool save(CSerializableFile *file, const char *prefix="")
index_t max_string_length
the class CMap, a map based on the hash-table. w: http://en.wikipedia.org/wiki/Hash_table ...
T * get_element_ptr(int32_t index)
virtual void save_serializable_post()
void print_modsel_params()
CSGObject * new_sgserializable(const char *sgserializable_name, EPrimitiveType generic)
Class Version provides version information.
Parameter * m_model_selection_parameters
void get_incremental_hash(uint32_t &hash, uint32_t &carry, uint32_t &total_length)
virtual bool equals(CSGObject *other, float64_t accuracy=0.0, bool tolerant=false)
virtual CSGObject * deep_copy() const
void set_global_parallel(Parallel *parallel)
void to_string(char *dest, size_t n) const
virtual void load_serializable_pre()
virtual void load_serializable_post()
Class Parallel provides helper functions for multithreading.
int32_t index_of(const K &key)
virtual void one_to_one_migration_prepare(DynArray< TParameter * > *param_base, const SGParamInfo *target, TParameter *&replacement, TParameter *&to_migrate, char *old_name=NULL)
const char * get_exception_string()
all of classes and functions are contained in the shogun namespace
bool load(CSerializableFile *file, const char *prefix="")
int32_t add(const K &key, const T &data)
void map_parameters(DynArray< TParameter * > *param_base, int32_t &base_version, DynArray< const SGParamInfo * > *target_param_infos)
index_t get_modsel_param_index(const char *param_name)
void set_global_io(SGIO *io)
bool copy(TParameter *target)
void copy_data(const TParameter *source)
Class SGIO, used to do input output operations throughout shogun.
Parameter * m_gradient_parameters
virtual void print_serializable(const char *prefix="")
virtual bool parameter_hash_changed()
void set_global_version(Version *version)
void allocate_data_from_scratch(SGVector< index_t > dims, bool new_cont_call=true)
template class SGStringList