File: openmp-2.cpp

package info (click to toggle)
dmtcp 2.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 6,496 kB
  • sloc: cpp: 33,592; ansic: 28,099; sh: 6,735; makefile: 1,950; perl: 1,690; python: 1,241; asm: 138; java: 13
file content (118 lines) | stat: -rw-r--r-- 2,396 bytes parent folder | download | duplicates (4)
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// This test was contributed by Nick Hall.

#include <iostream>
using namespace std;

#include <omp.h>

void MetaTestOptimize();
void ScoreMetaCallback();
void ScoreCallback(unsigned int m);


const unsigned int size2 = 10000;
const unsigned int size3 = 10000;
const unsigned int size4 = 1000;


int **memarray;

double scratch[4] = {0, 0, 0, 0};


int main(int argc, char *argv[])
{
	omp_set_num_threads(4);
	cout << "Number of OpenMP threads: " << omp_get_max_threads() << endl;

	MetaTestOptimize();

	cout << "scratch[0]: " << scratch[0] << endl;
	cout << "scratch[1]: " << scratch[1] << endl;
	cout << "scratch[2]: " << scratch[2] << endl;
	cout << "scratch[3]: " << scratch[3] << endl;


  return 0;
}



void MetaTestOptimize()
{
	omp_set_num_threads(1);

// gcc 4.2 and 4.3 implement OpenMP 2.5. gcc 4.4 and later implement OpenMP 3.0
// Some distros (Red Hat?, Open Suse?) backported OpenMP to gcc 4.1
// OpenMP 2.5 requires 'signed int'
#if __GNUC__ == 4 && __GNUC_MINOR__ < 4
	int j = 0;
#else
	unsigned int j = 0;
#endif
#pragma omp parallel for \
   private(j) \
   schedule(static)
	for (j = 0; j < size2; j++) {
		cout << "j: " << j << endl;
		ScoreMetaCallback();
	}

}


void ScoreMetaCallback()
{
	omp_set_num_threads(4);

	// DMTCP hangs with this uncommented:
	#pragma omp critical(ScoreMetaCallback)
	{
		memarray = new int*[size3];
// gcc 4.2 and 4.3 implement OpenMP 2.5. gcc 4.4 and later implement OpenMP 3.0
// Some distros (Red Hat?, Open Suse?) backported OpenMP to gcc 4.1
// OpenMP 2.5 requires 'signed int'
#if __GNUC__ == 4 && __GNUC_MINOR__ < 4
		int m = 0;
#else
		unsigned int m = 0;
#endif

		#pragma omp parallel for \
		   private(m) \
		   schedule(static)
			for (m = 0; m < size3; m++) {
				//cout << "add m: " << m << endl;
				memarray[m] = new int[size4];
			}

		#pragma omp parallel for \
		   private(m) \
		   schedule(static)
			for (m = 0; m < size3; m++) {
                          if (m%1000 == 0) {
				cout << "m: " << m << endl;
                          }
				ScoreCallback(m);
			}


		#pragma omp parallel for \
		   private(m) \
		   schedule(static)
			for (m = 0; m < size3; m++) {
				//cout << "del m: " << m << endl;
				delete[] memarray[m];
			}

			delete[] memarray;
	}
}


void ScoreCallback(unsigned int m)
{
	int threadNum = omp_get_thread_num();
	scratch[threadNum] += m * (threadNum - 2);   // meaningless calculation
}