LSSTApplications  10.0+286,10.0+36,10.0+46,10.0-2-g4f67435,10.1+152,10.1+37,11.0,11.0+1,11.0-1-g47edd16,11.0-1-g60db491,11.0-1-g7418c06,11.0-2-g04d2804,11.0-2-g68503cd,11.0-2-g818369d,11.0-2-gb8b8ce7
LSSTDataManagementBasePackage
CudaMemory.h
Go to the documentation of this file.
1 // -*- LSST-C++ -*-
2 
3 /*
4  * LSST Data Management System
5  * Copyright 2008 - 2012 LSST Corporation.
6  *
7  * This product includes software developed by the
8  * LSST Project (http://www.lsst.org/).
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the LSST License Statement and
21  * the GNU General Public License along with this program. If not,
22  * see <http://www.lsstcorp.org/LegalNotices/>.
23  */
24 
42 #ifdef GPU_BUILD
43 
44 namespace lsst {
45 namespace afw {
46 namespace gpu {
47 namespace detail {
48 
63 template<typename T>
64 T* AllocOnGpu(int size)
65 {
66  T* dataGpu;
67  cudaError_t cudaError = cudaMalloc((void**)&dataGpu, size * sizeof(T));
68  if (cudaError != cudaSuccess) {
69  return NULL;
70  }
71  return dataGpu;
72 }
73 
88 template<typename T>
89 void CopyFromGpu(T* destCpu, T* sourceGpu, int size)
90 {
91  cudaError_t cudaError = cudaMemcpy(
92  /* Desination:*/ destCpu,
93  /* Source: */ sourceGpu,
94  /* Size in bytes: */ size * sizeof(T),
95  /* Direction */ cudaMemcpyDeviceToHost
96  );
97  if (cudaError != cudaSuccess)
98  throw LSST_EXCEPT(GpuMemoryError, "CopyFromGpu: failed");
99 }
100 
115 template<typename T>
116 void CopyToGpu(T* destGpu, T* sourceCpu, int size)
117 {
118  cudaError_t cudaError;
119  cudaError = cudaMemcpy(
120  /* Desination:*/ destGpu,
121  /* Source: */ sourceCpu,
122  /* Size in bytes: */ size * sizeof(T),
123  /* Direction */ cudaMemcpyHostToDevice
124  );
125  if (cudaError != cudaSuccess) {
126  throw LSST_EXCEPT(GpuMemoryError, "CopyToGpu: failed");
127  }
128 }
129 
149 template<typename T>
150 T* TransferToGpu(const T* sourceCpu, int size)
151 {
152  T* dataGpu;
153  cudaError_t cudaError = cudaMalloc((void**)&dataGpu, size * sizeof(T));
154  if (cudaError != cudaSuccess) {
155  return NULL;
156  }
157  cudaError = cudaMemcpy(
158  /* Desination:*/ dataGpu,
159  /* Source: */ sourceCpu,
160  /* Size in bytes: */ size * sizeof(T),
161  /* Direction */ cudaMemcpyHostToDevice
162  );
163  if (cudaError != cudaSuccess) {
164  throw LSST_EXCEPT(GpuMemoryError, "TransferToGpu: transfer failed");
165  }
166  return dataGpu;
167 }
168 
182 template<typename T>
183 class GpuMemOwner
184 {
185 private:
186  void operator=(const GpuMemOwner& rhs);
187 
188 public:
189  GpuMemOwner(const GpuMemOwner& rhs) {
190  assert(rhs.getPtr() == NULL);
191  ptr=NULL;
192  }
193 
194  T* ptr;
195  int size;
196 
198  GpuMemOwner() : ptr(NULL) {}
199 
201  T* getPtr() const
202  {
203  return ptr;
204  }
205 
207  int getSize() const
208  {
209  return size;
210  }
211 
235  T* Transfer(const T* source, int size_p) {
236  assert(ptr == NULL);
237  size = size_p;
238  ptr = TransferToGpu(source, size);
239  return ptr;
240  }
241 
253  T* Transfer(const GpuBuffer2D<T>& source) {
254  assert(ptr == NULL);
255  size = source.Size();
256  ptr = TransferToGpu(source.img, size);
257  return ptr;
258  }
259 
271  T* TransferVec(const std::vector<T>& source) {
272  assert(ptr == NULL);
273  size = int(source.size());
274  ptr = TransferToGpu(&source[0], size);
275  return ptr;
276  }
277 
295  T* Alloc(int size_p) {
296  assert(ptr == NULL);
297  size = size_p;
298  ptr = AllocOnGpu<T>(size);
299  return ptr;
300  }
301 
306  T* CopyToGpu(detail::GpuBuffer2D<T>& source) const {
307  assert(ptr != NULL);
308  assert(source.Size() == size);
309  lsst::afw::gpu::detail::CopyToGpu(ptr, source.img, size);
310  return ptr;
311  }
312 
317  T* CopyFromGpu(detail::GpuBuffer2D<T>& dest) const {
318  assert(ptr != NULL);
319  assert(dest.Size() == size);
320  lsst::afw::gpu::detail::CopyFromGpu(dest.img, ptr, size);
321  return ptr;
322  }
323 
328  T* CopyFromGpu(T* dest) const {
329  assert(ptr != NULL);
330  lsst::afw::gpu::detail::CopyFromGpu(dest, ptr, size);
331  return ptr;
332  }
333 
338  int TransferFromImageBase(const lsst::afw::image::ImageBase<T>& img);
339 
346  int AllocImageBaseBuffer(const lsst::afw::image::ImageBase<T>& img);
347 
355  void CopyToImageBase(lsst::afw::image::ImageBase<T>& img) const;
356 
363  ~GpuMemOwner() {
364  if (ptr != NULL) cudaFree(ptr);
365  }
366 };
367 
368 }}}} // namespace lsst::afw::gpu::detail ends
369 
370 #endif //IS_GPU_BUILD
371 
The base class for all image classed (Image, Mask, MaskedImage, ...)
Definition: Image.h:115
#define LSST_EXCEPT(type,...)
Definition: Exception.h:46