File: simd_conservative_ordered.c

package info (click to toggle)
llvm-toolchain-21 1%3A21.1.2-4
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 2,236,292 kB
  • sloc: cpp: 7,619,115; ansic: 1,433,921; asm: 1,058,734; python: 252,125; f90: 94,671; objc: 70,753; lisp: 42,813; pascal: 18,401; sh: 10,093; ml: 5,111; perl: 4,720; awk: 3,523; makefile: 3,397; javascript: 2,272; xml: 892; fortran: 770
file content (84 lines) | stat: -rw-r--r-- 2,210 bytes parent folder | download | duplicates (3)
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
// RUN: %libomp-compile -O3 -ffast-math
// RUN: %libomp-run
#include <float.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int compare_float(float x1, float x2, float scalar) {
  const float diff = fabsf(x1 - x2);
  x1 = fabsf(x1);
  x2 = fabsf(x2);
  const float l = (x2 > x1) ? x2 : x1;
  if (diff <= l * scalar * FLT_EPSILON)
    return 1;
  else
    return 0;
}

#define ARRAY_SIZE 256

__attribute__((noinline)) void
initialization_loop(float X[ARRAY_SIZE][ARRAY_SIZE],
                    float Y[ARRAY_SIZE][ARRAY_SIZE]) {
  const float max = 1000.0;
  srand(time(NULL));
  for (int r = 0; r < ARRAY_SIZE; r++) {
    for (int c = 0; c < ARRAY_SIZE; c++) {
      X[r][c] = ((float)rand() / (float)(RAND_MAX)) * max;
      Y[r][c] = X[r][c];
    }
  }
}

__attribute__((noinline)) void omp_simd_loop(float X[ARRAY_SIZE][ARRAY_SIZE]) {
  for (int r = 1; r < ARRAY_SIZE; ++r) {
    for (int c = 1; c < ARRAY_SIZE; ++c) {
#pragma omp simd
      for (int k = 2; k < ARRAY_SIZE; ++k) {
#pragma omp ordered simd
        X[r][k] = X[r][k - 2] + sinf((float)(r / c));
      }
    }
  }
}

__attribute__((noinline)) int comparison_loop(float X[ARRAY_SIZE][ARRAY_SIZE],
                                              float Y[ARRAY_SIZE][ARRAY_SIZE]) {
  int totalErrors_simd = 0;
  const float scalar = 1.0;
  for (int r = 1; r < ARRAY_SIZE; ++r) {
    for (int c = 1; c < ARRAY_SIZE; ++c) {
      for (int k = 2; k < ARRAY_SIZE; ++k) {
        Y[r][k] = Y[r][k - 2] + sinf((float)(r / c));
      }
    }
    // check row for simd update
    for (int k = 0; k < ARRAY_SIZE; ++k) {
      if (!compare_float(X[r][k], Y[r][k], scalar)) {
        ++totalErrors_simd;
      }
    }
  }
  return totalErrors_simd;
}

int main(void) {
  float X[ARRAY_SIZE][ARRAY_SIZE];
  float Y[ARRAY_SIZE][ARRAY_SIZE];

  initialization_loop(X, Y);
  omp_simd_loop(X);
  const int totalErrors_simd = comparison_loop(X, Y);

  if (totalErrors_simd) {
    fprintf(stdout, "totalErrors_simd: %d \n", totalErrors_simd);
    fprintf(stdout, "%s : %d - FAIL: error in ordered simd computation.\n",
            __FILE__, __LINE__);
  } else {
    fprintf(stdout, "Success!\n");
  }

  return totalErrors_simd;
}