MPI-SCATCI 2.0
An MPI version of SCATCI
Loading...
Searching...
No Matches
BaseManager_module.f90
Go to the documentation of this file.
1! Copyright 2019
2!
3! For a comprehensive list of the developers that contributed to these codes
4! see the UK-AMOR website.
5!
6! This file is part of UKRmol-in (UKRmol+ suite).
7!
8! UKRmol-in is free software: you can redistribute it and/or modify
9! it under the terms of the GNU General Public License as published by
10! the Free Software Foundation, either version 3 of the License, or
11! (at your option) any later version.
12!
13! UKRmol-in is distributed in the hope that it will be useful,
14! but WITHOUT ANY WARRANTY; without even the implied warranty of
15! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16! GNU General Public License for more details.
17!
18! You should have received a copy of the GNU General Public License
19! along with UKRmol-in (in source/COPYING). Alternatively, you can also visit
20! <https://www.gnu.org/licenses/>.
21
22!> \brief Base manager type
23!> \authors A Al-Refaie
24!> \date 2017
25!>
26!> This module contains the BaseManager class that is used to track all memory allocations within a single process,
27!> as of now it is fairly simple in implementation.
28!>
29!> \note 16/01/2019 - Jakub Benda: Unifom coding style and expanded documentation.
30!>
31module basemanager_module
32
33 use precisn, only: longint
34 use basempi_module, only: basempi
35
36 implicit none
37
38 public basemanager, memory_man
39
40 private
41
42 !>This is a simple class to handle memory management tracking
43 type, extends(basempi) :: basemanager
44 private
45 integer(longint) :: total_local_memory !< The total memory we have been assigned
46 integer(longint) :: available_local_memory !< Memory we have left over
47 contains
48 private
49 procedure :: basemanager_ctor
50 procedure :: init_memory !< Initialize the memory with a new value
51 procedure :: track_memory !< Tracks
52 procedure :: free_memory
53 procedure :: get_available_memory
54 procedure :: get_available_global_memory
55 generic, public :: construct => basemanager_ctor
56 end type basemanager
57
58 !Declare a static BaseManager for entire program scope
59 type(basemanager) :: memory_man
60
61 !interface BaseManager
62 ! procedure :: BaseManager_ctor
63 !end interface
64
65contains
66
67 !> \brief Type constructor
68 !> \authors A Al-Refaie
69 !> \date 2017
70 !>
71 subroutine basemanager_ctor (this, total_memory)
72 class(basemanager) :: this
73 integer(longint), intent(in) :: total_memory
74
75 this % total_local_memory = total_memory
76 this % available_local_memory = this % total_local_memory
77 call this % construct
78
79 end subroutine
80
81
82 !> \brief Initialize memory
83 !> \authors A Al-Refaie
84 !> \date 2017
85 !>
86 subroutine init_memory (this, total_memory)
87 class(basemanager) :: this
88 integer(longint), intent(in) :: total_memory
89
90 this % total_local_memory = total_memory
91 this % available_local_memory = this % total_local_memory
92
93 end subroutine init_memory
94
95
96 !> \brief Track memory
97 !> \authors A Al-Refaie
98 !> \date 2017
99 !>
100 subroutine track_memory (this, alloc_memory, error)
101 class(basemanager) :: this
102 integer(longint), intent(in) :: alloc_memory
103 integer, intent(out) :: error
104
105 this % available_local_memory = this % available_local_memory - alloc_memory
106
107 if (this % available_local_memory < 0) then
108 error = -1
109 call this % verbose_log_message('Run out of memory')
110 else
111 error = 0
112 end if
113
114 end subroutine track_memory
115
116
117 !> \brief Free memory
118 !> \authors A Al-Refaie
119 !> \date 2017
120 !>
121 subroutine free_memory (this, alloc_memory, error)
122 class(basemanager) :: this
123 integer(longint), intent(in) :: alloc_memory
124 integer, intent(out) :: error
125
126 this % available_local_memory = this % available_local_memory + alloc_memory
127
128 if (this % available_local_memory > this % total_local_memory) then
129 error = -1
130 call this % verbose_log_message('Memory mismatch')
131 else
132 error = 0
133 end if
134
135 end subroutine free_memory
136
137
138 !> \brief Obtain available local memory
139 !> \authors A Al-Refaie
140 !> \date 2017
141 !>
142 integer(longint) function get_available_memory (this)
143 class(basemanager) :: this
144
145 get_available_memory = this % available_local_memory
146
147 end function get_available_memory
148
149
150 !> \brief Obtain available global memory
151 !> \authors A Al-Refaie
152 !> \date 2017
153 !>
154 integer(longint) function get_available_global_memory (this)
155 class(basemanager) :: this
156 integer(longint) :: local(1), global(1)
157
158 local = this % available_local_memory
159 !call mpi_reduceall_inplace_sum_cfp(local,1)
160 get_available_global_memory = local(1)
161
162 end function get_available_global_memory
163
164end module basemanager_module