Skip to main content

sycl_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 sycl_rs_sys::{context::ffi, kernel_bundle, types::ffi::DevicePtr};
10
11use crate::{Result, 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    /// Constructs a context for devices from the same platform.
25    ///
26    /// Returns an error if the device list is empty or context construction fails.
27    pub fn new(devices: &[&Device]) -> Result<Self> {
28        let devices = devices
29            .iter()
30            .map(|d| DevicePtr {
31                ptr: (*d).clone().0,
32            })
33            .collect::<Vec<_>>();
34
35        ffi::new_context(devices).map(Into::into)
36    }
37
38    pub fn create_kernel_bundle_from_source(&self, source: &str) -> Result<SourceKernelBundle> {
39        kernel_bundle::ffi::create_kernel_bundle_from_source(&self.0, source).map(Into::into)
40    }
41}