/
allocator.h
80 lines (76 loc) · 1.82 KB
/
allocator.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
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
#ifndef ISPM_ALLOCATOR_H
#define ISPM_ALLOCATOR_H
#include <cuda_runtime.h>
#include "check.h"
template<typename T>
class cuda_device_allocator: public std::allocator<T>
{
typedef std::allocator<T> parent;
public:
using typename parent::pointer;
using typename parent::size_type;
pointer allocate(size_type n, const void * = 0)
{
pointer p;
CUDA_CHECK(cudaMalloc(&p, n * sizeof(T)));
CUDA_CHECK(cudaMemset(p, 0, n * sizeof(T)));
return p;
}
void deallocate(pointer p, size_type)
{
CUDA_CHECK(cudaFree(p));
}
template<typename O>
struct rebind
{
typedef cuda_device_allocator<O> other;
};
};
template<typename T1, typename T2>
inline bool operator==(const cuda_device_allocator<T1> &,
const cuda_device_allocator<T2> &)
{
return true;
}
template<typename T1, typename T2>
inline bool operator!=(const cuda_device_allocator<T1> &,
const cuda_device_allocator<T2> &)
{
return false;
}
template<typename T>
class cuda_pinned_allocator: public std::allocator<T>
{
typedef std::allocator<T> parent;
public:
using typename parent::pointer;
using typename parent::size_type;
pointer allocate(size_type n, const void * = 0)
{
pointer p;
CUDA_CHECK(cudaHostAlloc(&p, n * sizeof(T), cudaHostAllocMapped));
return p;
}
void deallocate(pointer p, size_type)
{
CUDA_CHECK(cudaFreeHost(p));
}
template<typename O>
struct rebind
{
typedef cuda_pinned_allocator<O> other;
};
};
template<typename T1, typename T2>
inline bool operator==(const cuda_pinned_allocator<T1> &,
const cuda_pinned_allocator<T2> &)
{
return true;
}
template<typename T1, typename T2>
inline bool operator!=(const cuda_pinned_allocator<T1> &,
const cuda_pinned_allocator<T2> &)
{
return false;
}
#endif