Skip to content

Instantly share code, notes, and snippets.

@MasonProtter
Created August 20, 2020 21:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MasonProtter/6704fd7ac9d424753ef5f2f7d0f02d69 to your computer and use it in GitHub Desktop.
Save MasonProtter/6704fd7ac9d424753ef5f2f7d0f02d69 to your computer and use it in GitHub Desktop.
#+BEGIN_SRC jupyter-julia
struct ArrayString{Store <: AbstractVector{<:AbstractChar}} <: AbstractString
chars::Store
end
ArrayString(str::String) = ArrayString(collect(str))
ArrayString(T, str::String) = ArrayString(T(collect(str)))
Base.String(s::ArrayString) = String(s.chars)
function Base.show(io::IO, ::MIME"text/plain", s::ArrayString{Store}) where {Store}
println(io, "String backed by $Store\n ", "\"", String(s), "\"")
end
function Base.show(io::IO, s::ArrayString{Store}) where {Store}
println(io, "\"", String(s), "\"")
end
Base.:(*)(s1::ArrayString, s2::ArrayString) = ArrayString(vcat(s1.chars, s2.chars))
Base.:(*)(ss::ArrayString...) = *((ss[1] * ss[2]), ss[3:end]...)
ArrayString("hi") * ArrayString(" ") * ArrayString("bye")
#+END_SRC
#+RESULTS:
: String backed by Array{Char,1}
: "hi bye"
________________________________________________________________________________________________________
#+BEGIN_SRC jupyter-julia
using CUDA
function print_kernel(::MIME"text/plain", str)
@cuprintln "ArrayString backed by CuVector"
@cuprint " \""
foreach(str) do char
@cuprint Cchar(char) #CUDA.jl printing only supports Cchars
end
@cuprint "\""
@cuprintln
CUDA.cudaDeviceSynchronize()
return nothing
end
function Base.show(io::IO, m::MIME"text/plain", s::ArrayString{<:CUDA.AbstractGPUArray})
@cuda print_kernel(m, s.chars)
end
ArrayString(cu, "this") *
ArrayString(cu, " is") *
ArrayString(cu, " on") *
ArrayString(cu, " the") *
ArrayString(cu, " gpu")
#+END_SRC
#+RESULTS:
: ArrayString backed by CuVector
: "this is on the gpu"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment