SpatialOps
ExternalAllocators.h
1 /*
2  * Copyright (c) 2014-2017 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 memcpyfromsymbol_to(void* dest, const void* symbol, const size_t sz,const unsigned int deviceID, const size_t offset, cudaStream_t stream);
94 
96  void memcpyfromsymbol_from(void* dest, const void* symbol, const size_t sz,const unsigned int deviceID, const size_t offset, cudaStream_t stream);
97 
99  void memcpy_peer(void* dest, const int dID, const void* src, const int sID, const size_t sz);
100 
102  void memset(void* dest, const int val, const size_t num, const unsigned int deviceID);
103  void memset(CUDASharedPointer& dest, const int val, const size_t num);
104 
106  int get_device_count() const;
107 
109  int get_best_device() const;
110 
112  void sync_stream( cudaStream_t stream=0 );
113 
115  void get_memory_statistics(CUDAMemStats& cms, const int K = 0) const;
116 
121  void update_memory_statistics();
122 
124  void print_device_info() const;
125 
126  private:
127 
128  };
129 
133  class CUDASharedPointer {
134  friend class CUDADeviceInterface;
135 
136  CUDASharedPointer(void*, int);
137 
138  public:
139  ~CUDASharedPointer();
140  CUDASharedPointer();
141  CUDASharedPointer(const CUDASharedPointer& x);
142 
144  bool operator==(const CUDASharedPointer& x) const;
145 
147  CUDASharedPointer& operator=( const CUDASharedPointer& x );
148 
154  CUDASharedPointer& operator=( void* x );
155 
159  CUDASharedPointer& detach();
160 
162  void* operator->();
163  const void* operator->() const;
164 
166  bool isnull() const;
167 
169  int get_deviceID() const;
170 
172  int get_refcount() const;
173 
174  private:
175  int* deviceID_;
176  int* refCount_;
177 
178  void* ptr_;
179  };
180 
181 } // End Namespace 'ema::cuda'
182 #endif // ENABLE_CUDA
183 }// End Namespace 'ema'
184 
185 #endif /* EXTERNALMEMORYALLOCATORS_H_ */