Skip to content

Commit df1055f

Browse files
committed
Implement allocator
1 parent 122cce8 commit df1055f

File tree

5 files changed

+64
-14
lines changed

5 files changed

+64
-14
lines changed

include/binsparse/array.h

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22

33
#include <assert.h>
4+
#include <binsparse/detail/allocator.h>
45
#include <binsparse/detail/shm_tools.h>
56
#include <binsparse/types.h>
67
#include <stdlib.h>
@@ -10,27 +11,26 @@ typedef struct bsp_array_t {
1011
void* data;
1112
size_t size;
1213
bsp_type_t type;
13-
bool shmat_memory;
14-
bsp_shm_t shm;
14+
bsp_allocator_t allocator;
1515
} bsp_array_t;
1616

1717
bsp_array_t bsp_construct_default_array_t() {
1818
bsp_array_t array;
1919
array.data = NULL;
2020
array.size = 0;
21-
array.shmat_memory = false;
21+
array.allocator = bsp_default_allocator;
2222
return array;
2323
}
2424

2525
bsp_array_t bsp_construct_array_t(size_t size, bsp_type_t type) {
2626
size_t byte_size = size * bsp_type_size(type);
2727

2828
bsp_array_t array;
29-
array.data = malloc(byte_size);
29+
array.allocator = bsp_default_allocator;
30+
array.data = array.allocator.malloc(byte_size);
3031
assert(array.data != NULL);
3132
array.size = size;
3233
array.type = type;
33-
array.shmat_memory = false;
3434

3535
return array;
3636
}
@@ -49,6 +49,7 @@ bsp_array_t bsp_complex_array_to_fp(bsp_array_t other) {
4949
bsp_array_t array;
5050
array.data = other.data;
5151
array.size = other.size * 2;
52+
array.allocator = other.allocator;
5253

5354
if (other.type == BSP_COMPLEX_FLOAT32) {
5455
array.type = BSP_FLOAT32;
@@ -65,6 +66,7 @@ bsp_array_t bsp_fp_array_to_complex(bsp_array_t other) {
6566
bsp_array_t array;
6667
array.data = other.data;
6768
array.size = other.size / 2;
69+
array.allocator = other.allocator;
6870

6971
if (other.type == BSP_FLOAT32) {
7072
array.type = BSP_COMPLEX_FLOAT32;
@@ -76,11 +78,7 @@ bsp_array_t bsp_fp_array_to_complex(bsp_array_t other) {
7678
}
7779

7880
void bsp_destroy_array_t(bsp_array_t array) {
79-
if (array.shmat_memory == false) {
80-
free(array.data);
81-
} else {
82-
bsp_shm_detach(array.data);
83-
}
81+
array.allocator.free(array.data);
8482
}
8583

8684
bool bsp_array_equal(bsp_array_t x, bsp_array_t y) {
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#pragma once
2+
3+
#include <stdlib.h>
4+
5+
typedef struct bsp_allocator_t {
6+
void* (*malloc)(size_t);
7+
void (*free)(void*);
8+
} bsp_allocator_t;
9+
10+
const static bsp_allocator_t bsp_default_allocator = {.malloc = malloc,
11+
.free = free};

include/binsparse/detail/shm_tools.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#pragma once
22

3+
#include <binsparse/detail/allocator.h>
34
#include <stddef.h>
45
#include <stdio.h>
56
#include <sys/ipc.h>
@@ -42,3 +43,20 @@ void* bsp_shm_attach(bsp_shm_t shm) {
4243
void bsp_shm_detach(void* data) {
4344
shmdt(data);
4445
}
46+
47+
void* bsp_shm_malloc(size_t size) {
48+
bsp_shm_t shm_id = bsp_shm_new(size);
49+
50+
void* ptr = bsp_shm_attach(shm_id);
51+
52+
bsp_shm_delete(shm_id);
53+
54+
return ptr;
55+
}
56+
57+
void bsp_shm_free(void* ptr) {
58+
bsp_shm_detach(ptr);
59+
}
60+
61+
const static bsp_allocator_t bsp_shm_allocator = {.malloc = bsp_shm_malloc,
62+
.free = bsp_shm_free};

include/binsparse/hdf5_wrapper.h

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99

1010
#include <binsparse/detail/shm_tools.h>
1111

12+
#if __STDC_VERSION__ >= 201112L
13+
#include <stdatomic.h>
14+
#endif
15+
1216
// Write an array to a dataset / file
1317
// Returns 0 on success, nonzero on error.
1418
int bsp_write_array(hid_t f, const char* label, bsp_array_t array,
@@ -65,6 +69,7 @@ int bsp_write_array(hid_t f, const char* label, bsp_array_t array,
6569
return 0;
6670
}
6771

72+
#if __STDC_VERSION__ >= 201112L
6873
bsp_array_t bsp_read_array_parallel(hid_t f, const char* label,
6974
int num_threads) {
7075
hid_t dset = H5Dopen2(f, label, H5P_DEFAULT);
@@ -91,12 +96,19 @@ bsp_array_t bsp_read_array_parallel(hid_t f, const char* label,
9196

9297
bsp_type_t type = bsp_get_bsp_type(hdf5_type);
9398

99+
// Array will be written into a POSIX shared memory.
94100
bsp_shm_t array_shm = bsp_shm_new(dims[0] * bsp_type_size(type));
95101
bsp_array_t array;
96102
array.type = type;
97103
array.size = dims[0];
98-
array.shm = array_shm;
99-
array.shmat_memory = true;
104+
array.allocator = bsp_shm_allocator;
105+
106+
bsp_shm_t active_children_shm = bsp_shm_new(sizeof(_Atomic int));
107+
108+
_Atomic int* active_children = bsp_shm_attach(active_children_shm);
109+
bsp_shm_delete(active_children_shm);
110+
111+
*active_children = num_threads - 1;
100112

101113
pid_t* pids = (pid_t*) malloc(sizeof(pid_t) * num_threads);
102114

@@ -115,7 +127,7 @@ bsp_array_t bsp_read_array_parallel(hid_t f, const char* label,
115127

116128
array.data = bsp_shm_attach(array_shm);
117129
if (thread_num == 0) {
118-
bsp_shm_delete(array.shm);
130+
bsp_shm_delete(array_shm);
119131
}
120132

121133
hsize_t chunk_size = (array.size + num_threads - 1) / num_threads;
@@ -131,22 +143,29 @@ bsp_array_t bsp_read_array_parallel(hid_t f, const char* label,
131143
hid_t memspace_id = H5Screate_simple(1, &count, NULL);
132144

133145
H5Dread(dset, bsp_get_hdf5_native_type(type), memspace_id, fspace,
134-
H5P_DEFAULT, array.data + start * bsp_type_size(type));
146+
H5P_DEFAULT, ((char*) array.data) + start * bsp_type_size(type));
135147
H5Sclose(memspace_id);
136148
}
137149

138150
H5Dclose(dset);
139151
H5Sclose(fspace);
140152

141153
if (thread_num > 0) {
154+
atomic_fetch_add_explicit(active_children, -1, memory_order_relaxed);
155+
bsp_shm_detach(active_children);
142156
bsp_shm_detach(array.data);
143157
exit(0);
144158
}
145159

146160
free(pids);
147161

162+
while (atomic_load(active_children) > 0) {
163+
}
164+
bsp_shm_detach(active_children);
165+
148166
return array;
149167
}
168+
#endif
150169

151170
bsp_array_t bsp_read_array(hid_t f, const char* label) {
152171
hid_t dset = H5Dopen2(f, label, H5P_DEFAULT);

include/binsparse/read_matrix.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <binsparse/matrix.h>
55
#include <cJSON/cJSON.h>
66

7+
#if __STDC_VERSION__ >= 201112L
78
bsp_matrix_t bsp_read_matrix_from_group_parallel(hid_t f, int num_threads) {
89
bsp_matrix_t matrix = bsp_construct_default_matrix_t();
910

@@ -107,6 +108,7 @@ bsp_matrix_t bsp_read_matrix_from_group_parallel(hid_t f, int num_threads) {
107108

108109
return matrix;
109110
}
111+
#endif
110112

111113
bsp_matrix_t bsp_read_matrix_from_group(hid_t f) {
112114
bsp_matrix_t matrix = bsp_construct_default_matrix_t();
@@ -219,6 +221,7 @@ size_t bsp_final_dot(const char* str) {
219221
return dot_idx;
220222
}
221223

224+
#if __STDC_VERSION__ >= 201112L
222225
bsp_matrix_t bsp_read_matrix_parallel(const char* file_name, const char* group,
223226
int num_threads) {
224227
if (group == NULL) {
@@ -243,6 +246,7 @@ bsp_matrix_t bsp_read_matrix_parallel(const char* file_name, const char* group,
243246
return matrix;
244247
}
245248
}
249+
#endif
246250

247251
bsp_matrix_t bsp_read_matrix(const char* file_name, const char* group) {
248252
if (group == NULL) {

0 commit comments

Comments
 (0)