Skip to content

Instantly share code, notes, and snippets.

import CLBLAS # The low-level Julia CLBLAS C wrapper
GPUArrays.blas_module(A::CLArray) = CLBLAS
function GPUArrays.blasbuffer(A::CLArray)
cl.CLArray(clbuffer(A), global_queue(A), size(A))
end
names_gpu = CLArray(FixedString{12}[
"Egon", "Hugo", "Frederick", "Franz"
])
julia> replace_with(names_gpu, "Egon", "Lorenz")
GPU: 4-element Array{FixedString{12},1}:
"Lorenz"
"Hugo"
"Frederick"
"Franz"
struct FixedString{N} # Size is a static type parameter
length::UInt32
data::NTuple{N, UInt8} # Immutable statically sized tuple
end
function Base.:(==)(a::FixedString, b::FixedString)
a.length == b.length || return false
for i in 1:a.length
a.data[i] == b.data[i] || return false
end
return true
using CLArrays
# `where T` is Julias way of declaring a free type parameter
function replace_with(input::AbstractArray{T}, name, with) where T
fsname, fswith = T(name), T(with) # convert args to element type
# creates an unitiliazed array similar to input
output = similar(input)
# now replace elements on the gpu
map!(n-> n == fsname ? fswith : n, output, input)
output
end

PDE

Kuramoto-Sivashinsky algorithm benchmark (original benchmark).

This benchmark is dominated by the cost of the FFT, leading to worse results for OpenCL with CLFFT compared to the faster CUFFT. Similarly the multithreaded backend doesn't improve much over base with the same FFT implementation. Result of the benchmarked PDE:

PDE

using GPUArrays
# Overloading the Julia Base map! function for GPUArrays
function Base.map!(f::Function, A::GPUArray, B::GPUArray)
# the below is the Julia syntax to create a closure and pass it
# as the first argument to gpu_call
gpu_call(A, (f, A, B)) do state, f, A, B
# If launch parameters arent specified, linear_index gets the index
# into the Array passed as second argument to gpu_call (`A`)
i = linear_index(state)
A[i] = f(B[i])
@SimonDanisch
SimonDanisch / 2_7billion_points.jl
Created October 31, 2017 11:25
Plotting 2.7 billion points
using TextParse
using TextParse: Record, Field, Numeric, tryparsenext
# Helper type to memory map a file as a string
struct MMapString{T}
# using Type parameter, too lazy to write out the result type of mmap,
# but also don't want to loose performance
x::T
end
MMapString(path::String) = MMapString(Mmap.mmap(open(path), Vector{UInt8}))
@SimonDanisch
SimonDanisch / makie.ipynb
Last active November 10, 2019 00:33
MakiE examples
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
using Gtk, GLWindow, GLAbstraction, Reactive, GeometryTypes, Colors, GLVisualize
using GtkReactive
using Gtk.GConstants, ModernGL
mutable struct GtkContext <: GLWindow.AbstractContext
window::Gtk.GLArea
framebuffer::GLWindow.GLFramebuffer
end
function make_context_current(screen::Screen)
@SimonDanisch
SimonDanisch / kmeans.jl
Created July 7, 2017 15:02
Multithreaded K-Means
using Colors, ColorVectorSpace, FileIO
using GPUArrays
import GPUArrays: mapidx, AbstractAccArray
@inline function euclidian(a::Colorant, b::Colorant)
abs(comp1(a) - comp1(b)) +
abs(comp2(a) - comp2(b)) +
abs(comp3(a) - comp3(b))
end