Parallel.h
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
00030 class CParallel
00031 {
00032 public:
00033 CParallel();
00034 CParallel(const CParallel& orig);
00035 ~CParallel();
00036
00037 inline int32_t 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 inline void set_num_threads(int32_t n)
00051 {
00052 #ifdef WIN32
00053 ASSERT(n==1);
00054 #endif
00055 num_threads=n;
00056 }
00057
00058 inline int32_t get_num_threads() const
00059 {
00060 return num_threads;
00061 }
00062
00063 protected:
00064 int32_t num_threads;
00065 };
00066 #endif