#include #include #include // #define USE_GENERALIZED_VECTOR_OF_VECTOR // Boost #include #include #include #include #include #include #include #include using namespace boost::numeric::ublas; typedef generalized_vector_of_vector< double, row_major, vector > > XType; /** * Compute v * M */ // vector ourProd(const vector& v, const XType& M){ // vector r(M.size2()); // const vector >& data = M.data(); // for(size_t i=0; i < M.size1(); ++i){ // r += v(i) * data(i); // } // return r; // } template RType ourProd(const VType& v, const MType& M){ RType r(M.size2()); std::cout << "M: " << M.size1() << "x" << M.size2() << std::endl; r = v(0) * matrix_row(M,0); for(size_t i=1; i < M.size1(); ++i){ matrix_row mr(M,i); r += v(i) * mr; } return r; } template void fill_ublas(const size_t row, const size_t col, const char* data, M& m){ for(size_t i = 0; i 0){ m(i,j) = value; } } // col }// row } template void test_ublas(const size_t row, const size_t col, const char* data, const unsigned int maxloop){ MType X(row,col); fill_ublas(row, col, data, X); vector w(X.size2()); for(size_t i=0; i result; const clock_t rightMultStartTime = clock(); for(unsigned int loop=0; loop l(row); for(size_t i=0; i ourGrad; const clock_t leftMultStartTime = clock(); for(unsigned int i=0; i, MType, vector >(l,X); } const clock_t leftMultEndTime = clock(); const double our_l1norm = norm_1(ourGrad); assert(l.size() == row); assert(ourGrad.size() == col); assert(X.size1() == row); assert(X.size2() == col); vector uBLASGrad; const clock_t uBlasLeftMultStart = clock(); for(unsigned int i=0; ilen == 0) return res; assert(svec->len > 0); for(register int i=0; ilen; i++) res += array[svec->fidx[i]] * svec->fval[i]; return res; } // add with scaled sparsevec void add_and_scale(const sparsevec *svec, const double& weight) { assert(len > 0); assert(svec->len > 0); assert(svec->fidx[svec->len-1] <= len); for(register int i=0; ilen; i++) array[svec->fidx[i]] += weight * svec->fval[i]; } // compute l1 norm double l1norm() { double res = 0; for(int i=0; i& m) { int actual_len = 0; sparsevec sp(col); for(size_t i=0; i 0) { sp.fidx[actual_len] = j; sp.fval[actual_len] = (double) data[i*col+j]; actual_len++; } } m.push_back(new sparsevec(actual_len, sp)); } } void test_fast(const size_t row, const size_t col, const char* data, const unsigned int maxloop) { std::vector X; fill_fast(row, col, data, X); densevec w(col); for(size_t i=0; i result(row); clock_t rightMultStartTime = clock(); for(unsigned int loop=0; loop l(row); for(size_t i=0; i >(row, col, data, maxLoop); printf("\n\n--------------------------\n"); printf("ublas compressed_matrix\n"); printf("--------------------------\n"); test_ublas >(row, col, data, maxLoop); printf("\n\n--------------------------\n"); printf("fast\n"); printf("--------------------------\n"); test_fast(row, col, data, maxLoop); printf("-----------------------------------------------------------\n\n\n"); return 0; }