SHOGUN  v3.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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 
14 #if defined(LINUX) && defined(_SC_NPROCESSORS_ONLN)
15 #include <unistd.h>
16 #elif defined(DARWIN)
17 #include <sys/types.h>
18 #include <sys/sysctl.h>
19 #endif
20 
21 
22 using namespace shogun;
23 
25 {
26  num_threads=get_num_cpus();
27  m_refcount = new RefCount();
28 }
29 
31 {
32  num_threads=orig.get_num_threads();
33  m_refcount = new RefCount(orig.m_refcount->ref_count());
34 }
35 
37 {
38  delete m_refcount;
39 }
40 
41 int32_t Parallel::get_num_cpus() const
42 {
43 #if defined(LINUX) && defined(_SC_NPROCESSORS_ONLN)
44  return sysconf( _SC_NPROCESSORS_ONLN );
45 #elif defined(DARWIN)
46  int num; /* for calling external lib */
47  size_t size=sizeof(num);
48  if (!sysctlbyname("hw.ncpu", &num, &size, NULL, 0))
49  return num;
50 #endif
51  return 1;
52 }
53 
55 {
56 #ifndef HAVE_PTHREAD
57  ASSERT(n==1)
58 #endif
59  num_threads=n;
60 }
61 
63 {
64  return num_threads;
65 }
66 
67 int32_t Parallel::ref()
68 {
69  return m_refcount->ref();
70 }
71 
72 int32_t Parallel::ref_count() const
73 {
74  return m_refcount->ref_count();
75 }
76 
77 int32_t Parallel::unref()
78 {
79  int32_t rc = m_refcount->unref();
80 
81  if (rc==0)
82  {
83  delete this;
84  return 0;
85  }
86 
87  return rc;
88 }

SHOGUN Machine Learning Toolbox - Documentation