SHOGUN  v2.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 
13 #if defined(LINUX) && defined(_SC_NPROCESSORS_ONLN)
14 #include <unistd.h>
15 #elif defined(DARWIN)
16 #include <sys/types.h>
17 #include <sys/sysctl.h>
18 #endif
19 
20 
21 using namespace shogun;
22 
23 Parallel::Parallel() : refcount(0)
24 {
25  num_threads=get_num_cpus();
26 }
27 
28 Parallel::Parallel(const Parallel& orig) : refcount(0)
29 {
30  num_threads=orig.get_num_threads();
31 }
32 
34 {
35 }
36 
37 int32_t Parallel::get_num_cpus() const
38 {
39 #if defined(LINUX) && defined(_SC_NPROCESSORS_ONLN)
40  return sysconf( _SC_NPROCESSORS_ONLN );
41 #elif defined(DARWIN)
42  int num; /* for calling external lib */
43  size_t size=sizeof(num);
44  if (!sysctlbyname("hw.ncpu", &num, &size, NULL, 0))
45  return num;
46 #endif
47  return 1;
48 }
49 
51 {
52 #ifndef HAVE_PTHREAD
53  ASSERT(n==1);
54 #endif
55  num_threads=n;
56 }
57 
59 {
60  return num_threads;
61 }
62 
63 int32_t Parallel::ref()
64 {
65  ++refcount;
66  return refcount;
67 }
68 
69 int32_t Parallel::ref_count() const
70 {
71  return refcount;
72 }
73 
74 int32_t Parallel::unref()
75 {
76  if (refcount==0 || --refcount==0)
77  {
78  delete this;
79  return 0;
80  }
81  else
82  return refcount;
83 }

SHOGUN Machine Learning Toolbox - Documentation