SHOGUN  3.2.1
 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 #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 }

SHOGUN Machine Learning Toolbox - Documentation