int main(void) { long long n; readInputData(&n); writeOutputData(countPrimes(n), n); return 0; }
Parser::Parser( const QString &df ) { openFileSuccess = checkDataFileOpen(df); inputData = ""; if(openFileSuccess){ //check that string passed is valid openFileSuccess = readInputData(); openFileSuccess = writeOutputData(); } }
XPLANEFlightSimData::XPLANEFlightSimData(QObject *pParent) :ACARSFlightSimData(pParent) { m_pSocket = new QUdpSocket(this); qDebug() << m_pSocket->bind(QHostAddress::LocalHost, 49004) << endl; this->connect(m_pSocket, SIGNAL(readyRead()), this, SLOT(readInputData())); }
int main(){ Simulation *sim = malloc(sizeof(struct simulation)); Settings *sett = malloc(sizeof(struct settings)); Grid *g = malloc(sizeof(struct grid)); Boundary regionBoundaryMap[9]; int i, j, k; double PositionComponentofForceX; double PositionComponentofForceY; double Bz; double TangentVelocityComponentOfForceX; double TangentVelocityComponentOfForceY; double Mass; double vxminus; double vyminus; double t_z; double s_z; double vxprime; double vyprime; double vxplus; double vyplus; double timeStep = sett->timeStep; int region; /**X index of local origin i.e. nearest grid point BEFORE particle push*/ int xStart; /**Y index of local origin i.e. nearest grid point BEFORE particle push*/ int yStart; /**X index of local origin i.e. nearest grid point AFTER particle push*/ int xEnd; /**Y index of local origin i.e. nearest grid point AFTER particle push*/ int yEnd; /**Normalized local x coordinate BEFORE particle push*/ double x; /**Normalized local y coordinate BEFORE particle push*/ double y; /**Normalized distance covered in X direction*/ double deltaX; /**Normalized distance covered in X direction*/ double deltaY; readInputData(sim, g, sett); createBoundaryMap(regionBoundaryMap, sett->simulationWidth, sett->simulationHeight); for(j = 0; j < sett->iterations; j++){ for(i = 0; i < sett->numOfParticles; i++){ /*--------------------------------------------------------------------------/ /---------------- Step 1: particlePush()------------------------------------/ /--------------------------------------------------------------------------*/ //a) particle.storePosition() sim->particles[i].prevX = sim->particles[i].x; sim->particles[i].prevY = sim->particles[i].y; //b)solver.timeStep(particle, force, timeStep)/----Boris solver-----/ PositionComponentofForceX = getPositionComponentofForceX(sim->particles[i]); PositionComponentofForceY = getPositionComponentofForceY(sim->particles[i]); Bz = getBz(sim->particles[i]); TangentVelocityComponentOfForceX = getTangentVelocityComponentOfForceX(sim->particles[i]); TangentVelocityComponentOfForceY = getTangentVelocityComponentOfForceY(sim->particles[i]); Mass = sim->particles[i].mass; // remember for complete() sim->particles[i].prevpositionComponentForceX = PositionComponentofForceX; sim->particles[i].prevpositionComponentForceY = PositionComponentofForceY; sim->particles[i].prevBz = Bz; sim->particles[i].prevtangentVelocityComponentOfForceX = TangentVelocityComponentOfForceX; sim->particles[i].prevtangentVelocityComponentOfForceY = TangentVelocityComponentOfForceY; vxminus = sim->particles[i].vx + PositionComponentofForceX * timeStep / (2.0 * Mass); vyminus = sim->particles[i].vy + PositionComponentofForceY * timeStep / (2.0 * Mass); t_z = sim->particles[i].charge * Bz * timeStep / (2.0 * Mass); //t vector s_z = 2 * t_z / (1 + t_z * t_z); //s vector vxprime = vxminus + vyminus * t_z; vyprime = vyminus - vxminus * t_z; vxplus = vxminus + vyprime * s_z; vyplus = vyminus - vxprime * s_z; sim->particles[i].vx = vxplus + PositionComponentofForceX * timeStep / (2.0 * Mass) + TangentVelocityComponentOfForceX * timeStep / Mass; sim->particles[i].vy = vyplus + PositionComponentofForceY * timeStep / (2.0 * Mass) + TangentVelocityComponentOfForceY * timeStep / Mass; sim->particles[i].x = sim->particles[i].x + sim->particles[i].vx * timeStep; sim->particles[i].y = sim->particles[i].y + sim->particles[i].vy * timeStep; //c) boundaries.applyOnParticleCenter(solver, force, particle, timeStep) region = get_region(sim->particles[i].x, sim->particles[i].x, sim->particles[i].y, sim->particles[i].y, sett->simulationWidth, sett->simulationHeight); sim->particles[i].x = sim->particles[i].x - regionBoundaryMap[region].xoffset; sim->particles[i].prevX = sim->particles[i].prevX - regionBoundaryMap[region].xoffset; sim->particles[i].y = sim->particles[i].y - regionBoundaryMap[region].yoffset; sim->particles[i].prevY = sim->particles[i].prevY - regionBoundaryMap[region].yoffset; /*--------------------------------------------------------------------------/ /---Step 4: interpolation.interpolateToGrid(particles, grid, tstep)---------/ /--------------------------------------------------------------------------*/ resetCurrent(g); x = sim->particles[i].prevX / g->cellWidth; y = sim->particles[i].prevY / g->cellHeight; xStart = (int) floor(x + 0.5); yStart = (int) floor(y + 0.5); deltaX = sim->particles[i].x / g->cellWidth; deltaY = sim->particles[i].y / g->cellHeight; xEnd = (int) floor(deltaX + 0.5); yEnd = (int) floor(deltaY + 0.5); deltaX -= x; deltaY -= y; x -= xStart; y -= yStart; double pCharge = sim->particles[i].charge; //4-boundary move? if (xStart == xEnd && yStart == yEnd) { fourBoundaryMove(xStart, yStart, x, y, deltaX, deltaY, pCharge, g); } //7-boundary move? else if (xStart == xEnd || yStart == yEnd) { sevenBoundaryMove(x, y, xStart, yStart, xEnd, yEnd, deltaX, deltaY, pCharge, g); } // 10-boundary move else { tenBoundaryMove(x, y, xStart, yStart, xEnd, yEnd, deltaX, deltaY, pCharge, g); } createBoundaryCells(g); } } // for(i = 0; i < sett->numOfParticles; i++){ // // fscanf(inParticles, "%lg", &sim->particles[i].x); // printf("%lg\n", sim->particles[i].x); // // fscanf(inParticles, "%lg", &sim->particles[i].y); // printf("%lg\n", sim->particles[i].y); // // fscanf(inParticles, "%lg", &sim->particles[i].radius); // printf("%lg\n", sim->particles[i].radius); // // fscanf(inParticles, "%lg", &sim->particles[i].vx); // printf("%lg\n", sim->particles[i].vx); // // fscanf(inParticles, "%lg", &sim->particles[i].vy); // printf("%lg\n", sim->particles[i].vy); // // fscanf(inParticles, "%lg", &sim->particles[i].ax); // printf("%lg\n", sim->particles[i].ax); // // fscanf(inParticles, "%lg", &sim->particles[i].ay); // printf("%lg\n", sim->particles[i].ay); // // fscanf(inParticles, "%lg", &sim->particles[i].mass); // printf("%lg\n", sim->particles[i].mass); // // fscanf(inParticles, "%lg", &sim->particles[i].charge); // printf("%lg\n", sim->particles[i].charge); // // fscanf(inParticles, "%lg", &sim->particles[i].prevX); // printf("%lg\n", sim->particles[i].prevX); // // fscanf(inParticles, "%lg", &sim->particles[i].prevY); // printf("%lg\n", sim->particles[i].prevY); // // fscanf(inParticles, "%lg", &sim->particles[i].Ex); // printf("%lg\n", sim->particles[i].Ex); // // fscanf(inParticles, "%lg", &sim->particles[i].Ey); // printf("%lg\n", sim->particles[i].Ey); // // fscanf(inParticles, "%lg", &sim->particles[i].Bz); // printf("%lg\n", sim->particles[i].Bz); // // fscanf(inParticles, "%lg", &sim->particles[i].prevpositionComponentForceX); // printf("%lg\n", sim->particles[i].prevpositionComponentForceX); // // fscanf(inParticles, "%lg", &sim->particles[i].prevpositionComponentForceY); // printf("%lg\n", sim->particles[i].prevpositionComponentForceY); // // fscanf(inParticles, "%lg", &sim->particles[i].prevtangentVelocityComponentOfForceX); // printf("%lg\n", sim->particles[i].prevtangentVelocityComponentOfForceX); // // fscanf(inParticles, "%lg", &sim->particles[i].prevtangentVelocityComponentOfForceY); // printf("%lg\n", sim->particles[i].prevtangentVelocityComponentOfForceY); // // fscanf(inParticles, "%lg", &sim->particles[i].prevnormalVelocityComponentOfForceX); // printf("%lg\n", sim->particles[i].prevnormalVelocityComponentOfForceX); // // fscanf(inParticles, "%lg", &sim->particles[i].prevnormalVelocityComponentOfForceY); // printf("%lg\n", sim->particles[i].prevnormalVelocityComponentOfForceY); // // fscanf(inParticles, "%lg", &sim->particles[i].prevBz); // printf("%lg\n", sim->particles[i].prevBz); // // fscanf(inParticles, "%lg", &sim->particles[i].prevLinearDragCoefficient); // printf("%lg\n", sim->particles[i].prevLinearDragCoefficient); // } return 0; }
int main(int argc, char** argv) { int l_speculation_start; utility::thread_number_range threads( task_scheduler_init::default_num_threads, task_scheduler_init::default_num_threads() // run only the default number of threads if none specified ); utility::parse_cli_arguments(argc,argv, utility::cli_argument_pack() //"-h" option for for displaying help is present implicitly .positional_arg(threads,"n-of-threads","number of threads to use; a range of the form low[:high], where low and optional high are non-negative integers or 'auto' for the TBB default.") // .positional_arg(InputFileName,"input-file","input file name") // .positional_arg(OutputFileName,"output-file","output file name") .positional_arg(radius_fraction, "radius-fraction","size of radius at which to start speculating") .positional_arg(nPasses, "number-of-epochs","number of examples used in learning phase") .arg(cancel_test, "cancel-test", "test for cancel signal while finding BMU") .arg(extra_debug, "debug", "additional output") .arg(dont_speculate,"nospeculate","don't speculate in SOM map teaching") ); readInputData(); max_radius = (xMax < yMax) ? yMax / 2 : xMax / 2; // need this value for the 1x1 timing below radius_decay_rate = -(log(1.0/(double)max_radius) / (double)nPasses); find_data_ranges(my_teaching, max_range, min_range ); if(extra_debug) { printf( "Data range: "); remark_SOM_element(min_range); printf( " to "); remark_SOM_element(max_range); printf( "\n"); } // find how much time is taken for the single function_node case. // adjust nPasses so the 1x1 time is somewhere around serial_time_adjust seconds. // make sure the example test runs for at least 0.5 second. for(;;) { task_scheduler_init init(1); SOMap map1(xMax,yMax); speculation_start = nPasses + 1; // Don't speculate xranges = 1; yranges = 1; map1.initialize(InitializeGradient, max_range, min_range); tick_count t0 = tick_count::now(); graph_teach(map1, my_teaching); tick_count t1 = tick_count::now(); double nSeconds = (t1-t0).seconds(); if(nSeconds < 0.5) { xMax *= 2; yMax *= 2; continue; } double size_adjust = sqrt(serial_time_adjust / nSeconds); xMax = (int)((double)xMax * size_adjust); yMax = (int)((double)yMax * size_adjust); max_radius = (xMax < yMax) ? yMax / 2 : xMax / 2; radius_decay_rate = log((double)max_radius) / (double)nPasses; if(extra_debug) { printf("original 1x1 case ran in %g seconds\n", nSeconds); printf(" Size of table == %d x %d\n", xMax, yMax); printf(" radius_decay_rate == %g\n", radius_decay_rate); } break; } // the "max_radius" starts at 1/2*radius_fraction the table size. To start the speculation when the radius is // 1 / n * the table size, the constant in the log below should be n / 2. so 2 == 1/4, 3 == 1/6th, // et c. if(dont_speculate) { l_speculation_start = nPasses + 1; if ( extra_debug )printf("speculation will not be done\n"); } else { if(radius_fraction < 1.0 ) { if ( extra_debug )printf("Warning: radius_fraction should be >= 1. Setting to 1.\n"); radius_fraction = 1.0; } l_speculation_start = (int)((double)nPasses * log(radius_fraction) / log((double)nPasses)); if ( extra_debug )printf( "We will start speculation at iteration %d\n", l_speculation_start ); } double single_time; // for speedup calculations for(int p = threads.first; p <= threads.last; ++p) { task_scheduler_init init(p); if ( extra_debug )printf( " -------------- Running with %d threads. ------------\n", p); // run the SOM build for a series of subranges for(xranges = 1; xranges <= xRangeMax; ++xranges) { for(yranges = xranges; yranges <= yRangeMax; ++yranges) { if(xranges == 1 && yranges == 1) { // don't pointlessly speculate if we're only running one subrange. speculation_start = nPasses + 1; } else { speculation_start = l_speculation_start; } SOMap map1(xMax, yMax); map1.initialize(InitializeGradient, max_range, min_range); if(extra_debug) printf( "Start learning for [%d,%d] ----------- \n", xranges,yranges); tick_count t0 = tick_count::now(); graph_teach(map1, my_teaching); tick_count t1 = tick_count::now(); if ( extra_debug )printf( "Done learning for [%d,%d], which took %g seconds ", xranges,yranges, (t1-t0).seconds()); if(xranges == 1 && yranges == 1) single_time = (t1-t0).seconds(); if ( extra_debug )printf( ": speedup == %g\n", single_time / (t1-t0).seconds()); } // yranges } // xranges } // #threads p printf("done\n"); return 0; }
// ----------------------------------------------------------------------------- // Main ICD reconstruction // ----------------------------------------------------------------------------- void BFReconstructionEngine::execute() { uint64_t totalTime = EIMTOMO_getMilliSeconds(); int32_t err = 0; std::stringstream ss; // int16_t i,j,k,Idx; size_t dims[3]; uint16_t maxNumberOfDetectorElts; uint8_t status; //set to 1 if ICD has converged Real_t checksum = 0, temp; RealImageType::Pointer voxelProfile; RealVolumeType::Pointer detectorResponse; RealVolumeType::Pointer h_t; RealVolumeType::Pointer y_Est; //Estimated Sinogram RealVolumeType::Pointer finalSinogram; //To store and write the final sinogram resulting from our reconstruction RealVolumeType::Pointer errorSino; //Error Sinogram std::string indent(""); //#ifdef COST_CALCULATE //Commented out because if not the code fails to run. std::string filepath(m_TomoInputs->tempDir); filepath = filepath.append(MXADir::getSeparator()).append(MBIR::Defaults::CostFunctionFile); CostData::Pointer cost = CostData::New(); cost->initOutputFile(filepath); //#endif #if OpenMBIR_USE_PARALLEL_ALGORITHMS tbb::task_scheduler_init init; #endif // Initialize the Sinogram if(m_TomoInputs == NULL) { setErrorCondition(-1); notify("Error: The TomoInput Structure was NULL. The proper API is to supply this class with that structure,", 100, Observable::UpdateErrorMessage); return; } //Based on the inputs , calculate the "other" variables in the structure definition if(m_Sinogram == NULL) { setErrorCondition(-1); notify("Error: The Sinogram Structure was NULL. The proper API is to supply this class with that structure,", 100, Observable::UpdateErrorMessage); return; } if(m_Geometry == NULL) { setErrorCondition(-1); notify("Error: The Geometry Structure was NULL. The proper API is to supply this class with that structure,", 100, Observable::UpdateErrorMessage); return; } if (m_ForwardModel.get() == NULL) { setErrorCondition(-1); notify("Error: The forward model was not set.", 100, Observable::UpdateErrorMessage); return; } // Read the Input data from the supplied data file - reconstruction from previous resolution ? err = readInputData(); if(err < 0) { return; } if (getCancel() == true) { setErrorCondition(-999); return; } if (getCancel() == true) { setErrorCondition(-999); return; } //Additional parameters associated with the system err = m_ForwardModel->createNuisanceParameters(m_Sinogram); if(err < 0) { return; } //Periodically checking if user has sent a cancel signal from the GUI if (getCancel() == true) { setErrorCondition(-999); return; } m_ForwardModel->printNuisanceParameters(m_Sinogram); //Take log of the input data after subtracting offset m_ForwardModel->processRawCounts(m_Sinogram); // Initialize the Geometry data from a rough reconstruction err = initializeRoughReconstructionData(); if(err < 0) { return; } if (getCancel() == true) { setErrorCondition(-999); return; } // Get the actual boundaries in the X Direction since we "Extend Object" which makes // the output mrc file much wider than they really need to be. uint16_t cropStart = 0; uint16_t cropEnd = m_Geometry->N_x; computeOriginalXDims(cropStart, cropEnd); // std::cout << "Crop Start: " << cropStart << std::endl; // std::cout << "Crop End: " << cropEnd << std::endl; m_ForwardModel->allocateNuisanceParameters(m_Sinogram); //#ifdef COST_CALCULATE // dims[0] = (m_TomoInputs->NumIter+1)*m_TomoInputs->NumOuterIter*3; //#endif dims[0] = m_Sinogram->N_theta; dims[1] = m_Sinogram->N_r; dims[2] = m_Sinogram->N_t; y_Est = RealVolumeType::New(dims, "y_Est");//y_Est = A*x_{initial} errorSino = RealVolumeType::New(dims, "ErrorSino");// y - y_est m_ForwardModel->weightInitialization(dims); //Initialize the \lambda matrix finalSinogram = RealVolumeType::New(dims, "Final Sinogram"); //Debug variable to store the final A*x_{Final} /*************** Computation of partial Amatrix *************/ //calculate the trapezoidal voxel profile for each angle.Also the angles in the Sinogram // Structure are converted to radians voxelProfile = calculateVoxelProfile(); //This is used for computation of the partial A matrix //Pre compute sine and cos theta to speed up computations DetectorParameters::Pointer haadfParameters = DetectorParameters::New(); haadfParameters->setOffsetR ( ((m_TomoInputs->delta_xz / sqrt(3.0)) + m_Sinogram->delta_r / 2) / m_AdvParams->DETECTOR_RESPONSE_BINS); haadfParameters->setOffsetT ( ((m_TomoInputs->delta_xz / 2) + m_Sinogram->delta_t / 2) / m_AdvParams->DETECTOR_RESPONSE_BINS); haadfParameters->setBeamWidth(m_Sinogram->delta_r); haadfParameters->calculateSinCos(m_Sinogram); //Initialize the e-beam haadfParameters->initializeBeamProfile(m_Sinogram, m_AdvParams); //The shape of the averaging kernel for the detector if (getCancel() == true) { setErrorCondition(-999); return; } //calculate sine and cosine of all angles and store in the global arrays sine and cosine DetectorResponse::Pointer dResponseFilter = DetectorResponse::New(); dResponseFilter->setTomoInputs(m_TomoInputs); dResponseFilter->setSinogram(m_Sinogram); dResponseFilter->setAdvParams(m_AdvParams); dResponseFilter->setDetectorParameters(haadfParameters); dResponseFilter->setVoxelProfile(voxelProfile); dResponseFilter->setObservers(getObservers()); dResponseFilter->setVerbose(getVerbose()); dResponseFilter->setVeryVerbose(getVeryVerbose()); dResponseFilter->execute(); if(dResponseFilter->getErrorCondition() < 0) { ss.str(""); ss << "Error Calling function detectorResponse in file " << __FILE__ << "(" << __LINE__ << ")" << std::endl; setErrorCondition(-2); notify(ss.str(), 100, Observable::UpdateErrorMessage); return; } detectorResponse = dResponseFilter->getResponse(); // Writer the Detector Response to an output file DetectorResponseWriter::Pointer responseWriter = DetectorResponseWriter::New(); responseWriter->setTomoInputs(m_TomoInputs); responseWriter->setSinogram(m_Sinogram); responseWriter->setAdvParams(m_AdvParams); responseWriter->setObservers(getObservers()); responseWriter->setResponse(detectorResponse); responseWriter->setVerbose(getVerbose()); responseWriter->setVeryVerbose(getVeryVerbose()); responseWriter->execute(); if(responseWriter->getErrorCondition() < 0) { ss.str(""); ss << __FILE__ << "(" << __LINE__ << ") " << "Error writing detector response to file." << std::endl; setErrorCondition(-3); notify(ss.str(), 100, Observable::UpdateErrorMessage); return; } if (getCancel() == true) { setErrorCondition(-999); return; } // Initialize H_t volume dims[0] = 1; dims[1] = m_Sinogram->N_theta; dims[2] = m_AdvParams->DETECTOR_RESPONSE_BINS; h_t = RealVolumeType::New(dims, "h_t"); initializeHt(h_t, haadfParameters->getOffsetT() ); checksum = 0; ss.str(""); ss << "Calculating A Matrix...."; notify(ss.str(), 0, Observable::UpdateProgressMessage); std::vector<AMatrixCol::Pointer> voxelLineResponse(m_Geometry->N_y); maxNumberOfDetectorElts = (uint16_t)((m_TomoInputs->delta_xy / m_Sinogram->delta_t) + 2); dims[0] = maxNumberOfDetectorElts; for (uint16_t i = 0; i < m_Geometry->N_y; i++) { AMatrixCol::Pointer vlr = AMatrixCol::New(dims, 0); voxelLineResponse[i] = vlr; } //Calculating A-Matrix one column at a time //For each entry the idea is to initially allocate space for Sinogram.N_theta * Sinogram.N_x // And then store only the non zero entries by allocating a new array of the desired size std::vector<AMatrixCol::Pointer> tempCol(m_Geometry->N_x * m_Geometry->N_z); checksum = 0; temp = 0; uint32_t voxel_count = 0; for (uint16_t z = 0; z < m_Geometry->N_z; z++) { for (uint16_t x = 0; x < m_Geometry->N_x; x++) { tempCol[voxel_count] = AMatrixCol::calculateAMatrixColumnPartial(m_Sinogram, m_Geometry, m_TomoInputs, m_AdvParams, z, x, 0, detectorResponse, haadfParameters); temp += tempCol[voxel_count]->count; if(0 == tempCol[voxel_count]->count ) { //If this line is never hit and the Object is badly initialized //set it to zero for (uint16_t y = 0; y < m_Geometry->N_y; y++) { m_Geometry->Object->setValue(0.0, z, x, y); } } voxel_count++; } } storeVoxelResponse(h_t, voxelLineResponse, haadfParameters); if (getCancel() == true) { setErrorCondition(-999); return; } if(getVerbose()) { printf("Number of non zero entries of the forward projector is %lf\n", temp); printf("Geometry-Z %d\n", m_Geometry->N_z); } /************ End of A matrix partial computations **********************/ //Gain and Offset Parameters Initialization of the forward model m_ForwardModel->gainAndOffsetInitialization(m_Sinogram->N_theta); initializeVolume(y_Est, 0.0); if (getCancel() == true) { setErrorCondition(-999); return; } //Forward Project Geometry->Object one slice at a time and compute the Sinogram for each slice // Forward Project using the Forward Model err = m_ForwardModel->forwardProject(m_Sinogram, m_Geometry, tempCol, voxelLineResponse, y_Est , errorSino); if (err < 0) { return; } if (getCancel() == true) { setErrorCondition(-999); return; } #ifdef FORWARD_PROJECT_MODE return 0; //exit the program once we finish forward projecting the object #endif//Forward Project mode // Initialize the Prior Model parameters - here we are using a QGGMRF Prior Model QGGMRF::QGGMRF_Values QGGMRF_values; QGGMRF::initializePriorModel(m_TomoInputs, &QGGMRF_values, NULL); #ifdef COST_CALCULATE err = calculateCost(cost, m_Sinogram, m_Geometry, errorSino, &QGGMRF_values); #endif //Cost calculation endif Real_t TempBraggValue, DesBraggValue; #ifdef BRAGG_CORRECTION if(m_TomoInputs->NumIter > 1) { //Get the value of the Bragg threshold from the User Interface the first time DesBraggValue = m_ForwardModel->getBraggThreshold(); if (getVeryVerbose()) {std::cout << "Desired Bragg threshold =" << DesBraggValue << std::endl;} TempBraggValue = DefBraggThreshold;//m_ForwardModel->getBraggThreshold(); m_ForwardModel->setBraggThreshold(TempBraggValue); //setting the Threshold T of \beta_{T,\delta} //the \delta value is set in BFMultiResolutionReconstruction.cpp } else { TempBraggValue = m_ForwardModel->getBraggThreshold(); m_ForwardModel->setBraggThreshold(TempBraggValue); } #endif //Bragg correction if (getVeryVerbose()) {std::cout << "Bragg threshold =" << TempBraggValue << std::endl;} //Generate a List if (getVeryVerbose()) {std::cout << "Generating a list of voxels to update" << std::endl;} //Takes all the voxels along x-z slice and forms a list m_VoxelIdxList = VoxelUpdateList::GenRegularList(m_Geometry->N_z, m_Geometry->N_x); //Randomize the list of voxels m_VoxelIdxList = VoxelUpdateList::GenRandList(m_VoxelIdxList); if(getVerbose()) { std::cout << "Initial random order list .." << std::endl; m_VoxelIdxList->printMaxList(std::cout); } // Create a copy of the Voxel Update List because we are going to adjust the VoxelUpdateList instance variable // with values for the array pointer and number of elements based on the iteration VoxelUpdateList::Pointer TempList = VoxelUpdateList::New(m_VoxelIdxList->numElements(), m_VoxelIdxList->getArray() ); uint32_t listselector = 0; //For the homogenous updates this cycles through the random list one sublist at a time //Initialize the update magnitude arrays (for NHICD mainly) & stopping criteria dims[0] = m_Geometry->N_z; //height dims[1] = m_Geometry->N_x; //width dims[2] = 0; RealImageType::Pointer magUpdateMap = RealImageType::New(dims, "Update Map for voxel lines"); RealImageType::Pointer filtMagUpdateMap = RealImageType::New(dims, "Filter Update Map for voxel lines"); UInt8Image_t::Pointer magUpdateMask = UInt8Image_t::New(dims, "Update Mask for selecting voxel lines NHICD");//TODO: Remove this variable. Under the new formulation not needed Real_t PrevMagSum = 0; magUpdateMap->initializeWithZeros(); filtMagUpdateMap->initializeWithZeros(); #if ROI //A mask to check the stopping criteria for the algorithm initializeROIMask(magUpdateMask); #endif //Debugging variable to check if all voxels are visited dims[0] = m_Geometry->N_z; dims[1] = m_Geometry->N_x; dims[2] = 0; UInt8Image_t::Pointer m_VisitCount = UInt8Image_t::New(dims, "VisitCount"); uint32_t EffIterCount = 0; //Maintains number of calls to updatevoxelroutine //Loop through every voxel updating it by solving a cost function for (int16_t reconOuterIter = 0; reconOuterIter < m_TomoInputs->NumOuterIter; reconOuterIter++) { ss.str(""); // Clear the string stream indent = ""; //The first time we may need to update voxels multiple times and then on //just optimize over I,d,sigma,f once each outer loop; //Each outer loop after the first outer iter updates a subset of the voxels //followed by other parameters if needed if(reconOuterIter > 0) { m_TomoInputs->NumIter = 1;//Inner iterations m_ForwardModel->setBraggThreshold(TempBraggValue); } for (int16_t reconInnerIter = 0; reconInnerIter < m_TomoInputs->NumIter; reconInnerIter++) { // If at the inner most loops at the coarsest resolution donot apply Bragg // Only at the coarsest scale the NumIter > 1 if(m_TomoInputs->NumIter > 1 && reconInnerIter == 0) { m_ForwardModel->setBraggThreshold(DefBraggThreshold); } #ifdef DEBUG //Prints the ratio of the sinogram entries selected m_ForwardModel->printRatioSelected(m_Sinogram); #endif //DEBUG ss.str(""); ss << "Outer Iterations: " << reconOuterIter << "/" << m_TomoInputs->NumOuterIter << " Inner Iterations: " << reconInnerIter << "/" << m_TomoInputs->NumIter << std::endl; indent = " "; #ifdef DEBUG // This is all done PRIOR to calling what will become a method if (getVeryVerbose()) {std::cout << "Bragg Threshold =" << m_ForwardModel->getBraggThreshold() << std::endl;} #endif //DEBUG // This could contain multiple Subloops also - voxel update /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ #ifdef NHICD //NHICD alternates between updating a fixed subset of voxels and a //list created on the fly based on the previous iterations // Creating a sublist of voxels for the Homogenous // iterations - cycle through the partial sub lists if(EffIterCount % 2 == 0) //Even iterations == Homogenous update { listselector %= MBIR::Constants::k_NumHomogeniousIter;// A variable to cycle through the randmoized list of voxels int32_t nElements = floor((Real_t)TempList->numElements() / MBIR::Constants::k_NumHomogeniousIter); //If the number of voxels is NOT exactly divisible by NUM_NON .. then compensate and make the last of the lists longer if(listselector == MBIR::Constants::k_NumHomogeniousIter - 1) { nElements += (TempList->numElements() - nElements * MBIR::Constants::k_NumHomogeniousIter); if(getVeryVerbose()) { std::cout << "**********Adjusted number of voxel lines for last iter**************" << std::endl; std::cout << nElements << std::endl; std::cout << "**********************************" << std::endl; } } //Set the list array for updating voxels int32_t start = (uint32_t)(floor(((Real_t)TempList->numElements() / MBIR::Constants::k_NumHomogeniousIter)) * listselector); m_VoxelIdxList = TempList->subList(nElements, start); //m_VoxelIdxList.Array = &(TempList.Array[]); if (getVeryVerbose()) { std::cout << "Partial random order list for homogenous ICD .." << std::endl; m_VoxelIdxList->printMaxList(std::cout); } listselector++; //printList(m_VoxelIdxList); } #endif //NHICD #ifdef DEBUG Real_t TempSum = 0; for (int32_t j = 0; j < m_Geometry->N_z; j++) { for (int32_t k = 0; k < m_Geometry->N_x; k++) { TempSum += magUpdateMap->getValue(j, k); } } if (getVeryVerbose()) { std::cout << "**********************************************" << std::endl; std::cout << "Average mag prior to calling update voxel = " << TempSum / (m_Geometry->N_z * m_Geometry->N_x) << std::endl; std::cout << "**********************************************" << std::endl; } #endif //debug status = updateVoxels(reconOuterIter, reconInnerIter, tempCol, errorSino, voxelLineResponse, cost, &QGGMRF_values, magUpdateMap, filtMagUpdateMap, magUpdateMask, m_VisitCount, PrevMagSum, EffIterCount); #ifdef NHICD if(EffIterCount % MBIR::Constants::k_NumNonHomogeniousIter == 0) // At the end of half an //equivalent iteration compute average Magnitude of recon to test //stopping criteria { PrevMagSum = roiVolumeSum(magUpdateMask); if (getVeryVerbose()) {std::cout << " Previous Magnitude of the Recon = " << PrevMagSum << std::endl;} } #else PrevMagSum = roiVolumeSum(magUpdateMask); std::cout << " Previous Magnitude of the Recon = " << PrevMagSum << std::endl; #endif //NHICD //Debugging information to test if every voxel is getting touched #ifdef NHICD //Debug every equivalent iteration if(EffIterCount % (2 * MBIR::Constants::k_NumNonHomogeniousIter) == 0 && EffIterCount > 0) #endif //NHICD { for (int16_t j = 0; j < m_Geometry->N_z; j++) { for (int16_t k = 0; k < m_Geometry->N_x; k++) { if(m_VisitCount->getValue(j, k) == 0) { printf("Pixel (%d %d) not visited\n", j, k); } } } //Reset the visit counter once we have cycled through //all voxels once via the homogenous updates for (int16_t j = 0; j < m_Geometry->N_z; j++) { for (int16_t k = 0; k < m_Geometry->N_x; k++) { m_VisitCount->setValue(0, j, k); } } } //end of debug EffIterCount++; //cycles between the two types of voxel updates /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ if(status == 0) { break; //stop inner loop if we have hit the threshold value for x } // Check to see if we are canceled. if (getCancel() == true) { setErrorCondition(-999); return; } // Write out the MRC File ; If NHICD only after half an equit do a write #ifdef NHICD if(EffIterCount % (MBIR::Constants::k_NumNonHomogeniousIter) == 0) #endif //NHICD { ss.str(""); ss << m_TomoInputs->tempDir << MXADir::getSeparator() << reconOuterIter << "_" << reconInnerIter << "_" << MBIR::Defaults::ReconstructedMrcFile; writeMRCFile(ss.str(), cropStart, cropEnd); notify(ss.str(), 0, Observable::UpdateIntermediateImage); m_TomoInputs->tempFiles.push_back(ss.str()); } #ifdef COST_CALCULATE //typically run only for debugging /*********************Cost Calculation*************************************/ int16_t err = calculateCost(cost, m_Sinogram, m_Geometry, errorSino, &QGGMRF_values); if(err < 0) { std::cout << "Cost went up after voxel update" << std::endl; return; // break; } /**************************************************************************/ #endif //Cost calculation endif #ifdef BRAGG_CORRECTION //If at the last iteration of the inner loops at coarsest resolution adjust parameters if(reconInnerIter == m_TomoInputs->NumIter - 1 && reconInnerIter != 0) { //The first time at the coarsest resolution at the end of // inner iterations set the Bragg Threshold TempBraggValue = DesBraggValue;//threshold; m_ForwardModel->setBraggThreshold(TempBraggValue); } #endif //Bragg correction } /* ++++++++++ END Inner Iteration Loop +++++++++++++++ */ if(0 == status && reconOuterIter >= 1) //if stopping criteria is met and //number of iterations is greater than 1 { if (getVeryVerbose()) {std::cout << "Exiting the code because status =0" << std::endl;} break; } if(getVeryVerbose()) { std::cout << " Starting nuisance parameter estimation" << std::endl; } if(m_TomoInputs->NumOuterIter > 1) //Dont update any parameters if we just have one outer iteration { if(m_AdvParams->JOINT_ESTIMATION) { m_ForwardModel->jointEstimation(m_Sinogram, errorSino, y_Est, cost); m_ForwardModel->updateSelector(m_Sinogram, errorSino); #ifdef COST_CALCULATE //Debug info int16_t err = calculateCost(cost, m_Sinogram, m_Geometry, errorSino, &QGGMRF_values); if(err < 0) { std::cout << "Cost went up after offset update" << std::endl; break; } #endif//cost } //Joint estimation endif if(m_AdvParams->NOISE_ESTIMATION) { m_ForwardModel->updateWeights(m_Sinogram, errorSino); m_ForwardModel->updateSelector(m_Sinogram, errorSino); #ifdef COST_CALCULATE //err = calculateCost(cost, Weight, errorSino); err = calculateCost(cost, m_Sinogram, m_Geometry, errorSino, &QGGMRF_values); if (err < 0 && reconOuterIter > 1) { std::cout << "Cost went up after variance update" << std::endl; std::cout << "Effective iterations =" << EffIterCount << std::endl; return; //break; } #endif//cost } if(getVeryVerbose()) { std::cout << " Ending nuisance parameter estimation" << std::endl; } } }/* ++++++++++ END Outer Iteration Loop +++++++++++++++ */ indent = ""; #if DEBUG_COSTS cost->printCosts(std::cout); #endif #ifdef FORWARD_PROJECT_MODE int fileError = 0; FILE* Fp6; MAKE_OUTPUT_FILE(Fp6, fileError, m_TomoInputs->tempDir, MBIR::Defaults::BFForwardProjectedObjectFile); if (fileError == 1) { } #endif if (getCancel() == true) { setErrorCondition(-999); return; } /* Write the Gains,Offsets and variance to an output file */ m_ForwardModel->writeNuisanceParameters(m_Sinogram); //Compute final value of Ax_{final} Real_t temp_final = 0.0; for (uint16_t i_theta = 0; i_theta < m_Sinogram->N_theta; i_theta++) { for (uint16_t i_r = 0; i_r < m_Sinogram->N_r; i_r++) { for (uint16_t i_t = 0; i_t < m_Sinogram->N_t; i_t++) { temp_final = m_Sinogram->counts->getValue(i_theta, i_r, i_t) - errorSino->getValue(i_theta, i_r, i_t); finalSinogram->setValue(temp_final, i_theta, i_r, i_t); } } } // This is writing the "ReconstructedSinogram.bin" file m_ForwardModel->writeSinogramFile(m_Sinogram, finalSinogram); // Writes the sinogram to a file if (getCancel() == true) { setErrorCondition(-999); return; } // Writes ReconstructedObject.bin file for next resolution { std::stringstream ss; ss << m_TomoInputs->tempDir << MXADir::getSeparator() << MBIR::Defaults::ReconstructedObjectFile; writeReconstructionFile(ss.str()); } // Write out the VTK file if (m_TomoInputs->vtkOutputFile.empty() == false) { writeVtkFile(m_TomoInputs->vtkOutputFile, cropStart, cropEnd); } // Write out the MRC File if (m_TomoInputs->mrcOutputFile.empty() == false) { writeMRCFile(m_TomoInputs->mrcOutputFile, cropStart, cropEnd); } //Write out avizo fire file if (m_TomoInputs->avizoOutputFile.empty() == false) { writeAvizoFile(m_TomoInputs->avizoOutputFile, cropStart, cropEnd); } //Debug : Writing out the selector array as an MRC file m_ForwardModel->writeSelectorMrc(m_TomoInputs->braggSelectorFile, m_Sinogram, m_Geometry, errorSino); if (getVerbose()) { std::cout << "Final Dimensions of Object: " << std::endl; std::cout << " Nx = " << m_Geometry->N_x << std::endl; std::cout << " Ny = " << m_Geometry->N_y << std::endl; std::cout << " Nz = " << m_Geometry->N_z << std::endl; #ifdef NHICD std::cout << "Number of equivalet iterations taken =" << EffIterCount / MBIR::Constants::k_NumNonHomogeniousIter << std::endl; #else std::cout << "Number of equivalet iterations taken =" << EffIterCount / MBIR::Constants::k_NumHomogeniousIter << std::endl; #endif //NHICD } notify("Reconstruction Complete", 100, Observable::UpdateProgressValueAndMessage); setErrorCondition(0); if (getVerbose()) {std::cout << "Total Running Time for Execute: " << (EIMTOMO_getMilliSeconds() - totalTime) / 1000 << std::endl;} return; }
int driver(const char* szFileStub, int nKStart, int nLMin, unsigned long lSeed, int nMaxIter, double dEpsilon, int bCOut) { t_Params tParams; t_Data tData; gsl_rng *ptGSLRNG = NULL; const gsl_rng_type *ptGSLRNGType = NULL; int i = 0, k = 0, nD = 0, nN = 0; char szOFile[MAX_FILE_NAME_LENGTH]; FILE *ofp = NULL; t_VBParams tVBParams; t_Cluster *ptBestCluster = NULL; gsl_matrix *ptTemp = NULL; gsl_matrix *ptTVar = NULL; /*initialise GSL RNG*/ gsl_rng_env_setup(); gsl_set_error_handler_off(); ptGSLRNGType = gsl_rng_default; ptGSLRNG = gsl_rng_alloc(ptGSLRNGType); /*get command line params*/ tParams.nKStart = nKStart; tParams.nLMin = nLMin; tParams.nMaxIter = nMaxIter; tParams.dEpsilon = dEpsilon; tParams.lSeed = lSeed; setParams(&tParams,szFileStub); /*read in input data*/ readInputData(tParams.szInputFile, &tData); readPInputData(tParams.szPInputFile, &tData); nD = tData.nD; nN = tData.nN; ptTemp = gsl_matrix_alloc(tData.nT,nD); ptTVar = gsl_matrix_alloc(tData.nT,tData.nT); setVBParams(&tVBParams, &tData); ptBestCluster = (t_Cluster *) malloc(sizeof(t_Cluster)); ptBestCluster->nN = nN; ptBestCluster->nK = tParams.nKStart; ptBestCluster->nD = nD; ptBestCluster->ptData = &tData; ptBestCluster->ptVBParams = &tVBParams; ptBestCluster->lSeed = tParams.lSeed; ptBestCluster->nMaxIter = tParams.nMaxIter; ptBestCluster->dEpsilon = tParams.dEpsilon; if(bCOut > 0){ ptBestCluster->szCOutFile = szFileStub; } else{ ptBestCluster->szCOutFile = NULL; } runRThreads((void *) &ptBestCluster); compressCluster(ptBestCluster); calcCovarMatrices(ptBestCluster,&tData); sprintf(szOFile,"%sclustering_gt%d.csv",tParams.szOutFileStub,tParams.nLMin); writeClusters(szOFile,ptBestCluster,&tData); sprintf(szOFile,"%spca_means_gt%d.csv",tParams.szOutFileStub,tParams.nLMin); writeMeans(szOFile,ptBestCluster); sprintf(szOFile,"%smeans_gt%d.csv",tParams.szOutFileStub,tParams.nLMin); writeTMeans(szOFile,ptBestCluster,&tData); for(k = 0; k < ptBestCluster->nK; k++){ sprintf(szOFile,"%spca_variances_gt%d_dim%d.csv",tParams.szOutFileStub,tParams.nLMin,k); writeSquareMatrix(szOFile, ptBestCluster->aptSigma[k], nD); /*not entirely sure this is correct?*/ gsl_blas_dgemm (CblasNoTrans,CblasNoTrans,1.0,tData.ptTMatrix,ptBestCluster->aptSigma[k],0.0,ptTemp); gsl_blas_dgemm (CblasNoTrans,CblasTrans,1.0,ptTemp,tData.ptTMatrix,0.0,ptTVar); sprintf(szOFile,"%svariances_gt%d_dim%d.csv",tParams.szOutFileStub,tParams.nLMin,k); writeSquareMatrix(szOFile, ptTVar, nD); } sprintf(szOFile,"%sresponsibilities.csv",tParams.szOutFileStub); ofp = fopen(szOFile,"w"); if(ofp){ for(i = 0; i < nN; i++){ for(k = 0; k < ptBestCluster->nK - 1; k++){ fprintf(ofp,"%f,",ptBestCluster->aadZ[i][k]); } fprintf(ofp,"%f\n",ptBestCluster->aadZ[i][ptBestCluster->nK - 1]); } fclose(ofp); } else{ fprintf(stderr,"Failed openining %s in main\n", szOFile); fflush(stderr); } sprintf(szOFile,"%svbl.csv",tParams.szOutFileStub); ofp = fopen(szOFile,"w"); if(ofp){ fprintf(ofp,"%d,%f,%d\n",ptBestCluster->nK,ptBestCluster->dVBL,ptBestCluster->nThread); fclose(ofp); } else{ fprintf(stderr,"Failed openining %s in main\n", szOFile); fflush(stderr); } /*free up memory in data object*/ destroyData(&tData); /*free up best BIC clusters*/ destroyCluster(ptBestCluster); free(ptBestCluster); destroyParams(&tParams); gsl_rng_free(ptGSLRNG); gsl_matrix_free(tVBParams.ptInvW0); gsl_matrix_free(ptTemp); gsl_matrix_free(ptTVar); return EXIT_SUCCESS; }