SpatialOps
ExternalAllocators.h
1 /*
2  * Copyright (c) 2014 The University of Utah
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to
6  * deal in the Software without restriction, including without limitation the
7  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8  * sell copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20  * IN THE SOFTWARE.
21  */
22 
23 #ifndef EXTERNALALLOCATORS_H_
24 #define EXTERNALALLOCATORS_H_
25 #include <spatialops/SpatialOpsConfigure.h>
26 #ifdef ENABLE_CUDA
27 #include <cuda_runtime.h>
28 #endif
29 
30 namespace ema {
31 #ifdef ENABLE_CUDA
32 namespace cuda { //ema::cuda
33 
34  class CUDADeviceInterface;// Intermediate structure to talk to CUDA capable devices
35  class CUDASharedPointer;
36 
38  struct CUDAMemStats {
39  size_t f;
40  size_t t;
41  size_t inuse;
42  };
43 
51  class CUDADeviceInterface {
52  friend class CUDASharedPointer;
53 
54  CUDADeviceInterface();
55  public:
56  ~CUDADeviceInterface();
57 
59  static CUDADeviceInterface& self();
60 
64  void* get_raw_pointer(const unsigned long int N, const unsigned int K = 0);
65 
68  void* get_pinned_pointer(const unsigned long int N, const unsigned int k = 0);
69 
73  CUDASharedPointer get_shared_pointer(const unsigned long int N, const unsigned int K = 0);
74 
76  void release(CUDASharedPointer& x);
77 
79  void release(void* x, const unsigned int K = 0);
80 
82  void releaseHost(void* x, const unsigned int K = 0);
83 
85  void memcpy_to(void* dest, const void* src, const size_t sz, const unsigned int deviceID, cudaStream_t stream=0);
86  void memcpy_to(CUDASharedPointer& dest, const void* src, const size_t sz, cudaStream_t stream=0);
87 
89  void memcpy_from(void* dest, const void* src, const size_t sz, const unsigned int deviceID, cudaStream_t stream=0);
90  void memcpy_from(void* dest, const CUDASharedPointer& src, const size_t sz, cudaStream_t stream=0);
91 
93  void memcpy_peer(void* dest, const int dID, const void* src, const int sID, const size_t sz);
94 
96  void memset(void* dest, const int val, const size_t num, const unsigned int deviceID);
97  void memset(CUDASharedPointer& dest, const int val, const size_t num);
98 
100  int get_device_count() const;
101 
103  int get_best_device() const;
104 
106  void sync_stream( cudaStream_t stream=0 );
107 
109  void get_memory_statistics(CUDAMemStats& cms, const int K = 0) const;
110 
115  void update_memory_statistics();
116 
118  void print_device_info() const;
119 
120  private:
121 
122  };
123 
127  class CUDASharedPointer {
128  friend class CUDADeviceInterface;
129 
130  CUDASharedPointer(void*, int);
131 
132  public:
133  ~CUDASharedPointer();
134  CUDASharedPointer();
135  CUDASharedPointer(const CUDASharedPointer& x);
136 
138  bool operator==(const CUDASharedPointer& x) const;
139 
141  CUDASharedPointer& operator=( const CUDASharedPointer& x );
142 
148  CUDASharedPointer& operator=( void* x );
149 
153  CUDASharedPointer& detach();
154 
156  void* operator->();
157  const void* operator->() const;
158 
160  bool isnull() const;
161 
163  int get_deviceID() const;
164 
166  int get_refcount() const;
167 
168  private:
169  int* deviceID_;
170  int* refCount_;
171 
172  void* ptr_;
173  };
174 
175 } // End Namespace 'ema::cuda'
176 #endif // ENABLE_CUDA
177 }// End Namespace 'ema'
178 
179 #endif /* EXTERNALMEMORYALLOCATORS_H_ */