Skip to content

Commit 4f8bc92

Browse files
committed
Feat: finish primary context switch
1 parent 1b16977 commit 4f8bc92

File tree

8 files changed

+1460
-138
lines changed

8 files changed

+1460
-138
lines changed

crates/cust/CHANGELOG.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,19 @@ Notable changes to this project will be documented in this file.
44

55
## [Unreleased]
66

7+
### Context handling overhaul
8+
9+
The way that contexts are handled in cust has been completely overhauled, it now
10+
uses primary context handling instead of the normal driver API context APIs. This
11+
is aimed at future-proofing cust for libraries such as cuBLAS and cuFFT, as well as
12+
overall simplifying the context handling APIs. This does mean that the API changed a bit:
13+
- `create_and_push` is now `new` and it only takes a device, not a device and flags.
14+
- `set_flags` is now used for setting context flags.
15+
- `ContextStack`, `UnownedContext`, and other legacy APIs are gone.
16+
17+
The old context handling is fully present in `cust::context::legacy` for anyone who needs it for specific reasons. If you use `quick_init` you don't need to worry about
18+
any breaking changes, the API is the same.
19+
720
## 0.2.2 - 12/5/21
821

922
- Update find_cuda_helper to 0.2

crates/cust/src/context.rs renamed to crates/cust/src/context/legacy.rs

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//! Legacy (old) context management which preceded primary contexts.
2+
//!
13
//! # CUDA context management
24
//!
35
//! Most CUDA functions require a context. A CUDA context is analogous to a CPU process - it's
@@ -38,7 +40,7 @@
3840
//!
3941
//! ```
4042
//! use cust::device::Device;
41-
//! use cust::context::{Context, ContextFlags};
43+
//! use cust::context::legacy::{Context, ContextFlags};
4244
//! # use std::error::Error;
4345
//! # fn main () -> Result<(), Box<dyn Error>> {
4446
//!
@@ -57,7 +59,7 @@
5759
//! to the single context and pass it to each thread.
5860
//!
5961
//! ```
60-
//! # use cust::context::{Context, ContextFlags, CurrentContext};
62+
//! # use cust::context::legacy::{Context, ContextFlags, CurrentContext};
6163
//! # use cust::device::Device;
6264
//! # use std::error::Error;
6365
//! # fn main() -> Result<(), Box<dyn Error>> {
@@ -90,7 +92,7 @@
9092
//!
9193
//! ```
9294
//! # use cust::device::Device;
93-
//! # use cust::context::{Context, ContextStack, ContextFlags, CurrentContext};
95+
//! # use cust::context::legacy::{Context, ContextStack, ContextFlags, CurrentContext};
9496
//! # use std::error::Error;
9597
//! #
9698
//! # fn main() -> Result<(), Box<dyn Error>> {
@@ -112,6 +114,7 @@
112114
//! # }
113115
//! ```
114116
117+
use crate::context::ContextHandle;
115118
use crate::device::Device;
116119
use crate::error::{CudaResult, DropResult, ToResult};
117120
use crate::private::Sealed;
@@ -240,7 +243,7 @@ impl Context {
240243
///
241244
/// ```
242245
/// # use cust::device::Device;
243-
/// # use cust::context::{Context, ContextFlags};
246+
/// # use cust::context::legacy::{Context, ContextFlags};
244247
/// # use std::error::Error;
245248
/// #
246249
/// # fn main () -> Result<(), Box<dyn Error>> {
@@ -270,7 +273,7 @@ impl Context {
270273
///
271274
/// ```
272275
/// # use cust::device::Device;
273-
/// # use cust::context::{Context, ContextFlags};
276+
/// # use cust::context::legacy::{Context, ContextFlags};
274277
/// # use std::error::Error;
275278
/// #
276279
/// # fn main () -> Result<(), Box<dyn Error>> {
@@ -300,7 +303,7 @@ impl Context {
300303
////* */
301304
/// ```
302305
/// # use cust::device::Device;
303-
/// # use cust::context::{Context, ContextFlags};
306+
/// # use cust::context::legacy::{Context, ContextFlags};
304307
/// # use std::error::Error;
305308
/// #
306309
/// # fn main() -> Result<(), Box<dyn Error>> {
@@ -324,7 +327,7 @@ impl Context {
324327
///
325328
/// ```
326329
/// # use cust::device::Device;
327-
/// # use cust::context::{Context, ContextFlags};
330+
/// # use cust::context::legacy::{Context, ContextFlags};
328331
/// # use std::error::Error;
329332
/// #
330333
/// # fn main () -> Result<(), Box<dyn Error>> {
@@ -371,11 +374,6 @@ impl Drop for Context {
371374
}
372375
}
373376

374-
/// Sealed trait for `Context` and `UnownedContext`. Not intended for use outside of cust.
375-
pub trait ContextHandle: Sealed {
376-
#[doc(hidden)]
377-
fn get_inner(&self) -> CUcontext;
378-
}
379377
impl Sealed for Context {}
380378
impl ContextHandle for Context {
381379
fn get_inner(&self) -> CUcontext {
@@ -405,7 +403,7 @@ impl UnownedContext {
405403
///
406404
/// ```
407405
/// # use cust::device::Device;
408-
/// # use cust::context::{Context, ContextFlags};
406+
/// # use cust::context::legacy::{Context, ContextFlags};
409407
/// # use std::error::Error;
410408
/// #
411409
/// # fn main () -> Result<(), Box<dyn Error>> {
@@ -440,7 +438,7 @@ impl ContextStack {
440438
///
441439
/// ```
442440
/// # use cust::device::Device;
443-
/// # use cust::context::{Context, ContextFlags, ContextStack};
441+
/// # use cust::context::legacy ::{Context, ContextFlags, ContextStack};
444442
/// # use std::error::Error;
445443
/// #
446444
/// # fn main () -> Result<(), Box<dyn Error>> {
@@ -466,7 +464,7 @@ impl ContextStack {
466464
///
467465
/// ```
468466
/// # use cust::device::Device;
469-
/// # use cust::context::{Context, ContextFlags, ContextStack};
467+
/// # use cust::context::legacy::{Context, ContextFlags, ContextStack};
470468
/// # use std::error::Error;
471469
/// #
472470
/// # fn main () -> Result<(), Box<dyn Error>> {
@@ -513,7 +511,7 @@ impl CurrentContext {
513511
///
514512
/// ```
515513
/// # use cust::device::Device;
516-
/// # use cust::context::{ Context, ContextFlags, CurrentContext };
514+
/// # use cust::context::legacy::{ Context, ContextFlags, CurrentContext };
517515
/// # use std::error::Error;
518516
/// #
519517
/// # fn main () -> Result<(), Box<dyn Error>> {
@@ -539,7 +537,7 @@ impl CurrentContext {
539537
///
540538
/// ```
541539
/// # use cust::device::Device;
542-
/// # use cust::context::{ Context, ContextFlags, CurrentContext };
540+
/// # use cust::context::legacy::{ Context, ContextFlags, CurrentContext };
543541
/// # use std::error::Error;
544542
/// #
545543
/// # fn main () -> Result<(), Box<dyn Error>> {
@@ -564,7 +562,7 @@ impl CurrentContext {
564562
///
565563
/// ```
566564
/// # use cust::device::Device;
567-
/// # use cust::context::{ Context, ContextFlags, CurrentContext };
565+
/// # use cust::context::legacy::{ Context, ContextFlags, CurrentContext };
568566
/// # use std::error::Error;
569567
/// #
570568
/// # fn main () -> Result<(), Box<dyn Error>> {
@@ -589,7 +587,7 @@ impl CurrentContext {
589587
///
590588
/// ```
591589
/// # use cust::device::Device;
592-
/// # use cust::context::{ Context, ContextFlags, CurrentContext, ResourceLimit };
590+
/// # use cust::context::legacy::{ Context, ContextFlags, CurrentContext, ResourceLimit };
593591
/// # use std::error::Error;
594592
/// #
595593
/// # fn main () -> Result<(), Box<dyn Error>> {
@@ -614,7 +612,7 @@ impl CurrentContext {
614612
///
615613
/// ```
616614
/// # use cust::device::Device;
617-
/// # use cust::context::{ Context, ContextFlags, CurrentContext, ResourceLimit };
615+
/// # use cust::context::legacy::{ Context, ContextFlags, CurrentContext, ResourceLimit };
618616
/// # use std::error::Error;
619617
/// #
620618
/// # fn main () -> Result<(), Box<dyn Error>> {
@@ -646,7 +644,7 @@ impl CurrentContext {
646644
///
647645
/// ```
648646
/// # use cust::device::Device;
649-
/// # use cust::context::{ Context, ContextFlags, CurrentContext};
647+
/// # use cust::context::legacy::{ Context, ContextFlags, CurrentContext};
650648
/// # use std::error::Error;
651649
/// #
652650
/// # fn main () -> Result<(), Box<dyn Error>> {
@@ -686,7 +684,7 @@ impl CurrentContext {
686684
///
687685
/// ```
688686
/// # use cust::device::Device;
689-
/// # use cust::context::{ Context, ContextFlags, CurrentContext, CacheConfig };
687+
/// # use cust::context::legacy::{ Context, ContextFlags, CurrentContext, CacheConfig };
690688
/// # use std::error::Error;
691689
/// #
692690
/// # fn main () -> Result<(), Box<dyn Error>> {
@@ -730,7 +728,7 @@ impl CurrentContext {
730728
///
731729
/// ```
732730
/// # use cust::device::Device;
733-
/// # use cust::context::{ Context, ContextFlags, CurrentContext, ResourceLimit };
731+
/// # use cust::context::legacy::{ Context, ContextFlags, CurrentContext, ResourceLimit };
734732
/// # use std::error::Error;
735733
/// #
736734
/// # fn main () -> Result<(), Box<dyn Error>> {
@@ -757,7 +755,7 @@ impl CurrentContext {
757755
///
758756
/// ```
759757
/// # use cust::device::Device;
760-
/// # use cust::context::{ Context, ContextFlags, CurrentContext, SharedMemoryConfig };
758+
/// # use cust::context::legacy::{ Context, ContextFlags, CurrentContext, SharedMemoryConfig };
761759
/// # use std::error::Error;
762760
/// #
763761
/// # fn main () -> Result<(), Box<dyn Error>> {
@@ -778,7 +776,7 @@ impl CurrentContext {
778776
///
779777
/// ```
780778
/// # use cust::device::Device;
781-
/// # use cust::context::{ Context, ContextFlags, CurrentContext };
779+
/// # use cust::context::legacy::{ Context, ContextFlags, CurrentContext };
782780
/// # use std::error::Error;
783781
/// #
784782
/// # fn main () -> Result<(), Box<dyn Error>> {
@@ -807,7 +805,7 @@ impl CurrentContext {
807805
///
808806
/// ```
809807
/// # use cust::device::Device;
810-
/// # use cust::context::{ Context, ContextFlags, CurrentContext };
808+
/// # use cust::context::legacy::{ Context, ContextFlags, CurrentContext };
811809
/// # use std::error::Error;
812810
/// #
813811
/// # fn main () -> Result<(), Box<dyn Error>> {

0 commit comments

Comments
 (0)