0

是否可以使用 Matlab (parfor) 中的并行计算工具箱并行运行 MEX 文件?我使用 gfortran 编译器将 Fortran 应用程序编译为 MEX 文件。它可以正常工作:

mass=getMass(x);

但是当我尝试并行运行它时,它会崩溃。费:

matlabpool 4
parfor i=1:80
    mass(i)=getMass(x);
end

我收到以下错误:

使用distcomp.remoteparfor/getCompleteIntervals 时出错(第 22 行) parfor 正在使用的会话已关闭。

原因:使用distcomp.remoteparfor/getCompleteIntervals 时出错(第 22 行) parfor 正在使用的会话已关闭。

客户端失去了与实验 3 的连接。这可能是由于网络问题,或者交互式 matlabpool 作业可能出错。

其他功能(不是 MEX)并行运行没有问题。我在 OS X 10.8.5 上使用 Matlab R2013a。

4

2 回答 2

1

您可以轻松地为每个open. 一定要避免从更多线程写入同一个文件,但从你提供的内容来看,你似乎只是草稿文件,它们应该没问题,它们由单元号区分。

如果您的编译器支持newunitFortran 2008 中的说明符,则非常简单:

integer :: u
open(newunit=u, form=...

write(u,fmt) the_variables

close(u)

它会自动分配一个未使用的单元号,并且不会与任何硬连线单元号发生冲突,因为它是负数。

也可以编写一个函数,使用 找到一些空闲单元inquire,但不能同时从多个线程调用这样的函数。

  subroutine newunit(unit)
     integer,intent(out) :: unit
     logical :: isOpen

     integer, parameter :: MIN_UNIT_NUMBER = 10
     integer, parameter :: MAX_UNIT_NUMBER = 99

     do unit = MIN_UNIT_NUMBER, MAX_UNIT_NUMBER
        inquire(unit = unit, opened = isOpen)
        if (.not. isOpen) then
           return
        end if
     end do

     unit = -1
  end subroutine newunit

存在更高级的版本。

于 2014-05-20T10:44:50.447 回答
0

Your mex function must be thread safe. If parts of it are not thread safe, e.g. IO operations to a single file, writes to shared memory etc., then these parts need to be protected with a mutex.

于 2014-05-20T09:06:11.200 回答