c++ - How to get raw pointer from cusp library matrix format -
i need raw pointer cusp library matrix format. example:
cusp::coo_matrix<int,double,cusp::device_memory> a(3,3,4); a.values[0] = 1; a.row_indices[0] = 0; a.column_indices[0]= 1; a.values[1] = 2; a.row_indices[1] = 1; a.column_indices[1]= 0; a.values[2] = 3; a.row_indices[2] = 1; a.column_indices[2]= 1; a.values[3] = 4; a.row_indices[3] = 2; a.column_indices[3]= 2;
how can raw pointer row_indices, column_indices , values arrays? need pass them kernels , i'd avoid unnecesary data copying if possible.
there multiple ways accomplish this. example, if wish start raw device data representation instead of cusp data representation, use methodology in cusp views functionality.
if have cusp data already, , want convert raw data representation, can use fact cusp built on top of thrust. here's worked example:
$ cat t346.cu #include <cusp/coo_matrix.h> #include <cusp/print.h> template <typename t> __global__ void my_swap_kernel(t *a, t *b, unsigned size){ int idx = threadidx.x+blockdim.x*blockidx.x; if (idx < size){ t temp = b[idx]; b[idx] = a[idx]; a[idx] = temp;} } int main(void) { // allocate storage (4,3) matrix 6 nonzeros cusp::coo_matrix<int,float,cusp::device_memory> a(4,3,6); // initialize matrix entries on host a.row_indices[0] = 0; a.column_indices[0] = 0; a.values[0] = 10; a.row_indices[1] = 0; a.column_indices[1] = 2; a.values[1] = 20; a.row_indices[2] = 2; a.column_indices[2] = 2; a.values[2] = 30; a.row_indices[3] = 3; a.column_indices[3] = 0; a.values[3] = 40; a.row_indices[4] = 3; a.column_indices[4] = 1; a.values[4] = 50; a.row_indices[5] = 3; a.column_indices[5] = 2; a.values[5] = 60; float *val0 = thrust::raw_pointer_cast(&a.values[0]); float *val3 = thrust::raw_pointer_cast(&a.values[3]); // represents following matrix // [10 0 20] // [ 0 0 0] // [ 0 0 30] // [40 50 60] // print matrix entries cusp::print(a); my_swap_kernel<<<1,3>>>(val0, val3, 3); cusp::print(a); return 0; } $ nvcc -arch=sm_20 -o t346 t346.cu $ cuda-memcheck ./t346 ========= cuda-memcheck sparse matrix <4, 3> 6 entries 0 0 10 0 2 20 2 2 30 3 0 40 3 1 50 3 2 60 sparse matrix <4, 3> 6 entries 0 0 40 0 2 50 2 2 60 3 0 10 3 1 20 3 2 30 ========= error summary: 0 errors $
Comments
Post a Comment