#include "cuda_runtime.h"
#include "device_launch_parameters.h"
#include <stdio.h>
//#include <cmath>
#include<stdint.h>
//typedef BYTE uint16_t;
//typedef int uint16_t;
#include "CudaKernelInfo.h"
#include <iostream>
//#include <iostream>
using namespace std;
cudaError_t addWithCuda(int *c, const int *a, const int *b, unsigned int size);
__global__ void addKernel(int *c, const int *a, const int *b)
{
int i = threadIdx.x;
c[i] = a[i] + b[i];
}
/*__device__ inline float lerp(float v0, float v1, float t)
{
return fmaf(t, v1, fmaf(-t, v0, v0));
}*/
__device__ float lerp(float v0, float v1, float t)
{
return fmaf(t, v1, fmaf(-t, v0, v0));
}
__global__ void VolumeProcessing_resizeAndMaskKernel(
uint16_t * out_ptr, const int out_stride,
const float * in_ptr, const int in_stride,
const int out_size_x, const int out_size_y,
const int in_size_x, const int in_size_y, const int in_size_z,
int slice_index, float resample_step, float radius_sqr,
const int max_voxel_value)
{
int ox = blockIdx.x*blockDim.x + threadIdx.x;
int oy = blockIdx.y*blockDim.y + threadIdx.y;
//std::cout << ox << " " << oy;
if (ox >= out_size_x || oy >= out_size_y)
{
//cout << "exceed limit";
return;
}
float dx = fmaf(0.5f, out_size_x, float(-ox) - 0.5f);
float dy = fmaf(0.5f, out_size_y, float(-oy) - 0.5f);
float d = fmaf(dx, dx, dy*dy);
float value = 0.0f;
if (d <= radius_sqr)
{
// clamp to edge
float ix = resample_step * ox;
float iy = resample_step * oy;
float iz = resample_step * slice_index;
// 0: first, 1: next voxel
int x0 = min(int(ix) + 0, in_size_x - 1);
int x1 = min(int(ix) + 1, in_size_x - 1);
int y0 = min(int(iy) + 0, in_size_y - 1);
int y1 = min(int(iy) + 1, in_size_y - 1);
int z0 = min(int(iz) + 0, in_size_z - 1);
int z1 = min(int(iz) + 1, in_size_z - 1);
// weight of next voxel
float t = min(ix - x0, 1.0f);
float u = min(iy - y0, 1.0f);
float v = min(iz - z0, 1.0f);
float xy0 = lerp(
lerp(in_ptr[x0 + (y0 + z0 * in_size_y) * in_stride]
, in_ptr[x1 + (y0 + z0 * in_size_y) * in_stride], t),
lerp(in_ptr[x0 + (y1 + z0 * in_size_y) * in_stride]
, in_ptr[x1 + (y1 + z0 * in_size_y) * in_stride], t), u);
float xy1 = lerp(
lerp(in_ptr[x0 + (y0 + z1 * in_size_y) * in_stride]
, in_ptr[x1 + (y0 + z1 * in_size_y) * in_stride], t),
lerp(in_ptr[x0 + (y1 + z1 * in_size_y) * in_stride]
, in_ptr[x1 + (y1 + z1 * in_size_y) * in_stride], t), u);
value = lerp(xy0, xy1, v);
}
out_ptr[ox + oy * out_stride] = uint16_t(min(max(0.5f, value + 0.5f), 0.5f + max_voxel_value));
}
int main()
{
/* const int arraySize = 5;
const int a[arraySize] = { 1, 2, 3, 4, 5 };
const int b[arraySize] = { 10, 20, 30, 40, 50 };
int c[arraySize] = { 0 };
// Add vectors in parallel.
cudaError_t cudaStatus = addWithCuda(c, a, b, arraySize);
if (cudaStatus != cudaSuccess) {
fprintf(stderr, "addWithCuda failed!");
return 1;
}
printf("{1,2,3,4,5} + {10,20,30,40,50} = {%d,%d,%d,%d,%d}\n",
c[0], c[1], c[2], c[3], c[4]); */
// cudaDeviceReset must be called before exiting in order for profiling and
// tracing tools such as Nsight and Visual Profiler to show complete traces.
// test resample
// add by yyy
cudaError_t cudaStatus;
cudaStatus = cudaSetDevice(0);
if (cudaStatus != cudaSuccess) {
fprintf(stderr, "cudaSetDevice failed! Do you have a CUDA-capable GPU installed?");
}
uint16_t *out_ptr;
const int out_stride = 448;
//const float *in_ptr;
float *in_ptr;
//void *v_in_ptr;
const int in_stride = 480;
const int out_size_x = 420;
const int out_size_y = 420;
const int in_size_x = 420;
const int in_size_y = 420;
const int in_size_z = 250;
int slice_index = 0;
float resample_step = 1;
float radius_sqr = 209.5;
const int max_voxel_value = 8191;
//CUDACHECK
//cudaMemset
out_ptr = new uint16_t[420 * 420];
//float *t1 = new float[420 * 420 * 250];
// 执行到这里报错 an illegal memory access was encountered
cudaStatus=cudaMalloc((void**)&in_ptr, 420 * 420 * 250 * sizeof(float));
if (cudaStatus != cudaSuccess) {
fprintf(stderr, "addKernel launch failed: %s\n", cudaGetErrorString(cudaStatus));
}
//cudaMemcpy(v_in_ptr, t1, 420 * 420 * 250 * sizeof(float), cudaMemcpyHostToDevice);
//cudaMemcpy()
//CudaKernelInfo launch(420, 420);
//CudaKernelInfo launch(slice.sizeX(), slice.sizeY());
// block size
CudaKernelInfo launch(420, 420);
//cout << launch.gridSize() << " " << launch.threadBlockSize();
VolumeProcessing_resizeAndMaskKernel<<<launch.gridSize(),launch.threadBlockSize()>>>(out_ptr, out_stride, in_ptr, in_stride,
out_size_x, out_size_y, in_size_x, in_size_y, in_size_z, slice_index, resample_step, radius_sqr, max_voxel_value);
cudaStatus = cudaDeviceSynchronize();
if (cudaStatus != cudaSuccess) {
fprintf(stderr, "cudaDeviceSynchronize returned error code %d after launching addKernel!\n", cudaStatus);
}
/*cudaStatus = cudaDeviceReset();
if (cudaStatus != cudaSuccess) {
fprintf(stderr, "cudaDeviceReset failed!");
return 1;
}*/
//cudaFreeArray(in_ptr);
delete[]out_ptr;
cudaFree(in_ptr);
return 0;
}
// Helper function for using CUDA to add vectors in parallel.
cudaError_t addWithCuda(int *c, const int *a, const int *b, unsigned int size)
{
int *dev_a = 0;
int *dev_b = 0;
int *dev_c = 0;
cudaError_t cudaStatus;
// Choose which GPU to run on, change this on a multi-GPU system.
cudaStatus = cudaSetDevice(0);
if (cudaStatus != cudaSuccess) {
fprintf(stderr, "cudaSetDevice failed! Do you have a CUDA-capable GPU installed?");
goto Error;
}
// Allocate GPU buffers for three vectors (two input, one output) .
cudaStatus = cudaMalloc((void**)&dev_c, size * sizeof(int));
if (cudaStatus != cudaSuccess) {
fprintf(stderr, "cudaMalloc failed!");
goto Error;
}
cudaStatus = cudaMalloc((void**)&dev_a, size * sizeof(int));
if (cudaStatus != cudaSuccess) {
fprintf(stderr, "cudaMalloc failed!");
goto Error;
}
cudaStatus = cudaMalloc((void**)&dev_b, size * sizeof(int));
if (cudaStatus != cudaSuccess) {
fprintf(stderr, "cudaMalloc failed!");
goto Error;
}
// Copy input vectors from host memory to GPU buffers.
cudaStatus = cudaMemcpy(dev_a, a, size * sizeof(int), cudaMemcpyHostToDevice);
if (cudaStatus != cudaSuccess) {
fprintf(stderr, "cudaMemcpy failed!");
goto Error;
}
cudaStatus = cudaMemcpy(dev_b, b, size * sizeof(int), cudaMemcpyHostToDevice);
if (cudaStatus != cudaSuccess) {
fprintf(stderr, "cudaMemcpy failed!");
goto Error;
}
// Launch a kernel on the GPU with one thread for each element.
addKernel<<<1, size>>>(dev_c, dev_a, dev_b);
// Check for any errors launching the kernel
cudaStatus = cudaGetLastError();
if (cudaStatus != cudaSuccess) {
fprintf(stderr, "addKernel launch failed: %s\n", cudaGetErrorString(cudaStatus));
goto Error;
}
// cudaDeviceSynchronize waits for the kernel to finish, and returns
// any errors encountered during the launch.
cudaStatus = cudaDeviceSynchronize();
if (cudaStatus != cudaSuccess) {
fprintf(stderr, "cudaDeviceSynchronize returned error code %d after launching addKernel!\n", cudaStatus);
goto Error;
}
// Copy output vector from GPU buffer to host memory.
cudaStatus = cudaMemcpy(c, dev_c, size * sizeof(int), cudaMemcpyDeviceToHost);
if (cudaStatus != cudaSuccess) {
fprintf(stderr, "cudaMemcpy failed!");
goto Error;
}
Error:
cudaFree(dev_c);
cudaFree(dev_a);
cudaFree(dev_b);
return cudaStatus;
}