gcc/libgomp/testsuite/libgomp.oacc-c-c++-common/lib-90.c
Jakub Jelinek 79e3f7d54b libgomp: Add openacc_{cuda,cublas,cudart} effective targets and use them in openacc testsuite
When gcc is configured for nvptx offloading with --without-cuda-driver
and full CUDA isn't installed, many libgomp.oacc-*/* tests fail,
some of them because cuda.h header can't be found, others because
the tests can't be linked against -lcuda, -lcudart or -lcublas.
I usually only have akmod-nvidia and xorg-x11-drv-nvidia-cuda rpms
installed, so libcuda.so.1 can be dlopened and the offloading works,
but linking against those libraries isn't possible nor are the
headers around (for the plugin itself there is the fallback
libgomp/plugin/cuda/cuda.h).

The following patch adds 3 new effective targets and uses them in tests that
needs those.

2021-05-27  Jakub Jelinek  <jakub@redhat.com>

	* testsuite/lib/libgomp.exp (check_effective_target_openacc_cuda,
	check_effective_target_openacc_cublas,
	check_effective_target_openacc_cudart): New.
	* testsuite/libgomp.oacc-fortran/host_data-4.f90: Require effective
	target openacc_cublas.
	* testsuite/libgomp.oacc-fortran/host_data-2.f90: Likewise.
	* testsuite/libgomp.oacc-fortran/host_data-3.f: Likewise.
	* testsuite/libgomp.oacc-c-c++-common/lib-91.c: Require effective
	target openacc_cuda.
	* testsuite/libgomp.oacc-c-c++-common/lib-70.c: Likewise.
	* testsuite/libgomp.oacc-c-c++-common/lib-90.c: Likewise.
	* testsuite/libgomp.oacc-c-c++-common/lib-75.c: Likewise.
	* testsuite/libgomp.oacc-c-c++-common/lib-69.c: Likewise.
	* testsuite/libgomp.oacc-c-c++-common/lib-74.c: Likewise.
	* testsuite/libgomp.oacc-c-c++-common/lib-81.c: Likewise.
	* testsuite/libgomp.oacc-c-c++-common/lib-72.c: Likewise.
	* testsuite/libgomp.oacc-c-c++-common/lib-85.c: Likewise.
	* testsuite/libgomp.oacc-c-c++-common/pr87835.c: Likewise.
	* testsuite/libgomp.oacc-c-c++-common/lib-82.c: Likewise.
	* testsuite/libgomp.oacc-c-c++-common/lib-73.c: Likewise.
	* testsuite/libgomp.oacc-c-c++-common/lib-83.c: Likewise.
	* testsuite/libgomp.oacc-c-c++-common/lib-78.c: Likewise.
	* testsuite/libgomp.oacc-c-c++-common/lib-76.c: Likewise.
	* testsuite/libgomp.oacc-c-c++-common/lib-84.c: Likewise.
	* testsuite/libgomp.oacc-c-c++-common/lib-79.c: Likewise.
	* testsuite/libgomp.oacc-c-c++-common/host_data-1.c: Require effective
	targets openacc_cublas and openacc_cudart.
	* testsuite/libgomp.oacc-c-c++-common/context-1.c: Likewise.
	* testsuite/libgomp.oacc-c-c++-common/context-2.c: Likewise.
	* testsuite/libgomp.oacc-c-c++-common/context-3.c: Likewise.
	* testsuite/libgomp.oacc-c-c++-common/context-4.c: Likewise.
	* testsuite/libgomp.oacc-c-c++-common/acc_get_property-nvptx.c:
	Require effective target openacc_cudart.
	* testsuite/libgomp.oacc-c-c++-common/asyncwait-1.c: Add -DUSE_CUDA_H
	for effective target openacc_cuda and add && defined USE_CUDA_H to
	preprocessor conditionals.  Guard -lcuda also on openacc_cuda
	effective target.
2021-05-27 22:44:36 +02:00

139 lines
2.3 KiB
C

/* { dg-do run { target openacc_nvidia_accel_selected } } */
/* { dg-additional-options "-lcuda" } */
/* { dg-require-effective-target openacc_cuda } */
#include <pthread.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <ctype.h>
#include <openacc.h>
#include <cuda.h>
unsigned char **x;
void **d_x;
const int N = 16;
const int NTHREADS = 32;
static void *
test (void *arg)
{
int i;
int tid;
unsigned char *p;
int devnum;
tid = (int) (long) arg;
devnum = acc_get_device_num (acc_device_nvidia);
acc_set_device_num (devnum, acc_device_nvidia);
if (acc_get_current_cuda_context () == NULL)
abort ();
p = (unsigned char *) malloc (N);
for (i = 0; i < N; i++)
{
p[i] = tid;
}
x[tid] = p;
d_x[tid] = acc_copyin (p, N);
acc_wait_all ();
return 0;
}
int
main (int argc, char **argv)
{
int i;
pthread_attr_t attr;
pthread_t *tid;
CUresult r;
CUstream s;
acc_init (acc_device_nvidia);
x = (unsigned char **) malloc (NTHREADS * N);
d_x = (void **) malloc (NTHREADS * N);
if (pthread_attr_init (&attr) != 0)
perror ("pthread_attr_init failed");
tid = (pthread_t *) malloc (NTHREADS * sizeof (pthread_t));
r = cuStreamCreate (&s, CU_STREAM_DEFAULT);
if (r != CUDA_SUCCESS)
{
fprintf (stderr, "cuStreamCreate failed: %d\n", r);
abort ();
}
if (!acc_set_cuda_stream (0, s))
abort ();
for (i = 0; i < NTHREADS; i++)
{
if (pthread_create (&tid[i], &attr, &test, (void *) (unsigned long) (i))
!= 0)
perror ("pthread_create failed");
}
if (pthread_attr_destroy (&attr) != 0)
perror ("pthread_attr_destroy failed");
for (i = 0; i < NTHREADS; i++)
{
void *res;
if (pthread_join (tid[i], &res) != 0)
perror ("pthread join failed");
}
for (i = 0; i < NTHREADS; i++)
{
if (acc_is_present (x[i], N) != 1)
abort ();
}
acc_get_cuda_stream (1);
for (i = 0; i < NTHREADS; i++)
{
memset (x[i], 0, N);
acc_copyout (x[i], N);
}
acc_wait_all ();
for (i = 0; i < NTHREADS; i++)
{
unsigned char *p;
int j;
p = x[i];
for (j = 0; j < N; j++)
{
if (p[j] != i)
abort ();
}
if (acc_is_present (x[i], N) != 0)
abort ();
}
acc_shutdown (acc_device_nvidia);
return 0;
}
/* { dg-output "" } */