9951 explained code solutions for 126 technologies


julialangHow to work with GPUs in JuliaLang?


JuliaLang supports GPU programming through the CUDA.jl package. To use it, you need to have a CUDA-enabled GPU and the CUDA Toolkit installed.

using CUDA

# Allocate memory on the GPU
a = CUDA.zeros(Float32, 10)

# Copy data from the CPU to the GPU
b = CUDA.ones(Float32, 10)
CUDA.copy!(a, b)

# Perform an operation on the GPU
c = a .+ b

# Copy data from the GPU to the CPU
d = Array(c)

Output example

10-element Array{Float32,1}:
 2.0
 2.0
 2.0
 2.0
 2.0
 2.0
 2.0
 2.0
 2.0
 2.0

Code explanation

  1. using CUDA: Loads the CUDA.jl package.
  2. a = CUDA.zeros(Float32, 10): Allocates 10 elements of type Float32 on the GPU.
  3. b = CUDA.ones(Float32, 10): Allocates 10 elements of type Float32 on the GPU and sets them to 1.
  4. CUDA.copy!(a, b): Copies the data from b to a.
  5. c = a .+ b: Performs an element-wise addition of a and b on the GPU.
  6. d = Array(c): Copies the data from c to the CPU.

Helpful links

Edit this code on GitHub