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

Making the library compatible with CUDAdrv and CuArrays. #40

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Julia bindings for the [NVIDIA CUSPARSE](http://docs.nvidia.com/cuda/cusparse/)

# Introduction

CUSPARSE.jl proves bindings to a subset of the CUSPARSE library. It extends the amazing [CUDArt.jl](https://github.com/JuliaGPU/CUDArt.jl) library to provide four new sparse matrix classes:
CUSPARSE.jl proves bindings to a subset of the CUSPARSE library. It extends the amazing [CUDAdrv.jl](https://github.com/JuliaGPU/CUDAdrv.jl) library to provide four new sparse matrix classes:

- `CudaSparseMatrixCSC`

Expand All @@ -37,19 +37,19 @@ which implement compressed sparse row/column storage, block CSR, and NVIDIA hybr
A = sprand(10,8,0.2)
d_A = CudaSparseMatrixCSR(A)
```
`A` is transformed into `CSC` format moved to the GPU, then auto-converted to `CSR` format for you. Thus, `d_A` is *not* a transpose of `A`! Similarly, if you have a matrix in dense format on the GPU (in a `CudaArray`), you can simply call `sparse` to turn it into a sparse representation. Right now `sparse` by default turns the matrix it is given into `CSR` format. It takes an optional argument that lets you select `CSC` or `HYB`:
`A` is transformed into `CSC` format moved to the GPU, then auto-converted to `CSR` format for you. Thus, `d_A` is *not* a transpose of `A`! Similarly, if you have a matrix in dense format on the GPU (in a `CuArray`), you can simply call `sparse` to turn it into a sparse representation. Right now `sparse` by default turns the matrix it is given into `CSR` format. It takes an optional argument that lets you select `CSC` or `HYB`:

```julia
d_A = CudaArray(rand(10,20))
d_A = CuArray(rand(10,20))
d_A = sparse(d_A) #now in CSR format

d_B = CudaArray(rand(10,20))
d_B = CuArray(rand(10,20))
d_B = sparse(d_B,'C') #now in CSC format

d_C = CudaArray(rand(10,20))
d_C = CuArray(rand(10,20))
d_C = sparse(d_C,'H') #now in HYB format

d_D = CudaArray(rand(10,20))
d_D = CuArray(rand(10,20))
d_D = sparse(d_C,'B') #now in BSR format
```
# Current Features
Expand Down Expand Up @@ -232,7 +232,7 @@ beta = rand()
d_C = CUSPARSE.geam(alpha, d_A, beta, d_B, 'O', 'O', 'O')

# bring the result back to the CPU
C = CUSPARSE.to_host(d_C)
C = CUSPARSE.collect(d_C)

# observe a zero matrix
alpha*A + beta*B - C
Expand All @@ -242,7 +242,7 @@ Some questions you might have:
- What are the three `'O'`s for?
- CUSPARSE allows us to use one- or zero-based indexing. Julia uses one-based indexing for arrays, but many other libraries (for instance, C-based libraries) use zero-based. The `'O'`s tell CUSPARSE that our matrices are one-based. If you had a zero-based matrix from an external library, you can tell CUSPARSE using `'Z'`.
- Should we move `alpha` and `beta` to the GPU?
- We do not have to. CUSPARSE can read in scalar parameters like `alpha` and `beta` from the host (CPU) memory. You can just pass them to the function and CUSPARSE.jl handles telling the CUDA functions where they are for you. If you have an array, like `A` and `B`, you do need to move it to the GPU before CUSPARSE can work on it. Similarly, to see results, if they are in array form, you will need to move them back to the CPU with `to_host`.
- We do not have to. CUSPARSE can read in scalar parameters like `alpha` and `beta` from the host (CPU) memory. You can just pass them to the function and CUSPARSE.jl handles telling the CUDA functions where they are for you. If you have an array, like `A` and `B`, you do need to move it to the GPU before CUSPARSE can work on it. Similarly, to see results, if they are in array form, you will need to move them back to the CPU with `collect`.
- Since `d_C` is in `CSR` format, is `C` the transpose of what we want?
- No. CUSPARSE.jl handles the conversion internally so that the final result is in `CSC` format for Julia, and *not* the transpose of the correct answer.

Expand Down
2 changes: 1 addition & 1 deletion REQUIRE
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
julia 0.5
CUDArt 0.3.0
CUDAdrv 0.3.0
Compat 0.18.0
2 changes: 1 addition & 1 deletion docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

### Container types

Analogous to `CUDArt.jl` and its `CudaArray` types, you can use
Analogous to `CUDAdrv.jl` and its `CuArray` types, you can use
a variety of sparse matrix containers similar to those in Julia
base.

Expand Down
6 changes: 4 additions & 2 deletions src/CUSPARSE.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ module CUSPARSE

importall Base.SparseArrays

using CUDArt
using CUDArt.rt.cudaStream_t
using CUDAdrv
using CuArrays
import CuArrays: CuMatrix, CuArray, CuVector

const SparseChar = Char
import Base.one
import Base.zero
import Base.collect

export CudaSparseMatrixCSC, CudaSparseMatrixCSR,
CudaSparseMatrixHYB, CudaSparseMatrixBSR,
Expand Down
13 changes: 7 additions & 6 deletions src/libcusparse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ end
function cusparseGetVersion(handle, version)
statuscheck(ccall( (:cusparseGetVersion, libcusparse), cusparseStatus_t, (cusparseHandle_t, Ptr{Cint}), handle, version))
end
function cusparseSetStream(handle, streamId)
statuscheck(ccall( (:cusparseSetStream, libcusparse), cusparseStatus_t, (cusparseHandle_t, cudaStream_t), handle, streamId))
end
function cusparseGetStream(handle, streamId)
statuscheck(ccall( (:cusparseGetStream, libcusparse), cusparseStatus_t, (cusparseHandle_t, Ptr{cudaStream_t}), handle, streamId))
end
# TODO: no idea what to do with these
# function cusparseSetStream(handle, streamId)
# statuscheck(ccall( (:cusparseSetStream, libcusparse), cusparseStatus_t, (cusparseHandle_t, cudaStream_t), handle, streamId))
# end
# function cusparseGetStream(handle, streamId)
# statuscheck(ccall( (:cusparseGetStream, libcusparse), cusparseStatus_t, (cusparseHandle_t, Ptr{cudaStream_t}), handle, streamId))
# end
function cusparseGetPointerMode(handle, mode)
statuscheck(ccall( (:cusparseGetPointerMode, libcusparse), cusparseStatus_t, (cusparseHandle_t, Ptr{cusparsePointerMode_t}), handle, mode))
end
Expand Down
Loading