Caffe2 - C++ API
A deep learning, cross platform ML framework
THCStorage.cpp
1 #ifndef THC_GENERIC_FILE
2 #define THC_GENERIC_FILE "THC/generic/THCStorage.cpp"
3 #else
4 
5 #include <c10/util/intrusive_ptr.h>
6 #include <c10/util/typeid.h>
7 
8 scalar_t* THCStorage_(data)(THCState *state, const THCStorage *self)
9 {
10  return self->data<scalar_t>();
11 }
12 
13 ptrdiff_t THCStorage_(size)(THCState *state, const THCStorage *self)
14 {
15  return THStorage_size(self);
16 }
17 
18 int THCStorage_(elementSize)(THCState *state)
19 {
20  return sizeof(scalar_t);
21 }
22 
23 void THCStorage_(set)(THCState *state, THCStorage *self, ptrdiff_t index, scalar_t value)
24 {
25  THArgCheck((index >= 0) && (index < self->numel()), 2, "index out of bounds");
26  cudaStream_t stream = THCState_getCurrentStream(state);
27  THCudaCheck(cudaMemcpyAsync(THCStorage_(data)(state, self) + index, &value, sizeof(scalar_t),
28  cudaMemcpyHostToDevice,
29  stream));
30  THCudaCheck(cudaStreamSynchronize(stream));
31 }
32 
33 scalar_t THCStorage_(get)(THCState *state, const THCStorage *self, ptrdiff_t index)
34 {
35  THArgCheck((index >= 0) && (index < self->numel()), 2, "index out of bounds");
36  scalar_t value;
37  cudaStream_t stream = THCState_getCurrentStream(state);
38  THCudaCheck(cudaMemcpyAsync(&value, THCStorage_(data)(state, self) + index, sizeof(scalar_t),
39  cudaMemcpyDeviceToHost, stream));
40  THCudaCheck(cudaStreamSynchronize(stream));
41  return value;
42 }
43 
44 THCStorage* THCStorage_(new)(THCState *state)
45 {
46  THStorage* storage = c10::make_intrusive<at::StorageImpl>(
47  caffe2::TypeMeta::Make<scalar_t>(),
48  0,
49  state->cudaDeviceAllocator,
50  true).release();
51  return storage;
52 }
53 
54 THCStorage* THCStorage_(newWithSize)(THCState *state, ptrdiff_t size)
55 {
56  THStorage* storage = c10::make_intrusive<at::StorageImpl>(
57  caffe2::TypeMeta::Make<scalar_t>(),
58  size,
59  state->cudaDeviceAllocator,
60  true).release();
61  return storage;
62 }
63 
64 THCStorage* THCStorage_(newWithAllocator)(THCState *state, ptrdiff_t size,
65  at::Allocator* allocator)
66 {
67  THStorage* storage = c10::make_intrusive<at::StorageImpl>(
68  caffe2::TypeMeta::Make<scalar_t>(),
69  size,
70  allocator,
71  true).release();
72  return storage;
73 }
74 
75 THCStorage* THCStorage_(newWithSize1)(THCState *state, scalar_t data0)
76 {
77  THCStorage *self = THCStorage_(newWithSize)(state, 1);
78  THCStorage_(set)(state, self, 0, data0);
79  return self;
80 }
81 
82 THCStorage* THCStorage_(newWithSize2)(THCState *state, scalar_t data0, scalar_t data1)
83 {
84  THCStorage *self = THCStorage_(newWithSize)(state, 2);
85  THCStorage_(set)(state, self, 0, data0);
86  THCStorage_(set)(state, self, 1, data1);
87  return self;
88 }
89 
90 THCStorage* THCStorage_(newWithSize3)(THCState *state, scalar_t data0, scalar_t data1, scalar_t data2)
91 {
92  THCStorage *self = THCStorage_(newWithSize)(state, 3);
93  THCStorage_(set)(state, self, 0, data0);
94  THCStorage_(set)(state, self, 1, data1);
95  THCStorage_(set)(state, self, 2, data2);
96  return self;
97 }
98 
99 THCStorage* THCStorage_(newWithSize4)(THCState *state, scalar_t data0, scalar_t data1, scalar_t data2, scalar_t data3)
100 {
101  THCStorage *self = THCStorage_(newWithSize)(state, 4);
102  THCStorage_(set)(state, self, 0, data0);
103  THCStorage_(set)(state, self, 1, data1);
104  THCStorage_(set)(state, self, 2, data2);
105  THCStorage_(set)(state, self, 3, data3);
106  return self;
107 }
108 
109 THCStorage* THCStorage_(newWithMapping)(THCState *state, const char *fileName, ptrdiff_t size, int isShared)
110 {
111  THError("not available yet for THCStorage");
112  return NULL;
113 }
114 
115 THCStorage* THCStorage_(newWithDataAndAllocator)(
116  THCState* state,
117  at::DataPtr&& data,
118  ptrdiff_t size,
119  at::Allocator* allocator) {
120  THStorage* storage = c10::make_intrusive<at::StorageImpl>(
121  caffe2::TypeMeta::Make<scalar_t>(),
122  size,
123  std::move(data),
124  allocator,
125  allocator != nullptr).release();
126  return storage;
127 }
128 
129 void THCStorage_(retain)(THCState *state, THCStorage *self)
130 {
131  THStorage_retain(self);
132 }
133 
134 void THCStorage_(free)(THCState *state, THCStorage *self)
135 {
136  THStorage_free(self);
137 }
138 #endif