Debian Codegraph @

gcc/testsuite/c-c++-common/gomp/metadirective-target-device-2.c

raw
1 /* { dg-do compile } */
2 /* { dg-additional-options "-fdump-tree-optimized" } */
3
4 /* In configurations without offloading configured, we can resolve many
5 instances of the target_device context selector at gimplification time
6 instead of waiting until late resolution. */
7
8 /* Device 0 may be either the host or an offloading device, in configurations
9 that support them. */
10 void
11 f1 (int n, double* a, double s)
12 {
13 #pragma omp metadirective \
14 when (target_device={device_num(0), kind(host)} \
15 : parallel for) \
16 default (error at(execution) message("f1 match failed"))
17 for (int i = 0; i < n; i++)
18 a[i] = a[i] * s;
19 }
20 /* { dg-final { scan-tree-dump-not "f1 match failed" "optimized" { target { ! offloading_enabled } } } } */
21 /* { dg-final { scan-tree-dump "f1 match failed" "optimized" { target offloading_enabled } } } */
22
23 /* Device -1 is always the host, even in offloading configurations. */
24 void
25 f2 (int n, double* a, double s)
26 {
27 #pragma omp metadirective \
28 when (target_device={device_num(-1), kind(host)} \
29 : parallel for) \
30 default (error at(execution) message("f2 match failed"))
31 for (int i = 0; i < n; i++)
32 a[i] = a[i] * s;
33 }
34 /* { dg-final { scan-tree-dump-not "f2 match failed" "optimized" } } */
35
36 /* Constant device numbers < -1 cause the match to fail, even in offloading
37 configurations. */
38 void
39 f3 (int n, double* a, double s)
40 {
41 #pragma omp metadirective \
42 when (target_device={device_num(-42), kind(host)} \
43 : error at(execution) message("f3 match failed")) \
44 default (parallel for)
45 for (int i = 0; i < n; i++)
46 a[i] = a[i] * s;
47 }
48 /* { dg-final { scan-tree-dump-not "f3 match failed" "optimized" } } */
49
50 /* In a non-offloading configuration, devices > 0 are always invalid and
51 will cause the match to fail. In an offload configuration, we don't
52 know at compile-time if the device number is valid or if it refers to
53 a host or offload device. */
54 void
55 f4 (int n, double* a, double s)
56 {
57 #pragma omp metadirective \
58 when (target_device={device_num(42), kind(host)} \
59 : error at(execution) message("f4 match failed")) \
60 default (parallel for)
61 for (int i = 0; i < n; i++)
62 a[i] = a[i] * s;
63 }
64 /* { dg-final { scan-tree-dump-not "f4 match failed" "optimized" { target { ! offloading_enabled } } } } */
65 /* { dg-final { scan-tree-dump "f4 match failed" "optimized" { target offloading_enabled } } } */
66
67 extern int omp_get_initial_device (void);
68 extern int omp_get_default_device (void);
69 extern int omp_get_num_devices (void);
70 extern int omp_get_device_num (void);
71
72 /* On a non-offloading configuration, omp_get_default_device() always refers
73 to the host. With offloading, we know it's a valid device number but
74 not whether it's the host. */
75 void
76 f5 (int n, double* a, double s)
77 {
78 #pragma omp metadirective \
79 when (target_device={device_num(omp_get_default_device ()), kind(host)} \
80 : parallel for) \
81 default (error at(execution) message("f5 match failed"))
82 for (int i = 0; i < n; i++)
83 a[i] = a[i] * s;
84 }
85 /* { dg-final { scan-tree-dump-not "f5 match failed" "optimized" { target { ! offloading_enabled } } } } */
86 /* { dg-final { scan-tree-dump "f5 match failed" "optimized" { target offloading_enabled } } } */
87
88 /* omp_get_initial_device() always refers to the host, whether offloading is
89 configured or not. */
90 void
91 f6 (int n, double* a, double s)
92 {
93 #pragma omp metadirective \
94 when (target_device={device_num(omp_get_initial_device ()), kind(host)} \
95 : parallel for) \
96 default (error at(execution) message("f6 match failed"))
97 for (int i = 0; i < n; i++)
98 a[i] = a[i] * s;
99 }
100 /* { dg-final { scan-tree-dump-not "f6 match failed" "optimized" } } */
101
102 /* omp_get_num_devices() returns the number of non-host offload devices,
103 therefore using it as a device number always refers to the host, whether
104 offloading is enabled or not. */
105 void
106 f7 (int n, double* a, double s)
107 {
108 #pragma omp metadirective \
109 when (target_device={device_num(omp_get_num_devices ()), kind(host)} \
110 : parallel for) \
111 default (error at(execution) message("f7 match failed"))
112 for (int i = 0; i < n; i++)
113 a[i] = a[i] * s;
114 }
115 /* { dg-final { scan-tree-dump-not "f7 match failed" "optimized" } } */
116
117 /* omp_get_device_num() always returns a valid device number but we don't
118 know whether it's the host or offload device, in configurations that
119 support offloading. */
120 void
121 f8 (int n, double* a, double s)
122 {
123 #pragma omp metadirective \
124 when (target_device={device_num(omp_get_device_num ()), kind(host)} \
125 : parallel for) \
126 default (error at(execution) message("f8 match failed"))
127 for (int i = 0; i < n; i++)
128 a[i] = a[i] * s;
129 }
130 /* { dg-final { scan-tree-dump-not "f8 match failed" "optimized" { target { ! offloading_enabled } } } } */
131 /* { dg-final { scan-tree-dump "f8 match failed" "optimized" { target offloading_enabled } } } */
132