Parallel.h

Go to the documentation of this file.
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-2008 Soeren Sonnenburg
00008  * Copyright (C) 1999-2008 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 
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; /* for calling external lib */
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

SHOGUN Machine Learning Toolbox - Documentation