SHOGUN  4.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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 
24 using namespace shogun;
25 
27 {
28  num_threads=get_num_cpus();
29  m_refcount = new RefCount();
30 }
31 
33 {
34  num_threads=orig.get_num_threads();
35  m_refcount = new RefCount();
36 }
37 
39 {
40  delete m_refcount;
41 }
42 
43 int32_t Parallel::get_num_cpus() const
44 {
45 #if defined(LINUX) && defined(_SC_NPROCESSORS_ONLN)
46  return sysconf( _SC_NPROCESSORS_ONLN );
47 #elif defined(DARWIN)
48  int num; /* for calling external lib */
49  size_t size=sizeof(num);
50  if (!sysctlbyname("hw.ncpu", &num, &size, NULL, 0))
51  return num;
52 #endif
53  return 1;
54 }
55 
57 {
58 #ifndef HAVE_PTHREAD
59  ASSERT(n==1)
60 #endif
61  num_threads=n;
62 }
63 
65 {
66  return num_threads;
67 }
68 
69 int32_t Parallel::ref()
70 {
71  return m_refcount->ref();
72 }
73 
74 int32_t Parallel::ref_count() const
75 {
76  return m_refcount->ref_count();
77 }
78 
79 int32_t Parallel::unref()
80 {
81  int32_t rc = m_refcount->unref();
82 
83  if (rc==0)
84  {
85  delete this;
86  return 0;
87  }
88 
89  return rc;
90 }
void set_num_threads(int32_t n)
Definition: Parallel.cpp:56
int32_t get_num_cpus() const
Definition: Parallel.cpp:43
int32_t get_num_threads() const
Definition: Parallel.cpp:64
int32_t ref()
Definition: Parallel.cpp:69
int32_t ref_count()
Definition: RefCount.cpp:31
int32_t unref()
Definition: Parallel.cpp:79
virtual ~Parallel()
Definition: Parallel.cpp:38
#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:74
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