SHOGUN  v2.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SGObject.cpp
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-2009 Soeren Sonnenburg
8  * Written (W) 2011-2012 Heiko Strathmann
9  * Copyright (C) 2008-2009 Fraunhofer Institute FIRST and Max Planck Society
10  */
11 
12 #include <shogun/lib/config.h>
13 #include <shogun/base/SGObject.h>
14 #include <shogun/io/SGIO.h>
15 #include <shogun/base/Parallel.h>
16 #include <shogun/base/init.h>
17 #include <shogun/base/Version.h>
18 #include <shogun/base/Parameter.h>
20 #include <shogun/base/DynArray.h>
21 #include <shogun/lib/Map.h>
22 
23 
24 
25 #include <stdlib.h>
26 #include <stdio.h>
27 
29 namespace shogun
30 {
31  class CMath;
32  class Parallel;
33  class IO;
34  class Version;
35 
36  extern CMath* sg_math;
37  extern Parallel* sg_parallel;
38  extern SGIO* sg_io;
39  extern Version* sg_version;
40 
41  template<> void CSGObject::set_generic<bool>()
42  {
43  m_generic = PT_BOOL;
44  }
45 
46  template<> void CSGObject::set_generic<char>()
47  {
48  m_generic = PT_CHAR;
49  }
50 
51  template<> void CSGObject::set_generic<int8_t>()
52  {
53  m_generic = PT_INT8;
54  }
55 
56  template<> void CSGObject::set_generic<uint8_t>()
57  {
58  m_generic = PT_UINT8;
59  }
60 
61  template<> void CSGObject::set_generic<int16_t>()
62  {
63  m_generic = PT_INT16;
64  }
65 
66  template<> void CSGObject::set_generic<uint16_t>()
67  {
68  m_generic = PT_UINT16;
69  }
70 
71  template<> void CSGObject::set_generic<int32_t>()
72  {
73  m_generic = PT_INT32;
74  }
75 
76  template<> void CSGObject::set_generic<uint32_t>()
77  {
78  m_generic = PT_UINT32;
79  }
80 
81  template<> void CSGObject::set_generic<int64_t>()
82  {
83  m_generic = PT_INT64;
84  }
85 
86  template<> void CSGObject::set_generic<uint64_t>()
87  {
88  m_generic = PT_UINT64;
89  }
90 
91  template<> void CSGObject::set_generic<float32_t>()
92  {
93  m_generic = PT_FLOAT32;
94  }
95 
96  template<> void CSGObject::set_generic<float64_t>()
97  {
98  m_generic = PT_FLOAT64;
99  }
100 
101  template<> void CSGObject::set_generic<floatmax_t>()
102  {
103  m_generic = PT_FLOATMAX;
104  }
105 
106 } /* namespace shogun */
107 
108 using namespace shogun;
109 
111 {
112  init();
113  set_global_objects();
114 
115  SG_GCDEBUG("SGObject created (%p)\n", this);
116 }
117 
119 :io(orig.io), parallel(orig.parallel), version(orig.version)
120 {
121  init();
122  set_global_objects();
123 }
124 
126 {
127  SG_GCDEBUG("SGObject destroyed (%p)\n", this);
128 
129 #ifdef HAVE_PTHREAD
130  PTHREAD_LOCK_DESTROY(&m_ref_lock);
131 #endif
132  unset_global_objects();
133  delete m_parameters;
135  delete m_parameter_map;
136 }
137 
138 #ifdef USE_REFERENCE_COUNTING
139 
140 int32_t CSGObject::ref()
141 {
142 #ifdef HAVE_PTHREAD
143  PTHREAD_LOCK(&m_ref_lock);
144 #endif //HAVE_PTHREAD
145  ++m_refcount;
146  int32_t count=m_refcount;
147 #ifdef HAVE_PTHREAD
148  PTHREAD_UNLOCK(&m_ref_lock);
149 #endif //HAVE_PTHREAD
150  SG_GCDEBUG("ref() refcount %ld obj %s (%p) increased\n", count, this->get_name(), this);
151  return m_refcount;
152 }
153 
154 int32_t CSGObject::ref_count()
155 {
156 #ifdef HAVE_PTHREAD
157  PTHREAD_LOCK(&m_ref_lock);
158 #endif //HAVE_PTHREAD
159  int32_t count=m_refcount;
160 #ifdef HAVE_PTHREAD
161  PTHREAD_UNLOCK(&m_ref_lock);
162 #endif //HAVE_PTHREAD
163  SG_GCDEBUG("ref_count(): refcount %d, obj %s (%p)\n", count, this->get_name(), this);
164  return count;
165 }
166 
167 int32_t CSGObject::unref()
168 {
169 #ifdef HAVE_PTHREAD
170  PTHREAD_LOCK(&m_ref_lock);
171 #endif //HAVE_PTHREAD
172  if (m_refcount==0 || --m_refcount==0)
173  {
174  SG_GCDEBUG("unref() refcount %ld, obj %s (%p) destroying\n", m_refcount, this->get_name(), this);
175 #ifdef HAVE_PTHREAD
176  PTHREAD_UNLOCK(&m_ref_lock);
177 #endif //HAVE_PTHREAD
178  delete this;
179  return 0;
180  }
181  else
182  {
183  SG_GCDEBUG("unref() refcount %ld obj %s (%p) decreased\n", m_refcount, this->get_name(), this);
184 #ifdef HAVE_PTHREAD
185  PTHREAD_UNLOCK(&m_ref_lock);
186 #endif //HAVE_PTHREAD
187  return m_refcount;
188  }
189 }
190 #endif //USE_REFERENCE_COUNTING
191 
192 
193 void CSGObject::set_global_objects()
194 {
195  if (!sg_io || !sg_parallel || !sg_version)
196  {
197  fprintf(stderr, "call init_shogun() before using the library, dying.\n");
198  exit(1);
199  }
200 
201  SG_REF(sg_io);
204 
205  io=sg_io;
208 }
209 
210 void CSGObject::unset_global_objects()
211 {
212  SG_UNREF(version);
214  SG_UNREF(io);
215 }
216 
218 {
219  SG_UNREF(sg_io);
220  sg_io=new_io;
221  SG_REF(sg_io);
222 }
223 
225 {
226  SG_REF(sg_io);
227  return sg_io;
228 }
229 
231 {
233  sg_parallel=new_parallel;
235 }
236 
238 {
239  uint32_t new_hash = 0;
240  uint32_t carry = 0;
241  uint32_t length = 0;
242 
243  get_parameter_incremental_hash(m_parameters, new_hash,
244  carry, length);
245 
246  new_hash = CHash::FinalizeIncrementalMurmurHash3(new_hash,
247  carry, length);
248 
249  if(new_hash != m_hash)
250  {
251  m_hash = new_hash;
252  return true;
253  }
254 
255  else
256  return false;
257 }
258 
260 {
262  return sg_parallel;
263 }
264 
266 {
268  sg_version=new_version;
270 }
271 
273 {
275  return sg_version;
276 }
277 
278 bool CSGObject::is_generic(EPrimitiveType* generic) const
279 {
280  *generic = m_generic;
281 
282  return m_generic != PT_NOT_GENERIC;
283 }
284 
286 {
287  m_generic = PT_NOT_GENERIC;
288 }
289 
290 void CSGObject::print_serializable(const char* prefix)
291 {
292  SG_PRINT("\n%s\n================================================================================\n", get_name());
293  m_parameters->print(prefix);
294 }
295 
297  const char* prefix, int32_t param_version)
298 {
299  SG_DEBUG("START SAVING CSGObject '%s'\n", get_name());
300  try
301  {
303  }
304  catch (ShogunException e)
305  {
306  SG_SWARNING("%s%s::save_serializable_pre(): ShogunException: "
307  "%s\n", prefix, get_name(),
309  return false;
310  }
311  if (!m_save_pre_called)
312  {
313  SG_SWARNING("%s%s::save_serializable_pre(): Implementation "
314  "error: BASE_CLASS::LOAD_SERIALIZABLE_PRE() not "
315  "called!\n", prefix, get_name());
316  return false;
317  }
318 
319  /* save parameter version */
320  if (!save_parameter_version(file, prefix, param_version))
321  return false;
322 
323  if (!m_parameters->save(file, prefix))
324  return false;
325 
326  try
327  {
329  }
330  catch (ShogunException e)
331  {
332  SG_SWARNING("%s%s::save_serializable_post(): ShogunException: "
333  "%s\n", prefix, get_name(),
335  return false;
336  }
337 
338  if (!m_save_post_called)
339  {
340  SG_SWARNING("%s%s::save_serializable_post(): Implementation "
341  "error: BASE_CLASS::LOAD_SERIALIZABLE_POST() not "
342  "called!\n", prefix, get_name());
343  return false;
344  }
345 
346  if (prefix == NULL || *prefix == '\0')
347  file->close();
348 
349  SG_DEBUG("DONE SAVING CSGObject '%s' (%p)\n", get_name(), this);
350 
351  return true;;
352 }
353 
355  const char* prefix, int32_t param_version)
356 {
357  SG_DEBUG("START LOADING CSGObject '%s'\n", get_name());
358  try
359  {
361  }
362  catch (ShogunException e)
363  {
364  SG_SWARNING("%s%s::load_serializable_pre(): ShogunException: "
365  "%s\n", prefix, get_name(),
367  return false;
368  }
369  if (!m_load_pre_called)
370  {
371  SG_SWARNING("%s%s::load_serializable_pre(): Implementation "
372  "error: BASE_CLASS::LOAD_SERIALIZABLE_PRE() not "
373  "called!\n", prefix, get_name());
374  return false;
375  }
376 
377  /* try to load version of parameters */
378  int32_t file_version=load_parameter_version(file, prefix);
379  SG_DEBUG("file_version=%d, current_version=%d\n", file_version, param_version);
380 
381  if (file_version<0)
382  {
383  SG_WARNING("%s%s::load_serializable(): File contains no parameter "
384  "version. Seems like your file is from the days before this "
385  "was introduced. Ignore warning or serialize with this version "
386  "of shogun to get rid of above and this warnings.\n",
387  prefix, get_name());
388  }
389 
390  if (file_version>param_version)
391  {
392  if (param_version==VERSION_PARAMETER)
393  {
394  SG_WARNING("%s%s::load_serializable(): parameter version of file "
395  "larger than the one of shogun. Try with a more recent"
396  "version of shogun.\n", prefix, get_name());
397  }
398  else
399  {
400  SG_WARNING("%s%s::load_serializable(): parameter version of file "
401  "larger than the current. This is probably an implementation"
402  " error.\n", prefix, get_name());
403  }
404  return false;
405  }
406 
407  if (file_version==param_version)
408  {
409  /* load normally if file has current version */
410  SG_DEBUG("loading normally\n");
411 
412  /* load all parameters, except new ones */
413  for (int32_t i=0; i<m_parameters->get_num_parameters(); i++)
414  {
416 
417  /* skip new parameters */
418  if (is_param_new(SGParamInfo(current, param_version)))
419  continue;
420 
421  if (!current->load(file, prefix))
422  return false;
423  }
424  }
425  else
426  {
427  /* load all parameters from file, mappings to current version */
428  DynArray<TParameter*>* param_base=load_all_file_parameters(file_version,
429  param_version, file, prefix);
430 
431  /* create an array of param infos from current parameters */
432  DynArray<const SGParamInfo*>* param_infos=
434  for (index_t i=0; i<m_parameters->get_num_parameters(); ++i)
435  {
437 
438  /* skip new parameters */
439  if (is_param_new(SGParamInfo(current, param_version)))
440  continue;
441 
442  param_infos->append_element(
443  new SGParamInfo(current, param_version));
444  }
445 
446  /* map all parameters, result may be empty if input is */
447  map_parameters(param_base, file_version, param_infos);
448  SG_DEBUG("mapping is done!\n");
449 
450  /* this is assumed now, mapping worked or no parameters in base */
451  ASSERT(file_version==param_version || !param_base->get_num_elements());
452 
453  /* delete above created param infos */
454  for (index_t i=0; i<param_infos->get_num_elements(); ++i)
455  delete param_infos->get_element(i);
456 
457  delete param_infos;
458 
459  /* replace parameters by loaded and mapped */
460  SG_DEBUG("replacing parameter data by loaded/mapped values\n");
461  for (index_t i=0; i<m_parameters->get_num_parameters(); ++i)
462  {
464  char* s=SG_MALLOC(char, 200);
465  current->m_datatype.to_string(s, 200);
466  SG_DEBUG("processing \"%s\": %s\n", current->m_name, s);
467  SG_FREE(s);
468 
469  /* skip new parameters */
470  if (is_param_new(SGParamInfo(current, param_version)))
471  {
472  SG_DEBUG("%s is new, skipping\n", current->m_name);
473  continue;
474  }
475 
476  /* search for current parameter in mapped ones */
477  index_t index=CMath::binary_search(param_base->get_array(),
478  param_base->get_num_elements(), current);
479 
480  TParameter* migrated=param_base->get_element(index);
481 
482  /* now copy data from migrated TParameter instance
483  * (this automatically deletes the old data allocations) */
484  SG_DEBUG("copying migrated data into parameter\n");
485  current->copy_data(migrated);
486  }
487 
488  /* delete the migrated parameter data base */
489  SG_DEBUG("deleting old parameter base\n");
490  for (index_t i=0; i<param_base->get_num_elements(); ++i)
491  {
492  TParameter* current=param_base->get_element(i);
493  SG_DEBUG("deleting old \"%s\"\n", current->m_name);
494  delete current;
495  }
496  delete param_base;
497  }
498 
499  try
500  {
502  }
503  catch (ShogunException e)
504  {
505  SG_SWARNING("%s%s::load_serializable_post(): ShogunException: "
506  "%s\n", prefix, get_name(),
508  return false;
509  }
510 
511  if (!m_load_post_called)
512  {
513  SG_SWARNING("%s%s::load_serializable_post(): Implementation "
514  "error: BASE_CLASS::LOAD_SERIALIZABLE_POST() not "
515  "called!\n", prefix, get_name());
516  return false;
517  }
518  SG_DEBUG("DONE LOADING CSGObject '%s' (%p)\n", get_name(), this);
519 
520  return true;
521 }
522 
524  const SGParamInfo* param_info, int32_t file_version,
525  CSerializableFile* file, const char* prefix)
526 {
527  /* ensure that recursion works */
528  SG_SDEBUG("entering %s::load_file_parameters\n", get_name());
529  if (file_version>param_info->m_param_version)
530  {
531  SG_SERROR("parameter version of \"%s\" in file (%d) is more recent than"
532  " provided %d!\n", param_info->m_name, file_version,
533  param_info->m_param_version);
534  }
535 
536  DynArray<TParameter*>* result_array=new DynArray<TParameter*>();
537 
538  /* do mapping */
539  char* s=param_info->to_string();
540  SG_SDEBUG("try to get mapping for: %s\n", s);
541  SG_FREE(s);
542 
543  /* mapping has only be deleted if was created here (no mapping was found) */
544  bool free_mapped=false;
545  DynArray<const SGParamInfo*>* mapped=m_parameter_map->get(param_info);
546  if (!mapped)
547  {
548  /* since a new mapped array will be created, set deletion flag */
549  free_mapped=true;
550  mapped=new DynArray<const SGParamInfo*>();
551 
552  /* if no mapping was found, nothing has changed. Simply create new param
553  * info with decreased version */
554  SG_SDEBUG("no mapping found\n");
555  if (file_version<param_info->m_param_version)
556  {
557  /* create new array and put param info with decreased version in */
558  mapped->append_element(new SGParamInfo(param_info->m_name,
559  param_info->m_ctype, param_info->m_stype,
560  param_info->m_ptype, param_info->m_param_version-1));
561 
562  SG_SDEBUG("using:\n");
563  for (index_t i=0; i<mapped->get_num_elements(); ++i)
564  {
565  s=mapped->get_element(i)->to_string();
566  SG_SDEBUG("\t%s\n", s);
567  SG_FREE(s);
568  }
569  }
570  else
571  {
572  /* create new array and put original param info in */
573  SG_SDEBUG("reached file version\n");
574  mapped->append_element(param_info->duplicate());
575  }
576  }
577  else
578  {
579  SG_SDEBUG("found:\n");
580  for (index_t i=0; i<mapped->get_num_elements(); ++i)
581  {
582  s=mapped->get_element(i)->to_string();
583  SG_SDEBUG("\t%s\n", s);
584  SG_FREE(s);
585  }
586  }
587 
588 
589  /* case file version same as provided version.
590  * means that parameters have to be loaded from file, recursion stops */
591  if (file_version==param_info->m_param_version)
592  {
593  SG_SDEBUG("recursion stop, loading from file\n");
594  /* load all parameters in mapping from file */
595  for (index_t i=0; i<mapped->get_num_elements(); ++i)
596  {
597  const SGParamInfo* current=mapped->get_element(i);
598  s=current->to_string();
599  SG_SDEBUG("loading %s\n", s);
600  SG_FREE(s);
601 
602  TParameter* loaded;
603  /* allocate memory for length and matrix/vector
604  * This has to be done because this stuff normally is in the class
605  * variables which do not exist in this case. Deletion is handled
606  * via the allocated_from_scratch flag of TParameter */
607 
608  /* create type and copy lengths, empty data for now */
609  TSGDataType type(current->m_ctype, current->m_stype,
610  current->m_ptype);
611  loaded=new TParameter(&type, NULL, current->m_name, "");
612 
613  /* allocate data/length variables for the TParameter, lengths are not
614  * important now, so set to one */
615  loaded->allocate_data_from_scratch(1, 1);
616 
617  /* tell instance to load data from file */
618  if (!loaded->load(file, prefix))
619  {
620  s=param_info->to_string();
621  SG_ERROR("Could not load %s. The reason for this might be wrong "
622  "parameter mappings\n", s);
623  SG_FREE(s);
624  }
625 
626  SG_DEBUG("loaded lengths: y=%d, x=%d\n",
627  loaded->m_datatype.m_length_y ? *loaded->m_datatype.m_length_y : -1,
628  loaded->m_datatype.m_length_x ? *loaded->m_datatype.m_length_x : -1);
629 
630  /* append new TParameter to result array */
631  result_array->append_element(loaded);
632  }
633  SG_SDEBUG("done loading from file\n");
634  }
635  /* recursion with mapped type, a mapping exists in this case (ensured by
636  * above assert) */
637  else
638  {
639  /* for all elements in mapping, do recursion */
640  for (index_t i=0; i<mapped->get_num_elements(); ++i)
641  {
642  const SGParamInfo* current=mapped->get_element(i);
643  s=current->to_string();
644  SG_SDEBUG("starting recursion over %s\n", s);
645 
646  /* recursively get all file parameters for this parameter */
647  DynArray<TParameter*>* recursion_array=
648  load_file_parameters(current, file_version, file, prefix);
649 
650  SG_SDEBUG("recursion over %s done\n", s);
651  SG_FREE(s);
652 
653  /* append all recursion data to current array */
654  SG_SDEBUG("appending all results to current result\n");
655  for (index_t j=0; j<recursion_array->get_num_elements(); ++j)
656  result_array->append_element(recursion_array->get_element(j));
657 
658  /* clean up */
659  delete recursion_array;
660  }
661  }
662 
663  SG_SDEBUG("cleaning up old mapping \n");
664 
665 
666  /* clean up mapping */
667  if (free_mapped)
668  {
669  for (index_t i=0; i<mapped->get_num_elements(); ++i)
670  delete mapped->get_element(i);
671 
672  delete mapped;
673  }
674 
675  SG_SDEBUG("leaving %s::load_file_parameters\n", get_name());
676  return result_array;
677 }
678 
680  int32_t current_version, CSerializableFile* file, const char* prefix)
681 {
683 
684  for (index_t i=0; i<m_parameters->get_num_parameters(); ++i)
685  {
687 
688  /* extract current parameter info */
689  const SGParamInfo* info=new SGParamInfo(current, current_version);
690 
691  /* skip new parameters */
692  if (is_param_new(*info))
693  {
694  delete info;
695  continue;
696  }
697 
698  /* in the other case, load parameters data from file */
699  DynArray<TParameter*>* temp=load_file_parameters(info, file_version,
700  file, prefix);
701 
702  /* and append them all to array */
703  for (index_t j=0; j<temp->get_num_elements(); ++j)
704  result->append_element(temp->get_element(j));
705 
706  /* clean up */
707  delete temp;
708  delete info;
709  }
710 
711  /* sort array before returning */
712  CMath::qsort(result->get_array(), result->get_num_elements());
713 
714  return result;
715 }
716 
718  int32_t& base_version, DynArray<const SGParamInfo*>* target_param_infos)
719 {
720  SG_DEBUG("entering %s::map_parameters\n", get_name());
721  /* NOTE: currently the migration is done step by step over every version */
722 
723  if (!target_param_infos->get_num_elements())
724  {
725  SG_DEBUG("no target parameter infos\n");
726  SG_DEBUG("leaving %s::map_parameters\n", get_name());
727  return;
728  }
729 
730  /* map all target parameter infos once */
731  DynArray<const SGParamInfo*>* mapped_infos=
734  for (index_t i=0; i<target_param_infos->get_num_elements(); ++i)
735  {
736  const SGParamInfo* current=target_param_infos->get_element(i);
737 
738  char* s=current->to_string();
739  SG_DEBUG("trying to get parameter mapping for %s\n", s);
740  SG_FREE(s);
741 
743 
744  if (mapped)
745  {
746  mapped_infos->append_element(mapped->get_element(0));
747  for (index_t j=0; j<mapped->get_num_elements(); ++j)
748  {
749  s=mapped->get_element(j)->to_string();
750  SG_DEBUG("found mapping: %s\n", s);
751  SG_FREE(s);
752  }
753  }
754  else
755  {
756  /* these have to be deleted above */
757  SGParamInfo* no_change=new SGParamInfo(*current);
758  no_change->m_param_version--;
759  s=no_change->to_string();
760  SG_DEBUG("no mapping found, using %s\n", s);
761  SG_FREE(s);
762  mapped_infos->append_element(no_change);
763  to_delete->append_element(no_change);
764  }
765  }
766 
767  /* assert that at least one mapping exists */
768  ASSERT(mapped_infos->get_num_elements());
769  int32_t mapped_version=mapped_infos->get_element(0)->m_param_version;
770 
771  /* assert that all param versions are equal for now (if not empty param) */
772  for (index_t i=1; i<mapped_infos->get_num_elements(); ++i)
773  {
774  ASSERT(mapped_infos->get_element(i)->m_param_version==mapped_version ||
775  *mapped_infos->get_element(i)==SGParamInfo());
776  }
777 
778  /* recursion, after this call, base is at version of mapped infos */
779  if (mapped_version>base_version)
780  map_parameters(param_base, base_version, mapped_infos);
781 
782  /* delete mapped parameter infos array */
783  delete mapped_infos;
784 
785  /* delete newly created parameter infos which have to name or type change */
786  for (index_t i=0; i<to_delete->get_num_elements(); ++i)
787  delete to_delete->get_element(i);
788 
789  delete to_delete;
790 
791  ASSERT(base_version==mapped_version);
792 
793  /* do migration of one version step, create new base */
795  for (index_t i=0; i<target_param_infos->get_num_elements(); ++i)
796  {
797  char* s=target_param_infos->get_element(i)->to_string();
798  SG_DEBUG("migrating one step to target: %s\n", s);
799  SG_FREE(s);
800  TParameter* p=migrate(param_base, target_param_infos->get_element(i));
801  new_base->append_element(p);
802  }
803 
804  /* replace base by new base, delete old base, if it was created in migrate */
805  SG_DEBUG("deleting parameters base version %d\n", base_version);
806  for (index_t i=0; i<param_base->get_num_elements(); ++i)
807  delete param_base->get_element(i);
808 
809  SG_DEBUG("replacing old parameter base\n");
810  *param_base=*new_base;
811  base_version=mapped_version+1;
812 
813  SG_DEBUG("new parameter base of size %d:\n", param_base->get_num_elements());
814  for (index_t i=0; i<param_base->get_num_elements(); ++i)
815  {
816  TParameter* current=param_base->get_element(i);
817  TSGDataType type=current->m_datatype;
818  if (type.m_ptype==PT_SGOBJECT)
819  {
820  if (type.m_ctype==CT_SCALAR)
821  {
822  CSGObject* object=*(CSGObject**)current->m_parameter;
823  SG_DEBUG("(%d:) \"%s\": sgobject \"%s\" at %p\n", i,
824  current->m_name, object ? object->get_name() : "",
825  object);
826  }
827  else
828  {
829  index_t len=1;
830  len*=type.m_length_x ? *type.m_length_x : 1;
831  len*=type.m_length_y ? *type.m_length_y : 1;
832  CSGObject** array=*(CSGObject***)current->m_parameter;
833  for (index_t j=0; j<len; ++j)
834  {
835  CSGObject* object=array[j];
836  SG_DEBUG("(%d:) \"%s\": sgobject \"%s\" at %p\n", i,
837  current->m_name, object ? object->get_name() : "",
838  object);
839  }
840  }
841  }
842  else
843  {
844  char* s=SG_MALLOC(char, 200);
845  current->m_datatype.to_string(s, 200);
846  SG_DEBUG("(%d:) \"%s\": type: %s at %p\n", i, current->m_name, s,
847  current->m_parameter);
848  SG_FREE(s);
849  }
850  }
851 
852  /* because content was copied, new base may be deleted */
853  delete new_base;
854 
855  /* sort the just created new base */
856  SG_DEBUG("sorting base\n");
857  CMath::qsort(param_base->get_array(), param_base->get_num_elements());
858 
859  /* at this point the param_base is at the same version as the version of
860  * the provided parameter infos */
861  SG_DEBUG("leaving %s::map_parameters\n", get_name());
862 }
863 
865  const SGParamInfo* target, TParameter*& replacement,
866  TParameter*& to_migrate, char* old_name)
867 {
868  SG_DEBUG("CSGObject::entering CSGObject::one_to_one_migration_prepare() for "
869  "\"%s\"\n", target->m_name);
870 
871  /* generate type of target structure */
872  TSGDataType type(target->m_ctype, target->m_stype, target->m_ptype);
873 
874  /* first find index of needed data.
875  * in this case, element in base with same name or old name */
876  char* name=target->m_name;
877  if (old_name)
878  name=old_name;
879 
880  /* dummy for searching, search and save result in to_migrate parameter */
881  TParameter* t=new TParameter(&type, NULL, name, "");
882  index_t i=CMath::binary_search(param_base->get_array(),
883  param_base->get_num_elements(), t);
884  delete t;
885 
886  /* assert that something is found */
887  ASSERT(i>=0);
888  to_migrate=param_base->get_element(i);
889 
890  /* result structure, data NULL for now */
891  replacement=new TParameter(&type, NULL, target->m_name,
892  to_migrate->m_description);
893 
894  /* allocate content to write into, lengths are needed for this */
895  index_t len_x=1;
896  if (to_migrate->m_datatype.m_length_x)
897  len_x=*to_migrate->m_datatype.m_length_x;
898 
899  index_t len_y=1;
900  if (to_migrate->m_datatype.m_length_y)
901  len_y=*to_migrate->m_datatype.m_length_y;
902 
903  replacement->allocate_data_from_scratch(len_y, len_x);
904 
905  /* in case of sgobject, copy pointer data and SG_REF */
906  if (to_migrate->m_datatype.m_ptype==PT_SGOBJECT)
907  {
908  /* note that the memory is already allocated before the migrate call */
909  CSGObject* object=*((CSGObject**)to_migrate->m_parameter);
910  *((CSGObject**)replacement->m_parameter)=object;
911  SG_REF(object);
912  SG_DEBUG("copied and SG_REF sgobject pointer for \"%s\" at %p\n",
913  object->get_name(), object);
914  }
915 
916  /* tell the old TParameter to delete its data on deletion */
917  to_migrate->m_delete_data=true;
918 
919  SG_DEBUG("CSGObject::leaving CSGObject::one_to_one_migration_prepare() for "
920  "\"%s\"\n", target->m_name);
921 }
922 
924  const SGParamInfo* target)
925 {
926  SG_DEBUG("entering %s::migrate\n", get_name());
927  /* this is only executed, iff there was no migration method which handled
928  * migration to the provided target. In this case, it is assumed that the
929  * parameter simply has not changed. Verify this here and return copy of
930  * data in case its true.
931  * If not, throw an exception -- parameter migration HAS to be implemented
932  * by hand everytime, a parameter changes type or name. */
933 
934  TParameter* result=NULL;
935 
936  /* first find index of needed data.
937  * in this case, element in base with same name */
938  /* type is also needed */
939  TSGDataType type(target->m_ctype, target->m_stype,
940  target->m_ptype);
941 
942  /* dummy for searching, search and save result */
943  TParameter* t=new TParameter(&type, NULL, target->m_name, "");
944  index_t i=CMath::binary_search(param_base->get_array(),
945  param_base->get_num_elements(), t);
946  delete t;
947 
948  /* check if name change occurred while no migration method was specified */
949  if (i<0)
950  {
951  SG_ERROR("Name change for parameter that has to be mapped to \"%s\","
952  " and to no migration method available\n", target->m_name);
953  }
954 
955  TParameter* to_migrate=param_base->get_element(i);
956 
957  /* check if element in base is equal to target one */
958  if (*target==SGParamInfo(to_migrate, target->m_param_version))
959  {
960  char* s=SG_MALLOC(char, 200);
961  to_migrate->m_datatype.to_string(s, 200);
962  SG_DEBUG("nothing changed, using old data: %s\n", s);
963  SG_FREE(s);
964  result=new TParameter(&to_migrate->m_datatype, NULL, to_migrate->m_name,
965  to_migrate->m_description);
966 
967  int len_x=1;
968  if (to_migrate->m_datatype.m_length_x)
969  len_x=*to_migrate->m_datatype.m_length_x;
970 
971  int len_y=1;
972  if (to_migrate->m_datatype.m_length_y)
973  len_y=*to_migrate->m_datatype.m_length_y;
974 
975  /* allocate lengths and evtl scalar data but not non-scalar data (no
976  * new_cont call */
977  result->allocate_data_from_scratch(len_y, len_x, false);
978 
979  /* now use old data */
980  if (to_migrate->m_datatype.m_ctype==CT_SCALAR &&
981  to_migrate->m_datatype.m_ptype!=PT_SGOBJECT)
982  {
983  /* copy data */
984  SG_DEBUG("copying scalar data\n");
985  memcpy(result->m_parameter,to_migrate->m_parameter,
986  to_migrate->m_datatype.get_size());
987  }
988  else
989  {
990  /* copy content of pointer */
991  SG_DEBUG("copying content of poitner for non-scalar data\n");
992  *(void**)result->m_parameter=*(void**)(to_migrate->m_parameter);
993  }
994  }
995  else
996  {
997  char* s=target->to_string();
998  SG_ERROR("No migration method available for %s!\n", s);
999  SG_FREE(s);
1000  }
1001 
1002  SG_DEBUG("leaving %s::migrate\n", get_name());
1003 
1004  return result;
1005 }
1006 
1007 bool CSGObject::save_parameter_version(CSerializableFile* file,
1008  const char* prefix, int32_t param_version)
1009 {
1010  TSGDataType t(CT_SCALAR, ST_NONE, PT_INT32);
1011  TParameter p(&t, &param_version, "version_parameter",
1012  "Version of parameters of this object");
1013  return p.save(file, prefix);
1014 }
1015 
1016 int32_t CSGObject::load_parameter_version(CSerializableFile* file,
1017  const char* prefix)
1018 {
1019  TSGDataType t(CT_SCALAR, ST_NONE, PT_INT32);
1020  int32_t v;
1021  TParameter tp(&t, &v, "version_parameter", "");
1022  if (tp.load(file, prefix))
1023  return v;
1024  else
1025  return -1;
1026 }
1027 
1029 {
1030  m_load_pre_called = true;
1031 }
1032 
1034 {
1035  m_load_post_called = true;
1036 }
1037 
1039 {
1040  m_save_pre_called = true;
1041 }
1042 
1044 {
1045  m_save_post_called = true;
1046 }
1047 
1048 #ifdef TRACE_MEMORY_ALLOCS
1049 #include <shogun/lib/Map.h>
1050 extern CMap<void*, shogun::MemoryBlock>* sg_mallocs;
1051 #endif
1052 
1053 void CSGObject::init()
1054 {
1055 #ifdef HAVE_PTHREAD
1056  PTHREAD_LOCK_INIT(&m_ref_lock);
1057 #endif
1058 
1059 #ifdef TRACE_MEMORY_ALLOCS
1060  if (sg_mallocs)
1061  {
1062  int32_t idx=sg_mallocs->index_of(this);
1063  if (idx>-1)
1064  {
1065  MemoryBlock* b=sg_mallocs->get_element_ptr(idx);
1066  b->set_sgobject();
1067  }
1068  }
1069 #endif
1070 
1071  m_refcount = 0;
1072  io = NULL;
1073  parallel = NULL;
1074  version = NULL;
1075  m_parameters = new Parameter();
1078  m_generic = PT_NOT_GENERIC;
1079  m_load_pre_called = false;
1080  m_load_post_called = false;
1081  m_hash = 0;
1082 }
1083 
1085 {
1086  SG_PRINT("parameters available for model selection for %s:\n", get_name());
1087 
1089 
1090  if (!num_param)
1091  SG_PRINT("\tnone\n");
1092 
1093  for (index_t i=0; i<num_param; i++)
1094  {
1096  index_t l=200;
1097  char* type=SG_MALLOC(char, l);
1098  if (type)
1099  {
1100  current->m_datatype.to_string(type, l);
1101  SG_PRINT("\t%s (%s): %s\n", current->m_name, current->m_description,
1102  type);
1103  SG_FREE(type);
1104  }
1105  }
1106 }
1107 
1109 {
1111 
1112  SGStringList<char> result(num_param, -1);
1113 
1114  index_t max_string_length=-1;
1115 
1116  for (index_t i=0; i<num_param; i++)
1117  {
1119  index_t len=strlen(name);
1120  // +1 to have a zero terminated string
1121  result.strings[i]=SGString<char>(name, len+1);
1122 
1123  if (len>max_string_length)
1124  max_string_length=len;
1125  }
1126 
1127  result.max_string_length=max_string_length;
1128 
1129  return result;
1130 }
1131 
1132 char* CSGObject::get_modsel_param_descr(const char* param_name)
1133 {
1134  index_t index=get_modsel_param_index(param_name);
1135 
1136  if (index<0)
1137  {
1138  SG_ERROR("There is no model selection parameter called \"%s\" for %s",
1139  param_name, get_name());
1140  }
1141 
1143 }
1144 
1146 {
1147  /* use fact that names extracted from below method are in same order than
1148  * in m_model_selection_parameters variable */
1150 
1151  /* search for parameter with provided name */
1152  index_t index=-1;
1153  for (index_t i=0; i<names.num_strings; i++)
1154  {
1156  if (!strcmp(param_name, current->m_name))
1157  {
1158  index=i;
1159  break;
1160  }
1161  }
1162 
1163  /* clean up */
1164  names.destroy_list();
1165 
1166  return index;
1167 }
1168 
1169 bool CSGObject::is_param_new(const SGParamInfo param_info) const
1170 {
1171  /* check if parameter is new in this version (has empty mapping) */
1172  DynArray<const SGParamInfo*>* value=m_parameter_map->get(&param_info);
1173  bool result=value && *value->get_element(0) == SGParamInfo();
1174 
1175  return result;
1176 }
1177 
1178 void CSGObject::get_parameter_incremental_hash(Parameter* param,
1179  uint32_t& hash, uint32_t& carry, uint32_t& total_length)
1180 {
1181  if (param)
1182  {
1183  for (index_t i=0; i<param->get_num_parameters(); i++)
1184  {
1185  TParameter* p = param->get_parameter(i);
1186 
1187  if (p->m_datatype.m_ptype == PT_SGOBJECT)
1188  {
1189  CSGObject* child =
1190  *((CSGObject**)(p->m_parameter));
1191 
1192  if (child)
1193  get_parameter_incremental_hash(
1194  child->m_parameters, hash,
1195  carry, total_length);
1196  }
1197 
1198  else
1199  p->get_incremental_hash(hash, carry, total_length);
1200  }
1201  }
1202 }
1203 
1205 {
1206  if (m_parameters)
1207  {
1208  for (index_t i=0; i<m_parameters->get_num_parameters(); i++)
1209  {
1211 
1212  dict.add(p, this);
1213 
1214  if (p->m_datatype.m_ptype == PT_SGOBJECT)
1215  {
1216  CSGObject* child =
1217  *((CSGObject**)(p->m_parameter));
1218  if (child)
1219  child->build_parameter_dictionary(dict);
1220  }
1221 
1222  }
1223  }
1224 }
1225 

SHOGUN Machine Learning Toolbox - Documentation