Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Python/.idea/
Python/devtest.py
Python/dist/
Python/tigre/utilities/cuda_interface/*.cpp
Python/tigre/plugins/*/*.cpp
.ipynb_checkpoints
# Object files
*.o
Expand Down
3 changes: 2 additions & 1 deletion Common/CUDA/TIGRE_common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
#include <string>
#include <vector>
#include "TIGRE_common.hpp"
// Prints like MATLAB's mexPrintf. It used to prefix every message with
// "Common/CUDA/TIGRE_common.cpp (9): ", the location of this line, not the caller.
void mexPrintf(const char* format, ...) {
PRINT_HERE("");
va_list argpointer;
va_start(argpointer, format);
vprintf(format, argpointer);
Expand Down
111 changes: 62 additions & 49 deletions Common/CUDA/plugins/pCT/improvedForwardProjections.cu
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

#include <cuda.h>
#include "../../TIGRE_common.hpp"
#include "../../tigre_release_on_error.hpp"
#include <cuda_runtime_api.h>
#include "improvedForwardProjections.hpp"
#include <algorithm>
Expand Down Expand Up @@ -497,8 +498,8 @@ __global__ void ParticleKernel(float* dhist1, float* dhist2, float* devicePosIn,
int indX, indY, linInd;
float tOld = 0.0;
if (count==0){
indX = int(pos1[0]/pix[0]+dimX/2.); // REPLACE: pos1 by pos0
indY = int(pos1[1]/pix[1]+dimY/2.);
indX = (int)floorf(pos1[0]/pix[0]+dimX/2.); // REPLACE: pos1 by pos0
indY = (int)floorf(pos1[1]/pix[1]+dimY/2.);

if ((0 <= indX) && (indX < dSizeX) && (0 <= indY) && (indY < dSizeY)){
linInd = indY + indX*(dSizeY);
Expand All @@ -511,8 +512,8 @@ __global__ void ParticleKernel(float* dhist1, float* dhist2, float* devicePosIn,
for(int i= 0; i<=count; i++){
lk = (InterceptsVec[i]- tOld)*lenZ;
if(tOld == 0){
indX = int(d[0]/pix[0] +dimX/2);
indY = int(d[1]/pix[1] +dimY/2);
indX = (int)floorf(d[0]/pix[0] +dimX/2);
indY = (int)floorf(d[1]/pix[1] +dimY/2);
linInd = indY + indX*(dSizeY);

if ((0 <= indX) && (indX < dSizeX) && (0 <= indY) && (indY < dSizeY)){
Expand All @@ -524,8 +525,8 @@ __global__ void ParticleKernel(float* dhist1, float* dhist2, float* devicePosIn,

}else if(i == count){
lk = lenZ - InterceptsVec[i-1]*lenZ;
indX = int(pos1[0]/pix[0] +dimX/2);
indY = int(pos1[1]/pix[1] +dimY/2);
indX = (int)floorf(pos1[0]/pix[0] +dimX/2);
indY = (int)floorf(pos1[1]/pix[1] +dimY/2);

if ((0 <= indX) && (indX < dSizeX) && (0 <= indY) && (indY < dSizeY)){
linInd = indY + indX*(dSizeY);
Expand All @@ -534,8 +535,8 @@ __global__ void ParticleKernel(float* dhist1, float* dhist2, float* devicePosIn,
}

}else{
indX = int(cspline(InterceptsVec[i] - eps, a[0], b[0], c[0], d[0])/pix[0] +dimX/2);
indY = int(cspline(InterceptsVec[i] - eps, a[1], b[1], c[1], d[1])/pix[1] +dimY/2);
indX = (int)floorf(cspline(InterceptsVec[i] - eps, a[0], b[0], c[0], d[0])/pix[0] +dimX/2);
indY = (int)floorf(cspline(InterceptsVec[i] - eps, a[1], b[1], c[1], d[1])/pix[1] +dimY/2);

if ((0 <= indX) && (indX < dSizeX) && (0 <= indY) && (indY < dSizeY)){
linInd = indY + indX*(dSizeY);
Expand Down Expand Up @@ -662,8 +663,8 @@ else{

// ---------------------------------------- Start with SL from detector to hull
if (countIn == 0){
indX = int(initpos[0]/pix[0] + dimX/2.);
indY = int(initpos[1]/pix[1] + dimY/2.);
indX = (int)floorf(initpos[0]/pix[0] + dimX/2.);
indY = (int)floorf(initpos[1]/pix[1] + dimY/2.);
lk = HullIn[2] - initpos[2];
if ((0 <= indX) && (indX < dSizeX) && (0 <= indY) && (indY < dSizeY)){
linInd = indY + indX*(dSizeY);
Expand All @@ -676,8 +677,8 @@ else{
for(int i= 0; i<=countIn; i++){
lk = InterceptsVecIn[i] - tOld;
if(i == 0){
indX = int(initpos[0]/pix[0] + dimX/2.);
indY = int(initpos[1]/pix[1] + dimY/2.);
indX = (int)floorf(initpos[0]/pix[0] + dimX/2.);
indY = (int)floorf(initpos[1]/pix[1] + dimY/2.);
if ((0 <= indX) && (indX < dSizeX) && (0 <= indY) && (indY < dSizeY) && (0 < lk) && (lk < (HullIn[2]-initpos[2]))){
linInd = indY + indX*(dSizeY);
atomicAdd(&dhist1[linInd], weight_air_in*(lk/lenZ)*(lk/lenZ)*p_wepl[protonIndex]);
Expand All @@ -687,8 +688,8 @@ else{
}
else if(i == countIn){
lk = HullIn[2] - InterceptsVecIn[i-1];
indX = int(HullIn[0]/pix[0] + dimX/2.);
indY = int(HullIn[1]/pix[1] + dimY/2.);
indX = (int)floorf(HullIn[0]/pix[0] + dimX/2.);
indY = (int)floorf(HullIn[1]/pix[1] + dimY/2.);
if ((0 <= indX) && (indX < dSizeX) && (0 <= indY) && (indY < dSizeY) && (0 < lk) && (lk < (HullIn[2]-initpos[2]))){
linInd = indY + indX*(dSizeY);
atomicAdd(&dhist1[linInd], weight_air_in*(lk/lenZ)*(lk/lenZ)*p_wepl[protonIndex]);
Expand All @@ -697,8 +698,8 @@ else{
}

else{
indX = int(((initdir[0]*(InterceptsVecIn[i]-eps) + (initpos[0] - initdir[0] * initpos[2])))/pix[0] + dimX/2.);
indY = int(((initdir[1]*(InterceptsVecIn[i]-eps) + (initpos[1] - initdir[1] * initpos[2])))/pix[1] + dimY/2.);
indX = (int)floorf(((initdir[0]*(InterceptsVecIn[i]-eps) + (initpos[0] - initdir[0] * initpos[2])))/pix[0] + dimX/2.);
indY = (int)floorf(((initdir[1]*(InterceptsVecIn[i]-eps) + (initpos[1] - initdir[1] * initpos[2])))/pix[1] + dimY/2.);
if ((0 <= indX) && (indX < dSizeX) && (0 <= indY) && (indY < dSizeY) && (0 < lk) && (lk < (HullIn[2]-initpos[2]))){
linInd = indY + indX*(dSizeY);
atomicAdd(&dhist1[linInd], weight_air_in*(lk/lenZ)*(lk/lenZ)*p_wepl[protonIndex]);
Expand All @@ -712,8 +713,8 @@ else{

tOld = 0.0;
if (countHull==0){
indX = int(HullIn[0]/pix[0] + dimX/2.);
indY = int(HullIn[1]/pix[1] + dimY/2.);
indX = (int)floorf(HullIn[0]/pix[0] + dimX/2.);
indY = (int)floorf(HullIn[1]/pix[1] + dimY/2.);
lk = HullOut[2] - HullIn[2];
if ((0 <= indX) && (indX < dSizeX) && (0 <= indY) && (indY < dSizeY)){
linInd = indY + indX*(dSizeY);
Expand All @@ -725,8 +726,8 @@ else{
for(int i= 0; i<=countHull; i++){
lk = (InterceptsVecHull[i] - tOld)*(HullOut[2] - HullIn[2]);
if(tOld == 0){
indX = int(d[0]/pix[0] + dimX/2.);
indY = int(d[1]/pix[1] + dimY/2.);
indX = (int)floorf(d[0]/pix[0] + dimX/2.);
indY = (int)floorf(d[1]/pix[1] + dimY/2.);
linInd = indY + indX*(dSizeY);

if ((0 <= indX) && (indX < dSizeX) && (0 <= indY) && (indY < dSizeY) && (0 < lk) && (lk < (HullOut[2]-HullIn[2]))){
Expand All @@ -738,8 +739,8 @@ else{

}else if(i == countHull){
lk = (HullOut[2] - HullIn[2]) - InterceptsVecHull[i-1]*(HullOut[2] - HullIn[2]);
indX = int(pos1[0]/pix[0] + dimX/2.);
indY = int(pos1[1]/pix[1] + dimY/2.);
indX = (int)floorf(pos1[0]/pix[0] + dimX/2.);
indY = (int)floorf(pos1[1]/pix[1] + dimY/2.);

if ((0 <= indX) && (indX < dSizeX) && (0 <= indY) && (indY < dSizeY) && (0 < lk) && (lk < (HullOut[2]-HullIn[2]))){
linInd = indY + indX*(dSizeY);
Expand All @@ -748,8 +749,8 @@ else{
}

}else{
indX = int(cspline(InterceptsVecHull[i] -eps, a[0], b[0], c[0], d[0])/pix[0] + dimX/2.);
indY = int(cspline(InterceptsVecHull[i] -eps, a[1], b[1], c[1], d[1])/pix[1] + dimY/2.);
indX = (int)floorf(cspline(InterceptsVecHull[i] -eps, a[0], b[0], c[0], d[0])/pix[0] + dimX/2.);
indY = (int)floorf(cspline(InterceptsVecHull[i] -eps, a[1], b[1], c[1], d[1])/pix[1] + dimY/2.);

if ((0 <= indX) && (indX < dSizeX) && (0 <= indY) && (indY < dSizeY) && (0 < lk) && (lk < (HullOut[2]-HullIn[2]))){
linInd = indY + indX*(dSizeY);
Expand All @@ -765,8 +766,8 @@ else{
// --------------------------- SL from hull to detector
tOld = HullOut[2];
if (countOut == 0){
indX = int(exitpos[0]/pix[0] + dimX/2.);
indY = int(exitpos[1]/pix[1] + dimY/2.);
indX = (int)floorf(exitpos[0]/pix[0] + dimX/2.);
indY = (int)floorf(exitpos[1]/pix[1] + dimY/2.);
lk = exitpos[2] - HullOut[2];
if ((0 <= indX) && (indX < dSizeX) && (0 <= indY) && (indY < dSizeY)){
linInd = indY + indX*(dSizeY);
Expand All @@ -779,8 +780,8 @@ else{
for(int i= 0; i<=countOut; i++){
lk = abs(InterceptsVecOut[i] - tOld);
if(i == 0){
indX = int(HullOut[0]/pix[0] + dimX/2.);
indY = int(HullOut[1]/pix[1] + dimY/2.);
indX = (int)floorf(HullOut[0]/pix[0] + dimX/2.);
indY = (int)floorf(HullOut[1]/pix[1] + dimY/2.);
if ((0 <= indX) && (indX < dSizeX) && (0 <= indY) && (indY < dSizeY) && (0 < lk) && (lk < (exitpos[2]-HullOut[2]))){
linInd = indY + indX*(dSizeY);
atomicAdd(&dhist1[linInd], weight_air_out*(lk/lenZ)*(lk/lenZ)*p_wepl[protonIndex]);
Expand All @@ -790,8 +791,8 @@ else{
}
else if(i == countOut){
lk = exitpos[2] - InterceptsVecOut[i-1];
indX = int(exitpos[0]/pix[0] + dimX/2.);
indY = int(exitpos[1]/pix[1] + dimY/2.);
indX = (int)floorf(exitpos[0]/pix[0] + dimX/2.);
indY = (int)floorf(exitpos[1]/pix[1] + dimY/2.);
if ((0 <= indX) && (indX < dSizeX) && (0 <= indY) && (indY < dSizeY) && (0 < lk) && (lk < (exitpos[2]-HullOut[2]))){
linInd = indY + indX*(dSizeY);
atomicAdd(&dhist1[linInd], weight_air_out*(lk/lenZ)*(lk/lenZ)*p_wepl[protonIndex]);
Expand All @@ -800,8 +801,8 @@ else{
}

else{
indX = int(((exitdir[0]*(InterceptsVecOut[i]-eps) + (HullOut[0] - exitdir[0] * HullOut[2])))/pix[0] + dimX/2.);
indY = int(((exitdir[1]*(InterceptsVecOut[i]-eps) + (HullOut[1] - exitdir[1] * HullOut[2])))/pix[1] + dimY/2.);
indX = (int)floorf(((exitdir[0]*(InterceptsVecOut[i]-eps) + (HullOut[0] - exitdir[0] * HullOut[2])))/pix[0] + dimX/2.);
indY = (int)floorf(((exitdir[1]*(InterceptsVecOut[i]-eps) + (HullOut[1] - exitdir[1] * HullOut[2])))/pix[1] + dimY/2.);
if ((0 <= indX) && (indX < dSizeX) && (0 <= indY) && (indY < dSizeY) && (0 < lk) && (lk < (exitpos[2]-HullOut[2]))){
linInd = indY + indX*(dSizeY);
atomicAdd(&dhist1[linInd], weight_air_out*(lk/lenZ)*(lk/lenZ)*p_wepl[protonIndex]);
Expand Down Expand Up @@ -847,8 +848,8 @@ else{
int indX, indY, linInd;
float tOld = initpos[2];
if (count==0){
indX = int(initpos[0]/pix[0] + dimX/2.);
indY = int(initpos[1]/pix[1] + dimY/2.);
indX = (int)floorf(initpos[0]/pix[0] + dimX/2.);
indY = (int)floorf(initpos[1]/pix[1] + dimY/2.);

if ((0 <= indX) && (indX < dSizeX) && (0 <= indY) && (indY < dSizeY)){
linInd = indY + indX*(dSizeY);
Expand All @@ -860,8 +861,8 @@ else{
for(int i= 0; i<=count; i++){
lk = InterceptsVec[i] - tOld;
if(tOld == initpos[2]){
indX = int(initpos[0]/pix[0] + dimX/2.);
indY = int(initpos[1]/pix[1] + dimY/2.);
indX = (int)floorf(initpos[0]/pix[0] + dimX/2.);
indY = (int)floorf(initpos[1]/pix[1] + dimY/2.);
linInd = indY + indX*(dSizeY);

if ((0 <= indX) && (indX < dSizeX) && (0 <= indY) && (indY < dSizeY) && (0 < lk) && (lk < lenZ)){
Expand All @@ -873,8 +874,8 @@ else{

}else if(i == count){
lk = exitpos[2] - InterceptsVec[i-1];
indX = int(exitpos[0]/pix[0] + dimX/2.);
indY = int(exitpos[1]/pix[1] + dimY/2.);
indX = (int)floorf(exitpos[0]/pix[0] + dimX/2.);
indY = (int)floorf(exitpos[1]/pix[1] + dimY/2.);

if ((0 <= indX) && (indX < dSizeX) && (0 <= indY) && (indY < dSizeY) && (0 < lk) && (lk < lenZ)){
linInd = indY + indX*(dSizeY);
Expand All @@ -883,8 +884,8 @@ else{
}

}else{
indX = int(((mydir[0]*(InterceptsVec[i]-eps) + (initpos[0] - mydir[0] * (initpos[2]))))/pix[0] + dimX/2.);
indY = int(((mydir[1]*(InterceptsVec[i]-eps) + (initpos[1] - mydir[1] * (initpos[2]))))/pix[1] + dimY/2.);
indX = (int)floorf(((mydir[0]*(InterceptsVec[i]-eps) + (initpos[0] - mydir[0] * (initpos[2]))))/pix[0] + dimX/2.);
indY = (int)floorf(((mydir[1]*(InterceptsVec[i]-eps) + (initpos[1] - mydir[1] * (initpos[2]))))/pix[1] + dimY/2.);

if ((0 <= indX) && (indX < dSizeX) && (0 <= indY) && (indY < dSizeY) && (0 < lk) && (lk < lenZ)){
linInd = indY + indX*(dSizeY);
Expand Down Expand Up @@ -920,18 +921,18 @@ __host__ void ParticleProjections(float * outProjection, float* posIn, float* po
Detect Size = 400x400
Prepare Input for GPU*/

const int sizeInputs = 2*numOfEntries*sizeof(float);
const int detectorMem = detectSizeX*detectSizeY*sizeof(float);
const size_t sizeInputs = 2*(size_t)numOfEntries*sizeof(float);
const size_t detectorMem = (size_t)detectSizeX*detectSizeY*sizeof(float);
float reject = 0.0;

float *dPosIn, *dPosOut, *ddirIn, *ddirOut, *dhist1, *dhist2, *d_wepl, *dHull;
int *dnumEntries, *ddetectorX, *ddetectorY;
float *dpixelSize, *dDetectDistIn, *dDetectDistOut, *dEin, *dReject;
float *dPosIn = NULL, *dPosOut = NULL, *ddirIn = NULL, *ddirOut = NULL, *dhist1 = NULL, *dhist2 = NULL, *d_wepl = NULL, *dHull = NULL;
int *dnumEntries = NULL, *ddetectorX = NULL, *ddetectorY = NULL;
float *dpixelSize = NULL, *dDetectDistIn = NULL, *dDetectDistOut = NULL, *dEin = NULL, *dReject = NULL;

float *hist1, *hist2;
hist1 = new float[detectSizeX*detectSizeY];
hist2 = new float[detectSizeX*detectSizeY];
for(int i = 0; i<detectSizeX*detectSizeY; i++){
hist1 = new float[(size_t)detectSizeX*detectSizeY];
hist2 = new float[(size_t)detectSizeX*detectSizeY];
for(size_t i = 0; i<(size_t)detectSizeX*detectSizeY; i++){
hist1[i] = 0.f;
hist2[i]= 0.f;

Expand All @@ -954,6 +955,17 @@ __host__ void ParticleProjections(float * outProjection, float* posIn, float* po
cudaMalloc( (void**) &dEin, sizeof(float));
cudaMalloc( (void**) &dReject, sizeof(float));
cudaMalloc( (void**) &dHull, 4*sizeof(float));
// If a CUDA call below raises an error, free everything first (Python can
// catch the error and carry on; before, the GPU memory was lost).
tigre::ReleaseOnError onError;
onError.add([=]{
float* fbufs[] = {dPosIn, dPosOut, ddirIn, ddirOut, dhist1, dhist2, d_wepl, dHull,
dpixelSize, dDetectDistIn, dDetectDistOut, dEin, dReject};
for (float* b : fbufs) cudaFree(b);
cudaFree(dnumEntries); cudaFree(ddetectorX); cudaFree(ddetectorY);
delete[] hist1;
delete[] hist2;
});
cudaCheckErrors("GPU Allocation failed!");

//Copy Arrays to GPU
Expand Down Expand Up @@ -999,7 +1011,8 @@ __host__ void ParticleProjections(float * outProjection, float* posIn, float* po
//mexPrintf("%s \n", cudaGetErrorString(_errcp));
cudaCheckErrors("Device to host transport failed!");

for(int j = 0; j<detectSizeX*detectSizeY; j++){
onError.dismiss();
for(size_t j = 0; j<(size_t)detectSizeX*detectSizeY; j++){
outProjection[j] = hist1[j]/hist2[j];
}

Expand Down
Loading
Loading