Skip to content

Instantly share code, notes, and snippets.

@99991
Created July 5, 2024 13:14
Show Gist options
  • Save 99991/7745f1e9634fc61b3dac65df14652238 to your computer and use it in GitHub Desktop.
Save 99991/7745f1e9634fc61b3dac65df14652238 to your computer and use it in GitHub Desktop.
Test CUDA
#include <cuda_runtime.h>
#include <iostream>
#define CHECK_CUDA_ERROR(call) \
do { \
cudaError_t err = call; \
if (err != cudaSuccess) { \
std::cerr << "CUDA error at " << __FILE__ << ":" << __LINE__ << " - " \
<< cudaGetErrorString(err) << " (" << err << ")" << std::endl; \
exit(EXIT_FAILURE); \
} \
} while (0)
int main() {
int deviceCount = 0;
CHECK_CUDA_ERROR(cudaGetDeviceCount(&deviceCount));
if (deviceCount == 0) {
std::cerr << "No CUDA devices found." << std::endl;
return EXIT_FAILURE;
}
CHECK_CUDA_ERROR(cudaSetDevice(0));
const size_t size = 1024 * sizeof(float); // 1024 floats
float* d_data = nullptr;
CHECK_CUDA_ERROR(cudaMalloc((void**)&d_data, size));
float* h_data = nullptr;
CHECK_CUDA_ERROR(cudaMallocHost((void**)&h_data, size));
CHECK_CUDA_ERROR(cudaMemset(d_data, 123, size));
CHECK_CUDA_ERROR(cudaFree(d_data));
CHECK_CUDA_ERROR(cudaFreeHost(h_data));
CHECK_CUDA_ERROR(cudaDeviceReset());
std::cout << "CUDA operations completed successfully." << std::endl;
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment