Hello everyone,
I want to do matrix-vector multiplication in either single or double pricision. To do so, I defined two modules for each precision, and at each one I defined pointers to function, dgemv for double precision, and sgemv for single precision.
The code is as follows:
module DoublePrecision
implicit none
integer, parameter :: precision = selected_real_kind(15, 307)
abstract interface
subroutine gemv(trans, m, n, alpha, a, lda, x, incx, beta, y, incy)
import :: precision
character :: trans
integer(4), value :: m, n, lda, incx, incy
real(precision), value :: alpha, beta
real(precision), dimension(lda, *), intent(in) :: a
real(precision), dimension(*), intent(in) :: x
real(precision), dimension(*), intent(inout) :: y
end subroutine gemv
end interface
procedure(gemv), pointer :: MatrixVectorMultiplication => null()
end module DoublePrecision
module SinglePrecision
implicit none
integer, parameter :: precision = selected_real_kind(6, 37)
abstract interface
subroutine gemv(trans, m, n, alpha, a, lda, x, incx, beta, y, incy)
import :: precision
character :: trans
integer(4), value :: m, n, lda, incx, incy
real(precision), value :: alpha, beta
real(precision), dimension(lda, *), intent(in) :: a
real(precision), dimension(*), intent(in) :: x
real(precision), dimension(*), intent(inout) :: y
end subroutine gemv
end interface
procedure(gemv), pointer :: MatrixVectorMultiplication => null()
end module SinglePrecision
program test_MatrixMultiplication
#ifdef single
use SinglePrecision
MatrixVectorMultiplication => sgemv
#else
use DoublePrecision
MatrixVectorMultiplication => dgemv
#endif
end program test_MatrixMultiplication
To compile with the single precision, I use
gfortran -Dsingle -cpp -L/usr/local/opt/openblas/lib/ -I/usr/local/opt/openblas/include/ -lopenblas -ffree-line-length-1024 SingleDoublePrecisionMatrixMultiplication.f90
and for double precision
gfortran -Ddouble -cpp -L/usr/local/opt/openblas/lib/ -I/usr/local/opt/openblas/include/ -lopenblas -ffree-line-length-1024 SingleDoublePrecisionMatrixMultiplication.f90
The compiler raise the error below:
SingleDoublePrecisionMatrixMultiplication.f90:60:33:
60 | MatrixVectorMultiplication => sgemv
| 1
Error: Explicit interface required for 'sgemv' at (1): value argument
Thank you, everyone, for your assistance in resolving this issue.