Hello,
I am a cusparse beginner and want to call the functions in the cusparse library to solve the tridiagonal matrix problem. Here is a program I wrote with reference to forum users’ code, The output of the program is not the solution of the matrix, but the value originally assigned to the B vector. How do I solve this problem?
Thank you very much!
PROGRAM TDMA
use iso_C_binding
use cudafor
use cusparse
implicit none
! atri - sub-diagonal (means it is the diagonal below the main diagonal)
! btri - the main diagonal
! ctri - sup-diagonal (means it is the diagonal above the main diagonal)
! dtri - right part
! npts - number of equations
integer(c_int), parameter :: npts = 31
real(c_double), dimension(npts) :: atri, btri, ctri, dtri
integer(c_int) :: cusparseCreate_status
type(cusparseHandle) :: handle
integer(c_int) :: m, n, ldb
real(c_double), dimension(npts) :: dl, d, du
real(c_double), dimension(npts, 1) :: B
integer(c_int) :: i
integer(c_size_t), target :: bufferSizeInBytes
integer(c_int) :: istat
integer(c_int), dimension(:), allocatable :: buffer
atri = 1.0
atri(1) = 0.0
btri = 2.0
ctri = 1.0
ctri(npts) = 0.0
do i = 1, 31
dtri(i) = i
end do
do i = 1, npts
B(i, 1) = dtri(i)
end do
dl = atri
d = btri
du = ctri
m = npts
n = 1
ldb = 1
cusparseCreate_status = cusparseCreate(handle)
print *, 'CREATE cusparseCreate_status: '
if (cusparseCreate_status == CUSPARSE_STATUS_SUCCESS) then
print *, 'CUSPARSE_STATUS_SUCCESS'
elseif (cusparseCreate_status == CUSPARSE_STATUS_NOT_INITIALIZED) then
print *, 'CUSPARSE_STATUS_NOT_INITIALIZED'
elseif (cusparseCreate_status == CUSPARSE_STATUS_ALLOC_FAILED) then
print *, 'CUSPARSE_STATUS_ALLOC_FAILED'
elseif (cusparseCreate_status == CUSPARSE_STATUS_ARCH_MISMATCH) then
print *, 'CUSPARSE_STATUS_ARCH_MISMATCHED'
end if
istat = cusparseDgtsv2_nopivot_buffersizeext(handle, m, n, dl, d, du, B, ldb, bufferSizeInBytes)
allocate(buffer(bufferSizeInBytes / c_sizeof(buffer(1))))
istat = cusparseDgtsv2_nopivot(handle, m, n, dl, d, du, B, ldb, buffer)
print *, 'Dgtsv STATUS: '
if (istat == CUSPARSE_STATUS_SUCCESS) then
print *, 'CUSPARSE_STATUS_SUCCESS'
elseif (istat == CUSPARSE_STATUS_NOT_INITIALIZED) then
print *, 'CUSPARSE_STATUS_NOT_INITIALIZED'
elseif (istat == CUSPARSE_STATUS_ALLOC_FAILED) then
print *, 'CUSPARSE_STATUS_ALLOC_FAILED'
elseif (istat == CUSPARSE_STATUS_INVALID_VALUE) then
print *, 'CUSPARSE_STATUS_INVALID_VALUE'
elseif (istat == CUSPARSE_STATUS_ARCH_MISMATCH) then
print *, 'CUSPARSE_STATUS_ARCH_MISMATCHED'
elseif (istat == CUSPARSE_STATUS_EXECUTION_FAILED) then
print *, 'CUSPARSE_STATUS_EXECUTION_FAILED'
elseif (istat == CUSPARSE_STATUS_INTERNAL_ERROR) then
print *, 'CUSPARSE_STATUS_INTERNAL_ERROR'
end if
do i = 1, npts
dtri(i) = B(i, 1)
end do
! Printing solution
print *, 'The solution is: '
! Expected Solution = transpose of (0,1,0,2,0,3,...,0,8,0,7,0,6,...,0,1,0)
do i = 1, npts
print *, 'SOL(', i, '):', dtri(i)
end do
END PROGRAM TDMA