int sthread_mutex_init(sthread_mutex_t *mutex) { int i=0; //lock should be available //to be acquired initially mutex->M = 0; //Q should start out being empty Q_init(&mutex->Q); return 0; }
int main() { Q_init(); pthread_t thread; pthread_create(&thread, NULL, thread2, NULL); thread1(0); pthread_join(thread, NULL); return 0; }
void LG_topPrint(LGraph lg) { unsigned *indegree; unsigned i,count=0; DLL_Node* node_ptr; Queue q; if(lg.count==0) return ; indegree=(unsigned *)malloc(sizeof(unsigned)*lg.count); for(i=0;i<lg.count;i++) indegree[i]=lg.in_ptr[i]; Q_init(&q); for(i=0;i<lg.count;i++) if(indegree[i]==0) Q_push(&q,i); while(!Q_isEmpty(q)) { Q_getFront(q,&i); Q_pop(&q); printf("%c ",lg.d_ptr[i]); ++count; for(node_ptr=lg.l_ptr[i].head;node_ptr;node_ptr=node_ptr->next) { i=node_ptr->data; if(--indegree[i]==0) Q_push(&q,i); } } if(count<lg.count) printf("ͼÖÐÓлØ·£¬ÅÅÐòʧ°Ü\n"); else printf("\n"); Q_clear(&q); free(indegree); }
/******************************************************************************* * For each run, the input filename and restart information (if needed) must * * be given on the command line. For non-restarted case, command line is: * * * * executable <input file name> * * * * For restarted run, command line is: * * * * executable <input file name> <restart directory> <restart number> * * * *******************************************************************************/ int main( int argc, char *argv[]) { // Initialize MPI and SAMRAI. SAMRAI_MPI::init(&argc, &argv); SAMRAI_MPI::setCallAbortInSerialInsteadOfExit(); SAMRAIManager::startup(); {// cleanup dynamically allocated objects prior to shutdown // Parse command line options, set some standard options from the input // file, initialize the restart database (if this is a restarted run), // and enable file logging. Pointer<AppInitializer> app_initializer = new AppInitializer(argc, argv, "advect.log"); Pointer<Database> input_db = app_initializer->getInputDatabase(); Pointer<Database> main_db = app_initializer->getComponentDatabase("Main"); // Get various standard options set in the input file. const bool dump_viz_data = app_initializer->dumpVizData(); const int viz_dump_interval = app_initializer->getVizDumpInterval(); const bool uses_visit = dump_viz_data && app_initializer->getVisItDataWriter(); const bool dump_restart_data = app_initializer->dumpRestartData(); const int restart_dump_interval = app_initializer->getRestartDumpInterval(); const string restart_dump_dirname = app_initializer->getRestartDumpDirectory(); const bool dump_timer_data = app_initializer->dumpTimerData(); const int timer_dump_interval = app_initializer->getTimerDumpInterval(); // Get solver configuration options. bool using_refined_timestepping = false; if (main_db->keyExists("timestepping")) { string timestepping_method = main_db->getString("timestepping"); if (timestepping_method == "SYNCHRONIZED") { using_refined_timestepping = false; } else { using_refined_timestepping = true; } } if (using_refined_timestepping) { pout << "using subcycled timestepping.\n"; } else { pout << "NOT using subcycled timestepping.\n"; } // Create major algorithm and data objects that comprise the // application. These objects are configured from the input database // and, if this is a restarted run, from the restart database. Pointer<AdvectorExplicitPredictorStrategy> explicit_predictor = new AdvectorExplicitPredictorStrategy( "AdvectorExplicitPredictorStrategy", app_initializer->getComponentDatabase("AdvectorExplicitPredictorStrategy")); Pointer<CartesianGridGeometry<NDIM> > grid_geometry = new CartesianGridGeometry<NDIM>( "CartesianGeometry", app_initializer->getComponentDatabase("CartesianGeometry")); Pointer<AdvectorPredictorCorrectorHyperbolicPatchOps> hyp_patch_ops = new AdvectorPredictorCorrectorHyperbolicPatchOps( "AdvectorPredictorCorrectorHyperbolicPatchOps", app_initializer->getComponentDatabase("AdvectorPredictorCorrectorHyperbolicPatchOps"), explicit_predictor, grid_geometry); Pointer<HyperbolicLevelIntegrator<NDIM> > hyp_level_integrator = new HyperbolicLevelIntegrator<NDIM>( "HyperbolicLevelIntegrator", app_initializer->getComponentDatabase("HyperbolicLevelIntegrator"), hyp_patch_ops, true, using_refined_timestepping); Pointer<PatchHierarchy<NDIM> > patch_hierarchy = new PatchHierarchy<NDIM>( "PatchHierarchy", grid_geometry); Pointer<StandardTagAndInitialize<NDIM> > error_detector = new StandardTagAndInitialize<NDIM>( "StandardTagAndInitialize", hyp_level_integrator, app_initializer->getComponentDatabase("StandardTagAndInitialize")); Pointer<BergerRigoutsos<NDIM> > box_generator = new BergerRigoutsos<NDIM>(); Pointer<LoadBalancer<NDIM> > load_balancer = new LoadBalancer<NDIM>( "LoadBalancer", app_initializer->getComponentDatabase("LoadBalancer")); Pointer<GriddingAlgorithm<NDIM> > gridding_algorithm = new GriddingAlgorithm<NDIM>( "GriddingAlgorithm", app_initializer->getComponentDatabase("GriddingAlgorithm"), error_detector, box_generator, load_balancer); Pointer<TimeRefinementIntegrator<NDIM> > time_integrator = new TimeRefinementIntegrator<NDIM>( "TimeRefinementIntegrator", app_initializer->getComponentDatabase("TimeRefinementIntegrator"), patch_hierarchy, hyp_level_integrator, gridding_algorithm); // Setup the advection velocity. const bool u_is_div_free = main_db->getBoolWithDefault("u_is_div_free", false); if (u_is_div_free) { pout << "advection velocity u is discretely divergence free.\n"; } else { pout << "advection velocity u is NOT discretely divergence free.\n"; } Pointer<FaceVariable<NDIM,double> > u_var = new FaceVariable<NDIM,double>("u"); UFunction u_fcn("UFunction", grid_geometry, app_initializer->getComponentDatabase("UFunction")); hyp_patch_ops->registerAdvectionVelocity(u_var); hyp_patch_ops->setAdvectionVelocityIsDivergenceFree(u_var, u_is_div_free); hyp_patch_ops->setAdvectionVelocityFunction(u_var, Pointer<CartGridFunction>(&u_fcn,false)); // Setup the advected quantity. const ConvectiveDifferencingType difference_form = IBAMR::string_to_enum<ConvectiveDifferencingType>( main_db->getStringWithDefault( "difference_form", IBAMR::enum_to_string<ConvectiveDifferencingType>(ADVECTIVE))); pout << "solving the advection equation in " << enum_to_string<ConvectiveDifferencingType>(difference_form) << " form.\n"; Pointer<CellVariable<NDIM,double> > Q_var = new CellVariable<NDIM,double>("Q"); QInit Q_init("QInit", grid_geometry, app_initializer->getComponentDatabase("QInit")); LocationIndexRobinBcCoefs<NDIM> physical_bc_coef( "physical_bc_coef", app_initializer->getComponentDatabase("LocationIndexRobinBcCoefs")); hyp_patch_ops->registerTransportedQuantity(Q_var); hyp_patch_ops->setAdvectionVelocity(Q_var, u_var); hyp_patch_ops->setConvectiveDifferencingType(Q_var, difference_form); hyp_patch_ops->setInitialConditions(Q_var, Pointer<CartGridFunction>(&Q_init,false)); hyp_patch_ops->setPhysicalBcCoefs(Q_var, &physical_bc_coef); // Set up visualization plot file writer. Pointer<VisItDataWriter<NDIM> > visit_data_writer = app_initializer->getVisItDataWriter(); if (uses_visit) hyp_patch_ops->registerVisItDataWriter(visit_data_writer); // Initialize hierarchy configuration and data on all patches. double dt_now = time_integrator->initializeHierarchy(); // Deallocate initialization objects. app_initializer.setNull(); // Print the input database contents to the log file. plog << "Input database:\n"; input_db->printClassData(plog); // Write out initial visualization data. int iteration_num = time_integrator->getIntegratorStep(); double loop_time = time_integrator->getIntegratorTime(); if (dump_viz_data && uses_visit) { pout << "\n\nWriting visualization files...\n\n"; visit_data_writer->writePlotData(patch_hierarchy, iteration_num, loop_time); } // Main time step loop. double loop_time_end = time_integrator->getEndTime(); while (!MathUtilities<double>::equalEps(loop_time,loop_time_end) && time_integrator->stepsRemaining()) { iteration_num = time_integrator->getIntegratorStep(); loop_time = time_integrator->getIntegratorTime(); pout << "\n"; pout << "+++++++++++++++++++++++++++++++++++++++++++++++++++\n"; pout << "At beginning of timestep # " << iteration_num << "\n"; pout << "Simulation time is " << loop_time << "\n"; double dt_new = time_integrator->advanceHierarchy(dt_now); loop_time += dt_now; dt_now = dt_new; pout << "\n"; pout << "At end of timestep # " << iteration_num << "\n"; pout << "Simulation time is " << loop_time << "\n"; pout << "+++++++++++++++++++++++++++++++++++++++++++++++++++\n"; pout << "\n"; // At specified intervals, write visualization and restart files, // and print out timer data. iteration_num += 1; const bool last_step = !time_integrator->stepsRemaining(); if (dump_viz_data && uses_visit && (iteration_num%viz_dump_interval == 0 || last_step)) { pout << "\nWriting visualization files...\n\n"; visit_data_writer->writePlotData(patch_hierarchy, iteration_num, loop_time); } if (dump_restart_data && (iteration_num%restart_dump_interval == 0 || last_step)) { pout << "\nWriting restart files...\n\nn"; RestartManager::getManager()->writeRestartFile(restart_dump_dirname, iteration_num); } if (dump_timer_data && (iteration_num%timer_dump_interval == 0 || last_step)) { pout << "\nWriting timer data...\n\n"; TimerManager::getManager()->print(plog); } } // Determine the accuracy of the computed solution. pout << "\n" << "+++++++++++++++++++++++++++++++++++++++++++++++++++\n" << "Computing error norms.\n\n"; VariableDatabase<NDIM>* var_db = VariableDatabase<NDIM>::getDatabase(); const Pointer<VariableContext> Q_ctx = hyp_level_integrator->getCurrentContext(); const int Q_idx = var_db->mapVariableAndContextToIndex(Q_var, Q_ctx); const int Q_cloned_idx = var_db->registerClonedPatchDataIndex(Q_var, Q_idx); const int coarsest_ln = 0; const int finest_ln = patch_hierarchy->getFinestLevelNumber(); for (int ln = coarsest_ln; ln <= finest_ln; ++ln) { patch_hierarchy->getPatchLevel(ln)->allocatePatchData(Q_cloned_idx, loop_time); } Q_init.setDataOnPatchHierarchy(Q_cloned_idx, Q_var, patch_hierarchy, loop_time); HierarchyMathOps hier_math_ops("HierarchyMathOps", patch_hierarchy); hier_math_ops.setPatchHierarchy(patch_hierarchy); hier_math_ops.resetLevels(coarsest_ln, finest_ln); const int wgt_idx = hier_math_ops.getCellWeightPatchDescriptorIndex(); HierarchyCellDataOpsReal<NDIM,double> hier_cc_data_ops(patch_hierarchy, coarsest_ln, finest_ln); hier_cc_data_ops.subtract(Q_idx, Q_idx, Q_cloned_idx); pout << "Error in " << Q_var->getName() << " at time " << loop_time << ":\n" << " L1-norm: " << hier_cc_data_ops.L1Norm(Q_idx,wgt_idx) << "\n" << " L2-norm: " << hier_cc_data_ops.L2Norm(Q_idx,wgt_idx) << "\n" << " max-norm: " << hier_cc_data_ops.maxNorm(Q_idx,wgt_idx) << "\n" << "+++++++++++++++++++++++++++++++++++++++++++++++++++\n"; if (dump_viz_data && uses_visit) { visit_data_writer->writePlotData(patch_hierarchy, iteration_num+1, loop_time); } }// cleanup dynamically allocated objects prior to shutdown SAMRAIManager::shutdown(); SAMRAI_MPI::finalize(); return 0; }// main
// // Add the points two the two initial triangles of a Tin tile from // file. Also add points from neighbor boundary arrays to the // triangulation to have boundary consistancy // TIN_TILE *initTilePoints(TIN_TILE *tt, double e, short useNodata){ // First two tris TRIANGLE *first = tt->t; TRIANGLE *second = tt->t->p1p3; // Get back to the beginning of the tile data file rewind(tt->gridFile); // Now build list of points in the triangle // Create a dummy tail for both point lists first->points = Q_init(); second->points = Q_init(); first->maxE = DONE; second->maxE = DONE; // Build the two point lists register int row, col; ELEV_TYPE maxE_first=0; ELEV_TYPE maxE_second=0; ELEV_TYPE tempE = 0; R_POINT temp; // iterate through all points and distribute them to the // two triangles for(row=0;row<tt->nrows;row++) { temp.x=row+tt->iOffset; for(col=0;col<tt->ncols;col++) { temp.y=col+tt->jOffset; fread(&temp.z,sizeof(ELEV_TYPE), 1, tt->gridFile); // Only set Z values for corner points since they already exist if(row==0 && col==0){ tt->nw->z = temp.z; continue; } if(row==0 && col==tt->ncols-1){ tt->ne->z = temp.z; continue; } if(row==tt->nrows-1 && col==tt->ncols-1){ tt->se->z = temp.z; continue; } if(row==tt->nrows-1 && col==0){ tt->sw->z = temp.z; continue; } //Ignore edge points if internal tile if(tt->iOffset != 0 && row == 0) continue; if(tt->jOffset != 0 && col == 0) continue; //Skip nodata or change it to min-1 if(temp.z == tt->nodata){ if(!useNodata) continue; else temp.z = tt->min-1; } // Add to the first triangle's list if(inTri2D(first->p1, first->p2, first->p3, &temp)) { Q_insert_elem_head(first->points, temp); //Update max error tempE = findError(temp.x,temp.y,temp.z,first); if (tempE > maxE_first) { maxE_first = tempE; assert(Q_first(first->points)); // store pointer to triangle w/ max err first->maxE = &Q_first(first->points)->e; first->maxErrorValue = tempE; } } // Add to the second triangle's list else { assert(inTri2D(second->p1, second->p2, second->p3, &temp)); Q_insert_elem_head(second->points, temp); //Update max error tempE = findError(temp.x,temp.y,temp.z,second); if (tempE > maxE_second) { maxE_second = tempE; assert(Q_first(second->points)); // store pointer to triangle w/ max err second->maxE = &Q_first(second->points)->e; second->maxErrorValue = tempE; } } }//for col }//for row //end distribute points among initial triangles DEBUG {checkPointList(first); checkPointList(second);} // First triangle has no points with error > e, mark as done if (first->maxE == DONE){ Q_free_queue(first->points); first->points = NULL; first->maxErrorValue = 0; } // Insert max error point into the PQ else PQ_insert(tt->pq,first); // Second triangle has no points with error > e, mark as done if (second->maxE == DONE){ Q_free_queue(second->points); second->points = NULL; second->maxErrorValue = 0; } // Insert max error point into the PQ else PQ_insert(tt->pq,second); // Initialize point pointer arrays // At most points can have (tl*tl)-2*tl points in it // At most bPoints and rPoints can have tl points tt->points = (R_POINT **)malloc( ((tt->nrows * tt->ncols) - (tt->nrows + tt->ncols)) * sizeof(R_POINT*)); tt->bPoints = (R_POINT **)malloc( tt->ncols * sizeof(R_POINT*)); tt->rPoints = (R_POINT **)malloc( tt->nrows * sizeof(R_POINT*)); // Add points to point pointer array tt->points[0]=tt->nw;//nw tt->bPoints[0]=tt->sw;//sw tt->bPoints[1]=tt->se;//se tt->rPoints[0]=tt->ne;//ne tt->rPoints[1]=tt->se;//se tt->pointsCount = 1; tt->bPointsCount = 2; tt->rPointsCount = 2; // // Add boundary points to the triangulation // int i; COORD_TYPE prevX = 0,prevY = 0; TRIANGLE *t1,*t2, *s, *sp; s = tt->t; sp = tt->t->p1p3; if(tt->left != NULL){ // The first & last point in this array are corner points for this // tile so we ignore them for(i = 1; i < tt->left->rPointsCount-1; i++){ assert(s && tt->pq && tt->left->rPoints[i]); assert(prevX <= tt->left->rPoints[i]->x && prevY <= tt->left->rPoints[i]->y); assert(tt->left->rPoints[i] != tt->nw && tt->left->rPoints[i] != tt->ne && tt->left->rPoints[i] != tt->sw && tt->left->rPoints[i] != tt->se); // add 2 tris in s t1 = addTri(tt, s->p1, tt->left->rPoints[i], s->p3, NULL,whichTri(s,s->p1,s->p3,tt),NULL); assert(t1); t2 = addTri(tt, tt->left->rPoints[i], s->p2, s->p3, NULL,t1,whichTri(s,s->p2,s->p3,tt)); assert(t2); // Verify that t1 and t2 are really inside s triangleCheck(s,t1,t2,NULL); // Distribute points in the 2 triangles if(s->maxE != DONE){ s->p1p2 = s->p1p3 = s->p2p3 = NULL; distrPoints(t1,t2,NULL,s,NULL,e,tt); //Mark triangle s for deletion from pq before distrpoints so we //can include its maxE in the newly created triangle PQ_delete(tt->pq,s->pqIndex); } else{ // Since distrpoints normally fixes corner we need to do it here if(s == tt->t){ updateTinTileCorner(tt,t1,t2,NULL); } t1->maxE = t2->maxE = DONE; t1->points = t2->points = NULL; } removeTri(s); tt->numTris++; tt->numPoints++; // Now split the next lowest boundary triangle s = t2; // Should we enforce Delaunay here? prevX = tt->left->rPoints[i]->x; prevY = tt->left->rPoints[i]->y; } } if(tt->top != NULL){ // The first & last point in this array are corner points for this // tile so we ignore them s = sp; prevX = prevY = 0; for(i = 1; i < tt->top->bPointsCount-1; i++){ assert(s && tt->pq && tt->top->bPoints[i]); assert(prevX <= tt->top->bPoints[i]->x && prevY <= tt->top->bPoints[i]->y); // add 2 tris in s t1 = addTri(tt, s->p1, tt->top->bPoints[i], s->p3, NULL,whichTri(s,s->p1,s->p3,tt),NULL); assert(t1); t2 = addTri(tt, tt->top->bPoints[i], s->p2, s->p3, NULL,t1,whichTri(s,s->p2,s->p3,tt)); assert(t2); // Verify that t1 and t2 are really inside s triangleCheck(s,t1,t2,NULL); // Distribute points in the 2 triangles if(s->maxE != DONE){ distrPoints(t1,t2,NULL,s,NULL,e,tt); //Mark triangle sp for deletion from pq before distrpoints so we //can include its maxE in the newly created triangle s->p1p2 = s->p1p3 = s->p2p3 = NULL; PQ_delete(tt->pq,s->pqIndex); } else{ // Since distrpoints normally fixes corner we need to do it here if(s == tt->t){ updateTinTileCorner(tt,t1,t2,NULL); } t1->maxE = t2->maxE = DONE; t1->points = t2->points = NULL; } removeTri(s); tt->numTris++; tt->numPoints++; // Now split the next lowest boundary triangle s = t2; // Should we enforce Delaunay here? prevX = tt->top->bPoints[i]->x; prevY = tt->top->bPoints[i]->y; } } return tt; }
void Q_clear() { Q_init(); }