代码之家  ›  专栏  ›  技术社区  ›  Vtik

将稀疏矩阵从特征::稀疏矩阵转换为尖点::hyb_矩阵

  •  -1
  • Vtik  · 技术社区  · 7 年前

    我正在寻找一种转换方式

    <-&燃气轮机; 尖点::hyb_矩阵<int,float,cusp::host\u memory>

    来回地

    特征矩阵是之前计算的结果,我需要一个尖点::hyb_矩阵,以便在以后使用GPU进行共轭梯度计算。

    谢谢

    1 回复  |  直到 6 年前
        1
  •  0
  •   Vtik    7 年前

    嗯,我已经找到了一种解决方法,可以做需要做的事情,但仍然缺少一种更直接的方法。

    example ,我只需要从特征::SparseMatrix中提取值的行/列/系数向量来构造尖点::hyb_矩阵。这可以通过以下方式实现:

    void SparseMatrix2Coo(Eigen::SparseMatrix<float> Matrix, std::vector<int>& rows, std::vector<int>& cols, std::vector<float>& coeffs)
    {
    rows.clear();
    cols.clear();
    coeffs.clear();
    for (int k=0; k < Matrix.outerSize(); ++k)
    {
        for (Eigen::SparseMatrix<float>::InnerIterator it(Matrix,k); it; ++it)
        {
            rows.push_back(it.row());
            cols.push_back(it.col());
            coeffs.push_back(Matrix.coeff(it.row(), it.col()));
        }
    }
    assert(cols.size() == coeffs.size());
    assert(rows.size() == cols.size());
    }
    

    现在,一旦我们有了行/列/系数,我们只需要使用上面示例中的那些作为输入:

    void computeConjugateGradientGPU(std::vector<int>& rows, std::vector<int>& cols, std::vector<float>& coeffs, std::vector<float>& b, Eigen::VectorXf& x)
    {
    int arrays_size = rows.size();
    /// allocate device memory for CSR format
    int   * device_I;
    cudaMalloc(&device_I, arrays_size * sizeof(int));
    int   * device_J;
    cudaMalloc(&device_J, arrays_size * sizeof(int));
    float * device_V;
    cudaMalloc(&device_V, arrays_size * sizeof(float));
    
    float * device_b;
    cudaMalloc(&device_b, b.size() * sizeof(float));
    
    /// copy raw data from host to device
    cudaMemcpy(device_I, &cols[0], arrays_size * sizeof(int),   cudaMemcpyHostToDevice);
    cudaMemcpy(device_J, &rows[0], arrays_size * sizeof(int),   cudaMemcpyHostToDevice);
    cudaMemcpy(device_V, &coeffs[0], arrays_size * sizeof(float), cudaMemcpyHostToDevice);
    cudaMemcpy(device_b, &b[0],  b.size() * sizeof(float), cudaMemcpyHostToDevice);
    
    /// and the rest is the same...
    }
    

    另一方面,同样的逻辑也很明显。

    希望这对别人有帮助。

    干杯