Commit f28fae01869537e98d7bca1db9bd59faebb2934f

Authored by Luigi Tibaldo
1 parent ad87ab33

implemented parallelization

Showing 1 changed file with 37 additions and 16 deletions
cscripts/csscs.py
... ... @@ -429,7 +429,7 @@ class csscs(ctools.csobservation):
429 429 # Return
430 430 return new_obs
431 431  
432   - def _pixel_analysis(self,ra,dec):
  432 + def _pixel_analysis(self,inx):
433 433 """
434 434 Performs analysis over the region of interest
435 435 corresponding to a single pixel of output map
... ... @@ -437,14 +437,20 @@ class csscs(ctools.csobservation):
437 437 Parameters
438 438 ----------
439 439 inx : `int` pixel index
440   - ra : `float` R.A. (deg)
441   - dec : `float` Dec (deg)
442 440  
443 441 Returns
444 442 -------
445 443 result : `dict` Results
446 444 """
447 445  
  446 + # Create template for output map to extract pixel coordinates
  447 + outmap = self._create_map()
  448 +
  449 + # Determine pixel centre coordinates
  450 + pixdir = outmap.inx2dir(inx)
  451 + ra = pixdir.ra_deg()
  452 + dec = pixdir.dec_deg()
  453 +
448 454 # Write header for spatial bin
449 455 msg = 'Spatial bin centred on (R.A.,Dec) = (%f,%f) deg' % (ra,dec)
450 456 self._log_header2(gammalib.EXPLICIT, msg)
... ... @@ -614,24 +620,39 @@ class csscs(ctools.csobservation):
614 620 # Write input observation container into logger
615 621 self._log_observations(gammalib.NORMAL, self.obs(), 'Input observation')
616 622  
617   - # Create template for output map, to use to extract pixel values
618   - outmap = self._create_map()
619   -
620 623 # Adjust model parameters dependent on input user parameters
621 624 self._adjust_model_pars()
622 625  
623   - # Loop over pixels and extract results
624   - results = {}
625   - for inx in range(outmap.npix()):
  626 + # Compute number of pixels of output map
  627 + npix = self._create_map().npix()
  628 +
  629 + # If more than a single thread is requested then use multiprocessing
  630 + if self._nthreads > 1:
626 631  
627   - # Determine pixel centre
628   - pixdir = outmap.inx2dir(inx)
629   - ra = pixdir.ra_deg()
630   - dec = pixdir.dec_deg()
  632 + # Compute values in pixels
  633 + args = [(self, '_pixel_analysis', i)
  634 + for i in range(npix)]
  635 + poolresults = mputils.process(self._nthreads, mputils.mpfunc, args)
631 636  
632   - # Analyse ROI
633   - result = self._pixel_analysis(ra,dec)
634   - results[inx] = result
  637 + # Construct results
  638 + results = []
  639 + for i in range(npix):
  640 + results.append(poolresults[i][0])
  641 + self._log_string(gammalib.TERSE, poolresults[i][1]['log'], False)
  642 +
  643 + # Otherwise loop over pixels and run the pixel analysis
  644 + else:
  645 + results = []
  646 + for i in range(npix):
  647 + results.append(self._pixel_analysis(i))
  648 +
  649 + # # Loop over pixels and extract results
  650 + # results = []
  651 + # for inx in range(self._create_map().npix()):
  652 + #
  653 + # # Analyse ROI
  654 + # result = self._pixel_analysis(inx)
  655 + # results.append(result)
635 656  
636 657 # Fill output Fits
637 658 self._fits = self._fill_fits(results)
... ...