10 examples of 'python get number of cores' in Python

Every line of 'python get number of cores' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
45def get_cpu_cores(hyperthreads=False):
46 """Returns the number of cores on the node.
47
48 If hyperthreads is true, this is the logical cpu cores, else
49 the physical cores are returned.
50
51 Note: This returns cores available on the current node - will
52 not work for systems of multiple node types
53 """
54 try:
55 import psutil
56 ranks_per_node = psutil.cpu_count(logical=hyperthreads)
57 except ImportError:
58 # logger
59 if hyperthreads:
60 import multiprocessing
61 ranks_per_node = multiprocessing.cpu_count()
62 else:
63 try:
64 ranks_per_node = _cpu_count_physical()
65 except Exception as e:
66 logger.warning("Could not detect physical cores - Logical cores (with hyperthreads) returned - specify ranks_per_node to override. Exception {}".format(e))
67 import multiprocessing
68 ranks_per_node = multiprocessing.cpu_count()
69 return ranks_per_node # This is ranks available per node
76def get_cores_available():
77 if not is_numa_capable():
78 cpu_list = range(multiprocessing.cpu_count())
79 cpus = {0: CPUNode(cpu_list)}
80 else:
81 cpus = get_numa_nodes()
82 return cpus
113def cpu_count():
114 '''
115 Returns the number of CPUs in the system
116 '''
117 if sys.platform == 'win32' or (sys.platform == 'cli' and os.name == 'nt'):
118 try:
119 num = int(os.environ['NUMBER_OF_PROCESSORS'])
120 except (ValueError, KeyError):
121 num = 0
122 elif 'bsd' in sys.platform or sys.platform == 'darwin':
123 comm = '/sbin/sysctl -n hw.ncpu'
124 if sys.platform == 'darwin':
125 comm = '/usr' + comm
126 try:
127 with os.popen(comm) as p:
128 num = int(p.read())
129 except ValueError:
130 num = 0
131 else:
132 try:
133 num = os.sysconf('SC_NPROCESSORS_ONLN')
134 except (ValueError, OSError, AttributeError):
135 num = 0
136
137 if num >= 1:
138 return num
139 else:
140 raise NotImplementedError('cannot determine number of cpus')
170def _get_processes(procs):
171 """Choose the best number of processes."""
172 cpus = cpu_count()
173 if cpus == 1:
174 return cpus
175 else:
176 if not 0 < procs < cpus:
177 return cpus - 1
178 else:
179 return procs
11def get_n_gpu():
12 detected_n_gpu = mp.RawValue('i', 0)
13 p = mp.Process(target=n_gpu_subprocess, args=(detected_n_gpu,))
14 p.start()
15 p.join()
16 n_gpu = int(detected_n_gpu.value)
17 if n_gpu == -1:
18 raise ImportError("Must be able to import pygpu to use GPUs.")
19 return n_gpu
2585def cpu_count():
2586 return rffi.cast(lltype.Signed, _cpu_count())
58def get_num_cpus():
59 """ Returns the number of CPUs on the emulated system
60
61 :return: The number of CPUs on the emulated system
62 :rtype: int
63 """
64 import c_api
65 # If this function call fails, it will raise an exception.
66 # Given that the exception is self explanatory, we just let it propagate
67 # upwards
68 return c_api.get_num_cpus()
133def coresReserved(self):
134 """The number of cores reserved for this frame.
135
136 :rtype: float
137 :return: Cores reserved for the running frame"""
138 return self.data.reserved_cores
166@property
167def cores_per_node(self) -> Optional[int]:
168 """Number of cores to provision per node.
169
170 Providers which set this property should ask for cores_per_node cores
171 when provisioning resources, and set the corresponding environment
172 variable PARSL_CORES before executing submitted commands.
173
174 If this property is set, executors may use it to calculate how many tasks can
175 run concurrently per node. This information is used by dataflow.Strategy to estimate
176 the resources required to run all outstanding tasks.
177 """
178 return self._cores_per_node
67@property
68def maximum_number_of_cores(self):
69 """
70 Get maximum number of cores for an individual job
71
72 Returns:
73 int: minimum number of cores
74 """
75 return self._maximum_number_of_cores

Related snippets