Пример #1
0
double luT(
	viennacl::matrix<T> &vclX,
	viennacl::vector_base<T> &vclD) {

	double logdet=-99.9;

	viennacl::linalg::lu_factorize(vclX);

	// pointer to the actual diagonal
	viennacl::vector_base<T> diagOfVar(
		vclX.handle(), vclX.size1(), 0, 
		vclX.internal_size2() + 1);

		// compute log determinant
	vclD = viennacl::linalg::element_log(diagOfVar);
	logdet = viennacl::linalg::sum(vclD);
// OPERATION_UNARY_LOG_TYPE 	
		//http://viennacl.sourceforge.net/doc/scheduler_8cpp-example.html#a11

		// put the diagonals in D, and 1's on the diagonal of L
	vclD = diagOfVar;

		//diagOfVar = T(1); // problem here
	
	return(logdet);
}
Пример #2
0
void init_random(viennacl::matrix<T, F> & M)
{
    std::vector<T> cM(M.internal_size());
    for (std::size_t i = 0; i < M.size1(); ++i)
        for (std::size_t j = 0; j < M.size2(); ++j)
            cM[F::mem_index(i, j, M.internal_size1(), M.internal_size2())] = T(rand())/T(RAND_MAX);
    viennacl::fast_copy(&cM[0],&cM[0] + cM.size(),M);
}
Пример #3
0
void matrix_print(viennacl::matrix<ScalarType, MatrixLayout>& A)
{
    for (unsigned int i = 0; i < A.size1(); i++) {
        for (unsigned int j = 0; j < A.size2(); j++)
           std::cout << std::fixed << A(i, j) << "\t";
        std::cout << "\n";
    }
}
Пример #4
0
void fill_matrix(viennacl::matrix<NumericT> & mat)
{
  for (std::size_t i = 0; i < mat.size1(); ++i)
  {
    for (std::size_t j = 0; j < mat.size2(); ++j)
      mat(i, j) = static_cast<NumericT>(-0.5) * random<NumericT>();
    mat(i, i) = NumericT(1.0) + NumericT(2.0) * random<NumericT>(); //some extra weight on diagonal for stability
  }
}
Пример #5
0
void matrix_print(viennacl::matrix<ScalarType>& A_orig)
{
    ublas::matrix<ScalarType> A(A_orig.size1(), A_orig.size2());
    viennacl::copy(A_orig, A);
    std::cout << "matrix_print!\n";
    for (unsigned int i = 0; i < A.size1(); i++) {
        for (unsigned int j = 0; j < A.size2(); j++)
           std::cout << A(i, j) << "\t";
        std::cout << "\n";
    }
}
Пример #6
0
 viennacl::vector_range<viennacl::vector_base<T> > sharedCol(){
     // viennacl::vector_base<T> tmp(ptr_matrix->handle(), ptr_matrix->internal_size(), 0, 1);
     
     // std::cout << "returning column" << std::endl;
     viennacl::vector_base<T> tmp(ptr_matrix->handle(), ptr_matrix->size1(), begin, ptr_matrix->internal_size2());
     // std::cout << "got column" << std::endl;
     
     viennacl::vector_range<viennacl::vector_base<T> > v_sub(tmp, r);
     // std::cout << "got range" << std::endl;
     
     return v_sub;
 }
Пример #7
0
bool test_eigen_val_vec(viennacl::matrix<ScalarType, MatrixLayout> & Q,
                      std::vector<ScalarType> & eigenvalues,
                      std::vector<ScalarType> & d,
                      std::vector<ScalarType> & e)
{
  unsigned int Q_size = Q.size2();
  ScalarType value = 0;

  for(unsigned int j = 0; j < Q_size; j++)
  {
    // calculate first row
    value = (d[0]- eigenvalues[j]) * Q(0, j) + e[1] * Q(1, j);
    if(value > EPS)
      return false;

    // calcuate inner rows
    for(unsigned int i = 1; i < Q_size - 1; i++)
    {
        value = e[i] * Q(i - 1, j) + (d[i]- eigenvalues[j]) * Q(i, j) + e[i + 1] * Q(i + 1, j);
        if(value > EPS)
          return false;
    }

    // calculate last row
    value = e[Q_size - 1] * Q(Q_size - 2, j) + (d[Q_size - 1] - eigenvalues[j]) * Q(Q_size - 1, j);
     if(value > EPS)
       return false;
  }
  return true;
}
Пример #8
0
bool check_tridiag(viennacl::matrix<ScalarType, MatrixLayout>& A_orig)
{
    ublas::matrix<ScalarType> A(A_orig.size1(), A_orig.size2());
    viennacl::copy(A_orig, A);

    for (unsigned int i = 0; i < A.size1(); i++) {
        for (unsigned int j = 0; j < A.size2(); j++) {
            if ((std::abs(A(i, j)) > EPS) && ((i - 1) != j) && (i != j) && ((i + 1) != j))
            {
                // std::cout << "Failed at " << i << " " << j << " " << A(i, j) << "\n";
                return false;
            }
        }
    }
    return true;
}
Пример #9
0
bool check_hessenberg(viennacl::matrix<ScalarType, MatrixLayout>& A_orig)
{
    ublas::matrix<ScalarType> A(A_orig.size1(), A_orig.size2());
    viennacl::copy(A_orig, A);

    for (std::size_t i = 0; i < A.size1(); i++) {
        for (std::size_t j = 0; j < A.size2(); j++) {
            if ((std::abs(A(i, j)) > EPS) && (i > (j + 1)))
            {
                // std::cout << "Failed at " << i << " " << j << " " << A(i, j) << "\n";
                return false;
            }
        }
    }
    return true;
}
Пример #10
0
float matrix_compare(viennacl::matrix<ScalarType>& res,
                     viennacl::matrix<ScalarType>& ref)
{
    std::vector<ScalarType> res_std(res.internal_size());
    std::vector<ScalarType> ref_std(ref.internal_size());

    viennacl::fast_copy(res, &res_std[0]);
    viennacl::fast_copy(ref, &ref_std[0]);

    float diff = 0.0;
    float mx = 0.0;

    for(std::size_t i = 0; i < res_std.size(); i++) {
        diff = std::max(diff, std::abs(res_std[i] - ref_std[i]));
        mx = std::max(mx, res_std[i]);
    }

    return diff / mx;
}
Пример #11
0
ScalarType diff(ublas::matrix<ScalarType> & mat1, viennacl::matrix<ScalarType, F, ALIGNMENT> & mat2)
{
   ublas::matrix<ScalarType> mat2_cpu(mat2.size1(), mat2.size2());
   copy(mat2, mat2_cpu);
   ScalarType ret = 0;
   ScalarType act = 0;

    for (unsigned int i = 0; i < mat2_cpu.size1(); ++i)
    {
      for (unsigned int j = 0; j < mat2_cpu.size2(); ++j)
      {
         act = fabs(mat2_cpu(i,j) - mat1(i,j)) / std::max( fabs(mat2_cpu(i, j)), fabs(mat1(i,j)) );
         if (act > ret)
           ret = act;
      }
    }
   //std::cout << ret << std::endl;
   return ret;
}
Пример #12
0
void read_matrix_body(std::fstream& f, viennacl::matrix<ScalarType, MatrixLayout>& A)
{
    if(!f.is_open())
    {
        throw std::invalid_argument("File is not opened");
    }

    boost::numeric::ublas::matrix<ScalarType> h_A(A.size1(), A.size2());

    for(std::size_t i = 0; i < h_A.size1(); i++) {
        for(std::size_t j = 0; j < h_A.size2(); j++) {
            ScalarType val = 0.0;
            f >> val;
            h_A(i, j) = val;
        }
    }

    viennacl::copy(h_A, A);
}
Пример #13
0
FeatureNormalize<Scalar>::FeatureNormalize(const viennacl::matrix<Scalar> &X):
        blas_matrix_(X.size1(), X.size2()),
        normalized_matrix_(X.size1(), X.size2()),
        mu_(X.size2()), sigma_(X.size2()) {
    viennacl::copy(X, blas_matrix_);
    runNormalization();
}
Пример #14
0
bool check_bidiag(viennacl::matrix<ScalarType>& A)
{
  const ScalarType EPS = 0.0001f;

  std::vector<ScalarType> aA(A.size1() * A.size2());
  viennacl::fast_copy(A, &aA[0]);

  for(std::size_t i = 0; i < A.size1(); i++)
  {
    for(std::size_t j = 0; j < A.size2(); j++)
    {
      ScalarType val = aA[i * A.size2() + j];
      if((fabs(val) > EPS) && (i != j) && ((i + 1) != j))
      {
        std::cout << "Failed at " << i << " " << j << " " << val << std::endl;
        return false;
      }
    }
  }

  return true;
}
Пример #15
0
    void svd(viennacl::matrix<SCALARTYPE, row_major, ALIGNMENT> & A,
              viennacl::matrix<SCALARTYPE, row_major, ALIGNMENT> & QL,
              viennacl::matrix<SCALARTYPE, row_major, ALIGNMENT> & QR)
    {
      viennacl::ocl::context & ctx = const_cast<viennacl::ocl::context &>(viennacl::traits::opencl_handle(A).context());
      viennacl::linalg::opencl::kernels::svd<SCALARTYPE>::init(ctx);

      vcl_size_t row_num = A.size1();
      vcl_size_t col_num = A.size2();

      vcl_size_t to = std::min(row_num, col_num);


      //viennacl::vector<SCALARTYPE, ALIGNMENT> d(to);
      //viennacl::vector<SCALARTYPE, ALIGNMENT> s(to + 1);

      // first stage
      detail::bidiag(A, QL, QR);

      // second stage
      //std::vector<SCALARTYPE> dh(to, 0);
      //std::vector<SCALARTYPE> sh(to + 1, 0);
      boost::numeric::ublas::vector<SCALARTYPE> dh = boost::numeric::ublas::scalar_vector<SCALARTYPE>(to, 0);
      boost::numeric::ublas::vector<SCALARTYPE> sh = boost::numeric::ublas::scalar_vector<SCALARTYPE>(to + 1, 0);


      viennacl::linalg::opencl::bidiag_pack_svd(A, dh, sh);

      detail::svd_qr_shift( QL, QR, dh, sh);

      // Write resulting diagonal matrix with singular values to A:
      boost::numeric::ublas::matrix<SCALARTYPE> h_Sigma(row_num, col_num);
      h_Sigma.clear();

      for (vcl_size_t i = 0; i < to; i++)
        h_Sigma(i, i) = dh[i];

      copy(h_Sigma, A);
    }
Пример #16
0
      void bidiag(viennacl::matrix<SCALARTYPE, row_major, ALIGNMENT> & Ai,
                  viennacl::matrix<SCALARTYPE, row_major, ALIGNMENT> & QL,
                  viennacl::matrix<SCALARTYPE, row_major, ALIGNMENT> & QR)
      {
        vcl_size_t row_num = Ai.size1();
        vcl_size_t col_num = Ai.size2();

        vcl_size_t to = std::min(row_num, col_num);
        vcl_size_t big_to = std::max(row_num, col_num);

        //for storing householder vector
        viennacl::vector<SCALARTYPE, ALIGNMENT> hh_vector(big_to, viennacl::traits::context(Ai));

        QL = viennacl::identity_matrix<SCALARTYPE>(QL.size1(), viennacl::traits::context(QL));
        QR = viennacl::identity_matrix<SCALARTYPE>(QR.size1(), viennacl::traits::context(QR));

        for (vcl_size_t i = 0; i < to; i++)
        {
          householder_c(Ai, QL, hh_vector, i, i);
          householder_r(Ai, QR, hh_vector, i, i+1);
        }
      }
Пример #17
0
      bool householder_r(viennacl::matrix<SCALARTYPE, row_major, ALIGNMENT> & A,
                          viennacl::matrix<SCALARTYPE, row_major, ALIGNMENT> & Q,
                          viennacl::vector<SCALARTYPE, ALIGNMENT>& D,
                          vcl_size_t row_start, vcl_size_t col_start)
      {
        viennacl::ocl::context & ctx = const_cast<viennacl::ocl::context &>(viennacl::traits::opencl_handle(A).context());

        if (col_start + 1 >= A.size2())
          return false;

        prepare_householder_vector(A, D, A.size2(), row_start, col_start, col_start, false);

        {
          viennacl::ocl::kernel& kernel = ctx.get_kernel(viennacl::linalg::opencl::kernels::svd<SCALARTYPE>::program_name(), SVD_HOUSEHOLDER_UPDATE_A_RIGHT_KERNEL);

          viennacl::ocl::enqueue(kernel(
                                        A,
                                        D,
                                        static_cast<cl_uint>(row_start),
                                        static_cast<cl_uint>(col_start),
                                        static_cast<cl_uint>(A.size1()),
                                        static_cast<cl_uint>(A.size2()),
                                        static_cast<cl_uint>(A.internal_size2()),
                                        viennacl::ocl::local_mem(static_cast<cl_uint>(128 * sizeof(SCALARTYPE)))
                                ));
        }

        {
          viennacl::ocl::kernel& kernel = ctx.get_kernel(viennacl::linalg::opencl::kernels::svd<SCALARTYPE>::program_name(), SVD_HOUSEHOLDER_UPDATE_QR_KERNEL);

          viennacl::ocl::enqueue(kernel(
                                        Q,
                                        D,
                                        static_cast<cl_uint>(A.size1()),
                                        static_cast<cl_uint>(A.size2()),
                                        static_cast<cl_uint>(Q.internal_size2()),
                                        viennacl::ocl::local_mem(static_cast<cl_uint>(128 * sizeof(SCALARTYPE)))
                                ));
        }

        return true;
      }
Пример #18
0
NumericT diff(std::vector<std::vector<NumericT> > const & A1, viennacl::matrix<NumericT> const & A2)
{
  std::vector<NumericT> host_values(A2.internal_size());
  for (std::size_t i=0; i<A2.size1(); ++i)
    for (std::size_t j=0; j<A2.size2(); ++j)
      host_values[i*A2.internal_size2() + j] = A1[i][j];

  std::vector<NumericT> device_values(A2.internal_size());
  viennacl::fast_copy(A2, &device_values[0]);
  viennacl::vector<NumericT> vcl_device_values(A2.internal_size());  // workaround to avoid code duplication
  viennacl::copy(device_values, vcl_device_values);

  return diff(host_values, vcl_device_values);
}
 static size_t size1(viennacl::matrix<ScalarType, F1, A1> const & lhs,
                     viennacl::matrix_expression<const viennacl::matrix<ScalarType, F2, A2>,
                                                 const viennacl::matrix<ScalarType, F2, A2>,
                                                 op_trans> const & rhs) { return lhs.size1(); }
Пример #20
0
void fill_random(viennacl::matrix<ScalarType, MajorT> & A)
{
  for (std::size_t i = 0; i < A.size1(); i++)
    for (std::size_t j = 0; j < A.size2(); ++j)
      A(i, j) = static_cast<ScalarType>(rand()) / ScalarType(RAND_MAX);
}
Пример #21
0
 viennacl::vector_range<viennacl::vector_base<T> > sharedVector(){
     viennacl::vector_base<T> tmp(ptr_matrix->handle(), ptr_matrix->internal_size(), 0, 1);
     viennacl::vector_range<viennacl::vector_base<T> > v_sub(tmp, r);
     return v_sub;
 }
Пример #22
0
 viennacl::vector_range<viennacl::vector_base<T> > sharedRow(){
     // viennacl::vector_base<T> tmp(ptr_matrix->handle(), ptr_matrix->internal_size(), 0, 1);
     viennacl::vector_base<T> tmp(ptr_matrix->handle(), ptr_matrix->size2(), begin, 1);
     viennacl::vector_range<viennacl::vector_base<T> > v_sub(tmp, r);
     return v_sub;
 }
Пример #23
0
 static size_t size(const viennacl::matrix<ScalarType, F, Amat> & lhs,
                    const viennacl::vector<ScalarType, A> & rhs) { return lhs.size1(); }
Пример #24
0
    void nmf(viennacl::matrix<ScalarType> const & v,
             viennacl::matrix<ScalarType> & w,
             viennacl::matrix<ScalarType> & h,
             std::size_t k,
             ScalarType eps = 0.000001,
             std::size_t max_iter = 10000,
             std::size_t check_diff_every_step = 100)
    {
      viennacl::linalg::kernels::nmf<ScalarType, 1>::init();
      
      w.resize(v.size1(), k);
      h.resize(k, v.size2());

      std::vector<ScalarType> stl_w(w.internal_size1() * w.internal_size2());
      std::vector<ScalarType> stl_h(h.internal_size1() * h.internal_size2());

      for (std::size_t j = 0; j < stl_w.size(); j++)
          stl_w[j] = static_cast<ScalarType>(rand()) / RAND_MAX;

      for (std::size_t j = 0; j < stl_h.size(); j++)
          stl_h[j] = static_cast<ScalarType>(rand()) / RAND_MAX;

      viennacl::matrix<ScalarType> wn(v.size1(), k);
      viennacl::matrix<ScalarType> wd(v.size1(), k);
      viennacl::matrix<ScalarType> wtmp(v.size1(), v.size2());

      viennacl::matrix<ScalarType> hn(k, v.size2());
      viennacl::matrix<ScalarType> hd(k, v.size2());
      viennacl::matrix<ScalarType> htmp(k, k);

      viennacl::matrix<ScalarType> appr(v.size1(), v.size2());
      viennacl::vector<ScalarType> diff(v.size1() * v.size2());

      viennacl::fast_copy(&stl_w[0], &stl_w[0] + stl_w.size(), w);
      viennacl::fast_copy(&stl_h[0], &stl_h[0] + stl_h.size(), h);

      ScalarType last_diff = 0.0f;


      
      for (std::size_t i = 0; i < max_iter; i++)
      {
        {
          hn = viennacl::linalg::prod(trans(w), v);
          htmp = viennacl::linalg::prod(trans(w), w);
          hd = viennacl::linalg::prod(htmp, h);

          viennacl::ocl::kernel & mul_div_kernel = viennacl::ocl::get_kernel(viennacl::linalg::kernels::nmf<ScalarType, 1>::program_name(), 
                                                                             NMF_MUL_DIV_KERNEL);
          viennacl::ocl::enqueue(mul_div_kernel(h, hn, hd, cl_uint(stl_h.size())));
        }
        {
          wn = viennacl::linalg::prod(v, trans(h));
          wtmp = viennacl::linalg::prod(w, h);
          wd = viennacl::linalg::prod(wtmp, trans(h));

          viennacl::ocl::kernel & mul_div_kernel = viennacl::ocl::get_kernel(viennacl::linalg::kernels::nmf<ScalarType, 1>::program_name(), 
                                                                             NMF_MUL_DIV_KERNEL);
          
          viennacl::ocl::enqueue(mul_div_kernel(w, wn, wd, cl_uint(stl_w.size())));
        }

        if (i % check_diff_every_step == 0)
        {
          appr = viennacl::linalg::prod(w, h);

         viennacl::ocl::kernel & sub_kernel = viennacl::ocl::get_kernel(viennacl::linalg::kernels::nmf<ScalarType, 1>::program_name(), 
                                                                        NMF_SUB_KERNEL);
          //this is a cheat. i.e save difference of two matrix into vector to get norm_2
          viennacl::ocl::enqueue(sub_kernel(appr, v, diff, cl_uint(v.size1() * v.size2())));
          ScalarType diff_val = viennacl::linalg::norm_2(diff);

          if((diff_val < eps) || (fabs(diff_val - last_diff) < eps))
          {
              //std::cout << "Breaked at diff - " << diff_val << "\n";
              break;
          }

          last_diff = diff_val;

          //printf("Iteration #%lu - %.5f \n", i, diff_val);
        }
      }
      
      
    }