-
Notifications
You must be signed in to change notification settings - Fork 1
/
MassAllocator.cpp
188 lines (162 loc) · 7.59 KB
/
MassAllocator.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
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
#include <iostream>
#include <array>
#include <thread>
#include <memory>
#include <tchar.h>
#include "massAllocator.h"
struct ObjectA
{
int a;
double b[1];
};
template <typename F>
void measureTime(F f, std::string message)
{
auto allocationStart = clock();
f();
auto allocationEnd = clock();
auto time = (double) (allocationEnd - allocationStart) / CLOCKS_PER_SEC;
std::cout << message << " took " << time << "sec" << std::endl;
}
int _tmain(int argc, _TCHAR* argv[])
{
const int N = 5000000;
const int ThreadCount = 8;
{//проверка выделения объектов через MassAllocator
auto allocationStart = clock();
MassAllocator<ObjectA> heap1;
std::cout << "is_lock_free = " << (heap1.is_lock_free() ? std::string("true") : std::string("false")) << std::endl;
std::cout << "Object size " << sizeof(ObjectA) << " bytes, allocate for " << N * ThreadCount<< " objects in " << ThreadCount << " threads, total objects size = " << sizeof(ObjectA) * N * ThreadCount / (1024 * 1024.0) << "MB" << std::endl;
//сюда будем складывать все полученный индексы выделенных объектов
std::array<std::vector<size_t>, ThreadCount> allocatedIndxs;
//функция для потока
auto func =
[&]
(int threadIndx)
{
auto &indxs = allocatedIndxs[threadIndx];
indxs.reserve(N);
for(int i = 0; i < N; ++i)
{
size_t indx;
//запрашиваем новый элемент и индекс этого элемента
ObjectA *obj = heap1.createElement(&indx);
obj->a = i;
indxs.push_back(indx);
}
std::cout << " Thread " << std::this_thread::get_id()<< " allocated " << N << " objects" << std::endl;
};
//запускаем потоки
typedef std::shared_ptr<std::thread> ThreadPtr;
std::vector<ThreadPtr> threads;
for(int i = 0; i < ThreadCount; ++i)
threads.push_back(ThreadPtr(new std::thread(func, i)));
//дожидаемся завершения всех потоков
for(auto ii = threads.begin(), ie = threads.end(); ii != ie; ++ii)
(*ii)->join();
auto allocationEnd = clock();
std::cout << "Objects in mass allocator = " << heap1.size() << " memory used = " << heap1.memUse() / (1024 * 1024.0) << "MB" << std::endl;
{
auto iterFunc =
[&]
()
{
//обработка элементов index-like
for(size_t i = 0, n = heap1.size(); i < n; ++i)
heap1[i].a += 1;
};
measureTime(iterFunc, "Index-based processing");
}
{
auto iterFunc =
[&]
()
{
//обработка элементов iterator-like
for(auto ii = heap1.begin(), ie = heap1.end(); ii != ie; ++ii)
ii->b[0] = ii->a * 42;
};
measureTime(iterFunc, "Iterator-based processing");
}
{
auto sortFunc =
[&]
()
{
std::sort(
heap1.begin(),
heap1.begin() + N,
[](const ObjectA &lh, const ObjectA &rh)
{ return lh.a > rh.a; }
);
};
measureTime(sortFunc, "Sort");
}
auto deallocationStart = clock();
heap1.clear();
auto deallocationEnd = clock();
auto allocTime = (double) (allocationEnd - allocationStart) / CLOCKS_PER_SEC;
auto deallocTime = (double) (deallocationEnd - deallocationStart) / CLOCKS_PER_SEC;
std::cout << "Allocation and deallocation " << N * ThreadCount << " objects took " << allocTime << "+" << deallocTime << " = " <<allocTime + deallocTime << "sec" << std::endl;
//проверяем корректность выделения объектов.
std::cout << "Check allocation continuity" << std::endl;
std::vector<size_t> threadsCurrentIndx;
threadsCurrentIndx.resize(ThreadCount);
for(size_t i = 0, n = N * ThreadCount; i < n; ++i)
{
//ищем какой поток сделал захват индекса i
for(int j = 0; j <= ThreadCount; ++j)
{
//Диагностируем ошибку, когда ни один из потоков не захватил элемент i
if (j == ThreadCount)
throw std::string("allocation error");
//пропускаем поток, в котром уже проверили все элементы
if (threadsCurrentIndx[j] == N)
continue;
//проверяем что поток j захватил элемент i
if (allocatedIndxs[j][threadsCurrentIndx[j]] != i)
continue;
//да, поток j захватил элемент i. перемещаемся к его следующему элементу
++threadsCurrentIndx[j];
break;
}
}
std::cout << "Check allocation continuity finished with success!" << std::endl;
}
{//проверка выделения объектов через стандартный менеджер памяти
auto allocationStart = clock();
std::vector<std::vector<ObjectA*>> allocatedObjects;
allocatedObjects.resize(ThreadCount);
auto func =
[&]
(int threadIndex)
{
auto &objects = allocatedObjects[threadIndex];
objects.reserve(N);
for(int i = 0; i < N; ++i)
{
auto obj = new ObjectA();
obj->a = i;
objects.push_back(obj);
}
std::cout << " Thread " << std::this_thread::get_id()<< " allocated " << N << std::endl;
};
typedef std::shared_ptr<std::thread> ThreadPtr;
std::vector<ThreadPtr> threads;
for(int i = 0; i < ThreadCount; ++i)
threads.push_back(ThreadPtr(new std::thread(func, i)));
for(auto ii = threads.begin(), ie = threads.end(); ii != ie; ++ii)
(*ii)->join();
auto allocationEnd = clock();
auto deallocationStart = clock();
for(auto ii = allocatedObjects.begin(), ie = allocatedObjects.end(); ii != ie; ++ii)
for(auto jj = ii->begin(), je = ii->end(); jj != je; ++jj)
delete *jj;
auto deallocationEnd = clock();
auto allocTime = (double) (allocationEnd - allocationStart) / CLOCKS_PER_SEC;
auto deallocTime = (double) (deallocationEnd - deallocationStart) / CLOCKS_PER_SEC;
std::cout << "operator new-based allocation and deallocation " << N * ThreadCount << " objects took " << allocTime << "+" << deallocTime << " = " <<allocTime + deallocTime << "sec" << std::endl;
}
std::getchar();
return 0;
}