本文介绍了在Matlab mex中使用DGESV时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在mex文件中解决DGESV的线性系统问题。
当我有一个2x2系统时,mex文件工作正常,没有错误发生,但是当系统大于2时,MATLAB系统错误对话框出现,并且说matlab遇到内部问题并需要关闭。
在64位Windows 10和Intel Composer XE 2013上使用matlab r2016a

I'm trying to solve a linear system with DGESV in a mex file.When I have a 2x2 system, the mex file works fine and no errors occurred, but when the system is larger than 2, MATLAB System Error dialog box apperas and says that matlab has encountered an internal problem and needs to be closed.Im using matlab r2016a on 64-bit windows 10 and intel composer XE 2013

编译界面是:

The compile line is:

mex -lmwlapack *.F

如下所示:

The code is as follows:

     #include "fintrf.h"

C     Gateway subroutine
      subroutine mexfunction(nlhs, plhs, nrhs, prhs)

C     Declarations
      implicit none

C     mexFunction arguments:
      mwPointer plhs(*), prhs(*)
      integer nlhs, nrhs

C     Function declarations:
      mwPointer mxGetPr
      mwPointer mxCreateDoubleMatrix
      mwPointer mxGetM

C     Pointers to input/output mxArrays:
      mwPointer pr_A, pr_B, pr_C

C     Array information:

      mwPointer sizea 
      real*8 , allocatable :: A(:,:)
     +        ,B(:,:),C(:,:)

C     Get the size of the input array.
      sizea = mxGetM(prhs(1))

        allocate(A(sizea,sizea),B(sizea,1))
        allocate(C(sizea,1))

C     Create Fortran array from the input argument.
      pr_A = mxGetPr(prhs(1))
      pr_B = mxGetPr(prhs(2))

      call mxCopyPtrToReal8(pr_A,A,sizea**2)
      call mxCopyPtrToReal8(pr_B,B,sizea)

C     Create matrix for the return argument.
      plhs(1) = mxCreateDoubleMatrix(sizea, 1, 0)
      pr_C = mxGetPr(plhs(1))

C     Call the computational routine.
      Call SolveLS(A,B,C,sizea)

      call mxCopyReal8ToPtr(C,pr_C,sizea)

      return
      end

C     Computational routine
      subroutine SolveLS(A,B,C,sizea)

      integer*4 :: sizea,pivot(sizea),info
      real*8 :: A(sizea,sizea),B(sizea,1), C(sizea,1)

      call DGESV(sizea, 1,A,sizea,pivot,B,sizea,info)
      C=B
      return
      end subroutine SolveLS


推荐答案

像这样的系统错误通常表明你已经损坏了存储器分配不当的内存。我注意到你正在使用标准Fortran分配函数而不是mxMalloc,它允许MATLAB处理内存分配和释放。请注意,mxMalloc的内存会在MEX函数调用结束时自动销毁,但您可以使用mxFree将其释放。

A system error like this normally indicates that you've corrupted the memory with a bad memory allocation. I notice you are using the standard Fortran allocate function instead of mxMalloc which allows MATLAB to handle the memory allocation and deallocation. Note that mxMalloc'ed memory is automatically destroyed at the end of the MEX function call, but you can use mxFree to free it up.

关于mxMalloc的信息可以在

Information on mxMalloc can be found in the Matlab help files

这篇关于在Matlab mex中使用DGESV时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 04:00