standard output in Fortran MPI code

前端 未结 3 1527
Happy的楠姐
Happy的楠姐 2021-01-19 03:28

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(r         


        
3条回答
  •  礼貌的吻别
    2021-01-19 03:32

    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.

提交回复
热议问题