Skip to content

Commit b074f86

Browse files
Implement change team and child heap management
We maintain that child teams can use the "remaining" memory from the current team's heap, which requires some addtional overhead during allocation and deallocation of coarrays.
1 parent a450387 commit b074f86

File tree

5 files changed

+84
-30
lines changed

5 files changed

+84
-30
lines changed

src/caffeine/allocation_s.f90

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@
2828
coarray_size = product(ubounds-lbounds+1)*element_size
2929

3030
me = caf_this_image(current_team%info%gex_team)
31+
if (caf_have_child_teams()) then
32+
! Free the child team space to make sure we have space to allocate the coarray
33+
if (me == 1) then
34+
call caf_deallocate(current_team%info%heap_mspace, current_team%info%child_heap_info%allocated_memory)
35+
end if
36+
end if
3137
if (me == 1) then
3238
handle_size = c_sizeof(unused)
3339
total_size = handle_size + coarray_size
@@ -52,6 +58,9 @@
5258
call add_to_team_list(current_team, coarray_handle)
5359

5460
allocated_memory = coarray_handle%info%coarray_data
61+
if (caf_have_child_teams()) then
62+
call caf_establish_child_heap
63+
end if
5564
end procedure
5665

5766
module procedure prif_allocate
@@ -118,6 +127,13 @@
118127
call caf_deallocate(current_team%info%heap_mspace, c_loc(coarray_handles(i)%info))
119128
end do
120129
if (present(stat)) stat = 0
130+
if (caf_have_child_teams()) then
131+
! reclaim any free space possible for the child teams to use
132+
if (caf_this_image(current_team%info%gex_team) == 1) then
133+
call caf_deallocate(current_team%info%heap_mspace, current_team%info%child_heap_info%allocated_memory)
134+
end if
135+
call caf_establish_child_heap
136+
end if
121137
end procedure
122138

123139
module procedure prif_deallocate

src/caffeine/caffeine.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,28 @@ void* caf_allocate(mspace heap, size_t bytes)
104104
return mspace_memalign(heap, 8, bytes);
105105
}
106106

107+
void* caf_allocate_remaining(mspace heap, void** allocated_space, size_t* allocated_size)
108+
{
109+
// The following doesn't necessarily give us all remaining space
110+
// nor necessarily the largest open space, but in practice is likely
111+
// to work out that way
112+
struct mallinfo heap_info = mspace_mallinfo(heap);
113+
*allocated_size = heap_info.keepcost;
114+
115+
*allocated_space = mspace_memalign(heap, 8, *allocated_size);
116+
}
117+
107118
void caf_deallocate(mspace heap, void* mem)
108119
{
109120
mspace_free(heap, mem);
110121
}
111122

123+
void caf_establish_mspace(mspace* heap, void* heap_start, size_t heap_size)
124+
{
125+
*heap = create_mspace_with_base(heap_start, heap_size, 0);
126+
mspace_set_footprint_limit(*heap, heap_size);
127+
}
128+
112129
// take address in a segment and convert to an address on given image
113130
intptr_t caf_convert_base_addr(void* addr, int image)
114131
{

src/caffeine/prif_private_s.f90

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,29 @@ function caf_allocate(mspace, bytes) result(ptr) bind(c)
6868
type(c_ptr) :: ptr
6969
end function
7070

71+
subroutine caf_allocate_remaining(mspace, allocated_space, allocated_size) bind(c)
72+
import c_size_t, c_ptr
73+
implicit none
74+
type(c_ptr), intent(in), value :: mspace
75+
type(c_ptr), intent(out) :: allocated_space
76+
integer(c_size_t), intent(out) :: allocated_size
77+
end subroutine
78+
7179
subroutine caf_deallocate(mspace, mem) bind(c)
7280
import c_ptr
7381
implicit none
7482
type(c_ptr), intent(in), value :: mspace
7583
type(c_ptr), intent(in), value :: mem
7684
end subroutine
7785

86+
subroutine caf_establish_mspace(mspace, mem, mem_size)
87+
import c_size_t, c_ptr
88+
implicit none
89+
type(c_ptr), intent(out) :: mspace
90+
type(c_ptr), intent(in), value :: mem
91+
integer(c_size_t), intent(in), value :: mem_size
92+
end subroutine
93+
7894
! ___________________ PRIF Queries ______________________
7995

8096
module function caf_convert_base_addr(addr, image) result(ptr) bind(c)
@@ -253,4 +269,21 @@ pure function optional_value(var) result(c_val)
253269
end if
254270
end function
255271

272+
subroutine caf_establish_child_heap
273+
if (caf_this_image(current_team%info%gex_team) == 1) then
274+
call caf_allocate_remaining( &
275+
current_team%info%heap_mspace, &
276+
current_team%info%child_heap_info%allocated_memory, &
277+
current_team%info%child_heap_info%size)
278+
current_team%info%child_heap_info%offset = &
279+
as_int(current_team%info%child_heap_info%allocated_memory) - current_team%info%heap_start
280+
end if
281+
call prif_co_broadcast(current_team%info%child_heap_info%offset, 1)
282+
call prif_co_broadcast(current_team%info%child_heap_info%size, 1)
283+
end subroutine
284+
285+
logical function caf_have_child_teams()
286+
caf_have_child_teams = associated(current_team%info%child_heap_info)
287+
end function
288+
256289
end submodule prif_private_s

src/caffeine/teams_s.f90

Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,35 @@
11
! Copyright (c), The Regents of the University of California
22
! Terms of use are as specified in LICENSE.txt
33
submodule(prif:prif_private_s) teams_s
4-
use iso_c_binding, only: c_null_funptr, c_f_pointer
4+
use iso_c_binding, only: c_null_funptr, c_f_pointer, c_loc
55

66
implicit none
77
contains
88

99
module procedure prif_change_team
10-
call unimplemented("prif_change_team")
10+
if (caf_this_image(team%info%gex_team) == 1) then ! need to setup the heap for the team
11+
team%info%heap_start = current_team%info%child_heap_info%offset + current_team%info%heap_start
12+
team%info%heap_size = current_team%info%child_heap_info%size
13+
call caf_establish_mspace( &
14+
team%info%heap_mspace, &
15+
as_c_ptr(team%info%heap_start), &
16+
current_team%info%child_heap_info%size)
17+
end if
18+
current_team = team
19+
if (caf_have_child_teams()) then ! need to establish heap for child teams
20+
call caf_establish_child_heap
21+
end if
1122
end procedure
1223

1324
module procedure prif_end_team
1425
call unimplemented("prif_end_team")
1526
end procedure
1627

1728
module procedure prif_form_team
18-
if (.not.associated(current_team%info%child_heap_info)) then
19-
block
20-
type(c_ptr) :: allocated_memory
21-
integer :: num_imgs
22-
type(child_team_info) :: dummy_element
23-
24-
call prif_num_images(num_images = num_imgs)
25-
call prif_allocate_coarray( &
26-
lcobounds = [1_c_intmax_t], &
27-
ucobounds = [int(num_imgs, c_intmax_t)], &
28-
lbounds = [integer(c_intmax_t)::], &
29-
ubounds = [integer(c_intmax_t)::], &
30-
element_size = int(storage_size(dummy_element)/8, c_size_t), &
31-
final_func = c_null_funptr, &
32-
coarray_handle = current_team%info%child_team_handle, &
33-
allocated_memory = allocated_memory)
34-
call c_f_pointer(allocated_memory, current_team%info%child_heap_info)
35-
36-
if (caf_this_image(current_team%info%gex_team) == 1) then
37-
! TODO: Take up remaining space and adjust during (de)allocations
38-
current_team%info%child_heap_info%size = int(current_team%info%heap_size * 0.1d0, c_size_t)
39-
current_team%info%child_heap_info%offset = &
40-
as_int(caf_allocate( &
41-
current_team%info%heap_mspace, current_team%info%child_heap_info%size)) &
42-
- current_team%info%heap_start
43-
end if
44-
end block
29+
! indicates this is the first time we're creating a child team
30+
if (.not.caf_have_child_teams()) then
31+
allocate(current_team%info%child_heap_info)
32+
call caf_establish_child_heap
4533
end if
4634

4735
block

src/prif.f90

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1034,11 +1034,11 @@ module subroutine prif_atomic_ref_logical_indirect(image_num, atom_remote_ptr, v
10341034
integer(c_size_t) :: heap_size
10351035
type(team_data), pointer :: parent_team => null()
10361036
type(handle_data), pointer :: coarrays => null()
1037-
type(prif_coarray_handle) :: child_team_handle
10381037
type(child_team_info), pointer :: child_heap_info => null()
10391038
end type
10401039

10411040
type :: child_team_info
1041+
type(c_ptr) :: allocated_memory
10421042
integer(c_ptrdiff_t) :: offset
10431043
integer(c_size_t) :: size
10441044
end type

0 commit comments

Comments
 (0)