8 #include "gVariables.hh" 9 #include "iIterativeAlgorithm.hh" 10 #include "iEventHistoCT.hh" 39 Cerr(
"***** iIterativeAlgorithm::StepBeforeIterationLoop() -> A problem occurred while calling StepBeforeIterationLoop() function !" << endl);
42 if (
m_verbose>=2)
Cout(
"iIterativeAlgorithm::StepBeforeIterationLoop ... " << endl);
50 Cerr(
"***** iIterativeAlgorithm::StepBeforeIterationLoop() -> An error occurred while reading the initialization image !" << endl);
68 Cerr(
"***** iIterativeAlgorithm::StepBeforeIterationLoop() -> A problem occured while calling PreOrPostIterationIndependentStep() function !" << endl);
86 if (
m_verbose>=3)
Cout(
"iIterativeAlgorithm::StepBeforeSubsetLoop ... " << endl);
102 if (
m_verbose>=3)
Cout(
"iIterativeAlgorithm::StepPreProcessInsideSubsetLoop ... " << endl);
119 Cerr(
"***** iIterativeAlgorithm::StepPreProcessInsideSubsetLoop() -> A problem occurred while applyin image processing to forward image !" << endl);
127 Cerr(
"***** iIterativeAlgorithm::StepPreProcessInsideSubsetLoop() -> A problem occurred while applying image convolver to forward image !" << endl);
156 if (
m_nbBeds>1)
Cout(
"iIterativeAlgorithm::StepInnerLoopInsideSubsetLoop() -> Start loop over events for bed " << a_bed+1 << endl << flush);
157 else Cout(
"iIterativeAlgorithm::StepInnerLoopInsideSubsetLoop() -> Start loop over events" << endl << flush);
169 cout <<
"0 10% 20% 30% 40% 50% 60% 70% 80% 90% 100%" << endl;
170 cout <<
"|----|----|----|----|----|----|----|----|----|----|" << endl;
171 cout <<
"|" << flush;
173 int progression_percentage_old = 0;
174 int progression_nb_bars = 0;
175 uint64_t progression_printing_index = 0;
178 int64_t index_start = 0;
179 int64_t index_stop = 0;
184 MPI_Barrier(MPI_COMM_WORLD);
193 bool problem =
false;
205 #pragma omp parallel for private(index) schedule(static, 1) 206 for ( index = index_start ; index < index_stop ; index +=
mp_nbSubsets[a_iteration] )
211 th = omp_get_thread_num();
216 if (progression_printing_index%1000==0)
218 int progression_percentage_new = ((int)( (((
float)(index-index_start+1))/((float)(index_stop-index_start)) ) * 100.));
219 if (progression_percentage_new>=progression_percentage_old+2)
221 int nb_steps = (progression_percentage_new-progression_percentage_old)/2;
222 for (
int i=0; i<nb_steps; i++)
224 cout <<
"-" << flush;
225 progression_nb_bars++;
227 progression_percentage_old += nb_steps*2;
230 progression_printing_index++;
233 if (p_end_loop_flags[th])
continue;
238 if (
m_verbose>=4)
Cout(
"iIterativeAlgorithm::StepInnerLoopInsideSubsetLoop() -> Step1: Get current event for that thread index " << endl);
243 Cerr(
"***** iIterativeAlgorithm::StepInnerLoopInsideSubsetLoop() -> An error occurred while getting the event from index " 244 << index <<
" (thread " << th <<
") !" << endl);
257 Cout(
"iIterativeAlgorithm::StepInnerLoopInsideSubsetLoop() -> Step2: Check for Dynamic event (frame/gate switch, image-based deformation " << endl);
260 int dynamic_switch_value =
mp_ID->
DynamicSwitch(index, event->GetTimeInMs(), a_bed, th);
273 #pragma omp critical (deformation_requirements_first) 278 bool wait_for_deformation =
true;
279 while (wait_for_deformation)
285 bool all_threads_ready =
false;
287 #pragma omp critical (deformation_requirements_first) 291 if (all_threads_ready)
296 #pragma omp critical (deformation_requirements_second) 303 #pragma omp critical (deformation_requirements_second) 316 cout <<
"0 10% 20% 30% 40% 50% 60% 70% 80% 90% 100%" << endl;
317 cout <<
"|----|----|----|----|----|----|----|----|----|----|" << endl;
318 int progression_percentage_new = ((int)( (((
float)(index-index_start+1))/((float)(index_stop-index_start)) ) * 100.));
321 for (
int i=0; i< progression_percentage_new / 2 ; i++) cout <<
"-";
328 p_end_loop_flags[th] =
true;
336 if (
m_verbose>=4)
Cout(
"iIterativeAlgorithm::StepInnerLoopInsideSubsetLoop() -> Step3: Compute the projection line " << endl);
341 Cerr(
"***** iIterativeAlgorithm::StepInnerLoopInsideSubsetLoop() -> A problem occurred while computing the projection line !" << endl);
352 if (
m_verbose>=4)
Cout(
"iIterativeAlgorithm::StepInnerLoopInsideSubsetLoop() -> Step4: Optimize in the data space " << endl);
366 MPI_Barrier(MPI_COMM_WORLD);
377 int progression_total_bars = 49;
378 for (
int i=0; i<progression_total_bars-progression_nb_bars; i++) cout <<
"-";
385 Cerr(
"***** iIterativeAlgorithm::StepInnerLoopInsideSubsetLoop() -> A problem occurred inside the parallel loop over events !" << endl);
400 if (
m_verbose>=3)
Cout(
"iIterativeAlgorithm::StepPostProcessInsideSubsetLoop ... " << endl);
415 Cerr(
"***** iIterativeAlgorithm::StepPostProcessInsideSubsetLoop() -> A problem occurred while applying convolver to backward images !" << endl);
423 Cerr(
"***** iIterativeAlgorithm::StepPostProcessInsideSubsetLoop() -> A problem occurred while performing the pre-image-update step !" << endl);
430 Cerr(
"***** iIterativeAlgorithm::StepPostProcessInsideSubsetLoop() -> A problem occurred while performing the image update step !" << endl);
438 Cerr(
"***** iIterativeAlgorithm::StepPostProcessInsideSubsetLoop() -> A problem occurred while applying convolver to current estimate images !" << endl);
446 Cerr(
"***** iIterativeAlgorithm::StepPostProcessInsideSubsetLoop() -> A problem occurred while applying dynamic model to current estimate images !" << endl);
453 Cerr(
"***** iIterativeAlgorithm::StepPostProcessInsideSubsetLoop() -> A problem occurred while applying image processing to current estimate images !" << endl);
465 stringstream temp_ss; temp_ss << a_subset + 1;
466 temp_sens.append(
"_it").append(temp_it.str()).append(
"_ss").append(temp_ss.str()).append(
"_sensitivity");
476 a_iteration /= nb_sub_iter;
478 if (
m_verbose>=1)
Cout(
"iIterativeAlgorithm::StepPostProcessInsideSubsetLoop() -> Save image at iteration " << a_iteration+1 <<
" for subset " << a_subset+1 << endl);
482 Cerr(
"***** iIterativeAlgorithm::StepPostProcessInsideSubsetLoop() -> A problem occurred while saving images at iteration " << a_iteration+1 <<
" !" << endl);
498 if (
m_verbose>=3)
Cout(
"iIterativeAlgorithm::StepAfterSubsetLoop() -> Clean never visited voxels and save images if needed" << endl);
506 a_iteration /= nb_sub_iter;
508 if (
m_verbose>=1)
Cout(
"iIterativeAlgorithm::StepAfterSubsetLoop() -> Save image at iteration " << a_iteration+1 << endl);
512 Cerr(
"***** iIterativeAlgorithm::StepAfterSubsetLoop() -> A problem occurred while saving images at iteration " << a_iteration+1 <<
" !" << endl);
535 Cerr(
"***** iIterativeAlgorithm::StepAfterIterationLoop() -> A problem occured while calling PreOrPostIterationIndependentStep() function !" << endl);
544 Cerr(
"***** iIterativeAlgorithm::StepAfterIterationLoop() -> A problem occurred while calling StepAfterIterationLoop() function !" << endl);
547 if (
m_verbose>=2)
Cout(
"iIterativeAlgorithm::StepAfterIterationLoop ... " << endl);
571 Cerr(
"***** iIterativeAlgorithm::StepImageOutput() -> A problem occurred while saving parametric images related to the dynamic model !" << endl);
583 Cerr(
"***** iIterativeAlgorithm::StepImageOutput() -> A problem occurred while convolving the output image !" << endl);
590 Cerr(
"***** iIterativeAlgorithm::StepImageOutput() -> A problem occurred while applying image processing the output image !" << endl);
596 Cerr(
"***** iIterativeAlgorithm::StepImageOutput() -> A problem occurred while applying output FOV masking !" << endl);
602 Cerr(
"***** iIterativeAlgorithm::StepImageOutput() -> A problem occurred while applying output flip !" << endl);
615 Cerr(
"***** iIterativeAlgorithm::StepImageOutput() -> A problem occurred while saving output image !" << endl);
629 Cerr(
"***** iIterativeAlgorithm::StepImageOutput() -> A problem occurred while saving output image !" << endl);
644 if (
m_verbose>=
VERBOSE_NORMAL)
Cout(
"iIterativeAlgorithm::PreOrPostIterationIndependentStep() -> Additional data processing" << endl);
650 clock_t clock_start_step = clock();
651 time_t time_start_step = time(NULL);
662 Cerr(
"***** iIterativeAlgorithm::PreOrPostIterationIndependentStep() -> A problem occurred while applying image processing to forward image !" << endl);
670 Cerr(
"***** iIterativeAlgorithm::PreOrPostIterationIndependentStep() -> A problem occurred while applying image convolver to forward image !" << endl);
686 MPI_Barrier(MPI_COMM_WORLD);
690 for (
int bed=0 ; bed<
m_nbBeds ; bed++)
699 cout <<
"0 10% 20% 30% 40% 50% 60% 70% 80% 90% 100%" << endl;
700 cout <<
"|----|----|----|----|----|----|----|----|----|----|" << endl;
701 cout <<
"|" << flush;
703 int progression_percentage_old = 0;
704 int progression_nb_bars = 0;
705 uint64_t progression_printing_index = 0;
707 int64_t index_start = 0;
708 int64_t index_stop = 0;
709 int first_subset = 0;
714 MPI_Barrier(MPI_COMM_WORLD);
721 bool problem =
false;
728 #pragma omp parallel for private(index) schedule(static, 1) 729 for ( index = index_start ; index < index_stop ; index += one_subset )
734 th = omp_get_thread_num();
739 if (progression_printing_index%1000==0)
741 int progression_percentage_new = ((int)( (((
float)(index-index_start+1))/((float)(index_stop-index_start)) ) * 100.));
742 if (progression_percentage_new>=progression_percentage_old+2)
744 int nb_steps = (progression_percentage_new-progression_percentage_old)/2;
745 for (
int i=0; i<nb_steps; i++)
747 cout <<
"-" << flush;
748 progression_nb_bars++;
750 progression_percentage_old += nb_steps*2;
753 progression_printing_index++;
756 if (p_end_loop_flags[th])
continue;
760 if (
m_verbose>=4)
Cout(
"iIterativeAlgorithm::PreOrPostIterationIndependentStep() -> Step1: Get current event for that thread index " << endl);
766 Cerr(
"***** iIterativeAlgorithm::PreOrPostIterationIndependentStep() -> An error occurred while getting the event from index " 767 << index <<
" (thread " << th <<
") !" << endl);
779 Cout(
"iIterativeAlgorithm::PreOrPostIterationIndependentStep() -> Step2: Check for Dynamic event (frame/gate switch, image-based deformation " << endl);
782 int dynamic_switch_value =
mp_ID->
DynamicSwitch(index, event->GetTimeInMs(), bed, th);
795 #pragma omp critical (deformation_requirements_first) 800 bool wait_for_deformation =
true;
801 while (wait_for_deformation)
807 bool all_threads_ready =
false;
809 #pragma omp critical (deformation_requirements_first) 813 if (all_threads_ready)
818 #pragma omp critical (deformation_requirements_second) 825 #pragma omp critical (deformation_requirements_second) 837 cout <<
"0 10% 20% 30% 40% 50% 60% 70% 80% 90% 100%" << endl;
838 cout <<
"|----|----|----|----|----|----|----|----|----|----|" << endl;
839 int progression_percentage_new = ((int)( (((
float)(index-index_start+1))/((float)(index_stop-index_start)) ) * 100.));
841 for (
int i=0; i< progression_percentage_new / 2 ; i++) cout <<
"-";
847 p_end_loop_flags[th] =
true;
854 if (
m_verbose>=4)
Cout(
"iIterativeAlgorithm::PreOrPostIterationIndependentStep() -> Step3: Compute the projection line " << endl);
859 Cerr(
"***** iIterativeAlgorithm::PreOrPostIterationIndependentStep() -> A problem occurred while computing the projection line !" << endl);
869 if (
m_verbose>=4)
Cout(
"iIterativeAlgorithm::PreOrPostIterationIndependentStep() -> Step4: Optimize in the data space " << endl);
883 MPI_Barrier(MPI_COMM_WORLD);
892 int progression_total_bars = 49;
893 for (
int i=0; i<progression_total_bars-progression_nb_bars; i++) cout <<
"-";
899 Cerr(
"***** iIterativeAlgorithm::PreOrPostIterationIndependentStep() -> A problem occurred inside the parallel loop over events !" << endl);
906 MPI_Barrier(MPI_COMM_WORLD);
922 Cerr(
"***** iIterativeAlgorithm::StepPostProcessInsideSubsetLoop() -> A problem occurred while applying convolver to backward images !" << endl);
930 Cerr(
"***** iIterativeAlgorithm::StepPostProcessInsideSubsetLoop() -> A problem occurred while performing the pre-image-update step !" << endl);
937 Cerr(
"***** iIterativeAlgorithm::StepPostProcessInsideSubsetLoop() -> A problem occurred while performing the image update step !" << endl);
942 clock_t clock_stop_step = clock();
943 time_t time_stop_step = time(NULL);
944 if (
m_verbose>=2)
Cout (
"iIterativeAlgorithm::PreOrPostIterationIndependentStep() -> Time spent for step | User: " << time_stop_step-time_start_step
945 <<
" sec | CPU: " << (clock_stop_step-clock_start_step)/((
FLTNB)CLOCKS_PER_SEC) <<
" sec" << endl);
957 (void)a_specificOptions;
958 if (
m_verbose>=2)
Cout(
"iIterativeAlgorithm::InitSpecificOptions ... " << endl);
int ApplyProcessingForward(oImageSpace *ap_ImageSpace)
void GetEventIndexStartAndStop(int64_t *ap_indexStart, int64_t *ap_indexStop, int a_subsetNum=0, int a_NbSubsets=1)
void DeallocateBackwardImageFromDynamicBasis()
Free memory for the backward image matrices.
FLTNB GetInitialValue()
Return the initial image value used by the optimizer, explaining why the eponym function of vOptimize...
bool NeedPreIteration()
Say if the optimizer needs a pre-process loop before iterations are done.
oImageDimensionsAndQuantification * mp_ID
void SetCurrentSubset(int a_currentSubset)
int PreImageUpdateStep()
A function that simply calls the eponym function from the vOptimizer.
void Reduce()
Merge parallel results into the matrix of the backward image matrix of the first thread. Also for MPI.
oImageSpace * mp_ImageSpace
#define DYNAMIC_SWITCH_DEFORMATION
void StartIterativeDataUpdateStep4(int a_thread)
int ConvolveForward(oImageSpace *ap_ImageSpace)
int InitSpecificOptions(string a_specificOptions)
int ApplyOutputFOVMasking()
Mask the outside of the transaxial FOV based on the m_fovOutPercent.
int StepInnerLoopInsideSubsetLoop(int a_iteration, int a_subset, int a_bed)
bool m_saveSensitivityHistoFlag
oOptimizerManager * mp_OptimizerManager
int GetCurrentCardImage(int a_th)
void InstantiateBackwardImageFromDynamicBasis(int a_nbBackwardImages)
void StartIterativeDataUpdateStep3(int a_thread)
void SetImageConvolverManager(oImageConvolverManager *ap_ImageConvolverManager)
Set the Image Convolver Manager Object.
void StartIterativeDataUpdateStep2(int a_thread)
int ApplyMaskToSensitivity()
Apply the mask to the sensitivity image (only for the first thread, the image must be reduced beforeh...
void StartIterativeDataUpdateStep1(int a_thread)
static sOutputManager * GetInstance()
Instanciate the singleton object and Initialize member variables if not already done, return a pointer to this object otherwise.
int ConvolvePost(oImageSpace *ap_ImageSpace)
int ConvolveIntra(oImageSpace *ap_ImageSpace)
int ApplyProcessingPost(oImageSpace *ap_ImageSpace)
int InitImage(const string &a_pathToInitialImage, FLTNB a_value)
int DynamicSwitch(int64_t a_currentEventIndex, uint32_t a_currentTime, int a_bed, int a_th)
int StepAfterIterationLoop()
This function is called at the end of the RunCPU function.
int DataUpdateStep(oProjectionLine *ap_Line, vEvent *ap_Event, int a_bed, int a_timeFrame, int a_respGate, int a_cardGate, int a_thread)
void StopIterativeDataUpdateStep2(int a_thread)
oImageProcessingManager * mp_ImageProcessingManager
bool GetRespStaticFlag()
Get the respiratory static flag that says if the reconstruction has only one respiratory gate or not...
int StepPostProcessInsideSubsetLoop(int a_iteration, int a_subset)
vEvent * GetEvent(int64_t a_eventIndex, int a_th=0)
void PrepareForwardImage()
Copy current image matrix in the forward-image buffer matrix.
int GetCurrentRespImage(int a_th)
int SaveOutputBasisCoefficientImage(int a_iteration, int a_subset=-1)
bool m_saveDynamicBasisCoefficients
void StopIterativeDataUpdateStep1(int a_thread)
const string & GetPathName()
bool GetCardStaticFlag()
Get the cardiac static flag that says if the reconstruction has only one cardiac gate or not...
int StepBeforeSubsetLoop(int a_iteration)
void CleanNeverVisitedVoxels()
Based on the visitedVoxelsImage, clean the never visited voxels in the image. This function must be c...
Singleton class that manages output writing on disk (images, sinograms, etc). It also manages loggi...
int StepPreProcessInsideSubsetLoop(int a_iteration, int a_subset)
string m_pathToInitialImg
int GetNbBackwardImages()
Return the number of backward images used by the optimizer, explaining why the eponym function of vOp...
int SaveOutputImage(int a_iteration, int a_subset=-1)
int PreDataUpdateStep()
A function that simply calls the eponym function from the vOptimizer.
void StopIterativeDataUpdateStep3(int a_thread)
void ResetCurrentDynamicIndices()
Call the eponym function from the oDynamicDataManager class using the member object.
virtual int StepBeforeIterationLoop()
This function is called at the beginning of the RunCPU function.
bool m_saveImageAfterSubsets
void ComputeOutputImage()
oProjectionLine * ComputeProjectionLine(vEvent *ap_Event, int a_th)
int ConvolveBackward(oImageSpace *ap_ImageSpace)
void StopIterativeDataUpdateStep4(int a_thread)
oImageConvolverManager * mp_ImageConvolverManager
int SaveParametricImages(int a_iteration, int a_subset=-1)
void ApplyBedOffset(int a_bed)
void SetCurrentIteration(int a_currentIteration)
void StartConvolution()
Start the timer for duration of convolution.
bool GetTimeStaticFlag()
Get the time static flag that says if the reconstruction has only one frame or not.
bool NotEmptyLine()
This function is used to know if the line contains any voxel contribution.
#define DYNAMIC_SWITCH_CONTINUE
int GetNbSubIterationsInOneIteration()
Get the number of sub iterations in one iteration.
int ApplyProcessingIntra(oImageSpace *ap_ImageSpace)
oProjectorManager * mp_ProjectorManager
int StepBeforeIterationLoop()
This function is called at the beginning of the RunCPU function.
oDeformationManager * mp_DeformationManager
This class is designed to manage and store system matrix elements associated to a vEvent...
const string & GetBaseName()
int ApplyOutputFlip()
Just flip the output image.
virtual int StepAfterIterationLoop()
This function is called at the end of the RunCPU function.
int StepImageOutput(int a_iteration, int a_subset=-1)
This is the base class for reconstructions, containing a framework with iteration and data subset loo...
void StopConvolution()
Stop the timer for duration of convolution.
void SetNumbersOfIterationsAndSubsets(int a_nbIterations, int *ap_nbSubsets)
Mother class for the Event objects.
int GetNbThreadsForImageComputation()
Get the number of threads used for image operations.
void EnterPostIteration()
Set post-process flag of the vOptimizer to true.
void EnterPreIteration()
Set pre-process flag of the vOptimizer to true.
void ExitPostIteration()
Set post-process flag of the vOptimizer to false.
int ImageUpdateStep()
A function dedicated to the update step in the image space (performed after the loop on events) ...
int GetNbThreadsForProjection()
Get the number of threads used for projections.
int StepAfterSubsetLoop(int a_iteration)
bool NeedPostIteration()
Say if the optimizer needs a post-process loop after iterations are done.
bool * mp_outputIterations
vDataFile ** m2p_DataFile
This class is designed to manage some profiling of the code.
int GetCurrentTimeFrame(int a_th)
void ExitPreIteration()
Set pre-process flag of the vOptimizer to false.
int SaveSensitivityImage(const string &a_pathToSensitivityImage)
#define DYNAMIC_SWITCH_END_LOOP
void InitBackwardImage()
Initialize each voxel of the backward images to 0, also for sensitivity if not loaded (estimated on t...
int PreOrPostIterationIndependentStep()
Perform an additional loop over data as well as the pre and post steps inside subset loop but without...
static sChronoManager * GetInstance()
Instantiate the singleton if not already done, then return the pointer to its instance.
int GetMPIRank()
Get the MPI instance number (the rank)
int ApplyDynamicModel(oImageSpace *ap_ImageS, int a_iteration, int a_subset)
void SetOutputIterations(bool *ap_outputIterations)
Set the selected output iterations to the vOptimizer.
oDynamicModelManager * mp_DynamicModelManager