Working with labels in OpenMP: How to or is it possible to jump in Threads?

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#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.
Last edited on

http://www.binaryconvert.com/result_signed_int.html?hexadecimal=CCCCCCCC

This is 0xcccccccc.

0xcccccccc is a marker value used by the microsoft compiler to identify uninitialised stack memory ( https://en.wikipedia.org/wiki/Magic_number_(programming) )

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.
Last edited on
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

#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]) ;
}
Topic archived. No new replies allowed.