forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MiscUtils.h
32 lines (28 loc) · 958 Bytes
/
MiscUtils.h
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
#pragma once
#include <ATen/cuda/Exceptions.h>
#include <ATen/cuda/CUDAContext.h>
#include <ATen/cuda/CUDAConfig.h>
#include <ATen/cuda/PinnedMemoryAllocator.h>
namespace at {
namespace native {
static inline int cuda_int_cast(int64_t value, const char* varname) {
auto result = static_cast<int>(value);
TORCH_CHECK(static_cast<int64_t>(result) == value,
"cuda_int_cast: The value of ", varname, "(", (long long)value,
") is too large to fit into a int (", sizeof(int), " bytes)");
return result;
}
// Creates an array of size elements of type T, backed by pinned memory
// wrapped in a Storage
template<class T>
static inline Storage pin_memory(int64_t size) {
auto* allocator = cuda::getPinnedMemoryAllocator();
int64_t adjusted_size = size * sizeof(T);
return Storage(
Storage::use_byte_size_t(),
adjusted_size,
allocator,
/*resizable=*/false);
}
} // namespace native
} // namespace at