Skip to content

Instantly share code, notes, and snippets.

@anirul
Created April 7, 2021 09:28
Show Gist options
  • Save anirul/a09bdba67caa89428bfbd398acbb59ab to your computer and use it in GitHub Desktop.
Save anirul/a09bdba67caa89428bfbd398acbb59ab to your computer and use it in GitHub Desktop.
CUDA test on the fly
#include <cuda_runtime.h>
#include <vector>
#include <random>
#include <iostream>
__global__ void kernel(const int* a, const int* b, int* c)
{
int index = threadIdx.x;
c[index] = a[index] * b[index];
}
std::ostream& operator<<(std::ostream& os, const std::vector<int>& vec)
{
os << "{";
for (const auto& value : vec)
{
os << value;
os << ", ";
}
os << "}";
return os;
}
void cuda_error(int line, cudaError_t error)
{
if (error == cudaSuccess) return;
std::cerr << "Error at line: " << line << " (" << error << ")";
exit(0);
}
int main()
{
std::vector<int> h_a(100, 0);
std::vector<int> h_b(100, 0);
std::vector<int> h_c(100, 0);
for (auto& value : h_a)
{
value = rand();
}
for (auto& value : h_b)
{
value = rand();
}
int* d_a;
int* d_b;
int* d_c;
cudaError_t cudaStatus;
cudaStatus = cudaSetDevice(0);
cuda_error(__LINE__ - 1, cudaStatus);
cudaStatus = cudaMalloc(&d_a, 100 * sizeof(int));
cuda_error(__LINE__ - 1, cudaStatus);
cudaStatus = cudaMalloc(&d_b, 100 * sizeof(int));
cuda_error(__LINE__ - 1, cudaStatus);
cudaStatus = cudaMalloc(&d_c, 100 * sizeof(int));
cuda_error(__LINE__ - 1, cudaStatus);
cudaStatus = cudaMemcpy(
d_a,
h_a.data(),
100 * sizeof(int),
cudaMemcpyHostToDevice);
cuda_error(__LINE__ - 5, cudaStatus);
cudaStatus = cudaMemcpy(
d_b,
h_b.data(),
100 * sizeof(int),
cudaMemcpyHostToDevice);
cuda_error(__LINE__ - 5, cudaStatus);
kernel<<<1, 100>>>(d_a, d_b, d_c);
cudaStatus = cudaGetLastError();
cuda_error(__LINE__ - 1, cudaStatus);
cudaStatus = cudaDeviceSynchronize();
cuda_error(__LINE__ - 1, cudaStatus);
cudaStatus = cudaMemcpy(
h_c.data(),
d_c,
100 * sizeof(int),
cudaMemcpyDeviceToHost);
cuda_error(__LINE__ - 5, cudaStatus);
std::cout << h_a << std::endl;
std::cout << h_b << std::endl;
std::cout << h_c << std::endl;
cudaFree(d_a);
cudaFree(d_b);
cudaFree(d_c);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment