aboutsummaryrefslogtreecommitdiff
path: root/libgomp/testsuite/libgomp.oacc-c-c++-common/deep-copy-5.c
blob: 89cafbb62abbf2d4467ca03dbfb1684d9858d54e (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
#include <assert.h>
#include <stdlib.h>
#include <openacc.h>

struct node
{
  struct node *next;
  int val;
};

int
sum_nodes (struct node *head)
{
  int i = 0, sum = 0;

#pragma acc parallel reduction(+:sum) present(head[:1])
  {
    for (; head != NULL; head = head->next)
      sum += head->val;
  }

  return sum;
}

void
insert (struct node *head, int val)
{
  struct node *n = (struct node *) malloc (sizeof (struct node));

  if (head->next)
    acc_detach ((void **) &head->next);

  n->val = val;
  n->next = head->next;
  head->next = n;

  acc_copyin (n, sizeof (struct node));
  acc_attach((void **) &head->next);

  if (n->next)
    acc_attach ((void **) &n->next);
}

void
destroy (struct node *head)
{
  while (head->next != NULL)
    {
      acc_detach ((void **) &head->next);
      struct node * n = head->next;
      head->next = n->next;
      if (n->next)
	acc_detach ((void **) &n->next);

      acc_delete (n, sizeof (struct node));
      if (head->next)
	acc_attach((void **) &head->next);

      free (n);
    }
}

int
main ()
{
  struct node list = { .next = NULL, .val = 0 };
  int i;

  acc_copyin (&list, sizeof (struct node));

  for (i = 0; i < 10; i++)
    insert (&list, 2);

  assert (sum_nodes (&list) == 10 * 2);

  destroy (&list);

  acc_delete (&list, sizeof (struct node));

  return 0;
}