// [[Rcpp::export]] arma::umat CalcNextState( const arma::umat& statesPrev, const double probIgnition, const double probGrowth ) { const unsigned int numRows = statesPrev.n_rows; const unsigned int numCols = statesPrev.n_cols; arma::umat statesNext(numRows, numCols); StateUpdaterWorker worker(statesPrev, statesNext, numRows, numCols, probIgnition, probGrowth); parallelFor(0, numCols, worker); //worker(0, numCols); return statesNext; }
void SphSystemData2::updateDensities() { auto p = positions(); auto d = densities(); const double m = mass(); parallelFor( kZeroSize, numberOfParticles(), [&] (size_t i) { double sum = sumOfKernelNearby(p[i]); d[i] = m * sum; }); }
void ParticleSystemSolver2::endAdvanceTimeStep(double timeStepInSeconds) { // Update data size_t n = _particleSystemData->numberOfParticles(); auto positions = _particleSystemData->positions(); auto velocities = _particleSystemData->velocities(); parallelFor( kZeroSize, n, [&] (size_t i) { positions[i] = _newPositions[i]; velocities[i] = _newVelocities[i]; }); onEndAdvanceTimeStep(timeStepInSeconds); }
// [[Rcpp::export]] List make_index_concurrent_hash_map( DataFrame data, CharacterVector by ){ int n = data.nrows() ; Visitors visitors(data, by) ; VisitorSetHasher<Visitors> hasher(visitors) ; VisitorSetEqualPredicate<Visitors> equal(visitors) ; ConcurrentMap map(1024, hasher, equal) ; IndexMaker2 indexer(map) ; parallelFor(0, n, indexer) ; return indexer.get() ; }
void ParticleSystemSolver2::resolveCollision( ArrayAccessor1<Vector2D> newPositions, ArrayAccessor1<Vector2D> newVelocities) { if (_collider != nullptr) { size_t numberOfParticles = _particleSystemData->numberOfParticles(); const double radius = _particleSystemData->radius(); parallelFor( kZeroSize, numberOfParticles, [&] (size_t i) { _collider->resolveCollision( radius, _restitutionCoefficient, &newPositions[i], &newVelocities[i]); }); } }
void ParticleSystemSolver2::timeIntegration(double timeStepInSeconds) { size_t n = _particleSystemData->numberOfParticles(); auto forces = _particleSystemData->forces(); auto velocities = _particleSystemData->velocities(); auto positions = _particleSystemData->positions(); const double mass = _particleSystemData->mass(); parallelFor( kZeroSize, n, [&] (size_t i) { // Integrate velocity first Vector2D& newVelocity = _newVelocities[i]; newVelocity = velocities[i] + timeStepInSeconds * forces[i] / mass; // Integrate position. Vector2D& newPosition = _newPositions[i]; newPosition = positions[i] + timeStepInSeconds * newVelocity; }); }
void ParticleSystemSolver2::accumulateExternalForces() { size_t n = _particleSystemData->numberOfParticles(); auto forces = _particleSystemData->forces(); auto velocities = _particleSystemData->velocities(); auto positions = _particleSystemData->positions(); const double mass = _particleSystemData->mass(); parallelFor( kZeroSize, n, [&] (size_t i) { // Gravity Vector2D force = mass * _gravity; // Wind forces Vector2D relativeVel = velocities[i] - _wind->sample(positions[i]); force += -_dragCoefficient * relativeVel; forces[i] += force; }); }
// [[Rcpp::export]] List detail_make_index_concurrent_hash_map( DataFrame data, CharacterVector by ){ Timer timer ; timer.step( "start" ) ; int n = data.nrows() ; Visitors visitors(data, by) ; VisitorSetHasher<Visitors> hasher(visitors) ; VisitorSetEqualPredicate<Visitors> equal(visitors) ; ConcurrentMap map(1024, hasher, equal) ; IndexMaker2 indexer(map) ; parallelFor(0, n, indexer) ; timer.step( "train and join" ) ; List res = indexer.get() ; timer.step( "structure" ) ; return List::create( (SEXP)timer, res ) ; }
void parallelFor(const std::vector<T> & elements, std::function<void(const T & element)> callback) { parallelFor(0, elements.size(), [callback, &elements](size_t i) { callback(elements[i]); }); }
// [[Rcpp::export]] SEXP cpp_replace(CharacterVector input, SEXP regexp, CharacterVector rewrite_, bool global_, bool parallel, size_t grain_size) { string errmsg; auto inputv = as_vec_opt_string(input); auto rewrite = as_vec_opt_string(rewrite_); vector<OptRE2 *> ptrv; build_regex_vector(regexp, ptrv); auto nrecycle = re2r_recycling_rule(true, 3, input.size(), ptrv.size(), rewrite.size()); for (auto i = 0; i != nrecycle; i++) { auto optptr = ptrv[i % ptrv.size()]; auto rewritei = rewrite[i % rewrite.size()]; if (bool(rewritei) && bool(*optptr)) { if (!optptr->value()->CheckRewriteString(rewritei.value(), &errmsg)) { throw ErrorRewriteString(errmsg); } } } if (!global_) { vector<tr2::optional<string>> replace_res; replace_res.reserve(nrecycle); if (!parallel || nrecycle < grain_size) { for (auto i = 0; i != nrecycle; i++) { auto optptr = ptrv[i % ptrv.size()]; auto rewritei = rewrite[i % rewrite.size()]; tr2::optional<string> & strix = inputv[i % input.size()]; if (!bool(strix) || !bool(*optptr)) { replace_res.push_back(tr2::nullopt); continue; } replace_res.push_back(strix.value()); tr2::optional<string> &stri = replace_res.back(); auto ptr = optptr->value().get(); if (!bool(rewritei)) { if (ptr->Match(stri.value(), 0, strlen(stri.value().c_str()), RE2::UNANCHORED, nullptr, 0)) { stri = tr2::nullopt; } continue; } ptr->Replace(&stri.value(), *ptr, rewritei.value()); } CharacterVector res(toprotect_optstring_sexp(replace_res)); return res; } else { optstring replace_res(nrecycle); ReplaceP pobj(inputv, ptrv, rewrite, replace_res); parallelFor(0, replace_res.size(), pobj, grain_size); CharacterVector res(toprotect_optstring_sexp(replace_res)); return res; } } else { vector<size_t> count; count.reserve(nrecycle); if (!parallel || input.size() < grain_size) { vector<tr2::optional<string>> replace_res; replace_res.reserve(nrecycle); for (auto i = 0; i != nrecycle; i++) { auto optptr = ptrv[i % ptrv.size()]; auto rewritei = rewrite[i % rewrite.size()]; tr2::optional<string> &strix = inputv[i % input.size()]; if (!bool(strix) || !bool(*optptr)) { replace_res.push_back(tr2::nullopt); count.push_back(0); continue; } replace_res.push_back(strix); tr2::optional<string> & stri = replace_res.back(); auto ptr = optptr->value().get(); if (!bool(rewritei)) { if (ptr->Match(stri.value(), 0, strlen(stri.value().c_str()), RE2::UNANCHORED, nullptr, 0)) { stri = tr2::nullopt; count.push_back(1); }else{ count.push_back(0); } continue; } count.push_back( ptr->GlobalReplace(&stri.value(), *ptr, rewritei.value())); } CharacterVector res(toprotect_optstring_sexp(replace_res)); res.attr("count") = count; return res; } else { vector<tr2::optional<string>> replace_res(nrecycle); count.resize(input.size()); ReplaceGlobalP pobj(inputv, count, ptrv, rewrite, replace_res); parallelFor(0, replace_res.size(), pobj, grain_size); CharacterVector res(toprotect_optstring_sexp(replace_res)); res.attr("count") = count; return res; } } }