-
Notifications
You must be signed in to change notification settings - Fork 6
/
CDoWbpRecon.cpp
executable file
·151 lines (140 loc) · 4.22 KB
/
CDoWbpRecon.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include "CReconInc.h"
#include <memory.h>
#include <stdio.h>
using namespace Recon;
static MrcUtil::CTomoStack* s_pTomoStack = 0L;
static MrcUtil::CAlignParam* s_pAlignParam = 0L;
static MrcUtil::CTomoStack* s_pVolStack = 0L;
MrcUtil::CTomoStack* CDoWbpRecon::DoIt
( MrcUtil::CTomoStack* pTomoStack,
MrcUtil::CAlignParam* pAlignParam,
int iVolZ,
float fRFactor,
int* piGpuIDs,
int iNumGpus
)
{ s_pTomoStack = pTomoStack;
s_pAlignParam = pAlignParam;
//--------------------------
int aiVolSize[3] = {1, iVolZ, pTomoStack->m_aiStkSize[1]};
//aiVolSize[0] = (pTomoStack->m_aiStkSize[0] + 16) / 2 * 2;
aiVolSize[0] = pTomoStack->m_aiStkSize[0] / 2 * 2;
s_pVolStack = new MrcUtil::CTomoStack;
s_pVolStack->Create(aiVolSize);
//-----------------
Util::CNextItem nextItem;
nextItem.Create(pTomoStack->m_aiStkSize[1]);
//-----------------
CDoWbpRecon* pThreads = new CDoWbpRecon[iNumGpus];
for(int i=0; i<iNumGpus; i++)
{ pThreads[i].Run(&nextItem, piGpuIDs[i]);
}
for(int i=0; i<iNumGpus; i++)
{ pThreads[i].WaitForExit(-1.0f);
}
delete[] pThreads;
printf("SART reconstruction completed.\n\n");
//-----------------
MrcUtil::CTomoStack* pVolStack = s_pVolStack;
s_pVolStack = 0L;
return pVolStack;
}
CDoWbpRecon::CDoWbpRecon(void)
{
}
CDoWbpRecon::~CDoWbpRecon(void)
{
this->Clean();
}
void CDoWbpRecon::Clean(void)
{
CDoBaseRecon::Clean();
m_aTomoWbp.Clean();
}
void CDoWbpRecon::ThreadMain(void)
{
cudaSetDevice(m_iGpuID);
//----------------------
int iPadX = (s_pTomoStack->m_aiStkSize[0] / 2 + 1) * 2;
size_t tBytes = sizeof(float) * iPadX * s_pTomoStack->m_aiStkSize[2];
cudaMalloc(&m_gfPadSinogram, tBytes);
cudaMallocHost(&m_pfPadSinogram, tBytes);
//---------------------------------------
tBytes = s_pVolStack->GetPixels() * sizeof(float);
cudaMalloc(&m_gfVolXZ, tBytes);
cudaMemset(m_gfVolXZ, 0, tBytes);
cudaMallocHost(&m_pfVolXZ, tBytes);
//---------------------------------
m_aTomoWbp.Setup(s_pVolStack->m_aiStkSize[0],
s_pVolStack->m_aiStkSize[1],
s_pTomoStack, s_pAlignParam);
//------------------------------
cudaStreamCreate(&m_stream);
cudaEventCreate(&m_eventSino);
//----------------------------
int iLastY = -1;
while(true)
{ int iY = m_pNextItem->GetNext();
if(iY < 0) break;
//---------------
if(iY % 101 == 0)
{ int iLeft = s_pTomoStack->m_aiStkSize[1] - 1 - iY;
printf("...... reconstruct slice %4d, "
"%4d slices left\n", iY+1, iLeft);
}
//-------------------------------------------
mExtractSinogram(iY);
mGetReconResult(iLastY);
mReconstruct(iY);
iLastY = iY;
}
cudaStreamSynchronize(m_stream);
mGetReconResult(iLastY);
//----------------------
cudaStreamDestroy(m_stream);
cudaEventDestroy(m_eventSino);
}
void CDoWbpRecon::mExtractSinogram(int iY)
{
int iProjX = s_pTomoStack->m_aiStkSize[0];
int iPadX = (iProjX / 2 + 1) * 2;
size_t tBytes = sizeof(float) * iProjX;
for(int i=0; i<s_pTomoStack->m_aiStkSize[2]; i++)
{ float* pfProj = s_pTomoStack->GetFrame(i);
float* pfSrc = pfProj + iY * iProjX;
float* pfDst = m_pfPadSinogram + i * iPadX;
memcpy(pfDst, pfSrc, tBytes);
}
//-----------------------------------
cudaStreamWaitEvent(m_stream, m_eventSino, 0);
tBytes = sizeof(float) * iPadX * s_pTomoStack->m_aiStkSize[2];
cudaMemcpyAsync(m_gfPadSinogram, m_pfPadSinogram, tBytes,
cudaMemcpyDefault, m_stream);
cudaEventRecord(m_eventSino, m_stream);
}
void CDoWbpRecon::mGetReconResult(int iLastY)
{
if(iLastY < 0) return;
float* pfVolXZ = s_pVolStack->GetFrame(iLastY);
cudaStreamSynchronize(m_stream);
//--------------------------------
// Flip z to match IMOD handedness
//--------------------------------
int iBytes = s_pVolStack->m_aiStkSize[0] * sizeof(float);
int iLastZ = s_pVolStack->m_aiStkSize[1] - 1;
for(int z=0; z<=iLastZ; z++)
{ float* pfSrc = m_pfVolXZ + z * s_pVolStack->m_aiStkSize[0];
float* pfDst = pfVolXZ + (iLastZ - z)
* s_pVolStack->m_aiStkSize[0];
memcpy(pfDst, pfSrc, iBytes);
}
}
void CDoWbpRecon::mReconstruct(int iY)
{
size_t tBytes = s_pVolStack->GetPixels() * sizeof(float);
cudaMemsetAsync(m_gfVolXZ, 0, tBytes, m_stream);
m_aTomoWbp.DoIt(m_gfPadSinogram, m_gfVolXZ, m_stream);
//----------------------------------------------------
cudaMemcpyAsync(m_pfVolXZ, m_gfVolXZ, tBytes,
cudaMemcpyDefault, m_stream);
}