int main () { init (); #pragma omp target update to(Q) check (accum (), accum_ref ()); return 0; }
Real CompositeObjective::TerminalCost(Real tend,const Vector& qend,const Vector& dqend) { ErrorAccumulator accum(norm); for(size_t i=0;i<components.size();i++) { Real d=components[i]->TerminalCost(tend,qend,dqend); Real w = (weights.empty()?1.0:weights[i]); accum.Add(d,w); } return accum.Value(); }
void factor_vm::primitive_callstack_to_array() { data_root<callstack> callstack(ctx->pop(),this); stack_frame_accumulator accum(this); iterate_callstack_object(callstack.untagged(),accum); accum.frames.trim(); ctx->push(accum.frames.elements.value()); }
inline void factorvm::vmprim_callstack_to_array() { gc_root<callstack> callstack(dpop(),this); stack_frame_accumulator accum(this); iterate_callstack_object(callstack.untagged(),accum); accum.frames.trim(); dpush(accum.frames.elements.value()); }
Real CompositeObjective::DifferentialCost(Real t,const Vector& q,const Vector& dq) { ErrorAccumulator accum(norm); for(size_t i=0;i<components.size();i++) { Real d=components[i]->DifferentialCost(t,q,dq); Real w = (weights.empty()?1.0:weights[i]); accum.Add(d,w); } return accum.Value(); }
int main() { // create array of 5 integer values int num[] = { 1, 2, 3, 4, 5 }; // print average value std::cout << "the average value of the integer values is " << accum(&num[0], &num[5]) / 5 << '\n'; // create array of character values char name[] = "templates"; int length = sizeof(name)-1; // (try to) print average character value std::cout << "the average value of the characters in \"" << name << "\" is " << accum(&name[0], &name[length]) / length << '\n'; }
Real CompositeObjective::PathCost(const ParabolicRamp::DynamicPath& path,Real tstart) { //TODO: this isn't exactly the same as the sum of increment costs except //for norm = 1 ErrorAccumulator accum(norm); for(size_t i=0;i<components.size();i++) { Real d=components[i]->PathCost(path,tstart); Real w = (weights.empty()?1.0:weights[i]); accum.Add(d,w); } return accum.Value(); }
Real CompositeObjective::IncrementalCost(Real t,const ParabolicRamp::ParabolicRampND& ramp) { //TODO: this isn't exactly the same as the sum of differential costs except //for norm = 1 ErrorAccumulator accum(norm); for(size_t i=0;i<components.size();i++) { Real d=components[i]->IncrementalCost(t,ramp); Real w = (weights.empty()?1.0:weights[i]); accum.Add(d,w); } return accum.Value(); }
int main () { int i; init (); #pragma omp target update to(Q) for (i = 0; i < TESTITERS; i++) check (accum (i), accum_ref (i)); return 0; }
Data * LocalCS_Data::Average(const vector<Data *> &other_data) const { quaternion quat(_ref_system, NULL); quaternion accum(quat); int i; for (i = 1; i < other_data.size(); ++i) { accum += quaternion(((LocalCS_Data *)other_data[i])->_ref_system, &quat); } vector<float> mat; accum.GetMatrix(mat); return new LocalCS_Data(mat); }
std::shared_ptr<Ledger> buildLedgerImpl( std::shared_ptr<Ledger const> const& parent, NetClock::time_point closeTime, const bool closeTimeCorrect, NetClock::duration closeResolution, Application& app, beast::Journal j, ApplyTxs&& applyTxs) { auto buildLCL = std::make_shared<Ledger>(*parent, closeTime); if (buildLCL->rules().enabled(featureSHAMapV2) && !buildLCL->stateMap().is_v2()) { buildLCL->make_v2(); } // Set up to write SHAMap changes to our database, // perform updates, extract changes { OpenView accum(&*buildLCL); assert(!accum.open()); applyTxs(accum, buildLCL); accum.apply(*buildLCL); } buildLCL->updateSkipList(); { // Write the final version of all modified SHAMap // nodes to the node store to preserve the new LCL int const asf = buildLCL->stateMap().flushDirty( hotACCOUNT_NODE, buildLCL->info().seq); int const tmf = buildLCL->txMap().flushDirty( hotTRANSACTION_NODE, buildLCL->info().seq); JLOG(j.debug()) << "Flushed " << asf << " accounts and " << tmf << " transaction nodes"; } buildLCL->unshare(); // Accept ledger buildLCL->setAccepted( closeTime, closeResolution, closeTimeCorrect, app.config()); return buildLCL; }
ComplexMatrix3x3 ComplexMatrix3x3::operator*(const ComplexMatrix3x3& m) const { ComplexMatrix3x3 t; for (int i = 0; i < 3; ++i) { for (int j = 0; j < 3; ++j) { ComplexNumber accum(0.0); for (int k = 0; k < 3; ++k) { accum = accum + (mMatrix[i][k] * m.Get(k, j)); } t.Set(i, j, accum); } } return t; }
static Point computeAvgBBoxCentroid( std::vector<BVH::ShapeWBound>::iterator startIncl, std::vector<BVH::ShapeWBound>::iterator endIncl ) { //Add toghether all the points Point accum(0.0f, 0.0f, 0.0f); std::vector<BVH::ShapeWBound>::iterator iter = startIncl; while(iter <= endIncl){ accum += (iter++)->centroid; } //Divide by the number of points const size_t numPoints = std::distance(startIncl, endIncl); const float mul = 1.0f / ((float)numPoints); return Point(accum.x * mul, accum.y * mul, accum.z * mul); }
Real CompositeObjective::Delta(PlannerObjectiveBase* priorGoal) { if(priorGoal->TypeString() != TypeString()) return Inf; CompositeObjective* pcomposite = dynamic_cast<CompositeObjective*>(priorGoal); if(pcomposite->components.size() != components.size()) return Inf; ErrorAccumulator accum(norm); for(size_t i=0;i<pcomposite->components.size();i++) { Real w1 = (weights.empty()?1.0:weights[i]); Real w2 = (pcomposite->weights.empty()?1.0:pcomposite->weights[i]); Real d=components[i]->Delta(pcomposite->components[i]); if(IsInf(d)) return Inf; accum.Add(d,w1); accum.Add(w1-w2); } return accum.Value(); }
bool DivideAndConquerShortcutPath( InputPathIt pfirst, InputPathIt plast, InputCostIt cfirst, InputCostIt clast, GeneratorIt gfirst, GeneratorIt glast, OutputPathIt ofirst, const CostCompare& leq) { typedef typename std::iterator_traits<InputPathIt>::value_type PointType; typedef typename std::iterator_traits<InputCostIt>::value_type CostType; const size_t psize = std::distance(pfirst, plast); const size_t csize = std::distance(cfirst, clast); if (psize == 0) { return true; } if (psize != csize + 1) { return false; } if (psize < 2) { for (auto pit = pfirst; pit != plast; ++pit) { *ofirst++ = *pit; } return true; } // compute accumulated costs at each point std::vector<CostType> accum(psize); accum[0] = (CostType)0; auto cit = cfirst; for (size_t i = 1; i < accum.size(); ++i) { accum[i] = accum[i - 1] + *cit++; } auto first = pfirst; auto last = plast; --last; auto acfirst = accum.cbegin(); auto aclast = accum.cend(); --aclast; *ofirst++ = *pfirst; DivideAndConquerShortcutPathRec( first, last, acfirst, aclast, gfirst, glast, ofirst, leq); return true; }
void factor_vm::primitive_callstack_to_array() { data_root<callstack> callstack(ctx->pop(),this); stack_frame_accumulator accum(this); iterate_callstack_object(callstack.untagged(),accum); /* The callstack iterator visits frames in reverse order (top to bottom) */ std::reverse( (stack_frame_in_array*)accum.frames.elements->data(), (stack_frame_in_array*)(accum.frames.elements->data() + accum.frames.count)); accum.frames.trim(); ctx->push(accum.frames.elements.value()); }
// TODO: consider using a vector ADT for the x and y coordinates, and a square // ADT for the square. float computeDistanceFromCenter( int centerX, int centerY, const std::vector<cv::Point>& square ) { std::vector<cv::Point2f> centeredSquare; // Get the square centered. for (size_t i = 0; i < square.size(); i++) { cv::Point2f newPoint(square[i].x - centerX, square[i].y - centerY); centeredSquare.push_back(newPoint); } cv::Point2f accum(0, 0); for (size_t i = 0; i < centeredSquare.size(); i++) { accum.x += centeredSquare[i].x; accum.y += centeredSquare[i].y; } accum.x /= (float) centeredSquare.size(); accum.y /= (float) centeredSquare.size(); return std::sqrt(accum.x * accum.x + accum.y * accum.y); }
void StageAndPhase::update() { // std::cout << qPrintable(id().label()) << " StageAndPhase::update() A\n"; if (inflow.size() != phaseK) { QString msg("inflow's size (phaseK=%1) does not match the size of the recipient (phaseK=%2)"); throw Exception(msg.arg(inflow.size()).arg(phaseK), this); } if (phaseInflow.size() != k) { QString msg("phaseInflow's size (k=%1) does not match the size of the recipient (k=%2)"); throw Exception(msg.arg(phaseInflow.size()).arg(k), this); } applyInstantMortality(); increment(inflowPending, inflow); latestInflow = inflow; inflowTotal += accum(inflow); increment(phaseInflowPending, phaseInflow); latestPhaseInflow = phaseInflow; phaseInflowTotal += accum(phaseInflow); // It remains to consider cases, when only one time step is zero if (TestNum::eqZero(timeStep) || TestNum::eqZero(phaseTimeStep)) { sum = dd->content() + accum(inflowPending) + accum(phaseInflowPending); outflow.fill(0.); phaseOutflow.fill(0.); growth = 0.; return; } DistributedDelay2D::UpdateParameters p; p.dt1 = timeStep; p.dt2 = phaseTimeStep; p.inflow1 = &inflowPending; p.inflow2 = &phaseInflowPending; dd->update(p); inflowPending.fill(0.); phaseInflowPending.fill(0.); sum = dd->content(); outflow = dd->state().outflow1; phaseOutflow = dd->state().outflow2; outflowTotal += accum(outflow); phaseOutflowTotal += accum(phaseOutflow); growth = dd->state().growthRate; // std::cout << qPrintable(id().label()) << " StageAndPhase::update() Z\n"; }
static void run( int n ) { typedef typename policy_type::memory_space memory_space ; // enum { MemoryCapacity = 4000 }; // Triggers infinite loop in memory pool enum { MemoryCapacity = 16000 }; enum { Log2_SuperBlockSize = 12 }; policy_type policy( memory_space() , MemoryCapacity , Log2_SuperBlockSize ); accum_type accum("accum"); typename accum_type::HostMirror host_accum = Kokkos::create_mirror_view( accum ); policy.host_spawn( TestTaskDependence(n,policy,accum) , Kokkos::TaskSingle ); Kokkos::wait( policy ); Kokkos::deep_copy( host_accum , accum ); ASSERT_EQ( host_accum() , n ); }
int main(int argc, char* argv[]) { std::ios::sync_with_stdio(false); std::vector<Fractal::Vector2> masses; //for (int i = 0; i < sources; i++) { // masses.push_back( RandomVector() ); //} { Fractal::Vector2 pos; pos(0) = 0.5; pos(1) = 0.5; masses.push_back(pos); } { Fractal::Vector2 pos; pos(0) = 0.6; pos(1) = 0.5; masses.push_back(pos); } { Fractal::Vector2 pos; pos(0) = 0.5; pos(1) = 0.6; masses.push_back(pos); } Magick::Image image(Magick::Geometry(width * supersample, height * supersample), Magick::ColorRGB(1.0, 0.0, 0.0)); Magick::Image accum(Magick::Geometry(width * supersample * accum_scale, height * supersample * accum_scale), Magick::ColorRGB(1.0, 0.0, 0.0)); ProduceGravityFractal(masses, image, accum); image.scale(Magick::Geometry(width, height)); accum.scale(Magick::Geometry(width * accum_scale, height * accum_scale)); image.write("images/gravity_image.tiff"); accum.write("images/gravity_accum.tiff"); return 0; }
static IplImage* fill(IplImage *a, IplImage *target, IplImage *mask) { int i, ax, ay, w = a->width, h = a->height; int aew = w - PATCH_W, aeh = h - PATCH_W; int tw = target->width, th = target->height; CvSize size = cvGetSize(a); IplImage *ann = cvCreateImage(size, IPL_DEPTH_32S, 1); IplImage *annd = cvCreateImage(size, IPL_DEPTH_32S, 1); IplImage *d = cvCreateImage(size, a->depth, a->nChannels); IplImage *acc = cvCreateImage(size, IPL_DEPTH_32F, a->nChannels+1); IplImage *imask = cvCreateImage(size, mask->depth, mask->nChannels); int32_t *data = (int32_t*)ann->imageData; int32_t *ddata = (int32_t*)annd->imageData; int stride = ann->widthStep/sizeof(int32_t); memset(acc->imageData, 0, acc->imageSize); IplImage *mask8u = cvCreateImage(size, IPL_DEPTH_8U, 1); IplImage *imask8u = cvCreateImage(size, IPL_DEPTH_8U, 1); cvConvertScale(mask, imask, -1, 1); cvConvertScale(mask, mask8u, 256, 0); cvConvertScale(mask8u, imask8u, -2, 256); printf("recursing %dx%d\n", w, h); if (w > 32 && h > 32 && tw > 32 && th > 32) { CvSize ssmall = {w/2, h/2}, tsmall = {tw/2, th/2}; IplImage *ssrc, *stgt, *smsk, *newtgt; ssrc = cvCreateImage(ssmall, a->depth, a->nChannels); stgt = cvCreateImage(tsmall, target->depth, target->nChannels); smsk = cvCreateImage(ssmall, mask->depth, mask->nChannels); cvResize(a, ssrc, CV_INTER_LINEAR); cvResize(target, stgt, CV_INTER_LINEAR); cvResize(mask, smsk, CV_INTER_LINEAR); newtgt = fill(ssrc, stgt, smsk); cvResize(newtgt, target, CV_INTER_LINEAR); cvReleaseImage(&ssrc); cvReleaseImage(&stgt); cvReleaseImage(&smsk); } for (i= 0; i< PM_ITERS; i++) { // completeness term patchmatch(a, target, ann, annd, imask); xy2img(ann, target, d); cvShowImage("completeness", d); for (ay = 0; ay < aeh; ay++) { for (ax = 0; ax < aew; ax++) { int xy = ay * stride + ax; int v = *(data + xy); int xbest = XY_TO_X(v), ybest = XY_TO_Y(v); int dbest = *(ddata + xy); accum(target, acc, xbest, ybest, ax, ay, dbest, imask); } } // coherence term patchmatch(target, a, ann, annd, mask); xy2img(ann, a, d); cvShowImage("coherence", d); for (ay = 0; ay < aeh; ay++) { for (ax = 0; ax < aew; ax++) { int xy = ay * stride + ax; int v = *(data + xy); int xbest = XY_TO_X(v), ybest = XY_TO_Y(v); int dbest = *(ddata + xy); accum(a, acc, xbest, ybest, ax, ay, dbest, mask); } } normalize(target, acc, mask); cvCopy(a, target, imask8u); } printf("finished %dx%d\n", w, h); //cvWaitKey(0); cvReleaseImage(&d); cvReleaseImage(&mask8u); cvReleaseImage(&imask); cvReleaseImage(&ann); cvReleaseImage(&annd); cvReleaseImage(&acc); return target; }
bool ShortcutPath( InputPathIt pfirst, InputPathIt plast, InputCostIt cfirst, InputCostIt clast, GeneratorIt gfirst, GeneratorIt glast, OutputPathIt ofirst, size_t window, size_t granularity, const CostCompare& leq) { typedef typename std::iterator_traits<InputPathIt>::value_type PointType; typedef typename std::iterator_traits<InputCostIt>::value_type CostType; const size_t psize = (size_t)std::distance(pfirst, plast); const size_t csize = (size_t)std::distance(cfirst, clast); if (psize == 0) { return true; } // in all other cases, assert one cost per point transition if (psize != csize + 1) { return false; } // nothing to do with a trivial path if (psize < 2) { for (auto pit = pfirst; pit != plast; ++pit) { *ofirst++ = *pit; } return true; } // gather the accumulated original costs per point std::vector<CostType> accum(psize); accum[0] = (CostType)0; auto cit = cfirst; for (size_t i = 1; i < accum.size(); ++i) { accum[i] = accum[i - 1] + *cit++; } // double-buffered path storage for maintaining the best path returned from // any generator for the segment of interest std::vector<PointType> path1; std::vector<PointType> path2; std::vector<PointType> *wpath = &path1, *rpath = &path2; CostType cost; // temporary cost returned from path generators // initilize curr_start and curr_end to point to the current segment of // interest InputPathIt curr_start = pfirst; InputPathIt curr_end; if (granularity > psize - 1) { curr_end = plast; --curr_end; } else { curr_end = pfirst; std::advance(curr_end, granularity); } // maintain indices into the original path for accessing the accumulated // costs vector size_t start_index = 0; size_t end_index = std::min(psize - 1, granularity); bool cost_improved = false; // iterators to the best path found for the segment of interest; either // point to the original path or to a range from rpath InputPathIt bpfirst = curr_start, bplast = curr_end; ++bplast; // best cost of the segment of interest CostType best_cost = accum[end_index] - accum[start_index]; // initialize the best path for the segment of interest for (auto git = gfirst; git != glast; ++git) { wpath->clear(); if ((*git)(*curr_start, *curr_end, std::back_inserter(*wpath), cost) && leq(cost, best_cost)) { bpfirst = wpath->begin(); bplast = wpath->end(); best_cost = cost; std::swap(rpath, wpath); } } *ofirst++ = *pfirst; while (curr_end != plast) { cost_improved = false; // expand the segment by granularity, without falling off the edge auto lookahead = curr_end; size_t look_dist = std::min(granularity, (size_t)(std::distance(curr_end, plast) - 1)); std::advance(lookahead, look_dist); // look for a better path using the trajectories generated by the path // generators if (look_dist != 0) { CostType exp_cost = accum[end_index + look_dist] - accum[end_index]; CostType new_cost = best_cost + exp_cost; // find the path between these two points with the lowest cost for (auto git = gfirst; git != glast; ++git) { wpath->clear(); if ((*git)(*curr_start, *lookahead, std::back_inserter(*wpath), cost) && leq(cost, new_cost)) { cost_improved = true; bpfirst = wpath->begin(); bplast = wpath->end(); new_cost = cost; std::swap(rpath, wpath); } } best_cost = new_cost; } if (cost_improved) { // keep on truckin' std::advance(curr_end, look_dist); end_index += look_dist; } else { if (look_dist == 0) { // terminate without adding back the final best path since it // gets added after the loop curr_end = plast; end_index = psize; } else { // record the best path auto bit = bpfirst; ++bit; for (; bit != bplast; ++bit) { *ofirst++ = *bit; } curr_start = curr_end; start_index = end_index; std::advance(curr_end, look_dist); end_index += look_dist; // reinitialize best_path for brand new segment of interest cost_improved = false; bpfirst = curr_start; bplast = curr_end; ++bplast; best_cost = accum[end_index] - accum[start_index]; for (auto git = gfirst; git != glast; ++git) { wpath->clear(); if ((*git)(*curr_start, *curr_end, std::back_inserter(*wpath), cost) && leq(cost, best_cost)) { bpfirst = wpath->begin(); bplast = wpath->end(); best_cost = cost; std::swap(rpath, wpath); } } } } } // record the best path auto bit = bpfirst; ++bit; for (; bit != bplast; ++bit) { *ofirst++ = *bit; } return true; }
int HostRender::run(RaytracingContext& context, PixelFunc const& render_pixel, int kill_timeout_seconds, std::function<void()> const& render_overlay) { auto render_pixel_wrapper = [&](int x, int y, RaytracingContext const &ctx, ThreadLocalData *tld) -> glm::vec3 { RenderData data(context, tld); switch(context.params.render_mode) { case RaytracingParameters::RECURSIVE: if (context.params.stereo) { data.camera_mode = Camera::StereoLeft; auto const left = render_pixel(x, y, ctx, data); data.camera_mode = Camera::StereoRight; auto const right = render_pixel(x, y, ctx, data); return combine_stereo(left, right); } else { return render_pixel(x, y, ctx, data); } case RaytracingParameters::DESATURATE: if (context.params.stereo) { data.camera_mode = Camera::StereoLeft; auto const left = render_pixel(x, y, ctx, data); data.camera_mode = Camera::StereoRight; auto const right = render_pixel(x, y, ctx, data); return combine_stereo(desaturate(left), desaturate(right)); } else { return desaturate(render_pixel(x, y, ctx, data)); } case RaytracingParameters::NUM_RAYS: render_pixel(x, y, ctx, data); return heatmap(float(data.num_cast_rays - 1) / 64.0f); case RaytracingParameters::NORMAL: render_pixel(x, y, ctx, data); if (context.params.normal_mapping) return glm::normalize(data.isect.shading_normal) * 0.5f + glm::vec3(0.5f); else return glm::normalize(data.isect.normal) * 0.5f + glm::vec3(0.5f); case RaytracingParameters::BVH_TIME: case RaytracingParameters::TIME: { Timer timer; timer.start(); if(context.params.render_mode == RaytracingParameters::TIME) { auto const color = render_pixel(x, y, ctx, data); (void) color; } else { Ray ray = createPrimaryRay(data, float(x) + 0.5f, float(y) + 0.5f); for(auto& o: context.scene->objects) { BVH *bvh = dynamic_cast<BVH *>(o.get()); if(bvh) { bvh->intersect(ray, nullptr); } } } timer.stop(); return heatmap(static_cast<float>(timer.getElapsedTimeInMilliSec()) * context.params.scale_render_time); } case RaytracingParameters::DUDV: { auto const color = render_pixel(x, y, ctx, data); (void) color; if(!data.isect.isValid()) return glm::vec3(0.0); return heatmap(std::log(1.0f + 5.0f * glm::length(data.isect.dudv))); } case RaytracingParameters::AABB_INTERSECT_COUNT: { Ray ray = createPrimaryRay(data, float(x) + 0.5f, float(y) + 0.5f); glm::vec3 accum(0.0f); for(auto& o: context.scene->objects) { auto *bvh = dynamic_cast<BVH *>(o.get()); if(bvh) { accum += bvh->intersect_count(ray, 0, 0) * 0.02f; } } return accum; } default: /* should never happen */ return glm::vec3(1, 0, 1); } }; if (context.params.interactive) { return run_interactive(context, render_pixel_wrapper, render_overlay); } else { return run_noninteractive(context, render_pixel_wrapper, kill_timeout_seconds); } }
ClockModule() { out_ports.push_back(&accum); accum.position = make_vector(0.25f, 0.0f); accum() = 0.0f; paused = true; }
void Codebook::unittest() { ofstream file; file.open ("tmp.dat435", ios::out); file << 3 << endl; file << 3 << endl; file << 1 << endl; file << 0 << endl; file << 0 << endl; file << 0 << endl; file << 1 << endl; file << 0 << endl; file << 0 << endl; file << 0 << endl; file << 1 << endl; file.close(); readIn("tmp.dat435"); save("tmp.dat435"); codebook.clear(); inverseDocFreq.clear(); readIn("tmp.dat435"); vector<float> term = {.5,0,.1}; vector< tuple<int,float> > quan = quantizeSoft(term,1); assert(get<0>(quan[0])==0 && get<1>(quan[0])==1); vector<float> term2 = {.3,0,.3}; quan = quantizeSoft(term2,2); assert(get<1>(quan[0])==.5 && get<1>(quan[1])==.5); file.open ("tmp.dat435", ios::out); file << 4 << endl; file << 4 << endl; file << 1 << endl; file << 0 << endl; file << 0 << endl; file << 0 << endl; file << 0 << endl; file << 1 << endl; file << 0 << endl; file << 0 << endl; file << 0 << endl; file << 0 << endl; file << 1 << endl; file << 0 << endl; file << 0 << endl; file << 0 << endl; file << 0 << endl; file << 1 << endl; file.close(); codebook.clear(); inverseDocFreq.clear(); readIn("tmp.dat435"); vector<float> term3 = {.3,0,.2,.1}; quan = quantizeSoft(term3,3); double score0=0; double score1=0; double score2=0; double score3=0; for (auto p : quan) { if (get<0>(p) == 0) score0 = get<1>(p); else if (get<0>(p) == 1) score1 = get<1>(p); else if (get<0>(p) == 2) score2 = get<1>(p); else if (get<0>(p) == 3) score3 = get<1>(p); } assert(score0>score2); assert(score2>score3); assert(score3>score1); assert(score0+score1+score2+score3>=.9999 && score0+score1+score2+score3<=1.0001); double sumtest=0; for (const auto &v : quan) { sumtest+=get<1>(v); } assert(sumtest<1.0001 && sumtest>.9999); vector<float> term4 = {.1,.05,.3,.2}; quan = quantizeSoft(term4,4); score0=0; score1=0; score2=0; score3=0; for (auto p : quan) { if (get<0>(p) == 0) score0 = get<1>(p); else if (get<0>(p) == 1) score1 = get<1>(p); else if (get<0>(p) == 2) score2 = get<1>(p); else if (get<0>(p) == 3) score3 = get<1>(p); } assert(score2>score3); assert(score3>score0); assert(score0>score1); assert(score0+score1+score2+score3>=.9999 && score0+score1+score2+score3<=1.0001); sumtest=0; for (const auto &v : quan) { sumtest+=get<1>(v); } assert(sumtest<1.0001 && sumtest>.9999); vector<float> term5 = {.1,.05,.3,.1}; quan = quantizeSoft(term5,3); score0=0; score1=0; score2=0; score3=0; for (auto p : quan) { if (get<0>(p) == 0) score0 = get<1>(p); else if (get<0>(p) == 1) score1 = get<1>(p); else if (get<0>(p) == 2) score2 = get<1>(p); else if (get<0>(p) == 3) score3 = get<1>(p); } assert(score2>score3); assert(score3==score0); assert(score0>score1); assert(score0+score1+score2+score3>=.9999 && score0+score1+score2+score3<=1.0001); sumtest=0; for (const auto &v : quan) { sumtest+=get<1>(v); } assert(sumtest<1.0001 && sumtest>.9999); vector<float> term6 = {.05,.1,.2,.3}; quan = quantizeSoft(term6,4); score0=0; score1=0; score2=0; score3=0; for (auto p : quan) { if (get<0>(p) == 0) score0 = get<1>(p); else if (get<0>(p) == 1) score1 = get<1>(p); else if (get<0>(p) == 2) score2 = get<1>(p); else if (get<0>(p) == 3) score3 = get<1>(p); } assert(score3>score2); assert(score2>score1); assert(score1>score0); assert(score0+score1+score2+score3>=.9999 && score0+score1+score2+score3<=1.0001); sumtest=0; for (const auto &v : quan) { sumtest+=get<1>(v); } assert(sumtest<1.0001 && sumtest>.9999); vector<float> term7 = {.05,1,100,5000}; quan = quantizeSoft(term7,4); score0=0; score1=0; score2=0; score3=0; for (auto p : quan) { if (get<0>(p) == 0) score0 = get<1>(p); else if (get<0>(p) == 1) score1 = get<1>(p); else if (get<0>(p) == 2) score2 = get<1>(p); else if (get<0>(p) == 3) score3 = get<1>(p); } assert(score3>score2); assert(score2>=score1); //assert(score1>=score0); assert(score0+score1+score2+score3>=.9999 && score0+score1+score2+score3<=1.0001); sumtest=0; for (const auto &v : quan) { sumtest+=get<1>(v); } assert(sumtest<1.0001 && sumtest>.9999); vector<float> term8 = {.05,1,1000,5000}; quan = quantizeSoft(term8,4); score0=0; score1=0; score2=0; score3=0; for (auto p : quan) { if (get<0>(p) == 0) score0 = get<1>(p); else if (get<0>(p) == 1) score1 = get<1>(p); else if (get<0>(p) == 2) score2 = get<1>(p); else if (get<0>(p) == 3) score3 = get<1>(p); } assert(score3>score2); assert(score2>score1); //assert(score1>score0); assert(score0+score1+score2+score3>=.9999 && score0+score1+score2+score3<=1.0001); sumtest=0; for (const auto &v : quan) { sumtest+=get<1>(v); } assert(sumtest<1.0001 && sumtest>.9999); ////Test training of codebook//////// codebook.clear(); vector< vector<double> >accum(1000); default_random_engine generator; normal_distribution<double> distributionP(5.0,.10); normal_distribution<double> distributionN(2.0,.10); normal_distribution<double> distributionR(3.0,.10); for (int i=0; i<accum.size(); i++) { for (int j=0; j<5*2+2; j++) { if (i%5 == j%5 && j<10) accum[i].push_back(fabs(distributionP(generator))); else if (j<10) accum[i].push_back(fabs(distributionN(generator))); else accum[i].push_back(fabs(distributionR(generator))); } } for (int i=0; i<accum.size(); i++) { for (int j=0; j<accum[i].size(); j++) { assert(accum[i][j] != 0); } } trainFromExamples(5,accum); vector<bool> found(5); for (int i=0; i<size(); i++) { double hi=0; int index; double hi2=0; int index2; for (int j=0; j<codebook[i].size(); j++) { if (codebook[i][j] > hi) { hi2=hi; index2=index; hi=codebook[i][j]; index=j; } else if (codebook[i][j] > hi2) { hi2=codebook[i][j]; index2=j; } } assert (index%5 == index2%5); assert(!found[index%5]); found[index%5]=true; } for (int i=0; i<size(); i++) assert(found[i]); vector<float> term9 = {7,3,3.3,2,2,3,1.5,1.7,.6,1.5,.1,10}; quan = quantizeSoft(term9,1); assert(quan.size()==1 && get<1>(quan[0])==1 && get<0>(quan[0])==cw_0); vector<float> term10 = {7,3,3.3,2,10,7.1,2,2,2,12,.1,10}; quan = quantizeSoft(term10,3); score0=0; double score4=0; double scoreOther=0; for (auto p : quan) { if (get<0>(p) == cw_0) score0 = get<1>(p); else if (get<0>(p) == cw_4) score4 = get<1>(p); else scoreOther = get<1>(p); } assert(quan.size()==3 && score0!=0 && score4!=0 && scoreOther!=0); assert(score0>scoreOther); assert(score4>scoreOther); sumtest=0; for (const auto &v : quan) { sumtest+=get<1>(v); } assert(sumtest<1.0001 && sumtest>.9999); vector< vector<double> > copy(codebook.size()); for (int i=0; i<codebook.size(); i++) { for (int j=0; j<depth(); j++) copy[i].push_back(codebook[i][j]); } save("tmp.dat435"); codebook.clear(); inverseDocFreq.clear(); readIn("tmp.dat435"); for (int i=0; i<codebook.size(); i++) { for (int j=0; j<depth(); j++) assert(fabs(copy[i][j] - codebook[i][j])<.00001); } //double check vector<bool> found_doublecheck(5); for (int i=0; i<size(); i++) { double hi=0; int index; double hi2=0; int index2; for (int j=0; j<codebook[i].size(); j++) { if (codebook[i][j] > hi) { hi2=hi; index2=index; hi=codebook[i][j]; index=j; } else if (codebook[i][j] > hi2) { hi2=codebook[i][j]; index2=j; } } assert (index%5 == index2%5); assert(!found_doublecheck[index%5]); found_doublecheck[index%5]=true; } for (int i=0; i<size(); i++) assert(found_doublecheck[i]); // codebook.clear(); // for (double y=0.1; y<6; y+=2+.05*(int)y) // for (double x=0.1; x<5; x+=2+1.33*(int)y) // for (double z=0.1; z<5; z+=2) // { // vector<double> v= {x,y,z}; // push_back(v); // } // vector<double> t1= {0.1,.4,.2}; // int t1q=quantize(t1); //// assert(t1q==0); // vector<double> t2= {1,2,1}; // vector< tuple<int,float> > t2q = quantizeSoft(t2,2); //// assert(fabs(get<1>(t2q[0])-.5)<.001 && fabs(get<1>(t2q[1])-.5)<.001); // vector<double> t3= {1,3,4}; // vector< tuple<int,float> > t3q = quantizeSoft(t3,4); //// assert(fabs(get<1>(t3q[0])-.333)<.001 && fabs(get<1>(t3q[1])-.333)<.001 && fabs(get<1>(t3q[2])-.333)<.001); cout << "Codebook passed its tests!" << endl; }
void Vc4Shader::Emit_Mad(CInstruction &Inst) { assert(this->uShaderType == D3D10_SB_PIXEL_SHADER || this->uShaderType == D3D10_SB_VERTEX_SHADER); VC4_ASSERT(Inst.m_NumOperands == 4); { for (uint8_t i = 0, aCurrent = D3D10_SB_OPERAND_4_COMPONENT_MASK_X; i < 4; i++) { if (Inst.m_Operands[0].m_WriteMask & aCurrent) { Vc4Register accum(VC4_QPU_ALU_R3, VC4_QPU_WADDR_ACC3); Vc4Register dst = Find_Vc4Register_M(Inst.m_Operands[0], aCurrent); // perform mul first 2 operands { Vc4Register src[2]; this->Setup_SourceRegisters(Inst, 1, ARRAYSIZE(src), i, src); { Vc4Instruction Vc4Inst; Vc4Inst.Vc4_m_FMUL(accum, src[0], src[1]); Vc4Inst.Emit(CurrentStorage); } } Vc4Register _dst; if (dst.GetFlags().packed) { // pack has to be done at mul pipe, so result to r3, // then use mul pipe to move to final dst (with pack). _dst = accum; } else { _dst = dst; } // perform add with 3rd operand. { Vc4Register src[1]; this->Setup_SourceRegisters(Inst, 3, ARRAYSIZE(src), i, src); { Vc4Instruction Vc4Inst; Vc4Inst.Vc4_a_FADD(_dst, accum, src[0]); Vc4Inst.Emit(CurrentStorage); } } // move to destination (with packing). if (dst.GetFlags().packed) { Vc4Instruction Vc4Inst; Vc4Inst.Vc4_m_MOV(dst, accum); Vc4Inst.Vc4_m_Pack(dst.GetPack(i)); Vc4Inst.Emit(CurrentStorage); } } aCurrent <<= 1; } } { // Emit a NOP Vc4Instruction Vc4Inst; Vc4Inst.Emit(CurrentStorage); } }
cell factor_vm::instances(cell type) { object_accumulator accum(type); each_object(accum); return std_vector_to_array(accum.objects); }
void Vc4Shader::Emit_DPx(CInstruction &Inst) { assert(this->uShaderType == D3D10_SB_PIXEL_SHADER || this->uShaderType == D3D10_SB_VERTEX_SHADER); VC4_ASSERT(Inst.m_NumOperands == 3); { // DP2 loop 2 times. // DP3 loop 3 times. // DP4 loop 4 times. uint8_t c = (uint8_t)(Inst.m_OpCode - 13); // where to accumulate result of mul. Vc4Register accum(VC4_QPU_ALU_R3, VC4_QPU_WADDR_ACC3); { Vc4Register zero(VC4_QPU_ALU_REG_B, 0); // 0 as small immediate in raddr_b Vc4Instruction Vc4Inst(vc4_alu_small_immediate); Vc4Inst.Vc4_m_MOV(accum, zero); Vc4Inst.Emit(CurrentStorage); } for(uint8_t i = 0; i < c; i++) { Vc4Register temp(VC4_QPU_ALU_R1, VC4_QPU_WADDR_ACC1); Vc4Register src[2]; Setup_SourceRegisters(Inst, 1, ARRAYSIZE(src), i, src); { Vc4Instruction Vc4Inst; Vc4Inst.Vc4_m_FMUL(temp, src[0], src[1]); if (i > 0) { Vc4Inst.Vc4_a_FADD(accum, accum, temp); } Vc4Inst.Emit(CurrentStorage); } if (i+1 == c) { Vc4Instruction Vc4Inst; Vc4Inst.Vc4_a_FADD(accum, accum, temp); Vc4Inst.Emit(CurrentStorage); } } // replicate ouput where specified. for (uint8_t i = 0, aCurrent = D3D10_SB_OPERAND_4_COMPONENT_MASK_X; i < 4; i++) { if (Inst.m_Operands[0].m_WriteMask & aCurrent) { Vc4Register dst = Find_Vc4Register_M(Inst.m_Operands[0], aCurrent); Vc4Instruction Vc4Inst; Vc4Inst.Vc4_m_MOV(dst, accum); Vc4Inst.Vc4_m_Pack(dst.GetPack(i)); Vc4Inst.Emit(CurrentStorage); } aCurrent <<= 1; } } { // Emit a NOP Vc4Instruction Vc4Inst; Vc4Inst.Emit(CurrentStorage); } }