Menu

copy C++ vector to ublas compressed_matrix

2013-09-02
2013-09-02
  • Matthew Musto

    Matthew Musto - 2013-09-02

    I was able to implement the following tutorial using ublas and viennacl for a few different iterative solvers:

    https://github.com/viennacl/viennacl-dev/blob/master/examples/tutorial/iterative-ublas.cpp

    My issue arose when trying to pass vectors calculated in C++ for the matrix.

    // Global Variables
    vector< vector<float> > Jacobian(0, vector<float>(0)); //Jacobian matrix
    vector<float> delta_PQ; //rhs

    //
    // Set up some ublas objects
    //
    ublas::vector<ScalarType> rhs;
    ublas::vector<ScalarType> result;
    ublas::compressed_matrix<ScalarType> ublas_matrix;
    using namespace boost::numeric;

    typedef float ScalarType;

    // Resize RHS from main program
    resize_vector(rhs2, j_dimension);
    ublas_matrix2.resize(j_dimension, j_dimension);

    //copy content to GPU vector (recommended initialization)
    copy(delta_PQ.begin(), delta_PQ.end(), rhs.begin()); //works
    copy(Jacobian.begin(), Jacobian.end(), ublas_matrix); //won't compile

    The last line throws an error. I tried using this form (below) as well but would need to convert the ublas::compressed_matrix to a viennacl::compressed_matrix.

    copy(Jacobian, ublas_matrix);

    Doing that results in a bunch of compile errors which I have another discussion topic open about. I have also posted on stackoverflow in hopes someone there will be able to help.

    http://stackoverflow.com/questions/18564460/conversion-from-stdvector-to-ublascompressed-matrix-in-viennacl

    Thanks in advance!

     
  • Karl Rupp

    Karl Rupp - 2013-09-02

    Hi,
    what are you trying to achieve? Please include the namespaces in the types. Is vector from std:: or from viennacl::? Regardless, if you're using vector<vector<T> >, this is always a dense data storage, whereas compressed_matrix<> is intended for sparse matrices. You usually don't want to mix the two.
    Best regards,
    Karli

     
  • Matthew Musto

    Matthew Musto - 2013-09-02

    Karl,

    Thank you for the quick reply. The vector is using namespace std::. I presently have a program which computes a sparse matrix and stores is in the form:

    vector< vector<float> > Jacobian(0, vector<float>(0)); //Jacobian

    This matrix is presently about 90% zeros however, I have not been able to get it to fit any standard form such as diagonal, yet. Pre-processing of the model files to produce a cleaner and more usable matrix is a future phase of my project.

    I am attempting to use the iterative solvers in viennacl from your tutorial below to place the precomputed jacobian into the ublas matrix which is of type ublas::compressed_matrix. I already have the rhs using the function copy(delta_PQ.begin(), delta_PQ.end(), rhs.begin());

    https://github.com/viennacl/viennacl-dev/blob/master/examples/tutorial/iterative-ublas.cpp

    Would you recommend using the ublas compressed matrix from the start and then using my C++ program to populate it? If so, what would the ublas::compressed_matrix equivalent of Jacobian[0][0] = 54.28f; be?

    Thanks again!
    -Matt

     
  • Karl Rupp

    Karl Rupp - 2013-09-02

    vector< vector> is not an efficient sparse matrix format, it is in almost all use cases a dense vector format. Use vector< map > instead (it has the same [i][j] bracket acess), and you can use
    viennacl::copy(Jacobian, viennacl_compressed_matrix_object);
    directly. As an alternative, fill the ublas-matrix directly via operator(), i.e.
    ublas_matrix(1,1) = value1;
    ublas_matrix(7,8) = value2;
    etc. Depending on the order of the values, filling ublas_matrix directly may be slower or faster. As a rule of thumb, vector< map > is faster whenever the entries are written in a 'random' fashion, whereas ublas_matrix is faster if you fill row and column entries in consecutive order (and eventually supply the number of nonzero entries to the matrix constructor upfront).

    Hope that helps :-)

    Best regards,
    Karli

     
  • Matthew Musto

    Matthew Musto - 2013-09-02

    It does and I will let you know how I make out! Thank you very much, you guys are going a phenomenal job!

     
  • Matthew Musto

    Matthew Musto - 2013-09-02

    Works like a charm. Thank you Karli! I am hopeful the bug or my misconfiguration with using the viennacl library as opposed to the boost ublas will be fix for VS2013 and AMD APP 2.8 soon so I can start harnessing the gpu for these calculations.

     

Log in to post a comment.