Skip to content

Commit

Permalink
DBM: Add asserts after malloc and realloc
Browse files Browse the repository at this point in the history
  • Loading branch information
oschuett committed Oct 2, 2024
1 parent 0dde330 commit c52757e
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/dbm/dbm_distribution.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ static void dbm_dist_1d_new(dbm_dist_1d_t *dist, const int length,
dist->nranks = dbm_mpi_comm_size(comm);
dist->length = length;
dist->index2coord = malloc(length * sizeof(int));
assert(dist->index2coord != NULL);
memcpy(dist->index2coord, coords, length * sizeof(int));

// Check that cart coordinates and ranks are equivalent.
Expand All @@ -47,6 +48,7 @@ static void dbm_dist_1d_new(dbm_dist_1d_t *dist, const int length,

// Store local rows/columns.
dist->local_indicies = malloc(dist->nlocals * sizeof(int));
assert(dist->local_indicies != NULL);
int j = 0;
for (int i = 0; i < length; i++) {
if (coords[i] == dist->my_rank) {
Expand Down
2 changes: 2 additions & 0 deletions src/dbm/dbm_library.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ void dbm_library_init(void) {

max_threads = omp_get_max_threads();
per_thread_counters = malloc(max_threads * sizeof(int64_t *));
assert(per_thread_counters != NULL);

// Using parallel regions to ensure memory is allocated near a thread's core.
#pragma omp parallel default(none) shared(per_thread_counters) \
Expand All @@ -49,6 +50,7 @@ void dbm_library_init(void) {
const int ithread = omp_get_thread_num();
const size_t counters_size = DBM_NUM_COUNTERS * sizeof(int64_t);
per_thread_counters[ithread] = malloc(counters_size);
assert(per_thread_counters[ithread] != NULL);
memset(per_thread_counters[ithread], 0, counters_size);
}

Expand Down
5 changes: 5 additions & 0 deletions src/dbm/dbm_matrix.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,24 @@ void dbm_create(dbm_matrix_t **matrix_out, dbm_distribution_t *dist,

size_t size = (strlen(name) + 1) * sizeof(char);
matrix->name = malloc(size);
assert(matrix->name != NULL);
memcpy(matrix->name, name, size);

matrix->nrows = nrows;
matrix->ncols = ncols;

size = nrows * sizeof(int);
matrix->row_sizes = malloc(size);
assert(matrix->row_sizes != NULL);
memcpy(matrix->row_sizes, row_sizes, size);

size = ncols * sizeof(int);
matrix->col_sizes = malloc(size);
assert(matrix->col_sizes != NULL);
memcpy(matrix->col_sizes, col_sizes, size);

matrix->shards = malloc(dbm_get_num_shards(matrix) * sizeof(dbm_shard_t));
assert(matrix->shards != NULL);
#pragma omp parallel for
for (int ishard = 0; ishard < dbm_get_num_shards(matrix); ishard++) {
dbm_shard_init(&matrix->shards[ishard]);
Expand Down Expand Up @@ -427,6 +431,7 @@ void dbm_iterator_start(dbm_iterator_t **iter_out, const dbm_matrix_t *matrix) {
assert(omp_get_num_threads() == omp_get_max_threads() &&
"Please call dbm_iterator_start within an OpenMP parallel region.");
dbm_iterator_t *iter = malloc(sizeof(dbm_iterator_t));
assert(iter != NULL);
iter->matrix = matrix;
iter->next_block = 0;
iter->next_shard = omp_get_thread_num();
Expand Down
1 change: 1 addition & 0 deletions src/dbm/dbm_mempool.c
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ static void *internal_mempool_malloc(const size_t size, const bool on_device) {
// If no chunk was found, allocate a new one.
if (chunk == NULL) {
chunk = malloc(sizeof(dbm_memchunk_t));
assert(chunk != NULL);
chunk->on_device = on_device;
chunk->size = 0;
chunk->mem = NULL;
Expand Down
3 changes: 3 additions & 0 deletions src/dbm/dbm_miniapp.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ static dbm_matrix_t *create_some_matrix(const int nrows, const int ncols,
// Create distribution.
int *row_dist = malloc(nrows * sizeof(int));
int *col_dist = malloc(ncols * sizeof(int));
assert(row_dist != NULL && col_dist != NULL);
for (int i = 0; i < nrows; i++) {
row_dist[i] = i % cart_dims[0];
}
Expand All @@ -65,6 +66,7 @@ static dbm_matrix_t *create_some_matrix(const int nrows, const int ncols,
// Create matrix.
int *row_sizes = malloc(nrows * sizeof(int));
int *col_sizes = malloc(ncols * sizeof(int));
assert(row_sizes != NULL && col_sizes != NULL);
for (int i = 0; i < nrows; i++) {
row_sizes[i] = row_size;
}
Expand Down Expand Up @@ -103,6 +105,7 @@ static void reserve_all_blocks(dbm_matrix_t *matrix) {
}
int *reserve_row = malloc(nblocks * sizeof(int));
int *reserve_col = malloc(nblocks * sizeof(int));
assert(reserve_row != NULL && reserve_col != NULL);
int iblock = 0;
#pragma omp for collapse(2)
for (int row = 0; row < nrows; row++) {
Expand Down
1 change: 1 addition & 0 deletions src/dbm/dbm_multiply.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ static float *compute_rows_max_eps(const bool trans, const dbm_matrix_t *matrix,
const int nrows = (trans) ? matrix->ncols : matrix->nrows;
int *nblocks_per_row = calloc(nrows, sizeof(int));
float *row_max_eps = malloc(nrows * sizeof(float));
assert(row_max_eps != NULL);

#pragma omp parallel
{
Expand Down
4 changes: 4 additions & 0 deletions src/dbm/dbm_multiply_comm.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ static void create_pack_plans(const bool trans_matrix, const bool trans_dist,
#pragma omp for
for (int ipack = 0; ipack < npacks; ipack++) {
plans_per_pack[ipack] = malloc(nblks_per_pack[ipack] * sizeof(plan_t));
assert(plans_per_pack[ipack] != NULL);
}

// 2nd pass: Plan where to send each block.
Expand Down Expand Up @@ -272,6 +273,7 @@ static void postprocess_received_blocks(
memset(nblocks_per_shard, 0, nshards * sizeof(int));
dbm_pack_block_t *blocks_tmp =
malloc(nblocks_recv * sizeof(dbm_pack_block_t));
assert(blocks_tmp != NULL);

#pragma omp parallel
{
Expand Down Expand Up @@ -346,6 +348,7 @@ static dbm_packed_matrix_t pack_matrix(const bool trans_matrix,
packed.dist_ticks = dist_ticks;
packed.nsend_packs = nsend_packs;
packed.send_packs = malloc(nsend_packs * sizeof(dbm_pack_t));
assert(packed.send_packs != NULL);

// Plan all packs.
plan_t *plans_per_pack[nsend_packs];
Expand Down Expand Up @@ -524,6 +527,7 @@ dbm_comm_iterator_t *dbm_comm_iterator_start(const bool transa,
const dbm_matrix_t *matrix_c) {

dbm_comm_iterator_t *iter = malloc(sizeof(dbm_comm_iterator_t));
assert(iter != NULL);
iter->dist = matrix_c->dist;

// During each communication tick we'll fetch a pack_a and pack_b.
Expand Down
1 change: 1 addition & 0 deletions src/dbm/dbm_multiply_gpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ void dbm_multiply_gpu_start(const int max_batch_size, const int nshards,

// Allocate and upload shards of result matrix C.
ctx->shards_c_dev = malloc(nshards * sizeof(dbm_shard_gpu_t));
assert(ctx->shards_c_dev != NULL);
for (int i = 0; i < nshards; i++) {
const dbm_shard_t *shard_c_host = &ctx->shards_c_host[i];
dbm_shard_gpu_t *shard_c_dev = &ctx->shards_c_dev[i];
Expand Down
15 changes: 11 additions & 4 deletions src/dbm/dbm_shard.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ static void hashtable_init(dbm_shard_t *shard) {
shard->hashtable_mask = shard->hashtable_size - 1;
shard->hashtable_prime = next_prime(shard->hashtable_size);
shard->hashtable = calloc(shard->hashtable_size, sizeof(int));
assert(shard->hashtable != NULL);
}

/*******************************************************************************
Expand All @@ -64,11 +65,13 @@ void dbm_shard_init(dbm_shard_t *shard) {
shard->nblocks = 0;
shard->nblocks_allocated = INITIAL_NBLOCKS_ALLOCATED;
shard->blocks = malloc(shard->nblocks_allocated * sizeof(dbm_block_t));
assert(shard->blocks != NULL);
hashtable_init(shard);
shard->data_size = 0;
shard->data_promised = 0;
shard->data_allocated = INITIAL_DATA_ALLOCATED;
shard->data = malloc(shard->data_allocated * sizeof(double));
assert(shard->data != NULL);

omp_init_lock(&shard->lock);
}
Expand All @@ -82,6 +85,7 @@ void dbm_shard_copy(dbm_shard_t *shard_a, const dbm_shard_t *shard_b) {
shard_a->nblocks = shard_b->nblocks;
shard_a->nblocks_allocated = shard_b->nblocks_allocated;
shard_a->blocks = malloc(shard_b->nblocks_allocated * sizeof(dbm_block_t));
assert(shard_a->blocks != NULL);
memcpy(shard_a->blocks, shard_b->blocks,
shard_b->nblocks * sizeof(dbm_block_t));

Expand All @@ -90,12 +94,14 @@ void dbm_shard_copy(dbm_shard_t *shard_a, const dbm_shard_t *shard_b) {
shard_a->hashtable_mask = shard_b->hashtable_mask;
shard_a->hashtable_prime = shard_b->hashtable_prime;
shard_a->hashtable = malloc(shard_b->hashtable_size * sizeof(int));
assert(shard_a->hashtable != NULL);
memcpy(shard_a->hashtable, shard_b->hashtable,
shard_b->hashtable_size * sizeof(int));

free(shard_a->data);
shard_a->data_allocated = shard_b->data_allocated;
shard_a->data = malloc(shard_b->data_allocated * sizeof(double));
assert(shard_a->data != NULL);
shard_a->data_size = shard_b->data_size;
memcpy(shard_a->data, shard_b->data, shard_b->data_size * sizeof(double));
}
Expand Down Expand Up @@ -173,8 +179,9 @@ dbm_block_t *dbm_shard_promise_new_block(dbm_shard_t *shard, const int row,
// Grow blocks array if necessary.
if (shard->nblocks_allocated < shard->nblocks + 1) {
shard->nblocks_allocated = ALLOCATION_FACTOR * (shard->nblocks + 1);
shard->blocks = (dbm_block_t *)realloc(
shard->blocks, shard->nblocks_allocated * sizeof(dbm_block_t));
shard->blocks =
realloc(shard->blocks, shard->nblocks_allocated * sizeof(dbm_block_t));
assert(shard->blocks != NULL);

// rebuild hashtable
free(shard->hashtable);
Expand Down Expand Up @@ -205,8 +212,8 @@ void dbm_shard_allocate_promised_blocks(dbm_shard_t *shard) {
// Reallocate data array if necessary.
if (shard->data_promised > shard->data_allocated) {
shard->data_allocated = ALLOCATION_FACTOR * shard->data_promised;
shard->data =
(double *)realloc(shard->data, shard->data_allocated * sizeof(double));
shard->data = realloc(shard->data, shard->data_allocated * sizeof(double));
assert(shard->data != NULL);
}

// Zero new blocks.
Expand Down

0 comments on commit c52757e

Please sign in to comment.