簡體   English   中英

使用unique_ptr <CCfits::FITS> 在omp並行for循環中導致SEG.FAULT

[英]Using unique_ptr<CCfits::FITS> in omp parallel for loop results in SEG.FAULT

采取以下代碼:

#include <omp.h>
#include <CCfits/CCfits>
#include <sys/stat.h>

int main ()
{
  std::string file ( "somefits.fits" );
  std::cout << file  << std::endl;

  // check if file exists
  struct stat bf;
  if ( stat(file.c_str(),&bf ) ) 
  { 
    std::cerr << "Error: the file: " << file << " does not exist " << std::endl;
    exit(-1);
  }

  #pragma omp parallel for schedule (dynamic)
  for ( uint i = 0; i<4; ++i )
  {
    std::unique_ptr<CCfits::FITS> fp (new CCfits::FITS (file, CCfits::Read));
    // do something
  }
  return 0;
}

第一部分要注意文件是否存在。 然后,我想在for循環中使用CCfits :: FITS文件(或其他文件),該文件必須是並行的。 在沒有並行化行的情況下運行代碼非常有效。

我以不同的方式運行代碼,請看下面。

  1. 為什么在此簡單示例中並行化失敗?
  2. 為什么會有90%的SEG。 故障和10%可以
  3. 如何解決這個問題?
  4. 我可以解決這個問題嗎?

I.簡單地運行代碼將產生90%的SEG。 故障10%可以

II。 使用valgrind --leak-check = full ./code運行代碼會導致

==6654==    by 0x4EC1C11: CCfits::HDUCreator::Make(int, bool, std::vector<std::string, std::allocator<std::string> > const&) (in /usr/lib/x86_64-linux-gnu/libCCfits.so.0.0.0)
==6654==    by 0x4EB490F: CCfits::FITS::readExtensions(bool) (in /usr/lib/x86_64-linux-gnu/libCCfits.so.0.0.0)
==6654==    by 0x4EB4FA3: CCfits::FITS::FITS(std::string const&, CCfits::RWmode, bool, std::vector<std::string, std::allocator<std::string> > const&) (in /usr/lib/x86_64-linux-gnu/libCCfits.so.0.0.0)
==6654==    by 0x4013C4: main._omp_fn.0 (ccfits_omp.cpp:21)
==6654==    by 0x540C349: ??? (in /usr/lib/x86_64-linux-gnu/libgomp.so.1.0.0)
==6654==    by 0x4C30E7B: ??? (in /usr/lib/valgrind/vgpreload_drd-amd64-linux.so)
==6654== Other segment start (thread 2)
==6654==    at 0x4C34544: pthread_mutex_unlock (in /usr/lib/valgrind/vgpreload_drd-amd64-linux.so)
==6654==    by 0x5C09DA1: fits_store_Fptr (in /usr/lib/x86_64-linux-gnu/libcfitsio.so.3.340)
==6654==    by 0x5C15042: ffopen (in /usr/lib/x86_64-linux-gnu/libcfitsio.so.3.340)
==6654==    by 0x4EB2997: CCfits::FITS::open(CCfits::RWmode) (in /usr/lib/x86_64-linux-gnu/libCCfits.so.0.0.0)
==6654==    by 0x4EB4F84: CCfits::FITS::FITS(std::string const&, CCfits::RWmode, bool, std::vector<std::string, std::allocator<std::string> > const&) (in /usr/lib/x86_64-linux-gnu/libCCfits.so.0.0.0)
==6654==    by 0x4013C4: main._omp_fn.0 (ccfits_omp.cpp:21)
==6654==    by 0x540C349: ??? (in /usr/lib/x86_64-linux-gnu/libgomp.so.1.0.0)
==6654==    by 0x4C30E7B: ??? (in /usr/lib/valgrind/vgpreload_drd-amd64-linux.so)
==6654==    by 0x62B5181: start_thread (pthread_create.c:312)
==6654==    by 0x5923EFC: clone (clone.S:111)
==6654== Other segment end (thread 2)
==6654==    at 0x4C338E3: pthread_mutex_lock (in /usr/lib/valgrind/vgpreload_drd-amd64-linux.so)
==6654==    by 0x5C09E1E: fits_clear_Fptr (in /usr/lib/x86_64-linux-gnu/libcfitsio.so.3.340)
==6654==    by 0x5C1066E: ffclos (in /usr/lib/x86_64-linux-gnu/libcfitsio.so.3.340)
==6654==    by 0x4EB2DE5: CCfits::FITS::close() (in /usr/lib/x86_64-linux-gnu/libCCfits.so.0.0.0)
==6654==    by 0x4EB32FD: CCfits::FITS::destroy() (in /usr/lib/x86_64-linux-gnu/libCCfits.so.0.0.0)
==6654==    by 0x4017E7: std::default_delete<CCfits::FITS>::operator()(CCfits::FITS*) const (unique_ptr.h:67)
==6654==    by 0x401680: std::unique_ptr<CCfits::FITS, std::default_delete<CCfits::FITS> >::~unique_ptr() (unique_ptr.h:184)
==6654==    by 0x4013EB: main._omp_fn.0 (ccfits_omp.cpp:21)
==6654==    by 0x540C349: ??? (in /usr/lib/x86_64-linux-gnu/libgomp.so.1.0.0)
==6654==    by 0x4C30E7B: ??? (in /usr/lib/valgrind/vgpreload_drd-amd64-linux.so)
==6654==    by 0x62B5181: start_thread (pthread_create.c:312)
==6654==    by 0x5923EFC: clone (clone.S:111)
==6654== 
==6654== 
==6654== For counts of detected and suppressed errors, rerun with: -v
==6654== ERROR SUMMARY: 53 errors from 19 contexts (suppressed: 495 from 333)

III。 使用valgrind --tool = drd ./code將產生以下結果:

==6654==    by 0x4EC1C11: CCfits::HDUCreator::Make(int, bool, std::vector<std::string, std::allocator<std::string> > const&) (in /usr/lib/x86_64-linux-gnu/libCCfits.so.0.0.0)
==6654==    by 0x4EB490F: CCfits::FITS::readExtensions(bool) (in /usr/lib/x86_64-linux-gnu/libCCfits.so.0.0.0)
==6654==    by 0x4EB4FA3: CCfits::FITS::FITS(std::string const&, CCfits::RWmode, bool, std::vector<std::string, std::allocator<std::string> > const&) (in /usr/lib/x86_64-linux-gnu/libCCfits.so.0.0.0)
==6654==    by 0x4013C4: main._omp_fn.0 (ccfits_omp.cpp:21)
==6654==    by 0x540C349: ??? (in /usr/lib/x86_64-linux-gnu/libgomp.so.1.0.0)
==6654==    by 0x4C30E7B: ??? (in /usr/lib/valgrind/vgpreload_drd-amd64-linux.so)
==6654== Other segment start (thread 2)
==6654==    at 0x4C34544: pthread_mutex_unlock (in /usr/lib/valgrind/vgpreload_drd-amd64-linux.so)
==6654==    by 0x5C09DA1: fits_store_Fptr (in /usr/lib/x86_64-linux-gnu/libcfitsio.so.3.340)
==6654==    by 0x5C15042: ffopen (in /usr/lib/x86_64-linux-gnu/libcfitsio.so.3.340)
==6654==    by 0x4EB2997: CCfits::FITS::open(CCfits::RWmode) (in /usr/lib/x86_64-linux-gnu/libCCfits.so.0.0.0)
==6654==    by 0x4EB4F84: CCfits::FITS::FITS(std::string const&, CCfits::RWmode, bool, std::vector<std::string, std::allocator<std::string> > const&) (in /usr/lib/x86_64-linux-gnu/libCCfits.so.0.0.0)
==6654==    by 0x4013C4: main._omp_fn.0 (ccfits_omp.cpp:21)
==6654==    by 0x540C349: ??? (in /usr/lib/x86_64-linux-gnu/libgomp.so.1.0.0)
==6654==    by 0x4C30E7B: ??? (in /usr/lib/valgrind/vgpreload_drd-amd64-linux.so)
==6654==    by 0x62B5181: start_thread (pthread_create.c:312)
==6654==    by 0x5923EFC: clone (clone.S:111)
==6654== Other segment end (thread 2)
==6654==    at 0x4C338E3: pthread_mutex_lock (in /usr/lib/valgrind/vgpreload_drd-amd64-linux.so)
==6654==    by 0x5C09E1E: fits_clear_Fptr (in /usr/lib/x86_64-linux-gnu/libcfitsio.so.3.340)
==6654==    by 0x5C1066E: ffclos (in /usr/lib/x86_64-linux-gnu/libcfitsio.so.3.340)
==6654==    by 0x4EB2DE5: CCfits::FITS::close() (in /usr/lib/x86_64-linux-gnu/libCCfits.so.0.0.0)
==6654==    by 0x4EB32FD: CCfits::FITS::destroy() (in /usr/lib/x86_64-linux-gnu/libCCfits.so.0.0.0)
==6654==    by 0x4017E7: std::default_delete<CCfits::FITS>::operator()(CCfits::FITS*) const (unique_ptr.h:67)
==6654==    by 0x401680: std::unique_ptr<CCfits::FITS, std::default_delete<CCfits::FITS> >::~unique_ptr() (unique_ptr.h:184)
==6654==    by 0x4013EB: main._omp_fn.0 (ccfits_omp.cpp:21)
==6654==    by 0x540C349: ??? (in /usr/lib/x86_64-linux-gnu/libgomp.so.1.0.0)
==6654==    by 0x4C30E7B: ??? (in /usr/lib/valgrind/vgpreload_drd-amd64-linux.so)
==6654==    by 0x62B5181: start_thread (pthread_create.c:312)
==6654==    by 0x5923EFC: clone (clone.S:111)
==6654== 
==6654== 
==6654== For counts of detected and suppressed errors, rerun with: -v
==6654== ERROR SUMMARY: 53 errors from 19 contexts (suppressed: 495 from 333)

好吧,您正在使用許多不良做法,即使我對Fits一無所知,我仍然可以看到:

  • 您正在創建4個執行相同操作的線程:讀取一個文件,該文件被讀取4次並且是SAME文件。 一次讀取並共享CONST數據會更好嗎?
  • 如果Fits跟蹤已經打開的文件(我不知道),則可能沒有為多線程做准備。

    對不起我的英語不好。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM