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