为什么在 mex 文件中 OpenMP 只产生 1 个线程? [英] Why is OpenMP in a mex file only producing 1 thread?

查看:70
本文介绍了为什么在 mex 文件中 OpenMP 只产生 1 个线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是OpenMP的新手.我有以下代码,可以使用通过MSVS2010配置的Matlab mex进行编译.该计算机有8个可用处理器(我也通过使用matlabpool检查了这些处理器).

I am new to OpenMP. I have the following code which compiles fine using Matlab mex configured with MSVS2010. The computer has 8 processors available (which I checked also by using matlabpool).

#include "mex.h"
#include <omp.h>

typedef unsigned char uchar;
typedef unsigned int uint;
//Takes a uint8 input array and uint32 index array and preallocated uint8 array the same
//size as the first one and copies the data over using the indexed mapping
void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray*prhs[] ) 
{
    uint N = mxGetN(prhs[0]);
    mexPrintf("n=%i\n", N); mexEvalString("drawnow");
    uchar *input = (uchar*)mxGetData(prhs[0]);
    uint *index = (uint*)mxGetData(prhs[1]);
    uchar *output = (uchar*)mxGetData(prhs[2]);

    uint nThreads, tid;
#pragma omp parallel private(tid) shared(input, index, output, N, nThreads) num_threads(8) 
    {
        tid = omp_get_thread_num();

        if (tid==0) {
            nThreads = omp_get_num_threads();

        }

        for (int i=tid*N/nThreads;i<tid*N/nThreads+N/nThreads;i++){
            output[i]=input[index[i]];
        }
    }
    mexPrintf("nThreads = %i\n",nThreads);mexEvalString("drawnow");
}

我得到的输出是

n=600000000
nThreads = 1

为什么我请求8个线程却只创建一个线程?

Why is only one thread being created despite me requesting 8?

推荐答案

感叹.通常,花费数小时尝试失败,然后在发布到SO上5分钟后找到答案.

Sigh. Typical, spend hours trying and failing and then find the answer 5 minutes after posting to SO.

文件需要与openmp支持混在一起

The file needs to be mexed with openmp support

mex mexIndexedCopy.cpp COMPFLAGS="/openmp $COMPFLAGS"

这篇关于为什么在 mex 文件中 OpenMP 只产生 1 个线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆