如何在Chapel中的稀疏矩阵中迭代非零 [英] How to iterate non-zeroes in a sparse matrix in Chapel

查看:102
本文介绍了如何在Chapel中的稀疏矩阵中迭代非零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我还有一个矩阵A仍然徘徊.它是大型,稀疏且对称的.我创建了一个名为spDom的稀疏域,其中包含非零条目.现在,我要遍历行r并在其中找到非零条目以及索引.我的目标是建立另一个域,该域本质上是行r的非零值.

I have a matrix A still hanging around. It's large, sparse and new symmetric. I've created a sparse domain called spDom that contains the non-zero entries. Now, I want to iterate along row r and find the non-zero entries there, along with the index. My goal is to build another domain that is essentially row r's non-zeroes.

推荐答案

只要您愿意以CSR格式存储稀疏域/数组,以下答案将适用于Chapel 1.15:

Here's an answer that will work with Chapel 1.15 as long as you're willing to store your sparse domain/array in CSR format:

首先,出于演示目的,我将建立我的(小型非对称)稀疏矩阵:

First, I'll establish my (small, non-symmetric) sparse matrix for demonstration purposes:

use LayoutCS;                               // use the CSR/CSC layout module

config const n = 10;                        // declare problem size
const D = {1..n, 1..n};                     // declare dense domain                                  
var SD: sparse subdomain(D) dmapped CS();   // declare sparse subdomain                 

// populate sparse domain with some indices                                                       
SD += (1,1);
SD += (1,n/2);
SD += (2, n/4);
SD += (2, 3*n/4);
SD += (n/2, 1);
SD += (n/2, n);

var A: [SD] real;                          // declare sparse array                                  

forall (i,j) in SD do                      // initialize sparse array values                       
  A[i,j] = i + j/10.0;

我的解决方案依赖于稀疏CS *域上未公开的迭代器,该域名为dimIter(),可用于遍历连续存储在内存中的维(因此CSR为行,CSC为列). dimIter()带有两个参数:要迭代的维(1 =行,2 =列)和另一个维中的索引.因此,要遍历上面定义的行,我可以这样做:

My solution relies on an undocumented iterator on sparse CS* domains named dimIter() which can be used to iterate over the dimension that's stored consecutively in memory (so rows for CSR and columns for CSC). dimIter() takes two arguments: the dimension to iterate over (1=rows, 2=columns) and the index in the other dimension. Thus, to iterate over the rows that I've defined above, I could do:

for r in 1..n {
  writeln("row ", r, " contains elements at:");
  for c in SD.dimIter(2, r) do
    writeln("  column ", c, ": ", A[r,c]);
}

对于上面显示的稀疏矩阵,得出:

For the sparse matrix I show above, this yields:

row 1 contains elements at:
  column 1: 1.1
  column 5: 1.5
row 2 contains elements at:
  column 2: 2.2
  column 7: 2.7
row 3 contains elements at:
row 4 contains elements at:
row 5 contains elements at:
  column 1: 5.1
  column 10: 6.0
row 6 contains elements at:
row 7 contains elements at:
row 8 contains elements at:
row 9 contains elements at:
row 10 contains elements at:

我们有兴趣推广dimIter()迭代器并将其作为标准稀疏域/数组接口的一部分,但由于(a)有关如何将其推广到n维稀疏的问题,我们尚未这样做数组和(b)关于我们是否需要支持效率低下的迭代方向的问题(例如,在费用允许的情况下,是否应该能够对CSR的列或CSC的行进行迭代?)

We're interested in generalizing the dimIter() iterator and making it part of the standard sparse domain/array interface, but haven't done so yet due to (a) questions about how to generalize it to n-dimensional sparse arrays and (b) questions about whether we need to support inefficient iteration directions (e.g., should one be able to iterate over columns of CSR or rows of CSC given the expense?)

这篇关于如何在Chapel中的稀疏矩阵中迭代非零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆