Skip to content

Commit 277c2a9

Browse files
author
Sergey Oblomov
committed
ALLOC_WITH_HINT: added implace realloc
- in some cases realloc operation may be completed without allocation of new buffer (and without additional data copy) - added logic to reallocate buffer inplace if possible Signed-off-by: Sergey Oblomov <sergeyo@mellanox.com>
1 parent fe5ad67 commit 277c2a9

File tree

4 files changed

+88
-12
lines changed

4 files changed

+88
-12
lines changed

oshmem/mca/spml/ucx/spml_ucx.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,8 @@ static int mca_spml_ucx_ctx_create_common(long options, mca_spml_ucx_ctx_t **ucx
551551
{
552552
ucp_worker_params_t params;
553553
ucp_ep_params_t ep_params;
554-
size_t i, j, nprocs = oshmem_num_procs();
554+
size_t i, nprocs = oshmem_num_procs();
555+
int j;
555556
ucs_status_t err;
556557
spml_ucx_mkey_t *ucx_mkey;
557558
sshmem_mkey_t *mkey;

oshmem/mca/sshmem/ucx/sshmem_ucx.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,19 @@ sshmem_ucx_shadow_allocator_t *sshmem_ucx_shadow_create(unsigned count);
4949
void sshmem_ucx_shadow_destroy(sshmem_ucx_shadow_allocator_t *allocator);
5050
int sshmem_ucx_shadow_alloc(sshmem_ucx_shadow_allocator_t *allocator,
5151
unsigned count, unsigned *index);
52+
53+
/* reallocate existing allocated buffer. if possible - used inplace
54+
* reallocation.
55+
* parameter 'inplace' - out, in case if zero - new buffer was allocated
56+
* (inplace is not possible), user should remove original buffer after data
57+
* is copied, else (if inplace == 0) - no additional action required */
58+
int sshmem_ucx_shadow_realloc(sshmem_ucx_shadow_allocator_t *allocator,
59+
unsigned count, unsigned old_index, unsigned *index,
60+
int *inplace);
5261
int sshmem_ucx_shadow_free(sshmem_ucx_shadow_allocator_t *allocator,
5362
unsigned index);
54-
size_t sshmem_ucx_shadow_size(sshmem_ucx_shadow_allocator_t *allocator,
55-
unsigned index);
63+
unsigned sshmem_ucx_shadow_size(sshmem_ucx_shadow_allocator_t *allocator,
64+
unsigned index);
5665

5766
END_C_DECLS
5867

oshmem/mca/sshmem/ucx/sshmem_ucx_module.c

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,6 @@ static uct_ib_device_mem_h alloc_device_mem(mca_spml_ucx_t *spml, size_t size,
190190
uct_md_h uct_md;
191191
void *address;
192192
size_t length;
193-
int ret;
194193

195194
uct_md = ucp_context_find_tl_md(spml->ucp_context, "mlx5");
196195
if (uct_md == NULL) {
@@ -354,8 +353,9 @@ static int sshmem_ucx_memheap_realloc(map_segment_t *s, size_t size,
354353
void* old_ptr, void** new_ptr)
355354
{
356355
mca_sshmem_ucx_segment_context_t *ctx = s->context;
357-
unsigned alloc_count, index;
356+
unsigned alloc_count, index, old_index, old_alloc_count;
358357
int res;
358+
int inplace;
359359

360360
if (size > s->seg_size) {
361361
return OSHMEM_ERR_OUT_OF_RESOURCE;
@@ -372,18 +372,24 @@ static int sshmem_ucx_memheap_realloc(map_segment_t *s, size_t size,
372372
/* Allocate new element. Zero-size allocation should still return a unique
373373
* pointer, so allocate 1 byte */
374374
alloc_count = max((size + ALLOC_ELEM_SIZE - 1) / ALLOC_ELEM_SIZE, 1);
375-
res = sshmem_ucx_shadow_alloc(ctx->shadow_allocator, alloc_count, &index);
375+
376+
if (!old_ptr) {
377+
res = sshmem_ucx_shadow_alloc(ctx->shadow_allocator, alloc_count, &index);
378+
} else {
379+
old_index = sshmem_ucx_memheap_ptr2index(s, old_ptr);
380+
res = sshmem_ucx_shadow_realloc(ctx->shadow_allocator, alloc_count,
381+
old_index, &index, &inplace);
382+
}
383+
376384
if (res != OSHMEM_SUCCESS) {
377385
return res;
378386
}
379387

380388
*new_ptr = sshmem_ucx_memheap_index2ptr(s, index);
381389

382390
/* Copy to new segment and release old*/
383-
if (old_ptr) {
384-
unsigned old_index = sshmem_ucx_memheap_ptr2index(s, old_ptr);
385-
unsigned old_alloc_count = sshmem_ucx_shadow_size(ctx->shadow_allocator,
386-
old_index);
391+
if (old_ptr && !inplace) {
392+
old_alloc_count = sshmem_ucx_shadow_size(ctx->shadow_allocator, old_index);
387393
sshmem_ucx_memheap_wordcopy(*new_ptr, old_ptr,
388394
min(size, old_alloc_count * ALLOC_ELEM_SIZE));
389395
sshmem_ucx_shadow_free(ctx->shadow_allocator, old_index);

oshmem/mca/sshmem/ucx/sshmem_ucx_shadow.c

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,66 @@ static void sshmem_ucx_shadow_merge_blocks(sshmem_ucx_shadow_allocator_t *alloca
107107
}
108108
}
109109

110+
111+
112+
int sshmem_ucx_shadow_realloc(sshmem_ucx_shadow_allocator_t *allocator,
113+
unsigned count, unsigned old_index, unsigned *index,
114+
int *inplace)
115+
{
116+
sshmem_ucx_shadow_alloc_elem_t *end = &allocator->elems[allocator->num_elems];
117+
sshmem_ucx_shadow_alloc_elem_t *elem = &allocator->elems[old_index];
118+
sshmem_ucx_shadow_alloc_elem_t *next = &elem[elem->block_size];
119+
unsigned old_count = elem->block_size;
120+
121+
assert(count > 0);
122+
assert(!sshmem_ucx_shadow_is_free(elem));
123+
124+
*inplace = 1;
125+
126+
if (count == old_count) {
127+
*index = old_index;
128+
return OSHMEM_SUCCESS;
129+
}
130+
131+
if (count < elem->block_size) {
132+
/* requested block is shorter than allocated block
133+
* then just cut current buffer */
134+
sshmem_ucx_shadow_set_elem(elem + count,
135+
SSHMEM_UCX_SHADOW_ELEM_FLAG_FREE,
136+
elem->block_size - count);
137+
elem->block_size = count;
138+
*index = old_index;
139+
sshmem_ucx_shadow_merge_blocks(allocator);
140+
return OSHMEM_SUCCESS;
141+
}
142+
143+
assert(count > old_count);
144+
145+
/* try to check if next element is free & has enough length */
146+
if ((next < end) && /* non-last element? */
147+
sshmem_ucx_shadow_is_free(next) && /* next is free */
148+
(old_count + next->block_size >= count))
149+
{
150+
assert(elem < next);
151+
assert(elem + count > next);
152+
assert(elem + count <= end);
153+
assert(next + next->block_size <= end);
154+
155+
if (old_count + next->block_size > count) {
156+
sshmem_ucx_shadow_set_elem(elem + count, SSHMEM_UCX_SHADOW_ELEM_FLAG_FREE,
157+
old_count + next->block_size - count);
158+
}
159+
160+
sshmem_ucx_shadow_set_elem(next, 0, 0);
161+
elem->block_size = count;
162+
*index = old_index;
163+
return OSHMEM_SUCCESS;
164+
}
165+
166+
*inplace = 0;
167+
return sshmem_ucx_shadow_alloc(allocator, count, index);
168+
}
169+
110170
int sshmem_ucx_shadow_free(sshmem_ucx_shadow_allocator_t *allocator,
111171
unsigned index)
112172
{
@@ -117,8 +177,8 @@ int sshmem_ucx_shadow_free(sshmem_ucx_shadow_allocator_t *allocator,
117177
return OSHMEM_SUCCESS;
118178
}
119179

120-
size_t sshmem_ucx_shadow_size(sshmem_ucx_shadow_allocator_t *allocator,
121-
unsigned index)
180+
unsigned sshmem_ucx_shadow_size(sshmem_ucx_shadow_allocator_t *allocator,
181+
unsigned index)
122182
{
123183
sshmem_ucx_shadow_alloc_elem_t *elem = &allocator->elems[index];
124184

0 commit comments

Comments
 (0)