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

#define LIST_LENGTH 10

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)
    {
#pragma acc exit data detach(head->next)
    }

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

#pragma acc enter data copyin(n[:1])
#pragma acc enter data attach(head->next)
  if (n->next)
    {
#pragma acc enter data attach(n->next)
    }
}

void
destroy (struct node *head)
{
  while (head->next != NULL)
    {
#pragma acc exit data detach(head->next)
      struct node * n = head->next;
      head->next = n->next;
      if (n->next)
	{
#pragma acc exit data detach(n->next)
	}
#pragma acc exit data delete (n[:1])
      if (head->next)
	{
#pragma acc enter data attach(head->next)
	}
      free (n);
    }
}

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

#pragma acc enter data copyin(list)

  for (i = 0; i < LIST_LENGTH; i++)
    insert (&list, i + 1);

  assert (sum_nodes (&list) == (LIST_LENGTH * LIST_LENGTH + LIST_LENGTH) / 2);

  destroy (&list);

#pragma acc exit data delete(list)

  return 0;
}