Apollo  6.0
Open source self driving car software
syncedmem.h
Go to the documentation of this file.
1 /******************************************************************************
2 COPYRIGHT
3 
4 All contributions by the University of California:
5 Copyright (c) 2014-2017 The Regents of the University of California (Regents)
6 All rights reserved.
7 
8 All other contributions:
9 Copyright (c) 2014-2017, the respective contributors
10 All rights reserved.
11 
12 Caffe uses a shared copyright model: each contributor holds copyright over
13 their contributions to Caffe. The project versioning records all such
14 contribution and copyright details. If a contributor wants to further mark
15 their specific copyright on a particular contribution, they should indicate
16 their copyright solely in the commit message of the change when it is
17 committed.
18 
19 LICENSE
20 
21 Redistribution and use in source and binary forms, with or without
22 modification, are permitted provided that the following conditions are met:
23 
24 1. Redistributions of source code must retain the above copyright notice, this
25  list of conditions and the following disclaimer.
26 2. 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 
30 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
31 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
32 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
33 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
34 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
35 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
36 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
37 ON 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
39 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 
41 CONTRIBUTION AGREEMENT
42 
43 By contributing to the BVLC/caffe repository through pull-request, comment,
44 or otherwise, the contributor releases their content to the
45 license 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 
68 namespace apollo {
69 namespace perception {
70 namespace base {
71 
72 inline void PerceptionMallocHost(void** ptr, size_t size, bool use_cuda) {
73 #if USE_GPU == 1
74  if (use_cuda) {
75  BASE_CUDA_CHECK(cudaMallocHost(ptr, size));
76  return;
77  }
78 #endif
79  *ptr = malloc(size);
80  ACHECK(*ptr) << "host allocation of size " << size << " failed";
81 }
82 
83 inline void PerceptionFreeHost(void* ptr, bool use_cuda) {
84 #if USE_GPU == 1
85  if (use_cuda) {
86  BASE_CUDA_CHECK(cudaFreeHost(ptr));
87  return;
88  }
89 #endif
90  free(ptr);
91 }
92 
99 class SyncedMemory {
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;
107  ~SyncedMemory();
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
#define ACHECK(cond)
Definition: log.h:80
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
SyncedHead
Definition: syncedmem.h:101
SyncedHead head() const
Definition: syncedmem.h:116
void operator=(const SyncedMemory &)=delete
void set_head_cpu()
Definition: syncedmem.h:119
void PerceptionMallocHost(void **ptr, size_t size, bool use_cuda)
Definition: syncedmem.h:72
void set_head_gpu()
Definition: syncedmem.h:118
void set_head(SyncedHead head)
Definition: syncedmem.h:117
Manages memory allocation and synchronization between the host (CPU) and device (GPU).
Definition: syncedmem.h:99
size_t size()
Definition: syncedmem.h:120
void PerceptionFreeHost(void *ptr, bool use_cuda)
Definition: syncedmem.h:83