aboutsummaryrefslogtreecommitdiffhomepage
path: root/zluda/tests/primary_context.rs
blob: 4f726a2253806ecca2acdf6e748eec034487b83a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
use crate::common::CudaDriverFns;
use cuda_types::*;
use std::{mem, ptr};
mod common;

cuda_driver_test!(primary_context);

unsafe fn primary_context<T: CudaDriverFns>(cuda: T) {
    assert_eq!(cuda.cuInit(0), CUresult::CUDA_SUCCESS);
    let mut flags = 0;
    let mut active = 0;
    assert_eq!(
        cuda.cuDevicePrimaryCtxGetState(CUdevice_v1(0), &mut flags, &mut active),
        CUresult::CUDA_SUCCESS
    );
    assert_eq!((0, 0), (flags, active));
    assert_eq!(
        cuda.cuDevicePrimaryCtxSetFlags_v2(CUdevice_v1(0), 1),
        CUresult::CUDA_SUCCESS
    );
    let mut primary_ctx = ptr::null_mut();
    assert_eq!(
        cuda.cuDevicePrimaryCtxRetain(&mut primary_ctx, CUdevice_v1(0)),
        CUresult::CUDA_SUCCESS
    );
    assert_ne!(primary_ctx, ptr::null_mut());
    let mut active_ctx = ptr::null_mut();
    assert_eq!(
        cuda.cuCtxGetCurrent(&mut active_ctx),
        CUresult::CUDA_SUCCESS
    );
    assert_ne!(primary_ctx, active_ctx);
    assert_eq!(
        cuda.cuDevicePrimaryCtxGetState(CUdevice_v1(0), &mut flags, &mut active),
        CUresult::CUDA_SUCCESS
    );
    assert_eq!((1, 1), (flags, active));
    let mut buffer = mem::zeroed();
    assert_eq!(
        cuda.cuCtxPushCurrent_v2(primary_ctx),
        CUresult::CUDA_SUCCESS
    );
    assert_eq!(cuda.cuMemAlloc_v2(&mut buffer, 4), CUresult::CUDA_SUCCESS);
    assert_eq!(
        cuda.cuDevicePrimaryCtxRelease_v2(CUdevice_v1(0)),
        CUresult::CUDA_SUCCESS
    );
    assert_eq!(
        cuda.cuDevicePrimaryCtxGetState(CUdevice_v1(0), &mut flags, &mut active),
        CUresult::CUDA_SUCCESS
    );
    assert_ne!(
        cuda.cuDevicePrimaryCtxRelease_v2(CUdevice_v1(0)),
        CUresult::CUDA_SUCCESS
    );
    assert_eq!((0, 0), (flags, active));
    // Already freed on context destruction
    // TODO: reenable when we start tracking allocations inside context
    //assert_ne!(cuda.cuMemFree_v2(buffer), CUresult::CUDA_SUCCESS);
    assert_eq!(
        cuda.cuDevicePrimaryCtxReset_v2(CUdevice_v1(0)),
        CUresult::CUDA_SUCCESS
    );
}