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 
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

SHOGUN Machine Learning Toolbox - Documentation