-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmpdalc.f90
269 lines (233 loc) · 9.98 KB
/
mpdalc.f90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
!> \file
!! Dynamic memory management.
!!
!! \author Claus Kleinwort, DESY, 2012 ([email protected])
!!
!! \copyright
!! Copyright (c) 2012 - 2015 Deutsches Elektronen-Synchroton,
!! Member of the Helmholtz Association, (DESY), HAMBURG, GERMANY \n\n
!! This library is free software; you can redistribute it and/or modify
!! it under the terms of the GNU Library General Public License as
!! published by the Free Software Foundation; either version 2 of the
!! License, or (at your option) any later version. \n\n
!! This library is distributed in the hope that it will be useful,
!! but WITHOUT ANY WARRANTY; without even the implied warranty of
!! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
!! GNU Library General Public License for more details. \n\n
!! You should have received a copy of the GNU Library General Public
!! License along with this program (see the file COPYING.LIB for more
!! details); if not, write to the Free Software Foundation, Inc.,
!! 675 Mass Ave, Cambridge, MA 02139, USA.
!!
!> (De)Allocate vectors and arrays.
MODULE mpdalc
USE mpdef
IMPLICIT NONE
SAVE
! variables
INTEGER(mpl) :: numwordsalloc = 0 !< current dynamic memory allocation (words)
INTEGER(mpl) :: maxwordsalloc = 0 !< peak dynamic memory allocation (words)
INTEGER(mpi) :: nummpalloc = 0 !< number of dynamic allocations
INTEGER(mpi) :: nummpdealloc = 0 !< number of dynamic deallocations
INTEGER(mpi) :: printflagalloc = 0 !< print flag for dynamic allocations
!> allocate array
INTERFACE mpalloc
MODULE PROCEDURE mpallocdvec, mpallocfvec, mpallocivec, &
mpallocfarr, mpallociarr, mpalloclarr, mpalloclist, mpalloccvec
END INTERFACE mpalloc
!> deallocate array
INTERFACE mpdealloc
MODULE PROCEDURE mpdeallocdvec, mpdeallocfvec, mpdeallocivec, &
mpdeallocfarr, mpdeallociarr, mpdealloclarr, mpdealloclist, mpdealloccvec
END INTERFACE mpdealloc
CONTAINS
! allocate dynamic vector or array
!> allocate (1D) double precision array
SUBROUTINE mpallocdvec(array,length,text)
REAL(mpd), DIMENSION(:), INTENT(IN OUT), ALLOCATABLE :: array
INTEGER(mpl), INTENT(IN) :: length
CHARACTER (LEN=*), INTENT(IN) :: text
INTEGER(mpi) :: ifail
ALLOCATE (array(length),stat=ifail)
CALL mpalloccheck(ifail,(mpd*length)/mpi,text)
END SUBROUTINE mpallocdvec
!> allocate (1D) single precision array
SUBROUTINE mpallocfvec(array,length,text)
REAL(mps), DIMENSION(:), INTENT(IN OUT), ALLOCATABLE :: array
INTEGER(mpl), INTENT(IN) :: length
CHARACTER (LEN=*), INTENT(IN) :: text
INTEGER(mpi) :: ifail
ALLOCATE (array(length),stat=ifail)
CALL mpalloccheck(ifail,(mps*length)/mpi,text)
END SUBROUTINE mpallocfvec
!> allocate (1D) integer array
SUBROUTINE mpallocivec(array,length,text)
INTEGER(mpi), DIMENSION(:), INTENT(IN OUT), ALLOCATABLE :: array
INTEGER(mpl), INTENT(IN) :: length
CHARACTER (LEN=*), INTENT(IN) :: text
INTEGER(mpi) :: ifail
ALLOCATE (array(length),stat=ifail)
CALL mpalloccheck(ifail,length,text)
END SUBROUTINE mpallocivec
!> allocate (2D) single precision array
SUBROUTINE mpallocfarr(array,rows,cols,text)
REAL(mps), DIMENSION(:,:), INTENT(IN OUT), ALLOCATABLE :: array
INTEGER(mpl), INTENT(IN) :: rows
INTEGER(mpl), INTENT(IN) :: cols
CHARACTER (LEN=*), INTENT(IN) :: text
INTEGER(mpi) :: ifail
ALLOCATE (array(rows,cols),stat=ifail)
CALL mpalloccheck(ifail,(mps*rows*cols)/mpi,text)
END SUBROUTINE mpallocfarr
!> allocate (2D) INTEGER(mpi) array
SUBROUTINE mpallociarr(array,rows,cols,text)
INTEGER(mpi), DIMENSION(:,:), INTENT(IN OUT), ALLOCATABLE :: array
INTEGER(mpl), INTENT(IN) :: rows
INTEGER(mpl), INTENT(IN) :: cols
CHARACTER (LEN=*), INTENT(IN) :: text
INTEGER(mpi) :: ifail
ALLOCATE (array(rows,cols),stat=ifail)
CALL mpalloccheck(ifail,rows*cols,text)
END SUBROUTINE mpallociarr
!> allocate (2D) large integer array
SUBROUTINE mpalloclarr(array,rows,cols,text)
INTEGER(mpl), DIMENSION(:,:), INTENT(IN OUT), ALLOCATABLE :: array
INTEGER(mpl), INTENT(IN) :: rows
INTEGER(mpl), INTENT(IN) :: cols
CHARACTER (LEN=*), INTENT(IN) :: text
INTEGER(mpi) :: ifail
ALLOCATE (array(rows,cols),stat=ifail)
CALL mpalloccheck(ifail,(mpl*rows*cols)/mpi,text)
END SUBROUTINE mpalloclarr
!> allocate (1D) list item array
SUBROUTINE mpalloclist(array,length,text)
TYPE(listItem), DIMENSION(:), INTENT(IN OUT), ALLOCATABLE :: array
INTEGER(mpl), INTENT(IN) :: length
CHARACTER (LEN=*), INTENT(IN) :: text
INTEGER(mpi) :: ifail
ALLOCATE (array(length),stat=ifail)
CALL mpalloccheck(ifail,((mps+mpi)*length)/mpi,text)
END SUBROUTINE mpalloclist
!> allocate (1D) character array
SUBROUTINE mpalloccvec(array,length,text)
CHARACTER, DIMENSION(:), INTENT(IN OUT), ALLOCATABLE :: array
INTEGER(mpl), INTENT(IN) :: length
CHARACTER (LEN=*), INTENT(IN) :: text
INTEGER(mpi) :: ifail
ALLOCATE (array(length),stat=ifail)
CALL mpalloccheck(ifail,(length+mpi-1)/mpi,text)
END SUBROUTINE mpalloccvec
!> check allocation
SUBROUTINE mpalloccheck(ifail,numwords,text)
INTEGER(mpi), INTENT(IN) :: ifail
INTEGER(mpl), INTENT(IN) :: numwords
CHARACTER (LEN=*), INTENT(IN) :: text
IF (ifail == 0) THEN
nummpalloc=nummpalloc+1
numwordsalloc = numwordsalloc + numwords
maxwordsalloc = MAX(maxwordsalloc, numwordsalloc)
IF (printflagalloc /= 0) THEN
print *, ' MPALLOC allocated ', numwords, ' words for : ', text
print *, ' words used ', numwordsalloc, maxwordsalloc
ENDIF
ELSE
print *, ' MPALLOC failed to allocate ', numwords, ' words for : ', text
print *, ' MPALLOC words used ', numwordsalloc, maxwordsalloc
print *, ' MPALLOC stat = ', ifail
CALL peend(30,'Aborted, memory allocation failed')
STOP
ENDIF
END SUBROUTINE mpalloccheck
! deallocate dynamic vector or array
!> deallocate (1D) double precision array
SUBROUTINE mpdeallocdvec(array)
REAL(mpd), DIMENSION(:), INTENT(IN OUT), ALLOCATABLE :: array
INTEGER(mpi) :: ifail
INTEGER(mpl) :: isize
isize = (mpd*size(array,kind=mpl))/mpi
DEALLOCATE (array,stat=ifail)
CALL mpdealloccheck(ifail,isize)
END SUBROUTINE mpdeallocdvec
!> deallocate (1D) single precision array
SUBROUTINE mpdeallocfvec(array)
REAL(mps), DIMENSION(:), INTENT(IN OUT), ALLOCATABLE :: array
INTEGER(mpi) :: ifail
INTEGER(mpl) :: isize
isize = (mps*size(array,kind=mpl))/mpi
DEALLOCATE (array,stat=ifail)
CALL mpdealloccheck(ifail,isize)
END SUBROUTINE mpdeallocfvec
!> deallocate (1D) integer array
SUBROUTINE mpdeallocivec(array)
INTEGER(mpi), DIMENSION(:), INTENT(IN OUT), ALLOCATABLE :: array
INTEGER(mpi) :: ifail
INTEGER(mpl) :: isize
isize = size(array,kind=mpl)
DEALLOCATE (array,stat=ifail)
CALL mpdealloccheck(ifail,isize)
END SUBROUTINE mpdeallocivec
!> allocate (2D) single precision array
SUBROUTINE mpdeallocfarr(array)
REAL(mps), DIMENSION(:,:), INTENT(IN OUT), ALLOCATABLE :: array
INTEGER(mpi) :: ifail
INTEGER(mpl) :: isize
isize = (mps*size(array,kind=mpl))/mpi
DEALLOCATE (array,stat=ifail)
CALL mpdealloccheck(ifail,isize)
END SUBROUTINE mpdeallocfarr
!> allocate (2D) integer array
SUBROUTINE mpdeallociarr(array)
INTEGER(mpi), DIMENSION(:,:), INTENT(IN OUT), ALLOCATABLE :: array
INTEGER(mpi) :: ifail
INTEGER(mpl) :: isize
isize = size(array,kind=mpl)
DEALLOCATE (array,stat=ifail)
CALL mpdealloccheck(ifail,isize)
END SUBROUTINE mpdeallociarr
!> deallocate (2D) large integer array
SUBROUTINE mpdealloclarr(array)
INTEGER(mpl), DIMENSION(:,:), INTENT(IN OUT), ALLOCATABLE :: array
INTEGER(mpi) :: ifail
INTEGER(mpl) :: isize
isize = (mpl*size(array,kind=mpl))/mpi
DEALLOCATE (array,stat=ifail)
CALL mpdealloccheck(ifail,isize)
END SUBROUTINE mpdealloclarr
!> deallocate (1D) list item array
SUBROUTINE mpdealloclist(array)
TYPE(listItem), DIMENSION(:), INTENT(IN OUT), ALLOCATABLE :: array
INTEGER(mpi) :: ifail
INTEGER(mpl) :: isize
isize = ((mpi+mps)*size(array,kind=mpl))/mpi
DEALLOCATE (array,stat=ifail)
CALL mpdealloccheck(ifail,isize)
END SUBROUTINE mpdealloclist
!> deallocate (1D) character array
SUBROUTINE mpdealloccvec(array)
CHARACTER, DIMENSION(:), INTENT(IN OUT), ALLOCATABLE :: array
INTEGER(mpi) :: ifail
INTEGER(mpl) :: isize
isize = (size(array,kind=mpl)+mpi-1)/mpi
DEALLOCATE (array,stat=ifail)
CALL mpdealloccheck(ifail,isize)
END SUBROUTINE mpdealloccvec
!> check deallocation
SUBROUTINE mpdealloccheck(ifail,numwords)
INTEGER(mpi), INTENT(IN) :: ifail
INTEGER(mpl), INTENT(IN) :: numwords
IF (ifail == 0) THEN
numwordsalloc = numwordsalloc - numwords
nummpdealloc=nummpdealloc+1
IF (printflagalloc /= 0) THEN
print *, ' MPDEALLOC deallocated ', numwords, ' words '
print *, ' words used ', numwordsalloc, maxwordsalloc
ENDIF
ELSE
print *, ' MPDEALLOC failed to deallocate ', numwords, ' words'
print *, ' MPDEALLOC words used ', numwordsalloc, maxwordsalloc
print *, ' MPDEALLOC stat = ', ifail
CALL peend(31,'Aborted, memory deallocation failed')
STOP
ENDIF
END SUBROUTINE mpdealloccheck
END MODULE mpdalc