brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.2 KiB · af0fa66 Raw
85 lines · c
1// RUN: %libomp-compile -O3 -ffast-math2// RUN: %libomp-run3#include <float.h>4#include <math.h>5#include <stdio.h>6#include <stdlib.h>7#include <time.h>8 9int compare_float(float x1, float x2, float scalar) {10  const float diff = fabsf(x1 - x2);11  x1 = fabsf(x1);12  x2 = fabsf(x2);13  const float l = (x2 > x1) ? x2 : x1;14  if (diff <= l * scalar * FLT_EPSILON)15    return 1;16  else17    return 0;18}19 20#define ARRAY_SIZE 25621 22__attribute__((noinline)) void23initialization_loop(float X[ARRAY_SIZE][ARRAY_SIZE],24                    float Y[ARRAY_SIZE][ARRAY_SIZE]) {25  const float max = 1000.0;26  srand(time(NULL));27  for (int r = 0; r < ARRAY_SIZE; r++) {28    for (int c = 0; c < ARRAY_SIZE; c++) {29      X[r][c] = ((float)rand() / (float)(RAND_MAX)) * max;30      Y[r][c] = X[r][c];31    }32  }33}34 35__attribute__((noinline)) void omp_simd_loop(float X[ARRAY_SIZE][ARRAY_SIZE]) {36  for (int r = 1; r < ARRAY_SIZE; ++r) {37    for (int c = 1; c < ARRAY_SIZE; ++c) {38#pragma omp simd39      for (int k = 2; k < ARRAY_SIZE; ++k) {40#pragma omp ordered simd41        X[r][k] = X[r][k - 2] + sinf((float)(r / c));42      }43    }44  }45}46 47__attribute__((noinline)) int comparison_loop(float X[ARRAY_SIZE][ARRAY_SIZE],48                                              float Y[ARRAY_SIZE][ARRAY_SIZE]) {49  int totalErrors_simd = 0;50  const float scalar = 1.0;51  for (int r = 1; r < ARRAY_SIZE; ++r) {52    for (int c = 1; c < ARRAY_SIZE; ++c) {53      for (int k = 2; k < ARRAY_SIZE; ++k) {54        Y[r][k] = Y[r][k - 2] + sinf((float)(r / c));55      }56    }57    // check row for simd update58    for (int k = 0; k < ARRAY_SIZE; ++k) {59      if (!compare_float(X[r][k], Y[r][k], scalar)) {60        ++totalErrors_simd;61      }62    }63  }64  return totalErrors_simd;65}66 67int main(void) {68  float X[ARRAY_SIZE][ARRAY_SIZE];69  float Y[ARRAY_SIZE][ARRAY_SIZE];70 71  initialization_loop(X, Y);72  omp_simd_loop(X);73  const int totalErrors_simd = comparison_loop(X, Y);74 75  if (totalErrors_simd) {76    fprintf(stdout, "totalErrors_simd: %d \n", totalErrors_simd);77    fprintf(stdout, "%s : %d - FAIL: error in ordered simd computation.\n",78            __FILE__, __LINE__);79  } else {80    fprintf(stdout, "Success!\n");81  }82 83  return totalErrors_simd;84}85