Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[GPU] Add kernels to scale CSC and CSR matrices #34

Open
amontoison opened this issue Nov 30, 2023 · 0 comments
Open

[GPU] Add kernels to scale CSC and CSR matrices #34

amontoison opened this issue Nov 30, 2023 · 0 comments

Comments

@amontoison
Copy link
Member

amontoison commented Nov 30, 2023

function scaling_csc!(A::SparseMatrixCSC{T}, s::Vector{T}) where T
  n = length(s)
  @assert size(A,2) == n
  for j = 1:n
    acc = zero(T)
    @inbounds @simd for i = A.colptr[j]:(A.colptr[j + 1] - 1)
      acc += A.nzval[i] * A.nzval[i]
    end
    s[j] = sqrt(acc)
    if acc  zero(T)
      @inbounds @simd for i = A.colptr[j]:(A.colptr[j + 1] - 1)
        A.nzval[i] /= s[j]
      end
    end
  end
end
function scaling_csc!(A::CuSparseMatrixCSC{T}, s::CuVector{T}) where T
  n = length(s)
  @assert size(A,2) == n
  for j = 1:n
    acc = zero(T)
    @inbounds @simd for i = A.colPtr[j]:(A.colPtr[j + 1] - 1)
      acc += A.nzVal[i] * A.nzVal[i]
    end
    s[j] = sqrt(acc)
    if acc  zero(T)
      @inbounds @simd for i = A.colPtr[j]:(A.colPtr[j + 1] - 1)
        A.nzVal[i] /= s[j]
      end
    end
  end
end
function scaling_csr!(A::CuSparseMatrixCSR{T}, s::CuVector{T}) where T
  m = length(s)
  @assert size(A,1) == m
  for j = 1:m
    acc = zero(T)
    @inbounds @simd for i = A.rowPtr[j]:(A.rowPtr[j + 1] - 1)
      acc += A.nzVal[i] * A.nzVal[i]
    end
    s[j] = sqrt(acc)
    if acc  zero(T)
      @inbounds @simd for i = A.rowPtr[j]:(A.rowPtr[j + 1] - 1)
        A.nzVal[i] /= s[j]
      end
    end
  end
end

cc @michel2323

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant