SHOGUN  5.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules
Parallel.cpp
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 3 of the License, or
5  * (at your option) any later version.
6  *
7  * Written (W) 1999-2009 Soeren Sonnenburg
8  * Copyright (C) 1999-2009 Fraunhofer Institute FIRST and Max-Planck-Society
9  */
10 
11 #include <shogun/base/Parallel.h>
12 #include <shogun/lib/RefCount.h>
13 #include <shogun/lib/config.h>
14 #include <shogun/lib/memory.h>
15 
16 #if defined(LINUX) && defined(_SC_NPROCESSORS_ONLN)
17 #include <unistd.h>
18 #elif defined(DARWIN)
19 #include <sys/types.h>
20 #include <sys/sysctl.h>
21 #endif
22 
23 #ifdef HAVE_OPENMP
24 #include <omp.h>
25 #endif
26 
27 using namespace shogun;
28 
30 {
31  num_threads=get_num_cpus();
32  m_refcount = new RefCount();
33 #ifdef HAVE_OPENMP
34  omp_set_dynamic(0);
35  omp_set_num_threads(num_threads);
36 #endif
37 }
38 
40 {
41  num_threads=orig.get_num_threads();
42  m_refcount = new RefCount();
43 #ifdef HAVE_OPENMP
44  omp_set_dynamic(0);
45  omp_set_num_threads(num_threads);
46 #endif
47 }
48 
50 {
51  delete m_refcount;
52 }
53 
54 int32_t Parallel::get_num_cpus() const
55 {
56 #if defined(LINUX) && defined(_SC_NPROCESSORS_ONLN)
57  return sysconf( _SC_NPROCESSORS_ONLN );
58 #elif defined(DARWIN)
59  int num; /* for calling external lib */
60  size_t size=sizeof(num);
61  if (!sysctlbyname("hw.ncpu", &num, &size, NULL, 0))
62  return num;
63 #endif
64  return 1;
65 }
66 
68 {
69 #ifndef HAVE_PTHREAD
70  ASSERT(n==1)
71 #endif
72  num_threads=n;
73 #ifdef HAVE_OPENMP
74  omp_set_num_threads(num_threads);
75 #endif
76 }
77 
79 {
80  return num_threads;
81 }
82 
83 int32_t Parallel::ref()
84 {
85  return m_refcount->ref();
86 }
87 
88 int32_t Parallel::ref_count() const
89 {
90  return m_refcount->ref_count();
91 }
92 
93 int32_t Parallel::unref()
94 {
95  int32_t rc = m_refcount->unref();
96 
97  if (rc==0)
98  {
99  delete this;
100  return 0;
101  }
102 
103  return rc;
104 }
void set_num_threads(int32_t n)
Definition: Parallel.cpp:67
int32_t get_num_cpus() const
Definition: Parallel.cpp:54
int32_t get_num_threads() const
Definition: Parallel.cpp:78
int32_t ref()
Definition: Parallel.cpp:83
int32_t ref_count()
Definition: RefCount.cpp:31
int32_t unref()
Definition: Parallel.cpp:93
virtual ~Parallel()
Definition: Parallel.cpp:49
#define ASSERT(x)
Definition: SGIO.h:201
int32_t unref()
Definition: RefCount.cpp:18
int32_t ref()
Definition: RefCount.cpp:5
int32_t ref_count() const
Definition: Parallel.cpp:88
Class Parallel provides helper functions for multithreading.
Definition: Parallel.h:27
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18

SHOGUN Machine Learning Toolbox - Documentation