Skip to main content

opencl3/
lib.rs

1// Copyright (c) 2020-2021 Via Technology Ltd. All Rights Reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//    http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! [![crates.io](https://img.shields.io/crates/v/opencl3.svg)](https://crates.io/crates/opencl3)
16//! [![docs.io](https://docs.rs/opencl3/badge.svg)](https://docs.rs/opencl3/)
17//! [![OpenCL 3.0](https://img.shields.io/badge/OpenCL-3.0-blue.svg)](https://www.khronos.org/registry/OpenCL/)
18//! [![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)
19//!
20//! A Rust implementation of the Khronos [OpenCL](https://www.khronos.org/registry/OpenCL/)
21//! API.
22//!
23//! # Description
24//!
25//! This crate provides a relatively simple, object based model of the OpenCL 3.0
26//! [API](https://www.khronos.org/registry/OpenCL/specs/3.0-unified/html/OpenCL_API.html).  
27//! It is built upon the [cl3](https://crates.io/crates/cl3) crate, which
28//! provides a functional interface to the OpenCL API.  
29//!
30//! **OpenCL** (Open Computing Language) is framework for general purpose
31//! parallel programming across heterogeneous devices including: CPUs, GPUs,
32//! DSPs, FPGAs and other processors or hardware accelerators.
33//!
34//! It is often considered as an open-source alternative to Nvidia's proprietary
35//! Compute Unified Device Architecture [CUDA](https://developer.nvidia.com/cuda-zone)
36//! for performing General-purpose computing on GPUs, see
37//! [GPGPU](https://en.wikipedia.org/wiki/General-purpose_computing_on_graphics_processing_units).
38//!
39//! The [OpenCL Specification](https://www.khronos.org/registry/OpenCL/specs/3.0-unified/html/OpenCL_API.html#_the_opencl_architecture)
40//! has evolved over time and not all device vendors support all OpenCL features.
41//!
42//! [OpenCL 3.0](https://www.khronos.org/registry/OpenCL/specs/3.0-unified/html/OpenCL_API.html)
43//! is a unified specification that adds little new functionality to previous OpenCL versions.  
44//! It specifies that all **OpenCL 1.2** features are **mandatory**, while all
45//! OpenCL 2.x and OpenCL 3.0 features are now optional.
46//!
47//! See [OpenCL Description](https://github.com/kenba/opencl3/blob/main/docs/opencl_description.md).
48//!
49//! # OpenCL Architecture
50//!
51//! The [OpenCL Specification](https://www.khronos.org/registry/OpenCL/specs/3.0-unified/html/OpenCL_API.html#_the_opencl_architecture)
52//! considers OpenCL as four models:
53//!
54//! * **Platform Model**  
55//!   The physical OpenCL hardware: a *host* containing one or more OpenCL [platform]s,
56//!   each connected to one or more OpenCL [device]s.  
57//!   An OpenCL application running on the *host*, creates an OpenCL environment
58//!   called a [context] on a single [platform] to process data on one or more
59//!   of the OpenCL [device]s connected to the [platform].
60//!
61//! * **Programming Model**  
62//!   An OpenCL [program] consists of OpenCL [kernel] functions that can run
63//!   on OpenCL [device]s within a [context].  
64//!   OpenCL [program]s must be created (and most must be built) for a [context]
65//!   before their OpenCL [kernel] functions can be created from them,
66//!   the exception being "built-in" [kernel]s which don't need to be built
67//!   (or compiled and linked).  
68//!   OpenCL [kernel]s are controlled by an OpenCL application that runs on the
69//!   *host*, see **Execution Model**.
70//!
71//! * **Memory Model**  
72//!   **OpenCL 1.2** memory is divided into two fundamental memory regions:
73//!   **host memory** and **device memory**.  
74//!   OpenCL [kernel]s run on **device memory**; an OpenCL application must write
75//!   **host memory** to **device memory** for OpenCL [kernel]s to process.
76//!   An OpenCL application must also read results from **device memory** to
77//!   **host memory** after a [kernel] has completed execution.  
78//!   **OpenCL 2.0** shared virtual memory ([svm]) is shared between the host
79//!   and device(s) and synchronised by OpenCL; eliminating the explicit transfer
80//!   of memory between host and device(s) memory regions.
81//!
82//! * **Execution Model**  
83//!   An OpenCL application creates at least one OpenCL [command_queue] for each
84//!   OpenCL [device] (or *sub-device*) within it's OpenCL [context].  
85//!   OpenCL [kernel] executions and **OpenCL 1.2** memory reads and writes are
86//!   "enqueued" by the OpenCL application on each [command_queue].
87//!   An application can wait for all "enqueued" commands to finish on a
88//!   [command_queue] or it can wait for specific [event]s to complete.
89//!   Normally [command_queue]s run commands in the order that they are given.
90//!   However, [event]s can be used to execute [kernel]s out-of-order.
91//!
92//! # OpenCL Objects
93//!
94//! [Platform]: platform/struct.Platform.html
95//! [Device]: device/struct.Device.html
96//! [SubDevice]: device/struct.SubDevice.html
97//! [Context]: context/struct.Context.html
98//! [Program]: program/struct.Program.html
99//! [Kernel]: kernel/struct.Kernel.html
100//! [Buffer]: memory/struct.Buffer.html
101//! [Image]: memory/struct.Image.html
102//! [Sampler]: memory/struct.Sampler.html
103//! [SvmVec]: svm/struct.SvmVec.html
104//! [Pipe]: memory/struct.Pipe.html
105//! [CommandQueue]: command_queue/struct.CommandQueue.html
106//! [Event]: event/struct.Event.html
107//!
108//! ## Platform Model
109//!
110//! The platform model has thee objects:
111//! * [Platform]
112//! * [Device]
113//! * [Context]
114//!
115//! Of these three objects, the OpenCL [Context] is by *far* the most important.
116//! Each application must create a [Context] from the most appropriate [Device]s
117//! available on one of [Platform]s on the *host* system that the application
118//! is running on.
119//!
120//! Most example OpenCL applications just choose the first available [Platform]
121//! and [Device] for their [Context]. However, since many systems have multiple
122//! platforms and devices, the first [Platform] and [Device] are unlikely to
123//! provide the best performance.  
124//! For example, on a system with an APU (combined CPU and GPU, e.g. Intel i7)
125//! and a discrete graphics card (e.g. Nvidia GTX 1070) OpenCL may find the
126//! either the integrated GPU or the GPU on the graphics card first.
127//!
128//! OpenCL applications often require the performance of discrete graphics cards
129//! or specific OpenCL features, such as [svm] or double/half floating point
130//! precision. In such cases, it is necessary to query the [Platform]s and
131//! [Device]s to choose the most appropriate [Device]s for the application before
132//! creating the [Context].
133//!
134//! The [Platform] and [Device] modules contain structures and methods to simplify
135//! querying the host system [Platform]s and [Device]s to create a [Context].
136//!
137//! ## Programming Model
138//!
139//! The OpenCL programming model has two objects:
140//! * [Program]
141//! * [Kernel]
142//!
143//! OpenCL [Kernel] functions are contained in OpenCL [Program]s.  
144//!
145//! Kernels are usually defined as functions in OpenCL [Program] source code,
146//! however OpenCL [Device]s may contain built-in [Kernel]s,
147//! e.g.: some Intel GPUs have built-in motion estimation kernels.
148//!
149//! OpenCL [Program] objects can be created from OpenCL source code,
150//! built-in kernels, binaries and intermediate language binaries.
151//! Depending upon how an OpenCL [Program] object was created, it may need to
152//! be built (or complied and linked) before the [Kernel]s in them can be
153//! created.
154//!
155//! All the [Kernel]s in an [Program] can be created together or they can be
156//! created individually, by name.
157//!
158//! ## Memory Model
159//!
160//! The OpenCL memory model consists of five objects:
161//! * [Buffer]
162//! * [Image]
163//! * [Sampler]
164//! * [SvmVec]
165//! * [Pipe]
166//!
167//! [Buffer], [Image] and [Sampler] are OpenCL 1.2 (i.e. **mandatory**) objects,  
168//! [svm] and [Pipe] are are OpenCL 2.0 (i.e. optional) objects.
169//!
170//! A [Buffer] is a contiguous block of memory used for general purpose data.  
171//! An [Image] holds data for one, two or three dimensional images.  
172//! A [Sampler] describes how a [Kernel] is to sample an [Image], see
173//! [Sampler objects](https://www.khronos.org/registry/OpenCL/specs/3.0-unified/html/OpenCL_API.html#_sampler_objects).  
174//!
175//! [Shared Virtual Memory](https://www.khronos.org/registry/OpenCL/specs/3.0-unified/html/OpenCL_API.html#shared-virtual-memory)
176//! enables the host and kernels executing on devices to directly share data
177//! without explicitly transferring it.
178//!
179//! [Pipes](https://www.khronos.org/registry/OpenCL/specs/3.0-unified/html/OpenCL_API.html#_pipes)
180//! store memory as FIFOs between [Kernel]s. [Pipe]s are not accessible from the host.
181//!
182//! ## Execution Model
183//!
184//! The OpenCL execution model has two objects:
185//! * [CommandQueue]
186//! * [Event]
187//!
188//! OpenCL commands to transfer memory and execute kernels on devices are
189//! performed via [CommandQueue]s.
190//!
191//! Each OpenCL device (and sub-device) must have at least one command_queue
192//! associated with it, so that commands may be enqueued on to the device.
193//!
194//! There are several OpenCL [CommandQueue] "enqueue_" methods to transfer
195//! data between host and device memory, map SVM memory and execute kernels.
196//! All the "enqueue_" methods accept an event_wait_list parameter and return
197//! an [Event] that can be used to monitor and control *out-of-order* execution
198//! of kernels on a [CommandQueue], see
199//! [Event Objects](https://www.khronos.org/registry/OpenCL/specs/3.0-unified/html/OpenCL_API.html#event-objects).
200
201extern crate cl3;
202
203#[cfg(any(feature = "cl_khr_command_buffer", feature = "dynamic"))]
204pub mod command_buffer;
205pub mod command_queue;
206pub mod context;
207pub mod device;
208pub mod event;
209pub mod kernel;
210pub mod memory;
211pub mod platform;
212pub mod program;
213#[cfg(any(feature = "CL_VERSION_2_0", feature = "dynamic"))]
214pub mod svm;
215
216pub mod error_codes {
217    pub use cl3::error_codes::*;
218}
219pub mod types {
220    pub use cl3::types::*;
221}
222
223use std::result;
224/// Custom Result type to output OpenCL error text.
225pub type Result<T> = result::Result<T, error_codes::ClError>;