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
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
//! Provides a representation for memory across different frameworks.
//!
//! Memory is allocated by a device in a way that it is accessible for its computations.
//!
//! Normally you will want to use [SharedMemory][shared_mem] which handles synchronization
//! of the latest memory copy to the required device.
//!
//! [shared_mem]: ../shared_mem/index.html

#[cfg(feature = "native")]
use frameworks::native::flatbox::FlatBox;
#[cfg(feature = "opencl")]
use frameworks::opencl::memory::Memory as OpenCLMemory;
#[cfg(feature = "cuda")]
use frameworks::cuda::memory::Memory as CudaMemory;

/// Specifies Memory behavior accross frameworks.
pub trait IMemory { }

#[derive(Debug)]
/// Container for all known IMemory implementations
pub enum MemoryType {
    /// A Native Memory representation.
    #[cfg(feature = "native")]
    Native(FlatBox),
    /// A OpenCL Memory representation.
    #[cfg(feature = "opencl")]
    OpenCL(OpenCLMemory),
    /// A Cuda Memory representation.
    #[cfg(feature = "cuda")]
    Cuda(CudaMemory),
}

impl MemoryType {
    /// Extract the FlatBox if MemoryType is Native.
    #[cfg(feature = "native")]
    pub fn as_native(&self) -> Option<&FlatBox> {
        match *self {
            MemoryType::Native(ref ret) => Some(ret),
            #[cfg(any(feature = "cuda", feature = "opencl"))]
            _ => None,
        }
    }

    /// Extract the FlatBox mutably if MemoryType is Native.
    #[cfg(feature = "native")]
    pub fn as_mut_native(&mut self) -> Option<&mut FlatBox> {
        match *self {
            MemoryType::Native(ref mut ret) => Some(ret),
            #[cfg(any(feature = "cuda", feature = "opencl"))]
            _ => None,
        }
    }

    #[cfg(feature = "native")]
    /// Consumes the Memory and returns an owned OpenCL Memory.
    pub fn into_native(self) -> Option<FlatBox> {
        match self {
            MemoryType::Native(ret) => Some(ret),
            #[cfg(any(feature = "cuda", feature = "opencl"))]
            _ => None,
        }
    }

    /// Extract the OpenCL Memory if MemoryType is OpenCL.
    #[cfg(feature = "opencl")]
    pub fn as_opencl(&self) -> Option<&OpenCLMemory> {
        match *self {
            MemoryType::OpenCL(ref ret) => Some(ret),
            #[cfg(any(feature = "cuda", feature = "native"))]
            _ => None,
        }
    }

    /// Extract the OpenCL Memory mutably if MemoryType is OpenCL.
    #[cfg(feature = "opencl")]
    pub fn as_mut_opencl(&mut self) -> Option<&mut OpenCLMemory> {
        match *self {
            MemoryType::OpenCL(ref mut ret) => Some(ret),
            #[cfg(any(feature = "cuda", feature = "native"))]
            _ => None,
        }
    }

    #[cfg(feature = "opencl")]
    /// Consumes the Memory and returns an owned OpenCL Memory.
    pub fn into_opencl(self) -> Option<OpenCLMemory> {
        match self {
            MemoryType::OpenCL(ret) => Some(ret),
            #[cfg(any(feature = "cuda", feature = "native"))]
            _ => None,
        }
    }

    #[cfg(feature = "cuda")]
    /// Extract the Cuda Memory if MemoryType is Cuda
    pub fn as_cuda(&self) -> Option<&CudaMemory> {
        match *self {
            MemoryType::Cuda(ref ret) => Some(ret),
            #[cfg(any(feature = "opencl", feature = "native"))]
            _ => None,
        }
    }

    #[cfg(feature = "cuda")]
    /// Extract the Cuda Memory mutably if MemoryType is Cuda
    pub fn as_mut_cuda(&mut self) -> Option<&mut CudaMemory> {
        match *self {
            MemoryType::Cuda(ref mut ret) => Some(ret),
            #[cfg(any(feature = "opencl", feature = "native"))]
            _ => None,
        }
    }

    #[cfg(feature = "cuda")]
    /// Consumes the Memory and returns an owned Cuda Memory.
    pub fn into_cuda(self) -> Option<CudaMemory> {
        match self {
            MemoryType::Cuda(ret) => Some(ret),
            #[cfg(any(feature = "opencl", feature = "native"))]
            _ => None,
        }
    }
}