Parallel processing Boost.python和OMP

Parallel processing Boost.python和OMP,parallel-processing,openmp,boost-python,Parallel Processing,Openmp,Boost Python,我不明白为什么用OMP编译下面的代码(chi2距离)需要更长的时间。在此之后,我发布了GIL,但仍然没有任何改进 np::ndarray additive_chi2_kernel(const np::ndarray& _h0, const np::ndarray& _h1) { auto dtype = np::dtype::get_builtin<float>(); auto h0 = _h0.astype(dtype); auto

我不明白为什么用OMP编译下面的代码(chi2距离)需要更长的时间。在此之后,我发布了GIL,但仍然没有任何改进

np::ndarray additive_chi2_kernel(const np::ndarray& _h0, 
              const np::ndarray& _h1) {

auto dtype = np::dtype::get_builtin<float>();

auto h0 = _h0.astype(dtype);
auto h1 = _h1.astype(dtype);

enter code here

assert (h0.get_nd() == 2 && h1.get_nd() == 2);

float* ptr_h0  = reinterpret_cast<float*>(h0.get_data());
float* ptr_h1  = reinterpret_cast<float*>(h1.get_data());

int M0 = h0.shape(0);
int M1 = h1.shape(0);
int N  = h1.shape(1);

float* result = new float[M0*M1]();

auto save_state = PyEval_SaveThread();

#pragma omp parallel
for(int m0 = 0; m0 < M0; ++m0) {
    for(int m1 = 0; m1 < M1; ++m1) {
        float error = 0;
        for(int i = 0; i < N; ++i) {
            float sum  = ptr_h0[m0*N+i] + ptr_h1[m1*N+i];
            if (sum != 0){
                float diff = ptr_h0[m0*N+i] - ptr_h1[m1*N+i];
                error += (diff*diff/sum);
            }
        }
        result[m0*M1+m1] = error;
    }
}

  PyEval_RestoreThread(save_state);

    np::ndarray D = np::from_data(result,np::dtype::get_builtin<float>(),
      py::make_tuple(M0,M1),py::make_tuple(sizeof(float)*M1,
       sizeof(float)), py::object());

return D;

}
编译器标志:
-std=c++11-Wall-fopenmp-O3-fPIC

所有线程都处于活动状态

     PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND                                                                                                                                                                 
   20706 memecs    20   0 3980080 818004  42880 R  3179  0.6   7:34.04 python
但是运行时间比单核上的运行时间要长。对于M0=100,M1=1000,N=1000,我得到

OMP: 2.214 seconds
SINGLE-CORE: 1.175 seconds.

可能的问题

您应该使用
#pragma omp parallel for
在线程之间划分外循环<代码>#pragma omp parallel只生成一组线程,但每个线程都在计算所有内容。

当然。这样一个幼稚的错误:)我正朝着一个完全不同的方向看。
OMP: 2.214 seconds
SINGLE-CORE: 1.175 seconds.