Beispiel #1
0
bool ConvexHull<N, T>::contains(const AABBType& aabb) const
{
	const VectorType min = aabb.getMin();
	const VectorType max = aabb.getMax();
	
	for (std::size_t i = 0; i < getPlaneCount(); ++i)
	{
		const VectorType& normal = planes[i].getNormal();
		
		VectorType pv;
		VectorType nv;
		
		for (std::size_t j = 0; j < N; ++j)
		{
			pv[j] = normal[j] > ScalarType(0) ? max[j] : min[j];
			nv[j] = normal[j] < ScalarType(0) ? max[j] : min[j];
		}
		
		if (planes[i].distance(pv) < ScalarType(0) ||
			planes[i].distance(nv) < ScalarType(0))
		{
			return false;
		}
	}
	
	return true;
}
Beispiel #2
0
typename MatrixType::ScalarType::BoundType OdeNum<MatrixType>::linEscTime(Real a, Real b, Real r)
{
  if(a>r) return -1;
  else{
    if(b>0)
      return ((ScalarType(r)-ScalarType(a))/ScalarType(b)).leftBound();
    else
      return OdeNum<MatrixType>::infty;
  }
}
Beispiel #3
0
std::ostream& operator<<(std::ostream& out, const capd::map::Node<ScalarType>& n)
{
  out << "{";
  if(n.getOrder()>0){
    if(n[0]>=ScalarType(0)) out << " ";
    out << n[0];
  }
  for(int i=1;i<n.getOrder();i++)
  {
    out << ",";
    if(n[i]>=ScalarType(0)) out << " ";
    out << n[i];
  }
  out << "}";
  return out;
}
Beispiel #4
0
ScalarType run_benchmark(size_t size, operation_type type)
{
    std::size_t n_bytes = size*sizeof(ScalarType);
    std::size_t n_transfers = 2;
    if(type==dot)
      n_transfers = 2;
    else if(type==assign)
      n_transfers = 2;
    viennacl::vector<ScalarType> vcl_A = viennacl::scalar_vector<ScalarType>(size,1);
    viennacl::vector<ScalarType> vcl_B = viennacl::scalar_vector<ScalarType>(size,1);

    viennacl::scalar<ScalarType> s(0);

    viennacl::scheduler::statement * statement = NULL;

    if(type==dot)
      statement = new viennacl::scheduler::statement(s, viennacl::op_assign(), viennacl::linalg::inner_prod(vcl_A, vcl_B));
    else //if(type==assign)
      statement = new viennacl::scheduler::statement(vcl_A, viennacl::op_assign(), vcl_B);

    viennacl::generator::generate_enqueue_statement(*statement, statement->array()[0]);
    viennacl::backend::finish();

    Timer timer;
    timer.start();
    for(unsigned int r = 0 ; r < N_RUNS ; ++r){
      viennacl::generator::generate_enqueue_statement(*statement, statement->array()[0]);
    }
    viennacl::backend::finish();

    double time = timer.get()/(double)N_RUNS;
    delete statement;

    return ScalarType((n_bytes*n_transfers) / time / 1e9);
}
Beispiel #5
0
typename OdeNum<MatrixType>::VectorType OdeNum<MatrixType>::enclosureGuaranteeingBounds(
      const VectorType &x,
      int *found
    )
{
  ScalarType trial_step = ScalarType(-0.2,1.2)*step;
  int dim = x.dimension();
  VectorType y(dim), z(dim), Small(dim);

  ScalarType h = ScalarType(0,1)*step;
  Real multf=1.5;
  *found=0;
  int counter=0,limit=10;

  for(int i=0;i<dim;i++)
    Small[i] = ScalarType(-1,1) * 1e-15;

  z = x + trial_step*ode.f(x);
  z += Small; // to be sure that z has noempty interior

  while((!(*found)) && (counter<limit))
  {
    counter++;
    y = x + h*ode.f(z);
    *found=1;
    for(int i=0;i<dim;i++)
    {
      if(!(y[i].subsetInterior(z[i])))
      {
        *found = 0;
        z[i] = y[i];
        ScalarType s;
        z[i].split(s);
        s = multf*s;
        z[i] += s;
      }
    }
  }

  if(!(*found))
  {
    //status="enclosureGuaranteeingBounds - loop limit exceeded";
    diagBadEnlcosure(x);
  }

  return y;
}
Beispiel #6
0
typename MatrixType::ScalarType::BoundType OdeNum<MatrixType>::quadEscTime(Real a, Real b, Real c, Real r)
{
  if(a>r) return -1;
  else{
    if( c == 0)
      return OdeNum<MatrixType>::linEscTime(a,b,r);
    else{
         ScalarType ib = ScalarType(b);
         ScalarType delta = power(ib,2)-4*ScalarType(c)*(ScalarType(a)-ScalarType(r));
//        ScalarType delta=ib^2-4*ScalarType(c)*(ScalarType(a)-ScalarType(r));
      if(c>0 || (delta > 0 && b > 0))
        return ((-b+sqrt(nonnegativePart(delta)))/2/c).leftBound();
      else
        return OdeNum<MatrixType>::infty;
    }
  }
}
Beispiel #7
0
bool ConvexHull<N, T>::contains(const VectorType& point) const
{
	for (std::size_t i = 0; i < getPlaneCount(); ++i)
	{
		if (planes[i].distance(point) < ScalarType(0))
			return false;
	}
	
	return true;
}
Beispiel #8
0
void Node<ScalarType>::allocateAndClear()
{
  value = new ScalarType[m_order];
  iterator b=begin(), e=end();
  while(b!=e)
  {
    *b = ScalarType(0);
    ++b;
  }
}
Beispiel #9
0
	/** Constructor for heap with specified capacity */
	fibonacci_heap(int capacity) : 
		min_root(NULL), nodes(NULL), num_nodes(0),
		num_trees(0), max_num_nodes(capacity), A(NULL), Dn(0)
	{
		nodes = (fibonacci_heap_node**)malloc(sizeof(fibonacci_heap_node*)*max_num_nodes);
		for (int i = 0; i < max_num_nodes; i++)
			nodes[i] = new fibonacci_heap_node;

		Dn = 1 + (int)(log(ScalarType(max_num_nodes))/log(2.));
		A = (fibonacci_heap_node**)malloc(sizeof(fibonacci_heap_node*)*Dn);
		for (int i = 0; i < Dn; i++)
			A[i] = NULL;

		num_nodes = 0;
		num_trees = 0;
	}
Beispiel #10
0
void Plane<N, T>::setCoefficients(const Vector<N + 1, T>& coefficients)
{
	for (std::size_t i = 0; i < N; ++i)
	{
		normal[i] = coefficients[i];
	}
	dist = coefficients[N];
	
	ScalarType length = normal.length();
	if (!length)
	{
		throw ZeroDivisionException();
	}
	
	ScalarType inverseLength = ScalarType(1) / length;
	normal *= inverseLength;
	dist *= inverseLength;
}
typename DiffInclusionLN<MapT, DynSysT>::VectorType DiffInclusionLN<MapT, DynSysT>::perturbations(const VectorType& x){

  VectorType W_1 = dynamicalSystemEnclosure(x);
  VectorType W_2 = diffInclusionEnclosure(x);

  MatrixType J = m_diffIncl.getVectorField()[W_2]; 
  VectorType deltha = m_diffIncl.perturbations(W_2);

  ScalarType C = right((*m_norm)(deltha));
  ScalarType l = right((*m_norm)(J));

  ScalarType D = (l.contains(0.0))? C*getStep() : (C*(exp(l*getStep())-1))/l;
  VectorType result(x.dimension());

  for(int i=0; i< result.dimension(); ++i)
    result[i] = ScalarType(-D.rightBound(), D.rightBound());

  return result;
}
Beispiel #12
0
void initialize(viennacl::matrix<ScalarType> & A, std::vector<ScalarType> & v)
{
  // System matrix:
  ScalarType M[9][9] = {{ 4,  1, -2, 2, -7,  3,  9, -6, -2},
                        { 1, -2,  0, 1, -1,  5,  4,  7,  3},
                        {-2,  0,  3, 2,  0,  3,  6,  1, -1},
                        { 2,  1,  2, 1,  4,  5,  6,  7,  8},
                        {-7, -1,  0, 4,  5,  4,  9,  1, -8},
                        { 3,  5,  3, 5,  4,  9, -3,  3,  3},
                        { 9,  4,  6, 6,  9, -3,  3,  6, -7},
                        {-6,  7,  1, 7,  1,  3,  6,  2,  6},
                        {-2,  3, -1, 8, -8,  3, -7,  6,  1}};

  for(std::size_t i = 0; i < 9; i++)
    for(std::size_t j = 0; j < 9; j++)
      A(i, j) = M[i][j];

  // Known eigenvalues:
  ScalarType V[9] = {ScalarType(12.6005), ScalarType(19.5905), ScalarType(8.06067), ScalarType(2.95074), ScalarType(0.223506),
                     ScalarType(24.3642), ScalarType(-9.62084), ScalarType(-13.8374), ScalarType(-18.3319)};

  for(std::size_t i = 0; i < 9; i++)
    v[i] = V[i];
}
	typename AACube< Point >::ScalarType
		AACube< Point >::DiagLength() const
	{
		return std::sqrt(ScalarType(Dim)) * _width;
	}
int vandermonde_test(ScalarType epsilon)
{
    std::size_t VANDERMONDE_SIZE = 61;

    viennacl::vandermonde_matrix<ScalarType> vcl_vandermonde1(VANDERMONDE_SIZE, VANDERMONDE_SIZE);
    viennacl::vandermonde_matrix<ScalarType> vcl_vandermonde2(VANDERMONDE_SIZE, VANDERMONDE_SIZE);

    viennacl::vector<ScalarType> vcl_input(VANDERMONDE_SIZE);
    viennacl::vector<ScalarType> vcl_result(VANDERMONDE_SIZE);

    std::vector<ScalarType> input_ref(VANDERMONDE_SIZE);
    std::vector<ScalarType> result_ref(VANDERMONDE_SIZE);

    dense_matrix<ScalarType> m1(vcl_vandermonde1.size1(), vcl_vandermonde1.size2());
    dense_matrix<ScalarType> m2(m1.size1(), m1.size2());

    for (std::size_t i = 0; i < m1.size1(); i++)
      for (std::size_t j = 0; j < m1.size2(); j++)
      {
        m1(i,j) = std::pow(ScalarType(1.0 + i/1000.0), ScalarType(j));
        m2(i,j) = std::pow(ScalarType(1.0 - i/2000.0), ScalarType(j));
      }

    for (std::size_t i = 0; i < input_ref.size(); i++)
      input_ref[i] = ScalarType(i);

    // Copy to ViennaCL
    viennacl::copy(m1, vcl_vandermonde1);
    viennacl::copy(m2, vcl_vandermonde2);
    viennacl::copy(input_ref, vcl_input);

    //
    // Matrix-Vector product:
    //
    vcl_result = viennacl::linalg::prod(vcl_vandermonde1, vcl_input);

    for (std::size_t i = 0; i < m1.size1(); i++)     //reference calculation
    {
      ScalarType entry = 0;
      for (std::size_t j = 0; j < m1.size2(); j++)
        entry += m1(i,j) * input_ref[j];

      result_ref[i] = entry;
    }

    viennacl::copy(vcl_result, input_ref);
    std::cout << "Matrix-Vector Product: " << diff_max(input_ref, result_ref);
    if (diff_max(input_ref, result_ref) < epsilon)
      std::cout << " [OK]" << std::endl;
    else
    {
      for (std::size_t i=0; i<input_ref.size(); ++i)
        std::cout << "Should: " << result_ref[i] << ", is: " << input_ref[i] << std::endl;
      std::cout << " [FAILED]" << std::endl;
      return EXIT_FAILURE;
    }


    //
    // Note: Matrix addition does not make sense for a Vandermonde matrix
    //


    //
    // Per-Element access:
    //
    vcl_vandermonde1(4) = static_cast<ScalarType>(1.0001);

    for (std::size_t j = 0; j < m1.size2(); j++)
    {
      m1(4, j) = std::pow(ScalarType(1.0001), ScalarType(j));
    }

    viennacl::copy(vcl_vandermonde1, m2);
    std::cout << "Element manipulation: " << diff(m1, m2);
    if (diff(m1, m2) < epsilon)
      std::cout << " [OK]" << std::endl;
    else
    {
      std::cout << " [FAILED]" << std::endl;
      return EXIT_FAILURE;
    }

    return EXIT_SUCCESS;
}
int hankel_test(ScalarType epsilon)
{
    std::size_t HANKEL_SIZE = 7;
    viennacl::hankel_matrix<ScalarType> vcl_hankel1(HANKEL_SIZE, HANKEL_SIZE);
    viennacl::hankel_matrix<ScalarType> vcl_hankel2(HANKEL_SIZE, HANKEL_SIZE);

    viennacl::vector<ScalarType> vcl_input(HANKEL_SIZE);
    viennacl::vector<ScalarType> vcl_result(HANKEL_SIZE);

    std::vector<ScalarType> input_ref(HANKEL_SIZE);
    std::vector<ScalarType> result_ref(HANKEL_SIZE);

    dense_matrix<ScalarType> m1(vcl_hankel1.size1(), vcl_hankel1.size2());
    dense_matrix<ScalarType> m2(m1.size1(), m1.size2());

    for (std::size_t i = 0; i < m1.size1(); i++)
      for (std::size_t j = 0; j < m1.size2(); j++)
      {
        m1(i,j) = static_cast<ScalarType>((i + j) % (2 * m1.size1()));
        m2(i,j) = m1(i,j) * m1(i,j) + ScalarType(1);
      }

    for (std::size_t i = 0; i < input_ref.size(); i++)
      input_ref[i] = ScalarType(i);

    // Copy to ViennaCL
    viennacl::copy(m1, vcl_hankel1);
    viennacl::copy(m2, vcl_hankel2);
    viennacl::copy(input_ref, vcl_input);

    //
    // Matrix-Vector product:
    //
    vcl_result = viennacl::linalg::prod(vcl_hankel1, vcl_input);

    for (std::size_t i = 0; i < m1.size1(); i++)     //reference calculation
    {
      ScalarType entry = 0;
      for (std::size_t j = 0; j < m1.size2(); j++)
        entry += m1(i,j) * input_ref[j];

      result_ref[i] = entry;
    }

    viennacl::copy(vcl_result, input_ref);
    std::cout << "Matrix-Vector Product: " << diff_max(input_ref, result_ref);
    if (diff_max(input_ref, result_ref) < epsilon)
      std::cout << " [OK]" << std::endl;
    else
    {
      for (std::size_t i=0; i<input_ref.size(); ++i)
        std::cout << "Should: " << result_ref[i] << ", is: " << input_ref[i] << std::endl;
      std::cout << " [FAILED]" << std::endl;
      return EXIT_FAILURE;
    }


    //
    // Matrix addition:
    //
    vcl_hankel1 += vcl_hankel2;

    for (std::size_t i = 0; i < m1.size1(); i++)    //reference calculation
      for (std::size_t j = 0; j < m1.size2(); j++)
        m1(i,j) += m2(i,j);

    viennacl::copy(vcl_hankel1, m2);
    std::cout << "Matrix Addition: " << diff(m1, m2);
    if (diff(m1, m2) < epsilon)
      std::cout << " [OK]" << std::endl;
    else
    {
      std::cout << " [FAILED]" << std::endl;
      return EXIT_FAILURE;
    }

    //
    // Per-Element access:
    //
    vcl_hankel1(4,2) = 42;

    for (std::size_t i = 0; i < m1.size1(); i++)    //reference calculation
      for (std::size_t j = 0; j < m1.size2(); j++)
      {
        if ((i + j) % (2*m1.size1()) == 6)
          m1(i, j) = 42;
      }

    viennacl::copy(vcl_hankel1, m2);
    std::cout << "Element manipulation: " << diff(m1, m2);
    if (diff(m1, m2) < epsilon)
      std::cout << " [OK]" << std::endl;
    else
    {
      std::cout << " [FAILED]" << std::endl;
      return EXIT_FAILURE;
    }

    return EXIT_SUCCESS;
}
Beispiel #16
0
ScalarType wrap(ScalarType val, ScalarType min = ScalarType(), ScalarType max = static_cast<ScalarType>(1.f))
{
	while(val < min) val += max - min;
	while(val > max) val -= max - min;
}
int toeplitz_test(ScalarType epsilon)
{
    std::size_t TOEPLITZ_SIZE = 47;
    viennacl::toeplitz_matrix<ScalarType> vcl_toeplitz1(TOEPLITZ_SIZE, TOEPLITZ_SIZE);
    viennacl::toeplitz_matrix<ScalarType> vcl_toeplitz2(TOEPLITZ_SIZE, TOEPLITZ_SIZE);

    viennacl::vector<ScalarType> vcl_input(TOEPLITZ_SIZE);
    viennacl::vector<ScalarType> vcl_result(TOEPLITZ_SIZE);

    std::vector<ScalarType> input_ref(TOEPLITZ_SIZE);
    std::vector<ScalarType> result_ref(TOEPLITZ_SIZE);

    dense_matrix<ScalarType> m1(TOEPLITZ_SIZE, TOEPLITZ_SIZE);
    dense_matrix<ScalarType> m2(TOEPLITZ_SIZE, TOEPLITZ_SIZE);

    for (std::size_t i = 0; i < TOEPLITZ_SIZE; i++)
      for (std::size_t j = 0; j < TOEPLITZ_SIZE; j++)
      {
        m1(i,j) = static_cast<ScalarType>(i) - static_cast<ScalarType>(j);
        m2(i,j) = m1(i,j) * m1(i,j) + ScalarType(1);
      }

    for (std::size_t i = 0; i < TOEPLITZ_SIZE; i++)
      input_ref[i] = ScalarType(i);

    // Copy to ViennaCL
    viennacl::copy(m1, vcl_toeplitz1);
    viennacl::copy(m2, vcl_toeplitz2);
    viennacl::copy(input_ref, vcl_input);

    //
    // Matrix-Vector product:
    //
    vcl_result = viennacl::linalg::prod(vcl_toeplitz1, vcl_input);

    for (std::size_t i = 0; i < m1.size1(); i++)     //reference calculation
    {
      ScalarType entry = 0;
      for (std::size_t j = 0; j < m1.size2(); j++)
        entry += m1(i,j) * input_ref[j];

      result_ref[i] = entry;
    }

    viennacl::copy(vcl_result, input_ref);
    std::cout << "Matrix-Vector Product: " << diff_max(input_ref, result_ref);
    if (diff_max(input_ref, result_ref) < epsilon)
      std::cout << " [OK]" << std::endl;
    else
    {
      for (std::size_t i=0; i<input_ref.size(); ++i)
        std::cout << "Should: " << result_ref[i] << ", is: " << input_ref[i] << std::endl;
      std::cout << " [FAILED]" << std::endl;
      return EXIT_FAILURE;
    }


    //
    // Matrix addition:
    //
    vcl_toeplitz1 += vcl_toeplitz2;

    for (std::size_t i = 0; i < m1.size1(); i++)    //reference calculation
      for (std::size_t j = 0; j < m1.size2(); j++)
        m1(i,j) += m2(i,j);

    viennacl::copy(vcl_toeplitz1, m2);
    std::cout << "Matrix Addition: " << diff(m1, m2);
    if (diff(m1, m2) < epsilon)
      std::cout << " [OK]" << std::endl;
    else
    {
      std::cout << " [FAILED]" << std::endl;
      return EXIT_FAILURE;
    }

    //
    // Per-Element access:
    //
    vcl_toeplitz1(2,4) = 42;

    for (std::size_t i=0; i<m1.size1(); ++i)    //reference calculation
    {
      if (i + 2 < m1.size2())
        m1(i, i+2) = 42;
    }

    viennacl::copy(vcl_toeplitz1, m2);
    std::cout << "Element manipulation: " << diff(m1, m2);
    if (diff(m1, m2) < epsilon)
      std::cout << " [OK]" << std::endl;
    else
    {
      std::cout << " [FAILED]" << std::endl;
      return EXIT_FAILURE;
    }

    return EXIT_SUCCESS;
}
int run_test()
{
    //typedef float               ScalarType;
    typedef boost::numeric::ublas::matrix<ScalarType>       MatrixType;
    typedef boost::numeric::ublas::vector<ScalarType>       VectorType;
    
    typedef viennacl::matrix<ScalarType, T>    VCLMatrixType;
    typedef viennacl::vector<ScalarType>       VCLVectorType;
    
    viennacl::scalar<ScalarType> gpu_pi = ScalarType(3.1415);
    
    std::size_t dim_large = 151;
    std::size_t dim_small = 37;
    //std::size_t dim_large = 35;
    //std::size_t dim_small = 11;
    
    //setup ublas objects:
    MatrixType ublas_A(dim_large, dim_large);
    for (std::size_t i=0; i<ublas_A.size1(); ++i)
      for (std::size_t j=0; j<ublas_A.size2(); ++j)
        ublas_A(i,j) = ScalarType((i+1) + (j+1)*(i+1));

    MatrixType ublas_B(dim_small, dim_small);
    for (std::size_t i=0; i<ublas_B.size1(); ++i)
      for (std::size_t j=0; j<ublas_B.size2(); ++j)
        ublas_B(i,j) = ScalarType((i+1) + (j+1)*(i+1));

    MatrixType ublas_C(dim_large, dim_small);
    for (std::size_t i=0; i<ublas_C.size1(); ++i)
      for (std::size_t j=0; j<ublas_C.size2(); ++j)
        ublas_C(i,j) = ScalarType((j+2) + (j+1)*(i+1));

    MatrixType ublas_D(dim_small, dim_large);
    for (std::size_t i=0; i<ublas_D.size1(); ++i)
      for (std::size_t j=0; j<ublas_D.size2(); ++j)
        ublas_D(i,j) = ScalarType((j+2) + (j+1)*(i+1));
      
    boost::numeric::ublas::range ublas_r1(0, dim_small);
    boost::numeric::ublas::range ublas_r2(dim_large - dim_small, dim_large);
    boost::numeric::ublas::matrix_range<MatrixType> ublas_A_sub1(ublas_A, ublas_r1, ublas_r1);
    boost::numeric::ublas::matrix_range<MatrixType> ublas_A_sub2(ublas_A, ublas_r2, ublas_r2);

    boost::numeric::ublas::matrix_range<MatrixType> ublas_C_sub(ublas_C, ublas_r1, ublas_r1);
    boost::numeric::ublas::matrix_range<MatrixType> ublas_D_sub(ublas_D, ublas_r1, ublas_r1);

    //Setup ViennaCL objects    
    VCLMatrixType vcl_A(dim_large, dim_large);
    viennacl::copy(ublas_A, vcl_A);
    VCLMatrixType vcl_B(dim_small, dim_small);
    viennacl::copy(ublas_B, vcl_B);
    VCLMatrixType vcl_C(dim_large, dim_small);
    viennacl::copy(ublas_C, vcl_C);
    VCLMatrixType vcl_D(dim_small, dim_large);
    viennacl::copy(ublas_D, vcl_D);
    
    viennacl::range vcl_r1(0, dim_small);
    viennacl::range vcl_r2(dim_large - dim_small, dim_large);
    viennacl::matrix_range<VCLMatrixType>   vcl_A_sub1(vcl_A, vcl_r1, vcl_r1);
    viennacl::matrix_range<VCLMatrixType>   vcl_A_sub2(vcl_A, vcl_r2, vcl_r2);
    
    viennacl::matrix_range<VCLMatrixType>   vcl_C_sub(vcl_C, vcl_r1, vcl_r1);
    viennacl::matrix_range<VCLMatrixType>   vcl_D_sub(vcl_D, vcl_r1, vcl_r1);

    std::cout << std::endl;
    std::cout << "//" << std::endl;
    std::cout << "////////// Test: Copy CTOR //////////" << std::endl;
    std::cout << "//" << std::endl;

    {
      std::cout << "Testing matrix created from range... ";
      ublas_B = ublas_A_sub1;
      VCLMatrixType vcl_temp = vcl_A_sub1;
      if (check_for_equality(ublas_B, vcl_temp))
        std::cout << "PASSED!" << std::endl;
      else
      {
        std::cout << std::endl << "TEST failed!";
        return EXIT_FAILURE;
      }
      
      std::cout << "Testing range created from range... ";
      //ublas_A_sub1 = ublas_A_sub1;
      VCLMatrixType vcl_ctor_sub1 = vcl_A_sub1;  //Note: This is mostly a compilation test only
      if (check_for_equality(ublas_A, vcl_A))
        std::cout << "PASSED!" << std::endl;
      else
      {
        std::cout << std::endl << "TEST failed!";
        return EXIT_FAILURE;
      }
    }
    
    
    std::cout << std::endl;
    std::cout << "//" << std::endl;
    std::cout << "////////// Test: Assignments //////////" << std::endl;
    std::cout << "//" << std::endl;

    std::cout << "Testing matrix assigned to range... ";
    ublas_A_sub1 = ublas_B;
    vcl_A_sub1 = vcl_B;
    if (check_for_equality(ublas_A, vcl_A))
      std::cout << "PASSED!" << std::endl;
    else
    {
      std::cout << std::endl << "TEST failed!";
      return EXIT_FAILURE;
    }
    
    std::cout << "Testing range assigned to matrix... ";
    ublas_B = ublas_A_sub2;
    vcl_B = vcl_A_sub2;
    if (check_for_equality(ublas_B, vcl_B))
      std::cout << "PASSED!" << std::endl;
    else
    {
      std::cout << std::endl << "TEST failed!";
      return EXIT_FAILURE;
    }
    
    std::cout << "Testing range assigned to range... ";
    ublas_A_sub1 = ublas_C_sub;
    vcl_A_sub1 = vcl_C_sub;
    if (check_for_equality(ublas_A, vcl_A))
      std::cout << "PASSED!" << std::endl;
    else
    {
      std::cout << std::endl << "TEST failed!";
      return EXIT_FAILURE;
    }
    
    
    
    std::cout << std::endl;
    std::cout << "//" << std::endl;
    std::cout << "////////// Test 1: Copy to GPU //////////" << std::endl;
    std::cout << "//" << std::endl;
    
    ublas_A_sub1 = ublas_B;
    viennacl::copy(ublas_B, vcl_A_sub1);
    std::cout << "Testing upper left copy to A... ";
    if (check_for_equality(ublas_A, vcl_A))
      std::cout << "PASSED!" << std::endl;
    else
    {
      std::cout << std::endl << "TEST failed!";
      return EXIT_FAILURE;
    }
    
    
    ublas_A_sub2 = ublas_B;
    viennacl::copy(ublas_B, vcl_A_sub2);
    std::cout << "Testing lower right copy to A... ";
    if (check_for_equality(ublas_A, vcl_A))
      std::cout << "PASSED!" << std::endl;
    else
    {
      std::cout << std::endl << "TEST failed!";
      return EXIT_FAILURE;
    }
    
    
    
    ublas_C_sub = ublas_B;
    viennacl::copy(ublas_B, vcl_C_sub);
    std::cout << "Testing upper copy to C... ";
    if (check_for_equality(ublas_C, vcl_C))
      std::cout << "PASSED!" << std::endl;
    else
    {
      std::cout << std::endl << "TEST failed!";
      return EXIT_FAILURE;
    }

    
    ublas_D_sub = ublas_B;
    viennacl::copy(ublas_B, vcl_D_sub);
    std::cout << "Testing left copy to D... ";
    if (check_for_equality(ublas_D, vcl_D))
      std::cout << "PASSED!" << std::endl;
    else
      std::cout << std::endl << "TEST failed!";
    
    std::cout << std::endl;
    std::cout << "//" << std::endl;
    std::cout << "////////// Test 2: Copy from GPU //////////" << std::endl;
    std::cout << "//" << std::endl;
    
    std::cout << "Testing upper left copy to A... ";
    if (check_for_equality(ublas_A_sub1, vcl_A_sub1))
      std::cout << "PASSED!" << std::endl;
    else
    {
      std::cout << std::endl << "TEST failed!";
      return EXIT_FAILURE;
    }
    
    std::cout << "Testing lower right copy to A... ";
    if (check_for_equality(ublas_A_sub2, vcl_A_sub2))
      std::cout << "PASSED!" << std::endl;
    else
    {
      std::cout << std::endl << "TEST failed!";
      return EXIT_FAILURE;
    }
    
    std::cout << "Testing upper copy to C... ";
    if (check_for_equality(ublas_C_sub, vcl_C_sub))
      std::cout << "PASSED!" << std::endl;
    else
    {
      std::cout << std::endl << "TEST failed!";
      return EXIT_FAILURE;
    }

    std::cout << "Testing left copy to D... ";
    if (check_for_equality(ublas_D_sub, vcl_D_sub))
      std::cout << "PASSED!" << std::endl;
    else
    {
      std::cout << std::endl << "TEST failed!";
      return EXIT_FAILURE;
    }
    
    
    std::cout << "//" << std::endl;
    std::cout << "////////// Test 3: Addition //////////" << std::endl;
    std::cout << "//" << std::endl;
    viennacl::copy(ublas_A_sub2, vcl_A_sub2);
    
    std::cout << "Inplace add to submatrix: ";
    ublas_A_sub2 += ublas_A_sub2;
    vcl_A_sub2 += vcl_A_sub2;

    if (check_for_equality(ublas_A, vcl_A))
      std::cout << "PASSED!" << std::endl;
    else
    {
      std::cout << std::endl << "TEST failed!";
      return EXIT_FAILURE;
    }

    std::cout << "Inplace add to matrix: ";
    ublas_B += ublas_A_sub2;
    vcl_B += vcl_A_sub2;

    if (check_for_equality(ublas_B, vcl_B))
      std::cout << "PASSED!" << std::endl;
    else
    {
      std::cout << std::endl << "TEST failed!";
      return EXIT_FAILURE;
    }

    std::cout << "Inplace add of matrix: ";
    ublas_A_sub2 += ublas_B;
    vcl_A_sub2 += vcl_B;

    if (check_for_equality(ublas_A, vcl_A))
      std::cout << "PASSED!" << std::endl;
    else
    {
      std::cout << std::endl << "TEST failed!";
      return EXIT_FAILURE;
    }
    
    std::cout << "Add to submatrix: ";
    ublas_A_sub2 = ublas_A_sub2 + ublas_A_sub2;
    vcl_A_sub2 = vcl_A_sub2 + vcl_A_sub2;

    if (check_for_equality(ublas_A, vcl_A))
      std::cout << "PASSED!" << std::endl;
    else
    {
      std::cout << std::endl << "TEST failed!";
      return EXIT_FAILURE;
    }

    std::cout << "Add to matrix: ";
    ublas_B = ublas_A_sub2 + ublas_A_sub2;
    vcl_B = vcl_A_sub2 + vcl_A_sub2;

    if (check_for_equality(ublas_B, vcl_B))
      std::cout << "PASSED!" << std::endl;
    else
    {
      std::cout << std::endl << "TEST failed!";
      return EXIT_FAILURE;
    }
    
    
    
    std::cout << "//" << std::endl;
    std::cout << "////////// Test 4: Subtraction //////////" << std::endl;
    std::cout << "//" << std::endl;
    viennacl::copy(ublas_A_sub2, vcl_A_sub2);
    
    std::cout << "Inplace sub to submatrix: ";
    ublas_A_sub2 -= ublas_A_sub2;
    vcl_A_sub2 -= vcl_A_sub2;

    if (check_for_equality(ublas_A, vcl_A))
      std::cout << "PASSED!" << std::endl;
    else
    {
      std::cout << std::endl << "TEST failed!";
      return EXIT_FAILURE;
    }

    std::cout << "Inplace sub to matrix: ";
    ublas_B -= ublas_A_sub2;
    vcl_B -= vcl_A_sub2;

    if (check_for_equality(ublas_B, vcl_B))
      std::cout << "PASSED!" << std::endl;
    else
    {
      std::cout << std::endl << "TEST failed!";
      return EXIT_FAILURE;
    }
    
    std::cout << "Inplace sub of matrix: ";
    ublas_A_sub2 -= ublas_B;
    vcl_A_sub2 -= vcl_B;

    if (check_for_equality(ublas_A, vcl_A))
      std::cout << "PASSED!" << std::endl;
    else
    {
      std::cout << std::endl << "TEST failed!";
      return EXIT_FAILURE;
    }
    
    std::cout << "Sub from submatrix: ";
    ublas_A_sub2 = ublas_A_sub2 - ublas_A_sub2;
    vcl_A_sub2 = vcl_A_sub2 - vcl_A_sub2;

    if (check_for_equality(ublas_A, vcl_A))
      std::cout << "PASSED!" << std::endl;
    else
    {
      std::cout << std::endl << "TEST failed!";
      return EXIT_FAILURE;
    }

    std::cout << "Sub from matrix: ";
    ublas_B = ublas_A_sub2 - ublas_A_sub2;
    vcl_B = vcl_A_sub2 - vcl_A_sub2;

    if (check_for_equality(ublas_B, vcl_B))
      std::cout << "PASSED!" << std::endl;
    else
    {
      std::cout << std::endl << "TEST failed!";
      return EXIT_FAILURE;
    }
    
    
    std::cout << "//" << std::endl;
    std::cout << "////////// Test 5: Scaling //////////" << std::endl;
    std::cout << "//" << std::endl;
    viennacl::copy(ublas_A, vcl_A);
    
    std::cout << "Multiplication with CPU scalar: ";
    ublas_A_sub2 *= ScalarType(3.1415);
    vcl_A_sub2 *= ScalarType(3.1415);

    if (check_for_equality(ublas_A, vcl_A))
      std::cout << "PASSED!" << std::endl;
    else
    {
      std::cout << std::endl << "TEST failed!";
      return EXIT_FAILURE;
    }

    std::cout << "Multiplication with GPU scalar: ";
    ublas_A_sub2 *= gpu_pi;
    vcl_A_sub2 *= gpu_pi;

    if (check_for_equality(ublas_A, vcl_A))
      std::cout << "PASSED!" << std::endl;
    else
    {
      std::cout << std::endl << "TEST failed!";
      return EXIT_FAILURE;
    }
    
    
    std::cout << "Division with CPU scalar: ";
    ublas_A_sub2 /= ScalarType(3.1415);
    vcl_A_sub2 /= ScalarType(3.1415);

    if (check_for_equality(ublas_A, vcl_A))
      std::cout << "PASSED!" << std::endl;
    else
    {
      std::cout << std::endl << "TEST failed!";
      return EXIT_FAILURE;
    }

    std::cout << "Division with GPU scalar: ";
    ublas_A_sub2 /= gpu_pi;
    vcl_A_sub2 /= gpu_pi;

    if (check_for_equality(ublas_A, vcl_A))
      std::cout << "PASSED!" << std::endl;
    else
    {
      std::cout << std::endl << "TEST failed!";
      return EXIT_FAILURE;
    }
    
    

    std::cout << "//" << std::endl;
    std::cout << "////////// Test 6: Matrix-Matrix Products //////////" << std::endl;
    std::cout << "//" << std::endl;

    std::cout << "Assigned C = A * B: ";
    ublas_A_sub1 = prod(ublas_C_sub, ublas_D_sub);
    vcl_A_sub1 = viennacl::linalg::prod(vcl_C_sub, vcl_D_sub);

    if (check_for_equality(ublas_A, vcl_A))
      std::cout << "PASSED!" << std::endl;
    else
    {
      std::cout << std::endl << "TEST failed!";
      return EXIT_FAILURE;
    }

    std::cout << "Assigned C = A^T * B: ";
    ublas_A_sub1 = prod(trans(ublas_C_sub), ublas_D_sub);
    vcl_A_sub1 = viennacl::linalg::prod(trans(vcl_C_sub), vcl_D_sub);

    if (check_for_equality(ublas_A, vcl_A))
      std::cout << "PASSED!" << std::endl;
    else
    {
      std::cout << std::endl << "TEST failed!";
      return EXIT_FAILURE;
    }

    std::cout << "Assigned C = A * B^T: ";
    ublas_A_sub1 = prod(ublas_C_sub, trans(ublas_D_sub));
    vcl_A_sub1 = viennacl::linalg::prod(vcl_C_sub, trans(vcl_D_sub));

    if (check_for_equality(ublas_A, vcl_A))
      std::cout << "PASSED!" << std::endl;
    else
    {
      std::cout << std::endl << "TEST failed!";
      return EXIT_FAILURE;
    }

    std::cout << "Assigned C = A^T * B^T: ";
    ublas_A_sub1 = prod(trans(ublas_C_sub), trans(ublas_D_sub));
    vcl_A_sub1 = viennacl::linalg::prod(trans(vcl_C_sub), trans(vcl_D_sub));

    if (check_for_equality(ublas_A, vcl_A))
      std::cout << "PASSED!" << std::endl;
    else
    {
      std::cout << std::endl << "TEST failed!";
      return EXIT_FAILURE;
    }

    std::cout << "Inplace add of prod(): ";
    ublas_A_sub1 += prod(ublas_C_sub, ublas_D_sub);
    vcl_A_sub1 += viennacl::linalg::prod(vcl_C_sub, vcl_D_sub);

    if (check_for_equality(ublas_A, vcl_A))
      std::cout << "PASSED!" << std::endl;
    else
    {
      std::cout << std::endl << "TEST failed!";
      return EXIT_FAILURE;
    }


    std::cout << "//" << std::endl;
    std::cout << "////////// Test 7: Matrix-Vector Products //////////" << std::endl;
    std::cout << "//" << std::endl;

    VectorType ublas_v1(dim_large);
    for (std::size_t i=0; i<ublas_v1.size(); ++i)
      ublas_v1(i) = i;
    boost::numeric::ublas::vector_range<VectorType> ublas_v1_sub(ublas_v1, ublas_r1);

    VectorType ublas_v2(dim_large);
    for (std::size_t i=0; i<ublas_v2.size(); ++i)
      ublas_v2(i) = i - 5;
    boost::numeric::ublas::vector_range<VectorType> ublas_v2_sub(ublas_v2, ublas_r1);

    
    VCLVectorType vcl_v1(ublas_v1.size());
    viennacl::vector_range<VCLVectorType> vcl_v1_sub(vcl_v1, vcl_r1);
    VCLVectorType vcl_v2(ublas_v2.size());
    viennacl::vector_range<VCLVectorType> vcl_v2_sub(vcl_v2, vcl_r1);
    viennacl::copy(ublas_v1, vcl_v1);
    viennacl::copy(ublas_v2, vcl_v2);
    viennacl::copy(ublas_A_sub1, vcl_A_sub1);
    
    
    ublas_v2_sub = prod(ublas_A_sub1, ublas_v1_sub);
    vcl_v2_sub = viennacl::linalg::prod(vcl_A_sub1, vcl_v1_sub);

    if (check_for_equality_vector(ublas_v2, vcl_v2))
      std::cout << "PASSED!" << std::endl;
    else
    {
      std::cout << std::endl << "TEST failed!";
      return EXIT_FAILURE;
    }

    std::cout << std::endl;
    std::cout << "----------------------------------------------" << std::endl;
    std::cout << std::endl;


    return EXIT_SUCCESS;
}    
Beispiel #19
0
void random_fill(std::vector<ScalarType>& in)
{
  for (std::size_t i = 0; i < in.size(); i++)
    in[i] = static_cast<ScalarType>(rand()) / ScalarType(RAND_MAX);
}
ScalarType diff(ScalarType const & s1, ScalarType const & s2)
{
  if (s1 > s2 || s1 < s2)
    return (s1 - s2) / std::max(std::fabs(s1), std::fabs(s2));
  return ScalarType(0);
}
Beispiel #21
0
/*!
This method initializes a mitk::LevelWindow from an mitk::Image. The algorithm is as follows:

Default to taking the central image slice for quick analysis.

Compute the smallest (minValue), second smallest (min2ndValue), second largest (max2ndValue), and
largest (maxValue) data value by traversing the pixel values only once. In the
same scan it also computes the count of minValue values and maxValue values.
After that a basic histogram with specific information about the
extrems is complete.

If minValue == maxValue, the center slice is uniform and the above scan is repeated for
the complete image, not just one slice

Next, special cases of images with only 1, 2 or 3 distinct data values
have hand assigned level window ranges.

Next the level window is set relative to the inner range IR = lengthOf([min2ndValue, max2ndValue])

For count(minValue) > 20% the smallest values are frequent and should be
distinct from the min2ndValue and larger values (minValue may be std:min, may signify
something special) hence the lower end of the level window is set to min2ndValue - 0.5 * IR

For count(minValue) <= 20% the smallest values are not so important and can
blend with the next ones => min(level window) = min2ndValue

And analog for max(level window):
count(max2ndValue) > 20%:  max(level window) = max2ndValue + 0.5 * IR
count(max2ndValue) < 20%:  max(level window) = max2ndValue

In both 20%+ cases the level window bounds are clamped to the [minValue, maxValue] range

In consequence the level window maximizes contrast with minimal amount of
computation and does do useful things if the data contains std::min or
std:max values or has only 1 or 2 or 3 data values.
*/
void mitk::LevelWindow::SetAuto(const mitk::Image* image, bool /*tryPicTags*/, bool guessByCentralSlice,
                                unsigned selectedComponent)
{
    if ( IsFixed() )
        return;

    if ( image == nullptr || !image->IsInitialized() ) return;

    if ((image->GetPixelType().GetComponentType() == 9) || (image->GetPixelType().GetComponentType() == 10))
    {
        // Floating image
        m_IsFloatingImage = true;
    } else
    {
        m_IsFloatingImage = false;
    }

    const mitk::Image* wholeImage = image;
    ScalarType minValue = 0.0;
    ScalarType maxValue = 0.0;
    ScalarType min2ndValue = 0.0;
    ScalarType max2ndValue = 0.0;
    mitk::ImageSliceSelector::Pointer sliceSelector = mitk::ImageSliceSelector::New();
    if ( guessByCentralSlice )
    {
        sliceSelector->SetInput(image);
        sliceSelector->SetSliceNr(image->GetDimension(2)/2);
        sliceSelector->SetTimeNr(image->GetDimension(3)/2);
        sliceSelector->SetChannelNr(image->GetDimension(4)/2);
        sliceSelector->Update();
        image = sliceSelector->GetOutput();
        if ( image == nullptr || !image->IsInitialized() ) return;

        minValue    = image->GetStatistics()->GetScalarValueMin(0, selectedComponent);
        maxValue    = image->GetStatistics()->GetScalarValueMaxNoRecompute();
        min2ndValue = image->GetStatistics()->GetScalarValue2ndMinNoRecompute();
        max2ndValue = image->GetStatistics()->GetScalarValue2ndMaxNoRecompute();
        if ( minValue == maxValue )
        {
            // guessByCentralSlice seems to have failed, lets look at all data
            image       = wholeImage;
            minValue    = image->GetStatistics()->GetScalarValueMin(0, selectedComponent);
            maxValue    = image->GetStatistics()->GetScalarValueMaxNoRecompute();
            min2ndValue = image->GetStatistics()->GetScalarValue2ndMinNoRecompute();
            max2ndValue = image->GetStatistics()->GetScalarValue2ndMaxNoRecompute();
        }
    }
    else
    {
        const_cast<Image*>(image)->Update();
        minValue    = image->GetStatistics()->GetScalarValueMin(0, selectedComponent);
        maxValue    = image->GetStatistics()->GetScalarValueMaxNoRecompute(0);
        min2ndValue = image->GetStatistics()->GetScalarValue2ndMinNoRecompute(0);
        max2ndValue = image->GetStatistics()->GetScalarValue2ndMaxNoRecompute(0);
        for (unsigned int i = 1; i < image->GetDimension(3); ++i)
        {
            ScalarType minValueTemp = image->GetStatistics()->GetScalarValueMin(i, selectedComponent);
            if (minValue > minValueTemp)
                minValue    = minValueTemp;
            ScalarType maxValueTemp = image->GetStatistics()->GetScalarValueMaxNoRecompute(i);
            if (maxValue < maxValueTemp)
                maxValue = maxValueTemp;
            ScalarType min2ndValueTemp = image->GetStatistics()->GetScalarValue2ndMinNoRecompute(i);
            if (min2ndValue > min2ndValueTemp)
                min2ndValue = min2ndValueTemp;
            ScalarType max2ndValueTemp = image->GetStatistics()->GetScalarValue2ndMaxNoRecompute(i);
            if (max2ndValue > max2ndValueTemp)
                max2ndValue = max2ndValueTemp;
        }
    }

    // Fix for bug# 344 Level Window wird bei Eris Cut bildern nicht richtig gesetzt
    if (   image->GetPixelType().GetPixelType()==itk::ImageIOBase::SCALAR
            && image->GetPixelType().GetComponentType() == itk::ImageIOBase::INT
            && image->GetPixelType().GetBpe() >= 8)
    {
        // the windows compiler complains about ambiguos 'pow' call, therefore static casting to (double, int)
        if (minValue == -( pow( (double) 2.0, static_cast<int>(image->GetPixelType().GetBpe()/2) ) ) )
        {
            minValue = min2ndValue;
        }
    }
    // End fix

    //// uniform image
    if ( minValue == maxValue )
    {
        minValue = maxValue-1;
    }
    else
    {
        //Due to bug #8690 level window now is no longer of fixed range by default but the range adapts according to levelwindow interaction
        //This is done because the range should be a little bit larger from the beginning so that the scale doesn't start to resize right from the beginning
        double additionalRange = 0.15*(maxValue-minValue);
        minValue -= additionalRange;
        maxValue += additionalRange;
    }
    SetRangeMinMax(minValue, maxValue);
    SetDefaultBoundaries(minValue, maxValue);
    /*
    if ( tryPicTags ) // level and window will be set by informations provided directly by the mitkIpPicDescriptor
    {
    if ( SetAutoByPicTags(const_cast<Image*>(image)->GetPic()) )
    {
    return;
    }
    }
    */

    unsigned int numPixelsInDataset = image->GetDimensions()[0];
    for ( unsigned int k=0;  k<image->GetDimension();  ++k ) numPixelsInDataset *= image->GetDimensions()[k];
    unsigned int minCount = image->GetStatistics()->GetCountOfMinValuedVoxelsNoRecompute();
    unsigned int maxCount = image->GetStatistics()->GetCountOfMaxValuedVoxelsNoRecompute();
    ScalarType minCountFraction = minCount/ScalarType(numPixelsInDataset);
    ScalarType maxCountFraction = maxCount/ScalarType(numPixelsInDataset);

    //// binary image
    if ( min2ndValue == maxValue )
    {
        // noop; full range is fine
    }

    //// triple value image, put middle value in center of gray level ramp
    else if ( min2ndValue == max2ndValue )
    {
        ScalarType minDelta = std::min(min2ndValue-minValue, maxValue-min2ndValue);
        minValue = min2ndValue - minDelta;
        maxValue = min2ndValue + minDelta;
    }

    // now we can assume more than three distict scalar values
    else
    {
        ScalarType innerRange = max2ndValue - min2ndValue;

        if ( minCountFraction > 0.2 )  //// lots of min values -> make different from rest, but not miles away
        {
            ScalarType halfInnerRangeGapMinValue = min2ndValue - innerRange/2.0;
            minValue = std::max(minValue, halfInnerRangeGapMinValue);
        }
        else  //// few min values -> focus on innerRange
        {
            minValue = min2ndValue;
        }

        if ( maxCountFraction > 0.2 )  //// lots of max values -> make different from rest
        {
            ScalarType halfInnerRangeGapMaxValue = max2ndValue + innerRange/2.0;
            maxValue = std::min(maxValue, halfInnerRangeGapMaxValue);
        }
        else  //// few max values -> focus on innerRange
        {
            maxValue = max2ndValue;
        }
    }
    SetWindowBounds(minValue, maxValue);
    SetDefaultLevelWindow((maxValue - minValue) / 2 + minValue, maxValue - minValue);
}
Beispiel #22
0
    viennacl::vector<ScalarType> solve(MatrixType const & A, //MatrixType const & A,
                                       viennacl::vector<ScalarType> const & rhs,
                                       bicgstab_tag const & tag,
                                       viennacl::linalg::no_precond)
    {
      viennacl::vector<ScalarType> result = viennacl::zero_vector<ScalarType>(rhs.size(), viennacl::traits::context(rhs));

      viennacl::vector<ScalarType> residual = rhs;
      viennacl::vector<ScalarType> p = rhs;
      viennacl::vector<ScalarType> r0star = rhs;
      viennacl::vector<ScalarType> Ap = rhs;
      viennacl::vector<ScalarType> s  = rhs;
      viennacl::vector<ScalarType> As = rhs;

      // Layout of temporary buffer:
      //  chunk 0: <residual, r_0^*>
      //  chunk 1: <As, As>
      //  chunk 2: <As, s>
      //  chunk 3: <Ap, r_0^*>
      //  chunk 4: <As, r_0^*>
      //  chunk 5: <s, s>
      vcl_size_t buffer_size_per_vector = 256;
      vcl_size_t num_buffer_chunks = 6;
      viennacl::vector<ScalarType> inner_prod_buffer = viennacl::zero_vector<ScalarType>(num_buffer_chunks*buffer_size_per_vector, viennacl::traits::context(rhs)); // temporary buffer
      std::vector<ScalarType>      host_inner_prod_buffer(inner_prod_buffer.size());

      ScalarType norm_rhs_host = viennacl::linalg::norm_2(residual);
      ScalarType beta;
      ScalarType alpha;
      ScalarType omega;
      ScalarType residual_norm = norm_rhs_host;
      inner_prod_buffer[0] = norm_rhs_host * norm_rhs_host;

      ScalarType  r_dot_r0 = 0;
      ScalarType As_dot_As = 0;
      ScalarType As_dot_s  = 0;
      ScalarType Ap_dot_r0 = 0;
      ScalarType As_dot_r0 = 0;
      ScalarType  s_dot_s  = 0;

      if (norm_rhs_host <= 0) //solution is zero if RHS norm is zero
        return result;

      for (vcl_size_t i = 0; i < tag.max_iterations(); ++i)
      {
        tag.iters(i+1);
        // Ap = A*p_j
        // Ap_dot_r0 = <Ap, r_0^*>
        viennacl::linalg::pipelined_bicgstab_prod(A, p, Ap, r0star,
                                                  inner_prod_buffer, buffer_size_per_vector, 3*buffer_size_per_vector);

        //////// first (weak) synchronization point ////

        ///// method 1: compute alpha on host:
        //
        //// we only need the second chunk of the buffer for computing Ap_dot_r0:
        //viennacl::fast_copy(inner_prod_buffer.begin(), inner_prod_buffer.end(), host_inner_prod_buffer.begin());
        //Ap_dot_r0 = std::accumulate(host_inner_prod_buffer.begin() +     buffer_size_per_vector, host_inner_prod_buffer.begin() + 2 * buffer_size_per_vector, ScalarType(0));

        //alpha = residual_dot_r0 / Ap_dot_r0;

        //// s_j = r_j - alpha_j q_j
        //s = residual - alpha * Ap;

        ///// method 2: compute alpha on device:
        // s = r - alpha * Ap
        // <s, s> first stage
        // dump alpha at end of inner_prod_buffer
        viennacl::linalg::pipelined_bicgstab_update_s(s, residual, Ap,
                                                      inner_prod_buffer, buffer_size_per_vector, 5*buffer_size_per_vector);

        // As = A*s_j
        // As_dot_As = <As, As>
        // As_dot_s  = <As, s>
        // As_dot_r0 = <As, r_0^*>
        viennacl::linalg::pipelined_bicgstab_prod(A, s, As, r0star,
                                                  inner_prod_buffer, buffer_size_per_vector, 4*buffer_size_per_vector);

        //////// second (strong) synchronization point ////

        viennacl::fast_copy(inner_prod_buffer.begin(), inner_prod_buffer.end(), host_inner_prod_buffer.begin());

         r_dot_r0 = std::accumulate(host_inner_prod_buffer.begin(),                              host_inner_prod_buffer.begin() +     buffer_size_per_vector, ScalarType(0));
        As_dot_As = std::accumulate(host_inner_prod_buffer.begin() +     buffer_size_per_vector, host_inner_prod_buffer.begin() + 2 * buffer_size_per_vector, ScalarType(0));
        As_dot_s  = std::accumulate(host_inner_prod_buffer.begin() + 2 * buffer_size_per_vector, host_inner_prod_buffer.begin() + 3 * buffer_size_per_vector, ScalarType(0));
        Ap_dot_r0 = std::accumulate(host_inner_prod_buffer.begin() + 3 * buffer_size_per_vector, host_inner_prod_buffer.begin() + 4 * buffer_size_per_vector, ScalarType(0));
        As_dot_r0 = std::accumulate(host_inner_prod_buffer.begin() + 4 * buffer_size_per_vector, host_inner_prod_buffer.begin() + 5 * buffer_size_per_vector, ScalarType(0));
         s_dot_s  = std::accumulate(host_inner_prod_buffer.begin() + 5 * buffer_size_per_vector, host_inner_prod_buffer.begin() + 6 * buffer_size_per_vector, ScalarType(0));

        alpha =         r_dot_r0 / Ap_dot_r0;
        beta  = -1.0 * As_dot_r0 / Ap_dot_r0;
        omega =        As_dot_s  / As_dot_As;

        residual_norm = std::sqrt(s_dot_s - 2.0 * omega * As_dot_s + omega * omega *  As_dot_As);
        if (std::fabs(residual_norm / norm_rhs_host) < tag.tolerance())
          break;

        // x_{j+1} = x_j + alpha * p_j + omega * s_j
        // r_{j+1} = s_j - omega * t_j
        // p_{j+1} = r_{j+1} + beta * (p_j - omega * q_j)
        // and compute first stage of r_dot_r0 = <r_{j+1}, r_o^*> for use in next iteration
         viennacl::linalg::pipelined_bicgstab_vector_update(result, alpha, p, omega, s,
                                                            residual, As,
                                                            beta, Ap,
                                                            r0star, inner_prod_buffer, buffer_size_per_vector);
      }

      //store last error estimate:
      tag.error(residual_norm / norm_rhs_host);

      return result;
    }
Beispiel #23
0
 ScalarType getScalarType(const string& pvalue) {
     for(size_t i=0; i<NELEMENTS(names); i++)
         if(pvalue==names[i])
             return ScalarType(i);
     THROW_EXCEPTION2(std::invalid_argument, "error unknown ScalarType");
 }
Beispiel #24
0
void OdeNum<MatrixType>::diagBadEnlcosure(const VectorType &x) const
{
  std::ofstream raport("raport.ecl",std::ios::app);
  raport << "-------------------------\n";
  raport << "x=" << x << "\n";
  raport << "step=" << step << "\n";
  raport << "f(x)=" << ode.f(x) << "\n";

  int dim = x.dimension();
  ScalarType trial_step = ScalarType(-0.2,1.2) * step;

  int diag=0;
  VectorType max_r = trial_step*ode.f(x);
  VectorType Ye = x+max_r;

  raport << "Ye=" << Ye << "\n";
  raport << "diam Ye=" ;
  for(int i=0; i<dim; i++)
    raport << rightBound(capd::abs(diam(Ye[i]))) << "  " ;
  raport << "\n";


  MatrixType dv = ode.df(x+max_r);
  capd::vectalg::MaxNorm<VectorType,MatrixType> maxN;
  ScalarType L = maxN(dv);

  if (!(step*L < 1))
  {
    diag=1;
    raport << "hL > 1 \n";
  }
  raport << "hL=" << step*L << "\n";

  Real *diamvect = new Real[dim];
  Real *dfabs = new Real[dim*dim];
  if (!diag)
  {
    int i;

    for(i=0;i<dim; i++)
      diamvect[i] = rightBound(capd::abs(diam(Ye[i])));

    for(i=0;i<dim; i++)
      for(int j=0; j<dim; j++)
        dfabs[i*dim+j]= rightBound(capd::abs(dv[i][j]));

    for(i=0;i<dim;i++)
    {
      Real t=0;
      for(int j=0;j<dim;j++)
        t += dfabs[i*dim+j]*diamvect[j];
      t *= 2.5* rightBound(capd::abs(step));
      if(t > diamvect[i])
      {
        diag=2;
        raport << "5/2 h Dfi/Dxj diam(Ye_j)=t" << t <<
           " >  diam(Ye_i)= " << diamvect[i] <<
           "  ; i=" << i << "\n";
      }
    }// i-loop
  } // if(!diag)

  if(!diag)
  {
    diag=3;
    raport << "unknown reason \n" ;
  }
  delete [] diamvect;
  delete [] dfabs;
}