File: threads.cc

package info (click to toggle)
coot 1.1.18%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 220,004 kB
  • sloc: cpp: 495,934; python: 35,043; ansic: 26,143; lisp: 22,768; sh: 13,186; makefile: 2,746; awk: 441; xml: 245; csh: 14
file content (88 lines) | stat: -rw-r--r-- 2,223 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*
 * utils/threads.cc
 *
 * Copyright 2017 Medical Research Council
 * Author: Paul Emsley
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation; either version 3 of the License, or (at
 * your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA
 */


#include <stdexcept>
#include <iostream>
#include <algorithm>
#include "coot-utils.hh"

#ifdef  _WIN32
#include <windows.h>
#else
#include <unistd.h>
#endif

// can return -1 if name is invalid
//
long coot::get_number_of_threads_by_system_call()  {

#ifdef _WIN32
SYSTEM_INFO sysinfo;
  GetSystemInfo(&sysinfo);
  return sysinfo.dwNumberOfProcessors;
#else
  return sysconf(_SC_NPROCESSORS_CONF);
#endif

}

// unsigned int coot::coot_n_threads = 0; // static

unsigned int coot::get_max_number_of_threads() {

   unsigned int n_threads_default = 4;

   // only do this once:
   // (if coot_n_threads is unset, then set it)
   //
   // is this initially set to 0 by the compiler?
   //
   if (coot_n_threads == 0) {
      // coot_n_threads_cs is null then
      const char *e = getenv("COOT_N_THREADS");
      if (e) {
	 try {
	    // can throw an runtime_error exception on unable to convert
	    unsigned int t = util::string_to_int(e);
	    if (t >= 1024) t = n_threads_default;
	    coot_n_threads = t;
	 }
	 catch (const std::runtime_error &rte) {
	    coot_n_threads = 1;
	 }
      } else {
	 // no environment variable, use system call.
	 coot_n_threads = n_threads_default;
	 long n = get_number_of_threads_by_system_call();
	 if (n > 0)
	    coot_n_threads = n;
      }
   }
   return coot_n_threads;
}

void
coot::set_max_number_of_threads(unsigned int n) {

   coot_n_threads = n;
}