-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple_benchmark.cpp
More file actions
197 lines (159 loc) · 6.64 KB
/
simple_benchmark.cpp
File metadata and controls
197 lines (159 loc) · 6.64 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
/*******************************************************************************
* FILE: simple_benchmark.cpp
*
* PURPOSE:
* Implementation of simple backend-agnostic benchmarks
******************************************************************************/
#include "simple_benchmark.h"
#include "backends/cuda/CUDABackend.h"
#include "backends/opencl/OpenCLBackend.h"
#include "backends/directcompute/DirectComputeBackend.h"
#include <cmath>
// CUDA kernel launchers (only used when backend is CUDA)
extern "C" void launchVectorAdd(const float* d_a, const float* d_b, float* d_c, int n);
extern "C" void launchMatrixMul(const float* d_A, const float* d_B, float* d_C, int N);
namespace GPUBenchmark {
BenchmarkResult SimpleVectorAddBenchmark(IComputeBackend* backend, size_t numElements, int iterations) {
BenchmarkResult result;
result.benchmarkName = "VectorAdd";
result.backendName = backend->GetBackendName();
result.problemSize = numElements;
result.timestamp = Logger::GetCurrentTimestamp();
Logger& logger = Logger::GetInstance();
logger.Info("Running VectorAdd: " + std::to_string(numElements) + " elements, " + std::to_string(iterations) + " iterations");
size_t bytes = numElements * sizeof(float);
// Allocate host memory
std::vector<float> hostA(numElements);
std::vector<float> hostB(numElements);
std::vector<float> hostC(numElements);
// Initialize data
for (size_t i = 0; i < numElements; i++) {
hostA[i] = static_cast<float>(i);
hostB[i] = static_cast<float>(i * 2);
}
// Allocate device memory
void* deviceA = backend->AllocateMemory(bytes);
void* deviceB = backend->AllocateMemory(bytes);
void* deviceC = backend->AllocateMemory(bytes);
if (!deviceA || !deviceB || !deviceC) {
logger.Error("Failed to allocate device memory");
result.resultCorrect = false;
return result;
}
// Copy to device
backend->CopyHostToDevice(deviceA, hostA.data(), bytes);
backend->CopyHostToDevice(deviceB, hostB.data(), bytes);
// Determine backend type and run appropriate kernel
BackendType backendType = backend->GetType();
// Warmup
for (int i = 0; i < 5; i++) {
if (backendType == BackendType::CUDA) {
launchVectorAdd((const float*)deviceA, (const float*)deviceB, (float*)deviceC, numElements);
}
// For OpenCL and DirectCompute, they need their own kernel execution
// For now, just sync
backend->Synchronize();
}
// Timed execution
backend->StartTimer();
for (int i = 0; i < iterations; i++) {
if (backendType == BackendType::CUDA) {
launchVectorAdd((const float*)deviceA, (const float*)deviceB, (float*)deviceC, numElements);
}
backend->Synchronize();
}
backend->StopTimer();
result.executionTimeMS = backend->GetElapsedTime() / iterations;
// Copy results back
backend->CopyDeviceToHost(hostC.data(), deviceC, bytes);
// Calculate bandwidth
double totalBytes = 3.0 * bytes; // Read A, B, write C
result.effectiveBandwidthGBs = (totalBytes / (result.executionTimeMS / 1000.0)) / 1e9;
// Verify results
int errors = 0;
const float epsilon = 1e-5f;
for (size_t i = 0; i < numElements && errors < 10; i++) {
float expected = hostA[i] + hostB[i];
if (std::abs(hostC[i] - expected) > epsilon) {
errors++;
}
}
result.resultCorrect = (errors == 0);
// Cleanup
backend->FreeMemory(deviceA);
backend->FreeMemory(deviceB);
backend->FreeMemory(deviceC);
return result;
}
BenchmarkResult SimpleMatrixMulBenchmark(IComputeBackend* backend, int matrixSize, int iterations) {
BenchmarkResult result;
result.benchmarkName = "MatrixMul";
result.backendName = backend->GetBackendName();
result.problemSize = matrixSize * matrixSize;
result.timestamp = Logger::GetCurrentTimestamp();
Logger& logger = Logger::GetInstance();
logger.Info("Running MatrixMul: " + std::to_string(matrixSize) + "x" + std::to_string(matrixSize) + ", " + std::to_string(iterations) + " iterations");
size_t numElements = matrixSize * matrixSize;
size_t bytes = numElements * sizeof(float);
// Allocate host memory
std::vector<float> hostA(numElements);
std::vector<float> hostB(numElements);
std::vector<float> hostC(numElements);
// Initialize data
for (size_t i = 0; i < numElements; i++) {
hostA[i] = 1.0f;
hostB[i] = 2.0f;
}
// Allocate device memory
void* deviceA = backend->AllocateMemory(bytes);
void* deviceB = backend->AllocateMemory(bytes);
void* deviceC = backend->AllocateMemory(bytes);
if (!deviceA || !deviceB || !deviceC) {
logger.Error("Failed to allocate device memory");
result.resultCorrect = false;
return result;
}
// Copy to device
backend->CopyHostToDevice(deviceA, hostA.data(), bytes);
backend->CopyHostToDevice(deviceB, hostB.data(), bytes);
// Determine backend type
BackendType backendType = backend->GetType();
// Warmup
for (int i = 0; i < 3; i++) {
if (backendType == BackendType::CUDA) {
launchMatrixMul((const float*)deviceA, (const float*)deviceB, (float*)deviceC, matrixSize);
}
backend->Synchronize();
}
// Timed execution
backend->StartTimer();
for (int i = 0; i < iterations; i++) {
if (backendType == BackendType::CUDA) {
launchMatrixMul((const float*)deviceA, (const float*)deviceB, (float*)deviceC, matrixSize);
}
backend->Synchronize();
}
backend->StopTimer();
result.executionTimeMS = backend->GetElapsedTime() / iterations;
// Copy results back
backend->CopyDeviceToHost(hostC.data(), deviceC, bytes);
// Calculate GFLOPS
double numOperations = 2.0 * matrixSize * matrixSize * matrixSize; // 2N^3 operations
result.computeThroughputGFLOPS = (numOperations / (result.executionTimeMS / 1000.0)) / 1e9;
// Verify (simple check)
float expected = 2.0f * matrixSize; // Each element should be N*1*2 = 2N
int errors = 0;
const float epsilon = 0.1f * matrixSize;
for (int i = 0; i < std::min(100, (int)numElements); i++) {
if (std::abs(hostC[i] - expected) > epsilon) {
errors++;
}
}
result.resultCorrect = (errors < 5); // Allow some numerical errors
// Cleanup
backend->FreeMemory(deviceA);
backend->FreeMemory(deviceB);
backend->FreeMemory(deviceC);
return result;
}
} // namespace GPUBenchmark