-
Notifications
You must be signed in to change notification settings - Fork 0
/
DLLFunctions.cuh
287 lines (225 loc) · 12.4 KB
/
DLLFunctions.cuh
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
//Program for performing GPU-accelerated single-photon peak event detection for FLIM with option for multiple
//thresholds, such as when using a hybrid photodetector (HPD)
//By Janet Sorrells and Rishee Iyer, 2022
//Contact: [email protected]
//See: https://doi.org/10.1021/acsphotonics.2c00505 (SPEED with HPD, 2022)
// and https://doi.org/10.1364/OE.439675 (SPEED with PMT, 2021)
//These DLL funcitons can be called by LabView for real-time processing!
#include "KernelFile.cuh"
int16_t* GPU_FLIMRawData_1Line;
float* GPU_numXxnumY_MeanLifetime_AfterAv, * GPU_numXxnumY_MPMImage_BeforeAv;
uint16_t* GPU_numXxnumY_S_AfterAv, * GPU_numXxnumY_G_AfterAv;
float* gThresh;
int32_t* gnumX, * gnumY, * gnumTTot, * gnum2T, * gDivFactor, * gNumChunks, * gLineIndex, * gnumYPerChunk, * gnumXnumYnumT, * gNumxidMax;
cudaStream_t FastFLIMStream, OCMStream;
//int CudaLeastPriority, CudaGreatestPriority;
CountDataType* GPU_numXxnumTxnumPulses_Count_Container;
AvgDataType* GPU_numXxnumYxnumT_Container, * GPU_numXxnumYxnumT_Container_Shifted;
// Initialize GPU to free memeroy and select device
DLLEXPORT cudaError_t FastFLIMGPU_StartGPU()
{
FILE* LogFile;
if (FILESTUFFEH) LogFile = fopen(FLIMLOGFILENAME, "wt");
if (FILESTUFFEH) fprintf(LogFile, "START GPU\n");
int deviceCount = 0;
cudaError_t CC;
cudaGetDeviceCount(&deviceCount);
for (int i = 0; i < 1; i++)
{
cudaDeviceProp prop;
cudaGetDeviceProperties(&prop, i);
CC = cudaSetDevice(i);
if (FILESTUFFEH) fprintf(LogFile, "I selected this device: %s. You cool with it, champ?\n\n", prop.name);
}
CC = cudaGetLastError();
if (FILESTUFFEH) fprintf(LogFile, "Last Cuda Error: %s\n", cudaGetErrorString(cudaPeekAtLastError()));
if (FILESTUFFEH) fclose(LogFile);
return (CC);
}
//Calculate array size, allocate memory, and copy data to device
DLLEXPORT cudaError_t FastFLIMGPU_InitializeAndAllocateMemory(int32_t* numX, int32_t* numY, int32_t* numTTot, int32_t* num2T, int32_t* numChunks)
{
FILE* LogFile;
if (FILESTUFFEH) fopen_s(&LogFile, FLIMLOGFILENAME, "w");
if (FILESTUFFEH) fprintf(LogFile, "INITIALIZATION STEP\n");
//Select the device and reset it
cudaSetDevice(0);
cudaDeviceReset();
if (FILESTUFFEH && (cudaPeekAtLastError() != cudaSuccess))
fprintf(LogFile, "Device reset failed: %s\n", cudaGetErrorString(cudaPeekAtLastError()));
//Allocate memory for array sizes and indices
cudaMalloc((void**)&gnumX, sizeof(int32_t));
cudaMalloc((void**)&gnumY, sizeof(int32_t));
cudaMalloc((void**)&gnum2T, sizeof(int32_t));
cudaMalloc((void**)&gnumTTot, sizeof(int32_t));
cudaMalloc((void**)&gDivFactor, sizeof(int32_t));
cudaMalloc((void**)&gNumChunks, sizeof(int32_t));
cudaMalloc((void**)&gLineIndex, sizeof(int32_t));
cudaMalloc((void**)&gnumYPerChunk, sizeof(int32_t));
cudaMalloc((void**)&gThresh, sizeof(float));
cudaMalloc((void**)&gnumXnumYnumT, sizeof(int32_t));
cudaMalloc((void**)&gNumxidMax, sizeof(int32_t));
if (FILESTUFFEH && (cudaPeekAtLastError() != cudaSuccess))
fprintf(LogFile, "Malloc 1 failed: %s\n", cudaGetErrorString(cudaPeekAtLastError()));
int32_t numYPerChunks = *numY / *numChunks;
//Allocate memory for data containers
cudaMalloc((void**)&GPU_FLIMRawData_1Line, sizeof(uint16_t) * *numX * *numTTot);
cudaMalloc((void**)&GPU_numXxnumYxnumT_Container, sizeof(float) * *numX * *numY * *num2T);
cudaMalloc((void**)&GPU_numXxnumYxnumT_Container_Shifted, sizeof(float) * *numX * *numY * *num2T);
cudaMalloc((void**)&GPU_numXxnumY_MeanLifetime_AfterAv, sizeof(float) * *numX * *numY);
cudaMalloc((void**)&GPU_numXxnumY_MPMImage_BeforeAv, sizeof(float) * *numX * *numY);
cudaMalloc((void**)&GPU_numXxnumY_S_AfterAv, sizeof(uint16_t) * *numX * *numY);
cudaMalloc((void**)&GPU_numXxnumY_G_AfterAv, sizeof(uint16_t) * *numX * *numY);
cudaMalloc((void**)&GPU_numXxnumTxnumPulses_Count_Container, sizeof(CountDataType) * *numX * *numTTot);
if (FILESTUFFEH && (cudaPeekAtLastError() != cudaSuccess))
fprintf(LogFile, "Malloc 2 failed: %s\n", cudaGetErrorString(cudaPeekAtLastError()));
//Array size calculations
int32_t DivFactor = *numTTot / *num2T;
int32_t xyt = *numX * *numY * *num2T;
int32_t xyttot = *numX * *numTTot - 2;
//Copy over array sizes
cudaMemcpy(gnumX, numX, sizeof(int32_t), cudaMemcpyHostToDevice);
cudaMemcpy(gnumY, numY, sizeof(int32_t), cudaMemcpyHostToDevice);
cudaMemcpy(gnum2T, num2T, sizeof(int32_t), cudaMemcpyHostToDevice);
cudaMemcpy(gnumTTot, numTTot, sizeof(int32_t), cudaMemcpyHostToDevice);
cudaMemcpy(gNumChunks, numChunks, sizeof(int32_t), cudaMemcpyHostToDevice);
cudaMemcpy(gnumYPerChunk, &numYPerChunks, sizeof(int32_t), cudaMemcpyHostToDevice);
cudaMemcpy(gDivFactor, &DivFactor, sizeof(int32_t), cudaMemcpyHostToDevice);
cudaMemcpy(gnumXnumYnumT, &xyt, sizeof(int32_t), cudaMemcpyHostToDevice);
cudaMemcpy(gNumxidMax, &xyttot, sizeof(int32_t), cudaMemcpyHostToDevice);
if (FILESTUFFEH) fprintf(LogFile, "Parameters | X: %d, Y: %d, TTot: %d, 2T: %d, C: %d, YpC: %d, DivFactor: %d\n",
*numX, *numY, *numTTot, *num2T, *numChunks, numYPerChunks, DivFactor);
if (FILESTUFFEH && (cudaPeekAtLastError() != cudaSuccess))
fprintf(LogFile, "Memcpy failed: %s\n", cudaGetErrorString(cudaPeekAtLastError()));
if (FILESTUFFEH) fprintf(LogFile, "ANYTHING THAT HAPPENS AFTER THIS IS IN THE PROCESSING STEP\n");
if (FILESTUFFEH) fclose(LogFile);
return cudaPeekAtLastError();
}
//Count photons and histogram counts for one line
DLLEXPORT cudaError_t FastFLIMGPU_ReportFrame(int16_t* RawData_Block, int32_t numX, int32_t numY, int32_t numTTot, int32_t num2T,
int32_t numChunks, int32_t LineIndex)
{
FILE* LogFile;
if (FILESTUFFEH) fopen_s(&LogFile, FLIMLOGFILENAME, "a");
dim3 dimBlock3D(16, 1, 5); // Avg pulses to a pulse, optimized
dim3 dimGrid3D((numX) / dimBlock3D.x, 1, (num2T) / dimBlock3D.z);
dim3 dimBlock1D(125, 1, 1); //Count Peaks, optimized
dim3 dimGrid1D(numX * numTTot / dimBlock1D.x, 1, 1);
int32_t LINEIDX = (LineIndex) % numY;
cudaMemcpy(gLineIndex, &LINEIDX, sizeof(int32_t), cudaMemcpyHostToDevice);
if (FILESTUFFEH && (cudaPeekAtLastError() != cudaSuccess))
fprintf(LogFile, "Memcpy failed: %s\n", cudaGetErrorString(cudaPeekAtLastError()));
cudaMemcpy(GPU_FLIMRawData_1Line, RawData_Block, sizeof(uint16_t) * numX * numTTot, cudaMemcpyHostToDevice);
if (FILESTUFFEH)
fprintf(LogFile, "Raw Data Memcpy for line %d (%d): %s\n", LineIndex, LINEIDX, cudaGetErrorString(cudaPeekAtLastError()));
cudaDeviceSynchronize();
CountPeaks_HPD << <dimGrid1D, dimBlock1D >> > (GPU_FLIMRawData_1Line, GPU_numXxnumTxnumPulses_Count_Container, gNumxidMax);
cudaDeviceSynchronize();
AverageCountsToAPulse << <dimGrid3D, dimBlock3D >> > (GPU_numXxnumTxnumPulses_Count_Container, GPU_numXxnumYxnumT_Container, gnumX,
gnumTTot, gnum2T, gDivFactor, gLineIndex);
cudaDeviceSynchronize();
if (FILESTUFFEH && (cudaPeekAtLastError() != cudaSuccess))
fprintf(LogFile, "Generating Fall-off curve failed: %s\n", cudaGetErrorString(cudaPeekAtLastError()));
if (FILESTUFFEH) fclose(LogFile);
return cudaPeekAtLastError();
}
// Shift, bin, and calculate intensity and lifetime for one frame
DLLEXPORT cudaError_t FastFLIMGPU_DoFastFLIM(float* MPMImage, float* OutputHistogram, float* OutputFLIM, uint16_t* Output_G, uint16_t* Output_S,
int32_t numX, int32_t numY, int32_t num2T, float* Thresh, uint8_t SaveEh_MPM,
char* SaveName_MPM, uint8_t SaveEh_LifeTime, char* SaveName_LifeTime)
{
FILE* LogFile;
if (FILESTUFFEH) fopen_s(&LogFile, FLIMLOGFILENAME, "a");
dim3 dimBlock2D(4, 4); //DoingMPMAndFLIMKernel, optimized
dim3 dimGrid2D((numX) / dimBlock2D.x, (numY) / dimBlock2D.y);
dim3 dimBlock1Dy(1, 4); //MaxMin Line Shift, optimized
dim3 dimGrid1Dy(1, (numY) / dimBlock2D.y);
dim3 dimBlock3D(8, 8, 5); //FluorescenceDecayBin, optmized
dim3 dimGrid3D((numX) / dimBlock3D.x, (numY) / dimBlock3D.y, (num2T) / dimBlock3D.z);
cudaMemcpy(gThresh, Thresh, sizeof(float), cudaMemcpyHostToDevice);
//Shift the histogram of each pixel so that the max value is at the 0th timepoint (aligned by each line for better accuracy)
MaxMinShift_LineShift << <dimGrid1Dy, dimBlock1Dy >> > (GPU_numXxnumYxnumT_Container, GPU_numXxnumYxnumT_Container_Shifted,
gnumX, gnum2T, gnumXnumYnumT);
cudaDeviceSynchronize();
if (FILESTUFFEH && (cudaPeekAtLastError() != cudaSuccess))
fprintf(LogFile, "Max and Min shifting failed: %s\n", cudaGetErrorString(cudaPeekAtLastError()));
//Optional averaging filter to increase the signal per pixel
FluorescenceDecayBin << <dimGrid3D, dimBlock3D >> > (GPU_numXxnumYxnumT_Container_Shifted, GPU_numXxnumYxnumT_Container, gnumX, gnumY, gnum2T);
cudaDeviceSynchronize();
if (FILESTUFFEH && (cudaPeekAtLastError() != cudaSuccess))
fprintf(LogFile, "Binning failed: %s\n", cudaGetErrorString(cudaPeekAtLastError()));
//Calculate intensity, lifetime, g, and s
DoingMPMAndFLIMKernel << <dimGrid2D, dimBlock2D >> > (GPU_numXxnumYxnumT_Container, GPU_numXxnumY_MPMImage_BeforeAv, gnumX, gnum2T,
gThresh, GPU_numXxnumY_G_AfterAv, GPU_numXxnumY_S_AfterAv, GPU_numXxnumY_MeanLifetime_AfterAv);
cudaDeviceSynchronize();
if (FILESTUFFEH && (cudaPeekAtLastError() != cudaSuccess))
fprintf(LogFile, "Generating intensity and lifetime failed: %s\n", cudaGetErrorString(cudaPeekAtLastError()));
//Copy intensity and histogram to host
cudaMemcpy(MPMImage, GPU_numXxnumY_MPMImage_BeforeAv, sizeof(float) * numX * numY, cudaMemcpyDeviceToHost);
if (FILESTUFFEH && (cudaPeekAtLastError() != cudaSuccess))
fprintf(LogFile, "Memcpy2 failed: %s\n", cudaGetErrorString(cudaPeekAtLastError()));
cudaMemcpy(OutputHistogram, GPU_numXxnumYxnumT_Container, sizeof(AvgDataType) * numX * numY * num2T, cudaMemcpyDeviceToHost);
if (FILESTUFFEH && (cudaPeekAtLastError() != cudaSuccess))
fprintf(LogFile, "Memcpy2 failed for decay: %s\n", cudaGetErrorString(cudaPeekAtLastError()));
//Save intensity and histogram
if (SaveEh_MPM)
{
FILE* FID_MPM = fopen(SaveName_MPM, "wb");
fwrite(MPMImage, sizeof(float), numX * numY, FID_MPM);
fclose(FID_MPM);
}
//Copy lifetime to host
cudaMemcpy(OutputFLIM, GPU_numXxnumY_MeanLifetime_AfterAv, sizeof(float) * numX * numY, cudaMemcpyDeviceToHost);
if (FILESTUFFEH && (cudaPeekAtLastError() != cudaSuccess))
fprintf(LogFile, "Copying Average Lifetime Image failed: %s\n", cudaGetErrorString(cudaPeekAtLastError()));
//Save lifetime
if (SaveEh_LifeTime)
{
FILE* FID_LifeTime = fopen(SaveName_LifeTime, "wb");
fwrite(OutputFLIM, sizeof(float), numX * numY, FID_LifeTime);
fclose(FID_LifeTime);
}
//Copy s and g to host
cudaMemcpy(Output_G, GPU_numXxnumY_G_AfterAv, sizeof(uint16_t) * numX * numY, cudaMemcpyDeviceToHost);
if (FILESTUFFEH && (cudaPeekAtLastError() != cudaSuccess))
fprintf(LogFile, "Copying Average G Values failed: %s\n", cudaGetErrorString(cudaPeekAtLastError()));
cudaMemcpy(Output_S, GPU_numXxnumY_S_AfterAv, sizeof(uint16_t) * numX * numY, cudaMemcpyDeviceToHost);
if (FILESTUFFEH && (cudaPeekAtLastError() != cudaSuccess))
fprintf(LogFile, "Copying Average S Values failed: %s\n", cudaGetErrorString(cudaPeekAtLastError()));
if (FILESTUFFEH) fclose(LogFile);
return cudaPeekAtLastError();
if (FILESTUFFEH && (cudaPeekAtLastError() != cudaSuccess))
fprintf(LogFile, "Something in the end: %s\n", cudaGetErrorString(cudaPeekAtLastError()));
if (FILESTUFFEH) fclose(LogFile);
return cudaPeekAtLastError();
}
//Free memory!
DLLEXPORT cudaError_t FastFLIMGPU_DestroyEverything()
{
FILE* LogFile;
if (FILESTUFFEH) fopen_s(&LogFile, FLIMLOGFILENAME, "a");
if (FILESTUFFEH) fprintf(LogFile, "CLEARING MEMORY\n");
cudaFree(gnumX);
cudaFree(gnumY);
cudaFree(gnum2T);
cudaFree(gnumTTot);
cudaFree(gDivFactor);
cudaFree(gThresh);
cudaFree(gLineIndex);
cudaFree(gNumChunks);
cudaFree(gnumYPerChunk);
cudaFree(GPU_numXxnumY_S_AfterAv);
cudaFree(GPU_numXxnumY_G_AfterAv);
cudaFree(GPU_FLIMRawData_1Line);
cudaFree(GPU_numXxnumYxnumT_Container);
cudaFree(GPU_numXxnumYxnumT_Container_Shifted);
cudaFree(GPU_numXxnumY_MeanLifetime_AfterAv);
cudaFree(GPU_numXxnumY_MPMImage_BeforeAv);
cudaFree(gnumXnumYnumT);
cudaFree(GPU_numXxnumTxnumPulses_Count_Container);
cudaFree(gNumxidMax);
if (FILESTUFFEH && (cudaPeekAtLastError() != cudaSuccess))
fprintf(LogFile, "Clearing memory failed : %s\n", cudaGetErrorString(cudaPeekAtLastError()));
if (FILESTUFFEH) fclose(LogFile);
return cudaPeekAtLastError();
}