-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathgpu_benchmark.hip
More file actions
135 lines (105 loc) · 4.21 KB
/
gpu_benchmark.hip
File metadata and controls
135 lines (105 loc) · 4.21 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
#include "hip/hip_runtime.h"
#include <iostream>
#include <chrono>
#include <cstdlib> // For std::atoi
#include "gpu_benchmark.h"
#define HIP_CHECK(expression) \
{ \
const hipError_t status = expression; \
if(status != hipSuccess){ \
std::cerr << "HIP error " \
<< status << ": " \
<< hipGetErrorString(status) \
<< " at " << __FILE__ << ":" \
<< __LINE__ << std::endl; \
} \
}
// 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
HIP_CHECK(hipMalloc(&d_data, max_work * sizeof(int)));
// Copy data to GPU
HIP_CHECK(hipMemcpy(d_data, h_data, max_work * sizeof(int), hipMemcpyHostToDevice));
// 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
HIP_CHECK(hipDeviceSynchronize());
// Copy results back to host (optional, just for validation)
HIP_CHECK(hipMemcpy(h_data, d_data, max_work * sizeof(int), hipMemcpyDeviceToHost));
// Cleanup
HIP_CHECK(hipFree(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
HIP_CHECK(hipMalloc(&d_data, max_work * sizeof(int)));
// Copy data to GPU
HIP_CHECK(hipMemcpy(d_data, h_data, max_work * sizeof(int), hipMemcpyHostToDevice));
// 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);
HIP_CHECK(hipDeviceSynchronize()); // Ensure the kernel has finished executing
}
// Copy results back to host (optional, just for validation)
HIP_CHECK(hipMemcpy(h_data, d_data, max_work * sizeof(int), hipMemcpyDeviceToHost));
// Cleanup
HIP_CHECK(hipFree(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;
}