-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathtest_alloc.cpp
149 lines (133 loc) · 3.27 KB
/
test_alloc.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include "utilities.h"
#include "sequence.h"
#include "get_time.h"
#include "list_allocator.h"
#include <vector>
using namespace pbbs;
using namespace std;
template <typename T, typename Allocator=pbbs::allocator<T>>
struct my_vect {
size_t n;
Allocator allocator;
my_vect(size_t n, const Allocator &allocator = Allocator()) : n(n), allocator(allocator) {}
};
int main (int argc, char *argv[]) {
//small_allocator pool;
size_t n = 100000000;
int rounds = 4;
timer t;
my_vect<double> a(10);
cout << "hello: " << sizeof(a) << endl;
void *x = aligned_alloc(256, ((size_t) 1) << 35);
parallel_for (0, (((size_t) 1) << 25), [&] (size_t i) {
((char*) x)[i * (1 << 10)] = 1;}, 1000);
free(x);
t.next("init");
// size_t bits = 30; // 22
// size_t num_blocks = 8; //2000;
// sequence<void*> a(num_blocks);
// parallel_for(0, num_blocks, [&] (size_t j) {
// void * x = aligned_alloc(256, 1 << bits);
// parallel_for (0, (1 << (bits - 18)), [&] (size_t i) {
// ((char*) x)[i * (1 << 18)] = 1;}, 10000);
// return x;
// }, 1);
// parallel_for(0, num_blocks, [&] (size_t j) {
// free(a[j]);});
// t.next("init");
{
//mem_pool mp;
for (int i=0; i < rounds; i++) {
sequence<char *> b(n, [&] (size_t i) {
char* foo = (char*) my_alloc(48);
foo[0] = 'a';
foo[1] = 0;
return foo;
});
t.next("allocate");
parallel_for (0,b.size(), [&] (size_t i) {
my_free(b[i]);
});
t.next("free");
//default_allocator.print_stats();
}
}
//t.next("delete allocator");
cout << endl;
{
std::vector<size_t> sizes = {16, 32, 64, 128};
pool_allocator sa(sizes);
t.next("initialize");
for (int i=0; i < rounds; i++) {
sequence<char *> b(n, [&] (size_t i) {
char* foo = (char*) sa.allocate(64);
foo[0] = 'a';
foo[1] = 0;
return foo;
});
t.next("small allocate");
parallel_for (0,b.size(), [&] (size_t i) {
sa.deallocate(b[i], 64);
});
t.next("small free");
}
}
t.next("delete allocator");
cout << endl;
{
block_allocator ba(64);
t.next("initialize");
for (int j=0; j < rounds; j++) {
sequence<char *> b(n, [&] (size_t i) {
int l = log2_up(i);
char* foo = (char*) ba.alloc();
foo[0] = l;
foo[1] = 0;
return foo;
});
t.next("block allocate");
parallel_for (0,b.size(), [&] (size_t i) {
ba.free(b[i]);
});
t.next("block free");
}
}
t.next("delete allocator");
cout << endl;
{
using T = std::array<long,8>;
using la = list_allocator<T>;
la::init();
t.next("initialize");
for (int j=0; j < rounds; j++) {
sequence<char *> b(n, [&] (size_t i) {
char* foo = (char*) la::alloc();
foo[0] = 'a';
foo[1] = 0;
return foo;
});
t.next("list allocate");
parallel_for (0,b.size(), [&] (size_t i) {
la::free((T*) b[i]);
});
t.next("list free");
}
//la::finish();
}
//t.next("clear allocator");
cout << endl;
for (int j=0; j < rounds; j++) {
sequence<char *> b(n, [&] (size_t i) {
char* foo = (char*) malloc(48);
foo[0] = 'a';
foo[1] = 0;
return foo;
});
t.next("malloc");
parallel_for (0,b.size(), [&] (size_t i) {
free(b[i]);
});
t.next("list free");
}
cout << endl;
}