SHOGUN v0.9.0
|
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) 1999-2009 Soeren Sonnenburg 00008 * Copyright (C) 1999-2009 Fraunhofer Institute FIRST and Max-Planck-Society 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; /* for calling external lib */ 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