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
00025 class CParallel
00026 {
00027 public:
00028 CParallel();
00029 CParallel(const CParallel& orig);
00030 ~CParallel();
00031
00032 inline INT get_num_cpus() const
00033 {
00034 #if defined(LINUX) && defined(_SC_NPROCESSORS_ONLN)
00035 return sysconf( _SC_NPROCESSORS_ONLN );
00036 #elif defined(DARWIN)
00037 int num;
00038 size_t size=sizeof(num);
00039 if (!sysctlbyname("hw.ncpu", &num, &size, NULL, 0))
00040 return num;
00041 #endif
00042 return 1;
00043 }
00044
00045 inline void set_num_threads(INT n)
00046 {
00047 #ifdef WIN32
00048 ASSERT(n==1);
00049 #endif
00050 num_threads=n;
00051 }
00052
00053 inline INT get_num_threads() const
00054 {
00055 return num_threads;
00056 }
00057
00058 protected:
00059 INT num_threads;
00060 };
00061 #endif