28 Determines the number of parallel processes to use
30 The number is based on the user parameter "nthreads" and the availability
31 of the multiprocessing module.
35 cls : `~ctools.cscript`
44 cls._log_header1(gammalib.NORMAL,
'Multiprocessing')
48 from multiprocessing
import cpu_count
50 cls._log_value(gammalib.NORMAL,
'Multiprocessing',
'available')
51 cls._log_value(gammalib.EXPLICIT,
'CPUs available', ncpus)
54 if cls[
'nthreads'].integer() == 0:
59 cls._log_value(gammalib.EXPLICIT,
'Processes requested',
60 cls[
'nthreads'].integer())
61 nthreads = cls[
'nthreads'].integer()
65 cls._log_value(gammalib.NORMAL,
'Multiprocessing',
'not available')
68 cls._log_value(gammalib.NORMAL,
'Processes available', nthreads)
79 Execute function in parallel
84 Number of parallel threads
85 function : Python function
86 Function to be executed in parallel
88 Function arguments for each evaluation
92 results : list of dicts
93 List of function evaluation results
96 from multiprocessing
import Pool
99 pool = Pool(processes=nthreads)
102 results = pool.map(function, args)
110 for i
in range(nchilds):
111 celapse = results[i][1][
'celapse']
112 args[i][0]._add_celapse(celapse)
123 Multiprocessing function
128 Tuple comprised of class, function name and function argument
133 Tuple of function result and information
142 cls._log.buffer_size(100000)
145 cstart = cls.celapse()
146 result = getattr(cls, fct)(i)
147 celapse = cls.celapse() - cstart
148 log = cls._log.buffer()
154 info = {
'celapse': celapse,
'log': log}