Retro68/gcc/libgomp/testsuite/libgomp.oacc-c-c++-common/host_data-1.c

105 lines
1.9 KiB
C
Raw Normal View History

2017-04-10 11:32:00 +00:00
/* { dg-do run { target openacc_nvidia_accel_selected } } */
2019-06-02 15:48:37 +00:00
/* { dg-additional-options "-lm -lcuda -lcublas -lcudart -Wall -Wextra" } */
2017-04-10 11:32:00 +00:00
#include <stdlib.h>
2019-06-02 15:48:37 +00:00
#include <math.h>
2017-04-10 11:32:00 +00:00
#include <openacc.h>
#include <cuda.h>
#include <cuda_runtime_api.h>
#include <cublas_v2.h>
2019-06-02 15:48:37 +00:00
#pragma acc routine
2017-04-10 11:32:00 +00:00
void
2019-06-02 15:48:37 +00:00
saxpy (int n, float a, float *x, float *y)
2017-04-10 11:32:00 +00:00
{
int i;
for (i = 0; i < n; i++)
y[i] = y[i] + a * x[i];
}
void
2019-06-02 15:48:37 +00:00
validate_results (int n, float *a, float *b)
2017-04-10 11:32:00 +00:00
{
int i;
for (i = 0; i < n; i++)
2019-06-02 15:48:37 +00:00
if (fabs (a[i] - b[i]) > .00001)
abort ();
2017-04-10 11:32:00 +00:00
}
int
2019-06-02 15:48:37 +00:00
main()
2017-04-10 11:32:00 +00:00
{
#define N 8
int i;
float x_ref[N], y_ref[N];
float x[N], y[N];
cublasHandle_t h;
float a = 2.0;
for (i = 0; i < N; i++)
{
x[i] = x_ref[i] = 4.0 + i;
y[i] = y_ref[i] = 3.0;
}
2019-06-02 15:48:37 +00:00
saxpy (N, a, x_ref, y_ref);
2017-04-10 11:32:00 +00:00
cublasCreate (&h);
#pragma acc data copyin (x[0:N]) copy (y[0:N])
{
#pragma acc host_data use_device (x, y)
{
cublasSaxpy (h, N, &a, x, 1, y, 1);
}
}
2019-06-02 15:48:37 +00:00
validate_results (N, y, y_ref);
2017-04-10 11:32:00 +00:00
#pragma acc data create (x[0:N]) copyout (y[0:N])
{
#pragma acc kernels
for (i = 0; i < N; i++)
y[i] = 3.0;
#pragma acc host_data use_device (x, y)
{
cublasSaxpy (h, N, &a, x, 1, y, 1);
}
}
cublasDestroy (h);
2019-06-02 15:48:37 +00:00
validate_results (N, y, y_ref);
2017-04-10 11:32:00 +00:00
for (i = 0; i < N; i++)
y[i] = 3.0;
/* There's no need to use host_data here. */
#pragma acc data copyin (x[0:N]) copyin (a) copy (y[0:N])
{
#pragma acc parallel present (x[0:N]) pcopy (y[0:N]) present (a)
2019-06-02 15:48:37 +00:00
saxpy (N, a, x, y);
2017-04-10 11:32:00 +00:00
}
2019-06-02 15:48:37 +00:00
validate_results (N, y, y_ref);
/* Exercise host_data with data transferred with acc enter data. */
2017-04-10 11:32:00 +00:00
for (i = 0; i < N; i++)
2019-06-02 15:48:37 +00:00
y[i] = 3.0;
#pragma acc enter data copyin (x, a, y)
#pragma acc parallel present (x[0:N]) pcopy (y[0:N]) present (a)
{
saxpy (N, a, x, y);
}
#pragma acc exit data delete (x, a) copyout (y)
validate_results (N, y, y_ref);
2017-04-10 11:32:00 +00:00
return 0;
}