Reputation: 1532
I am building an c++ application that uses nested omp. It however causes crash. The problem is solved when either one of the two omp is removed, or the wait function is inside the main file itself. OS is MacOS X Lion, compiler should be either llvm-gcc or gcc-4.2 (I am not sure, simply used cmake...) I then built the following app to demonstrate:
EDIT: I now tried the same on a linux machine, it works fine. So it's a pure MACOS X (lion) issue.
OMP_NESTED is set to true.
The main:
#include "waiter.h"
#include "iostream"
#include "time.h"
#include <omp.h>
void wait(){
int seconds = 1;
#pragma omp parallel for
for (int i=0;i<2;i++){
clock_t endwait;
endwait = clock () + seconds * CLOCKS_PER_SEC ;
while (clock() < endwait) {}
std::cout << i << "\n";
}
}
int main(){
std::cout << "blub\n";
#pragma omp parallel for
for(int i=0;i<5;i++){
Waiter w; // causes crash
// wait(); // works
}
std::cout << "blub\n";
return 0;
}
header:
#ifndef WAITER_H_
#define WAITER_H_
class Waiter {
public:
Waiter ();
};
#endif // WAITER_H_
implementation:
#include "waiter.h"
#include "omp.h"
#include "time.h"
#include <iostream>
Waiter::Waiter(){
int seconds = 1;
#pragma omp parallel for
for (int i=0;i<5;i++){
clock_t endwait;
endwait = clock () + seconds * CLOCKS_PER_SEC ;
while (clock() < endwait) {}
std::cout << i << "\n";
}
}
CMakeLists.txt:
cmake_minimum_required (VERSION 2.6)
project (waiter)
set(CMAKE_CXX_FLAGS "-fPIC -fopenmp")
set(CMAKE_C_FLAGS "-fPIC -fopenmp")
set(CMAKE_SHARED_LINKDER_FLAGS "-fPIC -fopenmp")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib)
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin)
add_library(waiter SHARED waiter.cpp waiter.h)
add_executable(use_waiter use_waiter.cpp)
target_link_libraries(use_waiter waiter)
thanks for help!
Upvotes: 1
Views: 997
Reputation: 8854
EDIT: rewritten with more details.
openmp causes intermittent failure on gcc 4.2, but it is fixed by gcc 4.6.1 (or perhaps 4.6). You can get the 4.6.1 binary from http://hpc.sourceforge.net/ (look for gcc-lion.tar.gz
).
The failure of openmp in lion with less than gcc 4.6.1 is intermittent. It seems to happen after many openmp calls, so is likely made more likely by nesting but nesting is not required. This link doesn't have nested openmp (there is a parallel for within a standard single threaded for) but fails. My own code had intermittent hanging or crashing due to openmp after many minutes of working fine with gcc 4.2 (with no nested pragmas) in lion and was completely fixed by gcc 4.6.1.
I downloaded your code and compiled it with gcc 4.2 and it ran fine on my machine (with both the Waiter w;
and wait();
options :-). I just used:
g++ -v -fPIC -fopenmp use_waiter.cpp waiter.cpp -o waiter
I tried increasing the loop maxes but still couldn't get it to fail. I see both the starting and ending blub
.
What error message do you see?
Are you sure that the gcc 4.6 you downloaded is being used (use -v
to make sure)?
See also here.
Upvotes: 2