Working with labels in OpenMP: How to or is it pos

2019-07-22 14:09发布

I am trying to get some code paralleled, and algorithm must be remained. Here is the original Code:

#include <stdio.h>
#define N 50
main()
{
int prime[N] ;
int j ;
int k ;
int n ;
int quo,rem ;
P1: prime[0] = 2 ;
n = 3 ;
j = 0 ;
P2: j = j+1 ;
prime[j] = n ;
P3: if (j == (N-1)) goto P9 ;
P4: n = n + 2 ;
P5: k = 1 ;
P6: quo = n / prime[k] ;
rem = n % prime[k] ;
if (rem == 0) goto P4 ;
P7: if (quo <= prime[k]) goto P2 ;
P8: k = k+1 ;
goto P6 ;
P9: for(j=0 ; j < N ; j++) printf("%d ",prime[j]) ;
}

And I changed it into this:

#include <stdio.h>
#include <omp.h>
#include <array>
#include <vector>

#define N 50

int tChecked = 0;

std::vector<int> tempArr;
std::array<int, 4> storeArr;

int main()
{

        int prime[N];
        int j;
        int k;
        int n;
        int quo, rem;
        int test = 0;

    P1: prime[0] = 2;
        n = 3;
        j = 0;
    P2: 
        if (tempArr.empty()) 
        {
            j = j + 1;
            prime[j] = n;
        }
        else
        {
            std::sort(std::begin(tempArr), std::end(tempArr));
            for (int i = 0; i < tempArr.size(); i++)
            {
                j = j + 1;
                prime[j] = tempArr[i];
            }
        }
        tChecked = 0;
        tempArr.clear();
    P3: if (j == (N - 1)) goto P9;
    //P4: n = n + 2;
    PX:     
    #pragma omp parallel num_threads(4)
    {
        int ID = omp_get_thread_num();
        if (tChecked = 1) 
        {
            n = storeArr[ID];
            goto P6;
        }

    P4:     
        n = n + 2 * (ID + 1);
        storeArr[ID] = n;

    P5: k = 1;

    P6: quo = n / prime[k];
        rem = n % prime[k];
        if (rem == 0) goto P4;

    P7:
        if (quo <= prime[k])
        {
            tempArr.push_back(n);
        }
    }
        if (!tempArr.empty()) goto P2;

    P8: k = k + 1;
        tChecked = 1;
        goto PX;

    P9: for (j = 0; j < N; j++) printf("%d ", prime[j]);

        getchar();

        return 0;

}

I am using Visual Studio 2015 and OpenMP suport is on, when I run the code it crashes like this:

Exception thrown at 0x00ED9D29 in HW1.exe: 0xC0000005: Access violation reading location 0x33612FA8.

When I look at the local variables, I see absurd values like:

prime[2] to prime[49] = -858993460 rem, quo, k = -858993460

What is this -858993460 anyway? I assume this is related to jumping in threads because original code works very fine.

So can you explain where this '-858993460' comes from and why? is it related to jumping or any other thing? And is it possible to jump in parallel threads?

Note: I am sharing the question also maybe it helps:

Implement an OpenMP program that generates prime numbers in a given interval. You should use the prime generation method given in the next page ( Do NOT use other method !). Your program should generate a csv le called results.csv that reports the timing results in the following format.

table

0条回答
登录 后发表回答