* c-c++-common/Wrestrict.c (test_strcpy_range): Revert latest change.
[official-gcc.git] / libgomp / testsuite / libgomp.oacc-c-c++-common / context-4.c
blob71365e8ed32e84f7b3887de9d63fe8faa378db0b
1 /* { dg-do run { target openacc_nvidia_accel_selected } } */
2 /* { dg-additional-options "-lcuda -lcublas -lcudart" } */
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <cuda.h>
7 #include <cuda_runtime_api.h>
8 #include <cublas_v2.h>
9 #include <openacc.h>
11 void
12 saxpy (int n, float a, float *x, float *y)
14 int i;
16 for (i = 0; i < n; i++)
18 y[i] = a * x[i] + y[i];
22 void
23 context_check (CUcontext ctx1)
25 CUcontext ctx2, ctx3;
26 CUresult r;
28 r = cuCtxGetCurrent (&ctx2);
29 if (r != CUDA_SUCCESS)
31 fprintf (stderr, "cuCtxGetCurrent failed: %d\n", r);
32 exit (EXIT_FAILURE);
35 if (ctx1 != ctx2)
37 fprintf (stderr, "new context established\n");
38 exit (EXIT_FAILURE);
41 ctx3 = (CUcontext) acc_get_current_cuda_context ();
43 if (ctx1 != ctx3)
45 fprintf (stderr, "acc_get_current_cuda_context returned wrong value\n");
46 exit (EXIT_FAILURE);
49 return;
52 int
53 main (int argc, char **argv)
55 cublasStatus_t s;
56 cublasHandle_t h;
57 CUcontext pctx;
58 CUresult r;
59 int i;
60 const int N = 256;
61 float *h_X, *h_Y1, *h_Y2;
62 float *d_X,*d_Y;
63 float alpha = 2.0f;
64 float error_norm;
65 float ref_norm;
67 /* Test 4 - OpenACC creates, cuBLAS shares. */
69 acc_set_device_num (0, acc_device_nvidia);
71 r = cuCtxGetCurrent (&pctx);
72 if (r != CUDA_SUCCESS)
74 fprintf (stderr, "cuCtxGetCurrent failed: %d\n", r);
75 exit (EXIT_FAILURE);
78 h_X = (float *) malloc (N * sizeof (float));
79 if (h_X == 0)
81 fprintf (stderr, "malloc failed: for h_X\n");
82 exit (EXIT_FAILURE);
85 h_Y1 = (float *) malloc (N * sizeof (float));
86 if (h_Y1 == 0)
88 fprintf (stderr, "malloc failed: for h_Y1\n");
89 exit (EXIT_FAILURE);
92 h_Y2 = (float *) malloc (N * sizeof (float));
93 if (h_Y2 == 0)
95 fprintf (stderr, "malloc failed: for h_Y2\n");
96 exit (EXIT_FAILURE);
99 for (i = 0; i < N; i++)
101 h_X[i] = rand () / (float) RAND_MAX;
102 h_Y2[i] = h_Y1[i] = rand () / (float) RAND_MAX;
105 #pragma acc parallel copyin (h_X[0:N]), copy (h_Y2[0:N]) copy (alpha)
107 int i;
109 for (i = 0; i < N; i++)
111 h_Y2[i] = alpha * h_X[i] + h_Y2[i];
115 r = cuCtxGetCurrent (&pctx);
116 if (r != CUDA_SUCCESS)
118 fprintf (stderr, "cuCtxGetCurrent failed: %d\n", r);
119 exit (EXIT_FAILURE);
122 d_X = (float *) acc_copyin (&h_X[0], N * sizeof (float));
123 if (d_X == NULL)
125 fprintf (stderr, "copyin error h_Y1\n");
126 exit (EXIT_FAILURE);
129 d_Y = (float *) acc_copyin (&h_Y1[0], N * sizeof (float));
130 if (d_Y == NULL)
132 fprintf (stderr, "copyin error h_Y1\n");
133 exit (EXIT_FAILURE);
136 s = cublasCreate (&h);
137 if (s != CUBLAS_STATUS_SUCCESS)
139 fprintf (stderr, "cublasCreate failed: %d\n", s);
140 exit (EXIT_FAILURE);
143 context_check (pctx);
145 s = cublasSaxpy (h, N, &alpha, d_X, 1, d_Y, 1);
146 if (s != CUBLAS_STATUS_SUCCESS)
148 fprintf (stderr, "cublasSaxpy failed: %d\n", s);
149 exit (EXIT_FAILURE);
152 context_check (pctx);
154 acc_memcpy_from_device (&h_Y1[0], d_Y, N * sizeof (float));
156 context_check (pctx);
158 error_norm = 0;
159 ref_norm = 0;
161 for (i = 0; i < N; ++i)
163 float diff;
165 diff = h_Y1[i] - h_Y2[i];
166 error_norm += diff * diff;
167 ref_norm += h_Y2[i] * h_Y2[i];
170 error_norm = (float) sqrt ((double) error_norm);
171 ref_norm = (float) sqrt ((double) ref_norm);
173 if ((fabs (ref_norm) < 1e-7) || ((error_norm / ref_norm) >= 1e-6f))
175 fprintf (stderr, "math error\n");
176 exit (EXIT_FAILURE);
179 free (h_X);
180 free (h_Y1);
181 free (h_Y2);
183 acc_free (d_X);
184 acc_free (d_Y);
186 context_check (pctx);
188 s = cublasDestroy (h);
189 if (s != CUBLAS_STATUS_SUCCESS)
191 fprintf (stderr, "cublasDestroy failed: %d\n", s);
192 exit (EXIT_FAILURE);
195 context_check (pctx);
197 acc_shutdown (acc_device_nvidia);
199 r = cuCtxGetCurrent (&pctx);
200 if (r != CUDA_SUCCESS)
202 fprintf (stderr, "cuCtxGetCurrent failed: %d\n", r);
203 exit (EXIT_FAILURE);
206 if (pctx)
208 fprintf (stderr, "Unexpected context\n");
209 exit (EXIT_FAILURE);
212 return EXIT_SUCCESS;