aboutsummaryrefslogtreecommitdiff
path: root/libgomp/testsuite/libgomp.oacc-c-c++-common/lib-91.c
blob: d14226c026e433d4de8668eb4910d7de455fde89 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/* { dg-do run { target openacc_nvidia_accel_selected } } */
/* { dg-additional-options "-lcuda" } */
/* { dg-require-effective-target openacc_cuda } */

#include <stdlib.h>
#include <unistd.h>
#include <openacc.h>
#include <sys/time.h>
#include <stdio.h>
#include <cuda.h>

int
main (int argc, char **argv)
{
  const int N = 1024 * 1024;
  int i;
  unsigned char *h;
  void *d;
  float async, sync;
  struct timeval start, stop;
  CUresult r;
  CUstream s;

  acc_init (acc_device_nvidia);

  h = (unsigned char *) malloc (N);

  for (i = 0; i < N; i++)
    {
      h[i] = i;
    }

  d = acc_malloc (N);

  acc_map_data (h, d, N);

  gettimeofday (&start, NULL);

  for (i = 0; i < 100; i++)
    {
#pragma acc update device(h[0:N])
    }

  gettimeofday (&stop, NULL);

  sync = (float) (stop.tv_sec - start.tv_sec);
  sync += (float) ((stop.tv_usec - start.tv_usec) / 1000000.0);

  gettimeofday (&start, NULL);

  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 < 100; i++)
    {
#pragma acc update device(h[0:N]) async(0)
    }

  acc_wait_all ();

  gettimeofday (&stop, NULL);

  async = (float) (stop.tv_sec - start.tv_sec);
  async += (float) ((stop.tv_usec - start.tv_usec) / 1000000.0);

  if (async > (sync * 1.5))
    abort ();

  acc_unmap_data (h);

  acc_free (d);

  free (h);

  acc_shutdown (acc_device_nvidia);

  return 0;
}

/* { dg-output "" } */