-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcuwise_input_optimized.txt
More file actions
122 lines (98 loc) · 3.18 KB
/
Copy pathcuwise_input_optimized.txt
File metadata and controls
122 lines (98 loc) · 3.18 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
#include <iostream>
#include <math.h>
#include <cuda_runtime.h>
// Kernel function to add the elements of two arrays
__global__
void add(int n, float *x, float *y)
{
int i = blockIdx.x * blockDim.x + threadIdx.x;
if (i < n)
y[i] = x[i] + y[i];
}
int main(void)
{
int N = 1 << 20; // 2^20 = 1M elements
float *x, *y;
cudaError_t err; // Declare error variable
cudaEvent_t start, stop;
cudaDeviceProp prop;
// Allocate Unified Memory – accessible from CPU or GPU
err = cudaMallocManaged(&x, N * sizeof(float));
if (err != cudaSuccess) {
std::cerr << "cudaMallocManaged for x failed: " << cudaGetErrorString(err) << std::endl;
return 1;
}
err = cudaMallocManaged(&y, N * sizeof(float));
if (err != cudaSuccess) {
std::cerr << "cudaMallocManaged for y failed: " << cudaGetErrorString(err) << std::endl;
// Free x if it was allocated
cudaFree(x);
return 1;
}
// Initialize input arrays
for (int i = 0; i < N; i++) {
x[i] = 1.0f;
y[i] = 2.0f;
}
// Launch the kernel
cudaEventCreate(&start);
cudaEventCreate(&stop);
cudaEventRecord(start);
cudaGetDeviceProperties(&prop, 0);
int blockSize = 256;
int numBlocks = (N + blockSize - 1) / blockSize;
add<<<numBlocks, blockSize>>>(N, x, y);
cudaEventRecord(stop);
cudaEventSynchronize(stop);
float ms = 0;
cudaEventElapsedTime(&ms, start, stop);
// Check for kernel launch errors
err = cudaGetLastError();
if (err != cudaSuccess) {
std::cerr << "add kernel launch failed: " << cudaGetErrorString(err) << std::endl;
// Free memory
cudaFree(x);
cudaFree(y);
return 1;
}
// Wait for GPU to finish before accessing on host
cudaDeviceSynchronize();
// Check for errors during device synchronization
err = cudaGetLastError();
if (err != cudaSuccess) {
std::cerr << "cudaDeviceSynchronize failed: " << cudaGetErrorString(err) << std::endl;
// Free memory
cudaFree(x);
cudaFree(y);
return 1;
}
// Verify result
float maxError = 0.0f;
for (int i = 0; i < N; i++) {
maxError = fmax(maxError, fabs(y[i] - 3.0f));
}
std::cout << " Max error: " << maxError << std::endl;
// Print simulated profile
std::cout << "Kernel time: " << ms << " ms" << std::endl;
std::cout << "Array size: " << N << std::endl;
std::cout << "GPU: " << prop.name << "\n";
std::cout << "SMs: " << prop.multiProcessorCount << "\n";
std::cout << "Shared memory per block: " << prop.sharedMemPerBlock << " bytes\n";
std::cout << "Max threads per block: " << prop.maxThreadsPerBlock << "\n";
// Free memory
cudaFree(x);
cudaFree(y);
return 0;
}
ptxas info : 0 bytes gmem
ptxas info : Compiling entry function '_Z3addiPfS_' for 'sm_70'
ptxas info : Function properties for _Z3addiPfS_
0 bytes stack frame, 0 bytes spill stores, 0 bytes spill loads
ptxas info : Used 10 registers, 376 bytes cmem[0]
Max error: 0
Kernel time: 2.17667 ms
Array size: 1048576
GPU: Tesla T4
SMs: 40
Shared memory per block: 49152 bytes
Max threads per block: 1024