void BinaryMX<ScX, ScY>:: generate(CodeGenerator& g, const std::string& mem, const std::vector<int>& arg, const std::vector<int>& res) const { // Quick return if nothing to do if (nnz()==0) return; // Check if inplace bool inplace; switch (op_) { case OP_ADD: case OP_SUB: case OP_MUL: case OP_DIV: inplace = res[0]==arg[0]; break; default: inplace = false; break; } // Scalar names of arguments (start assuming all scalars) string r = g.workel(res[0]); string x = g.workel(arg[0]); string y = g.workel(arg[1]); // Codegen loop, if needed if (nnz()>1) { // Iterate over result g.local("rr", "real_t", "*"); g.local("i", "int"); g << "for (i=0, " << "rr=" << g.work(res[0], nnz()); r = "(*rr++)"; // Iterate over first argument? if (!ScX && !inplace) { g.local("cr", "const real_t", "*"); g << ", cr=" << g.work(arg[0], dep(0).nnz()); x = "(*cr++)"; } // Iterate over second argument? if (!ScY) { g.local("cs", "const real_t", "*"); g << ", cs=" << g.work(arg[1], dep(1).nnz()); y = "(*cs++)"; } // Close loop g << "; i<" << nnz() << "; ++i) "; } // Perform operation g << r << " "; if (inplace) { g << casadi_math<double>::sep(op_) << "= " << y; } else { g << " = " << casadi_math<double>::print(op_, x, y); } g << ";\n"; }
void DenseTranspose::generate(const std::vector<int>& arg, const std::vector<int>& res, CodeGenerator& g) const { g.body << " for (i=0, rr=" << g.work(res[0], nnz()) << ", " << "cs=" << g.work(arg[0], nnz()) << "; i<" << dep().size2() << "; ++i) " << "for (j=0; j<" << dep().size1() << "; ++j) " << "rr[i+j*" << dep().size2() << "] = *cs++;" << endl; }
void after_iteration(int iteration, graphchi_context &gcontext) { if (gcontext.iteration % 2 == 1) { for (int i=0; i< (int)adjcontainer->adjs.size(); i++) { if (debug) Rcpp::Rcerr<<"Going over user" << adjcontainer->adjs[i].vid << std::endl; dense_adj &user = adjcontainer->adjs[i]; if (nnz(user.edges) == 0 || nnz(user.ratings) == 0) { if (debug) Rcpp::Rcerr<<"User with no edges" << std::endl; continue; } //assert(user.ratings.size() == N); ivec positions = reverse_sort_index(user.ratings, K); assert(positions.size() > 0); for (int j=0; j < positions.size(); j++) { assert(positions[j] >= 0); assert(positions[j] < (int)N); //skip zero entries if (get_val(user.ratings, positions[j])== 0) { if (debug) Rcpp::Rcerr<<"Found zero in position " << j << std::endl; break; } int rc = fprintf(out_file, "%u %u %lg\n", user.vid+1, positions[j]+1, get_val(user.ratings, positions[j]));//write item similarity to file if (debug) Rcpp::Rcerr<<"Writing rating from user" << user.vid+1 << " to item: " << positions[j] << std::endl; assert(rc > 0); written_pairs++; } } grabbed_edges = 0; adjcontainer->clear(); } }
void Transpose::generate(const std::vector<int>& arg, const std::vector<int>& res, CodeGenerator& g) const { g.addAuxiliary(CodeGenerator::AUX_TRANS); g.body << " trans(" << g.work(arg[0], nnz()) << ", " << g.sparsity(dep().sparsity()) << ", " << g.work(res[0], nnz()) << ", " << g.sparsity(sparsity()) << ", iw);" << endl; }
void Assertion::generate(CodeGenerator& g, const std::string& mem, const std::vector<int>& arg, const std::vector<int>& res) const { // Generate assertion g.body << " if (" << g.workel(arg[1]) << "!=1.) {" << endl << " /* " << fail_message_ << " */" << endl << " return 1;" << endl << " }" << endl; // Copy if not inplace if (arg[0]!=res[0]) { g.body << " " << g.copy(g.work(arg[0], nnz()), nnz(), g.work(res[0], nnz())) << endl; } }
void BinaryMX<ScX, ScY>::evalGen(const T* const* arg, T* const* res, int* iw, T* w) { // Get data T* output0 = res[0]; const T* input0 = arg[0]; const T* input1 = arg[1]; if (!ScX && !ScY) { casadi_math<T>::fun(op_, input0, input1, output0, nnz()); } else if (ScX) { casadi_math<T>::fun(op_, *input0, input1, output0, nnz()); } else { casadi_math<T>::fun(op_, input0, *input1, output0, nnz()); } }
/* * This function returns a vector of length m+n with the number of nonzeros of the matrix A * where the first m elements corresponds to the row, while the other n to the cols. */ long* number_nonzeros(struct sparsematrix* A){ int m = A->m; int n = A->n; long* nzi = nnz(A->i,A->NrNzElts,m); long* nzj = nnz(A->j,A->NrNzElts,n); long* output = vecallocl(m+n); int i; for(i=0;i<m;i++) output[i] = nzi[i]; for(i=0;i<n;i++) output[m+i] = nzj[i]; vecfreel(nzi); vecfreel(nzj); return output; }
// Element access const_reference operator()(size_type i) const { SIZE_CHECK(i < m_size); std::size_t pos = lower_bound(i); if (pos == nnz() || m_indices[pos] != i) return m_zero; return m_values [pos]; }
void Split::generate(CodeGenerator& g, const std::string& mem, const std::vector<int>& arg, const std::vector<int>& res) const { int nx = nout(); for (int i=0; i<nx; ++i) { int nz_first = offset_[i]; int nz_last = offset_[i+1]; int nz = nz_last-nz_first; if (res[i]>=0 && nz>0) { // if anything to assign if (nz==1) { // assign scalar g.body << " " << g.workel(res[i]) << " = "; if (dep(0).nnz()==1) { // rhs is also scalar casadi_assert(nz_first==0); g.body << g.workel(arg[0]) << ";" << endl; } else { // rhs is an element in a vector g.body << g.work(arg[0], dep(0).nnz()) << "[" << nz_first << "];" << endl; } } else { // assign vector std::string r = g.work(arg[0], dep(0).nnz()); if (nz_first!=0) r = r + "+" + g.to_string(nz_first); g.body << " " << g.copy(r, nz, g.work(res[i], nnz(i))) << endl; } } } }
void printLongResults(const MatrixType& matrix, IterationType& iter, const SolverOptions& options) { const double elapsed = getTime() - startTime; std::cout.precision(5); std::cout.setf(std::ios::fixed); std::cout << "Library: desola" << std::endl; std::cout << "Matrix: " << getLeaf(options.getFile()) << std::endl; std::cout << "Matrix Size: " << num_cols(matrix) << std::endl; std::cout << "Compiler: " << getCompiler() << std::endl; std::cout << "Code Caching: " << getStatus(configManager.codeCachingEnabled()) << std::endl; std::cout << "Loop Fusion: " << getStatus(configManager.loopFusionEnabled()) << std::endl; std::cout << "Array Contraction: " << getStatus(configManager.arrayContractionEnabled()) << std::endl; std::cout << "Iterations: " << iter.iterations() << std::endl; std::cout << "Liveness Analysis: " << getStatus(configManager.livenessAnalysisEnabled()) << std::endl; std::cout << "Time per Iteration: " << elapsed / iter.iterations() << " seconds" << std::endl; std::cout << "Compile Time: " << statsCollector.getCompileTime() << " seconds" << std::endl; std::cout << "Compile Count: " << statsCollector.getCompileCount() << std::endl; std::cout << "Total Time: " << elapsed << " seconds" << std::endl; std::cout << "FLOPs: " << statsCollector.getFlops() << std::endl; std::cout << "High-Level Fusion: " << getStatus(configManager.highLevelFusionEnabled()) << std::endl; std::cout << "Single For-Loop Sparse Iteration: " << getStatus(configManager.singleForLoopSparseIterationEnabled()) << std::endl; std::cout << "Sparse Row Length Specialisation: " << getStatus(configManager.sparseSpecialisationEnabled()) << std::endl; if (options.useSparse()) std::cout << "NNZ: " << nnz(matrix) << std::endl; if (configManager.livenessAnalysisEnabled()) std::cout << std::endl << "Warning: FLOPs figure may be misleading with liveness analysis enabled." << std::endl; }
int main(){ V3f x(0,0,1); V3f xr(rot_x(x, 0.87)); same("x rotation", x.dot(xr), cos(0.87)); V3f y(0,0,1); V3f yr(rot_y(y, 0.23)); same("y rotation", y.dot(yr), cos(0.23)); V3f z(1,0,0); V3f zr(rot_z(z, 0.19)); same("z rotation", z.dot(zr), cos(0.19)); V3f nx(3,2,5); V3f ny(-2,3,4); V3f nz(-4,4,3.8); V3f nnx(3,2,5); V3f nny(-2,3,4); V3f nnz(-4,4,3.8); ortoNormalize(nnx, nny, nnz); same("x unit", nnx.length(), 1.0); same("y unit", nny.length(), 1.0); same("z unit", nnz.length(), 1.0); V3f tmp; tmp.cross(nnx, nx); same("x colinear", tmp.length(), 0.0); tmp.cross(nnx, nny); tmp-=nnz; same("x orto", tmp.length(), 0); tmp.cross(nny, nnz); tmp-=nnx; same("y orto", tmp.length(), 0); tmp.cross(nnz, nnx); tmp-=nny; same("z orto", tmp.length(), 0); };
void printShortResults(const MatrixType& matrix, IterationType& iter, const SolverOptions& options) { const double elapsed = getTime() - startTime; const char d = ':'; std::cout.precision(5); std::cout.setf(std::ios::fixed); std::cout << d; std::cout << "lib=desola" << d; std::cout << "mat=" << getLeaf(options.getFile()) << d; std::cout << "mat_n=" << num_cols(matrix) << d; std::cout << "compiler=" << getCompiler() << d; std::cout << "code_cache=" << getStatus(configManager.codeCachingEnabled()) << d; std::cout << "fusion=" << getStatus(configManager.loopFusionEnabled()) << d; std::cout << "contraction=" << getStatus(configManager.arrayContractionEnabled()) << d; std::cout << "liveness=" << getStatus(configManager.livenessAnalysisEnabled()) << d; std::cout << "iterations=" << iter.iterations() << d; std::cout << "compile_time=" << statsCollector.getCompileTime() << d; std::cout << "compile_count=" << statsCollector.getCompileCount() << d; std::cout << "total_time=" << elapsed << d; std::cout << "flop=" << statsCollector.getFlops() << d; std::cout << "high_level_fusion=" << getStatus(configManager.highLevelFusionEnabled()) << d; std::cout << "single_for_loop_sparse=" << getStatus(configManager.singleForLoopSparseIterationEnabled()) << d; std::cout << "specialise_sparse=" << getStatus(configManager.sparseSpecialisationEnabled()) << d; if (options.useSparse()) std::cout << "nnz=" << nnz(matrix) << d; std::cout << std::endl; }
void Assertion::eval(const double** arg, double** res, int* iw, double* w, int mem) const { if (arg[1][0]!=1) { casadi_error("Assertion error: " << fail_message_); } if (arg[0]!=res[0]) { copy(arg[0], arg[0]+nnz(), res[0]); } }
int SquareMatrix::getRowEnds(int row){ if (rowStarts[row-1]==-1) return -2; // starts[row-1+1] = starts[row]! while (row < N && rowStarts[row]==-1) { ++row; } return (row==N) ? nnz()-1 : rowStarts[row]-1; }
void SparseMatrix::print(ostream& out) const { out << "%triples: (" << _num_rows << "x" << _num_cols << ", nnz:" << nnz() << ")" << endl; out.precision(12); for (int row=0; row<_num_rows; row++) { for (SparseVectorIter iter(*_rows[row]); iter.valid(); iter.next()) { double val; int col = iter.get(val); out << row << " " << col << " " << val << endl; } } }
/** * method that returns a logical vector of length m+n * 1 if the row/column is split, 0 otherwise * * input = A,B (respectively the two partitioned parts, A1 A2) * of size mxn */ long* cut_vector(struct sparsematrix* A, struct sparsematrix* B){ int m = A->m; int n = A->n; long* nnzAi = nnz(A->i,A->NrNzElts,m); long* nnzAj = nnz(A->j,A->NrNzElts,n); long* nnzBi = nnz(B->i,B->NrNzElts,m); long* nnzBj = nnz(B->j,B->NrNzElts,n); long* cut= vecallocl(m+n); int i; for(i=0;i<m;i++) cut[i] = nnzAi[i] && nnzBi[i]; for(i=0;i<n;i++) cut[m+i] = nnzAj[i] && nnzBj[i]; vecfreel(nnzAi); vecfreel(nnzAj); vecfreel(nnzBi); vecfreel(nnzBj); return cut; }
void Assertion::spAdj(bvec_t** arg, bvec_t** res, int* iw, bvec_t* w, int mem) { bvec_t *a = arg[0]; bvec_t *r = res[0]; int n = nnz(); if (a != r) { for (int i=0; i<n; ++i) { *a++ |= *r; *r++ = 0; } } }
void clear() { for(std::vector<dense_adj>::iterator it=adjs.begin(); it != adjs.end(); ++it) { if (nnz(it->edges)) { it->edges.resize(0); } } adjs.clear(); if (debug) std::cout<<"setting pivot end to " << pivot_en << std::endl; pivot_st = pivot_en; }
/* * Computes density for a directed graph. Connection weights are ignored. */ FP_T BCT_NAMESPACE::density_dir(const MATRIX_T* CIJ) { if (safe_mode) check_status(CIJ, SQUARE | DIRECTED, "density_dir"); // N = size(CIJ,1); int N = CIJ->size1; // K = nnz(CIJ); int K = nnz(CIJ); // kden = K/(N^2-N); return (FP_T)K / (FP_T)(N * (N - 1)); }
void printLongResults(const MatrixType& matrix, IterationType& iter, const SolverOptions& options) { const double elapsed = getTime() - startTime; std::cout.precision(5); std::cout.setf(std::ios::fixed); std::cout << "Library: MTL" << std::endl; std::cout << "Matrix: " << getLeaf(options.getFile()) << std::endl; std::cout << "Matrix Size: " << num_cols(matrix) << std::endl; std::cout << "Iterations: " << iter.iterations() << std::endl; std::cout << "Time per Iteration: " << elapsed / iter.iterations() << " seconds" << std::endl; std::cout << "Total Time: " << elapsed << " seconds" << std::endl; if (options.useSparse()) std::cout << "NNZ: " << nnz(matrix) << std::endl; }
void BinaryMX<ScX, ScY>:: sp_fwd(const bvec_t** arg, bvec_t** res, int* iw, bvec_t* w, int mem) const { const bvec_t *a0=arg[0], *a1=arg[1]; bvec_t *r=res[0]; int n=nnz(); for (int i=0; i<n; ++i) { if (ScX && ScY) *r++ = *a0 | *a1; else if (ScX && !ScY) *r++ = *a0 | *a1++; else if (!ScX && ScY) *r++ = *a0++ | *a1; else *r++ = *a0++ | *a1++; } }
/* * Computes degree for an undirected graph. Connection weights are ignored. */ VECTOR_T* BCT_NAMESPACE::degrees_und(const MATRIX_T* CIJ) { if (safe_mode) check_status(CIJ, SQUARE | UNDIRECTED, "degrees_und"); // CIJ = double(CIJ~=0); // deg = sum(CIJ); VECTOR_T* deg = VECTOR_ID(alloc)(CIJ->size2); #ifdef _OPENMP #pragma omp parallel for shared(deg) #endif for (int i = 0; i < (int)CIJ->size2; i++) { VECTOR_ID(const_view) CIJ_col_i = MATRIX_ID(const_column)(CIJ, i); VECTOR_ID(set)(deg, i, nnz(&CIJ_col_i.vector)); } return deg; }
void BinaryMX<ScX, ScY>:: sp_rev(bvec_t** arg, bvec_t** res, int* iw, bvec_t* w, int mem) const { bvec_t *a0=arg[0], *a1=arg[1], *r = res[0]; int n=nnz(); for (int i=0; i<n; ++i) { bvec_t s = *r; *r++ = 0; if (ScX) *a0 |= s; else *a0++ |= s; if (ScY) *a1 |= s; else *a1++ |= s; } }
void CSparseCholeskyInterface::factorize(void* mem, const double* A) const { auto m = static_cast<CsparseCholMemory*>(mem); // Set the nonzeros of the matrix m->A.x = const_cast<double*>(A); // Make sure that all entries of the linear system are valid int nnz = m->nnz(); for (int k=0; k<nnz; ++k) { casadi_assert_message(!isnan(A[k]), "Nonzero " << k << " is not-a-number"); casadi_assert_message(!isinf(A[k]), "Nonzero " << k << " is infinite"); } if (m->L) cs_nfree(m->L); m->L = cs_chol(&m->A, m->S) ; // numeric Cholesky factorization casadi_assert(m->L!=0); }
void Transpose::spFwd(const bvec_t** arg, bvec_t** res, int* iw, bvec_t* w) { // Shortands const bvec_t *x = arg[0]; bvec_t *xT = res[0]; // Get sparsity int nz = nnz(); const int* x_row = dep().row(); const int* xT_colind = sparsity().colind(); int xT_ncol = sparsity().size2(); // Loop over the nonzeros of the argument copy(xT_colind, xT_colind+xT_ncol+1, iw); for (int el=0; el<nz; ++el) { xT[iw[*x_row++]++] = *x++; } }
void CSparseCholeskyInterface::reset(void* mem, const int* sp) const { LinsolInternal::reset(mem, sp); auto m = static_cast<CsparseCholMemory*>(mem); m->L = 0; m->S = 0; m->A.nzmax = m->nnz(); // maximum number of entries m->A.m = m->nrow(); // number of columns m->A.n = m->ncol(); // number of rows m->A.p = const_cast<int*>(m->colind()); // row pointers (size n+1) // or row indices (size nzmax) m->A.i = const_cast<int*>(m->row()); // column indices, size nzmax m->A.x = 0; // numerical values, size nzmax m->A.nz = -1; // of entries in triplet matrix, -1 for compressed-row // Temporary m->temp.resize(m->A.n); }
void printShortResults(const MatrixType& matrix, IterationType& iter, const SolverOptions& options) { const double elapsed = getTime() - startTime; const char d = ':'; std::cout.precision(5); std::cout.setf(std::ios::fixed); std::cout << d; std::cout << "lib=mtl" << d; std::cout << "mat=" << getLeaf(options.getFile()) << d; std::cout << "mat_n=" << num_cols(matrix) << d; std::cout << "iterations=" << iter.iterations() << d; std::cout << "total_time=" << elapsed << d; if (options.useSparse()) std::cout << "nnz=" << nnz(matrix) << d; std::cout << std::endl; }
static float Objective (const Matrix<cxfl>& ffdbx, const Matrix<cxfl>& ffdbg, const Matrix<cxfl>& ttdbx, const Matrix<cxfl>& ttdbg, const Matrix<cxfl>& x, const Matrix<cxfl>& g, const Matrix<cxfl>& data, const float t, float& rmse, const CSParam& cgp) { float obj; float nz = (float) nnz (data); obj = Obj (ffdbx, ffdbg, data, t); rmse = sqrt(obj/nz); if (cgp.tvw) obj += ObjTV (ttdbx, ttdbg, t, cgp); if (cgp.xfmw) obj += ObjXFM (x, g, t, cgp); return obj; }
std::size_t lower_bound( index_type t)const { index_type const* begin = indices(); index_type const* end = indices()+nnz(); return std::lower_bound(begin, end, t)-begin; }
iterator end() { return iterator(values(),indices(),nnz()); }