Tencent / Tencent/ncnn

OpenMP bugs segmenation fault with OepnMP enable in libncnn.a

Open
#4,166 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C++
Stars
23.8k
Forks
4.5k
Avg merge
2d 20h
Merged PRs (30d)
37

Description

Fragment from your source code "ncnn/src/layer/riscv/convolution_7x7_pack1ton.h" (loop inside loop and inside parallel)
There is a lot of similar code in your sources
In this variant only variable "p" will be private but variable "q" will be shared and all threads will be use the same variable, and that will be produce the data race and segmentation fault errors.

#pragma omp parallel for num_threads(opt.num_threads)
    for (int p = 0; p < outch; p++)
    {
        Mat out0 = top_blob.channel(p);

        vfloat32m1_t _bias0 = bias ? vle32_v_f32m1(bias + p * packn, vl) : vfmv_v_f_f32m1(0.f, vl);
        out0.fill(_bias0);

        for (int q = 0; q < inch; q++)
        {
            float* outptr0 = out0;

            const Mat img0 = bottom_blob.channel(q);

Example from OpenMP lectures
During parallel execution of the for loop, index “i” is a private variable, while “b”, “cptr” and heap data are shared.

int main()
{
 int b[3];
 char *cptr;
 int i;
 cptr = malloc(1);
 #pragma omp parallel for
 for(i=0; i<3; i++)
 b[i]=i;
}

Another one example from OpenMP lectures. Where Loop inside another loop.

#pragma omp parallel for private (j)
 for(i = 0; i < M; i++)
      for(j=0; j < N; j++)
          a[i][j] = min(a[i][j], a[i][k]+tmp[j]);

We need every thread to work through N values of “j” for each iteration of the “i” loop.
• If we do not make “j” private, all of threads try to initialize and increment the same shared variable “j” – meaning the data race.
• The private copies of variable “j” will be accessible only inside the for loop. The values are undefined on loop entry and exit.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with ncnn/src/layer/riscv/convolution_7x7_pack1ton.h and inspect the nested loops under the OpenMP parallel-for directive, then compare similar loops mentioned in the report. Reproduce the segmentation fault with OpenMP enabled and verify that the affected convolution paths no longer exhibit the reported race or crash.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
machine-learning
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
28/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.