-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconjgrad.c
155 lines (131 loc) · 3.66 KB
/
conjgrad.c
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/**
* This benchmark created on September 1, 2022.
*
* The (partial) conjugate gradient routine iteration loop
* from NAS CG benchmark.
* CG = Conjugate Gradient, irregular memory access and communication.
*
* Web address: https://www.nas.nasa.gov/software/npb.html
*/
/* conjgrad.c */
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <math.h>
/* Include polybench common header. */
#include <polybench.h>
/* Include benchmark-specific header. */
/* Default data type is double, default size is N=1024. */
#include <conjgrad.h>
/* Array initialization. */
static
void init_array(int na,
DATA_TYPE POLYBENCH_1D(p,NA,na),
DATA_TYPE POLYBENCH_1D(q,NA,na),
DATA_TYPE POLYBENCH_1D(z,NA,na),
DATA_TYPE POLYBENCH_1D(r,NA,na))
{
int j;
for (j = 0; j < na; j++) {
q[j] = 0.00001 *j;
z[j] = 0.00002 *j;
r[j] = 0.00003 *j;
p[j] = r[j];
}
}
/* DCE code. Must scan the entire live-out data.
Can be used also to check the correctness of the output. */
static
void print_array(int na,
DATA_TYPE POLYBENCH_1D(z,NA,na),
DATA_TYPE POLYBENCH_1D(r,NA,na))
{
int i;
POLYBENCH_DUMP_START;
POLYBENCH_DUMP_BEGIN("z");
for (i = 0; i < na; i++) {
fprintf (stderr, DATA_PRINTF_MODIFIER, z[i]);
if (i % 20 == 0) fprintf (stderr, "\n");
}
POLYBENCH_DUMP_END("z");
POLYBENCH_DUMP_BEGIN("r");
for (i = 0; i < na; i++) {
fprintf (stderr, DATA_PRINTF_MODIFIER, r[i]);
if (i % 20 == 0) fprintf (stderr, "\n");
}
POLYBENCH_DUMP_END("r");
POLYBENCH_DUMP_FINISH;
}
/* Main computational kernel. The whole function will be timed,
including the call and return. */
static
void kernel_conjgrad(int na, int niter,
DATA_TYPE POLYBENCH_1D(p,NA,na),
DATA_TYPE POLYBENCH_1D(q,NA,na),
DATA_TYPE POLYBENCH_1D(z,NA,na),
DATA_TYPE POLYBENCH_1D(r,NA,na))
{
int i;
int j;
double rho;
double d;
double alpha;
rho = 0;
d = 0;
for (i = 1; i <= niter; i += 1) {
#pragma omp parallel for private (j) reduction (+:rho)
for (j = 0; j <= -1 + na; j += 1) {
rho = rho + r[j] * r[j];
}
#pragma omp parallel for private (j) reduction (+:d)
for (j = 0; j <= -1 + na; j += 1) {
d = d + p[j] * q[j];
}
alpha = rho / d;
#pragma omp parallel for private (j)
for (j = 0; j <= -1 + na; j += 1) {
z[j] = z[j] + alpha * p[j];
}
#pragma omp parallel for private (j) firstprivate (alpha)
for (j = 0; j <= -1 + na; j += 1) {
r[j] = r[j] - alpha * q[j];
}
}
}
int main(int argc, char** argv)
{
/* Retrieve problem size. */
int na = NA;
int niter = NITER;
/* Variable declaration/allocation. */
POLYBENCH_1D_ARRAY_DECL(p,DATA_TYPE,NA,na);
POLYBENCH_1D_ARRAY_DECL(q,DATA_TYPE,NA,na);
POLYBENCH_1D_ARRAY_DECL(z,DATA_TYPE,NA,na);
POLYBENCH_1D_ARRAY_DECL(r,DATA_TYPE,NA,na);
/* Initialize array(s). */
init_array (na,
POLYBENCH_ARRAY(p),
POLYBENCH_ARRAY(q),
POLYBENCH_ARRAY(z),
POLYBENCH_ARRAY(r));
/* Start timer. */
polybench_start_instruments;
/* Run kernel. */
kernel_conjgrad (na, niter,
POLYBENCH_ARRAY(p),
POLYBENCH_ARRAY(q),
POLYBENCH_ARRAY(z),
POLYBENCH_ARRAY(r));
/* Stop and print timer. */
polybench_stop_instruments;
polybench_print_instruments;
/* Prevent dead-code elimination. All live-out data must be printed
by the function call in argument. */
polybench_prevent_dce(print_array(na, POLYBENCH_ARRAY(z), POLYBENCH_ARRAY(r)));
/* Be clean. */
POLYBENCH_FREE_ARRAY(p);
POLYBENCH_FREE_ARRAY(q);
POLYBENCH_FREE_ARRAY(z);
POLYBENCH_FREE_ARRAY(r);
return 0;
}