(root)/
gcc-13.2.0/
libgomp/
testsuite/
libgomp.oacc-c-c++-common/
deep-copy-5.c
       1  #include <assert.h>
       2  #include <stdlib.h>
       3  #include <openacc.h>
       4  
       5  struct node
       6  {
       7    struct node *next;
       8    int val;
       9  };
      10  
      11  int
      12  sum_nodes (struct node *head)
      13  {
      14    int i = 0, sum = 0;
      15  
      16  #pragma acc parallel reduction(+:sum) present(head[:1])
      17    {
      18      for (; head != NULL; head = head->next)
      19        sum += head->val;
      20    }
      21  
      22    return sum;
      23  }
      24  
      25  void
      26  insert (struct node *head, int val)
      27  {
      28    struct node *n = (struct node *) malloc (sizeof (struct node));
      29  
      30    if (head->next)
      31      acc_detach ((void **) &head->next);
      32  
      33    n->val = val;
      34    n->next = head->next;
      35    head->next = n;
      36  
      37    acc_copyin (n, sizeof (struct node));
      38    acc_attach((void **) &head->next);
      39  
      40    if (n->next)
      41      acc_attach ((void **) &n->next);
      42  }
      43  
      44  void
      45  destroy (struct node *head)
      46  {
      47    while (head->next != NULL)
      48      {
      49        acc_detach ((void **) &head->next);
      50        struct node * n = head->next;
      51        head->next = n->next;
      52        if (n->next)
      53  	acc_detach ((void **) &n->next);
      54  
      55        acc_delete (n, sizeof (struct node));
      56        if (head->next)
      57  	acc_attach((void **) &head->next);
      58  
      59        free (n);
      60      }
      61  }
      62  
      63  int
      64  main ()
      65  {
      66    struct node list = { .next = NULL, .val = 0 };
      67    int i;
      68  
      69    acc_copyin (&list, sizeof (struct node));
      70  
      71    for (i = 0; i < 10; i++)
      72      insert (&list, 2);
      73  
      74    assert (sum_nodes (&list) == 10 * 2);
      75  
      76    destroy (&list);
      77  
      78    acc_delete (&list, sizeof (struct node));
      79  
      80    return 0;
      81  }