Commit e53da543e35aa4ed0bada135f98bd0c4277fbc9f

Authored by Jürgen Knödlseder
1 parent 2d011293

Fix OpenMP issue in ctbin when using energy bounds from file (#2912).

Showing 1 changed file with 64 additions and 51 deletions
src/support/ctool.cpp
... ... @@ -589,65 +589,78 @@ GEbounds ctool::create_ebounds(void)
589 589 // If no extension name was provided then use default extension names
590 590 if (!ebinfile.has_extname()) {
591 591  
592   - // Open energy boundary file using the EBOUNDS or ENERGYBINS
593   - // extension or energy values using the ENERGIES extension.
594   - // Throw an exception if opening fails.
595   - GFits file(ebinfile.url());
596   - if (file.contains(gammalib::extname_ebounds)) {
597   - file.close();
598   - ebounds.load(ebinfile);
599   - }
600   - else if (file.contains("ENERGYBINS")) {
601   - file.close();
602   - ebinfile = ebinfile.url() + "[ENERGYBINS]";
603   - ebounds.load(ebinfile);
604   - }
605   - else if (file.contains(gammalib::extname_energies)) {
606   - file.close();
607   - ebounds.set(GEnergies(ebinfile));
608   - }
609   - else {
610   - file.close();
611   - std::string msg = "No extension with name \""+
612   - gammalib::extname_ebounds+"\", "
613   - "\"ENERGYBINS\" or \""+
614   - gammalib::extname_energies+"\" found in FITS "
615   - "file \""+ebinfile+"\". Please specify a "
616   - "valid energy binning file.";
617   - throw GException::invalid_value(G_CREATE_EBOUNDS, msg);
618   - }
619   - }
  592 + // Make loading of energy boundaries OpenMP thread save
  593 + #pragma omp critical(ctool_create_ebounds1)
  594 + {
  595 +
  596 + // Open energy boundary file using the EBOUNDS or ENERGYBINS
  597 + // extension or energy values using the ENERGIES extension.
  598 + // Throw an exception if opening fails.
  599 + GFits file(ebinfile.url());
  600 + if (file.contains(gammalib::extname_ebounds)) {
  601 + file.close();
  602 + ebounds.load(ebinfile);
  603 + }
  604 + else if (file.contains("ENERGYBINS")) {
  605 + file.close();
  606 + ebinfile = ebinfile.url() + "[ENERGYBINS]";
  607 + ebounds.load(ebinfile);
  608 + }
  609 + else if (file.contains(gammalib::extname_energies)) {
  610 + file.close();
  611 + ebounds.set(GEnergies(ebinfile));
  612 + }
  613 + else {
  614 + file.close();
  615 + std::string msg = "No extension with name \""+
  616 + gammalib::extname_ebounds+"\", "
  617 + "\"ENERGYBINS\" or \""+
  618 + gammalib::extname_energies+"\" found in FITS "
  619 + "file \""+ebinfile+"\". Please specify a "
  620 + "valid energy binning file.";
  621 + throw GException::invalid_value(G_CREATE_EBOUNDS, msg);
  622 + }
  623 +
  624 + } // end: omp critical section
  625 +
  626 + } // endif: no extension name specified
620 627  
621 628 // ... otherwise load energy boundaries from filename including
622 629 // extension
623 630 else {
624 631  
625   - // Open energy boundary file
626   - GFits file(ebinfile.url());
  632 + // Make loading of energy boundaries OpenMP thread save
  633 + #pragma omp critical(ctool_create_ebounds2)
  634 + {
  635 +
  636 + // Open energy boundary file
  637 + GFits file(ebinfile.url());
  638 +
  639 + // If FITS file does not contain requested extension then throw
  640 + // an exception
  641 + if (!file.contains(ebinfile.extname())) {
  642 + std::string msg = "No extension \""+ebinfile.extname()+"\" "
  643 + "found in energy binning file \""+
  644 + ebinfile.url()+"\". Please provide a valid "
  645 + "extension name.";
  646 + throw GException::invalid_value(G_CREATE_EBOUNDS, msg);
  647 + }
627 648  
628   - // If FITS file does not contain requested extension then throw
629   - // an exception
630   - if (!file.contains(ebinfile.extname())) {
631   - std::string msg = "No extension \""+ebinfile.extname()+"\" "
632   - "found in energy binning file \""+
633   - ebinfile.url()+"\". Please provide a valid "
634   - "extension name.";
635   - throw GException::invalid_value(G_CREATE_EBOUNDS, msg);
636   - }
  649 + // Get table extension
  650 + GFitsTable& table = *file.table(ebinfile.extname());
637 651  
638   - // Get table extension
639   - GFitsTable& table = *file.table(ebinfile.extname());
  652 + // If table contains one column then load energies, otherwise load
  653 + // energy boundaries.
  654 + if (table.ncols() == 1) {
  655 + file.close();
  656 + ebounds.set(GEnergies(ebinfile));
  657 + }
  658 + else {
  659 + file.close();
  660 + ebounds.load(ebinfile);
  661 + }
640 662  
641   - // If table contains one column then load energies, otherwise load
642   - // energy boundaries.
643   - if (table.ncols() == 1) {
644   - file.close();
645   - ebounds.set(GEnergies(ebinfile));
646   - }
647   - else {
648   - file.close();
649   - ebounds.load(ebinfile);
650   - }
  663 + } // end: omp critical section
651 664  
652 665 } // endelse: loaded energy boundaries from table extension
653 666  
... ...