Search code examples
c++tbb

What cause segmentation fault error when using tbb::parallel_invoke?


I tried using both openmp and intel tbb on ubuntu 18.04 with gcc and g++ version 6 for optimizing my code. I use openmp to optimize for loops and tbb::parallel_invoke to run 1 function with 4 different input.

I tried using lambda expression to pass some argument by reference, but I am not sure whether I wrote that right or not. However, I keep getting "segmentation fault (core dumped)" error when using parallel_invoke.

int shrink = 6;
cv::Mat mat1, mat2;
tbb::parallel_invoke([&]
        {mat1 = myfunction(image1, arg1, arg2) * shrink;}, [&]
        {mat2 = myfunction(image2, arg1, arg2) * shrink;});

image1, image2, and arg1 are opencv matrix while arg2 is my own defined class.

When I run the program without parallelization

mat1 = myfunction(image1, arg1, arg2) * shrink;
mat2 = myfunction(image2, arg1, arg2) * shrink;

There are no problem and the program run just fine. I use parallel_invoke because I expect those two can be run simultaneously. Is it not working because both arg1 and arg2 are used simultaneously?

I don't know what to do because Segmentation fault (core dumped) was all that is shown in the terminal.

Edit: This is what Myfunction() looks like:

Mat edgebox_main(Mat E0, Mat O0, _para o) {
    // check and get inputs
    arrayf E;
    arrayf O;
    int h = E0.rows;
    O._h = E._h = h;
    int w = E0.cols;
    O._w = E._w = w;
    E._x = new float[h * w];
    O._x = new float[h * w];
    getadd(E0, E._x);
    getadd(O0, O._x);

    //optionally create memory for visualization
    arrayf V;

    // setup and run EdgeBoxGenerator
    EdgeBoxGenerator edgeBoxGen;
    Boxes boxes;
    edgeBoxGen._alpha = o.alpha;
    edgeBoxGen._beta = o.beta;
    edgeBoxGen._eta = o.eta;
    edgeBoxGen._minScore = o.minScore;
    edgeBoxGen._maxBoxes = o.maxBoxes;
    edgeBoxGen._edgeMinMag = o.edgeMinMag;
    edgeBoxGen._edgeMergeThr = o.edgeMinMag;
    edgeBoxGen._clusterMinMag = o.clusterMinMag;
    edgeBoxGen._maxAspectRatio = o.maxAspectRatio;
    edgeBoxGen._minBoxArea = o.minBoxArea;
    edgeBoxGen._maxBoxLength = std::min(std::min((int)o.maxBoxLength,h),w);
    edgeBoxGen._gamma = o.gamma;
    edgeBoxGen._kappa = o.kappa;
    edgeBoxGen.generate(boxes, E, O, V);
    boxesNms(boxes,edgeBoxGen._beta,edgeBoxGen._eta,edgeBoxGen._maxBoxes);

    // create output bbs and output to Matlab
    int n = (int) boxes.size();
    float *bbs = new float[n * 5];
    for (int i = 0; i < n; i++) {
        bbs[i + 0 * n] = (float) boxes[i].c + 1;
        bbs[i + 1 * n] = (float) boxes[i].r + 1;
        bbs[i + 2 * n] = (float) boxes[i].w;
        bbs[i + 3 * n] = (float) boxes[i].h;
        bbs[i + 4 * n] = boxes[i].s;
    }

    Mat bbs_mat(n, 5, CV_32FC1);
    fillmat(bbs, bbs_mat);
    return bbs_mat;
}

The code calls several other class and function which I think would be too long if I write all of them here. Also I am not very experienced with debugging in Linux. I usually use Microsoft Visual Studio for debugging C++, so I don't really know how to generate additional debugging information in the terminal, do you have any suggestion about what debugger to use or is g++ alone is enough?


Solution

  • After I use stacktrace and check the core dump as written in this post. I got additional error messages which says SIGSEGV error. That error basically tells that I tried to access an element that wasn't available in the array.