brintos

brintos / llvm-project-archived public Read only

0
0
Text · 1.3 KiB · 1042bd2 Raw
59 lines · c
1// RUN: %libomptarget-compile-generic2// RUN: %libomptarget-run-generic 2>&1 \3// RUN: | %fcheck-generic4 5#include <stdio.h>6#include <stdlib.h>7 8#define N 10249#define FROM 6410#define LENGTH 12811 12int main() {13  float *A = (float *)malloc(N * sizeof(float));14  float *B = (float *)malloc(N * sizeof(float));15  float *C = (float *)malloc(N * sizeof(float));16 17  for (int i = 0; i < N; i++) {18    C[i] = 0.0;19  }20 21  for (int i = 0; i < N; i++) {22    A[i] = i;23    B[i] = 2 * i;24  }25 26#pragma omp target enter data map(to : A[FROM : LENGTH], B[FROM : LENGTH])27#pragma omp target enter data map(alloc : C[FROM : LENGTH])28 29// A, B and C have been mapped starting at index FROM, but inside the kernel30// they are captured implicitly so the library must look them up using their31// base address.32#pragma omp target33  {34    for (int i = FROM; i < FROM + LENGTH; i++) {35      C[i] = A[i] + B[i];36    }37  }38 39#pragma omp target exit data map(from : C[FROM : LENGTH])40#pragma omp target exit data map(delete : A[FROM : LENGTH], B[FROM : LENGTH])41 42  int errors = 0;43  for (int i = FROM; i < FROM + LENGTH; i++)44    if (C[i] != A[i] + B[i])45      ++errors;46 47  // CHECK: Success48  if (errors)49    fprintf(stderr, "Failure\n");50  else51    fprintf(stderr, "Success\n");52 53  free(A);54  free(B);55  free(C);56 57  return 0;58}59