ComIn 0.5.1
ICON Community Interface
Loading...
Searching...
No Matches
comin_setup.F90
Go to the documentation of this file.
1!> @file comin_setup.F90
2!! @brief Routines to set up ComIn (except for callbacks).
3!
4! @authors 08/2021 :: ICON Community Interface <comin@icon-model.org>
5!
6! SPDX-License-Identifier: BSD-3-Clause
7!
8! See LICENSES for license information.
9! Where software is supplied by third parties, it is indicated in the
10! headers of the routines.
11!
13
14 USE iso_c_binding, ONLY: c_int, c_char, c_bool, c_funptr, c_funloc
15 USE comin_setup_constants, ONLY: wp
16 USE comin_state, ONLY: state, &
24 USE comin_plugin_info, ONLY: comin_severity_debug, comin_severity_info, &
25 & comin_severity_warning, comin_severity_error
32
33 IMPLICIT NONE
34
35 PRIVATE
36
37 PUBLIC :: comin_setup_check
39 PUBLIC :: comin_setup_init
40 PUBLIC :: comin_setup_finalize
43
44#include "comin_global.inc"
45
46 !> The elements of this derived data type describe the current
47 !! community interface.
48 !! @ingroup fortran_interface
49 TYPE, BIND(C) :: t_comin_setup_version_info
50 !> ComIn versioning (major and minor version info)
51 INTEGER(kind=c_int) :: version_no_major, version_no_minor, &
52 & version_no_patch
54
55 !> C-compatible version of the plugin info type.
56 TYPE, BIND(C) :: t_comin_plugin_description_c
57 CHARACTER(c_char), DIMENSION(COMIN_MAX_LEN_PLUGIN_NAME+1) :: name
58 CHARACTER(c_char), DIMENSION(COMIN_MAX_LEN_PLUGIN_LIBRARY+1) :: plugin_library
59 CHARACTER(c_char), DIMENSION(COMIN_MAX_LEN_PRIMARY_CONSTRUCTOR+1) :: primary_constructor
60 CHARACTER(c_char), DIMENSION(COMIN_MAX_LEN_OPTIONS+1) :: options
61 CHARACTER(c_char), DIMENSION(COMIN_MAX_LEN_COMM+1) :: comm
62 INTEGER(c_int) :: log_severity
63 END TYPE t_comin_plugin_description_c
64
65 INTERFACE
66 SUBROUTINE comin_plugin_primaryconstructor_c(plugin_list, len) &
67 & bind(c, name='comin_plugin_primaryconstructor')
68 IMPORT t_comin_plugin_description_c, c_int
69 TYPE(t_comin_plugin_description_c), INTENT(IN) :: plugin_list(*)
70 INTEGER(c_int), VALUE, INTENT(IN) :: len
72 END INTERFACE
73
74#include "comin_version.inc"
75
76CONTAINS
77
78 !> Returns version info.
79 !! @ingroup fortran_interface
82 comin_setup_get_version%version_no_major = comin_version_major
83 comin_setup_get_version%version_no_minor = comin_version_minor
84 comin_setup_get_version%version_no_patch = comin_version_patch
85 END FUNCTION comin_setup_get_version
86
87 !> Execute primary constructors.
88 SUBROUTINE comin_plugin_primaryconstructor(plugin_list)
89 TYPE(t_comin_plugin_description), INTENT(IN) :: plugin_list(:)
90
91 TYPE(t_comin_plugin_description_c) :: plugin_list_c(size(plugin_list))
92 INTEGER :: i
93
94 DO i = 1, SIZE(plugin_list)
95 IF (plugin_list(i)%log_debug) THEN
96 plugin_list_c(i)%log_severity = comin_severity_debug
97 ELSE IF (plugin_list(i)%log_info) THEN
98 plugin_list_c(i)%log_severity = comin_severity_info
99 ELSE IF (plugin_list(i)%log_warning) THEN
100 plugin_list_c(i)%log_severity = comin_severity_warning
101 ELSE
102 plugin_list_c(i)%log_severity = comin_severity_error
103 END IF
104
105 CALL convert_f_string(plugin_list(i)%name, plugin_list_c(i)%name)
106 CALL convert_f_string(plugin_list(i)%options, plugin_list_c(i)%options)
107 CALL convert_f_string(plugin_list(i)%plugin_library, plugin_list_c(i)%plugin_library)
108 CALL convert_f_string(plugin_list(i)%primary_constructor, plugin_list_c(i)%primary_constructor)
109 CALL convert_f_string(plugin_list(i)%comm, plugin_list_c(i)%comm)
110 END DO
111
112 CALL comin_plugin_primaryconstructor_c(plugin_list_c, SIZE(plugin_list))
113
115
116 !> Performs basic compatibility checks.
117 SUBROUTINE comin_setup_check(plugin_str, wp_check)
118 CHARACTER(LEN=*), INTENT(IN) :: plugin_str !< plugin name
119 INTEGER, INTENT(IN) :: wp_check !< KIND value for compatibility checks.
120 !
121
122 IF (.NOT. comin_state_is_host_errhandler_set()) THEN
123 CALL comin_error_set(comin_error_setup_errhandler_not_set); RETURN
124 END IF
125
126 ! compare floating point precision
127 IF (wp /= wp_check) THEN
128 CALL comin_error_set(comin_error_setup_precision_test_failed); RETURN
129 ELSE
130 CALL comin_message(" " // plugin_str // ": working precision test successful.", 0)
131 END IF
132 END SUBROUTINE comin_setup_check
133
134 !> Initialize the comin state
135 !! This routine needs to be called by the host before any other comin call
136 !!
137 SUBROUTINE comin_setup_init(lstdout, output_unit)
138 LOGICAL(KIND=C_BOOL), INTENT(IN) :: lstdout !< do print on stdout or not
139 INTEGER, INTENT(IN), OPTIONAL :: output_unit !< output unit to print messages to
140 IF (ASSOCIATED(state)) THEN
141 ! <! cant use comin_message due to circular dependencies
142 CALL comin_error_set(comin_error_setup_comin_already_initialized); RETURN
143 END IF
144 ALLOCATE(state)
145
146 CALL comin_state_set_lstdout(lstdout)
147 IF(PRESENT(output_unit)) THEN
148 CALL comin_state_set_output_unit(output_unit)
149 END IF
150 END SUBROUTINE comin_setup_init
151
152 !> Destructor.
154
155 CALL comin_parallel_free_mpi_comms()
156
157 ! we don't call dlclose in the finalization because it has no
158 ! advantages, while it would triggers some problems:
159 !
160 ! - NVHPC compilers for GPU try to unload the library again in at
161 ! the end of the program resulting in a segfault. See
162 ! https://gitlab.dkrz.de/icon-comin/comin/-/issues/210
163 !
164 ! - parallel HDF5 registers calls backs at `MPI_COMM_SELF` atexit
165 ! handler, which also results in a segfault as the call gets
166 ! unloaded in dlclose before the handler is called.
167 ! See https://gitlab.dkrz.de/icon-comin/comin/-/issues/214
168 !
169 END SUBROUTINE comin_setup_finalize
170
171 !> Sets the global error handler procedure pointer.
172 !!
173 !! To be called by the host application (ICON).
174 !!
175 !! Aborts with an error code if the error handler has already been
176 !! set.
177 SUBROUTINE comin_setup_errhandler(error_handler)
178 PROCEDURE(comin_host_errhandler_fct) :: error_handler !< error handler
179 !
180 TYPE(c_funptr) :: fct_ptr
181 fct_ptr = c_funloc(error_handler)
183 IF (.NOT. comin_state_is_host_errhandler_set()) THEN
184 CALL comin_error_set(comin_error_setup_errhandler_not_associated); RETURN
185 END IF
186 END SUBROUTINE comin_setup_errhandler
187
188END MODULE comin_setup
void comin_error_set(t_comin_error_code error_code)
void comin_setup_get_version(unsigned int *major, unsigned int *minor, unsigned int *patch)
void comin_plugin_primaryconstructor(const t_comin_plugin_description *plugin_list, int num_plugins)
void comin_state_set_output_unit(int unit)
bool comin_state_is_host_errhandler_set()
void comin_state_set_lstdout(bool lstdout)
void comin_state_set_host_errhandler_fct(t_comin_host_errhandler_fct_ptr fct)
The elements of this derived data type describe the current community interface.
integer, parameter wp
working precision
In order to be C-compliant, the following interface is incompatible with ICON finish; it requires a w...
subroutine, public convert_f_string(string, arr)
Convert Fortran string into C-style character array.
subroutine, public comin_message(message, lvl)
Prints a message on rank 0 if the global verbosity level larger than lvl.
subroutine, public comin_parallel_free_mpi_comms()
subroutine, public comin_setup_finalize()
Destructor.
subroutine, public comin_setup_errhandler(error_handler)
Sets the global error handler procedure pointer.
subroutine, public comin_setup_init(lstdout, output_unit)
Initialize the comin state This routine needs to be called by the host before any other comin call.
subroutine, public comin_setup_check(plugin_str, wp_check)
Performs basic compatibility checks.
type(t_comin_state), pointer, public state