嗯,我已经找到了一种解决方法,可以做需要做的事情,但仍然缺少一种更直接的方法。
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...
}
另一方面,同样的逻辑也很明显。
希望这对别人有帮助。
干杯