有多种方法可以实现这一点。例如,如果您希望从原始设备数据表示而不是尖点数据表示开始,您可以使用尖点视图功能中的方法。
如果您已经有 cusp 数据,并且想要转换为原始数据表示,我们可以使用 cusp 构建在推力之上的事实。这是一个完整的示例:
$ 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 for (4,3) matrix with 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]);
// A now represents the 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> with 6 entries
0 0 10
0 2 20
2 2 30
3 0 40
3 1 50
3 2 60
sparse matrix <4, 3> with 6 entries
0 0 40
0 2 50
2 2 60
3 0 10
3 1 20
3 2 30
========= ERROR SUMMARY: 0 errors
$