From b39b19cb2dcf9c1765a83a15b77c1fced7d73e82 Mon Sep 17 00:00:00 2001 From: Jürgen Knödlseder Date: Fri, 20 Jul 2018 08:51:50 +0200 Subject: [PATCH] Add nthreads parameter to ctlike and cterror (#2421) --- ChangeLog | 2 +- NEWS | 4 ++-- cscripts/cslightcrv.py | 3 ++- cscripts/csphasecrv.py | 9 ++++++--- cscripts/cspull.py | 5 ++++- cscripts/cssens.py | 8 +++++--- cscripts/csspec.py | 6 ++++-- cscripts/cstsdist.py | 6 ++++-- doc/source/admin/release_history/1.6.rst | 3 +++ doc/source/users/reference_manual/cslightcrv.rst | 8 ++++---- doc/source/users/reference_manual/csphasecrv.rst | 8 ++++---- doc/source/users/reference_manual/cterror.rst | 3 +++ doc/source/users/reference_manual/ctlike.rst | 3 +++ src/cterror/cterror.cpp | 13 +++++++++++++ src/cterror/cterror.par | 11 ++++++----- src/ctlike/ctlike.cpp | 13 +++++++++++++ src/ctlike/ctlike.par | 11 ++++++----- 17 files changed, 83 insertions(+), 33 deletions(-) diff --git a/ChangeLog b/ChangeLog index 1039e54..6b47b1a 100755 --- a/ChangeLog +++ b/ChangeLog @@ -10,7 +10,7 @@ Add Exponential spectral model to science verification (#2454) Fix bug when using precomputed model in csresspec (#2448) Parallelise cslightcrv, csphasecrv, csspec, cssens, csphagen, cspull and - cstsdist (#2421) + cstsdist and add nthreads parameter to ctlike and cterror (#2421) Keep existing library path in installation script on Mac OS X (#2420) csphagen produces Xspec compliant data (#2404) Add show_rmf.py example script diff --git a/NEWS b/NEWS index b58c61c..1014e7b 100755 --- a/NEWS +++ b/NEWS @@ -74,7 +74,7 @@ None cterror - Likelihood profile error computation ---------------------------------------------- -None +Add hidden "nthreads" parameter to control parallel computations (#2421). ctexpcube - CTA exposure cube generation @@ -84,7 +84,7 @@ None ctlike - Maximum likelihood fitting ----------------------------------- -None +Add hidden "nthreads" parameter to control parallel computations (#2421). ctmapcube - CTA map cube generation diff --git a/cscripts/cslightcrv.py b/cscripts/cslightcrv.py index 9f5e5e2..aa23d5f 100755 --- a/cscripts/cslightcrv.py +++ b/cscripts/cslightcrv.py @@ -530,7 +530,8 @@ class cslightcrv(ctools.csobservation): # Do maximum likelihood model fitting if obs.size() > 0: like = ctools.ctlike(obs) - like['edisp'] = self['edisp'].boolean() + like['edisp'] = self['edisp'].boolean() + like['nthreads'] = 1 # Avoids OpenMP conflict like.run() # Skip bin if no event was present diff --git a/cscripts/csphasecrv.py b/cscripts/csphasecrv.py index be92c7a..75b5273 100755 --- a/cscripts/csphasecrv.py +++ b/cscripts/csphasecrv.py @@ -443,7 +443,8 @@ class csphasecrv(ctools.csobservation): # Do maximum likelihood model fitting like = ctools.ctlike(obs) - like['edisp'] = self['edisp'].boolean() + like['edisp'] = self['edisp'].boolean() + like['nthreads'] = 1 # Avoids OpenMP conflict like.run() # Renormalize models to phase selection @@ -464,11 +465,13 @@ class csphasecrv(ctools.csobservation): # Set empty models container fitmodels = gammalib.GModels() - result = {'phstr' :phstr, - 'fitmodels' :fitmodels} + # Set results + result = {'phstr': phstr, 'fitmodels': fitmodels} + # Return results return result + # Public methods def run(self): """ diff --git a/cscripts/cspull.py b/cscripts/cspull.py index 67a81ac..7eb16bf 100755 --- a/cscripts/cspull.py +++ b/cscripts/cspull.py @@ -2,7 +2,7 @@ # ========================================================================== # This script generates the pull distribution for all model parameters. # -# Copyright (C) 2011-2017 Juergen Knoedlseder +# Copyright (C) 2011-2018 Juergen Knoedlseder # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -25,6 +25,7 @@ from cscripts import obsutils from cscripts import ioutils from cscripts import mputils + # ============================================ # # Global functions for multiprocessing support # # ============================================ # @@ -282,6 +283,7 @@ class cspull(ctools.csobservation): like['srcname'] = model.name() like['edisp'] = edisp like['statistic'] = statistic + like['nthreads'] = 1 # Avoids OpenMP conflict like['debug'] = self._logDebug() like['chatter'] = self['chatter'].integer() like.run() @@ -289,6 +291,7 @@ class cspull(ctools.csobservation): like = ctools.ctlike(obs) like['edisp'] = edisp like['statistic'] = statistic + like['nthreads'] = 1 # Avoids OpenMP conflict like['debug'] = self._logDebug() like['chatter'] = self['chatter'].integer() like.run() diff --git a/cscripts/cssens.py b/cscripts/cssens.py index 2f24df0..89b426e 100755 --- a/cscripts/cssens.py +++ b/cscripts/cssens.py @@ -27,6 +27,7 @@ from cscripts import modutils from cscripts import ioutils from cscripts import mputils + # ============================================ # # Global functions for multiprocessing support # # ============================================ # @@ -434,9 +435,10 @@ class cssens(ctools.csobservation): # Fit test source to the simulated events in the observation # container fit = ctools.ctlike(sim) - fit['edisp'] = self['edisp'].boolean() - fit['debug'] = self['debug'].boolean() - fit['chatter'] = self['chatter'].integer() + fit['edisp'] = self['edisp'].boolean() + fit['nthreads'] = 1 # Avoids OpenMP conflict + fit['debug'] = self['debug'].boolean() + fit['chatter'] = self['chatter'].integer() fit.run() # Get model fitting results diff --git a/cscripts/csspec.py b/cscripts/csspec.py index d00a03b..2a8fb74 100755 --- a/cscripts/csspec.py +++ b/cscripts/csspec.py @@ -23,6 +23,7 @@ import gammalib import ctools from cscripts import mputils + # ============================================ # # Global functions for multiprocessing support # # ============================================ # @@ -674,8 +675,9 @@ class csspec(ctools.csobservation): self._log_header3(gammalib.EXPLICIT, 'Performing fit') # Perform maximum likelihood fit. Skip bins that have no events. - like = ctools.ctlike(obs) - like['edisp'] = self['edisp'].boolean() + like = ctools.ctlike(obs) + like['edisp'] = self['edisp'].boolean() + like['nthreads'] = 1 # Avoids OpenMP conflict if like.obs().nobserved() > 0: like.run() diff --git a/cscripts/cstsdist.py b/cscripts/cstsdist.py index b0b53f4..db8f075 100755 --- a/cscripts/cstsdist.py +++ b/cscripts/cstsdist.py @@ -26,6 +26,7 @@ from cscripts import modutils from cscripts import ioutils from cscripts import mputils + # ============================================ # # Global functions for multiprocessing support # # ============================================ # @@ -235,8 +236,9 @@ class cstsdist(ctools.csobservation): # Fit model fit = ctools.ctlike(sim) - fit['debug'] = self['debug'].boolean() - fit['chatter'] = self['chatter'].integer() + fit['nthreads'] = 1 # Avoids OpenMP conflict + fit['debug'] = self['debug'].boolean() + fit['chatter'] = self['chatter'].integer() fit.run() # Get model fitting results diff --git a/doc/source/admin/release_history/1.6.rst b/doc/source/admin/release_history/1.6.rst index 11b9cec..de845a5 100644 --- a/doc/source/admin/release_history/1.6.rst +++ b/doc/source/admin/release_history/1.6.rst @@ -34,6 +34,9 @@ Improvements Transparently update parameter files * [`2490 `_] - Change formula for number of true energy bins in :ref:`csphagen` +* [`2421 `_] - + Add `nthreads` parameter to :ref:`ctlike` and :ref:`cterror` to control + parallel computations * [`2404 `_] - :ref:`csphagen` produces Xspec compliant data * Add ``show_rmf.py`` example script diff --git a/doc/source/users/reference_manual/cslightcrv.rst b/doc/source/users/reference_manual/cslightcrv.rst index cc01413..a9d2d68 100755 --- a/doc/source/users/reference_manual/cslightcrv.rst +++ b/doc/source/users/reference_manual/cslightcrv.rst @@ -31,10 +31,10 @@ are necessary to run internally the :ref:`csphagen` script (``inexclusion``, ``c ``xref``, ``yref``, ``srcshape``, ``rad``, ``bkgmethod``, ``bkgregmin``, ``maxoffset``, ``etruemin``, ``etruemax``, ``etruebins``). -:ref:`cslightcrv` supports multiprocessing. By default the analysis in each time bin will -be performed in parallel over as many processes as the number of CPUs available on your -machine. The maximum number of parallel processes can be set by the user through the -``nthreads`` hidden parameter. +:ref:`cslightcrv` supports multiprocessing. By default the analysis in each time bin +will be performed in parallel over as many processes as the number of CPUs +available on your machine. The maximum number of parallel processes can be set +by the user through the ``nthreads`` hidden parameter. :ref:`cslightcrv` writes the fitted model parameters and their statistical errors in a FITS file. In addition, it computes for each time bin the statistical diff --git a/doc/source/users/reference_manual/csphasecrv.rst b/doc/source/users/reference_manual/csphasecrv.rst index 70914ac..c7b4b7a 100755 --- a/doc/source/users/reference_manual/csphasecrv.rst +++ b/doc/source/users/reference_manual/csphasecrv.rst @@ -31,10 +31,10 @@ are necessary to run internally the :ref:`csphagen` script (``inexclusion``, ``c ``xref``, ``yref``, ``srcshape``, ``rad``, ``bkgmethod``, ``bkgregmin``, ``maxoffset``, ``etruemin``, ``etruemax``, ``etruebins``). -:ref:`csphasecrv` supports multiprocessing. By default the analysis in each phase bin will -be performed in parallel over as many processes as the number of CPUs available on your -machine. The maximum number of parallel processes can be set by the user through the -``nthreads`` hidden parameter. +:ref:`csphasecrv` supports multiprocessing. By default the analysis in each phase bin +will be performed in parallel over as many processes as the number of CPUs +available on your machine. The maximum number of parallel processes can be set +by the user through the ``nthreads`` hidden parameter. The script writes the fit results into a FITS file. The script also produces one XML file per phase bin that contains the best-fit model for that bin. diff --git a/doc/source/users/reference_manual/cterror.rst b/doc/source/users/reference_manual/cterror.rst index 8474adf..dc15883 100755 --- a/doc/source/users/reference_manual/cterror.rst +++ b/doc/source/users/reference_manual/cterror.rst @@ -83,6 +83,9 @@ General parameters Standard parameters ------------------- +``(nthreads = 0) [integer]`` + Number of parallel processes (0=use all available CPUs). + ``(chatter = 2) [integer]`` Verbosity of the executable: ``chatter = 0``: no information will be logged diff --git a/doc/source/users/reference_manual/ctlike.rst b/doc/source/users/reference_manual/ctlike.rst index 756d7c7..c1d96e5 100755 --- a/doc/source/users/reference_manual/ctlike.rst +++ b/doc/source/users/reference_manual/ctlike.rst @@ -112,6 +112,9 @@ General parameters Standard parameters ------------------- +``(nthreads = 0) [integer]`` + Number of parallel processes (0=use all available CPUs). + ``(chatter = 2) [integer]`` Verbosity of the executable: ``chatter = 0``: no information will be logged diff --git a/src/cterror/cterror.cpp b/src/cterror/cterror.cpp index 44fed01..307c5ed 100755 --- a/src/cterror/cterror.cpp +++ b/src/cterror/cterror.cpp @@ -33,6 +33,11 @@ #include "GTools.hpp" #include "GOptimizer.hpp" +/* __ OpenMP section _____________________________________________________ */ +#ifdef _OPENMP +#include +#endif + /* __ Method name definitions ____________________________________________ */ #define G_ERR_BISECTION "cterror::error_bisection(double&, double&)" @@ -477,6 +482,14 @@ void cterror::get_parameters(void) (*this)["outmodel"].query(); } + // Set number of OpenMP threads + #ifdef _OPENMP + int nthreads = (*this)["nthreads"].integer(); + if (nthreads > 0) { + omp_set_num_threads(nthreads); + } + #endif + // Write parameters into logger log_parameters(TERSE); diff --git a/src/cterror/cterror.par b/src/cterror/cterror.par index c7cbfbe..643879f 100755 --- a/src/cterror/cterror.par +++ b/src/cterror/cterror.par @@ -40,8 +40,9 @@ max_iter, i, h, 50,1,1000, "Maximum number of iterations" # # Standard parameters #==================== -chatter, i, h, 2,0,4, "Chattiness of output" -clobber, b, h, yes,,, "Overwrite existing output files with new output files?" -debug, b, h, no,,, "Activate debugging mode?" -mode, s, h, ql,,, "Mode of automatic parameters" -logfile, f, h, cterror.log,,, "Log filename" +nthreads, i, h, 0,0,1000, "Number of parallel processes (0=use all available CPUs)" +chatter, i, h, 2,0,4, "Chattiness of output" +clobber, b, h, yes,,, "Overwrite existing output files with new output files?" +debug, b, h, no,,, "Activate debugging mode?" +mode, s, h, ql,,, "Mode of automatic parameters" +logfile, f, h, cterror.log,,, "Log filename" diff --git a/src/ctlike/ctlike.cpp b/src/ctlike/ctlike.cpp index 4f892d4..a27c1c7 100755 --- a/src/ctlike/ctlike.cpp +++ b/src/ctlike/ctlike.cpp @@ -32,6 +32,11 @@ #include "ctlike.hpp" #include "GTools.hpp" +/* __ OpenMP section _____________________________________________________ */ +#ifdef _OPENMP +#include +#endif + /* __ Method name definitions ____________________________________________ */ /* __ Debug definitions __________________________________________________ */ @@ -422,6 +427,14 @@ void ctlike::get_parameters(void) static_cast(&m_opt)->logger(NULL); } + // Set number of OpenMP threads + #ifdef _OPENMP + int nthreads = (*this)["nthreads"].integer(); + if (nthreads > 0) { + omp_set_num_threads(nthreads); + } + #endif + // Write parameters into logger log_parameters(TERSE); diff --git a/src/ctlike/ctlike.par b/src/ctlike/ctlike.par index bb107d6..2e24762 100755 --- a/src/ctlike/ctlike.par +++ b/src/ctlike/ctlike.par @@ -38,8 +38,9 @@ fix_spat_for_ts, b, h, no,,, "Fix spatial parameters for TS computation?" # # Standard parameters #==================== -chatter, i, h, 2,0,4, "Chattiness of output" -clobber, b, h, yes,,, "Overwrite existing output files with new output files?" -debug, b, h, no,,, "Activate debugging mode?" -mode, s, h, ql,,, "Mode of automatic parameters" -logfile, f, h, ctlike.log,,, "Log filename" +nthreads, i, h, 0,0,1000, "Number of parallel processes (0=use all available CPUs)" +chatter, i, h, 2,0,4, "Chattiness of output" +clobber, b, h, yes,,, "Overwrite existing output files with new output files?" +debug, b, h, no,,, "Activate debugging mode?" +mode, s, h, ql,,, "Mode of automatic parameters" +logfile, f, h, ctlike.log,,, "Log filename" -- libgit2 0.21.4