-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cxx
238 lines (229 loc) · 7.55 KB
/
main.cxx
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#include <cstdint>
#include <cstdio>
#include <vector>
#include <omp.h>
#include "inc/main.hxx"
using namespace std;
#pragma region CONFIGURATION
#ifndef MAX_THREADS
/** Maximum number of threads to use. */
#define MAX_THREADS 64
#endif
#ifndef REPEAT_METHOD
/** Number of times to repeat each method. */
#define REPEAT_METHOD 5
#endif
#pragma endregion
#pragma region METHODS
/**
* Main function.
* @param argc number of command-line arguments
* @param argv command-line arguments
* @returns 0 if successful
*/
int main(int argc, char **argv) {
constexpr size_t ALLOCS = 1ULL << 28;
constexpr size_t SIZE = 1ULL << 6;
constexpr size_t MIXED = 64;
omp_set_num_threads(MAX_THREADS);
// Manage memory using malloc and free.
{
vector<void*> ptrs(ALLOCS);
float tm = measureDuration([&] {
#pragma omp parallel for schedule(dynamic, 2048)
for (size_t i=0; i<ALLOCS; ++i)
ptrs[i] = malloc(SIZE);
});
printf("malloc: %.3f ms\n", tm);
float tf = measureDuration([&] {
#pragma omp parallel for schedule(dynamic, 2048)
for (size_t i=0; i<ALLOCS; ++i)
free(ptrs[i]);
});
printf("free: %.3f ms\n", tf);
}
// Manage memory using malloc and free (mixed).
{
vector<void*> ptrs(ALLOCS/MIXED);
float ta = measureDuration([&] {
for (size_t l=0; l<MIXED; ++l) {
#pragma omp parallel for schedule(dynamic, 2048)
for (size_t i=0; i<ALLOCS/MIXED; ++i)
ptrs[i] = malloc(SIZE);
#pragma omp parallel for schedule(dynamic, 2048)
for (size_t i=0; i<ALLOCS/MIXED; ++i)
free(ptrs[i]);
}
});
printf("malloc + free: %.3f ms\n", ta);
}
// Manage memory using new and delete.
{
vector<void*> ptrs(ALLOCS);
float tm = measureDuration([&] {
#pragma omp parallel for schedule(dynamic, 2048)
for (size_t i=0; i<ALLOCS; ++i)
ptrs[i] = new char[SIZE];
});
printf("new: %.3f ms\n", tm);
float tf = measureDuration([&] {
#pragma omp parallel for schedule(dynamic, 2048)
for (size_t i=0; i<ALLOCS; ++i)
delete[] (char*) ptrs[i];
});
printf("delete: %.3f ms\n", tf);
}
// Manage memory using new and delete (mixed).
{
vector<void*> ptrs(ALLOCS/MIXED);
float ta = measureDuration([&] {
for (size_t l=0; l<MIXED; ++l) {
#pragma omp parallel for schedule(dynamic, 2048)
for (size_t i=0; i<ALLOCS/MIXED; ++i)
ptrs[i] = new char[SIZE];
#pragma omp parallel for schedule(dynamic, 2048)
for (size_t i=0; i<ALLOCS/MIXED; ++i)
delete[] (char*) ptrs[i];
}
});
printf("new + delete: %.3f ms\n", ta);
}
// Manage memory using FixedArenaAllocator.
{
vector<void*> ptrs(ALLOCS);
vector<FixedArenaAllocator<SIZE, ALLOCS>*> mems(MAX_THREADS);
for (int i=0; i<MAX_THREADS; ++i)
mems[i] = new FixedArenaAllocator<SIZE, ALLOCS>(malloc(ALLOCS * SIZE));
float tm = measureDuration([&] {
#pragma omp parallel for schedule(dynamic, 2048)
for (size_t i=0; i<ALLOCS; ++i) {
int t = omp_get_thread_num();
ptrs[i] = mems[t]->allocate();
}
});
printf("FixedArenaAllocator.allocate: %.3f ms\n", tm);
float tf = measureDuration([&] {
#pragma omp parallel for schedule(dynamic, 2048)
for (size_t i=0; i<ALLOCS; ++i) {
int t = omp_get_thread_num();
mems[t]->deallocate(ptrs[i]);
}
});
printf("FixedArenaAllocator.deallocate: %.3f ms\n", tf);
}
// Manage memory using FixedArenaAllocator (mixed).
{
vector<void*> ptrs(ALLOCS/MIXED);
vector<FixedArenaAllocator<SIZE, ALLOCS/MIXED>*> mems(MAX_THREADS);
for (int i=0; i<MAX_THREADS; ++i)
mems[i] = new FixedArenaAllocator<SIZE, ALLOCS/MIXED>(malloc(ALLOCS/MIXED * SIZE));
float ta = measureDuration([&] {
for (size_t l=0; l<MIXED; ++l) {
#pragma omp parallel for schedule(dynamic, 2048)
for (size_t i=0; i<ALLOCS/MIXED; ++i) {
int t = omp_get_thread_num();
ptrs[i] = mems[t]->allocate();
}
#pragma omp parallel for schedule(dynamic, 2048)
for (size_t i=0; i<ALLOCS/MIXED; ++i) {
int t = omp_get_thread_num();
mems[t]->deallocate(ptrs[i]);
}
}
});
printf("FixedArenaAllocator.allocate + deallocate: %.3f ms\n", ta);
}
// Manage memory using ArenaAllocator (growing).
{
constexpr size_t CAPACITY = 4096;
vector<void*> ptrs(ALLOCS);
vector<ArenaAllocator<SIZE, CAPACITY>*> mems(MAX_THREADS);
for (int i=0; i<MAX_THREADS; ++i)
mems[i] = new ArenaAllocator<SIZE, CAPACITY>();
float tm = measureDuration([&] {
#pragma omp parallel for schedule(dynamic, 2048)
for (size_t i=0; i<ALLOCS; ++i) {
int t = omp_get_thread_num();
ptrs[i] = mems[t]->allocate();
}
});
printf("ArenaAllocator.allocate: %.3f ms\n", tm);
float tf = measureDuration([&] {
#pragma omp parallel for schedule(dynamic, 2048)
for (size_t i=0; i<ALLOCS; ++i) {
int t = omp_get_thread_num();
mems[t]->deallocate(ptrs[i]);
}
});
printf("ArenaAllocator.deallocate: %.3f ms\n", tf);
}
// Manage memory using ArenaAllocator (growing, mixed).
{
constexpr size_t CAPACITY = 4096;
vector<void*> ptrs(ALLOCS/MIXED);
vector<ArenaAllocator<SIZE, CAPACITY>*> mems(MAX_THREADS);
for (int i=0; i<MAX_THREADS; ++i)
mems[i] = new ArenaAllocator<SIZE, CAPACITY>();
float ta = measureDuration([&] {
for (size_t l=0; l<MIXED; ++l) {
#pragma omp parallel for schedule(dynamic, 2048)
for (size_t i=0; i<ALLOCS/MIXED; ++i) {
int t = omp_get_thread_num();
ptrs[i] = mems[t]->allocate();
}
#pragma omp parallel for schedule(dynamic, 2048)
for (size_t i=0; i<ALLOCS/MIXED; ++i) {
int t = omp_get_thread_num();
mems[t]->deallocate(ptrs[i]);
}
}
});
printf("ArenaAllocator.allocate + deallocate: %.3f ms\n", ta);
}
// Manage memory using ConcurrentPow2Allocator (growing).
{
constexpr size_t CAPACITY = 4096;
vector<void*> ptrs(ALLOCS);
ConcurrentPow2Allocator<SIZE*CAPACITY> mems;
float tm = measureDuration([&] {
#pragma omp parallel for schedule(dynamic, 2048)
for (size_t i=0; i<ALLOCS; ++i) {
// int t = omp_get_thread_num();
ptrs[i] = mems.allocate(SIZE);
}
});
printf("ConcurrentPow2Allocator.allocate: %.3f ms\n", tm);
float tf = measureDuration([&] {
#pragma omp parallel for schedule(dynamic, 2048)
for (size_t i=0; i<ALLOCS; ++i) {
// int t = omp_get_thread_num();
mems.deallocate(ptrs[i], SIZE);
}
});
printf("ConcurrentPow2Allocator.deallocate: %.3f ms\n", tf);
}
// Manage memory using ConcurrentPow2Allocator (growing, mixed).
{
constexpr size_t CAPACITY = 4096;
vector<void*> ptrs(ALLOCS/MIXED);
ConcurrentPow2Allocator<SIZE*CAPACITY> mems;
float ta = measureDuration([&] {
for (size_t l=0; l<MIXED; ++l) {
#pragma omp parallel for schedule(dynamic, 2048)
for (size_t i=0; i<ALLOCS/MIXED; ++i) {
// int t = omp_get_thread_num();
ptrs[i] = mems.allocate(SIZE);
}
#pragma omp parallel for schedule(dynamic, 2048)
for (size_t i=0; i<ALLOCS/MIXED; ++i) {
// int t = omp_get_thread_num();
mems.deallocate(ptrs[i], SIZE);
}
}
});
printf("ConcurrentPow2Allocator.allocate + deallocate: %.3f ms\n", ta);
}
printf("Performed %zu allocations of %zu bytes each.\n", ALLOCS, SIZE);
printf("\n");
}
#pragma endregion