OpenFAST version 3.5.0 is compiled successfully on a M1 Mac but it fails to compile or an invalid FAST_SFunc.mexw64 is generated on Windows 10 #1819
Replies: 5 comments
-
@hyoshimine , thank you for finding the issue with the OpenMP line in The Windows system module for Matlab assumes that the Intel Fortran compiler will be used, which is why the Using the Intel compilers to build OpenFAST and generate the MEX file completed successfully. However, this gave the error There are two solutions:
Please let me know if this resolves the issue for you. I'll see about supporting GCC for building the MEX file on Windows. |
Beta Was this translation helpful? Give feedback.
-
Derek Slaughter, thanks for the two solutions to this issue. I immediately tried these two methods. In method 1, setting the path c: ¥Program Files(x86)¥Intel¥oneAPI¥mkl¥latest¥redist¥intel64¥ to mkl_intel_thread2.dll did not solve this issue on my system. However, I knew that setting the environment by setvars.bat only works in the command window, so I was able to use the alternative method 1 below to resolve the issue. Method 2 solved the issue.
Alternative method
1. After development, type matlab in the command window set up by "c:¥Program Files(x86)¥Intel¥oneAPI¥setvars.bat" or start matlab by typing "c: ¥Program Files(x86)¥Intel¥oneAPI¥setvars.bat" & matlab in a new command window.
Then I tried to modify the source code and CmakeLists.txt for building mex files using gcc on Windows. However, an undefined reference to `mexprintf' occurs when linking. Please tell me how to fix this error.
**OpenFAST Version**
version rc-3.5.1
**To Reproduce**
build
cmake .. -G"MinGW Makefiles" -DOPENMP=ON -DBUILD_TESTING=ON -DCMAKE_Fortran_FLAGS="-DUA_OUTS=ON" -DBUILD_OPENFAST_SIMULINK_API=ON -DCMAKE_Fortran_FLAGS=-fno-underscoring
mingw32-make install
error message
Linking Fortran shared library FAST_SFunc.mexw64
C:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: ../../modules/nwtc-library/libnwtclibs-matlab.a(SysMatlabWinGnu.f90.obj):SysMatlabWinGnu.f90:(.text+0xbbc): undefined reference to `mexprintf'
C:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: ../../modules/nwtc-library/libnwtclibs-matlab.a(SysMatlabWinGnu.f90.obj):SysMatlabWinGnu.f90:(.text+0xd05): undefined reference to `mexprintf'
C:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: ../../modules/nwtc-library/libnwtclibs-matlab.a(SysMatlabWinGnu.f90.obj):SysMatlabWinGnu.f90:(.text+0xe08): undefined reference to `mexprintf'
Modify modules\nwtc-library CmakeLists.txt
24 set(NWTC_SYS_FILE_MATLAB ${CMAKE_CURRENT_SOURCE_DIR}/src/SysMatlabWindows.f90) original
SysMatlabWinGnu.f90 modify
modules\nwtc-library\src
Create SysMatlabWinGnu.f90 from SysMatlabWindows.f90
modify 57
57 CHARACTER(*), PARAMETER :: OS_Desc = 'Intel Visual Fortran for Windows/Matlab' original
'GNU Fortran for Windows/Matlab' modify
delete 69
69 USE IFPORT
modify 73
73 INTEGER :: StatArray(12) original
StatArray(13) modify
delete 93-95
93 #ifndef CONSOLE_FILE
94 USE IFPORT, ONLY: FLUSH
95 #endif
delete 102-104
102 #ifndef CONSOLE_FILE
103 CALL FLUSH ( INT(Unit, B4Ki) )
104 #endif
delete 115
115 USE IFPORT, ONLY: GETCWD
delete 138
138 USE, INTRINSIC :: ieee_arithmetic
modify
143 Is_NaN = IEEE_IS_NAN( DblNum ) original
IsNaN( DblNum ) modify
modify
213 inquire( directory=trim(new_directory_path), exist=directory_exists ) original
file=trim(new_directory_path), exist=directory_exists ) modfy
modify
216 make_command = 'mkdir "'//trim(new_directory_path)//'"' original
make_command = 'mkdir -p '//trim(new_directory_path) modify
delete 226, 232, 235, 237, 239, 240, 242
226 #ifdef CONSOLE_FILE
232 INTEGER :: IOS
235 OPEN ( CU , FILE='CONSOLE.TXT' , STATUS='UNKNOWN', FORM='FORMATTED', IOSTAT=IOS, ACTION="WRITE" )
237 IF ( IOS /= 0 ) THEN
239 CALL ProgExit ( 1 )
240 END IF
242 #endif
delete 281-284
282 #ifdef CONSOLE_FILE
283 CLOSE ( CU )
284 #endif
delete
297 USE, INTRINSIC :: ieee_arithmetic
replace 309-316
delete 309-316
309 NaN_D = ieee_value(0.0_DbKi, ieee_quiet_nan)
310 Inf_D = ieee_value(0.0_DbKi, ieee_positive_inf)
312 NaN = ieee_value(0.0_ReKi, ieee_quiet_nan)
313 Inf = ieee_value(0.0_ReKi, ieee_positive_inf)
315 NaN_S = ieee_value(0.0_SiKi, ieee_quiet_nan)
316 Inf_S = ieee_value(0.0_SiKi, ieee_positive_inf)
addition 17 lines
real(DbKi) :: Neg_D
real(SiKi) :: Neg_S
real(ReKi) :: Neg
#ifndef FPE_TRAP_ENABLED
Neg_D = -1.0_DbKi
Neg_S = -1.0_SiKi
Neg = -1.0_ReKi
NaN_D = SQRT ( Neg_D )
NaN_S = SQRT ( Neg_S )
NaN = SQRT ( Neg )
Neg_D = 0.0_DbKi
Neg_S = 0.0_SiKi
Neg = 0.0_ReKi
Inf_D = 1.0_DbKi / Neg_D
Inf_S = 1.0_SiKi / Neg_S
Inf = 1.0_ReKi / Neg
#endif
addition 325
325 CALL WrNR ( CHAR( 7 ) )
delete 406-407
406 USE IFWINTY, ONLY : HANDLE
407 USE kernel32, ONLY : LoadLibrary
replace 412
delete 412
412 INTEGER(HANDLE) :: FileAddr
addition 9 lines
INTERFACE ! Definitions of Windows API routines
FUNCTION LoadLibrary(lpFileName) BIND(C,NAME='LoadLibraryA')
USE, INTRINSIC :: ISO_C_BINDING
IMPLICIT NONE
!GCC$ ATTRIBUTES STDCALL :: LoadLibrary
INTEGER(C_INTPTR_T) :: LoadLibrary
CHARACTER(KIND=C_CHAR) :: lpFileName(*)
END FUNCTION LoadLibrary
END INTERFACE
replace 418-419
delete 418-419
418 FileAddr = LoadLibrary( TRIM(DLL%FileName)//C_NULL_CHAR )
419 DLL%FileAddr = TRANSFER(FileAddr, DLL%FileAddr)
addition 418
418 DLL%FileAddr = LoadLibrary( TRIM(DLL%FileName)//C_NULL_CHAR )
delete 439-440
439 USE IFWINTY, ONLY : LPVOID
440 USE kernel32, ONLY : GetProcAddress
delete 445
445 INTEGER(LPVOID) :: ProcAddr
addition 9 lines
447 INTERFACE ! Definitions of Windows API routines
FUNCTION GetProcAddress(hModule, lpProcName) BIND(C, NAME='GetProcAddress')
USE, INTRINSIC :: ISO_C_BINDING
IMPLICIT NONE
TYPE(C_FUNPTR) :: GetProcAddress
INTEGER(C_INTPTR_T),VALUE :: hModule
CHARACTER(KIND=C_CHAR) :: lpProcName(*)
END FUNCTION GetProcAddress
END INTERFACE
replace 455,456
delete 455,456
455 ProcAddr = GetProcAddress( DLL%FileAddr, TRIM(DLL%ProcName(i))//C_NULL_CHAR )
456 DLL%ProcAddr(i) = TRANSFER(ProcAddr, DLL%ProcAddr(i))
addition
455 DLL%ProcAddr(i) = GetProcAddress( DLL%FileAddr, TRIM(DLL%ProcName(i))//C_NULL_CHAR )
delete 473,474
473 USE IFWINTY, ONLY : BOOL, HANDLE, FALSE !, LPVOID
474 USE kernel32, ONLY : FreeLibrary
modify 479
479 INTEGER(HANDLE) :: FileAddr original
INTEGER(C_INT), PARAMETER :: FALSE = 0 modify
modify 480
480 INTEGER(BOOL) :: Success original
INTEGER(C_INT) :: Success modify
addition 9 lines
481 INTERFACE ! Definitions of Windows API routines
FUNCTION FreeLibrary(hLibModule) BIND(C, NAME='FreeLibrary')
USE, INTRINSIC :: ISO_C_BINDING
IMPLICIT NONE
!GCC$ ATTRIBUTES STDCALL :: FreeLibrary
INTEGER(C_INT) :: FreeLibrary ! BOOL
INTEGER(C_INTPTR_T),VALUE :: hLibModule ! HMODULE hLibModule
END FUNCTION
END INTERFACE
delete
487 FileAddr = TRANSFER(DLL%FileAddr, FileAddr)
modify 490
490 Success = FreeLibrary( FileAddr ) original
Success = FreeLibrary( DLL%FileAddr ) modify
… 2023/06/30 3:15、Derek Slaughter ***@***.***>のメール:
@hyoshimine <https://github.com/hyoshimine> , thank you for finding the issue with the OpenMP line in CMakeLists.txt, this has been corrected in the rc-3.5.1 branch. Using CMake to generate the MEX file for Simulink is a new feature which has not been sufficiently tested on Windows. Windows users typically build with the Visual Studio project instead of with CMake. However, I was able to reproduce this issue and get it working.
The Windows system module for Matlab assumes that the Intel Fortran compiler will be used, which is why the IFPORT module was not found when using GCC. I will investigate the possibility of using GCC with Matlab on Windows since this is currently possible for OpenFAST on Linux and Mac.
Using the Intel compilers to build OpenFAST and generate the MEX file completed successfully. However, this gave the error The specified module could not be found.' when trying to use the MEX file. It turns out that the MEX file was produced correctly and this error refers to a shared library which the MEX file is trying to load when starting the simulation. For my setup, the missing library turned out to be the mkl_intel_thread.2.dllwhich was not on my path. It gets added to the path bysetvars.bat` during development, but when Matlab is run, it can't find the file.
There are two solutions:
Add this file to your path (it was located at C:\Program Files (x86)\Intel\oneAPI\mkl\latest\redist\intel64\mkl_intel_thread.2.dll on my system)
Statically link the MKL into the MEX file by adding -DBLA_STATIC=ON when running the CMake configure step (I tested this and it worked on my system)
Please let me know if this resolves the issue for you. I'll see about supporting GCC for building the MEX file on Windows.
—
Reply to this email directly, view it on GitHub <#1583 (comment)>, or unsubscribe <https://github.com/notifications/unsubscribe-auth/A7NYYLMD7UGEMAX6RFSDQFLXNXA5XANCNFSM6AAAAAAYJHF7BE>.
You are receiving this because you were mentioned.
|
Beta Was this translation helpful? Give feedback.
-
@hyoshimine , I got it working in the branch at https://github.com/deslaughter/openfast/tree/f/matlab-gcc-win, however, using MinGW to build the MEX file directly does not seem to be well supported by Matlab. I had to use the version MinGW compatible with Matlab2023b according to the table at https://www.mathworks.com/matlabcentral/fileexchange/52848-matlab-support-for-mingw-w64-c-c-compiler. As for fixing the interface
function mexPrintf(message) bind(C, NAME='mexPrintf')
use, intrinsic :: ISO_C_BINDING
!GCC$ ATTRIBUTES STDCALL :: mexPrintf
character(KIND=C_CHAR) :: message(*)
end function
end interface |
Beta Was this translation helpful? Give feedback.
-
Derek Slaughter, thanks for your answer about GCC for building mex files on windows.
I cloned form"https://github.com/deslaughter/openfast/tree/f/matlab-gcc-win" and I confiromed it working.
and I modified SysMatlabWinGnu.f90 in module/nwtc-library/src directory, CMakeLists.txt in module/nwtc-library directory and CMakeLists in glue-codes/simulink directory according with your advice and I confiromed it working.
However nwtc_library_utest of regression test was failed. What is a cause of this error?
93/96 Testing: nwtc_library_utest
93/96 Test: nwtc_library_utest
Command: "C:/openfast/build/unit_tests/nwtc-library/nwtc_library_utest.exe"
Directory: C:/openfast/build/unit_tests/nwtc-library
"nwtc_library_utest" start time: Jul 21 11:35 東京 (標準時)
Output:
----------------------------------------------------------
.....At line 20 of file C:\openfast\build\unit_tests\tests\nwtc-library\NWTC_Library_test_tools.F90 (unit = 6)
Fortran runtime error: Cannot open file '/dev/null': No such file or directory
nwtc-library/CMakeLists.txt
24 set(NWTX_SYS_FILE_MATLAB ${CMAKE_CURRENT_SOURCE_DIR} /src/SysMatlabWindows.f90)
change SysMatlabWinGnu.f90
glue-codes/simulink/CMakeLists.txt
add
78 if(WIN32 AND (${CMAKE_Fortran_COMPILER_ID} STREQUAL “GNU”))
79 target_link_options(FAST_SFunc PRIVATE “wl, --export-all-symbols”)
80 endif()
modify from SysMatlabWindows.f90 to SysMatlabWinGnu.f90
[report-modifyfromSysMatlabWindowstoSysMatlabWinGnu.pdf](https://github.com/OpenFAST/openfast/files/12259235/report-modifyfromSysMatlabWindowstoSysMatlabWinGnu.pdf)
[report-modifyfromSysMatlabWindowstoSysMatlabWinGnu2.pdf](https://github.com/OpenFAST/openfast/files/12259246/report-modifyfromSysMatlabWindowstoSysMatlabWinGnu2.pdf)
|
Beta Was this translation helpful? Give feedback.
-
@hyoshimine I'm converting this issue to a discussion as we don't currently have the ability to support building the Simulink module with GCC on Windows. MATLAB doesn't recommend using GCC on Windows and the current solution with the Intel compiler works for most users. I'll leave this open in case this becomes a feature that many people request. |
Beta Was this translation helpful? Give feedback.
-
Bug description
(1) First of all, as a referece to #1308, I compiled an OpenFAST version 3.5.0 with a MacOS-arm64 compiler (gcc version 12.2.0) and an arm native version of MatLab (Matlab_R2022b_Beta) on a M1 Mac, in order to run simulation using Simulink and OpenFAST. The compilation was successful. I also tested Run_Openloop.m then Simulink called OpenFAST subroutines and received resulting data from OpenFAST.
(2) Second, I tried to compile an OpenFAST version 3.5.0 with a Windows-intel64 compiler (gcc version 12.2.0) and an intel version of Malab (Malab_R2023a) on a windows 10. The compilation failed.
Error message was
c:\openfast\modules\nwtc-library\src\SysMatlabWindows.f90:69:5
69 USE IFPORT
Fatal Error : Cannot open module file 'ifport.mod' for reading at (1) :
No such file or diretory
compilation terminated.
I think that IFPORT library is not a gfortran library so it could not open module file 'ifport.mod'.
(3) Third, I changed the compiler from gcc to Intel Fortran and C, C++ Compiler version 2023.1.0. The compilation was finished. When Run_Openloop.m testing, Simulink outputs error message.
Error while obtaining sizes from Mex s-function 'FAST_SFunc in OpenLoop/FAST Nonlinear Wind Turbine/s-function' Caused by : Invalid MEX-file 'c:\openfast\install\bin\FAST_SFunc.mexw64 : The specified module could not be found.
(4) About mistakes of CMakeLists.txt in OpenFAST directory
OPENMP could not enable so I checked a CMakeLists.txt.
I found a mistake on line 113 of CMakeLists.txt in OpenFAST directory.
Mistake is
113 set(CMAKE_FORTRAN_FLAGS "${CMAKE_FORTRAN_FLAGS} ${OpenMP_FORTRAN_FLAGS}")
Correct is
113 set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} ${OpenMP_Fortran_FLAGS}")
To Reproduce
(1)
% FAST_InputFileName = '../../../build/reg_tests/glue-codes/openfast/5MW_Land_DLL_WTurb/5MW_Land_DLL_WTurb.fst'
% TMax=60
System Information
(2)
System Information
(3)
% FAST_InputFileName = '..¥..¥..¥build¥reg_tests¥glue-codes¥openfast¥5MW_Land_DLL_WTurb¥5MW_Land_DLL_WTurb.fst'
% TMax=60
System Information
OpenFAST Version
version 3.5.0
Beta Was this translation helpful? Give feedback.
All reactions