# Animated plot and CUDA computations with Makie

**URL:** https://discourse.julialang.org/t/animated-plot-and-cuda-computations-with-makie/48974
**Category:** GPU
**Tags:** question, plotting, cuda, makie
**Created:** [October 25, 2020, 11:40am UTC](https://discourse.julialang.org/t/animated-plot-and-cuda-computations-with-makie/48974 "2020-10-25T11:40:53Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![R366Y](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/r366y/32/10970_2.png) [@R366Y](https://discourse.julialang.org/u/R366Y)
#### Post date: [October 25, 2020, 11:40am UTC](https://discourse.julialang.org/t/animated-plot-and-cuda-computations-with-makie/48974/1 "2020-10-25T11:40:53Z")

</div>

Hello everyone ,  
I’m learning GPU programming with Julia and I want to generate a procedurally animated picture inside a window with CUDA , for instance I’m porting the GPU Ripple example from Chapter 5 “Cuda by example” book.  
I managed to make it work with Makie but rendering doesn’t look performing really well and even worse I have to transfer the resulting array from GPU to CPU and then update the image.  
Is there a better way to do that with Makie or any other library in Julia ? I tried to look into GLFW, GLMakie etc but I had difficulty to understand how those libraries work.

```julia
module GPU_RIPPLE

using CUDA
using Makie
using AbstractPlotting
using Colors

const DIM = 1024
const PI = 3.1415926535897932

function kernel(image, ticks)
    # map from threadIdx/blockIdx to pixel position
    x = threadIdx().x + (blockIdx().x - 1) * blockDim().x
    y = threadIdx().y + (blockIdx().y - 1) * blockDim().y
    offset = x + (y-1) * blockDim().x * gridDim().x

    # calculate value at the position
    fx = x - DIM/2
    fy = y - DIM/2
    d = CUDA.sqrt(fx * fx + fy * fy)
    grey = (128.0 + 127.0 * CUDA.cos(d/10.0 - ticks/7.0) / (d/10.0 + 1.0))/255
    image[offset] = grey
    return nothing
end

function main()

    imoutput = zeros(RGB{Float32}, DIM, DIM)
    
    img_node = Node(imoutput)

    num_threads = 16
    num_blocks = ceil(Int, DIM/num_threads)
    blocks = (num_blocks, num_blocks)
    threads = (num_threads, num_threads)

    scene = Scene(background_color=:black)
    scene = image!(img_node, show_axis=false)
    display(scene)

    ticks = 1
    @async while isopen(scene)
        d_imoutput = CuArray(imoutput)
        CUDA.@sync begin
            @cuda blocks=blocks threads=threads kernel(d_imoutput, ticks)
        end
        img_node[] = Array(d_imoutput)
        ticks +=1
        sleep(1/300)
    end
    
end

end

```

---

<div class="post-metadata">

### Author: ![maleadt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maleadt/32/10097_2.png) [@maleadt](https://discourse.julialang.org/u/maleadt)
#### Post date: [October 26, 2020, 11:23am UTC](https://discourse.julialang.org/t/animated-plot-and-cuda-computations-with-makie/48974/2 "2020-10-26T11:23:06Z")

</div>

There’s no reason to allocate `imoutput` for every iteration. It’s also performing a copy, where you should just construct a empty CuArray. But ultimately there should be some integration between the plotting library and CUDA.jl so that the array can be used directly, and that is currently not implemented.

---

<div class="post-metadata">

### Author: ![R366Y](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/r366y/32/10970_2.png) [@R366Y](https://discourse.julialang.org/u/R366Y)
#### Post date: [October 26, 2020, 8:20pm UTC](https://discourse.julialang.org/t/animated-plot-and-cuda-computations-with-makie/48974/3 "2020-10-26T20:20:39Z")

</div>

Thank you very much for your answer @maleadt
