1 // Copyright (c) 2018, ETH Zurich and UNC Chapel Hill.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met:
6 //
7 //     * Redistributions of source code must retain the above copyright
8 //       notice, this list of conditions and the following disclaimer.
9 //
10 //     * Redistributions in binary form must reproduce the above copyright
11 //       notice, this list of conditions and the following disclaimer in the
12 //       documentation and/or other materials provided with the distribution.
13 //
14 //     * Neither the name of ETH Zurich and UNC Chapel Hill nor the names of
15 //       its contributors may be used to endorse or promote products derived
16 //       from this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
22 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 // POSSIBILITY OF SUCH DAMAGE.
29 //
30 // Author: Johannes L. Schoenberger (jsch-at-demuc-dot-de)
31 
32 #ifndef COLMAP_SRC_MVS_CUDA_ARRAY_WRAPPER_H_
33 #define COLMAP_SRC_MVS_CUDA_ARRAY_WRAPPER_H_
34 
35 #include <memory>
36 
37 #include <cuda_runtime.h>
38 
39 #include "mvs/gpu_mat.h"
40 #include "util/cudacc.h"
41 
42 namespace colmap {
43 namespace mvs {
44 
45 template <typename T>
46 class CudaArrayWrapper {
47  public:
48   CudaArrayWrapper(const size_t width, const size_t height, const size_t depth);
49   ~CudaArrayWrapper();
50 
51   const cudaArray* GetPtr() const;
52   cudaArray* GetPtr();
53 
54   size_t GetWidth() const;
55   size_t GetHeight() const;
56   size_t GetDepth() const;
57 
58   void CopyToDevice(const T* data);
59   void CopyToHost(const T* data);
60   void CopyFromGpuMat(const GpuMat<T>& array);
61 
62  private:
63   // Define class as non-copyable and non-movable.
64   CudaArrayWrapper(CudaArrayWrapper const&) = delete;
65   void operator=(CudaArrayWrapper const& obj) = delete;
66   CudaArrayWrapper(CudaArrayWrapper&&) = delete;
67 
68   void Allocate();
69   void Deallocate();
70 
71   cudaArray* array_;
72 
73   size_t width_;
74   size_t height_;
75   size_t depth_;
76 };
77 
78 ////////////////////////////////////////////////////////////////////////////////
79 // Implementation
80 ////////////////////////////////////////////////////////////////////////////////
81 
82 template <typename T>
CudaArrayWrapper(const size_t width,const size_t height,const size_t depth)83 CudaArrayWrapper<T>::CudaArrayWrapper(const size_t width, const size_t height,
84                                       const size_t depth)
85     : width_(width), height_(height), depth_(depth), array_(nullptr) {}
86 
87 template <typename T>
~CudaArrayWrapper()88 CudaArrayWrapper<T>::~CudaArrayWrapper() {
89   Deallocate();
90 }
91 
92 template <typename T>
GetPtr()93 const cudaArray* CudaArrayWrapper<T>::GetPtr() const {
94   return array_;
95 }
96 
97 template <typename T>
GetPtr()98 cudaArray* CudaArrayWrapper<T>::GetPtr() {
99   return array_;
100 }
101 
102 template <typename T>
GetWidth()103 size_t CudaArrayWrapper<T>::GetWidth() const {
104   return width_;
105 }
106 
107 template <typename T>
GetHeight()108 size_t CudaArrayWrapper<T>::GetHeight() const {
109   return height_;
110 }
111 
112 template <typename T>
GetDepth()113 size_t CudaArrayWrapper<T>::GetDepth() const {
114   return depth_;
115 }
116 
117 template <typename T>
CopyToDevice(const T * data)118 void CudaArrayWrapper<T>::CopyToDevice(const T* data) {
119   cudaMemcpy3DParms params = {0};
120   Allocate();
121   params.extent = make_cudaExtent(width_, height_, depth_);
122   params.kind = cudaMemcpyHostToDevice;
123   params.dstArray = array_;
124   params.srcPtr =
125       make_cudaPitchedPtr((void*)data, width_ * sizeof(T), width_, height_);
126   CUDA_SAFE_CALL(cudaMemcpy3D(&params));
127 }
128 
129 template <typename T>
CopyToHost(const T * data)130 void CudaArrayWrapper<T>::CopyToHost(const T* data) {
131   cudaMemcpy3DParms params = {0};
132   params.extent = make_cudaExtent(width_, height_, depth_);
133   params.kind = cudaMemcpyDeviceToHost;
134   params.dstPtr =
135       make_cudaPitchedPtr((void*)data, width_ * sizeof(T), width_, height_);
136   params.srcArray = array_;
137   CUDA_SAFE_CALL(cudaMemcpy3D(&params));
138 }
139 
140 template <typename T>
CopyFromGpuMat(const GpuMat<T> & array)141 void CudaArrayWrapper<T>::CopyFromGpuMat(const GpuMat<T>& array) {
142   Allocate();
143   cudaMemcpy3DParms parameters = {0};
144   parameters.extent = make_cudaExtent(width_, height_, depth_);
145   parameters.kind = cudaMemcpyDeviceToDevice;
146   parameters.dstArray = array_;
147   parameters.srcPtr = make_cudaPitchedPtr((void*)array.GetPtr(),
148                                           array.GetPitch(), width_, height_);
149   CUDA_SAFE_CALL(cudaMemcpy3D(&parameters));
150 }
151 
152 template <typename T>
Allocate()153 void CudaArrayWrapper<T>::Allocate() {
154   Deallocate();
155   struct cudaExtent extent = make_cudaExtent(width_, height_, depth_);
156   cudaChannelFormatDesc fmt = cudaCreateChannelDesc<T>();
157   CUDA_SAFE_CALL(cudaMalloc3DArray(&array_, &fmt, extent, cudaArrayLayered));
158 }
159 
160 template <typename T>
Deallocate()161 void CudaArrayWrapper<T>::Deallocate() {
162   if (array_ != nullptr) {
163     CUDA_SAFE_CALL(cudaFreeArray(array_));
164     array_ = nullptr;
165   }
166 }
167 
168 }  // namespace mvs
169 }  // namespace colmap
170 
171 #endif  // COLMAP_SRC_MVS_CUDA_ARRAY_WRAPPER_H_
172