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-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 
00031 class CParallel
00032 {
00033 public:
00034     CParallel();
00035     CParallel(const CParallel& orig);
00036     virtual ~CParallel();
00037 
00038     inline int32_t get_num_cpus() const
00039     {
00040 #if defined(LINUX) && defined(_SC_NPROCESSORS_ONLN)
00041         return sysconf( _SC_NPROCESSORS_ONLN );
00042 #elif defined(DARWIN)
00043         int num; /* for calling external lib */
00044         size_t size=sizeof(num);
00045         if (!sysctlbyname("hw.ncpu", &num, &size, NULL, 0))
00046             return num;
00047 #endif
00048         return 1;
00049     }
00050 
00051     inline void set_num_threads(int32_t n)
00052     {
00053 #ifdef WIN32
00054         ASSERT(n==1);
00055 #endif
00056         num_threads=n;
00057     }
00058 
00059     inline int32_t get_num_threads() const
00060     {
00061         return num_threads;
00062     }
00063 
00064     inline int32_t ref()
00065     {
00066         ++refcount;
00067         return refcount;
00068     }
00069 
00070     inline int32_t ref_count() const
00071     {
00072         return refcount;
00073     }
00074 
00075     inline int32_t unref()
00076     {
00077         if (refcount==0 || --refcount==0)
00078         {
00079             delete this;
00080             return 0;
00081         }
00082         else
00083             return refcount;
00084     }
00085 
00086 private:
00087     int32_t refcount;
00088     int32_t num_threads;
00089 };
00090 #endif

SHOGUN Machine Learning Toolbox - Documentation