Extended CUDA Library (ecuda)  2.0
 All Classes Namespaces Files Functions Variables Typedefs Friends Macros
common.hpp
Go to the documentation of this file.
1 /*
2 Copyright (c) 2015, Scott Zuyderduyn
3 All rights reserved.
4 
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7 
8 1. Redistributions of source code must retain the above copyright notice, this
9  list of conditions and the following disclaimer.
10 2. Redistributions in binary form must reproduce the above copyright notice,
11  this list of conditions and the following disclaimer in the documentation
12  and/or other materials provided with the distribution.
13 
14 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
15 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
18 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
23 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 
25 The views and conclusions contained in the software and documentation are those
26 of the authors and should not be interpreted as representing official policies,
27 either expressed or implied, of the FreeBSD Project.
28 */
29 
30 //----------------------------------------------------------------------------
31 // ptr/common.hpp
32 //
33 // Classes and helper functions that are needed by smart and specialized
34 // pointers.
35 //
36 // Author: Scott D. Zuyderduyn, Ph.D. (scott.zuyderduyn@utoronto.ca)
37 //----------------------------------------------------------------------------
38 #ifndef ECUDA_PTR_COMMON_HPP
39 #define ECUDA_PTR_COMMON_HPP
40 
41 #include "../global.hpp"
42 
43 namespace ecuda {
44 
46 namespace detail {
47 
48 // this is hacky structure that takes any pointer (const or not)
49 // and casts it to void* so it can be used by the deleter dispose() method
50 // and other places that call cudaFree
51 template<typename T> struct void_cast;
52 template<typename T> struct void_cast<const T*> { __HOST__ __DEVICE__ inline void* operator()( const T* ptr ) { return reinterpret_cast<void*>( const_cast<T*>(ptr) ); } };
53 template<typename T> struct void_cast { __HOST__ __DEVICE__ inline void* operator()( T ptr ) { return reinterpret_cast<void*>(ptr); } };
54 
55 } // namespace detail
57 
63 template<typename T>
65 
70 
78 
83  __HOST__ __DEVICE__ inline void operator()( T* ptr ) const {
84  #ifdef __CUDA_ARCH__
85  //ptr = NULL;
86  #else
87  #ifndef __CUDACC__
88  delete [] reinterpret_cast<char*>(ptr); // hacky as hell but this should be valid for most test cases
89  #else
90  if( ptr ) cudaFree( detail::void_cast<T*>()(ptr) );
91  //if( ptr ) cudaFree(ptr);
92  #endif // __CUDACC__
93  #endif
94  }
95 
96 };
97 
103 template<typename T>
107  __HOST__ __DEVICE__ inline void operator()( T* ptr ) const {
108  #ifdef __CUDA_ARCH__
109  #else
110  #ifndef __CUDACC__
111  delete [] reinterpret_cast<char*>(ptr);
112  #else
113  if( ptr ) cudaFreeHost( detail::void_cast<T*>()(ptr) );
114  #endif
115  #endif
116  }
117 };
118 
119 } // namespace ecuda
120 
121 
122 #endif
__HOST__ __DEVICE__ default_device_delete() __NOEXCEPT__
Constructs an ecuda::default_device_delete object.
Definition: common.hpp:69
The default destruction policy used by smart pointers to device memory.
Definition: common.hpp:64
#define __CONSTEXPR__
Definition: global.hpp:141
The default destruction policy used by smart pointers to page-locked host memory. ...
Definition: common.hpp:104
#define __NOEXCEPT__
Definition: global.hpp:140
#define __HOST__
Definition: global.hpp:150
__HOST__ __DEVICE__ void operator()(T *ptr) const
Definition: common.hpp:107
__HOST__ __DEVICE__ void operator()(T *ptr) const
Calls cudaFree() on a pointer.
Definition: common.hpp:83
__HOST__ __DEVICE__ __CONSTEXPR__ default_host_delete() __NOEXCEPT__
Definition: common.hpp:105
__HOST__ __DEVICE__ default_device_delete(const default_device_delete< U > &src) __NOEXCEPT__
Constructs an ecuda::default_device_delete object from another one.
Definition: common.hpp:77
__HOST__ __DEVICE__ default_host_delete(const default_host_delete< U > &src) __NOEXCEPT__
Definition: common.hpp:106
#define __DEVICE__
Definition: global.hpp:151