-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathgpu_benchmark.cu
More file actions
132 lines (101 loc) · 4.05 KB
/
gpu_benchmark.cu
File metadata and controls
132 lines (101 loc) · 4.05 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
#include <iostream>
#include <chrono>
#include <cstdlib> // For std::atoi
#include "gpu_benchmark.h"
// The macro wraps any CUDA API call
#define CUDA_CHECK(ans) { gpuAssert((ans), __FILE__, __LINE__); }
inline void gpuAssert(cudaError_t code, const char *file, int line, bool abort=true) {
if (code != cudaSuccess) {
fprintf(stderr, "GPUassert: %s %s %d\n", cudaGetErrorString(code), file, line);
if (abort) exit(code);
}
}
// Kernel function to perform a simple workload
__global__ void simpleKernel(int* data, int size) {
int idx = blockIdx.x * blockDim.x + threadIdx.x;
if (idx < size) {
data[idx] = data[idx] * data[idx]; // Simple workload: squaring each element
}
}
// Function to run the GPU benchmark with no time limit
void runBenchmark(int max_work) {
int* h_data = new int[max_work];
int* d_data;
// Initialize data
for (int i = 0; i < max_work; i++) {
h_data[i] = i;
}
// Allocate GPU memory
CUDA_CHECK(cudaMalloc(&d_data, max_work * sizeof(int)));
// Copy data to GPU
CUDA_CHECK(cudaMemcpy(d_data, h_data, max_work * sizeof(int), cudaMemcpyHostToDevice));
// Kernel configuration
int threadsPerBlock = 256;
int blocksPerGrid = (max_work + threadsPerBlock - 1) / threadsPerBlock;
// Run the kernel
simpleKernel<<<blocksPerGrid, threadsPerBlock>>>(d_data, max_work);
// Ensure the kernel has finished executing
CUDA_CHECK(cudaDeviceSynchronize());
// Copy results back to host (optional, just for validation)
CUDA_CHECK(cudaMemcpy(h_data, d_data, max_work * sizeof(int), cudaMemcpyDeviceToHost));
// Cleanup
CUDA_CHECK(cudaFree(d_data));
delete[] h_data;
std::cout << "Benchmark completed!" << std::endl;
}
// Function to run the GPU benchmark for a specified time
void runBenchmarkTime(int max_work, int runtime_in_seconds) {
int* h_data = new int[max_work];
int* d_data;
// Initialize data
for (int i = 0; i < max_work; i++) {
h_data[i] = i;
}
// Allocate GPU memory
CUDA_CHECK(cudaMalloc(&d_data, max_work * sizeof(int)));
// Copy data to GPU
CUDA_CHECK(cudaMemcpy(d_data, h_data, max_work * sizeof(int), cudaMemcpyHostToDevice));
// Start the timer
auto start = std::chrono::high_resolution_clock::now();
// Kernel configuration
int threadsPerBlock = 256;
int blocksPerGrid = (max_work + threadsPerBlock - 1) / threadsPerBlock;
// Run the workload loop until the specified runtime is reached
while (std::chrono::duration_cast<std::chrono::seconds>(std::chrono::high_resolution_clock::now() - start).count() < runtime_in_seconds) {
simpleKernel<<<blocksPerGrid, threadsPerBlock>>>(d_data, max_work);
cudaDeviceSynchronize(); // Ensure the kernel has finished executing
}
// Copy results back to host (optional, just for validation)
CUDA_CHECK(cudaMemcpy(h_data, d_data, max_work * sizeof(int), cudaMemcpyDeviceToHost));
// Cleanup
CUDA_CHECK(cudaFree(d_data));
delete[] h_data;
std::cout << "Benchmark completed!" << std::endl;
}
int main(int argc, char* argv[]) {
// Check for the correct number of command line arguments
if (argc == 2) {
// Parse the command line arguments
int max_work = std::atoi(argv[1]);
// Validate the input arguments
if (max_work <= 0) {
std::cerr << "max_work must be a positive integer." << std::endl;
return 1;
}
runBenchmark(max_work);
} else if (argc == 3) {
// Parse the command line arguments
int max_work = std::atoi(argv[1]);
int runtime_in_seconds = std::atoi(argv[2]);
// Validate the input arguments
if (max_work <= 0 || runtime_in_seconds <= 0) {
std::cerr << "Both max_work and runtime_in_seconds must be positive integers." << std::endl;
return 1;
}
runBenchmarkTime(max_work, runtime_in_seconds);
} else {
std::cerr << "Usage: " << argv[0] << " <max_work> [runtime_in_seconds]" << std::endl;
return 1;
}
return 0;
}