Commit 87ec65c84b7368c13caf0484ca81faf66a8a6294
1 parent
5aeb1560
Reduce code duplication for multiprocessing in cscripts
Showing
8 changed files
with
64 additions
and
212 deletions
cscripts/cslightcrv.py
| ... | ... | @@ -26,33 +26,6 @@ from cscripts import ioutils |
| 26 | 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 | 30 | # cslightcrv class # |
| 58 | 31 | # ================ # |
| ... | ... | @@ -651,9 +624,9 @@ class cslightcrv(ctools.csobservation): |
| 651 | 624 | if self._nthreads > 1: |
| 652 | 625 | |
| 653 | 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 | 631 | # Construct results |
| 659 | 632 | results = [] | ... | ... |
cscripts/csphagen.py
| ... | ... | @@ -25,32 +25,6 @@ import math |
| 25 | 25 | import sys |
| 26 | 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 | 30 | # csfindobs class # |
| ... | ... | @@ -579,9 +553,9 @@ class csphagen(ctools.csobservation): |
| 579 | 553 | if self._nthreads > 1 and self.obs().size() > 1: |
| 580 | 554 | |
| 581 | 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 | 560 | # Construct results |
| 587 | 561 | for i in range(self.obs().size()): | ... | ... |
cscripts/csphasecrv.py
| ... | ... | @@ -25,32 +25,6 @@ from cscripts import obsutils |
| 25 | 25 | from cscripts import ioutils |
| 26 | 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 | 30 | # csphasecrv class # |
| ... | ... | @@ -497,9 +471,8 @@ class csphasecrv(ctools.csobservation): |
| 497 | 471 | if self._nthreads > 1: |
| 498 | 472 | |
| 499 | 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 | 477 | # Construct results |
| 505 | 478 | for i in range(len(self._phbins)): | ... | ... |
cscripts/cspull.py
| ... | ... | @@ -26,33 +26,6 @@ from cscripts import ioutils |
| 26 | 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 | 30 | # cspull class # |
| 58 | 31 | # ============ # |
| ... | ... | @@ -385,9 +358,8 @@ class cspull(ctools.csobservation): |
| 385 | 358 | |
| 386 | 359 | # If more than a single thread is requested then use multiprocessing |
| 387 | 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 | 364 | # Continue with regular processing |
| 393 | 365 | for i in range(ntrials): | ... | ... |
cscripts/cssens.py
| ... | ... | @@ -28,33 +28,6 @@ from cscripts import ioutils |
| 28 | 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 | 32 | # cssens class # |
| 60 | 33 | # ============ # |
| ... | ... | @@ -715,10 +688,10 @@ class cssens(ctools.csobservation): |
| 715 | 688 | # If using multiprocessing |
| 716 | 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 | 696 | # Construct results |
| 724 | 697 | for ieng in range(self._ebounds.size()): | ... | ... |
cscripts/csspec.py
| ... | ... | @@ -24,33 +24,6 @@ import ctools |
| 24 | 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 | 28 | # csspec class # |
| 56 | 29 | # ============ # |
| ... | ... | @@ -775,9 +748,9 @@ class csspec(ctools.csobservation): |
| 775 | 748 | if self._nthreads > 1: |
| 776 | 749 | |
| 777 | 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 | 755 | # Construct results |
| 783 | 756 | for i in range(self._ebounds.size()): | ... | ... |
cscripts/cstsdist.py
| ... | ... | @@ -27,33 +27,6 @@ from cscripts import ioutils |
| 27 | 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 | 31 | # cstsdist class # |
| 59 | 32 | # ============== # |
| ... | ... | @@ -334,9 +307,8 @@ class cstsdist(ctools.csobservation): |
| 334 | 307 | |
| 335 | 308 | # If more than a single thread is requested then use multiprocessing |
| 336 | 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 | 313 | # Continue with regular processing |
| 342 | 314 | for i in range(ntrials): | ... | ... |
cscripts/mputils.py
| ... | ... | @@ -20,9 +20,9 @@ |
| 20 | 20 | import gammalib |
| 21 | 21 | |
| 22 | 22 | |
| 23 | -# ====================== # | |
| 24 | -# Get number of threads # | |
| 25 | -# ====================== # | |
| 23 | +# ===================== # | |
| 24 | +# Get number of threads # | |
| 25 | +# ===================== # | |
| 26 | 26 | def nthreads(cls): |
| 27 | 27 | """ |
| 28 | 28 | Determines the number of parallel processes to use |
| ... | ... | @@ -107,3 +107,45 @@ def process(nthreads, function, args): |
| 107 | 107 | |
| 108 | 108 | # Return results |
| 109 | 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 | ... | ... |