Yet Another eXchange Tool  DO_NOT_EDIT_HERE
xt_idxsection_f.f90
Go to the documentation of this file.
1 
12 
13 !
14 ! Keywords:
15 ! Maintainer: Jörg Behrens <behrens@dkrz.de>
16 ! Moritz Hanke <hanke@dkrz.de>
17 ! Thomas Jahns <jahns@dkrz.de>
18 ! URL: https://doc.redmine.dkrz.de/yaxt/html/
19 !
20 ! Redistribution and use in source and binary forms, with or without
21 ! modification, are permitted provided that the following conditions are
22 ! met:
23 !
24 ! Redistributions of source code must retain the above copyright notice,
25 ! this list of conditions and the following disclaimer.
26 !
27 ! Redistributions in binary form must reproduce the above copyright
28 ! notice, this list of conditions and the following disclaimer in the
29 ! documentation and/or other materials provided with the distribution.
30 !
31 ! Neither the name of the DKRZ GmbH nor the names of its contributors
32 ! may be used to endorse or promote products derived from this software
33 ! without specific prior written permission.
34 !
35 ! THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
36 ! IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
37 ! TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
38 ! PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
39 ! OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
40 ! EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
41 ! PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
42 ! PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
43 ! LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
44 ! NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
45 ! SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
46 !
48  USE iso_c_binding, ONLY: c_int, c_ptr
49  USE xt_core, ONLY: xt_int_kind, xt_abort, i2, i4, i8
51  IMPLICIT NONE
52  PRIVATE
54 
55  INTERFACE xt_idxsection_new
56  MODULE PROCEDURE xt_idxsection_new_a
57  MODULE PROCEDURE xt_idxsection_new_i2
58  MODULE PROCEDURE xt_idxsection_new_i4
59  MODULE PROCEDURE xt_idxsection_new_i8
60  END INTERFACE xt_idxsection_new
61 
62  INTERFACE
63  FUNCTION xt_idxsection_new_c(start, num_dimensions, global_size, &
64  local_size, local_start) bind(c, name='xt_idxsection_new') &
65  result(idxsection)
66  IMPORT :: c_int, c_ptr, xt_idxlist, xt_int_kind
67  INTEGER(xt_int_kind), VALUE, INTENT(in) :: start
68  INTEGER(c_int), VALUE, INTENT(in) :: num_dimensions
69  INTEGER(xt_int_kind), INTENT(in) :: global_size(num_dimensions), &
70  local_start(num_dimensions)
71  INTEGER(c_int), INTENT(in) :: local_size(num_dimensions)
72  TYPE(c_ptr) :: idxsection
73  END FUNCTION xt_idxsection_new_c
74  END INTERFACE
75 
76 CONTAINS
77 
78  FUNCTION xt_idxsection_new_a(start, global_size, local_size, local_start) &
79  result(idxsection)
80  INTEGER(xt_int_kind), INTENT(in) :: start, local_start(:), global_size(:)
81  INTEGER, INTENT(in) :: local_size(:)
82  TYPE(xt_idxlist) :: idxsection
83  INTEGER :: num_dimensions
84  INTEGER(c_int) :: num_dimensions_c
85  num_dimensions = SIZE(global_size)
86  IF (SIZE(local_size) /= num_dimensions &
87  .OR. SIZE(local_start) /= num_dimensions) &
88  CALL xt_abort("non-matching array sizes", &
89  __file__, &
90  __line__)
91  num_dimensions_c = int(num_dimensions, c_int)
92  idxsection = xt_idxlist_c2f(&
93  xt_idxsection_new_c(start, num_dimensions_c, global_size, &
94  & int(local_size, c_int), local_start))
95  END FUNCTION xt_idxsection_new_a
96 
97  FUNCTION xt_idxsection_new_i2(start, num_dimensions, global_size, &
98  local_size, local_start) RESULT(idxsection)
99  INTEGER(i2), INTENT(in) :: num_dimensions
100  INTEGER(xt_int_kind), INTENT(in) :: start, global_size(num_dimensions), &
101  local_start(num_dimensions)
102  INTEGER, INTENT(in) :: local_size(num_dimensions)
103  TYPE(xt_idxlist) :: idxsection
104  INTEGER(c_int) :: num_dimensions_c
105 
106  num_dimensions_c = int(num_dimensions, c_int)
107  idxsection = xt_idxlist_c2f(&
108  xt_idxsection_new_c(start, num_dimensions_c, global_size, &
109  & int(local_size, c_int), local_start))
110  END FUNCTION xt_idxsection_new_i2
111 
112  FUNCTION xt_idxsection_new_i4(start, num_dimensions, global_size, &
113  local_size, local_start) RESULT(idxsection)
114  INTEGER(i4), INTENT(in) :: num_dimensions
115  INTEGER(xt_int_kind), INTENT(in) :: start, global_size(num_dimensions), &
116  local_start(num_dimensions)
117  INTEGER, INTENT(in) :: local_size(num_dimensions)
118  TYPE(xt_idxlist) :: idxsection
119  INTEGER(c_int), PARAMETER :: dummy = 1
120  INTEGER(c_int) :: num_dimensions_c
121 
122  IF (num_dimensions > huge(dummy)) &
123  CALL xt_abort("num_dimensions too large", &
124  __file__, &
125  __line__)
126  num_dimensions_c = int(num_dimensions, c_int)
127  idxsection = xt_idxlist_c2f(&
128  xt_idxsection_new_c(start, num_dimensions_c, global_size, &
129  & int(local_size, c_int), local_start))
130  END FUNCTION xt_idxsection_new_i4
131 
132  FUNCTION xt_idxsection_new_i8(start, num_dimensions, global_size, &
133  local_size, local_start) RESULT(idxsection)
134  INTEGER(i8), INTENT(in) :: num_dimensions
135  INTEGER(xt_int_kind), INTENT(in) :: start, global_size(num_dimensions), &
136  local_start(num_dimensions)
137  INTEGER, INTENT(in) :: local_size(num_dimensions)
138  TYPE(xt_idxlist) :: idxsection
139  INTEGER(c_int), PARAMETER :: dummy = 1
140  INTEGER(c_int) :: num_dimensions_c
141 
142  IF (num_dimensions > huge(dummy)) &
143  CALL xt_abort("num_dimensions too large", &
144  __file__, &
145  __line__)
146  num_dimensions_c = int(num_dimensions, c_int)
147  idxsection = xt_idxlist_c2f(&
148  xt_idxsection_new_c(start, num_dimensions_c, global_size, &
149  & int(local_size, c_int), local_start))
150  END FUNCTION xt_idxsection_new_i8
151 
161  FUNCTION xt_idxfsection_new(start, global_size, local_size, local_start) &
162  result(idxfsection)
163  INTEGER(xt_int_kind), INTENT(in) :: start, global_size(:), local_start(:)
164  INTEGER, INTENT(in) :: local_size(:)
165  TYPE(xt_idxlist) :: idxfsection
166 
167  INTEGER :: idim, ndim
168  LOGICAL :: err_state
169 
170  ndim = SIZE(global_size)
171  IF (SIZE(local_size) /= ndim .OR. SIZE(local_start) /= ndim) &
172  CALL xt_abort("non-matching array sizes", &
173  __file__, &
174  __line__)
175 
176  ! check if local indices are a subset of global indices:
177  err_state = .false.
178  DO idim = 1, ndim
179  err_state = err_state .OR. (local_start(idim) < 1) .OR. &
180  (local_start(idim) + local_size(idim) - 1 > global_size(idim))
181  ENDDO
182  IF (err_state) CALL xt_abort("local indices out of global index space", &
183  __file__, &
184  __line__)
185 
186  ! Fortran style map of mult-dim coords to indices:
187  ! => reverse order of dimensions and coords starting at 1 (instead of 0 as in c)
188  idxfsection = xt_idxsection_new(start, &
189  global_size(ndim:1:-1), &
190  local_size(ndim:1:-1), &
191  local_start(ndim:1:-1) - 1_xt_int_kind )
192 
193  END FUNCTION xt_idxfsection_new
194 
195 END MODULE xt_idxsection
196 !
197 ! Local Variables:
198 ! f90-continuation-indent: 5
199 ! coding: utf-8
200 ! indent-tabs-mode: nil
201 ! show-trailing-whitespace: t
202 ! require-trailing-newline: t
203 ! End:
204 !
type(xt_idxlist) function, public xt_idxfsection_new(start, global_size, local_size, local_start)
Fortran style version of xt_idxsection_new. Compared to xt_idxsection_new, here the elements of the v...
integer, parameter, public i8
Definition: xt_core_f.f90:60
integer, parameter, public i4
Definition: xt_core_f.f90:59
type(xt_idxlist) function, public xt_idxlist_c2f(idxlist)
integer, parameter, public i2
Definition: xt_core_f.f90:58
Xt_idxlist xt_idxsection_new(Xt_int start, int num_dimensions, const Xt_int global_size[num_dimensions], const int local_size[num_dimensions], const Xt_int local_start[num_dimensions])
integer, parameter, public xt_int_kind
Definition: xt_core_f.f90:54