SHOGUN  v3.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
init.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) 2009 Soeren Sonnenburg
00008  * Copyright (C) 2009 Fraunhofer Institute FIRST and Max-Planck-Society
00009  */
00010 
00011 #include <shogun/base/init.h>
00012 #include <shogun/mathematics/Math.h>
00013 #include <shogun/mathematics/Random.h>
00014 #include <shogun/lib/common.h>
00015 #include <shogun/lib/Map.h>
00016 #include <shogun/base/Parallel.h>
00017 #include <shogun/base/Version.h>
00018 
00019 #ifdef TRACE_MEMORY_ALLOCS
00020 shogun::CMap<void*, shogun::MemoryBlock>* sg_mallocs=NULL;
00021 #endif
00022 
00023 namespace shogun
00024 {
00025     Parallel* sg_parallel=NULL;
00026     SGIO* sg_io=NULL;
00027     Version* sg_version=NULL;
00028     CMath* sg_math=NULL;
00029     CRandom* sg_rand=NULL;
00030 
00032     void (*sg_print_message)(FILE* target, const char* str) = NULL;
00033 
00035     void (*sg_print_warning)(FILE* target, const char* str) = NULL;
00036 
00038     void (*sg_print_error)(FILE* target, const char* str) = NULL;
00039 
00041     void (*sg_cancel_computations)(bool &delayed, bool &immediately)=NULL;
00042 
00043 
00044     void init_shogun(void (*print_message)(FILE* target, const char* str),
00045             void (*print_warning)(FILE* target, const char* str),
00046             void (*print_error)(FILE* target, const char* str),
00047             void (*cancel_computations)(bool &delayed, bool &immediately))
00048     {
00049         if (!sg_io)
00050             sg_io = new shogun::SGIO();
00051         if (!sg_parallel)
00052             sg_parallel=new shogun::Parallel();
00053         if (!sg_version)
00054             sg_version = new shogun::Version();
00055         if (!sg_math)
00056             sg_math = new shogun::CMath();
00057         if (!sg_rand)
00058             sg_rand = new shogun::CRandom();
00059 #ifdef TRACE_MEMORY_ALLOCS
00060         if (!sg_mallocs)
00061             sg_mallocs = new shogun::CMap<void*, MemoryBlock>(631, 1024, false);
00062 
00063         SG_REF(sg_mallocs);
00064 #endif
00065         SG_REF(sg_io);
00066         SG_REF(sg_parallel);
00067         SG_REF(sg_version);
00068         SG_REF(sg_math);
00069         SG_REF(sg_rand);
00070 
00071         sg_print_message=print_message;
00072         sg_print_warning=print_warning;
00073         sg_print_error=print_error;
00074         sg_cancel_computations=cancel_computations;
00075     }
00076 
00077     void sg_global_print_default(FILE* target, const char* str)
00078     {
00079         fprintf(target, "%s", str);
00080     }
00081 
00082     void init_shogun_with_defaults()
00083     {
00084         init_shogun(&sg_global_print_default, &sg_global_print_default,
00085                 &sg_global_print_default);
00086     }
00087 
00088     void exit_shogun()
00089     {
00090 #ifdef TRACE_MEMORY_ALLOCS
00091         list_memory_allocs();
00092         shogun::CMap<void*, shogun::MemoryBlock>* mallocs=sg_mallocs;
00093         sg_mallocs=NULL;
00094         SG_UNREF(mallocs);
00095 #endif
00096         sg_print_message=NULL;
00097         sg_print_warning=NULL;
00098         sg_print_error=NULL;
00099         sg_cancel_computations=NULL;
00100 
00101         SG_UNREF(sg_rand);
00102         SG_UNREF(sg_math);
00103         SG_UNREF(sg_version);
00104         SG_UNREF(sg_parallel);
00105         SG_UNREF(sg_io);
00106 
00107     }
00108 
00109     void set_global_io(SGIO* io)
00110     {
00111         SG_REF(io);
00112         SG_UNREF(sg_io);
00113         sg_io=io;
00114     }
00115 
00116     SGIO* get_global_io()
00117     {
00118         SG_REF(sg_io);
00119         return sg_io;
00120     }
00121 
00122     void set_global_parallel(Parallel* parallel)
00123     {
00124         SG_REF(parallel);
00125         SG_UNREF(sg_parallel);
00126         sg_parallel=parallel;
00127     }
00128 
00129     Parallel* get_global_parallel()
00130     {
00131         SG_REF(sg_parallel);
00132         return sg_parallel;
00133     }
00134 
00135     void set_global_version(Version* version)
00136     {
00137         SG_REF(version);
00138         SG_UNREF(sg_version);
00139         sg_version=version;
00140     }
00141 
00142     Version* get_global_version()
00143     {
00144         SG_REF(sg_version);
00145         return sg_version;
00146     }
00147 
00148     void set_global_math(CMath* math)
00149     {
00150         SG_REF(math);
00151         SG_UNREF(sg_math);
00152         sg_math=math;
00153     }
00154 
00155     CMath* get_global_math()
00156     {
00157         SG_REF(sg_math);
00158         return sg_math;
00159     }
00160 
00161     void set_global_rand(CRandom* rand)
00162     {
00163         SG_REF(rand);
00164         SG_UNREF(sg_rand);
00165         sg_rand=rand;
00166     }
00167 
00168     CRandom* get_global_rand()
00169     {
00170         SG_REF(sg_rand);
00171         return sg_rand;
00172     }
00173 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

SHOGUN Machine Learning Toolbox - Documentation