Skip to content

Commit 271ad67

Browse files
committed
[Flang][OpenMP] Add offload runtime test for custom reduction with derived types
1 parent d914b85 commit 271ad67

File tree

1 file changed

+88
-0
lines changed

1 file changed

+88
-0
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
! Basic offloading test with custom OpenMP reduction on derived type
2+
! REQUIRES: flang, amdgpu
3+
!
4+
! RUN: %libomptarget-compile-fortran-generic
5+
! RUN: env LIBOMPTARGET_INFO=16 %libomptarget-run-generic 2>&1 | %fcheck-generic
6+
module maxtype_mod
7+
implicit none
8+
9+
type maxtype
10+
integer::sumval
11+
integer::maxval
12+
end type maxtype
13+
14+
contains
15+
16+
subroutine initme(x,n)
17+
type(maxtype) :: x,n
18+
x%sumval=0
19+
x%maxval=0
20+
end subroutine initme
21+
22+
function mycombine(lhs, rhs)
23+
type(maxtype) :: lhs, rhs
24+
type(maxtype) :: mycombine
25+
mycombine%sumval = lhs%sumval + rhs%sumval
26+
mycombine%maxval = max(lhs%maxval, rhs%maxval)
27+
end function mycombine
28+
29+
end module maxtype_mod
30+
31+
program main
32+
use maxtype_mod
33+
implicit none
34+
35+
integer :: n = 100
36+
integer :: i
37+
integer :: error = 0
38+
type(maxtype) :: x(100)
39+
type(maxtype) :: res
40+
integer :: expected_sum, expected_max
41+
42+
!$omp declare reduction(red_add_max:maxtype:omp_out=mycombine(omp_out,omp_in)) initializer(initme(omp_priv,omp_orig))
43+
44+
! Initialize array with test data
45+
do i = 1, n
46+
x(i)%sumval = i
47+
x(i)%maxval = i
48+
end do
49+
50+
! Initialize reduction variable
51+
res%sumval = 0
52+
res%maxval = 0
53+
54+
! Perform reduction in target region
55+
!$omp target parallel do map(to:x) reduction(red_add_max:res)
56+
do i = 1, n
57+
res = mycombine(res, x(i))
58+
end do
59+
!$omp end target parallel do
60+
61+
! Compute expected values
62+
expected_sum = 0
63+
expected_max = 0
64+
do i = 1, n
65+
expected_sum = expected_sum + i
66+
expected_max = max(expected_max, i)
67+
end do
68+
69+
! Check results
70+
if (res%sumval /= expected_sum) then
71+
error = 1
72+
endif
73+
74+
if (res%maxval /= expected_max) then
75+
error = 1
76+
endif
77+
78+
if (error == 0) then
79+
print *,"PASSED"
80+
else
81+
print *,"FAILED"
82+
endif
83+
84+
end program main
85+
86+
! CHECK: "PluginInterface" device {{[0-9]+}} info: Launching kernel {{.*}}
87+
! CHECK: PASSED
88+

0 commit comments

Comments
 (0)