gtool5 Fortran 90/95 ライブラリ 1.0.0-rc5
English
Loading...
Searching...
No Matches
dc_message.f90
Go to the documentation of this file.
1! -*- mode: f90; coding: utf-8 -*-
2!-----------------------------------------------------------------------
3! Copyright (c) 2000-2026 Gtool Development Group. All rights reserved.
4!-----------------------------------------------------------------------
5!>
6!> @author Yasuhiro MORIKAWA, Masatsugu ODAKA
7!> @copyright Copyright (C) GFD Dennou Club, 2000-2026. All rights reserved. <br/>
8!> License is BSD-2-Clause. see [COPYRIGHT](@ref COPYRIGHT) in detail
9!>
10!> @en
11!> @brief Message output module
12!> @details
13!> This module provides subroutines for outputting messages.
14!> It is intended to be used as a higher-level interface to
15!> dcl's MSGDMP.f (http://www.gfd-dennou.org/arch/dcl/dcl-f77doc/rc1/math1/node26.html).
16!> Currently, the output device is fixed to standard output.
17!>
18!> @section dc_message_output Output Form
19!>
20!> Messages are output in the following formats by subroutines of this module:
21!>
22!> @code
23!> *** MESSAGE [where] *** message
24!>
25!> *** WARNING [where] *** message
26!>
27!> *** ERROR (Code number) [where] *** message
28!> @endcode
29!>
30!> @enden
31!>
32!> @ja
33!> @brief メッセージの出力
34!> @details
35!> メッセージの出力を行うためのサブルーチン群を持つモジュールです。
36!> dcl の MSGDMP.f (http://www.gfd-dennou.org/arch/dcl/dcl-f77doc/rc1/math1/node26.html)
37!> の上位互換としても利用することを想定しています。
38!> 現在、出力装置は標準出力に固定されています。
39!>
40!> @section dc_message_output_ja 出力形式
41!>
42!> 本モジュールのサブルーチンによって以下のような形式のメッセージ
43!> が出力されます。
44!>
45!> @code
46!> *** MESSAGE [where] *** message
47!>
48!> *** WARNING [where] *** message
49!>
50!> *** ERROR (Code number) [where] *** message
51!> @endcode
52!>
53!> @endja
54!>
56 ! MPI ライブラリ
57 ! MPI library
58 !
59 use mpi
60 implicit none
61 private
62 public:: messagenotify
63 public:: messagesuppressmpi
64 interface messagenotify
65 module procedure messagenotifyc
66 module procedure messagenotifyi
67 end interface
69 module procedure messagesuppressmpi0
70 end interface
71 integer, save:: output_rank = -1
72 integer, save:: save_myrank = -1
73contains
74 !> @en
75 !> @brief Output message or terminate with error
76 !> @details
77 !> Use this subroutine when you want to output a message to standard output.
78 !>
79 !> Give the character variable `where` as the program name (subroutine name) etc.,
80 !> indicating where in the program the message is output.
81 !>
82 !> Give the character variable `message` the string you want to output.
83 !> You can also add optional variables i, r, d, L, s, n, c1, c2, c3.
84 !> See dc_string::CPrintf for details.
85 !>
86 !> The character variable `level` determines the type of message to output:
87 !> - "W" (or "Warning", etc. starting with "W") indicates a **warning**
88 !> - "E" (or "Error", etc. starting with "E") indicates an **error** (program terminates after message output)
89 !> - Other characters (usually "M" is expected) indicate a **normal message**
90 !>
91 !> If "E" is given, the program is forcibly terminated after the message is output.
92 !> The error code will be dc_error::USR_ERRNO.
93 !>
94 !> @param[in] level Message level ("E", "W", or "M")
95 !> @param[in] where Program name, procedure name
96 !> @param[in] message Message text
97 !> @param[in] i Integer values for formatting
98 !> @param[in] r Real values for formatting
99 !> @param[in] d Double precision values for formatting
100 !> @param[in] L Logical values for formatting
101 !> @param[in] n Integer values for formatting
102 !> @param[in] c1 Character string 1 for formatting
103 !> @param[in] c2 Character string 2 for formatting
104 !> @param[in] c3 Character string 3 for formatting
105 !> @param[in] ca Character array for formatting
106 !> @param[in] rank_mpi When MPI is used, messages are output only on the node with
107 !> this rank number. If a negative value is given, output is done on all nodes.
108 !> This option is ignored if MPI is not used.
109 !> @enden
110 !>
111 !> @ja
112 !> @brief メッセージの出力およびエラーによる終了
113 !> @details
114 !> メッセージを標準出力へ出力したい場合に用います。
115 !>
116 !> 文字型変数 `where` にはプログラム名 (サブルーチン名) など、
117 !> プログラム内のどこでメッセージを出力するのかを示すものを与えます。
118 !>
119 !> 文字型変数 `message` には、出力したい文字列を与えます。
120 !> オプション変数 i, r, d, L, s, n, c1, c2, c3 を付加する事も出来ます。
121 !> 詳細に関しては dc_string::CPrintf を参照して下さい。
122 !>
123 !> 文字型変数 `level` は出力するメッセージの種類を決める引数で、
124 !> - "W" (または "Warning" など "W" で始まる文字) を与える事で **警告** であることを指定
125 !> - "E" (または "Error" など "E" で始まる文字) を与える事で **エラー** (メッセージ出力後プログラムを終了) であることを指定
126 !> - それ以外の文字 (大抵は "M" を与えることを想定) を与える事で **通常のメッセージ** であることを指定
127 !>
128 !> "E" を与えた場合はメッセージ出力後、プログラムを強制終了させます。
129 !> エラーコードは dc_error::USR_ERRNO となります。
130 !>
131 !> @param[in] level メッセージレベル ("E", "W", "M" のいずれか)
132 !> @param[in] where プログラム名、手続き名
133 !> @param[in] message メッセージ
134 !> @param[in] i フォーマット用整数配列
135 !> @param[in] r フォーマット用実数配列
136 !> @param[in] d フォーマット用倍精度実数配列
137 !> @param[in] L フォーマット用論理配列
138 !> @param[in] n フォーマット用整数配列
139 !> @param[in] c1 フォーマット用文字列1
140 !> @param[in] c2 フォーマット用文字列2
141 !> @param[in] c3 フォーマット用文字列3
142 !> @param[in] ca フォーマット用文字列配列
143 !> @param[in] rank_mpi MPI 使用時に、ここで指定されたランク数のノードでのみ
144 !> メッセージ出力を行います。負の値を与えた場合には、
145 !> 全てのノードで出力を行います。MPI を使用していない場合には無視されます。
146 !> @endja
147 subroutine messagenotifyc(level, where, message, &
148 & i, r, d, L, n, c1, c2, c3, ca, rank_mpi )
149 use dc_types ,only: string, dp
150 use dc_string ,only: uchar, strhead, printf, cprintf
151 use dc_error ,only: storeerror, usr_errno
152 implicit none
153 character(*), intent(in) :: level
154 character(*), intent(in) :: where
155 character(*), intent(in) :: message
156 integer , intent(in), optional:: i(:), n(:)
157 real , intent(in), optional:: r(:)
158 real(DP) , intent(in), optional:: d(:)
159 logical , intent(in), optional:: L(:)
160 character(*), intent(in), optional:: c1, c2, c3
161 character(*), intent(in), optional:: ca(:)
162 integer , intent(in), optional:: rank_mpi
163 character(string) :: msg
164 continue
165 if ( invalid_rank_number( rank_mpi ) ) return
166 if ( strhead( 'ERROR', trim( uchar(level) ) ) ) then
167 msg = cprintf(message, &
168 & i=i, r=r, d=d, l=l, n=n, c1=c1, c2=c2, c3=c3, ca=ca)
169 call storeerror(usr_errno, where, cause_c=msg)
170 elseif ( strhead( 'WARNING', trim( uchar(level) ) ) ) then
171 msg = cprintf(message, &
172 & i=i, r=r, d=d, l=l, n=n, c1=c1, c2=c2, c3=c3, ca=ca)
173 msg=' *** WARNING [' // trim(where) // '] *** '// trim(msg)
174 call printf(fmt='%c', c1=msg)
175 else
176 msg = cprintf(message, &
177 & i=i, r=r, d=d, l=l, n=n, c1=c1, c2=c2, c3=c3, ca=ca)
178 msg=' *** MESSAGE [' // trim(where) // '] *** ' // trim(msg)
179 call printf(fmt='%c', c1=msg)
180 endif
181 return
182 end subroutine messagenotifyc
183 !> @en
184 !> @brief Output message or terminate with error (with error code)
185 !> @details
186 !> Basically the same as the other MessageNotify (or dc_message::MessageNotifyC),
187 !> but this one takes a numeric variable `number` as the first argument.
188 !> This `number` is passed directly to dc_error::StoreError as an error code.
189 !> See dc_error for error codes.
190 !>
191 !> @param[in] number Error code (see dc_error)
192 !> @param[in] where Program name, procedure name
193 !> @param[in] message Message text (optional)
194 !> @param[in] i Integer values for formatting
195 !> @param[in] r Real values for formatting
196 !> @param[in] d Double precision values for formatting
197 !> @param[in] L Logical values for formatting
198 !> @param[in] n Integer values for formatting
199 !> @param[in] c1 Character string 1 for formatting
200 !> @param[in] c2 Character string 2 for formatting
201 !> @param[in] c3 Character string 3 for formatting
202 !> @param[in] ca Character array for formatting
203 !> @param[in] rank_mpi When MPI is used, messages are output only on the node with
204 !> this rank number. If a negative value is given, output is done on all nodes.
205 !> This option is ignored if MPI is not used.
206 !> @enden
207 !>
208 !> @ja
209 !> @brief メッセージの出力およびエラーによる終了 (エラーコード版)
210 !> @details
211 !> 基本的にもう一方の MessageNotify (または dc_message::MessageNotifyC)
212 !> と同様ですが、こちらは第1引数に数値型変数 `number` をとります。
213 !> この `number` はエラーコードとして、そのまま dc_error::StoreError に
214 !> 引き渡されます。エラーコードに関しては dc_error を参照ください。
215 !>
216 !> @param[in] number エラーコード (dc_error 参照)
217 !> @param[in] where プログラム名、手続き名
218 !> @param[in] message メッセージ (省略可)
219 !> @param[in] i フォーマット用整数配列
220 !> @param[in] r フォーマット用実数配列
221 !> @param[in] d フォーマット用倍精度実数配列
222 !> @param[in] L フォーマット用論理配列
223 !> @param[in] n フォーマット用整数配列
224 !> @param[in] c1 フォーマット用文字列1
225 !> @param[in] c2 フォーマット用文字列2
226 !> @param[in] c3 フォーマット用文字列3
227 !> @param[in] ca フォーマット用文字列配列
228 !> @param[in] rank_mpi MPI 使用時に、ここで指定されたランク数のノードでのみ
229 !> メッセージ出力を行います。負の値を与えた場合には、
230 !> 全てのノードで出力を行います。MPI を使用していない場合には無視されます。
231 !> @endja
232 subroutine messagenotifyi(number, where, message, &
233 & i, r, d, L, n, c1, c2, c3, ca, rank_mpi )
234 use dc_types ,only: dp
235 use dc_string ,only: cprintf
236 use dc_error ,only: storeerror
237 implicit none
238 integer, intent(in) :: number
239 character(*), intent(in) :: where
240 character(*), intent(in), optional:: message
241 integer , intent(in), optional:: i(:), n(:)
242 real , intent(in), optional:: r(:)
243 real(DP) , intent(in), optional:: d(:)
244 logical , intent(in), optional:: L(:)
245 character(*), intent(in), optional:: c1, c2, c3
246 character(*), intent(in), optional:: ca(:)
247 integer , intent(in), optional:: rank_mpi
248 continue
249 if ( invalid_rank_number( rank_mpi ) ) return
250 if (.not. present(message)) then
251 call storeerror(number, where)
252 else
253 call storeerror(number, where, &
254 & cause_c=cprintf( message, &
255 & i=i, r=r, d=d, l=l, n=n, c1=c1, c2=c2, c3=c3, ca=ca ) )
256 endif
257 return
258 end subroutine messagenotifyi
259 !> @en
260 !> @brief Suppress message output on other MPI nodes
261 !> @details
262 !> Sets the rank of the node that outputs messages.
263 !> Output on nodes that do not have this rank number is suppressed.
264 !> This subroutine is ignored if MPI is not used.
265 !>
266 !> @param[in] rank Number of rank of the node that outputs messages.
267 !> Output on nodes that do not have this rank number is suppressed.
268 !> @enden
269 !>
270 !> @ja
271 !> @brief MPI 使用時のメッセージ出力抑止設定
272 !> @details
273 !> 出力するノードのランク数を設定します。
274 !> ここに指定されたランク数以外のノードでの出力は抑止されます。
275 !> MPI を使用していない場合にはサブルーチンは無効です。
276 !>
277 !> @param[in] rank 出力するノードのランク数。
278 !> ここに指定されたランク数以外のノードでの出力は抑止されます。
279 !> @endja
280 subroutine messagesuppressmpi0( rank )
281 implicit none
282 integer, intent(in):: rank
283 continue
284 output_rank = rank
285 end subroutine messagesuppressmpi0
286 !> @en
287 !> @brief Check if the current MPI rank should suppress output
288 !> @details
289 !> Returns .true. if the current MPI rank should not output messages,
290 !> .false. if output is allowed. When MPI is not used, always returns .false.
291 !>
292 !> @param[in] rank_mpi When MPI is used, messages are output only on the node with
293 !> this rank number. If a negative value is given, output is done on all nodes.
294 !> This option is ignored if MPI is not used.
295 !> @return .true. if output should be suppressed, .false. otherwise
296 !> @enden
297 !>
298 !> @ja
299 !> @brief 現在の MPI ランクが出力を抑止すべきかチェック
300 !> @details
301 !> 現在の MPI ランクがメッセージを出力すべきでない場合に .true. を返し、
302 !> 出力が許可されている場合は .false. を返します。
303 !> MPI を使用していない場合は常に .false. を返します。
304 !>
305 !> @param[in] rank_mpi MPI 使用時に、ここで指定されたランク数のノードでのみ
306 !> メッセージ出力を行います。負の値を与えた場合には、
307 !> 全てのノードで出力を行います。MPI を使用していない場合には無視されます。
308 !> @return 出力を抑止すべき場合は .true.、それ以外は .false.
309 !> @endja
310 logical function invalid_rank_number( rank_mpi ) result(result)
311 implicit none
312 integer , intent(in), optional:: rank_mpi
313 logical:: initflag_mpi
314 integer:: err_mpi
315 continue
316 if ( save_myrank < 0 ) then
317 call mpi_initialized(initflag_mpi, err_mpi)
318 if ( initflag_mpi ) then
319 call mpi_comm_rank(mpi_comm_world, save_myrank, err_mpi)
320 else
321 result = .false.
322 return
323 end if
324 end if
325 if ( .not. present(rank_mpi) ) then
326 if ( output_rank > -1 ) then
327 if ( output_rank == save_myrank ) then
328 result = .false.
329 return
330 else
331 result = .true.
332 return
333 end if
334 end if
335 result = .false.
336 return
337 end if
338 if ( rank_mpi < 0 ) then
339 result = .false.
340 return
341 end if
342 if ( rank_mpi == save_myrank ) then
343 result = .false.
344 else
345 result = .true.
346 end if
347 end function invalid_rank_number
348 !> @namespace dc_message
349end module dc_message
エラー処理用モジュール
Definition dc_error.f90:454
subroutine, public storeerror(number, where, err, cause_c, cause_i)
Definition dc_error.f90:891
integer, parameter, public usr_errno
-1000 以下: ユーザー定義
Definition dc_error.f90:579
メッセージの出力
文字型変数の操作
Definition dc_string.f90:83
種別型パラメタを提供します。
Definition dc_types.f90:55
integer, parameter, public string
文字列を保持する 文字型変数の種別型パラメタ
Definition dc_types.f90:137
integer, parameter, public dp
倍精度実数型変数
Definition dc_types.f90:92