This is a follow up on :
I am still under windows. The fortran code in mkl_example.f
:
subroutine matmultmkl(M1, M2, M3, M, N, K) bind(c, name='matmultmkl')
!DEC$ ATTRIBUTES DLLEXPORT :: matmultmkl
use iso_c_binding, only: c_float, c_int
integer(c_int),intent(in) :: M, N, K
real(c_float), intent(in) :: M1(M, N), M2(N, K)
real(c_float), intent(out):: M3(M, K)
CALL DGEMM('N','N',M,K,N,1.,M1,M,M2,N,0.,M3,M)
end subroutine
is still the same. I compile it with command line (in a cmd windows where I ran compilervars.bat
before) using the following batch file :
@Echo off
setlocal ENABLEDELAYEDEXPANSION
SET "IFORT_INITIAL_FLAGS=-c -fpp"
SET "IFORT_OPTIMIZATION_FLAGS=/O3"
ifort %IFORT_OPTIMIZATION_FLAGS% %IFORT_INITIAL_FLAGS% /I"C:\Program Files (x86)\IntelSWTools\compilers_and_libraries_2020.4.311\windows\mkl\include" -o mkl_example.obj mkl_example.f
ifort -dll -o mylib.dll mkl_example.obj /link /LIBPATH:"C:\Program Files (x86)\IntelSWTools\compilers_and_libraries_2020.4.311\windows\mkl\lib\intel64_win" mkl_intel_lp64.lib mkl_intel_thread.lib mkl_core.lib libiomp5md.lib
that has not changed either. Then I run the follow python script mkl_example.py
, that I changed to point to the static and import libraries :
import os
os.add_dll_directory(r"C:/Program Files (x86)/IntelSWTools/compilers_and_libraries_2020.4.311/windows/redist/intel64_win/mkl")
os.add_dll_directory(r"C:/Program Files (x86)/IntelSWTools/compilers_and_libraries_2020.4.311/windows/redist/intel64_win/compiler")
import time
from ctypes import *
import signal
import numpy as np
mylib = CDLL(r"C:/path/to/the/mylib.dll")
def main():
mylib.matmultmkl.argtypes = [ POINTER(c_float),
POINTER(c_float),
POINTER(c_float),
POINTER(c_int),
POINTER(c_int),
POINTER(c_int) ]
mylib.matmultmkl.restype = None
M=231
N=231
K=231
a = np.empty((M,N), dtype=c_float)
b = np.empty((N,K), dtype=c_float)
c = np.empty((M,K), dtype=c_float)
a[:] = np.random.rand(M,N)
b[:] = np.random.rand(N,K)
c[:] = np.full((M,K), 0.0)
# Fortran mkl call
start = time.time()
mylib.matmultmkl( a.ctypes.data_as(POINTER(c_float)),
b.ctypes.data_as(POINTER(c_float)),
c.ctypes.data_as(POINTER(c_float)),
c_int(M), c_int(N), c_int(K) )
stop = time.time()
print(f"Fortran mkl \t {stop - start}s")
if __name__ == "__main__":
main()
dgemm
executes a matrix product. Here, I have two square matrices of size 231, which is a totally silly small size for a matrix product today, yet : the call to the fortran function triggers a segmentation fault. And doesn't if the size is 230 or smaller.
Still, I can't figure out why would the 231 size cause issues, so I thought about aliasing but don't see anything particular.
mi = c_int(M)
,ni = c_int(N)
,ki = c_int(K)
, and the last 3 arguments ofmylib.matmultmkl( ... , byref(mi), byref(ni), byref(ki))
? – CristiFati Feb 26 at 15:21