amgcl-CRS format

在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

amgcl-CRS format_第1张图片

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.

你可能感兴趣的:(amgcl-CRS format)