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

Make a package and add CUDA support #17

Open
wants to merge 9 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
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
*.jl.cov
*.jl.mem
.DS_Store
/Manifest.toml
/test/Manifest.toml
17 changes: 10 additions & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
## Documentation: http://docs.travis-ci.com/user/languages/julia/
language: julia
os:
- freebsd
- linux
- osx
- windows
julia:
- 0.4
- 0.5
- 1.3
- 1.4
- nightly
notifications:
email: false
script:
- if [[ -a .git/shallow ]]; then git fetch --unshallow; fi
- julia --check-bounds=yes -e 'Pkg.clone(pwd()); Pkg.build("StructsOfArrays"); Pkg.test("StructsOfArrays"; coverage=true)'
after_success:
- julia -e 'cd(Pkg.dir("StructsOfArrays")); Pkg.add("Coverage"); using Coverage; Codecov.submit(Codecov.process_folder())'
git:
depth: 99999999
jobs:
allow_failures:
- julia: nightly
22 changes: 22 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Copyright (c) 2015: Simon Kornblith.
Copyright (c) 2018-2019: Valentin Churavy, and other contributors
Copyright (c) 2016 Simon Danisch
Copyright (c) 2018 JuliaGPU developers

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
22 changes: 0 additions & 22 deletions LICENSE.md

This file was deleted.

20 changes: 20 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name = "StructsOfArrays"
uuid = "ad108753-388a-4a68-a626-efa2fa9e4278"
authors = ["Simon Kornblith <simon@simonster.com>", "Valentin Churavy <v.churavy@gmail.com>", "Other Contributors"]
version = "0.0.3"

[deps]
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
CUDAapi = "3895d2a7-ec45-59b8-82bb-cfc6a382f9b3"
CUDAnative = "be33ccc6-a3ff-5ff2-a52e-74243cff1e17"
CuArrays = "3a865a2d-5b23-5a0f-bc46-62713ec82fae"
GPUArrays = "0c68f7d7-f131-5f86-a1c3-88cf8149b2d7"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"

[compat]
Adapt = "~1"
CUDAapi = "~4"
CUDAnative = "~3"
CuArrays = "~2"
GPUArrays = "~3"
julia = "≥ 1.3"
76 changes: 31 additions & 45 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
# StructsOfArrays

[![Build Status](https://travis-ci.org/simonster/StructsOfArrays.jl.svg?branch=master)](https://travis-ci.org/simonster/StructsOfArrays.jl)
[![codecov.io](http://codecov.io/github/simonster/StructsOfArrays.jl/coverage.svg?branch=master)](http://codecov.io/github/simonster/StructsOfArrays.jl?branch=master)

A traditional Julia array of immutable objects is an array of structures. Fields
of a given object are stored adjacent in memory. However, this often inhibits
SIMD optimizations. StructsOfArrays implements the classic structure of arrays
Expand All @@ -13,66 +10,55 @@ contains padding. It is especially useful for arrays of complex numbers.

## Usage

You can construct a StructOfArrays directly:
You can construct a `StructOfArrays` directly with:

```julia
using StructsOfArrays
A = StructOfArrays(Complex128, 10, 10)
A = StructOfArrays(ComplexF64, Array, 10, 10)
```

or by converting an AbstractArray:
or by converting an `AbstractArray`:

```julia
A = convert(StructOfArrays, complex(randn(10), randn(10)))
A = convert(StructOfArrays, rand(ComplexF64, 10, 10))
```

Beyond that, there's not much to say. Assignment and indexing works as with
other AbstractArray types. Indexing a `StructOfArrays{T}` yields an object of
type `T`, and you can assign objects of type `T` to a given index. The "magic"
is in the optimizations that the alternative memory layout allows LLVM to
perform.

While you can create a StructOfArrays of non-`isbits` immutables, this is
probably slower than an ordinary array, since a new object must be heap
allocated every time the StructOfArrays is indexed. In practice, StructsOfArrays
works best with `isbits` immutables such as `Complex{T}`.

## Benchmark
A `StructOfArrays` can have different storage arrays. You can construct a
`CuArray`-based `StructOfArrays` directly with:

```julia
using StructsOfArrays
regular = complex(randn(1000000), randn(1000000))
soa = convert(StructOfArrays, regular)
using CuArrays
A = StructOfArrays(ComplexF64, CuArray, 10, 10)
```

function f(x, a)
s = zero(eltype(x))
@simd for i in 1:length(x)
@inbounds s += x[i] * a
end
s
end
or by converting an existing `StructOfArrays` using `replace_storage`:

using Benchmarks
@benchmark f(regular, 0.5+0.5im)
@benchmark f(soa, 0.5+0.5im)
```julia
A = replace_storage(CuArray, convert(StructOfArrays, rand(ComplexF64, 10, 10)))
```

The time for `f(regular, 0.5+0.5im)` is:
This array can be used either in a kernel:

```
Average elapsed time: 1.244 ms
95% CI for average: [1.183 ms, 1.305 ms]
Minimum elapsed time: 1.177 ms
```julia
using CUDAnative
function kernel!(A)
i = (blockIdx().x-1)*blockDim().x + threadIdx().x
if i <= length(A)
A[i] += A[i]
end
return nothing
end
threads = 256
blocks = cld(length(A), threads)
@cuda threads=threads blocks=blocks kernel!(A)
```

and for `f(soa, 0.5+0.5im)`:
or via broadcasting:

```
Average elapsed time: 832.198 μs
95% CI for average: [726.349 μs, 938.048 μs]
Minimum elapsed time: 713.730 μs
```julia
A .+= A
```

In this case, StructsOfArrays are about 1.5x faster than ordinary arrays.
Inspection of generated code demonstrates that `f(soa, a)` uses SIMD
instructions, while `f(regular, a)` does not.
Assignment and indexing works as with other `AbstractArray` types. Indexing a
`StructOfArrays{T}` yields an object of type `T`, and you can assign objects of
type `T` to a given index.
2 changes: 0 additions & 2 deletions REQUIRE

This file was deleted.

37 changes: 0 additions & 37 deletions appveyor.yml

This file was deleted.

Loading