1 /* { dg-do run { target openacc_nvidia_accel_selected } } */
2 /* { dg-additional-options "-lcuda" } */
3 /* { dg-require-effective-target openacc_cuda } */
4
5 #include <pthread.h>
6 #include <stdio.h>
7 #include <string.h>
8 #include <stdlib.h>
9 #include <unistd.h>
10 #include <errno.h>
11 #include <ctype.h>
12 #include <openacc.h>
13 #include <cuda.h>
14
15 unsigned char **x;
16 void **d_x;
17 const int N = 16;
18 const int NTHREADS = 32;
19
20 static void *
21 test (void *arg)
22 {
23 int i;
24 int tid;
25 unsigned char *p;
26 int devnum;
27
28 tid = (int) (long) arg;
29
30 devnum = acc_get_device_num (acc_device_nvidia);
31 acc_set_device_num (devnum, acc_device_nvidia);
32
33 if (acc_get_current_cuda_context () == NULL)
34 abort ();
35
36 p = (unsigned char *) malloc (N);
37
38 for (i = 0; i < N; i++)
39 {
40 p[i] = tid;
41 }
42
43 x[tid] = p;
44
45 d_x[tid] = acc_copyin (p, N);
46
47 acc_wait_all ();
48
49 return 0;
50 }
51
52 int
53 main (int argc, char **argv)
54 {
55 int i;
56 pthread_attr_t attr;
57 pthread_t *tid;
58 CUresult r;
59 CUstream s;
60
61 acc_init (acc_device_nvidia);
62
63 x = (unsigned char **) malloc (NTHREADS * N);
64 d_x = (void **) malloc (NTHREADS * N);
65
66 if (pthread_attr_init (&attr) != 0)
67 perror ("pthread_attr_init failed");
68
69 tid = (pthread_t *) malloc (NTHREADS * sizeof (pthread_t));
70
71 r = cuStreamCreate (&s, CU_STREAM_DEFAULT);
72 if (r != CUDA_SUCCESS)
73 {
74 fprintf (stderr, "cuStreamCreate failed: %d\n", r);
75 abort ();
76 }
77
78 if (!acc_set_cuda_stream (0, s))
79 abort ();
80
81 for (i = 0; i < NTHREADS; i++)
82 {
83 if (pthread_create (&tid[i], &attr, &test, (void *) (unsigned long) (i))
84 != 0)
85 perror ("pthread_create failed");
86 }
87
88 if (pthread_attr_destroy (&attr) != 0)
89 perror ("pthread_attr_destroy failed");
90
91 for (i = 0; i < NTHREADS; i++)
92 {
93 void *res;
94
95 if (pthread_join (tid[i], &res) != 0)
96 perror ("pthread join failed");
97 }
98
99
100 for (i = 0; i < NTHREADS; i++)
101 {
102 if (acc_is_present (x[i], N) != 1)
103 abort ();
104 }
105
106 acc_get_cuda_stream (1);
107
108 for (i = 0; i < NTHREADS; i++)
109 {
110 memset (x[i], 0, N);
111 acc_copyout (x[i], N);
112 }
113
114 acc_wait_all ();
115
116 for (i = 0; i < NTHREADS; i++)
117 {
118 unsigned char *p;
119 int j;
120
121 p = x[i];
122
123 for (j = 0; j < N; j++)
124 {
125 if (p[j] != i)
126 abort ();
127 }
128
129 if (acc_is_present (x[i], N) != 0)
130 abort ();
131 }
132
133 acc_shutdown (acc_device_nvidia);
134
135 return 0;
136 }
137
138 /* { dg-output "" } */