def set_affinity(mask): if sched_setaffinity: sched_setaffinity(0, mask) else: if PsutilProcess: p = PsutilProcess() p.cpu_affinity(mask) else: if os.name == "posix": omp_set_num_threads(len(mask))
def limit_cpu(self, cpus: Union[int, List[int]], logical: bool = True) -> NoReturn: count = len(cpus) if logical: assert count <= self._cpu_logical_cores_count else: assert count <= self._cpu_physical_cores_count logical_cores = list(range(self._cpu_logical_cores_count)) physical_cores = np.array_split( logical_cores, indices_or_sections=self._cpu_physical_cores_count) cpus = list(chain(*itemgetter(*cpus)(physical_cores))) cpus = self._force_list(cpus) process = Process(pid=os.getpid()) process.cpu_affinity(cpus=cpus)
def configure_process(proc: psutil.Process, cores: List[int], infer_multi_team=False): try: if infer_multi_team: existing_affinity = proc.cpu_affinity() if len(existing_affinity) < psutil.cpu_count(): # The process in question has already been pinned to a subset of the CPUs. # This might be a process spanning multiple teams, so we will set affinity to # the combination of existing and newly requested cores. cores = sorted(set(existing_affinity + cores)) proc.cpu_affinity(cores) if platform.system() == 'Windows': proc.nice(psutil.HIGH_PRIORITY_CLASS ) # Allow the process to run at high priority except Exception as e: get_logger(DEFAULT_LOGGER).info(e)
def get_affinity(): if sched_getaffinity: return sched_getaffinity(0) else: if PsutilProcess: p = PsutilProcess() return p.cpu_affinity() else: return [i for i in range(cpu_count())]
def process_viewer(viewer, pid): """Processor for multicam view. Runs the viewer itself as a subprocess. Parameters ---------- viewer : utils.viewer.Viewer The Viewer object to be run. It should be previously initialized. pid : int Contains an identifier of the process used to assign CPUs. """ p = Process() p.cpu_affinity([pid + 1]) viewer.run()
def main(global_consensus: str, local_consensus: str, fasta: str, no_write: bool = False, cpus: int = -1, out_prefix: str = 'A2G_aln', remove_duplicates: bool = True): p = Process() n_cpus = cpus if cpus > 0 else cpu_count() all_cpus = list(range(n_cpus)) p.cpu_affinity(all_cpus) kwargs = dict(out_prefix=out_prefix, no_write=no_write, cpus=cpus, remove_duplicates=remove_duplicates) aln = Align(global_consensus, local_consensus, **kwargs) aln.query = fasta full, subset = aln.run() if no_write: return full, subset
def _get_process_cores(process: psutil.Process): # cores = len(process.cpu_affinity) try: core = len(process.cpu_affinity()) except psutil.AccessDenied: cores = 0