2014-12-07 4 views
1

의 루프 결과 OMP 평행 : 다음 코드를 가지고 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가있는 이유는 무엇입니까? FAULT 및 10 % OK
  3. 해결 방법?
  4. 해결할 수 있습니까?

I. 단순히 코드를 실행하면 90 % SEG가됩니다. FAULT 10 % OK

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가 표시됩니다.

==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) 

답변

0

글쎄, 당신은 나쁜 관행을 많이 사용하고, 나는 맞는에 대한 anythings를 모르더라도, 내가 볼 수 있습니다 :

다음에 Valgrind의 --tool = DRD ./code 결과를 사용하여
  • 동일한 작업을 수행하는 4 개의 스레드가 생성됩니다. 파일 읽기는 파일이 4 번 읽히고 같은 파일입니다. 한 번 읽고 CONST 데이터를 공유하는 것이 좋을까요?
  • Fits가 이미 열려있는 파일 (내가 모르는 파일)을 추적하는 경우 멀티 스레딩을 위해 준비되지 않은 것이 가능합니다.

    죄송합니다.

관련 문제