PSFEstimationwithCPSO
|
00001 /* 00002 * psf_estimator.cpp 00003 * 00004 * Created on: 16/07/2012 00005 * Author: Peter Frank Perroni (pfperroni@inf.ufpr.br) 00006 */ 00007 00008 #include "psf_estimator.hpp" 00009 00010 CPSO **PsfEstimator::cpso_pool; 00011 bool *PsfEstimator::cpso_in_use; 00012 int PsfEstimator::cpso_pool_size = 0; 00013 omp_lock_t PsfEstimator::mutex; 00014 double *PsfEstimator::zernikes; 00015 int *PsfEstimator::phase_mask; 00016 int *PsfEstimator::diffraction_mask; 00017 int PsfEstimator::phase_size, PsfEstimator::image_size, PsfEstimator::n_zernikes, PsfEstimator::psf_range, PsfEstimator::n_particles, PsfEstimator::n_swarms; 00018 WORD PsfEstimator::w, PsfEstimator::c1, PsfEstimator::c2, PsfEstimator::reset_at; 00019 00042 void PsfEstimator::startup(int _cpso_pool_size, double* _zernikes, int *_phase_mask, int *_diffraction_mask, 00043 int _phase_size, int _image_size, int _n_zernikes, int _psf_range, WORD _w, WORD _c1, WORD _c2, 00044 WORD _reset_at, int _n_particles, int _n_swarms, vector<int> *_devices){ 00045 // Allows one 1 startup. 00046 if(cpso_pool_size > 0 || _cpso_pool_size < 1){ 00047 return; 00048 } 00049 00050 omp_init_lock(&mutex); 00051 clFactory::startup(_devices); 00052 00053 #ifdef _PROFILING_ 00054 // Reset the profiling counters and initialize the locks. 00055 Profiling::startup(); 00056 #endif 00057 00058 zernikes = _zernikes; 00059 phase_mask = _phase_mask; 00060 diffraction_mask = _diffraction_mask; 00061 phase_size = _phase_size; 00062 image_size = _image_size; 00063 n_zernikes = _n_zernikes; 00064 psf_range = _psf_range; 00065 w = _w; 00066 c1 = _c1; 00067 c2 = _c2; 00068 reset_at = _reset_at; 00069 n_particles = _n_particles; 00070 n_swarms = _n_swarms; 00071 00072 cpso_pool_size = _cpso_pool_size; 00073 cpso_in_use = new bool[cpso_pool_size]; 00074 cpso_pool = new CPSO*[cpso_pool_size]; 00075 for(int i=0; i < cpso_pool_size; i++){ 00076 // Lazy load pool. 00077 cpso_pool[i] = NULL; 00078 cpso_in_use[i] = false; 00079 } 00080 } 00081 00088 void PsfEstimator::shutdown(){ 00089 for(int i=0; i < cpso_pool_size; i++){ 00090 if(cpso_pool[i] != NULL){ 00091 cpso_pool[i]->finalize_cl(); 00092 delete cpso_pool[i]; 00093 } 00094 } 00095 delete[] cpso_pool; 00096 delete cpso_in_use; 00097 00098 omp_destroy_lock(&mutex); 00099 00100 #ifdef _PROFILING_ 00101 Profiling::shutdown(); 00102 #endif 00103 00104 CPSO::clear_static_data(); 00105 clFactory::shutdown(); 00106 } 00107 00127 CPSO* PsfEstimator::getCPSO_internal(double *object, double *image, WORD *startp_coefs){ 00128 CPSO *cpso = NULL; 00129 int i; 00130 while(cpso == NULL){ 00131 omp_set_lock(&mutex); 00132 for(i=0; i < cpso_pool_size; i++){ 00133 // If there's no instance into this position of the pool, creates one. 00134 if(cpso_pool[i] == NULL){ 00135 cpso_pool[i] = new CPSO(zernikes, phase_mask, diffraction_mask, phase_size, image_size, n_zernikes, 00136 psf_range, w, c1, c2, reset_at, n_particles, n_swarms); 00137 } 00138 // Search for one instance in the pool that is not in use. 00139 if(!cpso_pool[i]->isInUse()){ 00140 cpso = cpso_pool[i]; 00141 // Lock the CPSO instance to inform that it's being used. 00142 cpso->lock(); 00143 omp_unset_lock(&mutex); 00144 // If provided, set the coefficients for the startup of the search. 00145 if(startp_coefs != NULL){ 00146 cpso->setStartupCoefs(startp_coefs); 00147 } 00148 // Set the new Object and Image for the search. 00149 cpso->set_images(object, image); 00150 return cpso; 00151 } 00152 } 00153 omp_unset_lock(&mutex); 00154 // If all instances are in use, wait a moment before checking again. 00155 usleep(10000); 00156 } 00157 return cpso; 00158 } 00159 00170 CPSO* PsfEstimator::getCPSO(double *object, double *image, WORD *startp_coefs){ 00171 return getCPSO_internal(object, image, startp_coefs); 00172 } 00173 00181 CPSO* PsfEstimator::getCPSO(double *object, double *image){ 00182 return getCPSO_internal(object, image, NULL); 00183 } 00184 00198 void PsfEstimator::resetPool(WORD _w, WORD _c1, WORD _c2, WORD _reset_at, int _n_particles, int _n_swarms){ 00199 omp_set_lock(&mutex); 00200 w = _w; 00201 c1 = _c1; 00202 c2 = _c2; 00203 reset_at = _reset_at; 00204 n_particles = _n_particles; 00205 n_swarms = _n_swarms; 00206 00207 for(int i=0; i < cpso_pool_size; i++){ 00208 if(cpso_pool[i] != NULL){ 00209 while(cpso_pool[i]->isInUse()) usleep(10000); 00210 cpso_pool[i]->finalize_cl(); 00211 delete cpso_pool[i]; 00212 cpso_pool[i] = NULL; 00213 cpso_in_use[i] = false; 00214 } 00215 } 00216 omp_unset_lock(&mutex); 00217 }