#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;
std::array<int, N> prime;
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];
printf("%d", prime[j]);
}
}
tChecked = 0;
tempArr.clear();
P3: if (j == (N - 1)) goto P9;
//P4: n = n + 2;
PX:
#pragma omp parallel private(n, quo, rem) num_threads(4)
{
int ID = omp_get_thread_num();
if (tChecked == 1)
{
n = storeArr[ID];
goto P6;
}
else
{
n = prime[j];
//printf("%d", n);
}
P4:
n = n + 2 * (ID + 1);
storeArr[ID] = n;
printf("Thread num: %d , checking number: %d \n", ID, n);
P5: if(tChecked == 0) 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 debug and exit I see this:
The program '[13320] HW1.exe' has exited with code -1073741510 (0xc000013a)
I assume this is related to jumping in threads because original code works very fine. And also, it always uses thread 2 and never goes into other threads. Why is that?
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.
It indicates that the values prime[2] to prime[49] (and the other such variables) have never had a value set by you. The compiler set them to this value at the start, and it was never changed.
The segFault: P6: quo = n / prime[k];
At this point, k has a value of some huge number, so you're trying to read prime[huge_number]. That is an attempt to read memory way off the end of the array prime, and that causes a segFault.
Edit: I see you have edited your original post, changing your questions and your code. Please don't do that. It wrecks the whole point of the question-answer thread.
Anyway, your threads are sharing some variables, and all reading and writing them at different times with different values. This is really bad. n, quo, rem are private. The others are not.
Sorry about the edit. I tried to edit as fast as I can once I post the Q.
However, I realized that I am doing this completely wrong. I the question requires different scheduling types, dynamic, static and guided. But the confusing part is, how will I do that without changing the method! As far as I know, I should use for loops for scheduled loops but there is no for loop in the original code: