首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用CUSP打印矩阵的csr格式

使用CUSP打印矩阵的csr格式
EN

Stack Overflow用户
提问于 2015-06-07 10:53:01
回答 1查看 338关注 0票数 -1

我正在尝试使用CUSP和CUDA将稀疏矩阵转换为csr格式。我稍微修改了CUSP文档中显示的代码:

代码语言:javascript
复制
#include <cusp/array2d.h>
#include <cusp/coo_matrix.h>
#include <cusp/csr_matrix.h>
#include <cusp/print.h>
int main(void)
{
// create a simple example
cusp::array2d<float, cusp::host_memory> A(3,4);
A(0,0) = 10;  A(0,1) =  0;  A(0,2) = 20;  A(0,3) =  0;
A(1,0) =  0;  A(1,1) = 30;  A(1,2) =  0;  A(1,3) = 40;
A(2,0) = 50;  A(2,1) = 60;  A(2,2) = 70;  A(2,3) = 80;
print(A);
// save A to disk in MatrixMarket format
cusp::io::write_matrix_market_file(A, "A.mtx");
// load A from disk into a coo_matrix
cusp::csr_matrix<int, float, cusp::device_memory> B;
cusp::io::read_matrix_market_file(B, "A.mtx");

cusp::io::write_matrix_market_file(B,"B.mtx");
// print B
cusp::print(B);
return 0;
}

但我得到的结果是coo格式的矩阵,索引移位了1:

代码语言:javascript
复制
%%MatrixMarket matrix coordinate real general
3   4   8
1 1 10
1 3 20
2 2 30
2 4 40
3 1 50
3 2 60
3 3 70
3 4 80

有什么帮助吗?

谢谢。

EN

回答 1

Stack Overflow用户

发布于 2015-06-10 12:12:32

感谢您的回复!事实上,我不得不手工编写csr格式,尽管这并不像我最初想象的那么难。我的代码结果如下:

代码语言:javascript
复制
// Imprimimos la matriz en formato csr:
cout<<"\n=======================\n";
cout<<"Matriz de adyacencia en formato CSR:\n";
cout<<"Row-offset   :";
for (int j=0 ; j<10 ; j++) cout<<B.row_offsets[j]<<" ";
cout<<"\n"; cout<<"Column index :";
for (int j=0 ; j<17 ; j++) cout<<B.column_indices[j]<<" ";
cout<<"\n"; cout<<"Values:";
for (int j=0 ; j<17 ; j++) cout<<B.values[j]<<" ";   
cout<<"\n==================\n";
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30689612

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档