forked from ROCm/rocSOLVER
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_basic.f90
More file actions
195 lines (161 loc) · 5.9 KB
/
Copy pathexample_basic.f90
File metadata and controls
195 lines (161 loc) · 5.9 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
! Copyright (c) 2020 Advanced Micro Devices, Inc.
!
! Permission is hereby granted, free of charge, to any person obtaining a copy
! of this software and associated documentation files (the "Software"), to deal
! in the Software without restriction, including without limitation the rights
! to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
! copies of the Software, and to permit persons to whom the Software is
! furnished to do so, subject to the following conditions:
!
! The above copyright notice and this permission notice shall be included in
! all copies or substantial portions of the Software.
!
! THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
! IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
! FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
! AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
! LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
! OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
! THE SOFTWARE.
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
subroutine HIP_CHECK(stat)
use iso_c_binding
implicit none
integer(c_int) :: stat
if(stat /= 0) then
write(*,*) 'Error: hip error'
stop
end if
end subroutine HIP_CHECK
subroutine ROCBLAS_CHECK(stat)
use iso_c_binding
implicit none
integer(c_int) :: stat
if(stat /= 0) then
write(*,*) 'Error: rocblas error'
stop
endif
end subroutine ROCBLAS_CHECK
subroutine ROCSOLVER_CHECK(stat)
use iso_c_binding
implicit none
integer(c_int) :: stat
if(stat /= 0) then
write(*,*) 'Error: rocsolver error'
stop
endif
end subroutine ROCSOLVER_CHECK
program example_fortran_basic
use iso_c_binding
use rocblas
implicit none
interface
function rocsolver_dgeqrf(handle, M, N, dA, lda, dIpiv) &
result(c_int) &
bind(c, name = 'rocsolver_dgeqrf')
use iso_c_binding
implicit none
type(c_ptr), value :: handle
integer(c_int), value :: M
integer(c_int), value :: N
type(c_ptr), value :: dA
integer(c_int), value :: lda
type(c_ptr), value :: dIpiv
end function rocsolver_dgeqrf
end interface
! TODO: hip workaround until plugin is ready.
interface
function hipMalloc(ptr, size) &
result(c_int) &
bind(c, name = 'hipMalloc')
use iso_c_binding
implicit none
type(c_ptr), value :: ptr
integer(c_size_t), value :: size
end function hipMalloc
end interface
interface
function hipFree(ptr) &
result(c_int) &
bind(c, name = 'hipFree')
use iso_c_binding
implicit none
type(c_ptr), value :: ptr
end function hipFree
end interface
interface
function hipMemcpy(dst, src, size, kind) &
result(c_int) &
bind(c, name = 'hipMemcpy')
use iso_c_binding
implicit none
type(c_ptr), value :: dst
type(c_ptr), intent(in), value :: src
integer(c_size_t), value :: size
integer(c_int), value :: kind
end function hipMemcpy
end interface
interface
function hipMemset(dst, val, size) &
result(c_int) &
bind(c, name = 'hipMemset')
use iso_c_binding
implicit none
type(c_ptr), value :: dst
integer(c_int), value :: val
integer(c_size_t), value :: size
end function hipMemset
end interface
interface
function hipDeviceSynchronize() &
result(c_int) &
bind(c, name = 'hipDeviceSynchronize')
use iso_c_binding
implicit none
end function hipDeviceSynchronize
end interface
interface
function hipDeviceReset() &
result(c_int) &
bind(c, name = 'hipDeviceReset')
use iso_c_binding
implicit none
end function hipDeviceReset
end interface
! TODO end
integer :: i, j ! indices for iterating over results
! Define our input data
real(c_double), target :: hA(3,3) = reshape((/12, 6, -4, -51, 167, 24, 4, -68, -41/), (/3, 3/))
integer(c_int), parameter :: M = 3
integer(c_int), parameter :: N = 3
integer(c_int), parameter :: lda = 3
real(c_double), target :: hIpiv(3) ! CPU buffer for Householder scalars
integer(c_size_t) :: size_A = size(hA)
integer(c_size_t) :: size_Ipiv = size(hIpiv)
type(c_ptr), target :: dA ! GPU buffer for A
type(c_ptr), target :: dIpiv ! GPU buffer for Householder scalars
type(c_ptr), target :: handle ! rocblas_handle
! Allocate device-side memory
call HIP_CHECK(hipMalloc(c_loc(dA), size_A * 8))
call HIP_CHECK(hipMalloc(c_loc(dIpiv), size_Ipiv * 8))
! Create rocBLAS handle
call ROCBLAS_CHECK(rocblas_create_handle(c_loc(handle)))
! Copy memory from host to device
call HIP_CHECK(hipMemcpy(dA, c_loc(hA), size_A * 8, 1))
! Compute the QR factorization on the device
call ROCSOLVER_CHECK(rocsolver_dgeqrf(handle, M, N, dA, lda, dIpiv))
! Copy result from device to host
call HIP_CHECK(hipMemcpy(c_loc(hA), dA, size_A * 8, 2))
call HIP_CHECK(hipMemcpy(c_loc(hIpiv), dIpiv, size_Ipiv * 8, 2))
! Output results
do i = 1,size(hA,1)
print *, (hA(i,j), j=1,size(hA,2))
end do
! Clean up
call HIP_CHECK(hipFree(dA))
call HIP_CHECK(hipFree(dIpiv))
call ROCSOLVER_CHECK(rocblas_destroy_handle(handle))
call HIP_CHECK(hipDeviceReset())
end program example_fortran_basic