PandA-2024.02
correlation.c
Go to the documentation of this file.
1 
10 /* correlation.c: this file is part of PolyBench/C */
11 
12 #include <stdio.h>
13 #include <unistd.h>
14 #include <string.h>
15 #include <math.h>
16 
17 /* Include polybench common header. */
18 #include <polybench.h>
19 
20 /* Include benchmark-specific header. */
21 #include "correlation.h"
22 
23 
24 /* Array initialization. */
25 static
26 void init_array (int m,
27  int n,
28  DATA_TYPE *float_n,
29  DATA_TYPE POLYBENCH_2D(data,N,M,n,m))
30 {
31  int i, j;
32 
33  *float_n = (DATA_TYPE)N;
34 
35  for (i = 0; i < N; i++)
36  for (j = 0; j < M; j++)
37  data[i][j] = (DATA_TYPE)(i*j)/M + i;
38 
39 }
40 
41 
42 /* DCE code. Must scan the entire live-out data.
43  Can be used also to check the correctness of the output. */
44 static
45 void print_array(int m,
46  DATA_TYPE POLYBENCH_2D(corr,M,M,m,m))
47 
48 {
49  int i, j;
50 
52  POLYBENCH_DUMP_BEGIN("corr");
53  for (i = 0; i < m; i++)
54  for (j = 0; j < m; j++) {
55  if ((i * m + j) % 20 == 0) fprintf (POLYBENCH_DUMP_TARGET, "\n");
56  fprintf (POLYBENCH_DUMP_TARGET, DATA_PRINTF_MODIFIER, corr[i][j]);
57  }
58  POLYBENCH_DUMP_END("corr");
60 }
61 
62 
63 /* Main computational kernel. The whole function will be timed,
64  including the call and return. */
65 __attribute__((noinline))
66 void kernel_correlation(int m, int n,
67  DATA_TYPE float_n,
68  DATA_TYPE POLYBENCH_2D(data,N,M,n,m),
69  DATA_TYPE POLYBENCH_2D(corr,M,M,m,m),
70  DATA_TYPE POLYBENCH_1D(mean,M,m),
71  DATA_TYPE POLYBENCH_1D(stddev,M,m))
72 {
73  int i, j, k;
74 
75  DATA_TYPE eps = SCALAR_VAL(0.1);
76 
77 
78 #pragma scop
79  for (j = 0; j < _PB_M; j++)
80  {
81  mean[j] = SCALAR_VAL(0.0);
82  for (i = 0; i < _PB_N; i++)
83  mean[j] += data[i][j];
84  mean[j] /= float_n;
85  }
86 
87 
88  for (j = 0; j < _PB_M; j++)
89  {
90  stddev[j] = SCALAR_VAL(0.0);
91  for (i = 0; i < _PB_N; i++)
92  stddev[j] += (data[i][j] - mean[j]) * (data[i][j] - mean[j]);
93  stddev[j] /= float_n;
94  stddev[j] = SQRT_FUN(stddev[j]);
95  /* The following in an inelegant but usual way to handle
96  near-zero std. dev. values, which below would cause a zero-
97  divide. */
98  stddev[j] = stddev[j] <= eps ? SCALAR_VAL(1.0) : stddev[j];
99  }
100 
101  /* Center and reduce the column vectors. */
102  for (i = 0; i < _PB_N; i++)
103  for (j = 0; j < _PB_M; j++)
104  {
105  data[i][j] -= mean[j];
106  data[i][j] /= SQRT_FUN(float_n) * stddev[j];
107  }
108 
109  /* Calculate the m * m correlation matrix. */
110  for (i = 0; i < _PB_M-1; i++)
111  {
112  corr[i][i] = SCALAR_VAL(1.0);
113  for (j = i+1; j < _PB_M; j++)
114  {
115  corr[i][j] = SCALAR_VAL(0.0);
116  for (k = 0; k < _PB_N; k++)
117  corr[i][j] += (data[k][i] * data[k][j]);
118  corr[j][i] = corr[i][j];
119  }
120  }
121  corr[_PB_M-1][_PB_M-1] = SCALAR_VAL(1.0);
122 #pragma endscop
123 
124 }
125 
126 
127 int main(int argc, char** argv)
128 {
129  /* Retrieve problem size. */
130  int n = N;
131  int m = M;
132 
133  /* Variable declaration/allocation. */
134  DATA_TYPE float_n;
139 
140  /* Initialize array(s). */
141  init_array (m, n, &float_n, POLYBENCH_ARRAY(data));
142 
143  /* Start timer. */
145 
146  /* Run kernel. */
147  kernel_correlation (m, n, float_n,
148  POLYBENCH_ARRAY(data),
149  POLYBENCH_ARRAY(corr),
150  POLYBENCH_ARRAY(mean),
151  POLYBENCH_ARRAY(stddev));
152 
153  /* Stop and print timer. */
156 
157  /* Prevent dead-code elimination. All live-out data must be printed
158  by the function call in argument. */
160 
161  /* Be clean. */
162  POLYBENCH_FREE_ARRAY(data);
163  POLYBENCH_FREE_ARRAY(corr);
164  POLYBENCH_FREE_ARRAY(mean);
165  POLYBENCH_FREE_ARRAY(stddev);
166 
167  return 0;
168 }
#define POLYBENCH_ARRAY(x)
Definition: polybench.h:84
#define POLYBENCH_DUMP_BEGIN(s)
Definition: polybench.h:167
static void print_array(int m, DATA_TYPE POLYBENCH_2D(corr, M, M, m, m))
Definition: correlation.c:45
static void init_array(int m, int n, DATA_TYPE *float_n, DATA_TYPE POLYBENCH_2D(data, N, M, n, m))
This version is stamped on May 10, 2016.
Definition: correlation.c:26
#define POLYBENCH_FREE_ARRAY(x)
Definition: polybench.h:88
#define POLYBENCH_2D(var, dim1, dim2, ddim1, ddim2)
Definition: polybench.h:98
static const uint32_t k[]
Definition: sha-256.c:22
#define POLYBENCH_DUMP_START
Definition: polybench.h:165
#define _PB_M
Definition: correlation.h:48
#define N
Definition: dfdiv.c:60
#define POLYBENCH_2D_ARRAY_DECL(var, type, dim1, dim2, ddim1, ddim2)
Definition: polybench.h:131
#define DATA_PRINTF_MODIFIER
Definition: correlation.h:73
#define polybench_prevent_dce(func)
Definition: polybench.h:170
#define SCALAR_VAL(x)
Definition: correlation.h:74
#define POLYBENCH_DUMP_TARGET
Definition: polybench.h:164
#define POLYBENCH_DUMP_END(s)
Definition: polybench.h:168
int main(int argc, char **argv)
Definition: correlation.c:127
#define M
Definition: gsm.c:30
__attribute__((noinline))
Convert the given fixedpt number to a decimal string.
Definition: correlation.c:65
#define POLYBENCH_1D(var, dim1, ddim1)
Definition: polybench.h:97
#define POLYBENCH_DUMP_FINISH
Definition: polybench.h:166
#define POLYBENCH_1D_ARRAY_DECL(var, type, dim1, ddim1)
Definition: polybench.h:128
#define SQRT_FUN(x)
Definition: correlation.h:75
#define _PB_N
Definition: correlation.h:49
#define polybench_stop_instruments
Definition: polybench.h:177
#define polybench_print_instruments
Definition: polybench.h:178
#define polybench_start_instruments
Definition: polybench.h:176
#define DATA_TYPE
Definition: correlation.h:72

Generated on Mon Feb 12 2024 13:02:50 for PandA-2024.02 by doxygen 1.8.13