-
Notifications
You must be signed in to change notification settings - Fork 3
/
copy.cpp
171 lines (151 loc) · 5.44 KB
/
copy.cpp
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
/*
Copyright (c) 2015-2016 Advanced Micro Devices, Inc. All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include <stdio.h>
#include <hip/hip_runtime.h>
#define CHECK(cmd) \
{\
hipError_t error = cmd;\
if (error != hipSuccess) { \
fprintf(stderr, "error: '%s'(%d) at %s:%d\n", hipGetErrorString(error), error,__FILE__, __LINE__); \
exit(EXIT_FAILURE);\
}\
}
/*
* Square each element in the array A and write to array C.
*/
template <typename T>
__global__ void
vector_copy(T *C_d, T *A_d, size_t N)
{
size_t offset = (blockIdx.x * blockDim.x + threadIdx.x);
size_t stride = blockDim.x * gridDim.x ;
for (size_t i=offset; i<N; i+=stride) {
C_d[i] = A_d[i];
}
}
int main(int argc, char *argv[])
{
uint8_t *A_d, *C_d;
int deviceID = 1;
CHECK(hipSetDevice(deviceID));
hipDeviceProp_t props;
CHECK(hipGetDeviceProperties(&props, deviceID));
printf("info: running on device %s\n", props.name);
for (size_t N = 1024 * 1024; N < 1024 * 1024 * 1024; N < 128 ? N++ : N += 1024 * 1024 * 256) {
size_t Nbytes = N * sizeof(uint8_t);
printf("info: allocate mem (0x%010zx B)\n", Nbytes);
uint8_t *A_h = new uint8_t[Nbytes];
CHECK(A_h == 0 ? hipErrorMemoryAllocation : hipSuccess );
uint8_t *C_h = new uint8_t[Nbytes];
CHECK(C_h == 0 ? hipErrorMemoryAllocation : hipSuccess );
// Fill with i
for (size_t i = 0; i < N; i++) A_h[i] = i;
//printf("info: allocate device mem (0x%0zx B)\n", Nbytes);
CHECK(hipMalloc(&A_d, Nbytes));
CHECK(hipMalloc(&C_d, Nbytes));
#ifdef USE_EVENTS
hipEvent_t e1, e2, e3, e4;
CHECK(hipEventCreate(&e1));
CHECK(hipEventCreate(&e2));
CHECK(hipEventCreate(&e3));
CHECK(hipEventCreate(&e4));
CHECK(hipEventRecord(e1));
#else
#endif
//printf("info: copy Host2Device\n");
CHECK(hipMemcpy(A_d, A_h, Nbytes, hipMemcpyHostToDevice));
#ifdef USE_EVENTS
CHECK(hipEventRecord(e2));
#else
#endif
const unsigned blocks = 512;
const unsigned threadsPerBlock = 256;
//printf("info: launch 'vector_copy' kernel\n");
hipLaunchKernelGGL((vector_copy), dim3(blocks), dim3(threadsPerBlock), 0, 0, C_d, A_d, N);
#ifdef USE_EVENTS
CHECK(hipEventRecord(e3));
#else
CHECK(hipDeviceSynchronize());
#endif
//printf("info: copy Device2Host\n");
CHECK(hipMemcpy(C_h, C_d, Nbytes, hipMemcpyDeviceToHost));
#ifdef USE_EVENTS
CHECK(hipEventRecord(e4));
CHECK(hipEventSynchronize(e4));
#else
#endif
//printf("info: check result\n");
for (size_t i = 0; i < N; i++) {
if (C_h[i] != A_h[i]) {
fprintf(stderr, "mismatch at index %zu. Expected %x, actual %x\n", i, A_h[i], C_h[i]);
CHECK(hipErrorUnknown);
}
}
#define MBpSec(bytes, ms) ((bytes * 1000.0) / (ms * 1048576.0))
#ifdef USE_EVENTS
float ms;
CHECK(hipEventElapsedTime(&ms, e1, e2));
printf("info: copy-in %8.4f MB/Sec\n", MBpSec(Nbytes, ms));
CHECK(hipEventElapsedTime(&ms, e2, e3));
printf("info: kernel %8.4f MB/Sec\n", MBpSec(Nbytes, ms));
CHECK(hipEventElapsedTime(&ms, e3, e4));
printf("info: copy-out %8.4f MB/Sec\n", MBpSec(Nbytes, ms));
#endif
#if 0
int device_count;
CHECK(hipGetDeviceCount(&device_count));
if (device_count > 1) {
uint8_t *B_d;
hipEvent_t e5, e6;
CHECK(hipEventCreate(&e5));
CHECK(hipEventCreate(&e6));
CHECK(hipSetDevice(1));
CHECK(hipMalloc(&B_d, Nbytes));
CHECK(hipSetDevice(0));
CHECK(hipEventRecord(e5));
CHECK(hipMemcpy(B_d, A_d, Nbytes, hipMemcpyDeviceToDevice));
CHECK(hipEventRecord(e6));
CHECK(hipEventSynchronize(e6));
float ms;
CHECK(hipEventElapsedTime(&ms, e5, e6));
printf("info: copy-d2d %8.4f MB/Sec\n", MBpSec(Nbytes, ms));
uint8_t *B_h = new uint8_t[Nbytes];
CHECK(hipMemcpy(B_h, B_d, Nbytes, hipMemcpyDeviceToHost));
for (size_t i = 0; i < N; i++) {
if (C_h[i] != B_h[i]) {
printf("mismatch at index %zu. Expected %x, actual %x\n", i, A_h[i], C_h[i]);
CHECK(hipErrorUnknown);
}
}
delete[] B_h;
}
#endif
delete[] A_h;
delete[] C_h;
CHECK(hipFree(A_d));
CHECK(hipFree(C_d));
#ifdef USE_EVENTS
CHECK(hipEventDestroy(e1));
CHECK(hipEventDestroy(e2));
CHECK(hipEventDestroy(e3));
CHECK(hipEventDestroy(e4));
#endif
}
printf("PASSED!\n");
}