-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscale_test.cpp
More file actions
200 lines (168 loc) · 6.12 KB
/
scale_test.cpp
File metadata and controls
200 lines (168 loc) · 6.12 KB
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
#include "memory_allocator.h"
#include <iostream>
#include <thread>
#include <vector>
#include <random>
#include <chrono>
#include <atomic>
#include <iomanip>
// Simple interface for allocators
class AllocInterface {
public:
virtual ~AllocInterface() {}
virtual void* allocate(size_t sz) = 0;
virtual void deallocate(void* ptr) = 0;
virtual const char* getName() const = 0;
};
// System allocator
class SystemAllocator : public AllocInterface {
public:
void* allocate(size_t sz) override { return std::malloc(sz); }
void deallocate(void* ptr) override { std::free(ptr); }
const char* getName() const override { return "System malloc/free"; }
};
// Fancy allocator with reclamation OFF
class FancyAllocatorNoReclaim : public AllocInterface {
public:
FancyAllocatorNoReclaim() : fancy_(64ULL * 1024ULL * 1024ULL, false) {}
void* allocate(size_t sz) override { return fancy_.allocate(sz); }
void deallocate(void* ptr) override { fancy_.deallocate(ptr); }
const char* getName() const override { return "Fancy (Reclamation OFF)"; }
private:
FancyPerThreadAllocator fancy_;
};
// Fancy allocator with reclamation ON
class FancyAllocatorWithReclaim : public AllocInterface {
public:
FancyAllocatorWithReclaim() : fancy_(64ULL * 1024ULL * 1024ULL, true) {}
void* allocate(size_t sz) override { return fancy_.allocate(sz); }
void deallocate(void* ptr) override { fancy_.deallocate(ptr); }
const char* getName() const override { return "Fancy (Reclamation ON)"; }
private:
FancyPerThreadAllocator fancy_;
};
// Optional TCMalloc support
#ifdef USE_TCMALLOC
#include <gperftools/tcmalloc.h>
class TCMallocAllocator : public AllocInterface {
public:
void* allocate(size_t sz) override { return tc_malloc(sz); }
void deallocate(void* ptr) override { tc_free(ptr); }
const char* getName() const override { return "TCMalloc"; }
};
#endif
// Optional JEMalloc support
#ifdef USE_JEMALLOC
#include <jemalloc/jemalloc.h>
class JEMallocAllocator : public AllocInterface {
public:
void* allocate(size_t sz) override { return malloc(sz); }
void deallocate(void* ptr) override { free(ptr); }
const char* getName() const override { return "JEMalloc"; }
};
#endif
// Worker function for the ephemeral HPC scenario
void ephemeralWorker(AllocInterface* alloc, int ops, int ringSize, std::atomic<size_t>& allocCount, std::atomic<size_t>& freeCount) {
struct Slot { void* ptr; int ttl; };
std::vector<Slot> ring(ringSize, {nullptr, 0});
int pos = 0;
std::default_random_engine rng(std::random_device{}());
std::uniform_int_distribution<int> catDist(1, 100);
std::uniform_int_distribution<int> smallDist(16, 256);
std::uniform_int_distribution<int> medDist(512, 2048);
std::uniform_int_distribution<int> largeDist(4096, 32768);
std::uniform_int_distribution<int> ttlDist(50, 2000);
for (int i = 0; i < ops; i++) {
auto& slot = ring[pos];
// free if expired
if (slot.ptr && slot.ttl <= 0) {
alloc->deallocate(slot.ptr);
slot.ptr = nullptr;
freeCount++;
}
// decrement TTL
if (slot.ptr && slot.ttl > 0) {
slot.ttl--;
}
// if empty => allocate new
if (!slot.ptr) {
int c = catDist(rng);
size_t sz = 0;
if (c <= 60) sz = smallDist(rng);
else if (c <= 90) sz = medDist(rng);
else sz = largeDist(rng);
void* p = alloc->allocate(sz);
if (p) {
slot.ptr = p;
slot.ttl = ttlDist(rng);
allocCount++;
}
}
pos = (pos + 1) % ringSize;
}
// final free
for (auto& s : ring) {
if (s.ptr) {
alloc->deallocate(s.ptr);
s.ptr = nullptr;
freeCount++;
}
}
}
// Run the test with a specific allocator
void runTest(AllocInterface* alloc, int threads, int opsPerThread, int ringSize) {
std::cout << "Testing " << alloc->getName() << "..." << std::endl;
std::atomic<size_t> allocCount(0);
std::atomic<size_t> freeCount(0);
auto start = std::chrono::high_resolution_clock::now();
std::vector<std::thread> workers;
workers.reserve(threads);
for (int i = 0; i < threads; i++) {
workers.emplace_back(ephemeralWorker, alloc, opsPerThread, ringSize, std::ref(allocCount), std::ref(freeCount));
}
for (auto& worker : workers) {
worker.join();
}
auto end = std::chrono::high_resolution_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::microseconds>(end - start).count();
std::cout << "-- " << alloc->getName() << " --" << std::endl;
std::cout << "Elapsed (us): " << elapsed << std::endl;
std::cout << "Alloc calls: " << allocCount << ", Free calls: " << freeCount << std::endl;
std::cout << std::endl;
}
int main() {
// Reduced test parameters to avoid memory exhaustion
const int threads = 128; // Reduced from 512
const int opsPerThread = 250000; // Reduced from 1000000
const int ringSize = 100000; // Reduced from 500000
std::cout << "=== High-Scale HPC Ephemeral Test (Reduced Memory) ===" << std::endl;
std::cout << "Threads: " << threads << ", Ops/Thread: " << opsPerThread << ", Ring Size: " << ringSize << std::endl;
std::cout << std::endl;
// Run tests with different allocators
{
SystemAllocator sysAlloc;
runTest(&sysAlloc, threads, opsPerThread, ringSize);
}
{
FancyAllocatorNoReclaim fancyNoReclaim;
runTest(&fancyNoReclaim, threads, opsPerThread, ringSize);
}
{
FancyAllocatorWithReclaim fancyWithReclaim;
runTest(&fancyWithReclaim, threads, opsPerThread, ringSize);
}
#ifdef USE_TCMALLOC
{
TCMallocAllocator tcmAlloc;
runTest(&tcmAlloc, threads, opsPerThread, ringSize);
}
#endif
#ifdef USE_JEMALLOC
{
JEMallocAllocator jemAlloc;
runTest(&jemAlloc, threads, opsPerThread, ringSize);
}
#endif
std::cout << "All tests completed." << std::endl;
return 0;
}