Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
28456 Discussions

How to declare `interface` block for MKL functions?

A_K_
Beginner
365 Views

Hi,
The following code was successfully compiled but returned wrong result:

program main
   use, intrinsic:: iso_fortran_env, only: INPUT_UNIT, OUTPUT_UNIT, ERROR_UNIT

   implicit none

   interface ! returns wrong result
      subroutine sscal(n, alpha, x, incx)
         Integer, intent(in):: n
         Real, intent(in):: alpha
         Real, intent(inout):: x(:)
         Integer, intent(in):: incx
      end subroutine sscal
   end interface
   ! external sscal ! returns expected result

   Real:: xs(2) = [1, 2]

   write(OUTPUT_UNIT, *) xs
   call sscal(2, 3.0, xs, 1)
   write(OUTPUT_UNIT, *) xs

   stop
end program main

 

   1.000000       2.000000
   1.000000       2.000000 


If I use `external` statement instead of the `interface` block, it works as expected:
 

   1.000000       2.000000
   3.000000       6.000000


Would you tell me how to make `interface` block version work correctly?
I use `ifort (IFORT) 13.1.0 20130121`.

 

0 Kudos
1 Solution
mecej4
Honored Contributor III
365 Views

Giving the wrong interface to the compiler could be seen as a mild act of sabotage, and the consequences are usually disastrous when the actual interface is such as to make the implicit interface work by itself.

In the interface block, you specified "x(:)" when you should have specified "x(*)".

To avoid such errors in your future work using MKL/BLAS/LAPACK, use this suggestion. Create a file, say mkl_blasi.f, containing the following three lines:

      module mkl_blas
      include 'mkl_blas.fi'
      end module mkl_blas

Compile this file to produce the module file. In your programs, add a "USE MKL_BLAS" statement in the proper places, instead of writing your own interface blocks. You can use ONLY clauses if you like. In this way, you will obtain the correct interfaces as written by Intel and delivered with MKL, and you will avoid the slow compilations that would occur if, instead, you placed "INCLUDE 'mkl.blas.fi'" in several places in your sources..

View solution in original post

0 Kudos
3 Replies
mecej4
Honored Contributor III
366 Views

Giving the wrong interface to the compiler could be seen as a mild act of sabotage, and the consequences are usually disastrous when the actual interface is such as to make the implicit interface work by itself.

In the interface block, you specified "x(:)" when you should have specified "x(*)".

To avoid such errors in your future work using MKL/BLAS/LAPACK, use this suggestion. Create a file, say mkl_blasi.f, containing the following three lines:

      module mkl_blas
      include 'mkl_blas.fi'
      end module mkl_blas

Compile this file to produce the module file. In your programs, add a "USE MKL_BLAS" statement in the proper places, instead of writing your own interface blocks. You can use ONLY clauses if you like. In this way, you will obtain the correct interfaces as written by Intel and delivered with MKL, and you will avoid the slow compilations that would occur if, instead, you placed "INCLUDE 'mkl.blas.fi'" in several places in your sources..

0 Kudos
TimP
Honored Contributor III
365 Views

As far as I could see with a brief search, proposals to add equivalent facilities to netlib BLAS haven't gone anywhere, so the best reply award given to mecej4's answer is well merited.

0 Kudos
A_K_
Beginner
365 Views

Thank you!
Your answer solved the problem.

0 Kudos
Reply