HypreSolver3D::HypreSolver3D(ptrdiff_t N0_, double Lz_, ptrdiff_t N1_, double Ly_, ptrdiff_t N2_, double Lx_): N0(N0_), Lz(Lz_), N1(N1_), Ly(Ly_), N2(N2_), Lx(Lx_) { MPI_Comm_rank(MPI_COMM_WORLD, &myid); MPI_Comm_size(MPI_COMM_WORLD, &num_procs); N = N0*N1*N2; dz = Lz/(N0+1); dy = Ly/(N1+1); dx = Lx/(N2+1); h2 = dy*dy; nx = N2; ny = N1; int zp = (int)(N0/num_procs); int extra = N0-zp*num_procs; if(myid<extra) { z0 = myid*zp + myid; nz = zp+1; } else { z0 = myid*zp + extra; nz = zp; } local_size = ny*nx*nz; ilower = z0*nx*ny; iupper = ilower+local_size-1; // Build rhs and init x HYPRE_IJVectorCreate(MPI_COMM_WORLD, ilower, iupper, &hv_b); HYPRE_IJVectorSetObjectType(hv_b, HYPRE_PARCSR); HYPRE_IJVectorCreate(MPI_COMM_WORLD, ilower, iupper, &hv_x); HYPRE_IJVectorSetObjectType(hv_x, HYPRE_PARCSR); std::vector<double> rhs_values(local_size, 1.); std::vector<double> x_values(local_size, 0.); rows.resize(local_size); for (int i=0; i<local_size; i++) rows[i] = ilower + i; set_x0(&x_values[0]); set_rhs(&rhs_values[0]); build_A(); build_solver(); }
void HypreSolver2D::solve(double *x) { set_rhs(x); HYPRE_BoomerAMGSolve(solver, parcsr_A, par_b, par_x); HYPRE_IJVectorGetValues(hv_x, local_size, &rows[0], x); HYPRE_BoomerAMGGetNumIterations(solver, &num_iterations); HYPRE_BoomerAMGGetFinalRelativeResidualNorm(solver, &final_res_norm); }
/* parses a line of the file * tries to set the corresponding row in the matrix * returns false on error */ bool parse_row(char* s, int row, LinearProgram* lp) { assert(lp_is_valid(lp)); assert(row >= 0); assert(row < get_rows(lp)); char* end_ptr; int cols = get_cols(lp); int i; for (i = 0; i < cols; i++) { num_t num = parse_num(s, &end_ptr); if (!is_num_valid(num, s, end_ptr)) { return false; } set_coef(lp, row, i, num); s = end_ptr; } s = parse_type(s, row, lp); if (NULL == s) { return false; } num_t num = parse_num(s, &end_ptr); if (!is_num_valid(num, s, end_ptr)) { return false; } s = end_ptr; s = skip_spaces(s); if ('\0' != *s) { return false; } set_rhs(lp, row, num); assert(lp_is_valid(lp)); return true; }
bool Dstar::search(Graph* graph, const Position& start, const Position& goal) { auto s = graph->get_state(start); s->set_rhs(MAX_WEIGHT); s->set_g(MAX_WEIGHT); auto g = graph->get_state(goal); auto g_elem = std::find(open_.begin(), open_.end(), g); if(g_elem == open_.end()) { g->set_rhs(0); g->set_g(MAX_WEIGHT); open_.push_back(g); } g->set_h(graph->get_h(start, g->get_position())); auto current = g; auto search_limit = graph->get_states().size() * 2; std::size_t cnt = 0; while((current->get_key() < s->get_key()) || (s->rhs() != s->g())) { if(cnt > search_limit) { emit report_exp_dst(cnt); return false; } ++cnt; current->set_expanded(); if(current->g() > current->rhs()) { current->set_g(current->rhs()); for(auto& pred : graph->get_succ(current)) // succ = pred update_state(pred, start, goal, graph); } else { current->set_g(MAX_WEIGHT); update_state(current, start, goal, graph); for(auto& pred : graph->get_succ(current)) // succ = pred update_state(pred, start, goal, graph); } auto it_elem = std::find(open_.begin(), open_.end(), current); if(it_elem != open_.end()) open_.erase(it_elem); current = *(std::min_element(open_.begin(), open_.end(), [](State * l, State * r) { return l->get_key() < r->get_key(); })); } emit report_exp_dst(cnt); return true; }