PROGRESS  master
prg_extras_mod.F90
Go to the documentation of this file.
1 
7 
9 
10  implicit none
11 
12  private
13 
14  interface
15  subroutine prg_memory_consumption(vm_peak, vm_size, pid, ppid) &
16  bind(C, name = "prg_memory_consumption")
17  use, intrinsic :: iso_c_binding
18  integer(C_LONG_LONG), intent(inout) :: vm_peak
19  integer(C_LONG_LONG), intent(inout) :: vm_size
20  integer(C_LONG_LONG), intent(inout) :: pid
21  integer(C_LONG_LONG), intent(inout) :: ppid
22  end subroutine prg_memory_consumption
23  end interface
24 
25  interface to_string
26  module procedure to_string_integer
27  module procedure to_string_long_long
28  module procedure to_string_double
29  end interface to_string
30 
31  integer, parameter :: dp = kind(1.0d0)
32 
33  public :: mls
34  public :: prg_delta
35  public :: prg_get_mem
36  public :: prg_print_matrix
37  public :: to_string
38  public :: prg_norm2
39 
40 contains
41 
46  function to_string_integer(i)
47 
48  character(len=:), allocatable :: to_string_integer
49  integer, intent(in) :: i
50  character(len=20) :: buffer
51 
52  write(buffer, "(I20)") i
53  allocate(character(len_trim(adjustl(buffer))) :: to_string_integer)
54  to_string_integer = trim(adjustl(buffer))
55 
56  end function to_string_integer
57 
62  function to_string_long_long(i)
63 
64  use, intrinsic :: iso_c_binding
65 
66  character(len=:), allocatable :: to_string_long_long
67  integer(kind=C_LONG_LONG), intent(in) :: i
68  character(len=30) :: buffer
69 
70  write(buffer, "(I30)") i
71  allocate(character(len_trim(adjustl(buffer))) :: to_string_long_long)
72  to_string_long_long = trim(adjustl(buffer))
73 
74  end function to_string_long_long
75 
80  function to_string_double(x)
81 
82  character(len=:), allocatable :: to_string_double
83  double precision, intent(in) :: x
84  character(len=20) :: buffer
85 
86  write(buffer, "(ES20.8)") x
87  allocate(character(len_trim(adjustl(buffer))) :: to_string_double)
88  to_string_double = trim(adjustl(buffer))
89 
90  end function to_string_double
91 
100  subroutine prg_print_matrix(matname,amat,i1,i2,j1,j2)
101 
102  integer :: ndim, i, j
103  integer, intent (in) :: i1,i2,j1,j2
104  integer :: ii2,jj2
105  real(dp), intent (in) :: amat(:,:)
106  character(len=*) :: matname
107 
108  if(i1 > i2)stop "Error at prg_print_matrix, i1 > i2"
109  if(j1 > j2)stop "Error at prg_print_matrix, j1 > j2"
110 
111  ndim = size(amat,dim=1)
112  if(i2 > ndim)then
113  ii2=ndim
114  else
115  ii2=i2
116  endif
117 
118  if(j2 > ndim)then
119  jj2=ndim
120  else
121  jj2=j2
122  endif
123 
124  write(*,*)""
125  write(*,*)" ============================================== "
126  write(*,*)matname
127  do i = i1, ii2
128  write(*,'(10F15.10)') (amat(i,j), j = j1,jj2)
129  end do
130  write(*,*)" ============================================== "
131  write(*,*)""
132 
133  end subroutine prg_print_matrix
134 
135 
139  function mls()
140  real(dp) :: mls
141  integer :: timevector(8)
142 
143  mls = 0.0_dp
144  call date_and_time(values=timevector)
145  mls=timevector(5)*60.0_dp*60.0_dp*1000.0_dp + timevector(6)*60.0_dp*1000.0_dp &
146  + timevector(7)*1000.0_dp + timevector(8)
147 
148  end function mls
149 
155  subroutine prg_delta(x,s,nn,dta)
156 
157  integer :: i, j, nn
158  real(dp) :: x(nn,nn),s(nn,nn),temp1(nn,nn),temp2(nn,nn), dta
159  real(dp) :: identity(nn,nn)
160 
161  identity=0.0
162 
163  do j = 1, nn
164  identity(j,j)=1.0
165  enddo
166 
167  temp1=matmul(transpose(x),s)
168  temp2=matmul(temp1,x)
169 
170  do j = 1, nn
171  identity(j,j)=1.0
172  enddo
173 
174  temp1=0.0
175  do i = 1, nn
176  do j = 1, nn
177  temp1(i,j) = identity(i,j)-temp2(i,j)
178  enddo
179  enddo
180 
181  !Take the max absolute value of the leading eigenvectors.
182  call prg_twonorm(temp1,nn,dta)
183 
184  end subroutine prg_delta
185 
186 
191  subroutine prg_get_mem(procname, tag)
192 
193  use, intrinsic :: iso_c_binding
194 
195  character(*), intent(in) :: procname
196  character(*), intent(in) :: tag
197  character(200) :: command
198  integer(kind=C_LONG_LONG) :: vm_peak, vm_size, pid, ppid
199 
200  call prg_memory_consumption(vm_peak, vm_size, pid, ppid)
201 
202  write(*, *) "Used mem "//trim(tag) &
203  //" (pid "//to_string(pid)//", " &
204  //" ppid "//to_string(ppid)//") = " &
205  //trim(to_string(vm_size))//" MiB (" &
206  //trim(to_string(vm_peak))//" MiB)"
207 
208  end subroutine prg_get_mem
209 
215  subroutine prg_twonorm(a,nn,norm2)
216 
217  integer :: info, nn
218  real(dp) :: a(nn,nn), norm2
219  integer :: tmp_lwork
220  real(dp) :: utmp(nn,nn), tmp_evals(nn)
221  real(dp), allocatable :: tmp_work(:)
222 
223  tmp_lwork=3*nn -1
224  allocate(tmp_work(tmp_lwork))
225 
226  utmp=a
227 
228  call dsyev("v", "u", nn, utmp, nn, tmp_evals, tmp_work, &
229  tmp_lwork, info)
230 
231  norm2=max(abs(tmp_evals(1)),abs(tmp_evals(nn)))
232 
233  deallocate(tmp_work)
234 
235  end subroutine prg_twonorm
236 
240  real(dp) function prg_norm2(a)
241  implicit none
242  integer :: nn, i
243  real(dp), intent(in) :: a(:)
244 
245  nn = size(a,dim=1)
246  prg_norm2 = 0.0_dp
247 #ifdef NORM2
248  prg_norm2 = norm2(a)
249 #else
250  do i = 1, nn
251  prg_norm2 = prg_norm2 + a(i)*a(i)
252  enddo
253  prg_norm2 = sqrt(prg_norm2)
254 #endif
255 
256  end function prg_norm2
257 
258 end module prg_extras_mod
Extra routines.
character(len=:) function, allocatable to_string_double(x)
Convert double to string.
subroutine, public prg_print_matrix(matname, amat, i1, i2, j1, j2)
To write a dense matrix to screen.
subroutine, public prg_get_mem(procname, tag)
Get proc memory.
character(len=:) function, allocatable to_string_integer(i)
Convert integer to string.
subroutine, public prg_delta(x, s, nn, dta)
Delta function ||X^tSX - I||.
subroutine prg_twonorm(a, nn, norm2)
Gets the norm2 of a square matrix.
character(len=:) function, allocatable to_string_long_long(i)
Convert integer to string.
real(dp) function, public mls()
To get the actual time in milliseconds.
integer, parameter dp
real(dp) function, public prg_norm2(a)
Gets the norm2 of a vector.
Module to handle input output files for the PROGRESS lib.