From 4ceb2c5dd2cf45b4279ca75fdbb5838ed9b419d7 Mon Sep 17 00:00:00 2001 From: Jürgen Knödlseder Date: Tue, 25 Jun 2019 17:23:59 +0200 Subject: [PATCH] Make also ctool::create_energies thread save --- src/support/ctool.cpp | 115 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------------------------------------------------- 1 file changed, 64 insertions(+), 51 deletions(-) diff --git a/src/support/ctool.cpp b/src/support/ctool.cpp index 9f977e8..01ec034 100755 --- a/src/support/ctool.cpp +++ b/src/support/ctool.cpp @@ -733,65 +733,78 @@ GEnergies ctool::create_energies(void) // If no extension name was provided then use default extension names if (!ebinfile.has_extname()) { - // Open energy boundary file using the EBOUNDS or ENERGYBINS - // extension or energy values using the ENERGIES extension. - // Throw an exception if opening fails. - GFits file(ebinfile.url()); - if (file.contains(gammalib::extname_energies)) { - file.close(); - energies.load(ebinfile); - } - else if (file.contains(gammalib::extname_ebounds)) { - file.close(); - energies.set(GEbounds(ebinfile)); - } - else if (file.contains("ENERGYBINS")) { - file.close(); - ebinfile = ebinfile.url() + "[ENERGYBINS]"; - energies.set(GEbounds(ebinfile)); - } - else { - file.close(); - std::string msg = "No extension with name \""+ - gammalib::extname_energies+"\", " - "\"ENERGYBINS\" or \""+ - gammalib::extname_ebounds+"\" found in FITS " - "file \""+ebinfile+"\". Please specify a " - "valid energy binning file."; - throw GException::invalid_value(G_CREATE_ENERGIES, msg); - } + // Make loading of energy boundaries OpenMP thread save + #pragma omp critical(ctool_create_energies1) + { + + // Open energy boundary file using the EBOUNDS or ENERGYBINS + // extension or energy values using the ENERGIES extension. + // Throw an exception if opening fails. + GFits file(ebinfile.url()); + if (file.contains(gammalib::extname_energies)) { + file.close(); + energies.load(ebinfile); + } + else if (file.contains(gammalib::extname_ebounds)) { + file.close(); + energies.set(GEbounds(ebinfile)); + } + else if (file.contains("ENERGYBINS")) { + file.close(); + ebinfile = ebinfile.url() + "[ENERGYBINS]"; + energies.set(GEbounds(ebinfile)); + } + else { + file.close(); + std::string msg = "No extension with name \""+ + gammalib::extname_energies+"\", " + "\"ENERGYBINS\" or \""+ + gammalib::extname_ebounds+"\" found in FITS " + "file \""+ebinfile+"\". Please specify a " + "valid energy binning file."; + throw GException::invalid_value(G_CREATE_ENERGIES, msg); + } + + } // end: omp critical section + } // ... otherwise load energy boundaries from filename including // extension else { - // Open energy boundary file - GFits file(ebinfile.url()); - - // If FITS file does not contain requested extension then throw - // an exception - if (!file.contains(ebinfile.extname())) { - std::string msg = "No extension \""+ebinfile.extname()+"\" " - "found in energy node file \""+ - ebinfile.url()+"\". Please provide a valid " - "extension name."; - throw GException::invalid_value(G_CREATE_ENERGIES, msg); - } + // Make loading of energy boundaries OpenMP thread save + #pragma omp critical(ctool_create_energies2) + { - // Get table extension - GFitsTable& table = *file.table(ebinfile.extname()); + // Open energy boundary file + GFits file(ebinfile.url()); - // If table contains one column then load energies, otherwise - // load energy boundaries. - if (table.ncols() == 1) { - file.close(); - energies.load(ebinfile); - } - else { - file.close(); - energies.set(GEbounds(ebinfile)); - } + // If FITS file does not contain requested extension then throw + // an exception + if (!file.contains(ebinfile.extname())) { + std::string msg = "No extension \""+ebinfile.extname()+"\" " + "found in energy node file \""+ + ebinfile.url()+"\". Please provide a valid " + "extension name."; + throw GException::invalid_value(G_CREATE_ENERGIES, msg); + } + + // Get table extension + GFitsTable& table = *file.table(ebinfile.extname()); + + // If table contains one column then load energies, otherwise + // load energy boundaries. + if (table.ncols() == 1) { + file.close(); + energies.load(ebinfile); + } + else { + file.close(); + energies.set(GEbounds(ebinfile)); + } + + } // end: omp critical section } // endelse: loaded energy boundaries from table extension -- libgit2 0.21.4