File: back2back_distribute.c

package info (click to toggle)
llvm-toolchain-19 1%3A19.1.7-19
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,999,616 kB
  • sloc: cpp: 6,951,724; ansic: 1,486,157; asm: 913,598; python: 232,059; f90: 80,126; objc: 75,281; lisp: 37,276; pascal: 16,990; sh: 10,079; ml: 5,058; perl: 4,724; awk: 3,523; makefile: 3,430; javascript: 2,504; xml: 892; fortran: 664; cs: 573
file content (75 lines) | stat: -rw-r--r-- 2,132 bytes parent folder | download | duplicates (10)
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
// RUN: %libomptarget-compile-generic -O3 && %libomptarget-run-generic | %fcheck-generic

#include <omp.h>
#include <stdio.h>
#include <stdlib.h>

#define MAX_N 25000

void reset_input(double *a, double *a_h, double *b, double *c) {
  for(int i = 0 ; i < MAX_N ; i++) {
    a[i] = a_h[i] = i;
    b[i] = i*2;
    c[i] = i-3;
  }
}

int main(int argc, char *argv[]) {
  double *a = (double *)calloc(MAX_N, sizeof(double));
  double *a_h = (double *)calloc(MAX_N, sizeof(double));
  double *d = (double *)calloc(MAX_N, sizeof(double));
  double *d_h = (double *)calloc(MAX_N, sizeof(double));
  double *b = (double *)calloc(MAX_N, sizeof(double));
  double *c = (double *)calloc(MAX_N, sizeof(double));

#pragma omp target enter data map(to:a[:MAX_N],b[:MAX_N],c[:MAX_N],d[:MAX_N])

  for (int n = 32 ; n < MAX_N ; n+=5000) {
    reset_input(a, a_h, b, c);

#pragma omp target update to(a[:n],b[:n],c[:n],d[:n])
    int t = 0;
    for (int tms = 1 ; tms <= 256 ; tms *= 2) { // 8 times
      for (int ths = 32 ; ths <= 1024 ; ths *= 2) { // 6 times
        t++;
#pragma omp target
#pragma omp teams num_teams(tms) thread_limit(ths)
        {
#pragma omp distribute parallel for
          for (int i = 0; i < n; ++i) {
            a[i] += b[i] + c[i];
          }
#pragma omp distribute parallel for
          for (int i = 0; i < n; ++i) {
           d[i] -= b[i] + c[i];
          }
        }
      } // loop over 'ths'
    } // loop over 'tms'

    // check results for each 'n'
    for (int times = 0 ; times < t ; times++) {
      for (int i = 0; i < n; ++i) {
        a_h[i] += b[i] + c[i];
      }
      for (int i = 0; i < n; ++i)
        d_h[i] -= b[i] + c[i];
    }
#pragma omp target update from(a[:n],d[:n])

    for (int i = 0; i < n; ++i) {
      if (a_h[i] != a[i]) {
        printf("A Error at n = %d, i = %d: host = %f, device = %f\n", n, i, a_h[i], a[i]);
        return 1;
      }
      if (d_h[i] != d[i]) {
        printf("D Error at n = %d, i = %d: host = %lf, device = %lf\n", n, i, d_h[i], d[i]);
        return 1;
      }
    }
  } // loop over 'n'

  // CHECK: Succeeded
  printf("Succeeded\n");
  return 0;
}