OpenMP: ideal way to use a shared vector

I need to write a prime number generator function with multi-thread. Here is my code below:

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

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

#define N 50

std::vector<int> pNum;

void function(void);

int main()
{

	pNum.push_back(2);
	pNum.push_back(3);

	function();

	for (int i = 0; i < pNum.size(); i++)
	{
		printf("%d \n", pNum[i]);
	}
	printf("top say: %d", pNum.size());
	getchar();
}


void function(void)
{
	//

#pragma omp parallel
{
	int i;
	int k, n, rem, quo, id;
	omp_set_num_threads(4);

#pragma omp parallel for schedule(dynamic, 1) private(i, k, n, quo, rem, id) shared(pNum) 

	for (int i = 0; i < (pNum.back() * pNum.back() + 1) / 2; i++ )
	{
		//
		int k = 1;

		int n = pNum[k] + i * 2;
		int quo = n / pNum[k];
		int rem = n % pNum[k];
		
		

		while (quo > pNum[k] && rem != 0)
		{
			k++;
			if (k >= pNum.size()) break;
			
			quo = n / pNum[k];
			rem = n % pNum[k];

		}

		if (rem != 0 && k < pNum.size())
		{

			
				pNum.push_back(n); std::sort(pNum.begin(), pNum.end());			
			
		}

		id = omp_get_thread_num();

		printf("id: %d; number: %d \n", id, n);

		if (pNum.size() >= N) break;

	}
	
}

}



The problem is output prompt closes without getchar. I can not see anything bu I can see all the threads are 0ID. So there is no parallel structure. I am new to openMP so can you guide me where am I doing wrong? Thanks.
Last edited on
Topic archived. No new replies allowed.