38 template<>
void CSGObject::set_generic<bool>()
43 template<>
void CSGObject::set_generic<char>()
48 template<>
void CSGObject::set_generic<int8_t>()
53 template<>
void CSGObject::set_generic<uint8_t>()
58 template<>
void CSGObject::set_generic<int16_t>()
63 template<>
void CSGObject::set_generic<uint16_t>()
65 m_generic = PT_UINT16;
68 template<>
void CSGObject::set_generic<int32_t>()
73 template<>
void CSGObject::set_generic<uint32_t>()
75 m_generic = PT_UINT32;
78 template<>
void CSGObject::set_generic<int64_t>()
83 template<>
void CSGObject::set_generic<uint64_t>()
85 m_generic = PT_UINT64;
88 template<>
void CSGObject::set_generic<float32_t>()
90 m_generic = PT_FLOAT32;
93 template<>
void CSGObject::set_generic<float64_t>()
95 m_generic = PT_FLOAT64;
98 template<>
void CSGObject::set_generic<floatmax_t>()
100 m_generic = PT_FLOATMAX;
103 template<>
void CSGObject::set_generic<CSGObject*>()
105 m_generic = PT_SGOBJECT;
108 template<>
void CSGObject::set_generic<complex128_t>()
110 m_generic = PT_COMPLEX128;
115 using namespace shogun;
121 set_global_objects();
125 :
SGRefObject(orig), io(orig.io), parallel(orig.parallel), version(orig.version)
128 set_global_objects();
133 unset_global_objects();
152 void CSGObject::set_global_objects()
156 fprintf(stderr,
"call init_shogun() before using the library, dying.\n");
169 void CSGObject::unset_global_objects()
203 get_parameter_incremental_hash(
m_hash, carry, length);
217 get_parameter_incremental_hash(hash, carry, length);
245 *
generic = m_generic;
257 SG_PRINT(
"\n%s\n================================================================================\n",
get_name())
262 const char* prefix, int32_t param_version)
271 SG_SWARNING(
"%s%s::save_serializable_pre(): ShogunException: "
277 if (!m_save_pre_called)
279 SG_SWARNING(
"%s%s::save_serializable_pre(): Implementation "
280 "error: BASE_CLASS::SAVE_SERIALIZABLE_PRE() not "
286 if (!save_parameter_version(file, prefix, param_version))
298 SG_SWARNING(
"%s%s::save_serializable_post(): ShogunException: "
304 if (!m_save_post_called)
306 SG_SWARNING(
"%s%s::save_serializable_post(): Implementation "
307 "error: BASE_CLASS::SAVE_SERIALIZABLE_POST() not "
312 if (prefix == NULL || *prefix ==
'\0')
321 const char* prefix, int32_t param_version)
330 SG_SWARNING(
"%s%s::load_serializable_pre(): ShogunException: "
335 if (!m_load_pre_called)
337 SG_SWARNING(
"%s%s::load_serializable_pre(): Implementation "
338 "error: BASE_CLASS::LOAD_SERIALIZABLE_PRE() not "
344 int32_t file_version=load_parameter_version(file, prefix);
345 SG_DEBUG(
"file_version=%d, current_version=%d\n", file_version, param_version)
349 SG_WARNING(
"%s%s::load_serializable(): File contains no parameter "
350 "version. Seems like your file is from the days before this "
351 "was introduced. Ignore warning or serialize with this version "
352 "of shogun to get rid of above and this warnings.\n",
356 if (file_version>param_version)
360 SG_WARNING(
"%s%s::load_serializable(): parameter version of file "
361 "larger than the one of shogun. Try with a more recent"
362 "version of shogun.\n", prefix,
get_name());
366 SG_WARNING(
"%s%s::load_serializable(): parameter version of file "
367 "larger than the current. This is probably an implementation"
373 if (file_version==param_version)
384 if (is_param_new(
SGParamInfo(current, param_version)))
387 if (!current->
load(file, prefix))
395 param_version, file, prefix);
405 if (is_param_new(
SGParamInfo(current, param_version)))
426 SG_DEBUG(
"replacing parameter data by loaded/mapped values\n")
430 char* s=SG_MALLOC(
char, 200);
436 if (is_param_new(
SGParamInfo(current, param_version)))
450 SG_DEBUG(
"copying migrated data into parameter\n")
455 SG_DEBUG(
"deleting old parameter base\n")
471 SG_SWARNING(
"%s%s::load_serializable_post(): ShogunException: "
477 if (!m_load_post_called)
479 SG_SWARNING(
"%s%s::load_serializable_post(): Implementation "
480 "error: BASE_CLASS::LOAD_SERIALIZABLE_POST() not "
490 const SGParamInfo* param_info, int32_t file_version,
497 SG_SERROR(
"parameter version of \"%s\" in file (%d) is more recent than"
498 " provided %d!\n", param_info->
m_name, file_version,
506 SG_SDEBUG(
"try to get mapping for: %s\n", s)
510 bool free_mapped=
false;
521 if (file_version<param_info->m_param_version)
559 SG_SDEBUG(
"recursion stop, loading from file\n")
587 if (!loaded->
load(file, prefix))
590 SG_ERROR(
"Could not load %s. The reason for this might be wrong "
591 "parameter mappings\n", s);
595 SG_DEBUG(
"loaded lengths: y=%d, x=%d\n",
613 SG_SDEBUG(
"starting recursion over %s\n", s)
623 SG_SDEBUG(
"appending all results to current result\n")
628 delete recursion_array;
661 if (is_param_new(*info))
694 SG_DEBUG(
"no target parameter infos\n")
708 SG_DEBUG(
"trying to get parameter mapping for %s\n", s)
729 SG_DEBUG(
"no mapping found, using %s\n", s)
748 if (mapped_version>base_version)
760 ASSERT(base_version==mapped_version)
767 SG_DEBUG(
"migrating one step to target: %s\n", s)
774 SG_DEBUG(
"deleting parameters base version %d\n", base_version)
778 SG_DEBUG(
"replacing old parameter base\n")
779 *param_base=*new_base;
780 base_version=mapped_version+1;
792 SG_DEBUG(
"(%d:) \"%s\": sgobject \"%s\" at %p\n", i,
805 SG_DEBUG(
"(%d:) \"%s\": sgobject \"%s\" at %p\n", i,
813 char* s=SG_MALLOC(
char, 200);
837 SG_DEBUG(
"CSGObject::entering CSGObject::one_to_one_migration_prepare() for "
838 "\"%s\"\n", target->
m_name);
845 char* name=target->
m_name;
882 SG_DEBUG(
"copied and SG_REF sgobject pointer for \"%s\" at %p\n",
889 SG_DEBUG(
"CSGObject::leaving CSGObject::one_to_one_migration_prepare() for "
890 "\"%s\"\n", target->
m_name);
921 SG_ERROR(
"Name change for parameter that has to be mapped to \"%s\","
922 " and to no migration method available\n", target->
m_name);
930 char* s=SG_MALLOC(
char, 200);
932 SG_DEBUG(
"nothing changed, using old data: %s\n", s)
962 SG_DEBUG(
"copying content of poitner for non-scalar data\n")
969 SG_ERROR(
"No migration method available for %s!\n", s)
979 const char* prefix, int32_t param_version)
982 TParameter p(&t, ¶m_version,
"version_parameter",
983 "Version of parameters of this object");
984 return p.save(file, prefix);
992 TParameter tp(&t, &v,
"version_parameter",
"");
993 if (tp.load(file, prefix))
1001 m_load_pre_called =
true;
1006 m_load_post_called =
true;
1011 m_save_pre_called =
true;
1016 m_save_post_called =
true;
1019 #ifdef TRACE_MEMORY_ALLOCS
1024 void CSGObject::init()
1026 #ifdef TRACE_MEMORY_ALLOCS
1029 int32_t idx=sg_mallocs->
index_of(
this);
1032 MemoryBlock* b=sg_mallocs->get_element_ptr(idx);
1046 m_load_pre_called =
false;
1047 m_load_post_called =
false;
1048 m_save_pre_called =
false;
1049 m_save_post_called =
false;
1062 for (
index_t i=0; i<num_param; i++)
1066 char* type=SG_MALLOC(
char, l);
1085 for (
index_t i=0; i<num_param; i++)
1092 if (len>max_string_length)
1093 max_string_length=len;
1107 SG_ERROR(
"There is no model selection parameter called \"%s\" for %s",
1125 if (!strcmp(param_name, current->
m_name))
1135 bool CSGObject::is_param_new(
const SGParamInfo param_info)
const
1144 void CSGObject::get_parameter_incremental_hash(uint32_t& hash, uint32_t& carry,
1145 uint32_t& total_length)
1161 child->get_parameter_incremental_hash(hash, carry,
1174 child[j]->get_parameter_incremental_hash(hash, carry,
1227 SG_INFO(
"leaving %s::equals(): name of other object differs\n",
get_name());
1235 SG_INFO(
"leaving %s::equals(): number of parameters of other object "
1242 SG_DEBUG(
"comparing parameter %d\n", i);
1249 if (!this_param && !other_param)
1252 if (!this_param && other_param)
1254 SG_DEBUG(
"leaving %s::equals(): parameter %d is NULL where other's "
1259 if (this_param && !other_param)
1261 SG_DEBUG(
"leaving %s::equals(): parameter %d is \"%s\" where other's "
1266 SG_DEBUG(
"comparing parameter \"%s\" to other's \"%s\"\n",
1270 if (!strcmp(
"DynamicObjectArray",
get_name()) &&
1271 !strcmp(this_param->
m_name,
"num_elements") &&
1272 !strcmp(other_param->
m_name,
"num_elements"))
1274 SG_DEBUG(
"Ignoring DynamicObjectArray::num_elements field\n");
1279 if (!strcmp(
"DynamicArray",
get_name()) &&
1280 !strcmp(this_param->
m_name,
"num_elements") &&
1281 !strcmp(other_param->
m_name,
"num_elements"))
1283 SG_DEBUG(
"Ignoring DynamicArray::num_elements field\n");
1288 if (!this_param->
equals(other_param, accuracy, tolerant))
1290 SG_INFO(
"leaving %s::equals(): parameters at position %d with name"
1291 " \"%s\" differs from other object parameter with name "
1311 REQUIRE(copy,
"Could not create empty instance of \"%s\". The reason for "
1312 "this usually is that get_name() of the class returns something "
1313 "wrong, or that a class has a wrongly set generic type.\n",
1318 SG_DEBUG(
"cloning parameter \"%s\" at index %d\n",
1323 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...
Class SGRefObject is a reference count based memory management class.
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 ...
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()
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