gpt4 book ai didi

linux - 在Linux中使用Matlab MEX时无法使用OpenMP加速

转载 作者:行者123 更新时间:2023-12-03 09:55:20 26 4
gpt4 key购买 nike

我正在使用OpenMP来加速Matlab MEX文件中的Fortran代码。但是,我发现OpenMP似乎不适用于Linux,但实际上适用于Windows。我将代码附加如下:

1)Matlab Mex文件:

clc; clear all; close all;   tic

FLAG_SYS = 0; % 0 for Windows; 1 for Linux

%--------------------------------------------------------------------------
% Mex Fortran code
%--------------------------------------------------------------------------
if FLAG_SYS == 0
mex COMPFLAGS="-Qopenmp $COMPFLAGS"...
LINKFLAGS="/Qopenmp $LINKFLAGS"...
OPTIMFLAGS="/Qopenmp $OPTIMFLAGS"...
'-IC:\Program Files (x86)\IntelSWTools\compilers_and_libraries_2017.5.267\windows\mkl\include'...
'-LC:\Program Files (x86)\IntelSWTools\compilers_and_libraries_2017.5.267\windows\mkl\lib\intel64'...
-lmkl_intel_ilp64.lib -lmkl_intel_thread.lib -lmkl_core.lib libiomp5md.lib...
Test_OpenMP_Mex.f90...
-output Test_OpenMP_Mex

elseif FLAG_SYS == 1
mex COMPFLAGS="-fopenmp $COMPFLAGS"...
LINKFLAGS="-fopenmp $LINKFLAGS"...
FFLAGS='$FFLAGS -fdec-math -cpp' ...
'-I${MKLROOT}/include'...
'-L${MKLROOT}/lib'...
-lmkl_avx2 -lmkl_gf_ilp64 -lmkl_core -lmkl_intel_thread -liomp5 -lpthread -lm -ldl...
Test_OpenMP_Mex.f90...
-output Test_OpenMP_Mex
end

Test_OpenMP_Mex;

2)Fortran代码
#include "fintrf.h"

!GATEWAY ROUTINE
SUBROUTINE MEXFUNCTION(NLHS, PLHS, NRHS, PRHS)

!DECLARATIONS
IMPLICIT NONE

!MEXFUNCTION ARGUMENTS:
MWPOINTER PLHS(*), PRHS(*)
INTEGER NLHS, NRHS

!FUNCTION DECLARATIONS:
MWPOINTER MXCREATEDOUBLEMATRIX

MWPOINTER MXGETM, MXGETN
INTEGER MXISNUMERIC

!POINTERS TO INPUT MXARRAYS:
MWPOINTER MIV1, MIV2

!POINTERS TO OUTPUT MXARRAYS:
MWPOINTER MOV1, MOV2

!CALL FORTRAN CODE
CALL TEST_OPENMP


RETURN

END

!-----------------------------------------------------------------------
SUBROUTINE TEST_OPENMP

USE OMP_LIB

IMPLICIT NONE

INTEGER I, J, K, STEP
REAL*8 STARTTIME, ENDTIME,Y


OPEN(1,FILE='1.TXT')

!COUNT ELAPSED TIME START
STARTTIME = OMP_GET_WTIME()

DO I = 1,1000000
DO J = 1,50000
DO K = 1,1000
Y=(I+10)*J-SQRT(789.1)+SQRT(789.1)-(I+10)*J
END DO
END DO
END DO


ENDTIME = OMP_GET_WTIME()
WRITE(1,*) ENDTIME-STARTTIME

!COUNT ELAPSED TIME START
STARTTIME = OMP_GET_WTIME()

!$OMP PARALLEL
!$OMP DO PRIVATE(I,J)
DO I = 1,1000000
DO J = 1,50000
DO K = 1,1000
Y=(I+10)*J-SQRT(789.1)+SQRT(789.1)-(I+10)*J
END DO
END DO
END DO
!$OMP END DO
!$OMP END PARALLEL

ENDTIME = OMP_GET_WTIME()
WRITE(1,*) ENDTIME-STARTTIME

!$OMP PARALLEL
! GET THE NUMBER OF THREADS
WRITE(1,*) OMP_GET_THREAD_NUM(), OMP_GET_NUM_THREADS()
!$OMP END PARALLEL
CLOSE(1)

RETURN

END SUBROUTINE TEST_OPENMP

Windows上的输出为:
   1.09620520001044     
4.50355500000296
0 6
1 6
3 6
5 6
2 6
4 6

在Linux上的输出是:
   0.0000   
0.0000
0 1

很明显,OpenMP可在Windows上运行,因为计算时间从4.5s减少到1.0s。我发现有6个线程用于计算。但是,在Linux上,似乎没有执行任何计算,并且只有2个线程(Linux上的线程数为36,但仅使用了2个)。

欢迎任何建议!

您可以从此链接直接下载代码:
https://www.dropbox.com/sh/crkuwhu22407sjs/AAAQrtzAvTmFOmAxv_jpTCBaa?dl=0

最佳答案

在Linux(和MacOS)下编译MEX文件时,COMPFLAGS变量将被忽略。它是Windows特定的环境变量。您需要对C使用CFLAGS,对于C++使用CXXFLAGS,对于Fortran使用FFLAGS,对于链接器使用LDFLAGS。这些是控制编译的标准Unix环境变量。

您的编译命令将如下所示:

mex LDFLAGS='-fopenmp $LDFLAGS'...
FFLAGS='-fopenmp -fdec-math -cpp $FFLAGS' ...
'-I${MKLROOT}/include'...
'-L${MKLROOT}/lib'...
-lmkl_avx2 -lmkl_gf_ilp64 -lmkl_core -lmkl_intel_thread -liomp5 -lpthread -lm -ldl...
Test_OpenMP_Mex.f90...
-output Test_OpenMP_Mex

引用:
  • MATLAB documentation
  • GCC environment variables
  • 关于linux - 在Linux中使用Matlab MEX时无法使用OpenMP加速,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62035354/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com