You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm writing an algorithm in C++ with OpenMP parallel acceleration, and compile it using pybind11 as a python module. I make some output in the omp parallel for, and redirect the std output to python, but the program just stucked, and I need to send a SIGKILL to stop it.
The C++ code is below:
#include<pybind11/pybind11.h>
#include<pybind11/iostream.h>
#include<mutex>namespacepy= pybind11;
#define_CRT_SECURE_NO_DEPRECATE
#include<omp.h>
#include<iostream>
#include<string>static std::mutex cout_mutex;
intrun() {
#pragma omp parallel for
for (int i = 0; i < 16; i++) {
// std::cout << "index: " << i << std::endl;
{
// #HelpAppreciated: Work on iostream.h thread safety.// Without this lock, the clang ThreadSanitizer (tsan) reliably reports a// data race, and this test is predictably flakey on Windows.// For more background see the discussion under// https://github.com/pybind/pybind11/pull/2982 and// https://github.com/pybind/pybind11/pull/2995.const std::lock_guard<std::mutex> lock(cout_mutex);
std::cout << "index: " << i << std::endl;
}
}
return0;
}
PYBIND11_MODULE(tr, m) {
m.def("run", &run,
py::call_guard<py::scoped_ostream_redirect, py::scoped_estream_redirect>());
}
And the python code to call the module is below:
importtrif__name__=='__main__':
tr.run()
I haved tried to lock the std::cout with mutex and #pramga omp critical block, as mentioned in PR2982PR2995, but failed. Nothing is printed in the console, and the program is stucked.
Is there any mistakes in my code? Or is there other ways to solve the problem?
Thanks.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hi,
I'm writing an algorithm in C++ with OpenMP parallel acceleration, and compile it using pybind11 as a python module. I make some output in the
omp parallel for
, and redirect the std output to python, but the program just stucked, and I need to send a SIGKILL to stop it.The C++ code is below:
And the python code to call the module is below:
I haved tried to lock the
std::cout
withmutex
and#pramga omp critical
block, as mentioned in PR2982 PR2995, but failed. Nothing is printed in the console, and the program is stucked.Is there any mistakes in my code? Or is there other ways to solve the problem?
Thanks.
Beta Was this translation helpful? Give feedback.
All reactions