SHOGUN  6.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 #ifdef HAVE_CXX11
17 #include <thread>
18 #endif
19 
20 #if defined(LINUX)
21 #include <unistd.h>
22 #elif defined(DARWIN)
23 #include <sys/types.h>
24 #include <sys/sysctl.h>
25 #endif
26 
27 #ifdef HAVE_OPENMP
28 #include <omp.h>
29 #endif
30 
31 using namespace shogun;
32 
34 {
35  num_threads=get_num_cpus();
36  m_refcount = new RefCount();
37 #ifdef HAVE_OPENMP
38  omp_set_dynamic(0);
39  omp_set_num_threads(num_threads);
40 #endif
41 }
42 
44 {
45  num_threads=orig.get_num_threads();
46  m_refcount = new RefCount();
47 #ifdef HAVE_OPENMP
48  omp_set_dynamic(0);
49  omp_set_num_threads(num_threads);
50 #endif
51 }
52 
54 {
55  delete m_refcount;
56 }
57 
58 int32_t Parallel::get_num_cpus() const
59 {
60  int32_t num_cpus = 1;
61 #if HAVE_CXX11
62  num_cpus = std::thread::hardware_concurrency();
63  // it can return 0, hence only the value
64  // if it's correct, else continue with detection
65  // of cpus
66  if (num_cpus > 0)
67  return num_cpus;
68 #endif
69 
70 #if defined(LINUX) && defined(_SC_NPROCESSORS_ONLN)
71  return sysconf(_SC_NPROCESSORS_ONLN);
72 #elif defined(DARWIN)
73  int32_t num; /* for calling external lib */
74  size_t size=sizeof(num);
75  if (!sysctlbyname("hw.ncpu", &num, &size, NULL, 0))
76  num_cpus = num;
77 #elif defined(_MSC_VER)
78  SYSTEM_INFO sysinfo;
79  GetSystemInfo(&sysinfo);
80  num_cpus = sysinfo.dwNumberOfProcessors;
81 #endif
82 
83  return num_cpus;
84 }
85 
87 {
88 #if !defined(HAVE_PTHREAD) && !defined(HAVE_OPENMP)
89  ASSERT(n==1)
90 #endif
91  num_threads=n;
92 #ifdef HAVE_OPENMP
93  omp_set_num_threads(num_threads);
94 #endif
95 }
96 
98 {
99  return num_threads;
100 }
101 
102 int32_t Parallel::ref()
103 {
104  return m_refcount->ref();
105 }
106 
107 int32_t Parallel::ref_count() const
108 {
109  return m_refcount->ref_count();
110 }
111 
113 {
114  int32_t rc = m_refcount->unref();
115 
116  if (rc==0)
117  {
118  delete this;
119  return 0;
120  }
121 
122  return rc;
123 }
void set_num_threads(int32_t n)
Definition: Parallel.cpp:86
int32_t get_num_cpus() const
Definition: Parallel.cpp:58
int32_t get_num_threads() const
Definition: Parallel.cpp:97
int32_t ref()
Definition: Parallel.cpp:102
SG_FORCED_INLINE int32_t ref()
Definition: RefCount.h:26
int32_t unref()
Definition: Parallel.cpp:112
virtual ~Parallel()
Definition: Parallel.cpp:53
#define ASSERT(x)
Definition: SGIO.h:200
SG_FORCED_INLINE int32_t unref()
Definition: RefCount.h:35
int32_t ref_count() const
Definition: Parallel.cpp:107
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
SG_FORCED_INLINE int32_t ref_count()
Definition: RefCount.h:44

SHOGUN Machine Learning Toolbox - Documentation