libgomp testsuite: OpenACC C++ testing.
[official-gcc.git] / libgomp / testsuite / libgomp.oacc-c-c++-common / lib-90.c
blobd17755bf3eb36e6f4d686681b89fc3916ba77084
1 /* { dg-do run { target openacc_nvidia_accel_selected } } */
2 /* { dg-additional-options "-lcuda" } */
4 #include <pthread.h>
5 #include <stdio.h>
6 #include <string.h>
7 #include <stdlib.h>
8 #include <unistd.h>
9 #include <errno.h>
10 #include <ctype.h>
11 #include <openacc.h>
12 #include <cuda.h>
14 unsigned char **x;
15 void **d_x;
16 const int N = 16;
17 const int NTHREADS = 32;
19 static void *
20 test (void *arg)
22 int i;
23 int tid;
24 unsigned char *p;
25 int devnum;
27 tid = (int) (long) arg;
29 devnum = acc_get_device_num (acc_device_nvidia);
30 acc_set_device_num (devnum, acc_device_nvidia);
32 if (acc_get_current_cuda_context () == NULL)
33 abort ();
35 p = (unsigned char *) malloc (N);
37 for (i = 0; i < N; i++)
39 p[i] = tid;
42 x[tid] = p;
44 d_x[tid] = acc_copyin (p, N);
46 acc_wait_all ();
48 return 0;
51 int
52 main (int argc, char **argv)
54 int i;
55 pthread_attr_t attr;
56 pthread_t *tid;
57 CUresult r;
58 CUstream s;
60 acc_init (acc_device_nvidia);
62 x = (unsigned char **) malloc (NTHREADS * N);
63 d_x = (void **) malloc (NTHREADS * N);
65 if (pthread_attr_init (&attr) != 0)
66 perror ("pthread_attr_init failed");
68 tid = (pthread_t *) malloc (NTHREADS * sizeof (pthread_t));
70 r = cuStreamCreate (&s, CU_STREAM_DEFAULT);
71 if (r != CUDA_SUCCESS)
73 fprintf (stderr, "cuStreamCreate failed: %d\n", r);
74 abort ();
77 if (!acc_set_cuda_stream (0, s))
78 abort ();
80 for (i = 0; i < NTHREADS; i++)
82 if (pthread_create (&tid[i], &attr, &test, (void *) (unsigned long) (i))
83 != 0)
84 perror ("pthread_create failed");
87 if (pthread_attr_destroy (&attr) != 0)
88 perror ("pthread_attr_destroy failed");
90 for (i = 0; i < NTHREADS; i++)
92 void *res;
94 if (pthread_join (tid[i], &res) != 0)
95 perror ("pthread join failed");
99 for (i = 0; i < NTHREADS; i++)
101 if (acc_is_present (x[i], N) != 1)
102 abort ();
105 acc_get_cuda_stream (1);
107 for (i = 0; i < NTHREADS; i++)
109 memset (x[i], 0, N);
110 acc_copyout (x[i], N);
113 acc_wait_all ();
115 for (i = 0; i < NTHREADS; i++)
117 unsigned char *p;
118 int j;
120 p = x[i];
122 for (j = 0; j < N; j++)
124 if (p[j] != i)
125 abort ();
128 if (acc_is_present (x[i], N) != 0)
129 abort ();
132 acc_shutdown (acc_device_nvidia);
134 return 0;
137 /* { dg-output "" } */