Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"new" memory allocator for LPs memory: DyMeLoR! #85

Draft
wants to merge 8 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ if(WIN32)
set(EXTRA_LIBS ${EXTRA_LIBS} psapi)
endif()

set(want_dymelor TRUE)
if(${want_dymelor}) # FIXME: super bad way of selecting dymelor at build time
add_compile_definitions(ROOTSIM_DYMELOR)
endif()

add_subdirectory(src)

# Run the tests
Expand Down
14 changes: 11 additions & 3 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,21 @@ set(rscore_srcs
lp/lp.c
lp/process.c
mm/auto_ckpt.c
mm/buddy/buddy.c
mm/buddy/ckpt.c
mm/buddy/multi.c
mm/msg_allocator.c
parallel/parallel.c
serial/serial.c)

if(${want_dymelor}) # FIXME: super bad way of selecting dymelor at build time
set(rscore_srcs ${rscore_srcs}
mm/dymelor/checkpoint.c
mm/dymelor/dymelor.c)
else()
set(rscore_srcs ${rscore_srcs}
mm/buddy/buddy.c
mm/buddy/ckpt.c
mm/buddy/multi.c)
endif()

if(NOT DISABLE_MPI)
set(rscore_srcs ${rscore_srcs} distributed/mpi.c)
else()
Expand Down
109 changes: 109 additions & 0 deletions src/mm/dymelor/checkpoint.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
#include <mm/dymelor/checkpoint.h>

#include <core/core.h>
#include <mm/dymelor/dymelor.h>
#include <mm/mm.h>

#include <stdio.h>

static size_t compute_log_size(const struct mm_state *ctx)
{
size_t ret = sizeof(offsetof(struct dymelor_ctx_checkpoint, data));
for(unsigned i = 0; i < NUM_AREAS; ++i) {
const struct dymelor_area *area = ctx->areas[i];
uint_least32_t num_chunks = MIN_NUM_CHUNKS;
uint_least32_t chunk_size = (1U << (MIN_CHUNK_EXP + i)) - sizeof(uint_least32_t);
while(area != NULL) {
ret += sizeof(offsetof(struct dymelor_area_checkpoint, data));
ret += bitmap_required_size(num_chunks);
#ifdef ROOTSIM_INCREMENTAL
ret += bitmap_required_size(num_chunks); // maybe not needed in full checkpoints
#endif
ret += chunk_size * area->alloc_chunks;
Fixed Show fixed Hide fixed
num_chunks *= 2;
area = area->next;
}
}
return ret;
}

struct dymelor_ctx_checkpoint *checkpoint_full_take(const struct mm_state *ctx)
{
struct dymelor_ctx_checkpoint *ckpt = mm_alloc(compute_log_size(ctx));
unsigned char * restrict ptr = ckpt->data;
ckpt->area_cnt = 0;
for(unsigned i = 0; i < NUM_AREAS; ++i) {
const struct dymelor_area *area = ctx->areas[i];
uint_least32_t num_chunks = MIN_NUM_CHUNKS;
uint_least32_t chunk_size = (1U << (MIN_CHUNK_EXP + i)) - sizeof(uint_least32_t);
while(area != NULL) {
struct dymelor_area_checkpoint *ackpt = (struct dymelor_area_checkpoint *)ptr;
ackpt->i = i;
ackpt->chunk_cnt = area->alloc_chunks;
size_t bitmap_size = bitmap_required_size(num_chunks);
memcpy(ackpt->data, area->use_bitmap, bitmap_size);

Check notice

Code scanning / Flawfinder (reported by Codacy)

Does not check for buffer overflows when copying to destination (CWE-120). Make sure destination can always hold the source data.

Does not check for buffer overflows when copying to destination (CWE-120). Make sure destination can always hold the source data.
ptr = ackpt->data + bitmap_size;

#define copy_from_area(x) \
({ \
memcpy(ptr, area->area + ((x) * (chunk_size + sizeof(uint_least32_t))), chunk_size); \

Check notice

Code scanning / Flawfinder (reported by Codacy)

Does not check for buffer overflows when copying to destination (CWE-120). Make sure destination can always hold the source data.

Does not check for buffer overflows when copying to destination (CWE-120). Make sure destination can always hold the source data.
ptr += chunk_size; \
})

// Copy only the allocated chunks
bitmap_foreach_set(area->use_bitmap, bitmap_size, copy_from_area);

#undef copy_from_area

++ckpt->area_cnt;
num_chunks *= 2;
area = area->next;
}
}

return ckpt;
}

void checkpoint_full_restore(struct mm_state *ctx, const struct dymelor_ctx_checkpoint *ckpt)
{
const unsigned char *ptr = ckpt->data;
unsigned last_i = UINT_MAX;
struct dymelor_area *area = NULL;
uint_least32_t num_chunks, chunk_size;
unsigned j = ckpt->area_cnt;
while(j--) {
struct dymelor_area_checkpoint *ackpt = (struct dymelor_area_checkpoint *)ptr;
if(last_i != ackpt->i) {
if (area != NULL)
while(unlikely(area->next != NULL)) {
area = area->next;
num_chunks *= 2;
memset(area->use_bitmap, 0, bitmap_required_size(num_chunks));
area->alloc_chunks = 0;
}

num_chunks = MIN_NUM_CHUNKS;
last_i = ackpt->i;
chunk_size = (1U << (MIN_CHUNK_EXP + last_i)) - sizeof(uint_least32_t);
area = ctx->areas[last_i];
} else {
num_chunks *= 2;
area = area->next;
}

area->alloc_chunks = ackpt->chunk_cnt;
size_t bitmap_size = bitmap_required_size(num_chunks);
memcpy(area->use_bitmap, ackpt->data, bitmap_size);

Check notice

Code scanning / Flawfinder (reported by Codacy)

Does not check for buffer overflows when copying to destination (CWE-120). Make sure destination can always hold the source data.

Does not check for buffer overflows when copying to destination (CWE-120). Make sure destination can always hold the source data.
ptr = ackpt->data + bitmap_size;

#define copy_to_area(x) \
({ \
memcpy(area->area + ((x) * (chunk_size + sizeof(uint_least32_t))), ptr, chunk_size); \

Check notice

Code scanning / Flawfinder (reported by Codacy)

Does not check for buffer overflows when copying to destination (CWE-120). Make sure destination can always hold the source data.

Does not check for buffer overflows when copying to destination (CWE-120). Make sure destination can always hold the source data.
ptr += chunk_size; \
})

bitmap_foreach_set(area->use_bitmap, bitmap_size, copy_to_area);

#undef copy_to_area
}
}
19 changes: 19 additions & 0 deletions src/mm/dymelor/checkpoint.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#pragma once

#include <mm/dymelor/dymelor.h>

#include <stdint.h>

struct dymelor_ctx_checkpoint {
unsigned area_cnt;
unsigned char data[];
};

struct dymelor_area_checkpoint {
unsigned i;
uint_least32_t chunk_cnt;
unsigned char data[];
};

extern struct dymelor_ctx_checkpoint *checkpoint_full_take(const struct mm_state *ctx);
extern void checkpoint_full_restore(struct mm_state *ctx, const struct dymelor_ctx_checkpoint *ckpt);
Loading