Skip to content

Commit 21d796b

Browse files
[smoke-fort-fails] Add test case for use_device_ptr (#2196)
Check if use_device_ptr generates the same result as use_device_ptr.
1 parent ea5887c commit 21d796b

3 files changed

Lines changed: 127 additions & 0 deletions

File tree

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
include ../../Makefile.defs
2+
3+
TESTNAME = use_device_ptr_cptr
4+
TESTSRC_MAIN = main.f90
5+
TESTSRC_AUX = bar.o
6+
TESTSRC_ALL = $(TESTSRC_MAIN) $(TESTSRC_AUX)
7+
AOMPHIP ?= $(AOMP)
8+
HIPCC ?= $(AOMPHIP)/bin/hipcc
9+
HIP_CLANG_PATH ?= $(AOMP)/bin
10+
11+
CFLAGS = -O3
12+
FLANG ?= flang
13+
OMP_BIN = $(AOMP)/bin/$(FLANG)
14+
CC = $(OMP_BIN) $(VERBOSE)
15+
EXTRA_CFLAGS = -L$(AOMPHIP)/lib -lamdhip64 -Wl,-rpath,$(AOMPHIP)/lib -fPIC
16+
#-ccc-print-phases
17+
#"-\#\#\#"
18+
19+
include ../Makefile.rules
20+
all: $(TESTNAME)
21+
22+
bar.o : bar.hip
23+
HIP_CLANG_PATH=$(HIP_CLANG_PATH) $(HIPCC) -c --offload-arch=$(AOMP_GPU) -fPIC $^ -o $@
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include <hip/hip_runtime.h>
2+
#include <stdlib.h>
3+
4+
__global__ void bar_kernel(int *x, int *y, int *z, int n)
5+
{
6+
int i = blockIdx.x * blockDim.x + threadIdx.x;
7+
8+
if (i < n) {
9+
z[i] += x[i] + y[i];
10+
}
11+
}
12+
13+
extern "C" {
14+
15+
void bar_GPU(int *x, int *y, int *z, int n)
16+
{
17+
int num_threads = 256;
18+
int num_blocks = n / num_threads + 1;
19+
hipLaunchKernelGGL(bar_kernel, dim3(num_blocks), dim3(num_threads), 0, 0, x, y, z, n);
20+
(void)hipDeviceSynchronize();
21+
}
22+
23+
} /* extern "C" */
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
MODULE foo
2+
USE iso_c_binding
3+
USE omp_lib
4+
IMPLICIT NONE
5+
PRIVATE
6+
PUBLIC :: bar_device_ptr, bar_device_addr
7+
8+
INTERFACE
9+
SUBROUTINE bar(x, y, z, n) BIND(C, name="bar_GPU")
10+
USE iso_c_binding
11+
TYPE(C_PTR), VALUE, INTENT(IN) :: x, y, z
12+
INTEGER(C_INT), VALUE, INTENT(IN) :: n
13+
END SUBROUTINE
14+
15+
END INTERFACE
16+
17+
CONTAINS
18+
19+
SUBROUTINE bar_device_addr(x,y,z,n)
20+
INTEGER, TARGET, INTENT(IN) :: x(:), y(:)
21+
INTEGER, TARGET, INTENT(INOUT) :: z(:)
22+
INTEGER(C_INT), INTENT(IN) :: n
23+
!$omp target data use_device_addr (x, y, z)
24+
CALL bar(c_loc(x), c_loc(y), c_loc(z), n)
25+
!$omp end target data
26+
END SUBROUTINE
27+
28+
SUBROUTINE bar_device_ptr(x,y,z,n)
29+
INTEGER, TARGET, INTENT(IN) :: x(:), y(:)
30+
INTEGER, TARGET, INTENT(INOUT) :: z(:)
31+
INTEGER(C_INT), INTENT(IN) :: n
32+
TYPE(C_PTR) :: x_ptr, y_ptr, z_ptr
33+
34+
x_ptr = c_loc(x)
35+
y_ptr = c_loc(y)
36+
z_ptr = c_loc(z)
37+
!$omp target data use_device_ptr (x_ptr, y_ptr, z_ptr)
38+
CALL bar(x_ptr, y_ptr, z_ptr, n)
39+
!$omp end target data
40+
END SUBROUTINE
41+
42+
END MODULE foo
43+
44+
PROGRAM test_ptr
45+
USE iso_c_binding
46+
USE omp_lib
47+
USE foo
48+
IMPLICIT NONE
49+
50+
INTEGER, ALLOCATABLE, TARGET :: x(:), y(:), z(:)
51+
INTEGER, ALLOCATABLE, TARGET :: x1(:), y1(:), z1(:)
52+
INTEGER(C_INT) :: i, n
53+
n = 1000
54+
ALLOCATE(x(n), y(n), z(n))
55+
ALLOCATE(x1(n), y1(n), z1(n))
56+
z = 0
57+
z1 = 0
58+
x = 1
59+
y = 2
60+
x1 = 1
61+
y1 = 2
62+
i = 1
63+
!$omp target enter data map(to: x,y,z,x1,y1,z1)
64+
65+
CALL bar_device_addr(x,y,z,n)
66+
CALL bar_device_ptr(x1,y1,z1,n)
67+
!$omp target exit data map(from: x,y,z,x1,y1,z1)
68+
DO i = 1,n
69+
IF (z(i) .ne. 3) then
70+
PRINT *, "Bad result for use_device_addr!"
71+
STOP 1
72+
ENDIF
73+
IF (z1(i) .ne. 3) then
74+
PRINT *, "Bad result for use_device_ptr!"
75+
STOP 1
76+
ENDIF
77+
END DO
78+
DEALLOCATE(x,y,z,x1,y1,z1)
79+
PRINT *, "Success"
80+
END PROGRAM test_ptr
81+

0 commit comments

Comments
 (0)