improved cuda error checking

This commit is contained in:
Djairo Hougee 2025-01-06 13:18:19 +01:00
parent edf4d9fd60
commit 04defb563f
5 changed files with 46 additions and 88 deletions

13
src/cuda_error.cpp Normal file
View File

@ -0,0 +1,13 @@
#include "cuda_error.h"
#include "cuda_runtime.h"
#include <iostream>
void check_cuda(cudaError_t res, char const* const func, const char* const file, int const line) {
if (res) {
std::cout << "CUDA encountered an error: " << cudaGetErrorString(res) << " in " << file << ":" << line << std::endl;
cudaDeviceReset();
exit(1);
}
}

10
src/cuda_error.h Normal file
View File

@ -0,0 +1,10 @@
#ifndef CUDA_ERROR_H
#define CUDA_ERROR_H
#include "cuda_runtime.h"
#define check_cuda_errors(val) check_cuda( (val), #val, __FILE__, __LINE__ )
void check_cuda(cudaError_t res, char const* const func, const char* const file, int const line);
#endif // CUDA_ERROR_H

View File

@ -119,7 +119,7 @@ void Window::tick() {
glfwSwapBuffers(this->window);
glfwPollEvents();
std::cout << "done ticking";
std::cout << "done ticking\n";
}

View File

@ -1,5 +1,6 @@
#include "Quad.h"
#include "cuda_error.h"
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include "cuda_runtime.h"
@ -71,22 +72,12 @@ void Quad::make_fbo(){
}
Quad::~Quad() {
int res = cudaGraphicsUnregisterResource(CGR);
if (res) {
std::cout << "CUDA error while deregistering the graphics resource: " << res;
cudaDeviceReset();
exit(1);
}
check_cuda_errors(cudaGraphicsUnregisterResource(CGR));
};
void Quad::cuda_init(float* data) {
int res = cudaGraphicsGLRegisterBuffer(&this->CGR, this->PBO, cudaGraphicsRegisterFlagsNone);
if (res) {
std::cout << "CUDA error while registering the graphics resource: " << res;
cudaDeviceReset();
exit(1);
}
check_cuda_errors(cudaGraphicsGLRegisterBuffer(&this->CGR, this->PBO, cudaGraphicsRegisterFlagsNone));
this->renderer = std::make_unique<Raycaster>(this->CGR, this->w, this->h, data);
};
@ -117,21 +108,10 @@ void Quad::resize(unsigned int w, unsigned int h) {
glBindFramebuffer(GL_FRAMEBUFFER, 0);
if (this->renderer != nullptr) {
// TODO: probably make a function for the cuda error checking
int res = cudaGraphicsUnregisterResource(CGR);
if (res) {
std::cout << "CUDA error while deregistering the graphics resource: " << res;
cudaDeviceReset();
exit(1);
}
check_cuda_errors(cudaGraphicsUnregisterResource(CGR));
check_cuda_errors(cudaGraphicsGLRegisterBuffer(&this->CGR, this->PBO, cudaGraphicsRegisterFlagsNone));
res = cudaGraphicsGLRegisterBuffer(&this->CGR, this->PBO, cudaGraphicsRegisterFlagsNone);
if (res) {
std::cout << "CUDA error while registering the graphics resource: " << res;
cudaDeviceReset();
exit(1);
}
this->renderer->resources = this->CGR;
this->renderer->resize(w, h);
this->renderer->resources = this->CGR;
this->renderer->resize(w, h);
}
};

View File

@ -5,6 +5,7 @@
#include "linalg/linalg.h"
#include "consts.h"
#include "cuda_error.h"
#include "shading.h"
#include <iostream>
#include "objs/sphere.h"
@ -130,8 +131,8 @@ __global__ void raycastKernel(float* volumeData, FrameBuffer framebuffer) {
Raycaster::Raycaster(cudaGraphicsResource_t resources, int w, int h, float* data) {
this->resources = resources;
this->w = h;
this->w = h;
this->w = w;
this->h = h;
this->fb = new FrameBuffer(w, h);
this->data = data;
@ -139,68 +140,27 @@ Raycaster::Raycaster(cudaGraphicsResource_t resources, int w, int h, float* data
// camera_info = CameraInfo(Vec3(0.0f, 0.0f, 0.0f), Vec3(0.0f, 0.0f, 0.0f), 90.0f, (float) w, (float) h);
// d_camera = thrust::device_new<Camera*>();
int res = cudaDeviceSynchronize();
if (res) {
std::cout << "CUDA error while synchronizing device: " << res;
cudaDeviceReset();
exit(1);
}
res = cudaDeviceSynchronize();
if (res) {
std::cout << "CUDA error while synchronizing device: " << res;
cudaDeviceReset();
exit(1);
}
check_cuda_errors(cudaDeviceSynchronize());
}
void Raycaster::render() {
int res = cudaGraphicsMapResources(1, &this->resources);
if (res) {
std::cout << "CUDA error while mapping graphic resource: " << res;
cudaDeviceReset();
exit(1);
}
res = cudaGraphicsResourceGetMappedPointer((void**)&(this->fb->buffer), &(this->fb->buffer_size), resources);
if (res) {
std::cout << "CUDA error while fetching resource pointer: " << res;
cudaDeviceReset();
exit(1);
}
check_cuda_errors(cudaGraphicsMapResources(1, &this->resources));
check_cuda_errors(cudaGraphicsResourceGetMappedPointer((void**)&(this->fb->buffer), &(this->fb->buffer_size), resources));
// FIXME: might not be the best parallelization configuraiton
int tx = 32;
int ty = 32;
dim3 blocks(this->w / tx + 1, this->h / ty + 1);
dim3 threads(tx, ty);
int tx = 16;
int ty = 16;
dim3 threadSize(this->w / tx + 1, this->h / ty + 1);
dim3 blockSize(tx, ty);
// TODO: pass camera info at some point
// frame buffer is implicitly copied to the device each frame
raycastKernel<<<blocks, threads>>> (this->data, *this->fb);
raycastKernel<<<threadSize, blockSize>>> (this->data, *this->fb);
cudaError_t err = cudaGetLastError();
if (err) {
std::cout << "CUDA error while raycasting: " << cudaGetErrorString(err);
cudaDeviceReset();
exit(1);
}
res = cudaDeviceSynchronize();
if (res) {
std::cout << "CUDA error while synchronizing device: " << res;
cudaDeviceReset();
exit(1);
}
res = cudaGraphicsUnmapResources(1, &this->resources);
if (res) {
std::cout << "CUDA error while unmapping a resource: " << res;
cudaDeviceReset();
exit(1);
}
check_cuda_errors(cudaGetLastError());
check_cuda_errors(cudaDeviceSynchronize());
check_cuda_errors(cudaGraphicsUnmapResources(1, &this->resources));
}
@ -218,10 +178,5 @@ void Raycaster::resize(int w, int h) {
dim3 blocks(w / tx + 1, h / ty + 1);
dim3 threads(tx, ty);
int res = cudaDeviceSynchronize();
if (res != 0) {
std::cout << "CUDA error while synchronizing device: " << res;
cudaDeviceReset();
exit(1);
}
check_cuda_errors(cudaDeviceSynchronize());
}