static int djoy_open(FAR struct file *filep) { FAR struct inode *inode; FAR struct djoy_upperhalf_s *priv; FAR const struct djoy_lowerhalf_s *lower; FAR struct djoy_open_s *opriv; #ifndef CONFIG_DISABLE_POLL djoy_buttonset_t supported; #endif int ret; DEBUGASSERT(filep && filep->f_inode); inode = filep->f_inode; DEBUGASSERT(inode->i_private); priv = (FAR struct djoy_upperhalf_s *)inode->i_private; /* Get exclusive access to the driver structure */ ret = djoy_takesem(&priv->du_exclsem); if (ret < 0) { ierr("ERROR: djoy_takesem failed: %d\n", ret); return ret; } /* Allocate a new open structure */ opriv = (FAR struct djoy_open_s *)kmm_zalloc(sizeof(struct djoy_open_s)); if (!opriv) { ierr("ERROR: Failled to allocate open structure\n"); ret = -ENOMEM; goto errout_with_sem; } /* Initialize the open structure */ #ifndef CONFIG_DISABLE_POLL lower = priv->du_lower; DEBUGASSERT(lower && lower->dl_supported); supported = lower->dl_supported(lower); opriv->do_pollevents.dp_press = supported; opriv->do_pollevents.dp_release = supported; #endif /* Attach the open structure to the device */ opriv->do_flink = priv->du_open; priv->du_open = opriv; /* Attach the open structure to the file structure */ filep->f_priv = (FAR void *)opriv; ret = OK; errout_with_sem: djoy_givesem(&priv->du_exclsem); return ret; }
int sam_ajoy_initialization(void) { int ret; int fd; int i; /* NOTE: The ADC driver was initialized earlier in the bring-up sequence. */ /* Open the ADC driver for reading. */ fd = open("/dev/adc0", O_RDONLY); if (fd < 0) { int errcode = get_errno(); ierr("ERROR: Failed to open /dev/adc0: %d\n", errcode); return -errcode; } /* Detach the file structure from the file descriptor so that it can be * used on any thread. */ ret = file_detach(fd, &g_adcfile); if (ret < 0) { ierr("ERROR: Failed to detach from file descriptor: %d\n", ret); (void)close(fd); return ret; } /* Configure the GPIO pins as interrupting inputs. */ for (i = 0; i < AJOY_NGPIOS; i++) { /* Configure the PIO as an input */ sam_configpio(g_joypio[i]); /* Configure PIO interrupts, attach the interrupt handler, but leave * the interrupt disabled. */ sam_pioirq(g_joypio[i]); (void)irq_attach(g_joyirq[i], ajoy_interrupt); sam_pioirqdisable(g_joyirq[i]); } /* Register the joystick device as /dev/ajoy0 */ ret = ajoy_register("/dev/ajoy0", &g_ajoylower); if (ret < 0) { ierr("ERROR: ajoy_register failed: %d\n", ret); file_close_detached(&g_adcfile); } return ret; }
int btn_register(FAR const char *devname, FAR const struct btn_lowerhalf_s *lower) { FAR struct btn_upperhalf_s *priv; int ret; DEBUGASSERT(devname && lower); /* Allocate a new button driver instance */ priv = (FAR struct btn_upperhalf_s *) kmm_zalloc(sizeof(struct btn_upperhalf_s)); if (!priv) { ierr("ERROR: Failed to allocate device structure\n"); return -ENOMEM; } /* Make sure that all button interrupts are disabled */ DEBUGASSERT(lower->bl_enable); lower->bl_enable(lower, 0, 0, NULL, NULL); /* Initialize the new button driver instance */ priv->bu_lower = lower; nxsem_init(&priv->bu_exclsem, 0, 1); DEBUGASSERT(lower->bl_buttons); priv->bu_sample = lower->bl_buttons(lower); /* And register the button driver */ ret = register_driver(devname, &btn_fops, 0666, priv); if (ret < 0) { ierr("ERROR: register_driver failed: %d\n", ret); goto errout_with_priv; } return OK; errout_with_priv: nxsem_destroy(&priv->bu_exclsem); kmm_free(priv); return ret; }
// ------------------------------------------------------------- // PETScConfigurable::p_unprocessOptions // ------------------------------------------------------------- void PETScConfigurable::p_unprocessOptions(void) { boost::char_separator<char> sep(" \t\f\n\r\v", ""); boost::tokenizer<boost::char_separator<char> > opttok(p_loadedOptions, sep); boost::tokenizer<boost::char_separator<char> >::iterator o; for (o = opttok.begin(); o != opttok.end(); ++o) { if ((*o)[0] == '-' && islower((*o)[1])) { PetscErrorCode ierr(0); try { if (verbose) { std::cout << "p_unprocessOptions: removing \"" << *o << "\"" << std::endl; } ierr = PetscOptionsClearValue( #if PETSC_VERSION_GE(3,7,0) NULL, #endif o->c_str()); } catch (const PETSC_EXCEPTION_TYPE& e) { throw PETScException(ierr, e); } } } p_loadedOptions.clear(); }
static void stmpe811_timeout(int argc, uint32_t arg1, ...) { FAR struct stmpe811_dev_s *priv = (FAR struct stmpe811_dev_s *)((uintptr_t)arg1); int ret; /* Are we still stuck in the pen down state? */ if (priv->sample.contact == CONTACT_DOWN || priv->sample.contact == CONTACT_MOVE) { /* Yes... is the worker thread available? If not, then apparently * we have work already pending? */ if (work_available(&priv->timeout)) { /* Yes.. Transfer processing to the worker thread. Since STMPE811 * interrupts are disabled while the work is pending, no special * action should be required to protect the work queue. */ ret = work_queue(HPWORK, &priv->timeout, stmpe811_timeoutworker, priv, 0); if (ret != 0) { ierr("ERROR: Failed to queue work: %d\n", ret); } } } }
PetscErrorCode PetscNonlinearSolverImplementation::FormJacobian(SNES snes, Vec x, Mat jac, Mat B, void *dummy) { PetscErrorCode ierr(0); // Necessary C cast PetscNonlinearSolverImplementation *solver = (PetscNonlinearSolverImplementation *)dummy; // Copy PETSc's current estimate into // Should be the case, but just make sure BOOST_ASSERT(jac == *(solver->p_petsc_J)); BOOST_ASSERT(B == *(solver->p_petsc_J)); // Not sure about this BOOST_ASSERT(x == *(solver->p_petsc_X)); // May need to do this, which seems slow. // ierr = VecCopy(x, *(solver->p_petsc_X)); CHKERRQ(ierr); // Call the user-specified function (object) to form the Jacobian (solver->p_jacobian)(*(solver->p_X), *(solver->p_J)); return ierr; }
// ------------------------------------------------------------- // PetscNonlinearSolverImplementation::FormFunction // ------------------------------------------------------------- PetscErrorCode PetscNonlinearSolverImplementation::FormFunction(SNES snes, Vec x, Vec f, void *dummy) { PetscErrorCode ierr(0); // Necessary C cast PetscNonlinearSolverImplementation *solver = (PetscNonlinearSolverImplementation *)dummy; // Apparently, you cannot count on x and f (like in FormJacobian()) // being the same as those used to set up SNES, so the PETSc // solution and function vectors are wrapped temporarily for the // user function boost::scoped_ptr<Vector> xtmp(new Vector(new PETScVectorImplementation(x, false))); boost::scoped_ptr<Vector> ftmp(new Vector(new PETScVectorImplementation(f, false))); // Call the user-specified function (object) to form the RHS (solver->p_function)(*xtmp, *ftmp); xtmp.reset(); ftmp.reset(); return ierr; }
int main(int argc, char* argv[]) { // Instantiating a boost::mpi::environment here calls MPI_Init() so // PETSc won't mpi::environment env(argc, argv); mpi::communicator world; int nproc = world.size(); int me = world.rank(); PetscErrorCode ierr(0); ierr = PetscInitialize(&argc, &argv, PETSC_NULL, PETSC_NULL); CHKERRQ(ierr); std::cout << "I am process " << me << " of " << nproc << "." << std::endl; world.barrier(); VWrap *v(new VWrap(world)); v->view(); PetscBool ok; delete v; ierr = PetscFinalize(); CHKERRQ(ierr); world.barrier(); return 0; }
// ------------------------------------------------------------- // PetscNonlinearSolverImplementation::p_solve // ------------------------------------------------------------- void PetscNonlinearSolverImplementation::p_solve(void) { PetscErrorCode ierr(0); p_petsc_X = PETScVector(*p_X); int me(this->processor_rank()); try { ierr = SNESSolve(p_snes, NULL, *p_petsc_X); CHKERRXX(ierr); SNESConvergedReason reason; PetscInt iter; ierr = SNESGetConvergedReason(p_snes, &reason); CHKERRXX(ierr); ierr = SNESGetIterationNumber(p_snes, &iter); CHKERRXX(ierr); std::string msg; if (reason < 0) { msg = boost::str(boost::format("%d: PETSc SNES diverged after %d iterations, reason: %d") % me % iter % reason); throw Exception(msg); } else if (me == 0) { msg = boost::str(boost::format("%d: PETSc SNES converged after %d iterations, reason: %d") % me % iter % reason); std::cerr << msg << std::endl; } } catch (const PETSC_EXCEPTION_TYPE& e) { throw PETScException(ierr, e); } catch (const Exception& e) { throw e; } }
/// Solve again w/ the specified RHS, put result in specified vector (specialized) void p_resolveImpl(const VectorType& b, VectorType& x) const { PetscErrorCode ierr(0); int me(this->processor_rank()); try { const Vec *bvec(PETScVector(b)); Vec *xvec(PETScVector(x)); ierr = KSPSolve(p_KSP, *bvec, *xvec); CHKERRXX(ierr); int its; KSPConvergedReason reason; PetscReal rnorm; ierr = KSPGetIterationNumber(p_KSP, &its); CHKERRXX(ierr); ierr = KSPGetConvergedReason(p_KSP, &reason); CHKERRXX(ierr); ierr = KSPGetResidualNorm(p_KSP, &rnorm); CHKERRXX(ierr); std::string msg; if (reason < 0) { msg = boost::str(boost::format("%d: PETSc KSP diverged after %d iterations, reason: %d") % me % its % reason); throw Exception(msg); } else if (me == 0) { msg = boost::str(boost::format("%d: PETSc KSP converged after %d iterations, reason: %d") % me % its % reason); std::cerr << msg << std::endl; } } catch (const PETSC_EXCEPTION_TYPE& e) { throw PETScException(ierr, e); } catch (const Exception& e) { throw e; } }
/// Solve w/ the specified RHS and estimate (result in x) void p_solveImpl(MatrixType& A, const VectorType& b, VectorType& x) const { PetscErrorCode ierr(0); try { Mat *Amat(PETScMatrix(A)); if (p_matrixSet && this->p_constSerialMatrix) { // KSPSetOperators can be skipped } else { #if PETSC_VERSION_LT(3,5,0) ierr = KSPSetOperators(p_KSP, *Amat, *Amat, SAME_NONZERO_PATTERN); CHKERRXX(ierr); #else ierr = KSPSetOperators(p_KSP, *Amat, *Amat); CHKERRXX(ierr); #endif p_matrixSet = true; } this->p_resolveImpl(b, x); } catch (const PETSC_EXCEPTION_TYPE& e) { throw PETScException(ierr, e); } catch (const Exception& e) { throw e; } }
/* * rlines -- display the last offset lines of the file. */ static void rlines(FILE *fp, const char *fn, off_t off, struct stat *sbp) { struct mapinfo map; off_t curoff, size; int i; if (!(size = sbp->st_size)) return; map.start = NULL; map.fd = fileno(fp); map.mapoff = map.maxoff = size; /* * Last char is special, ignore whether newline or not. Note that * size == 0 is dealt with above, and size == 1 sets curoff to -1. */ curoff = size - 2; while (curoff >= 0) { if (curoff < map.mapoff && maparound(&map, curoff) != 0) { ierr(fn); return; } for (i = curoff - map.mapoff; i >= 0; i--) if (map.start[i] == '\n' && --off == 0) break; /* `i' is either the map offset of a '\n', or -1. */ curoff = map.mapoff + i; if (i >= 0) break; } curoff++; if (mapprint(&map, curoff, size - curoff) != 0) { ierr(fn); exit(1); } /* Set the file pointer to reflect the length displayed. */ if (fseeko(fp, sbp->st_size, SEEK_SET) == -1) { ierr(fn); return; } if (map.start != NULL && munmap(map.start, map.maplen)) { ierr(fn); return; } }
/* * bytes -- read bytes to an offset from the end and display. * * This is the function that reads to a byte offset from the end of the input, * storing the data in a wrap-around buffer which is then displayed. If the * rflag is set, the data is displayed in lines in reverse order, and this * routine has the usual nastiness of trying to find the newlines. Otherwise, * it is displayed from the character closest to the beginning of the input to * the end. */ int bytes(FILE *fp, const char *fn, off_t off) { int ch, len, tlen; char *ep, *p, *t; int wrap; char *sp; if ((sp = p = malloc(off)) == NULL) err(1, "malloc"); for (wrap = 0, ep = p + off; (ch = getc(fp)) != EOF;) { *p = ch; if (++p == ep) { wrap = 1; p = sp; } } if (ferror(fp)) { ierr(fn); free(sp); return 1; } if (rflag) { for (t = p - 1, len = 0; t >= sp; --t, ++len) if (*t == '\n' && len) { WR(t + 1, len); len = 0; } if (wrap) { tlen = len; for (t = ep - 1, len = 0; t >= p; --t, ++len) if (*t == '\n') { if (len) { WR(t + 1, len); len = 0; } if (tlen) { WR(sp, tlen); tlen = 0; } } if (len) WR(t + 1, len); if (tlen) WR(sp, tlen); } } else { if (wrap && (len = ep - p)) WR(p, len); len = p - sp; if (len) WR(sp, len); } free(sp); return 0; }
int idxrewrite (unit *ftnunit) { s_idxwrite (ftnunit); if (isrewcurr (ftnunit->isfd, ftnunit->f77fio_buf) < SUCCESS) ierr (ftnunit->f77errlist.cierr, iserrno, "indexed rewrite"); return 0; }
/* * r_reg -- display a regular file in reverse order by line. */ static int r_reg(FILE *fp, enum STYLE style, off_t off, struct stat *sbp) { off_t start, pos, end; int ch; end = sbp->st_size; if (end == 0) return (0); /* Position before char, ignore last char whether newline or not */ pos = end-2; ch = EOF; start = 0; if (style == RBYTES && off < end) start = end - off; for (; pos >= start; pos--) { /* A seek per char isn't a problem with a smart stdio */ if (fseeko(fp, pos, SEEK_SET) != 0) { ierr(); return (0); } if ((ch = getc(fp)) == '\n') { while (--end > pos) COPYCHAR(fp, ch); end++; if (style == RLINES && --off == 0) break; } else if (ch == EOF) { ierr(); return (0); } } if (pos < start) { if (ch != EOF && ungetc(ch, fp) == EOF) { ierr(); return (0); } while (--end >= start) COPYCHAR(fp, ch); } return (0); }
int djoy_register(FAR const char *devname, FAR const struct djoy_lowerhalf_s *lower) { FAR struct djoy_upperhalf_s *priv; int ret; DEBUGASSERT(devname && lower); /* Allocate a new djoystick driver instance */ priv = (FAR struct djoy_upperhalf_s *) kmm_zalloc(sizeof(struct djoy_upperhalf_s)); if (!priv) { ierr("ERROR: Failed to allocate device structure\n"); return -ENOMEM; } /* Make sure that all djoystick interrupts are disabled */ DEBUGASSERT(lower->dl_enable); lower->dl_enable(lower, 0, 0, NULL, NULL); /* Initialize the new djoystick driver instance */ priv->du_lower = lower; sem_init(&priv->du_exclsem, 0, 1); DEBUGASSERT(lower->dl_sample); priv->du_sample = lower->dl_sample(lower); /* And register the djoystick driver */ ret = register_driver(devname, &djoy_fops, 0666, priv); if (ret < 0) { ierr("ERROR: register_driver failed: %d\n", ret); sem_destroy(&priv->du_exclsem); kmm_free(priv); } return ret; }
static ssize_t btn_read(FAR struct file *filep, FAR char *buffer, size_t len) { FAR struct inode *inode; FAR struct btn_upperhalf_s *priv; FAR const struct btn_lowerhalf_s *lower; int ret; DEBUGASSERT(filep && filep->f_inode); inode = filep->f_inode; DEBUGASSERT(inode->i_private); priv = (FAR struct btn_upperhalf_s *)inode->i_private; /* Make sure that the buffer is sufficiently large to hold at least one * complete sample. * * REVISIT: Should also check buffer alignment. */ if (len < sizeof(btn_buttonset_t)) { ierr("ERROR: buffer too small: %lu\n", (unsigned long)len); return -EINVAL; } /* Get exclusive access to the driver structure */ ret = btn_takesem(&priv->bu_exclsem); if (ret < 0) { ierr("ERROR: btn_takesem failed: %d\n", ret); return ret; } /* Read and return the current state of the buttons */ lower = priv->bu_lower; DEBUGASSERT(lower && lower->bl_buttons); *(FAR btn_buttonset_t *)buffer = lower->bl_buttons(lower); btn_givesem(&priv->bu_exclsem); return (ssize_t)sizeof(btn_buttonset_t); }
static ssize_t djoy_read(FAR struct file *filep, FAR char *buffer, size_t len) { FAR struct inode *inode; FAR struct djoy_upperhalf_s *priv; FAR const struct djoy_lowerhalf_s *lower; int ret; DEBUGASSERT(filep && filep->f_inode); inode = filep->f_inode; DEBUGASSERT(inode->i_private); priv = (FAR struct djoy_upperhalf_s *)inode->i_private; /* Make sure that the buffer is sufficiently large to hold at least one * complete sample. */ if (len < sizeof(djoy_buttonset_t)) { ierr("ERROR: buffer too small: %lu\n", (unsigned long)len); return -EINVAL; } /* Get exclusive access to the driver structure */ ret = djoy_takesem(&priv->du_exclsem); if (ret < 0) { ierr("ERROR: djoy_takesem failed: %d\n", ret); return ret; } /* Read and return the current state of the joystick buttons */ lower = priv->du_lower; DEBUGASSERT(lower && lower->dl_sample); priv->du_sample = lower->dl_sample(lower); *(FAR djoy_buttonset_t *)buffer = priv->du_sample; ret = sizeof(djoy_buttonset_t); djoy_givesem(&priv->du_exclsem); return (ssize_t)ret; }
// ------------------------------------------------------------- // Finalize // ------------------------------------------------------------- /// Does whatever is necessary to shut down the PETSc library void Finalize(void) { if (!Initialized()) return; PetscErrorCode ierr(0); try { ierr = PetscFinalize(); CHKERRXX(ierr); } catch (const PETSC_EXCEPTION_TYPE& e) { throw PETScException(ierr, e); } }
// ------------------------------------------------------------- // Initialize // ------------------------------------------------------------- /// Does whatever is necessary to start up the PETSc library void Initialize(void) { if (Initialized()) return; PetscErrorCode ierr(0); PetscBool flg; #if USE_PROGRESS_RANKS gridpack::parallel::Communicator comm; MPI_Comm world = GA_MPI_Comm(); PETSC_COMM_WORLD = world; #endif try { // Turn this on to enable PETSc logging. #if 0 int argc = 2; char **args; args = new char*[2]; args[0] = new char[32]; args[1] = new char[32]; sprintf(args[0],"powerflow.x"); sprintf(args[1],"-log_summary"); ierr = PetscInitialize(&argc,&args,NULL,NULL); CHKERRXX(ierr); delete [] args[1]; delete [] args[0]; delete [] args; #else ierr = PetscInitializeNoArguments(); CHKERRXX(ierr); #endif PetscOptionsHasName( #if PETSC_VERSION_GE(3,7,0) NULL, #endif NULL, "-log_summary", &flg); ierr = PetscOptionsInsertFile(PETSC_COMM_WORLD, #if PETSC_VERSION_GE(3,7,0) NULL, #endif "gridpack.petscrc", PETSC_FALSE); CHKERRXX(ierr); } catch (const PETSC_EXCEPTION_TYPE& e) { throw PETScException(ierr, e); } // Print out some information on processor configuration int mpi_err, me, nproc; mpi_err = MPI_Comm_rank(PETSC_COMM_WORLD, &me); mpi_err = MPI_Comm_size(PETSC_COMM_WORLD, &nproc); if (mpi_err > 0) { throw gridpack::Exception("MPI initialization failed"); } if (me == 0) { printf("\nGridPACK math module configured on %d processors\n",nproc); } }
// ------------------------------------------------------------- // Initialized // ------------------------------------------------------------- bool Initialized(void) { PetscErrorCode ierr(0); PetscBool result; try { ierr = PetscInitialized(&result); CHKERRXX(ierr); } catch (const PETSC_EXCEPTION_TYPE& e) { throw PETScException(ierr, e); } return result; }
PetscNonlinearSolverImplementation::~PetscNonlinearSolverImplementation(void) { PetscErrorCode ierr(0); try { PetscBool ok; ierr = PetscInitialized(&ok); CHKERRXX(ierr); if (ok) { ierr = SNESDestroy(&p_snes); CHKERRXX(ierr); } } catch (...) { // just eat it } }
/// Destructor ~PETScLinearSolverImplementation(void) { PetscErrorCode ierr(0); try { PetscBool ok; ierr = PetscInitialized(&ok); if (ok) { ierr = KSPDestroy(&p_KSP); CHKERRXX(ierr); } } catch (...) { // just eat it } }
int sam_tsc_setup(int minor) { FAR struct spi_dev_s *dev; int ret; iinfo("minor %d\n", minor); DEBUGASSERT(minor == 0); /* Configure and enable the ADS7843E interrupt pin as an input */ (void)sam_configgpio(GPIO_TCS_BUSY); (void)sam_configgpio(GPIO_TCS_IRQ); /* Configure the PIO interrupt */ sam_gpioirq(GPIO_TCS_IRQ); /* Get an instance of the SPI interface for the touchscreen chip select */ dev = sam_spibus_initialize(TSC_CSNUM); if (!dev) { ierr("ERROR: Failed to initialize SPI chip select %d\n", TSC_CSNUM); return -ENODEV; } /* Initialize and register the SPI touschscreen device */ ret = ads7843e_register(dev, &g_tscinfo, CONFIG_ADS7843E_DEVMINOR); if (ret < 0) { ierr("ERROR: Failed to initialize SPI chip select %d\n", TSC_CSNUM); /* sam_spibus_uninitialize(dev); */ return -ENODEV; } return OK; }
int idxclose (unit *ftnunit, flag idxerr) { if (isclose (ftnunit->isfd) < SUCCESS) ierr (idxerr, iserrno, "indexed close"); if (ftnunit->ufnm) { if (ftnunit->udisp & DELETE) if (iserase (ftnunit->ufnm) < SUCCESS) ierr (idxerr, iserrno, "indexed close"); free (ftnunit->ufnm); ftnunit->ufnm = NULL; } /* The following fixes bug #231656. The pointers involved are initialized to zero (both when originally allocated in f_init() and when reallocated in map_luno()). So, if non-zero, the buffers must have been allocated, and we should free them. */ if (ftnunit->f77syl) { free(ftnunit->f77syl); ftnunit->f77syl = NULL; } if (ftnunit->f77fio_buf) { free(ftnunit->f77fio_buf); ftnunit->f77fio_buf = NULL; ftnunit->f77fio_size = 0; } if (ftnunit->ukeys) { free(ftnunit->ukeys); ftnunit->ukeys = NULL; } ftnunit->ufd = NULL; ftnunit->uconn = 0; ftnunit->luno = 0; ftnunit->ukeyid = -1; return SUCCESS; }
// ------------------------------------------------------------- // PetscNonlinearSolverImplementation::p_build // ------------------------------------------------------------- void PetscNonlinearSolverImplementation::p_build(const std::string& option_prefix) { PetscErrorCode ierr(0); try { ierr = SNESCreate(this->communicator(), &p_snes); CHKERRXX(ierr); p_petsc_F = PETScVector(*p_F); if (!p_function.empty()) { ierr = SNESSetFunction(p_snes, *p_petsc_F, FormFunction, static_cast<void *>(this)); CHKERRXX(ierr); } p_petsc_J = PETScMatrix(*p_J); if (!p_jacobian.empty()) { ierr = SNESSetJacobian(p_snes, *p_petsc_J, *p_petsc_J, FormJacobian, static_cast<void *>(this)); CHKERRXX(ierr); } // set the ierr = SNESSetOptionsPrefix(p_snes, option_prefix.c_str()); CHKERRXX(ierr); KSP ksp; ierr = SNESGetKSP(p_snes, &ksp); CHKERRXX(ierr); ierr = KSPSetOptionsPrefix(ksp, option_prefix.c_str()); CHKERRXX(ierr); PC pc; ierr = KSPGetPC(ksp, &pc); CHKERRXX(ierr); ierr = PCSetOptionsPrefix(pc, option_prefix.c_str()); CHKERRXX(ierr); ierr = SNESMonitorSet(p_snes, MonitorNorms, PETSC_NULL, PETSC_NULL); CHKERRXX(ierr); ierr = SNESSetTolerances(p_snes, p_functionTolerance, PETSC_DEFAULT, p_solutionTolerance, p_maxIterations, PETSC_DEFAULT); ierr = SNESSetFromOptions(p_snes); CHKERRXX(ierr); } catch (const PETSC_EXCEPTION_TYPE& e) { throw PETScException(ierr, e); } }
void SerializedOutputConsole::process(const char *line) { _ss_out.str(""); _ss_err.str(""); StreamInterceptor iout(&stdout, _tmp_out); StreamInterceptor ierr(&stderr, _tmp_err); _console->process(line); iout.getInterceptedOutput(_ss_out); ierr.getInterceptedOutput(_ss_err); string str; SerializedConsoleOutput sco(_ss_out.str(), _ss_err.str(), _console->prompt(), _console->input()); sco.writeToString(&str); std::cout << str; std::cout.flush(); }
/** * * * @param comm * @param props * * @return PETSc options prefix to use */ void PETScConfigurable::p_processOptions(utility::Configuration::CursorPtr props) { if (!props) return; p_prefix = props->get(p_prefixKey, p_generatePrefix(p_comm)); if (*p_prefix.rbegin() != '_') { p_prefix.append("_"); } std::string optsorig, optsmod, optsfmt; optsorig = props->get(p_optionsKey, ""); boost::char_separator<char> sep(" \t\f\n\r\v", ""); boost::tokenizer<boost::char_separator<char> > opttok(optsorig, sep); boost::tokenizer<boost::char_separator<char> >::iterator o; for (o = opttok.begin(); o != opttok.end(); ++o) { optsfmt.append(*o); optsfmt.append(" "); optsmod.append(prefixOptionMaybe(p_prefix, *o)); optsmod.append(" "); } if (verbose) { std::cout << "p_processOptions: in: " << optsorig << std::endl; std::cout << "p_processOptions: fmt: " << optsfmt << std::endl; std::cout << "p_processOptions: out: " << optsmod << std::endl; } p_loadedOptions = optsmod; PetscErrorCode ierr(0); try { ierr = PetscOptionsInsertString( #if PETSC_VERSION_GE(3,7,0) NULL, #endif p_loadedOptions.c_str()); CHKERRXX(ierr); } catch (const PETSC_EXCEPTION_TYPE& e) { throw PETScException(ierr, e); } return; }
static PetscErrorCode fill_pattern(Mat A, InsertMode addv) { PetscErrorCode ierr(0); PetscScalar x(0.0); PetscInt lo, hi; ierr = MatGetOwnershipRange(A, &lo, &hi); CHKERRXX(ierr); for (int i = lo; i < hi; ++i) { for (int j = lo; j < hi; ++j) { ierr = MatSetValues(A, 1, &i, 1, &j, &x, addv); CHKERRXX(ierr); x += 1.0; } } ierr = MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY); CHKERRXX(ierr); ierr = MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY); CHKERRXX(ierr); return ierr; }
static void convert_and_check(Mat A) { PetscErrorCode ierr(0); PetscInt lo, hi; Mat B; ierr = MatConvertToDenseGA(A, &B); CHKERRXX(ierr); ierr = MatGetOwnershipRange(B, &lo, &hi); CHKERRXX(ierr); for (int i = lo; i < hi; ++i) { for (int j = lo; j < hi; ++j) { PetscScalar x; PetscScalar y; ierr = MatGetValues(A, 1, &i, 1, &j, &x); CHKERRXX(ierr); ierr = MatGetValues(B, 1, &i, 1, &j, &y); CHKERRXX(ierr); BOOST_CHECK_EQUAL(x, y); } } ierr = MatDestroy(&B); CHKERRXX(ierr); }