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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
// Copyright 2019-2022 Clemens Lutz
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Heterogeneous memory allocator.
//!
//! Presents a consistent interface for allocating memory with specific
//! properties. Examples include allocating NUMA-local memory, or allocating
//! CUDA device memory.
//!
//! The allocated memory is of type Mem, and specialized to DerefMem whenever
//! possible.

use rustacuda::memory::{DeviceBuffer, DeviceCopy, LockedBuffer, UnifiedBuffer};

use std::alloc::{self, Layout};
use std::cell::RefCell;
use std::convert::TryFrom;
use std::default::Default;
use std::mem::size_of;
use std::rc::Rc;
use std::slice;

use super::hw_info::ProcessorCache;
use super::memory::{DerefMem, Mem, PageLock};
use super::numa::{DistributedNumaMemory, NodeLen, NodeRatio, NumaMemory, PageType};
use crate::error::{Error, ErrorKind, Result};

/// Heterogeneous memory allocator.
pub struct Allocator;

/// Memory type specifier
///
/// Some memory types cannot be directly accessed on the host, e.g., CudaDevMem.
#[derive(Clone, Debug, PartialEq)]
pub enum MemType {
    /// System memory allocated with Rust's global allocator
    SysMem,
    /// Aligned system memory allocated with Rust's global allocator
    ///
    /// Alignment is specified in bytes.
    AlignedSysMem { align_bytes: usize },
    /// NUMA memory allocated on the specified NUMA node and with the specified page type
    NumaMem { node: u16, page_type: PageType },
    /// NUMA memory allocated on the specified NUMA node and pinned with CUDA
    NumaPinnedMem { node: u16, page_type: PageType },
    /// NUMA memory distributed in proportion to a ratio over multiple NUMA nodes
    DistributedNumaMem {
        nodes: Box<[NodeRatio]>,
        page_type: PageType,
    },
    /// NUMA memory distributed over multiple NUMA nodes using a length per node
    DistributedNumaMemWithLen {
        nodes: Box<[NodeLen]>,
        page_type: PageType,
    },
    /// CUDA pinned memory (using cudaHostAlloc())
    CudaPinnedMem,
    /// CUDA unified memory
    CudaUniMem,
    /// CUDA device memory
    CudaDevMem,
}

/// Dereferencable memory type specifier
///
/// These memory types can be directly accessed on the host.
#[derive(Clone, Debug, PartialEq)]
pub enum DerefMemType {
    /// System memory allocated with Rust's global allocator
    SysMem,
    /// Aligned system memory allocated with Rust's global allocator
    ///
    /// Alignment is specified in bytes.
    AlignedSysMem { align_bytes: usize },
    /// NUMA memory allocated on the specified NUMA node and with the specified page type
    NumaMem { node: u16, page_type: PageType },
    /// NUMA memory allocated on the specified NUMA node, with the specified page type, and pinned with CUDA
    NumaPinnedMem { node: u16, page_type: PageType },
    /// NUMA memory distributed in proportion to a ratio over multiple NUMA nodes
    DistributedNumaMem {
        nodes: Box<[NodeRatio]>,
        page_type: PageType,
    },
    /// NUMA memory distributed over multiple NUMA nodes using a length per node
    DistributedNumaMemWithLen {
        nodes: Box<[NodeLen]>,
        page_type: PageType,
    },
    /// CUDA pinned memory (using cudaHostAlloc())
    CudaPinnedMem,
    /// CUDA unified memory
    CudaUniMem,
}

#[derive(Clone, Debug, PartialEq)]
pub enum CacheSpillType {
    NoSpill(MemType),
    CacheAndSpill {
        cache_node: u16,
        spill_node: u16,
        page_type: PageType,
    },
}

impl MemType {
    pub fn page_type(&self) -> PageType {
        match *self {
            MemType::NumaMem { page_type, .. } => page_type,
            MemType::NumaPinnedMem { page_type, .. } => page_type,
            MemType::DistributedNumaMem { page_type, .. } => page_type,
            MemType::DistributedNumaMemWithLen { page_type, .. } => page_type,
            MemType::SysMem
            | MemType::AlignedSysMem { .. }
            | MemType::CudaPinnedMem
            | MemType::CudaUniMem => PageType::Default,
            MemType::CudaDevMem => PageType::Default,
        }
    }
}

impl DerefMemType {
    pub fn page_type(&self) -> PageType {
        match *self {
            DerefMemType::NumaMem { page_type, .. } => page_type,
            DerefMemType::NumaPinnedMem { page_type, .. } => page_type,
            DerefMemType::DistributedNumaMem { page_type, .. } => page_type,
            DerefMemType::DistributedNumaMemWithLen { page_type, .. } => page_type,
            DerefMemType::SysMem
            | DerefMemType::AlignedSysMem { .. }
            | DerefMemType::CudaPinnedMem
            | DerefMemType::CudaUniMem => PageType::Default,
        }
    }
}

impl From<DerefMemType> for MemType {
    fn from(dmt: DerefMemType) -> Self {
        match dmt {
            DerefMemType::SysMem => MemType::SysMem,
            DerefMemType::AlignedSysMem { align_bytes } => MemType::AlignedSysMem { align_bytes },
            DerefMemType::NumaMem { node, page_type } => MemType::NumaMem { node, page_type },
            DerefMemType::NumaPinnedMem { node, page_type } => {
                MemType::NumaPinnedMem { node, page_type }
            }
            DerefMemType::DistributedNumaMem { nodes, page_type } => {
                MemType::DistributedNumaMem { nodes, page_type }
            }
            DerefMemType::DistributedNumaMemWithLen { nodes, page_type } => {
                MemType::DistributedNumaMemWithLen { nodes, page_type }
            }
            DerefMemType::CudaPinnedMem => MemType::CudaPinnedMem,
            DerefMemType::CudaUniMem => MemType::CudaUniMem,
        }
    }
}

impl From<MemType> for CacheSpillType {
    fn from(mem_type: MemType) -> CacheSpillType {
        CacheSpillType::NoSpill(mem_type)
    }
}

impl TryFrom<MemType> for DerefMemType {
    type Error = Error;

    fn try_from(mt: MemType) -> Result<Self> {
        match mt {
            MemType::SysMem => Ok(DerefMemType::SysMem),
            MemType::AlignedSysMem { align_bytes } => {
                Ok(DerefMemType::AlignedSysMem { align_bytes })
            }
            MemType::NumaMem { node, page_type } => Ok(DerefMemType::NumaMem { node, page_type }),
            MemType::NumaPinnedMem { node, page_type } => {
                Ok(DerefMemType::NumaPinnedMem { node, page_type })
            }
            MemType::DistributedNumaMem { nodes, page_type } => {
                Ok(DerefMemType::DistributedNumaMem { nodes, page_type })
            }
            MemType::DistributedNumaMemWithLen { nodes, page_type } => {
                Ok(DerefMemType::DistributedNumaMemWithLen { nodes, page_type })
            }
            MemType::CudaPinnedMem => Ok(DerefMemType::CudaPinnedMem),
            MemType::CudaUniMem => Ok(DerefMemType::CudaUniMem),
            MemType::CudaDevMem => Err(ErrorKind::InvalidConversion(
                "Cannot convert device memory to &[T] slice",
            )
            .into()),
        }
    }
}

/// Generic memory allocator for Mem that hides concrete memory type
///
/// The intended use-case is when a callee (such as a library) must allocate
/// memory. In this case, the caller can pass in a generic memory allocator
/// This allows the callee to generalize over all memory types.
pub type MemAllocFn<T> = Box<dyn Fn(usize) -> Mem<T>>;

/// Generic memory allocator for DerefMem that hides concrete memory type
///
/// The intended use-case is when a callee (such as a library) must allocate
/// memory. In this case, the caller can pass in a generic memory allocator
/// This allows the callee to generalize over all memory types.
pub type DerefMemAllocFn<T> = Box<dyn Fn(usize) -> DerefMem<T>>;

/// A curried memory allocator for caching and spilling memory
///
/// Takes as an argument the maximum GPU cache length.
pub type MemSpillAllocFn<T> = Box<dyn Fn(usize) -> MemAllocFn<T>>;

impl Allocator {
    /// Allocates memory of the specified type
    pub fn alloc_mem<T: Clone + Default + DeviceCopy>(mem_type: MemType, len: usize) -> Mem<T> {
        match mem_type {
            MemType::SysMem => Self::alloc_system(len).into(),
            MemType::AlignedSysMem { align_bytes } => Self::alloc_aligned(len, align_bytes).into(),
            MemType::NumaMem { node, page_type } => Self::alloc_numa(len, node, page_type).into(),
            MemType::NumaPinnedMem { node, page_type } => {
                Self::alloc_numa_pinned(len, node, page_type).into()
            }
            MemType::DistributedNumaMem { nodes, page_type } => {
                Self::alloc_distributed_numa(len, nodes, page_type).into()
            }
            MemType::DistributedNumaMemWithLen { nodes, page_type } => {
                Self::alloc_distributed_numa_with_len(len, nodes, page_type).into()
            }
            MemType::CudaPinnedMem => Self::alloc_cuda_pinned(len).into(),
            MemType::CudaUniMem => Self::alloc_cuda_unified(len).into(),
            MemType::CudaDevMem => Self::alloc_cuda_device(len),
        }
    }

    /// Allocates host-dereferencable memory of the specified type
    pub fn alloc_deref_mem<T: Clone + Default + DeviceCopy>(
        mem_type: DerefMemType,
        len: usize,
    ) -> DerefMem<T> {
        match mem_type {
            DerefMemType::SysMem => Self::alloc_system(len),
            DerefMemType::AlignedSysMem { align_bytes } => {
                Self::alloc_aligned(len, align_bytes).into()
            }
            DerefMemType::NumaMem { node, page_type } => Self::alloc_numa(len, node, page_type),
            DerefMemType::NumaPinnedMem { node, page_type } => {
                Self::alloc_numa_pinned(len, node, page_type).into()
            }
            DerefMemType::DistributedNumaMem { nodes, page_type } => {
                Self::alloc_distributed_numa(len, nodes, page_type).into()
            }
            DerefMemType::DistributedNumaMemWithLen { nodes, page_type } => {
                Self::alloc_distributed_numa_with_len(len, nodes, page_type).into()
            }
            DerefMemType::CudaPinnedMem => Self::alloc_cuda_pinned(len),
            DerefMemType::CudaUniMem => Self::alloc_cuda_unified(len),
        }
    }

    /// Returns a generic 'Mem' memory allocator that allocates memory of the
    /// specified 'Mem' type.
    pub fn mem_alloc_fn<T: Clone + Default + DeviceCopy>(mem_type: MemType) -> MemAllocFn<T> {
        match mem_type {
            MemType::SysMem => Box::new(|len| Self::alloc_system(len).into()),
            MemType::AlignedSysMem { align_bytes } => {
                Box::new(move |len| Self::alloc_aligned(len, align_bytes).into())
            }
            MemType::NumaMem { node, page_type } => {
                Box::new(move |len| Self::alloc_numa(len, node, page_type).into())
            }
            MemType::NumaPinnedMem { node, page_type } => {
                Box::new(move |len| Self::alloc_numa_pinned(len, node, page_type).into())
            }
            MemType::DistributedNumaMem { nodes, page_type } => Box::new(move |len| {
                Self::alloc_distributed_numa(len, nodes.clone(), page_type).into()
            }),
            MemType::DistributedNumaMemWithLen { nodes, page_type } => Box::new(move |len| {
                Self::alloc_distributed_numa_with_len(len, nodes.clone(), page_type).into()
            }),
            MemType::CudaPinnedMem => Box::new(|len| Self::alloc_cuda_pinned(len).into()),
            MemType::CudaUniMem => Box::new(|len| Self::alloc_cuda_unified(len).into()),
            MemType::CudaDevMem => Box::new(|len| Self::alloc_cuda_device(len)),
        }
    }

    /// Returns a generic 'DerefMem' memory allocator that allocates memory of
    /// the specified 'DerefMem' type.
    pub fn deref_mem_alloc_fn<T: Clone + Default + DeviceCopy>(
        mem_type: DerefMemType,
    ) -> DerefMemAllocFn<T> {
        match mem_type {
            DerefMemType::SysMem => Box::new(|len| Self::alloc_system(len)),
            DerefMemType::AlignedSysMem { align_bytes } => {
                Box::new(move |len| Self::alloc_aligned(len, align_bytes).into())
            }
            DerefMemType::NumaMem { node, page_type } => {
                Box::new(move |len| Self::alloc_numa(len, node, page_type))
            }
            DerefMemType::NumaPinnedMem { node, page_type } => {
                Box::new(move |len| Self::alloc_numa_pinned(len, node, page_type))
            }
            DerefMemType::DistributedNumaMem { nodes, page_type } => Box::new(move |len| {
                Self::alloc_distributed_numa(len, nodes.clone(), page_type).into()
            }),
            DerefMemType::DistributedNumaMemWithLen { nodes, page_type } => Box::new(move |len| {
                Self::alloc_distributed_numa_with_len(len, nodes.clone(), page_type).into()
            }),
            DerefMemType::CudaPinnedMem => Box::new(|len| Self::alloc_cuda_pinned(len)),
            DerefMemType::CudaUniMem => Box::new(|len| Self::alloc_cuda_unified(len)),
        }
    }

    /// Allocates system memory using Rust's global allocator.
    fn alloc_system<T: Clone + Default + DeviceCopy>(len: usize) -> DerefMem<T> {
        DerefMem::SysMem(vec![T::default(); len])
    }

    /// Allocates aligned system memory using Rust's global allocator.
    fn alloc_aligned<T: Clone + Default + DeviceCopy>(len: usize, alignment: usize) -> DerefMem<T> {
        let mem = unsafe {
            let layout = Layout::from_size_align(len * size_of::<T>(), alignment)
                .expect("Memory alignment must be at least size of T");
            let ptr = alloc::alloc(layout) as *mut T;
            assert!(!ptr.is_null(), "Failed to allocate aligned memory");

            let slice = slice::from_raw_parts_mut(ptr, len);
            slice.iter_mut().for_each(|x| *x = T::default());

            let output: Box<[T]> = Box::from_raw(slice);
            output
        };
        DerefMem::BoxedSysMem(mem)
    }

    /// Allocates memory on the specified NUMA node.
    fn alloc_numa<T: DeviceCopy>(len: usize, node: u16, page_type: PageType) -> DerefMem<T> {
        DerefMem::NumaMem(NumaMemory::new(len, node, page_type))
    }

    /// Allocates pinned memory on the specified NUMA node.
    fn alloc_numa_pinned<T: DeviceCopy>(len: usize, node: u16, page_type: PageType) -> DerefMem<T> {
        let mut mem = NumaMemory::new(len, node, page_type);
        mem.page_lock().expect("Failed to pin memory");
        DerefMem::NumaMem(mem)
    }

    /// Allocates memory on multiple, specified NUMA nodes.
    fn alloc_distributed_numa<T: DeviceCopy>(
        len: usize,
        nodes: Box<[NodeRatio]>,
        page_type: PageType,
    ) -> DerefMem<T> {
        DerefMem::DistributedNumaMem(DistributedNumaMemory::new_with_ratio(len, nodes, page_type))
    }

    /// Allocates memory on multiple, specified NUMA nodes.
    fn alloc_distributed_numa_with_len<T: DeviceCopy>(
        len: usize,
        nodes: Box<[NodeLen]>,
        page_type: PageType,
    ) -> DerefMem<T> {
        DerefMem::DistributedNumaMem(DistributedNumaMemory::new_with_len(len, nodes, page_type))
    }

    /// Allocates CUDA pinned memory using cudaHostAlloc
    ///
    /// Warning: Returns uninitialized memory. The reason is that CUDA allocates
    /// the memory local to the processor that first touches the memory. This
    /// decision is left to the user.
    fn alloc_cuda_pinned<T: Clone + Default + DeviceCopy>(len: usize) -> DerefMem<T> {
        DerefMem::CudaPinnedMem(LockedBuffer::<T>::new(&T::default(), len).expect(&format!(
            "Failed dot allocate {} bytes of CUDA pinned memory",
            len * size_of::<T>()
        )))
    }

    /// Allocates CUDA unified memory.
    ///
    /// Warning: Returns uninitialized memory. The reason is that CUDA allocates
    /// the memory local to the processor that first touches the memory. This
    /// decision is left to the user.
    fn alloc_cuda_unified<T: Clone + Default + DeviceCopy>(len: usize) -> DerefMem<T> {
        unsafe {
            DerefMem::CudaUniMem(UnifiedBuffer::<T>::uninitialized(len).expect(&format!(
                "Failed dot allocate {} bytes of CUDA unified memory",
                len * size_of::<T>()
            )))
        }
    }

    /// Allocates CUDA device memory.
    ///
    /// Device memory cannot be dereferenced on the host. To access it, use
    /// cudaMemcpy() to copy it to the host.
    ///
    /// Warning: Returns uninitialized memory. The reason is that the allocator
    /// cannot initialize the memory asynchronously, due to the user not
    /// providing a CUDA stream in the API.
    fn alloc_cuda_device<T: DeviceCopy>(len: usize) -> Mem<T> {
        unsafe {
            Mem::CudaDevMem(DeviceBuffer::<T>::uninitialized(len).expect(&format!(
                "Failed to allocate {} bytes of CUDA device memory",
                len * size_of::<T>()
            )))
        }
    }

    /// Captures the cache memory type and returns a function that returns an allocator
    ///
    /// Effectively, we're gathering the arguments by currying
    /// `mem_spill_alloc_fn_internal`.
    ///
    /// The returned allocator uses GPU memory until `cache_max_len` is reached.
    /// Then, the allocator spills the remainder to CPU memory.
    ///
    /// The functional programming approach used here solves the leaky abstraction
    /// problem. Oftentimes, the knowledge about how much cache space is available,
    /// and the total required space for the data relation reside in two different
    /// code modules. Thus, we aggregate this knowledge in a closure to retain
    /// modularity, instead of leaking internal details of the modules.
    ///
    /// Returns a "future" that is set to the cached length when the allocator is
    /// invoked, for logging purposes.
    pub fn mem_spill_alloc_fn<T>(
        cache_spill_type: CacheSpillType,
    ) -> (MemSpillAllocFn<T>, Rc<RefCell<Option<usize>>>)
    where
        T: Clone + Default + DeviceCopy,
    {
        let cached_len_future = Rc::new(RefCell::new(None));
        let cached_len_setter = cached_len_future.clone();

        let alloc: MemSpillAllocFn<T> = match cache_spill_type {
            CacheSpillType::CacheAndSpill {
                cache_node,
                spill_node,
                page_type,
            } => Box::new(move |cache_max_len| {
                Self::mem_spill_alloc_fn_internal(
                    cache_max_len,
                    cache_node,
                    spill_node,
                    page_type,
                    cached_len_setter.clone(),
                )
            }),
            CacheSpillType::NoSpill(mem_type) => {
                cached_len_setter.replace(None);
                let mem_alloc: MemSpillAllocFn<T> =
                    Box::new(move |_| Self::mem_alloc_fn(mem_type.clone()));
                mem_alloc
            }
        };

        (alloc, cached_len_future)
    }

    /// Builds an allocator that caches data in GPU memory
    fn mem_spill_alloc_fn_internal<T>(
        cache_max_len: usize,
        gpu_node: u16,
        cpu_node: u16,
        page_type: PageType,
        cached_len_setter: Rc<RefCell<Option<usize>>>,
    ) -> MemAllocFn<T>
    where
        T: Clone + Default + DeviceCopy,
    {
        // Round down to the page size. Assume huge pages, as this will also work
        // with small pages, but not vice-versa.
        let page_size = ProcessorCache::huge_page_size().expect("Failed to get the huge page size");
        let cache_max_len = (cache_max_len / page_size) * page_size;

        let alloc = move |len| {
            let (cached_len, spilled_len) = if len <= cache_max_len {
                (len, 0)
            } else {
                (cache_max_len, len - cache_max_len)
            };

            cached_len_setter.replace(Some(cached_len));

            let mem_type = MemType::DistributedNumaMemWithLen {
                nodes: Box::new([
                    NodeLen {
                        node: gpu_node,
                        len: cached_len,
                    },
                    NodeLen {
                        node: cpu_node,
                        len: spilled_len,
                    },
                ]),
                page_type,
            };

            Allocator::alloc_mem(mem_type, len)
        };

        Box::new(alloc)
    }
}