Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include <shogun/base/Parallel.h>
00012
00013 #if defined(LINUX) && defined(_SC_NPROCESSORS_ONLN)
00014 #include <unistd.h>
00015 #elif defined(DARWIN)
00016 #include <sys/types.h>
00017 #include <sys/sysctl.h>
00018 #endif
00019
00020
00021 using namespace shogun;
00022
00023 Parallel::Parallel() : refcount(0)
00024 {
00025 num_threads=get_num_cpus();
00026 }
00027
00028 Parallel::Parallel(const Parallel& orig) : refcount(0)
00029 {
00030 num_threads=orig.get_num_threads();
00031 }
00032
00033 Parallel::~Parallel()
00034 {
00035 }
00036
00037 int32_t Parallel::get_num_cpus() const
00038 {
00039 #if defined(LINUX) && defined(_SC_NPROCESSORS_ONLN)
00040 return sysconf( _SC_NPROCESSORS_ONLN );
00041 #elif defined(DARWIN)
00042 int num;
00043 size_t size=sizeof(num);
00044 if (!sysctlbyname("hw.ncpu", &num, &size, NULL, 0))
00045 return num;
00046 #endif
00047 return 1;
00048 }
00049
00050 void Parallel::set_num_threads(int32_t n)
00051 {
00052 #ifndef HAVE_PTHREAD
00053 ASSERT(n==1);
00054 #endif
00055 num_threads=n;
00056 }
00057
00058 int32_t Parallel::get_num_threads() const
00059 {
00060 return num_threads;
00061 }
00062
00063 int32_t Parallel::ref()
00064 {
00065 ++refcount;
00066 return refcount;
00067 }
00068
00069 int32_t Parallel::ref_count() const
00070 {
00071 return refcount;
00072 }
00073
00074 int32_t Parallel::unref()
00075 {
00076 if (refcount==0 || --refcount==0)
00077 {
00078 delete this;
00079 return 0;
00080 }
00081 else
00082 return refcount;
00083 }