0

I have read this post on how to fuse a loop. The goal is to fuse my double for loop in order to parallelize it with OpenMP. The reason why I don't use collapse(2) is because the inner loop has dependencies on the outer one. I have also read this relevant post.

My problem though, is that when I fuse my loop I get a Segmentation Fault error and that sounds pretty fuzzy. I am pretty sure I am making the right conversion. Unfortunately there is no way I can provide a reproducible - minimal example as my program has a ton of functions where they call one another. Here is my initial loop though:

for(int i=0; i<size; i++)
{  
    int counter = 0;
    for(int j=0; j<size; j++)
    {
        if (i==j)
            continue;
        if(arr[size * i + j])
        {
          
            graph->nodes[i]->degree++;
            graph->nodes[i]->neighbours[counter] = (Node*)malloc(sizeof(Node));
            graph->nodes[i]->neighbours[counter] = graph->nodes[j];
            counter++;
        }
    }
}

where graph is a pointer to Struct and graph->nodes is an array of pointers to the graph's nodes. Same goes for graph->nodes[i]->neighbours. An array of pointers (pointed to by a pointer pointed to by another pointer - sorry).

As you can see the fact that I am using the counter variable
that restricts me from using #pragma omp parallel for collapse(2). Below you can see my converted loop:

for(int n=0; n<size*size; n++)
{  
    int i = n / size;
    int j = n % size;
    int counter = 0;
    for(int j=0; j<size; j++)
    {
        if (i==j)
            continue;
        if(arr[size * i + j])
        {
          
            graph->nodes[i]->degree++;
            graph->nodes[i]->neighbours[counter] = (Node*)malloc(sizeof(Node));
            graph->nodes[i]->neighbours[counter] = graph->nodes[j];
            counter++;
        }
    }
}

I have tried debugging with valgrind and what's ultra weird is that the Segmentation Fault does not appear to be on these specific lines, although it happens only when I make the loop conversion.

Mini disclaimer: As you may guess, because of these pointer to pointer to pointer variables I use lots of mallocs.

I don't expect you to get the same error with the code that I have posted that is why my question is more of a general one: How could theoretically a loop fusion cause a segfault error?

user3666197
  • 1
  • 6
  • 50
  • 92
gonidelis
  • 885
  • 10
  • 32

1 Answers1

0

I think in your converted loop you got i and j mixed up.

It should be int i = n % size;, not j.

n / size always equals 0.

matt
  • 90
  • 1
  • 8
  • Sorry that's not the case. It was just a typo. I fixed it. It's `size*size` so `n/size` doesn't always give `0`.Although thanks for noticing. – gonidelis Sep 27 '20 at 06:42