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

Demo with lazy mapping (and fix a bug) #24

Merged
merged 2 commits into from
Oct 18, 2020
Merged
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
26 changes: 23 additions & 3 deletions examples/histogram_msd.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ end == 5
end == 2
#-

# ## Computing histogram of MSD
# ## MSD of random numbers

using CUDA
using FLoops
Expand Down Expand Up @@ -63,9 +63,29 @@ collect(view(xs, 1:10)) # preview
# Pass an array of (real) numbers to `histogram_msd` to compute the
# histogram of MSD:

hist = histogram_msd(xs)
hist1 = histogram_msd(xs)

# Frequency in percentage:

pairs(round.((collect(hist) ./ length(xs) .* 100); digits = 1))
aspercentage(h) = pairs(round.((collect(h) ./ length(xs) .* 100); digits = 1))
aspercentage(hist1)
#-

# ## MSD of lazily computed numbers

# FoldsCUDA supports running reduction over non-`CuArray` containers
# such as `UnitRange` and iterator transformation wrapping it.
# However, you need to explicitly specify to use CUDA by, e.g.,
# passing `CUDAEx` to `@floop`:

executor = has_cuda_gpu() ? CUDAEx() : ThreadedEx() # fallback to thread
hist2 = histogram_msd((x^2 for x in 1:10^8), executor)

# Frequency in percentage:
aspercentage(hist2)
#-

hist3 = histogram_msd((exp(x) for x in range(1, 35, length=10^8)), executor)

# Frequency in percentage:
aspercentage(hist3)
2 changes: 1 addition & 1 deletion src/kernels.jl
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ function _transduce!(buf, rf::F, init, arrays...) where {F}
@assert blocks <= kernel_config.blocks

if buf === nothing
dest_buf = similar(arrays[1], acctype, blocks + cld(blocks, threads))
dest_buf = CuVector{acctype}(undef, blocks + cld(blocks, threads))
dest = view(dest_buf, 1:blocks)
buf = view(dest_buf, blocks+1:length(dest_buf))
else
Expand Down