Apollo 10.0
自动驾驶开放平台
syncedmem.h
浏览该文件的文档.
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 *****************************************************************************/
63#pragma once
64
65#include "cyber/common/log.h"
67
68namespace apollo {
69namespace perception {
70namespace base {
71
72inline void PerceptionMallocHost(void** ptr, size_t size, bool use_cuda) {
73#if USE_GPU == 1
74 if (use_cuda) {
75 BASE_GPU_CHECK(cudaMallocHost(ptr, size));
76 return;
77 }
78#endif
79 *ptr = malloc(size);
80 ACHECK(*ptr) << "host allocation of size " << size << " failed";
81}
82
83inline void PerceptionFreeHost(void* ptr, bool use_cuda) {
84#if USE_GPU == 1
85 if (use_cuda) {
86 BASE_GPU_CHECK(cudaFreeHost(ptr));
87 return;
88 }
89#endif
90 free(ptr);
91}
92
100 public:
102
103 explicit SyncedMemory(bool use_cuda);
104 SyncedMemory(size_t size, bool use_cuda);
105 SyncedMemory(const SyncedMemory&) = delete;
106 void operator=(const SyncedMemory&) = delete;
108
109 const void* cpu_data();
110 void set_cpu_data(void* data);
111 const void* gpu_data();
112 void set_gpu_data(void* data);
113 void* mutable_cpu_data();
114 void* mutable_gpu_data();
115
116 SyncedHead head() const { return head_; }
117 void set_head(SyncedHead head) { head_ = head; }
120 size_t size() { return size_; }
121
122#if USE_GPU == 1
123 void async_gpu_push(const cudaStream_t& stream);
124#endif
125
126 private:
127 void check_device();
128 void to_cpu();
129 void to_gpu();
130
131 private:
132 void* cpu_ptr_;
133 void* gpu_ptr_;
134 size_t size_;
135 SyncedHead head_;
136 bool own_cpu_data_;
137 bool cpu_malloc_use_cuda_;
138 bool own_gpu_data_;
139 int device_;
140}; // class SyncedMemory
141
142} // namespace base
143} // namespace perception
144} // namespace apollo
Manages memory allocation and synchronization between the host (CPU) and device (GPU).
Definition syncedmem.h:99
void operator=(const SyncedMemory &)=delete
SyncedMemory(const SyncedMemory &)=delete
#define ACHECK(cond)
Definition log.h:80
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