How to declare `interface` block for MKL functions?

How to declare `interface` block for MKL functions?

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`.

 

4 posts / 0 nouveau(x)
Dernière contribution
Reportez-vous à notre Notice d'optimisation pour plus d'informations sur les choix et l'optimisation des performances dans les produits logiciels Intel.
Portrait de mecej4
Best Reply

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..

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.

Thank you!
Your answer solved the problem.

Connectez-vous pour laisser un commentaire.