improved cuda error checking
This commit is contained in:
parent
edf4d9fd60
commit
04defb563f
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -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
|
||||||
|
|
||||||
|
|
@ -119,7 +119,7 @@ void Window::tick() {
|
||||||
glfwSwapBuffers(this->window);
|
glfwSwapBuffers(this->window);
|
||||||
glfwPollEvents();
|
glfwPollEvents();
|
||||||
|
|
||||||
std::cout << "done ticking";
|
std::cout << "done ticking\n";
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
#include "Quad.h"
|
#include "Quad.h"
|
||||||
|
|
||||||
|
#include "cuda_error.h"
|
||||||
#include <glad/glad.h>
|
#include <glad/glad.h>
|
||||||
#include <GLFW/glfw3.h>
|
#include <GLFW/glfw3.h>
|
||||||
#include "cuda_runtime.h"
|
#include "cuda_runtime.h"
|
||||||
|
|
@ -71,22 +72,12 @@ void Quad::make_fbo(){
|
||||||
}
|
}
|
||||||
|
|
||||||
Quad::~Quad() {
|
Quad::~Quad() {
|
||||||
int res = cudaGraphicsUnregisterResource(CGR);
|
check_cuda_errors(cudaGraphicsUnregisterResource(CGR));
|
||||||
if (res) {
|
|
||||||
std::cout << "CUDA error while deregistering the graphics resource: " << res;
|
|
||||||
cudaDeviceReset();
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
void Quad::cuda_init(float* data) {
|
void Quad::cuda_init(float* data) {
|
||||||
int res = cudaGraphicsGLRegisterBuffer(&this->CGR, this->PBO, cudaGraphicsRegisterFlagsNone);
|
check_cuda_errors(cudaGraphicsGLRegisterBuffer(&this->CGR, this->PBO, cudaGraphicsRegisterFlagsNone));
|
||||||
if (res) {
|
|
||||||
std::cout << "CUDA error while registering the graphics resource: " << res;
|
|
||||||
cudaDeviceReset();
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
this->renderer = std::make_unique<Raycaster>(this->CGR, this->w, this->h, data);
|
this->renderer = std::make_unique<Raycaster>(this->CGR, this->w, this->h, data);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -117,20 +108,9 @@ void Quad::resize(unsigned int w, unsigned int h) {
|
||||||
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
||||||
|
|
||||||
if (this->renderer != nullptr) {
|
if (this->renderer != nullptr) {
|
||||||
// TODO: probably make a function for the cuda error checking
|
check_cuda_errors(cudaGraphicsUnregisterResource(CGR));
|
||||||
int res = cudaGraphicsUnregisterResource(CGR);
|
check_cuda_errors(cudaGraphicsGLRegisterBuffer(&this->CGR, this->PBO, cudaGraphicsRegisterFlagsNone));
|
||||||
if (res) {
|
|
||||||
std::cout << "CUDA error while deregistering the graphics resource: " << res;
|
|
||||||
cudaDeviceReset();
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
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->resources = this->CGR;
|
||||||
this->renderer->resize(w, h);
|
this->renderer->resize(w, h);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@
|
||||||
|
|
||||||
#include "linalg/linalg.h"
|
#include "linalg/linalg.h"
|
||||||
#include "consts.h"
|
#include "consts.h"
|
||||||
|
#include "cuda_error.h"
|
||||||
#include "shading.h"
|
#include "shading.h"
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include "objs/sphere.h"
|
#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) {
|
Raycaster::Raycaster(cudaGraphicsResource_t resources, int w, int h, float* data) {
|
||||||
this->resources = resources;
|
this->resources = resources;
|
||||||
this->w = h;
|
this->w = w;
|
||||||
this->w = h;
|
this->h = h;
|
||||||
|
|
||||||
this->fb = new FrameBuffer(w, h);
|
this->fb = new FrameBuffer(w, h);
|
||||||
this->data = data;
|
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);
|
// 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*>();
|
// d_camera = thrust::device_new<Camera*>();
|
||||||
|
|
||||||
int res = cudaDeviceSynchronize();
|
check_cuda_errors(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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void Raycaster::render() {
|
void Raycaster::render() {
|
||||||
int res = cudaGraphicsMapResources(1, &this->resources);
|
check_cuda_errors(cudaGraphicsMapResources(1, &this->resources));
|
||||||
if (res) {
|
check_cuda_errors(cudaGraphicsResourceGetMappedPointer((void**)&(this->fb->buffer), &(this->fb->buffer_size), resources));
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
// FIXME: might not be the best parallelization configuraiton
|
// FIXME: might not be the best parallelization configuraiton
|
||||||
int tx = 32;
|
int tx = 16;
|
||||||
int ty = 32;
|
int ty = 16;
|
||||||
|
dim3 threadSize(this->w / tx + 1, this->h / ty + 1);
|
||||||
dim3 blocks(this->w / tx + 1, this->h / ty + 1);
|
dim3 blockSize(tx, ty);
|
||||||
dim3 threads(tx, ty);
|
|
||||||
|
|
||||||
// TODO: pass camera info at some point
|
// TODO: pass camera info at some point
|
||||||
// frame buffer is implicitly copied to the device each frame
|
// 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();
|
check_cuda_errors(cudaGetLastError());
|
||||||
if (err) {
|
check_cuda_errors(cudaDeviceSynchronize());
|
||||||
std::cout << "CUDA error while raycasting: " << cudaGetErrorString(err);
|
check_cuda_errors(cudaGraphicsUnmapResources(1, &this->resources));
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -218,10 +178,5 @@ void Raycaster::resize(int w, int h) {
|
||||||
dim3 blocks(w / tx + 1, h / ty + 1);
|
dim3 blocks(w / tx + 1, h / ty + 1);
|
||||||
dim3 threads(tx, ty);
|
dim3 threads(tx, ty);
|
||||||
|
|
||||||
int res = cudaDeviceSynchronize();
|
check_cuda_errors(cudaDeviceSynchronize());
|
||||||
if (res != 0) {
|
|
||||||
std::cout << "CUDA error while synchronizing device: " << res;
|
|
||||||
cudaDeviceReset();
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue