Commit 4ceb2c5dd2cf45b4279ca75fdbb5838ed9b419d7

Authored by Jürgen Knödlseder
1 parent e7a1ac61

Make also ctool::create_energies thread save

Showing 1 changed file with 64 additions and 51 deletions
src/support/ctool.cpp
@@ -733,65 +733,78 @@ GEnergies ctool::create_energies(void) @@ -733,65 +733,78 @@ GEnergies ctool::create_energies(void)
733 // If no extension name was provided then use default extension names 733 // If no extension name was provided then use default extension names
734 if (!ebinfile.has_extname()) { 734 if (!ebinfile.has_extname()) {
735 735
736 - // Open energy boundary file using the EBOUNDS or ENERGYBINS  
737 - // extension or energy values using the ENERGIES extension.  
738 - // Throw an exception if opening fails.  
739 - GFits file(ebinfile.url());  
740 - if (file.contains(gammalib::extname_energies)) {  
741 - file.close();  
742 - energies.load(ebinfile);  
743 - }  
744 - else if (file.contains(gammalib::extname_ebounds)) {  
745 - file.close();  
746 - energies.set(GEbounds(ebinfile));  
747 - }  
748 - else if (file.contains("ENERGYBINS")) {  
749 - file.close();  
750 - ebinfile = ebinfile.url() + "[ENERGYBINS]";  
751 - energies.set(GEbounds(ebinfile));  
752 - }  
753 - else {  
754 - file.close();  
755 - std::string msg = "No extension with name \""+  
756 - gammalib::extname_energies+"\", "  
757 - "\"ENERGYBINS\" or \""+  
758 - gammalib::extname_ebounds+"\" found in FITS "  
759 - "file \""+ebinfile+"\". Please specify a "  
760 - "valid energy binning file.";  
761 - throw GException::invalid_value(G_CREATE_ENERGIES, msg);  
762 - } 736 + // Make loading of energy boundaries OpenMP thread save
  737 + #pragma omp critical(ctool_create_energies1)
  738 + {
  739 +
  740 + // Open energy boundary file using the EBOUNDS or ENERGYBINS
  741 + // extension or energy values using the ENERGIES extension.
  742 + // Throw an exception if opening fails.
  743 + GFits file(ebinfile.url());
  744 + if (file.contains(gammalib::extname_energies)) {
  745 + file.close();
  746 + energies.load(ebinfile);
  747 + }
  748 + else if (file.contains(gammalib::extname_ebounds)) {
  749 + file.close();
  750 + energies.set(GEbounds(ebinfile));
  751 + }
  752 + else if (file.contains("ENERGYBINS")) {
  753 + file.close();
  754 + ebinfile = ebinfile.url() + "[ENERGYBINS]";
  755 + energies.set(GEbounds(ebinfile));
  756 + }
  757 + else {
  758 + file.close();
  759 + std::string msg = "No extension with name \""+
  760 + gammalib::extname_energies+"\", "
  761 + "\"ENERGYBINS\" or \""+
  762 + gammalib::extname_ebounds+"\" found in FITS "
  763 + "file \""+ebinfile+"\". Please specify a "
  764 + "valid energy binning file.";
  765 + throw GException::invalid_value(G_CREATE_ENERGIES, msg);
  766 + }
  767 +
  768 + } // end: omp critical section
  769 +
763 } 770 }
764 771
765 // ... otherwise load energy boundaries from filename including 772 // ... otherwise load energy boundaries from filename including
766 // extension 773 // extension
767 else { 774 else {
768 775
769 - // Open energy boundary file  
770 - GFits file(ebinfile.url());  
771 -  
772 - // If FITS file does not contain requested extension then throw  
773 - // an exception  
774 - if (!file.contains(ebinfile.extname())) {  
775 - std::string msg = "No extension \""+ebinfile.extname()+"\" "  
776 - "found in energy node file \""+  
777 - ebinfile.url()+"\". Please provide a valid "  
778 - "extension name.";  
779 - throw GException::invalid_value(G_CREATE_ENERGIES, msg);  
780 - } 776 + // Make loading of energy boundaries OpenMP thread save
  777 + #pragma omp critical(ctool_create_energies2)
  778 + {
781 779
782 - // Get table extension  
783 - GFitsTable& table = *file.table(ebinfile.extname()); 780 + // Open energy boundary file
  781 + GFits file(ebinfile.url());
784 782
785 - // If table contains one column then load energies, otherwise  
786 - // load energy boundaries.  
787 - if (table.ncols() == 1) {  
788 - file.close();  
789 - energies.load(ebinfile);  
790 - }  
791 - else {  
792 - file.close();  
793 - energies.set(GEbounds(ebinfile));  
794 - } 783 + // If FITS file does not contain requested extension then throw
  784 + // an exception
  785 + if (!file.contains(ebinfile.extname())) {
  786 + std::string msg = "No extension \""+ebinfile.extname()+"\" "
  787 + "found in energy node file \""+
  788 + ebinfile.url()+"\". Please provide a valid "
  789 + "extension name.";
  790 + throw GException::invalid_value(G_CREATE_ENERGIES, msg);
  791 + }
  792 +
  793 + // Get table extension
  794 + GFitsTable& table = *file.table(ebinfile.extname());
  795 +
  796 + // If table contains one column then load energies, otherwise
  797 + // load energy boundaries.
  798 + if (table.ncols() == 1) {
  799 + file.close();
  800 + energies.load(ebinfile);
  801 + }
  802 + else {
  803 + file.close();
  804 + energies.set(GEbounds(ebinfile));
  805 + }
  806 +
  807 + } // end: omp critical section
795 808
796 } // endelse: loaded energy boundaries from table extension 809 } // endelse: loaded energy boundaries from table extension
797 810