summaryrefslogtreecommitdiff
path: root/libgomp/testsuite/libgomp.oacc-c-c++-common/lib-90.c
blob: d17755bf3eb36e6f4d686681b89fc3916ba77084 (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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/* { dg-do run { target openacc_nvidia_accel_selected } } */
/* { dg-additional-options "-lcuda" } */

#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 "" } */