|
| 1 | +```@meta |
| 2 | +CurrentModule = OpenCL |
| 3 | +``` |
| 4 | + |
| 5 | +# OpenCL |
| 6 | + |
| 7 | +*Julia interface for the OpenCL parallel computation API* |
| 8 | + |
| 9 | +This package aims to be a complete solution for OpenCL programming in Julia, similar in |
| 10 | +scope to [PyOpenCL] for Python. It provides a high level API for OpenCL to make programing |
| 11 | +hardware accelerators, such as GPUs, FPGAs, and DSPs, as well as multicore CPUs much less |
| 12 | +onerous. |
| 13 | + |
| 14 | + |
| 15 | +!!! note "OpenCL.jl needs your help!" |
| 16 | + If you can help maintain this package, please reach out on the [JuliaLang Slack](https://julialang.org/slack/) #gpu channel. |
| 17 | + |
| 18 | +!!! warning "OpenCL.jl is currently undergoing major changes." |
| 19 | + If you have old code developed for OpenCL.jl v0.9, please check [`NEWS.md`](https://github.com/JuliaGPU/OpenCL.jl/blob/master/NEWS.md) for an overview of the changes. |
| 20 | + |
| 21 | + |
| 22 | +## Installation |
| 23 | + |
| 24 | +1. Install an OpenCL driver. You can install one system-wide, i.e., using your package |
| 25 | + manager, or use `pocl_jll.jl` for a CPU back-end. |
| 26 | +2. Add OpenCL to your Julia environment: |
| 27 | + |
| 28 | +```julia |
| 29 | +using Pkg |
| 30 | +Pkg.add("OpenCL") |
| 31 | +``` |
| 32 | + |
| 33 | +3. Test your installation: |
| 34 | + |
| 35 | +```julia-repl |
| 36 | +julia> OpenCL.versioninfo() |
| 37 | +OpenCL.jl version 0.10.0 |
| 38 | +
|
| 39 | +Toolchain: |
| 40 | + - Julia v1.10.5 |
| 41 | + - OpenCL_jll v2024.5.8+1 |
| 42 | +
|
| 43 | +Available platforms: 3 |
| 44 | + - Portable Computing Language |
| 45 | + version: OpenCL 3.0 PoCL 6.0 Linux, Release, RELOC, SPIR-V, LLVM 15.0.7jl, SLEEF, DISTRO, POCL_DEBUG |
| 46 | + · cpu-haswell-AMD Ryzen 9 5950X 16-Core Processor (fp64, il) |
| 47 | + - NVIDIA CUDA |
| 48 | + version: OpenCL 3.0 CUDA 12.6.65 |
| 49 | + · NVIDIA RTX 6000 Ada Generation (fp64) |
| 50 | + - Intel(R) OpenCL Graphics |
| 51 | + version: OpenCL 3.0 |
| 52 | + · Intel(R) Arc(TM) A770 Graphics (fp16, il) |
| 53 | +
|
| 54 | +!!! warning "Platform list is only computed once" |
| 55 | + OpenCL is only computing the list of platforms [once](https://github.com/KhronosGroup/OpenCL-ICD-Loader/blob/d547426c32f9af274ec1369acd1adcfd8fe0ee40/loader/linux/icd_linux.c#L234-L238). |
| 56 | + Therefore if `using pocl_jll` is executed after `OpenCL.versioninfo()` or other calls to the OpenCL API then it won't affect the list of platforms available and you will need to restart the Julia session and run `using pocl_jll` before `OpenCL` is used. |
| 57 | +
|
| 58 | +## Basic example: vector add |
| 59 | +
|
| 60 | +The traditional way of using OpenCL is by writing kernel source code in OpenCL C. For |
| 61 | +example, a simple vector addition: |
| 62 | +
|
| 63 | +```julia |
| 64 | +using OpenCL, pocl_jll |
| 65 | +
|
| 66 | +const source = """ |
| 67 | + __kernel void vadd(__global const float *a, |
| 68 | + __global const float *b, |
| 69 | + __global float *c) { |
| 70 | + int gid = get_global_id(0); |
| 71 | + c[gid] = a[gid] + b[gid]; |
| 72 | + }""" |
| 73 | +
|
| 74 | +a = rand(Float32, 50_000) |
| 75 | +b = rand(Float32, 50_000) |
| 76 | +
|
| 77 | +d_a = CLArray(a) |
| 78 | +d_b = CLArray(b) |
| 79 | +d_c = similar(d_a) |
| 80 | +
|
| 81 | +p = cl.Program(; source) |> cl.build! |
| 82 | +k = cl.Kernel(p, "vadd") |
| 83 | +
|
| 84 | +clcall(k, Tuple{CLPtr{Float32}, CLPtr{Float32}, CLPtr{Float32}}, |
| 85 | + d_a, d_b, d_c; global_size=size(a)) |
| 86 | +
|
| 87 | +c = Array(d_c) |
| 88 | +
|
| 89 | +@assert a + b ≈ c |
| 90 | +``` |
| 91 | + |
| 92 | + |
| 93 | +## Native example: vector add |
| 94 | + |
| 95 | +If your platform supports SPIR-V, it's possible to use Julia functions as kernels: |
| 96 | + |
| 97 | +```julia |
| 98 | +using OpenCL, pocl_jll |
| 99 | + |
| 100 | +function vadd(a, b, c) |
| 101 | + gid = get_global_id(1) |
| 102 | + @inbounds c[gid] = a[gid] + b[gid] |
| 103 | + return |
| 104 | +end |
| 105 | + |
| 106 | +a = rand(Float32, 50_000) |
| 107 | +b = rand(Float32, 50_000) |
| 108 | + |
| 109 | +d_a = CLArray(a) |
| 110 | +d_b = CLArray(b) |
| 111 | +d_c = similar(d_a) |
| 112 | + |
| 113 | +@opencl global_size=size(a) vadd(d_a, d_b, d_c) |
| 114 | + |
| 115 | +c = Array(d_c) |
| 116 | + |
| 117 | +@assert a + b ≈ c |
| 118 | +``` |
| 119 | + |
| 120 | + |
| 121 | +## More examples |
| 122 | + |
| 123 | +You may want to check out the `examples` folder. Either `git clone` the repository to your |
| 124 | +local machine or navigate to the OpenCL.jl install directory via: |
| 125 | + |
| 126 | +```julia |
| 127 | +using OpenCL |
| 128 | +cd(joinpath(dirname(pathof(OpenCL)), "..")) |
| 129 | +``` |
| 130 | + |
| 131 | +Otherwise, feel free to take a look at the Jupyter notebooks below: |
| 132 | + |
| 133 | + * [Julia set fractals](https://github.com/JuliaGPU/OpenCL.jl/blob/master/examples/notebooks/julia_set_fractal.ipynb) |
| 134 | + * [Mandlebrot fractal](https://github.com/JuliaGPU/OpenCL.jl/blob/master/examples/notebooks/mandelbrot_fractal.ipynb) |
| 135 | + * [Transpose bandwidth](https://github.com/JuliaGPU/OpenCL.jl/blob/master/examples/notebooks/Transpose.ipynb) |
| 136 | + |
| 137 | + |
| 138 | +## Credit |
| 139 | + |
| 140 | +This package is heavily influenced by the work of others: |
| 141 | + |
| 142 | + * [PyOpenCL](http://mathema.tician.de/software/pyopencl/) by Andreas Klockner |
| 143 | + * [oclpb](https://github.com/srossross/oclpb) by Sean Ross |
| 144 | + * [Boost.Compute](https://github.com/kylelutz/compute) by Kyle Lutz |
| 145 | + * [rust-opencl](https://github.com/luqmana/rust-opencl) |
| 146 | + |
| 147 | + |
0 commit comments