Fortran MPI代码中的标准输出 [英] standard output in Fortran MPI code

查看:224
本文介绍了Fortran MPI代码中的标准输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个并行Fortran代码,我只希望rank = 0进程能够写入stdout,但我不想用以下代码抛弃代码:

  if(rank == 0)write(*,*)... 

所以我想知道如果做下面这样的事情会是一个好主意,还是有更好的办法?

 程序测试

使用mpi

隐式无

整数:: ierr
整数:: nproc
integer :: rank

integer :: stdout

call mpi_init(ierr)
call mpi_comm_rank(mpi_comm_world,rank,ierr)
调用mpi_comm_size(mpi_comm_world,nproc,ierr)

select case(rank)
case(0)
stdout = 6
case default
stdout = 7
open(unit = stdout,file ='/ dev / null')
end select

write(stdout,*)from rank =你好,等级

调用mpi_finalize(ierr)

结束程序测试
  $ mpirun  -   

n 10 ./a.out
您好等级= 0

感谢您的任何建议!

解决方案

您的解决方案有两个缺点:


  1. 这个聪明的解决方案实际上掩盖了代码,因为它存在于:stdout不再是标准输出。如果有人阅读代码,他/她会认为所有进程都写入标准输出,而实际上他们不是。

  2. 如果您希望所有进程在某个时刻写入标准输出,那么你会怎么做?添加更多技巧?

如果你确实想坚持这个技巧,请不要使用stdout作为变量单位数量,但例如主或任何表明你没有真正写入标准输出的东西。此外,你应该知道数字 6 并不总是stdout。 Fortran 2003允许你检查stdout的单元号,所以你应该使用它。



我的建议是继续使用 if(rank == 0)语句。他们清楚地表明代码中发生了什么。如果您使用大量类似的I / O语句,则可以编写仅用于等级0或所有进程的子例程。这些可以有意义的名称,表明预期的用法。


I have a parallel fortran code in which I want only the rank=0 process to be able to write to stdout, but I don't want to have to litter the code with:

if(rank==0) write(*,*) ...

so I was wondering if doing something like the following would be a good idea, or whether there is a better way?

program test

  use mpi

  implicit none

  integer :: ierr
  integer :: nproc
  integer :: rank

  integer :: stdout

  call mpi_init(ierr)
  call mpi_comm_rank(mpi_comm_world, rank, ierr)
  call mpi_comm_size(mpi_comm_world, nproc, ierr)

  select case(rank)
  case(0)
     stdout = 6
  case default
     stdout = 7
     open(unit=stdout, file='/dev/null')
  end select

  write(stdout,*) "Hello from rank=", rank

  call mpi_finalize(ierr)

end program test

This gives:

$ mpirun -n 10 ./a.out
Hello from rank=           0

Thanks for any advice!

解决方案

There are two disadvantages to your solution:

  1. This "clever" solution actually obscures the code, since it lies: stdout isn't stdout any more. If someone reads the code he/she will think that all processes are writing to stdout, while in reality they aren't.
  2. If you want all processes to write to stdout at some point, what will you do then? Add more tricks?

If you really want to stick with this trick, please don't use "stdout" as a variable for the unit number, but e.g. "master" or anything that indicates you're not actually writing to stdout. Furthermore, you should be aware that the number 6 isn't always stdout. Fortran 2003 allows you to check the unit number of stdout, so you should use that if you can.

My advice would be to stay with the if(rank==0) statements. They are clearly indicating what happens in the code. If you use lots of similar i/o statements, you could write subroutines for writing only for rank 0 or for all processes. These can have meaningful names that indicate the intended usage.

这篇关于Fortran MPI代码中的标准输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆