简体   繁体   English

如何在我的 MEX 文件中使用 OpenMP 而不会导致 MATLAB 崩溃?

[英]How to use OpenMP in my MEX-file without crashing MATLAB?

I built a Windows MEX-file from Visual Studio 2019. My MATLAB version is R2019a.我从 Visual Studio 2019 构建了一个 Windows MEX 文件。我的 MATLAB 版本是 R2019a。 The code just does some "sort" to the two input matlab array.该代码只是对两个输入 matlab 数组进行了一些“排序”。 This is an example which I try to use with OpenMP.这是我尝试与 OpenMP 一起使用的示例。 "test.cpp" “测试.cpp”

#include "mex.h"
#include <vector>
#include <iostream>
#include <algorithm>
#include <omp.h>
using namespace std;
void mexFunction(int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[])
{ 
    mxDouble *p0 = mxGetDoubles(prhs[0]);
    mwSize size0 = mxGetNumberOfElements(prhs[0]);
    mxDouble* p1 = mxGetDoubles(prhs[1]);
    mwSize size1 = mxGetNumberOfElements(prhs[1]);
    vector<vector<int>> v(2);
    int i;
    for (i = 0; i < size0; i++) {
        v[0].push_back(int(*p0++));
    };
    for (i = 0; i < size1; i++) {
        v[1].push_back(int(*p1++));
    };
#pragma omp parallel for 
    for (i = 0; i < 2; i++) {
        sort(v[i].begin(), v[i].end());
    };


    plhs[0] = mxCreateCellMatrix(2, 1);
    for (int i = 0; i < 2; i++) {
        mxArray* str = mxCreateNumericMatrix(1, v[i].size(), mxDOUBLE_CLASS, mxREAL);
        copy(v[i].begin(), v[i].end(), mxGetPr(str));
        mxSetCell(plhs[0], i, mxDuplicateArray(str));
        mxDestroyArray(str);
    };
    return;
}

My test M-file is:我的测试 M 文件是:

a1= [1,2,3,4,5];
a2= [1,1,1,1];
result = test(a1,a2);
clear mex;

The ideal output will be a 2*1 cell in result .理想的 output result将是 2*1 单元格。 If I remove the # pragma line the MEX-file works well.如果我删除# pragma行,则 MEX 文件运行良好。 If I add it,MATLAB crashed.如果我添加它,MATLAB 崩溃了。 I have been trying to find the problem for several days and have not find any solution.几天来我一直试图找到问题并没有找到任何解决方案。 Besides,除了,

  1. I have choose the OpenMP support key to "yes" in my Visual Studio.我已在 Visual Studio 中选择 OpenMP 支持键为“是”。
  2. I don't use mexPrintf in my code.我不在我的代码中使用mexPrintf I heard that function will crash the MEX-file with OpenMP.我听说 function 会使用 OpenMP 使 MEX 文件崩溃。
  3. I have installed the Intel Studio parallel XE 2019 and update 5 of Matlab R2019a.我已经安装了 Intel Studio 并行 XE 2019 并更新了 Matlab R2019a 的 5。 Perhaps I made a wrong parallel domain definition.也许我做了一个错误的并行域定义。 I don't want to include the last part from plhs[0] = mxCreateCellMatrix(2, 1);我不想包含plhs[0] = mxCreateCellMatrix(2, 1);的最后一部分into the #pragma parallel part.进入#pragma并行部分。 As the codes above, am I do it right?如上面的代码,我做对了吗?

You cannot call any mx... or mex... functions in the parallel part of your code*.您不能在代码的并行部分调用任何mx...mex...函数*。 You must get pointers to the all the data before the parallel section, including creating output arrays and getting their pointers, then do only your computations in parallel.您必须在并行部分之前获取指向所有数据的指针,包括创建 output arrays 并获取它们的指针,然后仅并行执行计算。


* The rule given here is overly broad, some mx... functions might be OK to use in a parallel section, as indicated in James Tursa's comment . * 这里给出的规则过于宽泛,一些mx...函数可能可以在并行部分中使用,如James Tursa 的评论所示。 I prefer not guessing and keeping the rules simple.我不喜欢猜测并保持规则简单。

This may not be the perfect solution.这可能不是完美的解决方案。 I just made some correction for the codes and succeeded in building the mexw64 inside Matlab.我刚刚对代码进行了一些更正,并成功在 Matlab 中构建了 mexw64。 As for compiling the code in visual studio, the mex rans very low.(VS built 15s and Matlab built 0.4sI could not figure out why. ) At first, I corrected two critical errors.至于在visual studio中编译代码,mex跑得很低。(VS构建15s和Matlab构建0.4s我不知道为什么。)首先,我纠正了两个严重错误。

  1. using the curly brace for #pragma... correctly.正确使用#pragma...的花括号。 The left curly brace must not directly after #pragma omp parallel .左花括号不能直接在#pragma omp parallel之后。 It should be in a new line.它应该在一个新行中。 As below,如下,
#pragma omp parallel
{ 
#pragma omp for
    for (i = 0; i < 2; i++) {
        sort(v[i].begin(), v[i].end());
    };
}

2. In the parallel loop, I can't use the mwIndex type index. 2.在并行循环中,不能使用mwIndex类型的索引。 It must be int type.它必须是int类型。 This error not happens in the codes in these questions.这些问题的代码中不会发生此错误。 I just note it down.我只是记下来。

Then I used mex -v COMPFLAGS="$COMPFLAGS /openmp" test.cpp;然后我用mex -v COMPFLAGS="$COMPFLAGS /openmp" test.cpp; to build the mex file.构建 mex 文件。 I just suggest not to build and apply the mex in one m file.我只是建议不要在一个 m 文件中构建和应用 mex。 My Matlab crashed sometimes.我的 Matlab 有时会崩溃。 I don't know the reason.我不知道原因。 I just succeeded in the way separating them, even sounds very weird.我刚刚成功地将它们分开,甚至听起来很奇怪。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM