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 <shogun/lib/config.h>
00015 #include <shogun/lib/common.h>
00016 #include <shogun/io/SGIO.h>
00017 
00018 #ifdef HAVE_PTHREAD
00019 #ifdef USE_SPINLOCKS
00020     #define PTHREAD_LOCK_T pthread_spinlock_t
00021     #define PTHREAD_LOCK_INIT(lock) pthread_spin_init(lock, 0)
00022     #define PTHREAD_LOCK_DESTROY(lock) pthread_spin_destroy(lock)
00023     #define PTHREAD_LOCK(lock) pthread_spin_lock(lock)
00024     #define PTHREAD_UNLOCK(lock) pthread_spin_unlock(lock)
00025 #else
00026     #define PTHREAD_LOCK_T pthread_mutex_t
00027     #define PTHREAD_LOCK_INIT(lock) pthread_mutex_init(lock, NULL)
00028     #define PTHREAD_LOCK_DESTROY(lock) pthread_mutex_destroy(lock)
00029     #define PTHREAD_LOCK(lock) pthread_mutex_lock(lock)
00030     #define PTHREAD_UNLOCK(lock) pthread_mutex_unlock(lock)
00031 #endif
00032 #endif
00033 
00034 namespace shogun
00035 {
00042 class Parallel
00043 {
00044 public:
00046     Parallel();
00047 
00049     Parallel(const Parallel& orig);
00050 
00052     virtual ~Parallel();
00053 
00057     int32_t get_num_cpus() const;
00058 
00062     inline void set_num_threads(int32_t n)
00063     {
00064 #ifndef HAVE_PTHREAD
00065         ASSERT(n==1);
00066 #endif
00067         num_threads=n;
00068     }
00069 
00073     inline int32_t get_num_threads() const
00074     {
00075         return num_threads;
00076     }
00077 
00081     inline int32_t ref()
00082     {
00083         ++refcount;
00084         return refcount;
00085     }
00086 
00090     inline int32_t ref_count() const
00091     {
00092         return refcount;
00093     }
00094 
00098     inline int32_t unref()
00099     {
00100         if (refcount==0 || --refcount==0)
00101         {
00102             delete this;
00103             return 0;
00104         }
00105         else
00106             return refcount;
00107     }
00108 
00109 private:
00110 
00112     int32_t refcount;
00113 
00115     int32_t num_threads;
00116 };
00117 }
00118 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

SHOGUN Machine Learning Toolbox - Documentation