PandA-2024.02
covariance.c
Go to the documentation of this file.
1 
10 /* covariance.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 "covariance.h"
22 
23 
24 /* Array initialization. */
25 static
26 void init_array (int m, int n,
27  DATA_TYPE *float_n,
28  DATA_TYPE POLYBENCH_2D(data,N,M,n,m))
29 {
30  int i, j;
31 
32  *float_n = (DATA_TYPE)n;
33 
34  for (i = 0; i < N; i++)
35  for (j = 0; j < M; j++)
36  data[i][j] = ((DATA_TYPE) i*j) / M;
37 }
38 
39 
40 /* DCE code. Must scan the entire live-out data.
41  Can be used also to check the correctness of the output. */
42 static
43 void print_array(int m,
44  DATA_TYPE POLYBENCH_2D(cov,M,M,m,m))
45 
46 {
47  int i, j;
48 
50  POLYBENCH_DUMP_BEGIN("cov");
51  for (i = 0; i < m; i++)
52  for (j = 0; j < m; j++) {
53  if ((i * m + j) % 20 == 0) fprintf (POLYBENCH_DUMP_TARGET, "\n");
54  fprintf (POLYBENCH_DUMP_TARGET, DATA_PRINTF_MODIFIER, cov[i][j]);
55  }
56  POLYBENCH_DUMP_END("cov");
58 }
59 
60 
61 /* Main computational kernel. The whole function will be timed,
62  including the call and return. */
63 __attribute__((noinline))
64 void kernel_covariance(int m, int n,
65  DATA_TYPE float_n,
66  DATA_TYPE POLYBENCH_2D(data,N,M,n,m),
67  DATA_TYPE POLYBENCH_2D(cov,M,M,m,m),
68  DATA_TYPE POLYBENCH_1D(mean,M,m))
69 {
70  int i, j, k;
71 
72 #pragma scop
73  for (j = 0; j < _PB_M; j++)
74  {
75  mean[j] = SCALAR_VAL(0.0);
76  for (i = 0; i < _PB_N; i++)
77  mean[j] += data[i][j];
78  mean[j] /= float_n;
79  }
80 
81  for (i = 0; i < _PB_N; i++)
82  for (j = 0; j < _PB_M; j++)
83  data[i][j] -= mean[j];
84 
85  for (i = 0; i < _PB_M; i++)
86  for (j = i; j < _PB_M; j++)
87  {
88  cov[i][j] = SCALAR_VAL(0.0);
89  for (k = 0; k < _PB_N; k++)
90  cov[i][j] += data[k][i] * data[k][j];
91  cov[i][j] /= (float_n - SCALAR_VAL(1.0));
92  cov[j][i] = cov[i][j];
93  }
94 #pragma endscop
95 
96 }
97 
98 
99 int main(int argc, char** argv)
100 {
101  /* Retrieve problem size. */
102  int n = N;
103  int m = M;
104 
105  /* Variable declaration/allocation. */
106  DATA_TYPE float_n;
110 
111 
112  /* Initialize array(s). */
113  init_array (m, n, &float_n, POLYBENCH_ARRAY(data));
114 
115  /* Start timer. */
117 
118  /* Run kernel. */
119  kernel_covariance (m, n, float_n,
120  POLYBENCH_ARRAY(data),
121  POLYBENCH_ARRAY(cov),
122  POLYBENCH_ARRAY(mean));
123 
124  /* Stop and print timer. */
127 
128  /* Prevent dead-code elimination. All live-out data must be printed
129  by the function call in argument. */
131 
132  /* Be clean. */
133  POLYBENCH_FREE_ARRAY(data);
135  POLYBENCH_FREE_ARRAY(mean);
136 
137  return 0;
138 }
#define POLYBENCH_ARRAY(x)
Definition: polybench.h:84
#define POLYBENCH_DUMP_BEGIN(s)
Definition: polybench.h:167
#define POLYBENCH_FREE_ARRAY(x)
Definition: polybench.h:88
#define POLYBENCH_2D(var, dim1, dim2, ddim1, ddim2)
Definition: polybench.h:98
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: covariance.c:26
__attribute__((noinline))
Convert the given fixedpt number to a decimal string.
Definition: covariance.c:63
static const uint32_t k[]
Definition: sha-256.c:22
int main(int argc, char **argv)
Definition: covariance.c:99
#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
static void print_array(int m, DATA_TYPE POLYBENCH_2D(cov, M, M, m, m))
Definition: covariance.c:43
#define M
Definition: gsm.c:30
#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 _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