long argmax(E&& expr) { auto arr = asarray(expr); long sz = arr.size(); if(not sz) throw types::ValueError("empty sequence"); return std::max_element(arr.fbegin(), arr.fend()) - arr.fbegin(); }
types::ndarray< typename __combined<typename types::numpy_expr_to_ndarray<E>::T, typename types::numpy_expr_to_ndarray<F>::T>::type, 1> intersect1d(E const &e, F const &f) { using T = typename __combined< typename types::numpy_expr_to_ndarray<E>::T, typename types::numpy_expr_to_ndarray<F>::T>::type; auto ae = asarray(e); auto af = asarray(f); std::set<T> sae(ae.fbegin(), ae.fend()); std::set<T> found; types::list<T> lout(0); lout.reserve(sae.size()); for (auto iter = af.fbegin(), end = af.fend(); iter != end; ++iter) { auto curr = *iter; if (sae.find(curr) != sae.end() and found.find(curr) == found.end()) { found.insert(curr); lout.push_back(curr); } } std::sort(lout.begin(), lout.end()); return types::ndarray<T, 1>(lout); }
void GlobObj::dumpSql(Sql *db, ostream &of) { // First define all functions for (const_fmap_iterator_type i = fbegin(); i != fend(); i++) { Call *fun = i->second; Tokid t = fun->get_site(); of << "INSERT INTO FUNCTIONS VALUES(" << ptr_offset(fun) << ", '" << fun->name << "', " << db->boolval(fun->is_macro()) << ',' << db->boolval(fun->is_defined()) << ',' << db->boolval(fun->is_declared()) << ',' << db->boolval(fun->is_file_scoped()) << ',' << t.get_fileid().get_id() << ',' << (unsigned)(t.get_streampos()) << ',' << fun->get_num_caller(); of << ");\n"; if (fun->is_defined()) { of << "INSERT INTO FUNCTIONMETRICS VALUES(" << ptr_offset(fun); for (int j = 0; j < FunMetrics::metric_max; j++) if (!Metrics::is_internal<FunMetrics>(j)) cout << ',' << fun->metrics().get_metric(j); of << ',' << fun->get_begin().get_tokid().get_fileid().get_id() << ',' << (unsigned)(fun->get_begin().get_tokid().get_streampos()) << ',' << fun->get_end().get_tokid().get_fileid().get_id() << ',' << (unsigned)(fun->get_end().get_tokid().get_streampos()); of << ");\n"; } int start = 0, ord = 0; for (dequeTpart::const_iterator j = fun->get_token().get_parts_begin(); j != fun->get_token().get_parts_end(); j++) { Tokid t2 = j->get_tokid(); int len = j->get_len() - start; int pos = 0; while (pos < len) { Eclass *ec = t2.get_ec(); of << "INSERT INTO FUNCTIONID VALUES(" << ptr_offset(fun) << ',' << ord << ',' << ptr_offset(ec) << ");\n"; pos += ec->get_len(); t2 += ec->get_len(); ord++; } start += j->get_len(); } } // Then their calls to satisfy integrity constraints for (const_fmap_iterator_type i = fbegin(); i != fend(); i++) { Call *fun = i->second; for (Call::const_fiterator_type dest = fun->call_begin(); dest != fun->call_end(); dest++) of << "INSERT INTO FCALLS VALUES(" << ptr_offset(fun) << ',' << ptr_offset(*dest) << ");\n"; } }
types::ndarray<typename types::numpy_expr_to_ndarray<E>::T, 1> ediff1d(E const& expr) { auto arr = asarray(expr); long n = arr.size() -1 ; types::ndarray<typename types::numpy_expr_to_ndarray<E>::T, 1> out(types::make_tuple(n), __builtin__::None); // Compute adjacent difference except for the first element std::adjacent_difference (arr.fbegin() + 1, arr.fend(), out.fbegin()); // First element can be done now (*out.fbegin()) = *(arr.fbegin()+1) - *(arr.fbegin()); return out; }
types::none_type putmask(types::ndarray<T, pS> &expr, E const &mask, F const &values) { auto amask = asarray(mask); auto avalues = asarray(values); auto iexpr = expr.fbegin(); auto n = avalues.flat_size(); for (long i = 0; i < expr.flat_size(); ++i) if (*(amask.fbegin() + i)) *(iexpr + i) = *(avalues.fbegin() + i % n); return __builtin__::None; }
types::ndarray<typename __combined<typename types::dtype_of<T>::type, typename types::dtype_of<U>::type>::type, types::pshape<long>> setdiff1d(T const &ar1, U const &ar2, bool assume_unique) { using dtype = typename __combined<typename types::dtype_of<T>::type, typename types::dtype_of<U>::type>::type; auto far1 = numpy::functor::array{}(ar1); auto far2 = numpy::functor::array{}(ar2); if (assume_unique) { std::sort(far1.fbegin(), far1.fend()); std::sort(far2.fbegin(), far2.fend()); dtype *out = (dtype *)malloc(far1.flat_size() * far2.flat_size() * sizeof(dtype)); dtype *out_last = std::set_difference(far1.fbegin(), far1.fend(), far2.fbegin(), far2.fend(), out); auto size = out_last - out; out = (dtype *)realloc(out, size * sizeof(dtype)); return {out, types::pshape<long>(size), types::ownership::owned}; } else { std::sort(far1.fbegin(), far1.fend()); std::sort(far2.fbegin(), far2.fend()); dtype *out = (dtype *)malloc(far1.flat_size() * far2.flat_size() * sizeof(dtype)); dtype *out_last = impl::set_difference_unique( far1.fbegin(), far1.fend(), far2.fbegin(), far2.fend(), out); auto size = out_last - out; out = (dtype *)realloc(out, size * sizeof(dtype)); return {out, types::pshape<long>(size), types::ownership::owned}; } }
typename std::enable_if<types::is_numexpr_arg<F>::value, types::none_type>::type put(types::ndarray<T, N> &expr, F const &ind, E const &v) { auto vind = asarray(ind); auto vv = asarray(v); for (long i = 0; i < ind.flat_size(); ++i) { auto val = *(vind.fbegin() + i); if (val >= expr.flat_size() || val < 0) throw types::ValueError("indice out of bound"); *(expr.fbegin() + val) = *(vv.fbegin() + i % vv.flat_size()); } return __builtin__::None; }
types::ndarray< long, 1 > digitize(E const& expr, F const& b) { auto bins = asarray(b); bool is_increasing = bins.size() > 1 and *bins.fbegin() < *(bins.fbegin() +1); types::ndarray<long, 1> out(types::make_tuple(long(expr.size())), __builtin__::None); auto out_iter = out.fbegin(); if(is_increasing) { _digitize(expr.begin(), expr.end(), out_iter, bins, operator_::proxy::lt(), utils::int_<types::numpy_expr_to_ndarray<E>::N>()); } else { _digitize(expr.begin(), expr.end(), out_iter, bins, operator_::proxy::gt(), utils::int_<types::numpy_expr_to_ndarray<E>::N>()); } return out; }
inline float const* fend(array const& a) { if(a.dtype() != dtype_t::float_) { throw std::runtime_error( "fend is called but dtype is not float"); } return fbegin(a) + total_size(a); }
typename std::enable_if<types::has_shape<U>::value and types::has_shape<V>::value,bool>::type array_equiv(U const& u, V const&v) { if(u.shape == v.shape) { return array_equal(u,v); } else if(u.size() > v.size()) { return array_equiv(v,u); } else if(v.size()%u.size() ==0) { auto uu = asarray(u); auto vv = asarray(v); for(auto vi = vv.fbegin(), ve = vv.fend(); vi != ve;) for(auto ui = uu.fbegin(), ue = uu.fend(); ui != ue; ++ui, ++vi) if(*ui != *vi) return false; return true; } return false; }
types::ndarray<typename E::dtype, E::value> diff(E const &expr, long n) { auto arr = asarray(expr); auto shape = expr.shape(); --shape[E::value - 1]; types::ndarray<typename E::dtype, E::value> out(shape, __builtin__::None); auto slice = expr.shape()[E::value - 1]; auto iter = arr.fbegin(); auto out_iter = out.fbegin(); for (long i = 0, sz = expr.flat_size(); i < sz; i += slice) { auto prev = *(iter + i); for (long k = 1; k < slice; ++k, ++out_iter) { auto nprev = *(iter + i + k); *(out_iter) = nprev - prev; prev = nprev; } } if (n == 1) return out; else return diff( out, n - 1); // TODO: inplace modification to avoid n-1 allocations }
//---------- void Mesh2DFromGraycode::triangulate() { this->throwIfMissingAnyConnection(); auto dataSet = this->getInput<Scan::Graycode>()->getDataSet(); if (!dataSet.getHasData()) { throw(Exception("No scan data available")); } //get camera coords in projector space map const auto & cameraInProjector = dataSet.getDataInverse(); const auto & active = dataSet.getActive(); const auto projectorWidth = cameraInProjector.getWidth(); const auto projectorHeight = cameraInProjector.getHeight(); const auto cameraWidth = active.getWidth(); auto getCameraPixelPosition = [&cameraInProjector, projectorWidth, cameraWidth](int i, int j) { const auto cameraPixelIndex = cameraInProjector.getData()[i + j * projectorWidth]; return ofVec2f(cameraPixelIndex % cameraWidth, cameraPixelIndex / cameraWidth); }; auto isActive = [& cameraInProjector, &active, projectorWidth](int i, int j) { const auto cameraPixelIndex = cameraInProjector.getData()[i + j * projectorWidth]; const auto isActive = active.getData()[cameraPixelIndex]; return isActive; }; vector<ofPoint> projectorSpaceActivePoints; //find all active pixels for (int j = 0; j < projectorHeight; j++) { for (int i = 0; i < projectorWidth; i++) { if (isActive(i, j)) { projectorSpaceActivePoints.emplace_back(ofVec2f(i, j)); } } } //triangulate ofMesh mesh; { //make vertices and tex coords Delaunay::Point tempP; vector<Delaunay::Point> delauneyPoints; for (const auto & projectorSpaceActivePoint : projectorSpaceActivePoints) { delauneyPoints.emplace_back(projectorSpaceActivePoint.x, projectorSpaceActivePoint.y); mesh.addVertex(projectorSpaceActivePoint); mesh.addTexCoord(getCameraPixelPosition(projectorSpaceActivePoint.x, projectorSpaceActivePoint.y)); } //triangulate auto delauney = make_shared<Delaunay>(delauneyPoints); delauney->Triangulate(); //apply indices for (auto it = delauney->fbegin(); it != delauney->fend(); ++it) { mesh.addIndex(delauney->Org(it)); mesh.addIndex(delauney->Dest(it)); mesh.addIndex(delauney->Apex(it)); } } swap(this->getInput<Data::Mesh>()->getMesh(), mesh); }
ViewVertex *ViewMap::InsertViewVertex(SVertex *iVertex, vector<ViewEdge*>& newViewEdges) { NonTVertex *vva = dynamic_cast<NonTVertex*>(iVertex->viewvertex()); if (vva) return vva; // because it is not already a ViewVertex, this SVertex must have only 2 FEdges. The incoming one still belongs // to ioEdge, the outgoing one now belongs to newVEdge const vector<FEdge *>& fedges = iVertex->fedges(); if (fedges.size() != 2) { cerr << "ViewMap warning: Can't split the ViewEdge" << endl; return NULL; } FEdge *fend(NULL), *fbegin(NULL); for (vector<FEdge *>::const_iterator fe = fedges.begin(), feend = fedges.end(); fe != feend; ++fe) { if ((*fe)->vertexB() == iVertex) { fend = (*fe); } if ((*fe)->vertexA() == iVertex) { fbegin = (*fe); } if ((fbegin != NULL) && (fend != NULL)) break; } ViewEdge *ioEdge = fbegin->viewedge(); ViewShape *vshape = ioEdge->viewShape(); vva = new NonTVertex(iVertex); // if the ViewEdge is a closed loop, we don't create a new VEdge if (ioEdge->A() == 0) { // closed loop ioEdge->setA(vva); ioEdge->setB(vva); // update sshape vshape->sshape()->RemoveEdgeFromChain(ioEdge->fedgeA()); vshape->sshape()->RemoveEdgeFromChain(ioEdge->fedgeB()); ioEdge->setFEdgeA(fbegin); ioEdge->setFEdgeB(fend); // Update FEdges fend->setNextEdge(NULL); fbegin->setPreviousEdge(NULL); // update new View Vertex: vva->AddOutgoingViewEdge(ioEdge); vva->AddIncomingViewEdge(ioEdge); vshape->sshape()->AddChain(ioEdge->fedgeA()); vshape->sshape()->AddChain(ioEdge->fedgeB()); } else { // Create new ViewEdge ViewEdge *newVEdge = new ViewEdge(vva, ioEdge->B(), fbegin, ioEdge->fedgeB(), vshape); newVEdge->setId(Id(ioEdge->getId().getFirst(), ioEdge->getId().getSecond() + 1)); newVEdge->setNature(ioEdge->getNature()); //newVEdge->UpdateFEdges(); // done in the ViewEdge constructor // Update old ViewEdge ioEdge->setB(vva); ioEdge->setFEdgeB(fend); // Update FEdges fend->setNextEdge(NULL); fbegin->setPreviousEdge(NULL); // update new View Vertex: vva->AddOutgoingViewEdge(newVEdge); vva->AddIncomingViewEdge(ioEdge); NonTVertex *vvb = dynamic_cast<NonTVertex*>(newVEdge->B()); if (vvb) vvb->Replace(ioEdge, newVEdge); // update ViewShape //vshape->AddEdge(newVEdge); // update SShape vshape->sshape()->AddChain(fbegin); // update ViewMap //_VEdges.push_back(newVEdge); newViewEdges.push_back(newVEdge); } // update ViewShape vshape->AddVertex(vva); // update ViewMap _VVertices.push_back(vva); return vva; }
inline float* fbegin(array& a) { return const_cast<float*>(fbegin(const_cast<array const&>(a))); }
inline auto make_parameter_memory_table( onnx::GraphProto const& graph, std::unordered_map<std::string, instant::array> const& parameter_table, mkldnn::engine const& engine) { std::unordered_map<std::string, const mkldnn::memory> memory_table; std::vector<array> temp_array_list; for(auto const& node : graph.node()) { if(node.op_type() == "Conv") { constexpr auto weight_index = 1; memory_table.insert(make_parameter_memory_pair( node, weight_index, mkldnn::memory::format::oihw, parameter_table, engine)); if(node.input_size() != 2) { constexpr auto bias_index = 2; memory_table.insert(make_parameter_memory_pair( node, bias_index, mkldnn::memory::format::x, parameter_table, engine)); } } else if(node.op_type() == "FC") { constexpr auto weight_index = 1; constexpr auto bias_index = 2; memory_table.insert(make_parameter_memory_pair( node, weight_index, mkldnn::memory::format::oi, // MEMO: is it correct? result is // correct... parameter_table, engine)); memory_table.insert(make_parameter_memory_pair( node, bias_index, mkldnn::memory::format::x, parameter_table, engine)); } else if(node.op_type() == "BatchNormalization") { constexpr auto scale_index = 1; constexpr auto b_index = 2; constexpr auto mean_index = 3; constexpr auto var_index = 4; auto const& scale_name = node.input(scale_index); auto const& scale_arr = find_value(parameter_table, scale_name); auto const& b_name = node.input(b_index); auto const& b_arr = find_value(parameter_table, b_name); mkldnn::memory::dims scale_dims(scale_arr.dims().begin(), scale_arr.dims().end()); std::vector<int> weights_dims{{2}}; weights_dims.insert(weights_dims.end(), scale_dims.begin(), scale_dims.end()); array weights_arr(dtype_t::float_, weights_dims); std::copy(fbegin(scale_arr), fend(scale_arr), fbegin(weights_arr)); std::copy(fbegin(b_arr), fend(b_arr), fbegin(weights_arr) + calc_total_size(scale_dims)); temp_array_list.push_back(weights_arr); auto weights_mem = mkldnn::memory({{{weights_dims}, mkldnn::memory::data_type::f32, mkldnn::memory::format::nc}, engine}, weights_arr.data()); memory_table.insert({scale_name, weights_mem}); /* memory_table.insert(make_parameter_memory_pair( node, scale_index, mkldnn::memory::format::x, parameter_table, engine)); memory_table.insert(make_parameter_memory_pair( node, b_index, mkldnn::memory::format::x, parameter_table, engine)); */ memory_table.insert(make_parameter_memory_pair( node, mean_index, mkldnn::memory::format::x, parameter_table, engine)); memory_table.insert(make_parameter_memory_pair( node, var_index, mkldnn::memory::format::x, parameter_table, engine)); } else { // TODO /* throw std::runtime_error("Not implemented yet: " + node.op_type()); */ } } return std::make_tuple(memory_table, temp_array_list); } // namespace instant