oneapi_rs/
context.rs

1//
2// Copyright (C) 2026 Intel Corporation
3//
4// Under the MIT License or the Apache License v2.0.
5// See LICENSE-MIT and LICENSE-APACHE for license information.
6// SPDX-License-Identifier: MIT OR Apache-2.0
7//
8
9use oneapi_rs_sys::{context::ffi, kernel_bundle, types::ffi::DevicePtr};
10
11use crate::{device::Device, kernel::SourceKernelBundle};
12
13/// A context represents the runtime data structures and state required by a SYCL backend API
14/// to interact with a group of devices associated with a platform.
15pub struct Context(pub(crate) cxx::UniquePtr<ffi::Context>);
16
17impl From<cxx::UniquePtr<ffi::Context>> for Context {
18    fn from(value: cxx::UniquePtr<ffi::Context>) -> Self {
19        Self(value)
20    }
21}
22
23impl Context {
24    pub fn new(devices: &[&Device]) -> Self {
25        let devices = devices
26            .iter()
27            .map(|d| DevicePtr {
28                ptr: (*d).clone().0,
29            })
30            .collect::<Vec<_>>();
31
32        ffi::new_context(devices).into()
33    }
34
35    pub fn create_kernel_bundle_from_source(&self, source: &str) -> SourceKernelBundle {
36        kernel_bundle::ffi::create_kernel_bundle_from_source(&self.0, source).into()
37    }
38}