File: declare_mapper_nested_default_mappers_complex_structure.cpp

package info (click to toggle)
llvm-toolchain-15 1%3A15.0.6-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,554,644 kB
  • sloc: cpp: 5,922,452; ansic: 1,012,136; asm: 674,362; python: 191,568; objc: 73,855; f90: 42,327; lisp: 31,913; pascal: 11,973; javascript: 10,144; sh: 9,421; perl: 7,447; ml: 5,527; awk: 3,523; makefile: 2,520; xml: 885; cs: 573; fortran: 567
file content (129 lines) | stat: -rw-r--r-- 3,492 bytes parent folder | download | duplicates (7)
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
// RUN: %libomptarget-compilexx-run-and-check-aarch64-unknown-linux-gnu
// RUN: %libomptarget-compilexx-run-and-check-powerpc64-ibm-linux-gnu
// RUN: %libomptarget-compilexx-run-and-check-powerpc64le-ibm-linux-gnu
// RUN: %libomptarget-compilexx-run-and-check-x86_64-pc-linux-gnu
// RUN: %libomptarget-compilexx-run-and-check-nvptx64-nvidia-cuda

#include <assert.h>
#include <omp.h>
#include <stdio.h>
#include <stdlib.h>

#define N 2

class MyObjectA {
public:
  MyObjectA() {
    data1 = 1;
    data2 = 2;
  }
  void show() {
    printf("\t\tObject A Contents:\n");
    printf("\t\t\tdata1 = %d  data2 = %d\n", data1, data2);
  }
  void foo() {
    data1 += 10;
    data2 += 20;
  }
  int data1;
  int data2;
};

class MyObjectB {
public:
  MyObjectB() {
    arr = new MyObjectA[N];
    len = N;
  }
  void show() {
    printf("\tObject B Contents:\n");
    for (int i = 0; i < len; i++)
      arr[i].show();
  }
  void foo() {
    for (int i = 0; i < len; i++)
      arr[i].foo();
  }
  MyObjectA *arr;
  int len;
};
#pragma omp declare mapper(MyObjectB obj) map(obj, obj.arr[:obj.len])

class MyObjectC {
public:
  MyObjectC() {
    arr = new MyObjectB[N];
    len = N;
  }
  void show() {
    printf("Object C Contents:\n");
    for (int i = 0; i < len; i++)
      arr[i].show();
  }
  void foo() {
    for (int i = 0; i < len; i++)
      arr[i].foo();
  }
  MyObjectB *arr;
  int len;
};
#pragma omp declare mapper(MyObjectC obj) map(obj, obj.arr[:obj.len])

int main(void) {
  MyObjectC *outer = new MyObjectC[N];

  printf("Original data hierarchy:\n");
  for (int i = 0; i < N; i++)
    outer[i].show();

  printf("Sending data to device...\n");
#pragma omp target enter data map(to : outer[:N])

  printf("Calling foo()...\n");
#pragma omp target teams distribute parallel for
  for (int i = 0; i < N; i++)
    outer[i].foo();

  printf("foo() complete!\n");

  printf("Sending data back to host...\n");
#pragma omp target exit data map(from : outer[:N])

  printf("Modified Data Hierarchy:\n");
  for (int i = 0; i < N; i++)
    outer[i].show();

  printf("Testing for correctness...\n");
  for (int i = 0; i < N; ++i)
    for (int j = 0; j < N; ++j)
      for (int k = 0; k < N; ++k) {
        printf("outer[%d].arr[%d].arr[%d].data1 = %d.\n", i, j, k,
               outer[i].arr[j].arr[k].data1);
        printf("outer[%d].arr[%d].arr[%d].data2 = %d.\n", i, j, k,
               outer[i].arr[j].arr[k].data2);
        assert(outer[i].arr[j].arr[k].data1 == 11 &&
               outer[i].arr[j].arr[k].data2 == 22);
      }
  // CHECK: outer[0].arr[0].arr[0].data1 = 11.
  // CHECK: outer[0].arr[0].arr[0].data2 = 22.
  // CHECK: outer[0].arr[0].arr[1].data1 = 11.
  // CHECK: outer[0].arr[0].arr[1].data2 = 22.
  // CHECK: outer[0].arr[1].arr[0].data1 = 11.
  // CHECK: outer[0].arr[1].arr[0].data2 = 22.
  // CHECK: outer[0].arr[1].arr[1].data1 = 11.
  // CHECK: outer[0].arr[1].arr[1].data2 = 22.
  // CHECK: outer[1].arr[0].arr[0].data1 = 11.
  // CHECK: outer[1].arr[0].arr[0].data2 = 22.
  // CHECK: outer[1].arr[0].arr[1].data1 = 11.
  // CHECK: outer[1].arr[0].arr[1].data2 = 22.
  // CHECK: outer[1].arr[1].arr[0].data1 = 11.
  // CHECK: outer[1].arr[1].arr[0].data2 = 22.
  // CHECK: outer[1].arr[1].arr[1].data1 = 11.
  // CHECK: outer[1].arr[1].arr[1].data2 = 22.
  assert(outer[1].arr[1].arr[0].data1 == 11 &&
         outer[1].arr[1].arr[0].data2 == 22 &&
         outer[1].arr[1].arr[1].data1 == 11 &&
         outer[1].arr[1].arr[1].data2 == 22);

  return 0;
}