Commit 87ec65c84b7368c13caf0484ca81faf66a8a6294

Authored by Jürgen Knödlseder
1 parent 5aeb1560

Reduce code duplication for multiprocessing in cscripts

cscripts/cslightcrv.py
@@ -26,33 +26,6 @@ from cscripts import ioutils @@ -26,33 +26,6 @@ from cscripts import ioutils
26 from cscripts import mputils 26 from cscripts import mputils
27 27
28 28
29 -# ============================================ #  
30 -# Global functions for multiprocessing support #  
31 -# ============================================ #  
32 -def _multiprocessing_func_wrapper(args):  
33 - return _multiprocessing_func(*args)  
34 -def _multiprocessing_func(cls, i):  
35 -  
36 - # Initialise thread logger  
37 - cls._log.clear()  
38 - cls._log.buffer_size(100000)  
39 -  
40 - # Compute light curve bin  
41 - cstart = cls.celapse()  
42 - result = cls._timebin(i)  
43 - celapse = cls.celapse() - cstart  
44 - log = cls._log.buffer()  
45 -  
46 - # Close logger  
47 - cls._log.close()  
48 -  
49 - # Collect thread information  
50 - info = {'celapse': celapse, 'log': log}  
51 -  
52 - # Return light curve bin result and thread information  
53 - return result, info  
54 -  
55 -  
56 # ================ # 29 # ================ #
57 # cslightcrv class # 30 # cslightcrv class #
58 # ================ # 31 # ================ #
@@ -651,9 +624,9 @@ class cslightcrv(ctools.csobservation): @@ -651,9 +624,9 @@ class cslightcrv(ctools.csobservation):
651 if self._nthreads > 1: 624 if self._nthreads > 1:
652 625
653 # Compute time bins 626 # Compute time bins
654 - args = [(self, i) for i in range(self._tbins.size())]  
655 - poolresults = mputils.process(self._nthreads,  
656 - _multiprocessing_func_wrapper, args) 627 + args = [(self, '_timebin', i)
  628 + for i in range(self._tbins.size())]
  629 + poolresults = mputils.process(self._nthreads, mputils.mpfunc, args)
657 630
658 # Construct results 631 # Construct results
659 results = [] 632 results = []
cscripts/csphagen.py
@@ -25,32 +25,6 @@ import math @@ -25,32 +25,6 @@ import math
25 import sys 25 import sys
26 from cscripts import mputils 26 from cscripts import mputils
27 27
28 -# ============================================ #  
29 -# Global functions for multiprocessing support #  
30 -# ============================================ #  
31 -def _multiprocessing_func_wrapper(args):  
32 - return _multiprocessing_func(*args)  
33 -def _multiprocessing_func(cls, i):  
34 -  
35 - # Initialise thread logger  
36 - cls._log.clear()  
37 - cls._log.buffer_size(100000)  
38 -  
39 - # Compute light curve bin  
40 - cstart = cls.celapse()  
41 - result = cls._process_observation(i)  
42 - celapse = cls.celapse() - cstart  
43 - log = cls._log.buffer()  
44 -  
45 - # Close logger  
46 - cls._log.close()  
47 -  
48 - # Collect thread information  
49 - info = {'celapse': celapse, 'log': log}  
50 -  
51 - # Return light curve bin result and thread information  
52 - return result, info  
53 -  
54 28
55 # =============== # 29 # =============== #
56 # csfindobs class # 30 # csfindobs class #
@@ -579,9 +553,9 @@ class csphagen(ctools.csobservation): @@ -579,9 +553,9 @@ class csphagen(ctools.csobservation):
579 if self._nthreads > 1 and self.obs().size() > 1: 553 if self._nthreads > 1 and self.obs().size() > 1:
580 554
581 # Compute observations 555 # Compute observations
582 - args = [(self, i) for i in range(self.obs().size())]  
583 - poolresults = mputils.process(self._nthreads,  
584 - _multiprocessing_func_wrapper, args) 556 + args = [(self, '_process_observation', i)
  557 + for i in range(self.obs().size())]
  558 + poolresults = mputils.process(self._nthreads, mputils.mpfunc, args)
585 559
586 # Construct results 560 # Construct results
587 for i in range(self.obs().size()): 561 for i in range(self.obs().size()):
cscripts/csphasecrv.py
@@ -25,32 +25,6 @@ from cscripts import obsutils @@ -25,32 +25,6 @@ from cscripts import obsutils
25 from cscripts import ioutils 25 from cscripts import ioutils
26 from cscripts import mputils 26 from cscripts import mputils
27 27
28 -# ============================================ #  
29 -# Global functions for multiprocessing support #  
30 -# ============================================ #  
31 -def _multiprocessing_func_wrapper(args):  
32 - return _multiprocessing_func(*args)  
33 -def _multiprocessing_func(cls, phbin):  
34 -  
35 - # Initialise thread logger  
36 - cls._log.clear()  
37 - cls._log.buffer_size(100000)  
38 -  
39 - # Compute light curve bin  
40 - cstart = cls.celapse()  
41 - result = cls._phase_bin(phbin)  
42 - celapse = cls.celapse() - cstart  
43 - log = cls._log.buffer()  
44 -  
45 - # Close logger  
46 - cls._log.close()  
47 -  
48 - # Collect thread information  
49 - info = {'celapse': celapse, 'log': log}  
50 -  
51 - # Return light curve bin result and thread information  
52 - return result, info  
53 -  
54 28
55 # ================ # 29 # ================ #
56 # csphasecrv class # 30 # csphasecrv class #
@@ -497,9 +471,8 @@ class csphasecrv(ctools.csobservation): @@ -497,9 +471,8 @@ class csphasecrv(ctools.csobservation):
497 if self._nthreads > 1: 471 if self._nthreads > 1:
498 472
499 # Compute phase bins 473 # Compute phase bins
500 - args = [(self, phbin) for phbin in self._phbins]  
501 - poolresults = mputils.process(self._nthreads,  
502 - _multiprocessing_func_wrapper, args) 474 + args = [(self, '_phase_bin', phbin) for phbin in self._phbins]
  475 + poolresults = mputils.process(self._nthreads, mputils.mpfunc, args)
503 476
504 # Construct results 477 # Construct results
505 for i in range(len(self._phbins)): 478 for i in range(len(self._phbins)):
cscripts/cspull.py
@@ -26,33 +26,6 @@ from cscripts import ioutils @@ -26,33 +26,6 @@ from cscripts import ioutils
26 from cscripts import mputils 26 from cscripts import mputils
27 27
28 28
29 -# ============================================ #  
30 -# Global functions for multiprocessing support #  
31 -# ============================================ #  
32 -def _multiprocessing_func_wrapper(args):  
33 - return _multiprocessing_func(*args)  
34 -def _multiprocessing_func(cls, i):  
35 -  
36 - # Initialise thread logger  
37 - cls._log.clear()  
38 - cls._log.buffer_size(100000)  
39 -  
40 - # Compute light curve bin  
41 - cstart = cls.celapse()  
42 - result = cls._trial(i)  
43 - celapse = cls.celapse() - cstart  
44 - log = cls._log.buffer()  
45 -  
46 - # Close logger  
47 - cls._log.close()  
48 -  
49 - # Collect thread information  
50 - info = {'celapse': celapse, 'log': log}  
51 -  
52 - # Return light curve bin result and thread information  
53 - return result, info  
54 -  
55 -  
56 # ============ # 29 # ============ #
57 # cspull class # 30 # cspull class #
58 # ============ # 31 # ============ #
@@ -385,9 +358,8 @@ class cspull(ctools.csobservation): @@ -385,9 +358,8 @@ class cspull(ctools.csobservation):
385 358
386 # If more than a single thread is requested then use multiprocessing 359 # If more than a single thread is requested then use multiprocessing
387 if self._nthreads > 1: 360 if self._nthreads > 1:
388 - args = [(self, i + seed) for i in range(ntrials)]  
389 - poolresults = mputils.process(self._nthreads,  
390 - _multiprocessing_func_wrapper, args) 361 + args = [(self, '_trial', i + seed) for i in range(ntrials)]
  362 + poolresults = mputils.process(self._nthreads, mputils.mpfunc, args)
391 363
392 # Continue with regular processing 364 # Continue with regular processing
393 for i in range(ntrials): 365 for i in range(ntrials):
cscripts/cssens.py
@@ -28,33 +28,6 @@ from cscripts import ioutils @@ -28,33 +28,6 @@ from cscripts import ioutils
28 from cscripts import mputils 28 from cscripts import mputils
29 29
30 30
31 -# ============================================ #  
32 -# Global functions for multiprocessing support #  
33 -# ============================================ #  
34 -def _multiprocessing_func_wrapper(args):  
35 - return _multiprocessing_func(*args)  
36 -def _multiprocessing_func(cls, i):  
37 -  
38 - # Initialise thread logger  
39 - cls._log.clear()  
40 - cls._log.buffer_size(100000)  
41 -  
42 - # Compute light curve bin  
43 - cstart = cls.celapse()  
44 - result = cls._e_bin(i)  
45 - celapse = cls.celapse() - cstart  
46 - log = cls._log.buffer()  
47 -  
48 - # Close logger  
49 - cls._log.close()  
50 -  
51 - # Collect thread information  
52 - info = {'celapse': celapse, 'log': log}  
53 -  
54 - # Return light curve bin result and thread information  
55 - return result, info  
56 -  
57 -  
58 # ============ # 31 # ============ #
59 # cssens class # 32 # cssens class #
60 # ============ # 33 # ============ #
@@ -715,10 +688,10 @@ class cssens(ctools.csobservation): @@ -715,10 +688,10 @@ class cssens(ctools.csobservation):
715 # If using multiprocessing 688 # If using multiprocessing
716 if self._nthreads > 1 and self._ebounds.size() > 1: 689 if self._nthreads > 1 and self._ebounds.size() > 1:
717 690
718 - # Compute phase bins  
719 - args = [(self, ieng) for ieng in range(self._ebounds.size())]  
720 - poolresults = mputils.process(self._nthreads,  
721 - _multiprocessing_func_wrapper, args) 691 + # Compute energy bins
  692 + args = [(self, '_e_bin', i)
  693 + for i in range(self._ebounds.size())]
  694 + poolresults = mputils.process(self._nthreads, mputils.mpfunc, args)
722 695
723 # Construct results 696 # Construct results
724 for ieng in range(self._ebounds.size()): 697 for ieng in range(self._ebounds.size()):
cscripts/csspec.py
@@ -24,33 +24,6 @@ import ctools @@ -24,33 +24,6 @@ import ctools
24 from cscripts import mputils 24 from cscripts import mputils
25 25
26 26
27 -# ============================================ #  
28 -# Global functions for multiprocessing support #  
29 -# ============================================ #  
30 -def _multiprocessing_func_wrapper(args):  
31 - return _multiprocessing_func(*args)  
32 -def _multiprocessing_func(cls, i):  
33 -  
34 - # Initialise thread logger  
35 - cls._log.clear()  
36 - cls._log.buffer_size(100000)  
37 -  
38 - # Compute light curve bin  
39 - cstart = cls.celapse()  
40 - result = cls._fit_energy_bin(i)  
41 - celapse = cls.celapse() - cstart  
42 - log = cls._log.buffer()  
43 -  
44 - # Close logger  
45 - cls._log.close()  
46 -  
47 - # Collect thread information  
48 - info = {'celapse': celapse, 'log': log}  
49 -  
50 - # Return light curve bin result and thread information  
51 - return result, info  
52 -  
53 -  
54 # ============ # 27 # ============ #
55 # csspec class # 28 # csspec class #
56 # ============ # 29 # ============ #
@@ -775,9 +748,9 @@ class csspec(ctools.csobservation): @@ -775,9 +748,9 @@ class csspec(ctools.csobservation):
775 if self._nthreads > 1: 748 if self._nthreads > 1:
776 749
777 # Compute energy bins 750 # Compute energy bins
778 - args = [(self, i) for i in range(self._ebounds.size())]  
779 - poolresults = mputils.process(self._nthreads,  
780 - _multiprocessing_func_wrapper, args) 751 + args = [(self, '_fit_energy_bin', i)
  752 + for i in range(self._ebounds.size())]
  753 + poolresults = mputils.process(self._nthreads, mputils.mpfunc, args)
781 754
782 # Construct results 755 # Construct results
783 for i in range(self._ebounds.size()): 756 for i in range(self._ebounds.size()):
cscripts/cstsdist.py
@@ -27,33 +27,6 @@ from cscripts import ioutils @@ -27,33 +27,6 @@ from cscripts import ioutils
27 from cscripts import mputils 27 from cscripts import mputils
28 28
29 29
30 -# ============================================ #  
31 -# Global functions for multiprocessing support #  
32 -# ============================================ #  
33 -def _multiprocessing_func_wrapper(args):  
34 - return _multiprocessing_func(*args)  
35 -def _multiprocessing_func(cls, i):  
36 -  
37 - # Initialise thread logger  
38 - cls._log.clear()  
39 - cls._log.buffer_size(100000)  
40 -  
41 - # Compute light curve bin  
42 - cstart = cls.celapse()  
43 - result = cls._trial(i)  
44 - celapse = cls.celapse() - cstart  
45 - log = cls._log.buffer()  
46 -  
47 - # Close logger  
48 - cls._log.close()  
49 -  
50 - # Collect thread information  
51 - info = {'celapse': celapse, 'log': log}  
52 -  
53 - # Return light curve bin result and thread information  
54 - return result, info  
55 -  
56 -  
57 # ============== # 30 # ============== #
58 # cstsdist class # 31 # cstsdist class #
59 # ============== # 32 # ============== #
@@ -334,9 +307,8 @@ class cstsdist(ctools.csobservation): @@ -334,9 +307,8 @@ class cstsdist(ctools.csobservation):
334 307
335 # If more than a single thread is requested then use multiprocessing 308 # If more than a single thread is requested then use multiprocessing
336 if self._nthreads > 1: 309 if self._nthreads > 1:
337 - args = [(self, i) for i in range(ntrials)]  
338 - poolresults = mputils.process(self._nthreads,  
339 - _multiprocessing_func_wrapper, args) 310 + args = [(self, '_trial', i) for i in range(ntrials)]
  311 + poolresults = mputils.process(self._nthreads, mputils.mpfunc, args)
340 312
341 # Continue with regular processing 313 # Continue with regular processing
342 for i in range(ntrials): 314 for i in range(ntrials):
cscripts/mputils.py
@@ -20,9 +20,9 @@ @@ -20,9 +20,9 @@
20 import gammalib 20 import gammalib
21 21
22 22
23 -# ====================== #  
24 -# Get number of threads #  
25 -# ====================== # 23 +# ===================== #
  24 +# Get number of threads #
  25 +# ===================== #
26 def nthreads(cls): 26 def nthreads(cls):
27 """ 27 """
28 Determines the number of parallel processes to use 28 Determines the number of parallel processes to use
@@ -107,3 +107,45 @@ def process(nthreads, function, args): @@ -107,3 +107,45 @@ def process(nthreads, function, args):
107 107
108 # Return results 108 # Return results
109 return results 109 return results
  110 +
  111 +
  112 +# ======== #
  113 +# Function #
  114 +# ======== #
  115 +def mpfunc(args):
  116 + """
  117 + Multiprocessing function
  118 +
  119 + Parameters
  120 + ----------
  121 + args : tuple
  122 + Tuple comprised of class, function name and function argument
  123 +
  124 + Returns
  125 + -------
  126 + result, info : tuple
  127 + Tuple of function result and information
  128 + """
  129 + # Extract
  130 + cls = args[0] # Class
  131 + fct = args[1] # Class method name
  132 + i = args[2] # Function argument
  133 +
  134 + # Initialise thread logger
  135 + cls._log.clear()
  136 + cls._log.buffer_size(100000)
  137 +
  138 + # Compute function for argument
  139 + cstart = cls.celapse()
  140 + result = getattr(cls, fct)(i)
  141 + celapse = cls.celapse() - cstart
  142 + log = cls._log.buffer()
  143 +
  144 + # Close logger
  145 + cls._log.close()
  146 +
  147 + # Collect thread information
  148 + info = {'celapse': celapse, 'log': log}
  149 +
  150 + # Return light curve bin result and thread information
  151 + return result, info