Menu

Deduction of optimal return type

mmoeller
2016-04-07
2016-04-19
  • mmoeller

    mmoeller - 2016-04-07

    Dear all,

    is it possible to deduce the optimal return type from a vector or matrix expression?

    Please consider the following demo code:

    viennacl::vector<double> v1(20),v2(20);
    
    // Create expression (not the vector)
    auto tmp v1+v2;
    
    // Create new vector from the expression
    viennacl::vector<double> a(tmp);
    

    In my application,I need to specialize some of my routines for ViennaCL, and therefore, I need to know
    EITHER: a base class of all vector/matrix expressions so that I can test against std::is_base_of<???,Expr>::value
    OR: a common type/attribute of all vector/matrix expressions, so that I can specialize using the presence of that type/attribute
    AND: the optimal return type (here: vector and double)

    Best regads,
    Matthias

     
  • Karl Rupp

    Karl Rupp - 2016-04-08

    Hi,

    the following is for ViennaCL 1.x.y:
    For vector expressions (including operations where the operands may not be vectors, but the result is a vector) such as v1 + v2, the return type is viennacl::vector_expression<LHS, RHS, OP>
    For matrix expressions the respective type is viennacl::matrix_expression<LHS, RHS, OP>.
    In a few cases there are also scalar expressions used (e.g. dot products), where the return type is viennacl::scalar_expression<LHS, RHS, OP>.

    The common base class for vectors and vector proxy objects is viennacl::vector_base<T>, for dense matrices it is viennacl::matrix_base<T>. Currently there is no common type for sparse matrices, but this may change.

    ViennaCL 2.0.0 with see a complete redesign (second half of 2016), so don't expect these types to be still in use then.

    Best regards,
    Karli

     
    • mmoeller

      mmoeller - 2016-04-18

      Hi,

      thank you very much for this information. What I would need is a mechanism to deduce the return type from an expression, e.g.

      viennacl::vector<double> v1(20),v2(20);
      auto tmp v1+v2;
      
      typedef viennacl::return_type<typename decltype(tmp)> return_type;
      return_type a(tmp);
      

      It would also suffice if I can extract the type (matrix/vector) and the data type (float/double) separately.

      Best regards,
      Matthias

       
  • Karl Rupp

    Karl Rupp - 2016-04-19

    Hi,

    the return type deduction (following your definition of a return type above) is as follows: For vector_expression<> it is always a vector<>, for matrix_expression it is always a matrix<>. Same for scalar_expression and scalar<>.

    You can obtain the data type via

    typedef viennacl::result_of::cpu_scalar_type<T>::type   data_type;
    

    (I think this is what you were asking for. Please let me know if that does not answer your question.)

    Best regards,
    Karli

     

Log in to post a comment.