Created
August 20, 2020 21:51
-
-
Save MasonProtter/6704fd7ac9d424753ef5f2f7d0f02d69 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#+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