D语言的MPI实现

6
这个问题与这个问题有关:MPI and D: Linker Options 我正在尝试让D语言使用MPI。在网络上可以找到几篇文章,但我发现没有一篇实际有效...因此,这是我迄今为止所做的:
我从这里https://github.com/1100110/OpenMPI/blob/master/mpi.d获取了mpi.d并设置了一个最小化的程序:
import mpi;
import std.stdio;

void* MPI_COMM_WORLD = cast(void*)0;

int main(string[] args)
{

  int rank, size;
  int argc = cast(int)args.length;
  char *** argv = cast(char***)&args;


  MPI_Init (&argc, argv);   /* starts MPI */
  MPI_Comm_rank (MPI_COMM_WORLD, &rank);    /* get current process id */
  MPI_Comm_size (MPI_COMM_WORLD, &size);    /* get number of processes */
  writefln( "Hello world from process %d of %d", rank, size );
  MPI_Finalize();

  return 0;
}

我使用编译器

dmd test_mpi.d -L-L/usr/lib/openmpi -L-lmpi -L-ldl -L-lhwloc

或者

gdc test_mpi.d -pthread -L/usr/lib/openmpi -lmpi -ldl -lhwloc -o test_mpi

并且运行

mpirun -n 2 ./test_mpi

这是我收到的错误信息:

[box:1871] *** An error occurred in MPI_Comm_rank
[box:1871] *** on communicator MPI_COMM_WORLD
[box:1871] *** MPI_ERR_COMM: invalid communicator
[box:1871] *** MPI_ERRORS_ARE_FATAL: your MPI job will now abort
--------------------------------------------------------------------------
mpirun has exited due to process rank 0 with PID 1870 on
node bermuda-iii exiting improperly. There are two reasons this could occur:

1. this process did not call "init" before exiting, but others in
the job did. This can cause a job to hang indefinitely while it waits
for all processes to call "init". By rule, if one process calls "init",
then ALL processes must call "init" prior to termination.

2. this process called "init", but exited without calling "finalize".
By rule, all processes that call "init" MUST call "finalize" prior to
exiting or it will be considered an "abnormal termination"

This may have caused other processes in the application to be
terminated by signals sent by mpirun (as reported here).
--------------------------------------------------------------------------
[box:01869] 1 more process has sent help message help-mpi-errors.txt / mpi_errors_are_fatal
[box:01869] Set MCA parameter "orte_base_help_aggregate" to 0 to see all help / error messages

显然我确实调用了MPI_Init和MPI_Finalize。那么我还需要什么?

1
那么……从我离开它的时候开始,它还能正常工作吗?我接受拉取请求! - 0b1100110
我还在测试中... 到目前为止,这个简单的例子可以工作,但更复杂的东西就不行了,所以我正在努力找出原因... - steffen
如果你觉得很容易,那就拉取代码并让我看看。也许我能够发现问题所在。(如果你觉得不容易,那就不用担心了。我会在某个时候更新它。) - 0b1100110
ping,可能吗?有更新给我吗? - 0b1100110
@0b1100110:我有一段时间没有在这上面工作了,你可能已经注意到了 ;) 不管怎样,我想拉下来看看,看看下午是否可以玩一下。我会给你发送电子邮件。 - steffen
2个回答

8
在Open MPI中,C通信句柄是指向真实通信结构的指针。MPI_COMM_WORLD是一个指向预先创建的全局通信结构的指针,而不是您定义的空指针。这就是为什么Open MPI在调用MPI_COMM_RANK时会中止 - 这相当于在C语言中调用MPI_Comm_rank(NULL,&rank)
如果您查看mpi.d第808行,您会注意到MPI_COMM_WORLD已经被定义为:
MPI_COMM_WORLD      = cast(void*) &(ompi_mpi_comm_world),

如果您删除重新定义MPI_COMM_WORLD的行,那么您的代码应该可以工作。


糟糕!我把它与另一个版本的mpi.d一起编译了,忘记删除它了!谢谢。 - steffen

4
你没有正确地将 string[] 转换为 char***。你应该这样做:
import std.string, std.algorithm, std.array;

char** argv = cast(char**)map!(toStringz)(args).array.ptr;
MPI_Init (&argc, &argv);

它是如何工作的:

  • 在每个args元素上映射toStringz

  • 因为map返回范围,我们使用array来拥有它的数组。

  • 获取数组指针。


缺少一个括号。在 map 之前加上吗? - steffen
好的,谢谢你指出这个问题。但不幸的是,它并没有解决问题... :( - steffen

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接