Apollo 10.0
自动驾驶开放平台
syncedmem.cc
浏览该文件的文档.
1/******************************************************************************
2COPYRIGHT
3
4All contributions by the University of California:
5Copyright (c) 2014-2017 The Regents of the University of California (Regents)
6All rights reserved.
7
8All other contributions:
9Copyright (c) 2014-2017, the respective contributors
10All rights reserved.
11
12Caffe uses a shared copyright model: each contributor holds copyright over
13their contributions to Caffe. The project versioning records all such
14contribution and copyright details. If a contributor wants to further mark
15their specific copyright on a particular contribution, they should indicate
16their copyright solely in the commit message of the change when it is
17committed.
18
19LICENSE
20
21Redistribution and use in source and binary forms, with or without
22modification, are permitted provided that the following conditions are met:
23
241. Redistributions of source code must retain the above copyright notice, this
25 list of conditions and the following disclaimer.
262. Redistributions in binary form must reproduce the above copyright notice,
27 this list of conditions and the following disclaimer in the documentation
28 and/or other materials provided with the distribution.
29
30THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
31ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
32WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
33DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
34ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
35(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
36LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
37ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
39SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40
41CONTRIBUTION AGREEMENT
42
43By contributing to the BVLC/caffe repository through pull-request, comment,
44or otherwise, the contributor releases their content to the
45license and copyright terms herein.
46 *****************************************************************************/
47
48/******************************************************************************
49 * Copyright 2018 The Apollo Authors. All Rights Reserved.
50 *
51 * Licensed under the Apache License, Version 2.0 (the "License");
52 * you may not use this file except in compliance with the License.
53 * You may obtain a copy of the License at
54 *
55 * http://www.apache.org/licenses/LICENSE-2.0
56 *
57 * Unless required by applicable law or agreed to in writing, software
58 * distributed under the License is distributed on an "AS IS" BASIS,
59 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
60 * See the License for the specific language governing permissions and
61 * limitations under the License.
62 *****************************************************************************/
64
65namespace apollo {
66namespace perception {
67namespace base {
68
70 : cpu_ptr_(NULL),
71 gpu_ptr_(NULL),
72 size_(0),
73 head_(UNINITIALIZED),
74 own_cpu_data_(false),
75 cpu_malloc_use_cuda_(use_cuda),
76 own_gpu_data_(false),
77 device_(-1) {
78#if USE_GPU == 1
79#ifdef PERCEPTION_DEBUG
80 BASE_GPU_CHECK(cudaGetDevice(&device_));
81#endif
82#endif
83}
84
85SyncedMemory::SyncedMemory(size_t size, bool use_cuda)
86 : cpu_ptr_(NULL),
87 gpu_ptr_(NULL),
88 size_(size),
89 head_(UNINITIALIZED),
90 own_cpu_data_(false),
91 cpu_malloc_use_cuda_(use_cuda),
92 own_gpu_data_(false),
93 device_(-1) {
94#if USE_GPU == 1
95#ifdef PERCEPTION_DEBUG
96 BASE_GPU_CHECK(cudaGetDevice(&device_));
97#endif
98#endif
99}
100
102 check_device();
103 if (cpu_ptr_ && own_cpu_data_) {
104 PerceptionFreeHost(cpu_ptr_, cpu_malloc_use_cuda_);
105 }
106
107#if USE_GPU == 1
108 if (gpu_ptr_ && own_gpu_data_) {
109 BASE_GPU_CHECK(cudaFree(gpu_ptr_));
110 }
111#endif // USE_GPU
112}
113
114inline void SyncedMemory::to_cpu() {
115 check_device();
116 switch (head_) {
117 case UNINITIALIZED:
118 PerceptionMallocHost(&cpu_ptr_, size_, cpu_malloc_use_cuda_);
119 if (cpu_ptr_ == nullptr) {
120 AERROR << "cpu_ptr_ is null";
121 return;
122 }
123 memset(cpu_ptr_, 0, size_);
124 head_ = HEAD_AT_CPU;
125 own_cpu_data_ = true;
126 break;
127 case HEAD_AT_GPU:
128#if USE_GPU == 1
129 if (cpu_ptr_ == nullptr) {
130 PerceptionMallocHost(&cpu_ptr_, size_, cpu_malloc_use_cuda_);
131 own_cpu_data_ = true;
132 }
133 BASE_GPU_CHECK(cudaMemcpy(cpu_ptr_, gpu_ptr_, size_, cudaMemcpyDefault));
134 head_ = SYNCED;
135#else
136 NO_GPU;
137#endif
138 break;
139 case HEAD_AT_CPU:
140 case SYNCED:
141 break;
142 }
143}
144
145inline void SyncedMemory::to_gpu() {
146 check_device();
147#if USE_GPU == 1
148 switch (head_) {
149 case UNINITIALIZED:
150 BASE_GPU_CHECK(cudaMalloc(&gpu_ptr_, size_));
151 BASE_GPU_CHECK(cudaMemset(gpu_ptr_, 0, size_));
152 head_ = HEAD_AT_GPU;
153 own_gpu_data_ = true;
154 break;
155 case HEAD_AT_CPU:
156 if (gpu_ptr_ == nullptr) {
157 BASE_GPU_CHECK(cudaMalloc(&gpu_ptr_, size_));
158 own_gpu_data_ = true;
159 }
160 BASE_GPU_CHECK(cudaMemcpy(gpu_ptr_, cpu_ptr_, size_, cudaMemcpyDefault));
161 head_ = SYNCED;
162 break;
163 case HEAD_AT_GPU:
164 case SYNCED:
165 break;
166 }
167#else
168 NO_GPU;
169#endif
170}
171
173 check_device();
174 to_cpu();
175 return (const void*)cpu_ptr_;
176}
177
179 check_device();
180 ACHECK(data);
181 if (own_cpu_data_) {
182 PerceptionFreeHost(cpu_ptr_, cpu_malloc_use_cuda_);
183 }
184 cpu_ptr_ = data;
185 head_ = HEAD_AT_CPU;
186 own_cpu_data_ = false;
187}
188
190 check_device();
191#if USE_GPU == 1
192 to_gpu();
193 return (const void*)gpu_ptr_;
194#else
195 NO_GPU;
196 return nullptr;
197#endif
198}
199
201 check_device();
202#if USE_GPU == 1
203 ACHECK(data);
204 if (own_gpu_data_) {
205 BASE_GPU_CHECK(cudaFree(gpu_ptr_));
206 }
207 gpu_ptr_ = data;
208 head_ = HEAD_AT_GPU;
209 own_gpu_data_ = false;
210#else
211 NO_GPU;
212#endif
213}
214
216 check_device();
217 to_cpu();
218 head_ = HEAD_AT_CPU;
219 return cpu_ptr_;
220}
221
223 check_device();
224#if USE_GPU == 1
225 to_gpu();
226 head_ = HEAD_AT_GPU;
227 return gpu_ptr_;
228#else
229 NO_GPU;
230 return nullptr;
231#endif
232}
233
234#if USE_GPU == 1
235void SyncedMemory::async_gpu_push(const cudaStream_t& stream) {
236 check_device();
237 CHECK_EQ(head_, HEAD_AT_CPU);
238 if (gpu_ptr_ == nullptr) {
239 BASE_GPU_CHECK(cudaMalloc(&gpu_ptr_, size_));
240 own_gpu_data_ = true;
241 }
242 const cudaMemcpyKind put = cudaMemcpyHostToDevice;
243 BASE_GPU_CHECK(cudaMemcpyAsync(gpu_ptr_, cpu_ptr_, size_, put, stream));
244 // Assume caller will synchronize on the stream before use
245 head_ = SYNCED;
246}
247#endif
248
249void SyncedMemory::check_device() {
250#if USE_GPU == 1
251#ifdef PERCEPTION_DEBUG
252 int device;
253 cudaGetDevice(&device);
254 CHECK_EQ(device, device_);
255 if (gpu_ptr_ && own_gpu_data_) {
256 cudaPointerAttributes attributes;
257 BASE_GPU_CHECK(cudaPointerGetAttributes(&attributes, gpu_ptr_));
258 CHECK_EQ(attributes.device, device_);
259 }
260#endif
261#endif
262}
263
264} // namespace base
265} // namespace perception
266} // namespace apollo
first check imutoantoffset saved in device
Definition readme.txt:2
#define ACHECK(cond)
Definition log.h:80
#define AERROR
Definition log.h:44
void PerceptionFreeHost(void *ptr, bool use_cuda)
Definition syncedmem.h:83
void PerceptionMallocHost(void **ptr, size_t size, bool use_cuda)
Definition syncedmem.h:72
class register implement
Definition arena_queue.h:37
#define NO_GPU
Definition common.h:69