amgcl-CRS format

25 篇文章 1 订阅
14 篇文章 1 订阅

在AMGCL里的第一步是要读如数据,但是它的存储格式是稀疏矩阵的形式,具体的读取代码见

template <class RHS>
inline int read_problem(const std::string &fname,
        std::vector<int>    &row,
        std::vector<int>    &col,
        std::vector<double> &val,
        RHS &rhs
        )
{
    std::cout << "Reading \"" << fname << "\"..." << std::endl;
    std::ifstream f(fname.c_str(), std::ios::binary);
    if (!f) throw std::invalid_argument("Failed to open problem file");

    int n;

    f.read((char*)&n, sizeof(int));

    row.resize(n + 1);
    f.read((char*)row.data(), row.size() * sizeof(int));

    col.resize(row.back());
    val.resize(row.back());
    rhs.resize(n);

    f.read((char*)&col[0], col.size() * sizeof(int));
    f.read((char*)&val[0], val.size() * sizeof(double));
    f.read((char*)&rhs[0], rhs.size() * sizeof(double));

    std::cout << "Done\n" << std::endl;

    return n;
}

template <class RHS, typename value_type>
inline int read_problem(const std::string &fname,
        std::vector<int>        &row,
        std::vector<int>        &col,
        std::vector<value_type> &val,
        RHS &rhs
        )
{
    std::cout << "Reading \"" << fname << "\"..." << std::endl;
    std::ifstream f(fname.c_str(), std::ios::binary);
    if (!f) throw std::invalid_argument("Failed to open problem file");

    int n;

    f.read((char*)&n, sizeof(int));

    row.resize(n + 1);
    f.read((char*)row.data(), row.size() * sizeof(int));

    col.resize(row.back());
    val.resize(row.back());
    rhs.resize(n);

    f.read((char*)col.data(), col.size() * sizeof(int));

    for(size_t i = 0, nnz = row.back(); i < nnz; ++i) {
        double v;
        f.read((char*)&v, sizeof(double));
        val[i] = v;
    }
    for(size_t i = 0; i < n; ++i) {
        double v;
        f.read((char*)&v, sizeof(double));
        rhs[i] = v;
    }

    std::cout << "Done\n" << std::endl;

    return n;
}

在这里它是利用了稀疏矩阵CRS的存储方式,以下CRS介绍的原文。简单的说就是只是存储非零元素的值和索引坐标。

转自:http://www.netlib.org/linalg/html_templates/node91.html

The Compressed Row and Column

The Compressed Row and Column (in the next section) Storage formats are the most general: they make absolutely no assumptions about the sparsity structure of the matrix, and they don't store any unnecessary elements. On the other hand, they are not very efficient, needing an indirect addressing step for every single scalar operation in a matrix-vector product or preconditioner solve.

The Compressed Row Storage (CRS) format puts the subsequent nonzeros of the matrix rows in contiguous memory locations. Assuming we have a nonsymmetric sparse matrix, we create vectors: one for floating-point numbers (val), and the other two for integers (col_ind,row_ptr). The val vector stores the values of the nonzero elements of the matrix, as they are traversed in a row-wise fashion. Thecol_ind vector stores the column indexes of the elements in the val vector. That is, if then. Therow_ptr vector stores the locations in the val vector that start a row, that is, if then. By convention, we define, where is the number of nonzeros in the matrix. The storage savings for this approach is significant. Instead of storing elements, we need only storage locations.

As an example, consider the nonsymmetric matrix defined by

The CRS format for this matrix is then specified by the arrays {val, col_ind, row_ptr} given below


.

If the matrix is symmetric, we need only store the upper (or lower) triangular portion of the matrix. The trade-off is a more complicated algorithm with a somewhat different pattern of data access.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值