m_mpout.F90 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. !~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2. ! NASA/GSFC, Data Assimilation Office, Code 910.3, GEOS/DAS !
  3. !-----------------------------------------------------------------------
  4. ! CVS m_mpout.F90,v 1.6 2007-01-02 23:00:42 jacob Exp
  5. ! CVS MCT_2_8_0
  6. !-----------------------------------------------------------------------
  7. !BOP
  8. !
  9. ! !MODULE: m_mpout - a multiple but mergable parallel output module
  10. !
  11. ! !DESCRIPTION:
  12. !
  13. ! !INTERFACE:
  14. module m_mpout
  15. use m_stdio, only : stdout,LEN_FILENAME
  16. implicit none
  17. private ! except
  18. public :: mpout ! The file handle as a Fortran logical unit
  19. public :: mpout_open ! open the multiple output streams
  20. public :: mpout_close ! close the multiple output streams
  21. public :: mpout_sync ! sync. the multiple output streams
  22. public :: mpout_flush ! flush the multople output streams
  23. public :: mpout_ison ! verify if mpout is proper defined
  24. public :: mpout_log ! write a message to mpout
  25. interface mpout_open; module procedure open_; end interface
  26. interface mpout_close; module procedure close_; end interface
  27. interface mpout_sync; module procedure sync_; end interface
  28. interface mpout_flush; module procedure flush_; end interface
  29. interface mpout_ison; module procedure ison_; end interface
  30. interface mpout_log
  31. module procedure log1_
  32. module procedure log2_
  33. end interface
  34. ! !REVISION HISTORY:
  35. ! 25Feb98 - Jing Guo <guo@thunder> - initial prototype/prolog/code
  36. ! 28Sep99 - Jing Guo <guo@thunder>
  37. ! - Added additional calls to support the "Violet" system
  38. ! development.
  39. !
  40. ! !DESIGN ISSUES:
  41. ! \begin{itemize}
  42. !
  43. ! \item It might be considered useful to implement this module to be
  44. ! applicable to a given {\sl communicator}. The argument
  45. ! taken now is to only have one multiple output stream handle
  46. ! per excution. This is consistent with \verb"stdout" in the
  47. ! traditional sense. (Jing Guo, 25Feb98)
  48. !
  49. ! \item \verb"mpout_log()" is implemented in a way producing output
  50. ! only if \verb"mpout_ison()" (being \verb".true."). The reason
  51. ! of not implementing a default output such as \verb"stdout", is
  52. ! hoping to provent too many unexpected output when the system is
  53. ! switched to a multiple PE system. The design principle for
  54. ! this module is that \verb"mpout" is basically {\sl not} the same
  55. ! module as \verb"stdout". (Jing Guo, 28Sep99)
  56. !
  57. ! \end{itemize}
  58. !EOP
  59. !_______________________________________________________________________
  60. character(len=*),parameter :: myname='MCT(MPEU)::m_mpout'
  61. character(len=*),parameter :: def_pfix='mpout'
  62. integer,save :: isec=-1
  63. integer,save :: mpout=stdout
  64. logical,save :: mpout_set=.false.
  65. character(len=LEN_FILENAME-4),save :: upfix=def_pfix
  66. integer,parameter :: mpout_MASK=3 ! every four PEs
  67. contains
  68. !~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  69. ! NASA/GSFC, Data Assimilation Office, Code 910.3, GEOS/DAS !
  70. !-----------------------------------------------------------------------
  71. !BOP
  72. !
  73. ! !IROUTINE: open_ - open a multiple files with the same name prefix
  74. !
  75. ! !DESCRIPTION:
  76. !
  77. ! !INTERFACE:
  78. subroutine open_(mask,pfix)
  79. use m_stdio, only : stderr,stdout
  80. use m_ioutil, only : luavail,opntext
  81. use m_dropdead, only : die
  82. use m_mpif90, only : MP_comm_WORLD
  83. use m_mpif90, only : MP_comm_rank
  84. use m_mpif90, only : MP_perr
  85. implicit none
  86. integer,optional,intent(in) :: mask
  87. character(len=*),optional,intent(in) :: pfix
  88. ! !EXAMPLES:
  89. !
  90. ! Examples of using mpout_MASK or mask:
  91. !
  92. ! If the mask has all "1" in every bit, there will be no output
  93. ! on every PE, except the PE of rank 0.
  94. !
  95. ! If the mask is 3 or "11"b, any PE of rank with any "dirty" bit
  96. ! in its rank value will not have output.
  97. !
  98. ! !REVISION HISTORY:
  99. ! 25Feb98 - Jing Guo <guo@thunder> - initial prototype/prolog/code
  100. !EOP
  101. !_______________________________________________________________________
  102. character(len=*),parameter :: myname_=myname//'::open_'
  103. integer :: lu
  104. character(len=4) :: sfix
  105. integer :: irank
  106. integer :: ier
  107. integer :: umask
  108. ! Set the filename prefix
  109. upfix=def_pfix
  110. if(present(pfix)) upfix=pfix
  111. ! Set the mask of the PEs with mpout
  112. umask=mpout_MASK
  113. if(present(mask)) umask=mask
  114. ! If a check is not in place, sent the outputs to stdout
  115. mpout=stdout
  116. mpout_set=.false.
  117. call MP_comm_rank(MP_comm_world,irank,ier)
  118. if(ier /= 0) then
  119. call MP_perr(myname_,'MP_comm_rank()',ier)
  120. call die(myname_)
  121. endif
  122. if(iand(irank,umask) == 0) then
  123. lu=luavail()
  124. if(lu > 0) mpout=lu
  125. write(sfix,'(a,z3.3)') '.',irank
  126. call opntext(mpout,trim(upfix)//sfix,'unknown',ier)
  127. if(ier /= 0) then
  128. write(stderr,'(4a,i4)') myname_, &
  129. ': opntext("',trim(upfix)//sfix,'") error, ier =',ier
  130. call die(myname_)
  131. endif
  132. mpout_set=.true.
  133. isec=0
  134. write(mpout,'(a,z8.8,2a)') '.BEGIN. ',isec,' ',trim(upfix)
  135. endif
  136. end subroutine open_
  137. !~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  138. ! NASA/GSFC, Data Assimilation Office, Code 910.3, GEOS/DAS !
  139. !-----------------------------------------------------------------------
  140. !BOP
  141. !
  142. ! !IROUTINE: close_ - close the unit opened by open_
  143. !
  144. ! !DESCRIPTION:
  145. !
  146. ! !INTERFACE:
  147. subroutine close_()
  148. use m_stdio, only : stderr
  149. use m_ioutil, only : clstext, luflush
  150. use m_dropdead, only : die
  151. implicit none
  152. ! !REVISION HISTORY:
  153. ! 25Feb98 - Jing Guo <guo@thunder> - initial prototype/prolog/code
  154. !EOP
  155. !_______________________________________________________________________
  156. character(len=*),parameter :: myname_=myname//'::close_'
  157. integer :: ier
  158. if(mpout_set) then
  159. call luflush(mpout)
  160. isec=isec+1
  161. write(mpout,'(a,z8.8,2a)') '.END. ',isec,' ',trim(upfix)
  162. endfile(mpout)
  163. call clstext(mpout,ier)
  164. if(ier /= 0) then
  165. write(stderr,'(2a,i3.3,a,i4)') myname_, &
  166. ': clstext("',mpout,'") error, ier =',ier
  167. call die(myname_)
  168. endif
  169. mpout=stdout
  170. mpout_set=.false.
  171. endif
  172. isec=-1
  173. end subroutine close_
  174. !~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  175. ! NASA/GSFC, Data Assimilation Office, Code 910.3, GEOS/DAS !
  176. !-----------------------------------------------------------------------
  177. !BOP
  178. !
  179. ! !IROUTINE: sync_ - write a mark for posible later file merging
  180. !
  181. ! !DESCRIPTION:
  182. !
  183. ! !INTERFACE:
  184. subroutine sync_(tag)
  185. use m_stdio, only : stderr
  186. use m_dropdead, only : die
  187. implicit none
  188. character(len=*),intent(in) :: tag
  189. ! !REVISION HISTORY:
  190. ! 25Feb98 - Jing Guo <guo@thunder> - initial prototype/prolog/code
  191. !
  192. ! !DESIGN ISSUES:
  193. ! \begin{itemize}
  194. !
  195. ! \item Should the variable \verb"tag" be implemented as an optional
  196. ! argument? Because the current implementation does not require
  197. ! actual synchronization between all threads of the multiple
  198. ! output streams, forcing the user to supply a unique \verb"tag"
  199. ! would make the final multi-stream merging verifiable. However,
  200. ! since the \verb"tag"s have not been forced to be unique, the
  201. ! synchronization operations are still symbolic.
  202. !
  203. ! \{itemize}
  204. !EOP
  205. !_______________________________________________________________________
  206. character(len=*),parameter :: myname_=myname//'::sync_'
  207. if(mpout_set) then
  208. isec=isec+1
  209. write(mpout,'(a,z8.8,2a)') '.SYNC. ',isec,' ',trim(tag)
  210. endif
  211. end subroutine sync_
  212. !~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  213. ! NASA/GSFC, Data Assimilation Office, Code 910.3, GEOS/DAS !
  214. !-----------------------------------------------------------------------
  215. !BOP
  216. !
  217. ! !IROUTINE: flush_ - flush the multiple output streams
  218. !
  219. ! !DESCRIPTION:
  220. !
  221. ! !INTERFACE:
  222. subroutine flush_()
  223. use m_stdio, only : stderr
  224. use m_ioutil, only : luflush
  225. use m_dropdead, only : die
  226. implicit none
  227. ! !REVISION HISTORY:
  228. ! 27Feb98 - Jing Guo <guo@thunder> - initial prototype/prolog/code
  229. !EOP
  230. !_______________________________________________________________________
  231. character(len=*),parameter :: myname_=myname//'::flush_'
  232. if(mpout_set) call luflush(mpout)
  233. end subroutine flush_
  234. !~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  235. ! NASA/GSFC, Data Assimilation Office, Code 910.3, GEOS/DAS !
  236. !BOP -------------------------------------------------------------------
  237. !
  238. ! !IROUTINE: ison_ - decide if the current PE has a defined mpout
  239. !
  240. ! !DESCRIPTION:
  241. !
  242. ! It needs to be checked to avoid undesired output.
  243. !
  244. ! !INTERFACE:
  245. function ison_()
  246. implicit none
  247. logical :: ison_
  248. ! !REVISION HISTORY:
  249. ! 14Sep99 - Jing Guo <guo@dao.gsfc.nasa.gov>
  250. ! - initial prototype/prolog/code
  251. !EOP ___________________________________________________________________
  252. character(len=*),parameter :: myname_=myname//'::ison_'
  253. ison_=mpout_set
  254. end function ison_
  255. !~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  256. ! ANL/MCS Mathematics and Computer Science Division !
  257. !BOP -------------------------------------------------------------------
  258. !
  259. ! !IROUTINE: log1_ - write a message to mpout
  260. !
  261. ! !DESCRIPTION:
  262. !
  263. ! !INTERFACE:
  264. subroutine log1_(message)
  265. implicit none
  266. character(len=*),intent(in) :: message
  267. ! !REVISION HISTORY:
  268. ! 07Jan02 - R. Jacob (jacob@mcs.anl.gov)
  269. ! - based on log2_.
  270. !EOP ___________________________________________________________________
  271. character(len=*),parameter :: myname_=myname//'::log1_'
  272. if(mpout_set) write(mpout,'(3a)') message
  273. end subroutine log1_
  274. !~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  275. ! NASA/GSFC, Data Assimilation Office, Code 910.3, GEOS/DAS !
  276. !BOP -------------------------------------------------------------------
  277. !
  278. ! !IROUTINE: log2_ - write a message to mpout with a where
  279. !
  280. ! !DESCRIPTION:
  281. !
  282. ! !INTERFACE:
  283. subroutine log2_(where,message)
  284. implicit none
  285. character(len=*),intent(in) :: where
  286. character(len=*),intent(in) :: message
  287. ! !REVISION HISTORY:
  288. ! 14Sep99 - Jing Guo <guo@dao.gsfc.nasa.gov>
  289. ! - initial prototype/prolog/code
  290. ! 07Jan02 - R. Jacob (jacob@mcs.anl.gov)
  291. ! - change name to log2_
  292. !EOP ___________________________________________________________________
  293. character(len=*),parameter :: myname_=myname//'::log2_'
  294. if(mpout_set) write(mpout,'(3a)') where,': ',message
  295. end subroutine log2_
  296. end module m_mpout
  297. !.