chundoong-lab-ta/APWS23/ans/pinned_memory_ans.cu

50 lines
1.8 KiB
Plaintext
Raw Normal View History

2023-02-14 20:17:53 +09:00
#include <chrono>
2023-02-15 01:33:28 +09:00
#include <cstdio>
2023-02-14 20:17:53 +09:00
2023-02-15 01:33:28 +09:00
#define CHECK_CUDA(call) \
do { \
cudaError_t status_ = call; \
if (status_ != cudaSuccess) { \
fprintf(stderr, "CUDA error (%s:%d): %s:%s\n", __FILE__, __LINE__, \
cudaGetErrorName(status_), cudaGetErrorString(status_)); \
exit(EXIT_FAILURE); \
} \
2023-02-14 20:17:53 +09:00
} while (0)
2023-02-15 01:33:28 +09:00
__global__ void init(int *a) {
2023-02-14 20:17:53 +09:00
int i = blockIdx.x * blockDim.x + threadIdx.x;
a[i] = i;
}
2023-02-15 01:33:28 +09:00
int main() {
2023-02-14 20:17:53 +09:00
int *d_a, *a, *a_pinned;
CHECK_CUDA(cudaMalloc(&d_a, sizeof(int) * (1 << 20)));
2023-02-15 01:33:28 +09:00
2023-02-14 20:17:53 +09:00
CHECK_CUDA(cudaMallocHost(&a_pinned, sizeof(int) * (1 << 20)));
2023-02-15 01:33:28 +09:00
a = (int *) malloc(sizeof(int) * (1 << 20));
2023-02-14 20:17:53 +09:00
2023-02-15 01:33:28 +09:00
for (int i = 0; i < (1 << 20); ++i) { a[i] = i; }
2023-02-14 20:17:53 +09:00
init<<<(1 << 15), 32>>>(d_a);
CHECK_CUDA(cudaDeviceSynchronize());
{
auto start = std::chrono::system_clock::now();
2023-02-15 01:33:28 +09:00
CHECK_CUDA(
cudaMemcpy(a, d_a, sizeof(int) * (1 << 20), cudaMemcpyDeviceToHost));
2023-02-14 20:17:53 +09:00
auto end = std::chrono::system_clock::now();
std::chrono::duration<double> diff = end - start;
2023-02-15 01:33:28 +09:00
printf("Pageable memory bandwidth: %lf GB/s\n",
(sizeof(int) / diff.count() / 1000.));
2023-02-14 20:17:53 +09:00
}
{
auto start = std::chrono::system_clock::now();
2023-02-15 01:33:28 +09:00
CHECK_CUDA(cudaMemcpy(a_pinned, d_a, sizeof(int) * (1 << 20),
cudaMemcpyDeviceToHost));
2023-02-14 20:17:53 +09:00
auto end = std::chrono::system_clock::now();
std::chrono::duration<double> diff = end - start;
2023-02-15 01:33:28 +09:00
printf("Pinned memory bandwidth: %lf GB/s\n",
(sizeof(int) / diff.count() / 1000.));
2023-02-14 20:17:53 +09:00
}
return 0;
}