50 integer(kind=1),
pointer :: mem(:)
73 character(len=*),
intent(in) :: env_var
74 logical,
intent(in) :: default_val
75 character(len=255) :: env_str
77 call get_environment_variable(env_var, env_str)
78 if (len_trim(env_str) > 0)
then
79 select case (trim(env_str))
80 case (
"1",
"ON",
"TRUE",
"on",
"true")
82 case (
"0",
"OFF",
"FALSE",
"off",
"false")
90 integer,
intent(in) :: mn(:)
91 integer,
intent(in) :: mx(:)
93 integer(kind=8) :: n_elem
96 n_elem = mx(i) - mn(i) + 1
98 n_elem = n_elem*(mx(i) - mn(i) + 1)
123 integer(kind=1),
pointer :: f_pointer(:)
124 integer(kind=8),
intent(in) :: n_bytes
128 if (.not.
pool(it)%slice(j)%acquired)
then
133 if (
associated(
pool(it)%slice(j)%mem))
then
134 if (
size(
pool(it)%slice(j)%mem) < n_bytes)
then
135 deallocate (
pool(it)%slice(j)%mem)
136 allocate (
pool(it)%slice(j)%mem(n_bytes))
139 allocate (
pool(it)%slice(j)%mem(n_bytes))
141 pool(it)%slice(j)%acquired = .true.
142 f_pointer =>
pool(it)%slice(j)%mem
146 type(c_ptr) :: c_pointer
150 if (c_associated(c_loc(
pool(it)%slice(j)%mem), c_pointer))
then
155 pool(it)%slice(j)%acquired = .false.
157 deallocate (
pool(it)%slice(j)%mem)
158 nullify (
pool(it)%slice(j)%mem)
165 subroutine mp_init(min_threads, verbosity, disable)
166 integer,
intent(in),
optional :: min_threads
167 logical,
intent(in),
optional :: verbosity
168 logical,
intent(in),
optional :: disable
170 character(len=32) :: disabled_str
171 if (
present(min_threads))
then
180 allocate (
pool(0:nt - 1))
184 pool(i)%slice(j)%acquired = .false.
185 nullify (
pool(i)%slice(j)%mem)
188 if (
present(verbosity))
then
193 if (
present(disable))
then
199 disabled_str =
", disabled"
205 write (*, *)
"mempool: initialized (OpenMP)"//trim(adjustl(disabled_str))
207 write (*, *)
"mempool: initialized (not threaded)"//trim(adjustl(disabled_str))
218 if (
associated(
pool(i)%slice(j)%mem))
then
219 deallocate (
pool(i)%slice(j)%mem)
220 nullify (
pool(i)%slice(j)%mem)
222 pool(i)%slice(j)%acquired = .false.
224 deallocate (
pool(i)%slice)
225 nullify (
pool(i)%slice)
230 write (*, *)
"mempool: finalized"
236 integer :: i, j, it, nt
242 if ((.not.
pool(i)%slice(j)%acquired) .and. (
associated(
pool(i)%slice(j)%mem)))
then
243 deallocate (
pool(i)%slice(j)%mem)
244 nullify (
pool(i)%slice(j)%mem)
253 integer :: i, j, it, nt, n_slices, n_acquired, n_allocated
254 integer(kind=8) :: n_bytes
255 character(len=32) :: thread_str, acquired_str, slices_str, bytes_str, alloc_str
258 if ((
verbose) .and. (it == 0))
then
259 write (*, *)
"mempool statistics"
265 if (
pool(i)%slice(j)%acquired)
then
266 n_acquired = n_acquired + 1
268 if (
associated(
pool(i)%slice(j)%mem))
then
269 n_allocated = n_allocated + 1
270 n_bytes = n_bytes +
size(
pool(i)%slice(j)%mem)
274 write (thread_str, *) i
276 write (acquired_str, *) n_acquired
277 write (bytes_str, *) n_bytes
278 write (alloc_str, *) n_allocated
279 write (*,
'(A)')
" "// &
280 "pool["//trim(adjustl(thread_str))//
"]: "// &
281 "n_slices="//trim(adjustl(slices_str))//
", "// &
282 "n_acquired="//trim(adjustl(acquired_str))//
", "// &
283 "n_allocated="//trim(adjustl(alloc_str))//
", "// &
284 "n_bytes="//trim(adjustl(bytes_str))
300 double precision,
pointer :: f_pointer(:)
301 integer,
intent(in) :: mn(1)
302 integer,
intent(in) :: mx(1)
303 integer(kind=8) :: n_bytes, n_elem
304 integer(kind=1),
pointer :: ptr(:)
305 double precision,
pointer :: flat_ptr(:)
311 call c_f_pointer(c_loc(ptr), flat_ptr, [n_elem])
312 f_pointer(mn(1):mx(1)) => flat_ptr
317 double precision,
pointer :: f_pointer(:)
324 double precision,
pointer :: f_pointer(:, :)
325 integer,
intent(in) :: mn(2)
326 integer,
intent(in) :: mx(2)
327 integer(kind=8) :: n_bytes, n_elem
328 integer(kind=1),
pointer :: ptr(:)
329 double precision,
pointer :: flat_ptr(:)
335 call c_f_pointer(c_loc(ptr), flat_ptr, [n_elem])
336 f_pointer(mn(1):mx(1), &
337 mn(2):mx(2)) => flat_ptr
342 double precision,
pointer :: f_pointer(:, :)
349 double precision,
pointer :: f_pointer(:, :, :)
350 integer,
intent(in) :: mn(3)
351 integer,
intent(in) :: mx(3)
352 integer(kind=8) :: n_bytes, n_elem
353 integer(kind=1),
pointer :: ptr(:)
354 double precision,
pointer :: flat_ptr(:)
360 call c_f_pointer(c_loc(ptr), flat_ptr, [n_elem])
361 f_pointer(mn(1):mx(1), &
363 mn(3):mx(3)) => flat_ptr
368 double precision,
pointer :: f_pointer(:, :, :)
375 double precision,
pointer :: f_pointer(:, :, :, :, :, :)
376 integer,
intent(in) :: mn(6)
377 integer,
intent(in) :: mx(6)
378 integer(kind=8) :: n_bytes, n_elem
379 integer(kind=1),
pointer :: ptr(:)
380 double precision,
pointer :: flat_ptr(:)
386 call c_f_pointer(c_loc(ptr), flat_ptr, [n_elem])
387 f_pointer(mn(1):mx(1), &
392 mn(6):mx(6)) => flat_ptr
397 double precision,
pointer :: f_pointer(:, :, :, :, :, :)
406 real,
pointer :: f_pointer(:)
407 integer,
intent(in) :: mn(1)
408 integer,
intent(in) :: mx(1)
409 integer(kind=8) :: n_bytes, n_elem
410 integer(kind=1),
pointer :: ptr(:)
411 real,
pointer :: flat_ptr(:)
417 call c_f_pointer(c_loc(ptr), flat_ptr, [n_elem])
418 f_pointer(mn(1):mx(1)) => flat_ptr
423 real,
pointer :: f_pointer(:)
430 real,
pointer :: f_pointer(:, :)
431 integer,
intent(in) :: mn(2)
432 integer,
intent(in) :: mx(2)
433 integer(kind=8) :: n_bytes, n_elem
434 integer(kind=1),
pointer :: ptr(:)
435 real,
pointer :: flat_ptr(:)
441 call c_f_pointer(c_loc(ptr), flat_ptr, [n_elem])
442 f_pointer(mn(1):mx(1), &
443 mn(2):mx(2)) => flat_ptr
448 real,
pointer :: f_pointer(:, :)
455 real,
pointer :: f_pointer(:, :, :)
456 integer,
intent(in) :: mn(3)
457 integer,
intent(in) :: mx(3)
458 integer(kind=8) :: n_bytes, n_elem
459 integer(kind=1),
pointer :: ptr(:)
460 real,
pointer :: flat_ptr(:)
466 call c_f_pointer(c_loc(ptr), flat_ptr, [n_elem])
467 f_pointer(mn(1):mx(1), &
469 mn(3):mx(3)) => flat_ptr
474 real,
pointer :: f_pointer(:, :, :)
481 real,
pointer :: f_pointer(:, :, :, :, :, :)
482 integer,
intent(in) :: mn(6)
483 integer,
intent(in) :: mx(6)
484 integer(kind=8) :: n_bytes, n_elem
485 integer(kind=1),
pointer :: ptr(:)
486 real,
pointer :: flat_ptr(:)
492 call c_f_pointer(c_loc(ptr), flat_ptr, [n_elem])
493 f_pointer(mn(1):mx(1), &
498 mn(6):mx(6)) => flat_ptr
503 real,
pointer :: f_pointer(:, :, :, :, :, :)
512 integer,
pointer :: f_pointer(:)
513 integer,
intent(in) :: mn(1)
514 integer,
intent(in) :: mx(1)
515 integer(kind=8) :: n_bytes, n_elem
516 integer(kind=1),
pointer :: ptr(:)
517 integer,
pointer :: flat_ptr(:)
523 call c_f_pointer(c_loc(ptr), flat_ptr, [n_elem])
524 f_pointer(mn(1):mx(1)) => flat_ptr
529 integer,
pointer :: f_pointer(:)
536 integer,
pointer :: f_pointer(:, :)
537 integer,
intent(in) :: mn(2)
538 integer,
intent(in) :: mx(2)
539 integer(kind=8) :: n_bytes, n_elem
540 integer(kind=1),
pointer :: ptr(:)
541 integer,
pointer :: flat_ptr(:)
547 call c_f_pointer(c_loc(ptr), flat_ptr, [n_elem])
548 f_pointer(mn(1):mx(1), &
549 mn(2):mx(2)) => flat_ptr
554 integer,
pointer :: f_pointer(:, :)
561 integer,
pointer :: f_pointer(:, :, :)
562 integer,
intent(in) :: mn(3)
563 integer,
intent(in) :: mx(3)
564 integer(kind=8) :: n_bytes, n_elem
565 integer(kind=1),
pointer :: ptr(:)
566 integer,
pointer :: flat_ptr(:)
572 call c_f_pointer(c_loc(ptr), flat_ptr, [n_elem])
573 f_pointer(mn(1):mx(1), &
575 mn(3):mx(3)) => flat_ptr
580 integer,
pointer :: f_pointer(:, :, :)
587 integer,
pointer :: f_pointer(:, :, :, :, :, :)
588 integer,
intent(in) :: mn(6)
589 integer,
intent(in) :: mx(6)
590 integer(kind=8) :: n_bytes, n_elem
591 integer(kind=1),
pointer :: ptr(:)
592 integer,
pointer :: flat_ptr(:)
598 call c_f_pointer(c_loc(ptr), flat_ptr, [n_elem])
599 f_pointer(mn(1):mx(1), &
604 mn(6):mx(6)) => flat_ptr
609 integer,
pointer :: f_pointer(:, :, :, :, :, :)
Plain Fortran implementation of a memory pool.
integer function get_omp_thread_idx()
integer(kind=8) function get_n_elem(mn, mx)
subroutine mp_release_int_6d(f_pointer)
subroutine, public mp_compactify()
subroutine mp_acquire_double_2d(f_pointer, mn, mx)
subroutine mp_acquire_int_3d(f_pointer, mn, mx)
subroutine mp_acquire_real_1d(f_pointer, mn, mx)
subroutine mp_release_bytes(c_pointer)
subroutine mp_release_double_1d(f_pointer)
subroutine mp_release_real_1d(f_pointer)
subroutine mp_release_double_2d(f_pointer)
type(mempool), dimension(:), pointer, save pool
subroutine, public mp_init(min_threads, verbosity, disable)
integer, parameter double_size
subroutine mp_release_double_6d(f_pointer)
subroutine mp_release_real_3d(f_pointer)
logical function, public mp_disabled()
logical function query_environment(env_var, default_val)
subroutine mp_acquire_int_2d(f_pointer, mn, mx)
subroutine, public mp_statistics()
subroutine mp_release_double_3d(f_pointer)
subroutine mp_release_real_6d(f_pointer)
subroutine mp_acquire_real_3d(f_pointer, mn, mx)
subroutine mp_acquire_int_6d(f_pointer, mn, mx)
integer, parameter n_max_slices
subroutine mp_acquire_bytes(f_pointer, n_bytes)
integer, parameter real_size
subroutine mp_release_int_3d(f_pointer)
integer, parameter int_size
subroutine mp_acquire_double_3d(f_pointer, mn, mx)
subroutine mp_acquire_int_1d(f_pointer, mn, mx)
subroutine mp_acquire_double_1d(f_pointer, mn, mx)
subroutine mp_acquire_double_6d(f_pointer, mn, mx)
subroutine mp_acquire_real_6d(f_pointer, mn, mx)
subroutine mp_release_int_1d(f_pointer)
subroutine mp_acquire_real_2d(f_pointer, mn, mx)
subroutine, public mp_finalize()
integer function get_omp_world_size()
subroutine mp_release_int_2d(f_pointer)
subroutine mp_release_real_2d(f_pointer)