Conversion from std::vector to ublas::compressed_matrix in ViennaCL

921 views Asked by At

I'm looking to do some calculations and pass the resultant Jacobian NxN matrix and a right hand side vector(n) to boost's ublas and eventually ViennaCL.

The vector was no issue using copy(), however, the matrix is proving to be difficult. Any help would be greatly appreciated

// 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

I have tried numerous variations and looked at the documentation:

http://ublas.sourceforge.net/refdoc/classboost_1_1numeric_1_1ublas_1_1compressed__matrix.html

Also, ViennaCL's example does not work for me:

http://viennacl.sourceforge.net/viennacl-examples-sparse-matrix.html

After a few hours of googling I decided to post on here in hopes someone else can crack it and it will be easier to find for the next person.

1

There are 1 answers

0
matusi143 On BEST ANSWER

To close the loop on this I wanted to let everyone know what I did to solve my problem. Special thanks to Karl Rupp over at the ViennaCL project.

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 than copy. 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).