Lift
Library of parallel computing primitives for GPUs and multi-core CPUs
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
ldg_wrapper.h
Go to the documentation of this file.
1 /*
2  * Lift
3  *
4  * Copyright (c) 2014-2015, NVIDIA CORPORATION
5  * Copyright (c) 2015, Nuno Subtil <subtil@gmail.com>
6  * Copyright (c) 2015, Roche Molecular Systems Inc.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions are met:
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in the
15  * documentation and/or other materials provided with the distribution.
16  * * Neither the name of the copyright holders nor the names of its
17  * contributors may be used to endorse or promote products derived from
18  * this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #pragma once
33 
34 #include "pointer.h"
35 
36 namespace lift {
37 
38 template <target_system system, typename T>
40 { };
41 
42 template <typename T>
43 struct __ldg_loader<host, T>
44 {
45  LIFT_HOST_DEVICE static inline T load(const T* pointer)
46  {
47  return *pointer;
48  }
49 };
50 
51 template <typename T>
52 struct __ldg_loader<cuda, T>
53 {
54  LIFT_HOST_DEVICE static inline T load(const T* pointer)
55  {
56  return __ldg(pointer);
57  }
58 };
59 
60 // wrap a read-only memory pointer and access it through __ldg() on the GPU
61 template <target_system system, typename value_type, typename index_type = uint32>
62 struct ldg_pointer : public pointer<system, value_type, index_type>
63 {
65 
67  typedef typename base::size_type size_type;
68 
69  using base::base;
70 
71  // ldg pointers are read-only
76 
78  {
80  }
81 
83  {
85  }
86 
88  {
90  }
91 
93  {
95  }
96 
97  // return a pointer to a memory range within this pointer
99  {
100  ldg_pointer ret;
101  ret.storage = base::storage + offset;
102 
103  if (len == size_type(-1))
104  {
105  len = base::storage_size - offset;
106  }
107 
108  ret.storage_size = len;
109 
110  return ret;
111  }
112 
113  // pointer arithmetic
114  // note that we don't do any bounds checking
116  {
117  ldg_pointer ret;
118  ret.storage = base::storage + offset;
119  ret.storage_size = base::storage_size - offset;
120 
121  return ret;
122  }
123 
125  {
126  ldg_pointer ret;
127  ret.storage = base::storage - offset;
128  ret.storage_size = base::storage_size + offset;
129 
130  return ret;
131  }
132 
133  // return a truncated pointer
135  {
136  ldg_pointer ret;
137  ret.storage = base::storage;
138  ret.storage_size = new_size;
139 
140  return ret;
141  }
142 };
143 
144 template <target_system system, typename T, typename I>
146 {
147  return p;
148 }
149 
150 } // namespace lift
base::size_type size_type
Definition: ldg_wrapper.h:67
LIFT_HOST_DEVICE reference_type at(size_type pos)=delete
LIFT_HOST_DEVICE ldg_pointer operator+(off_t offset) const
Definition: ldg_wrapper.h:115
LIFT_HOST_DEVICE reference_type front()=delete
static LIFT_HOST_DEVICE T load(const T *pointer)
Definition: ldg_wrapper.h:54
LIFT_HOST_DEVICE ldg_pointer truncate(size_t new_size)
Definition: ldg_wrapper.h:134
LIFT_HOST_DEVICE value_type back() const
Definition: ldg_wrapper.h:92
tagged_pointer_base< host, value_type, index_type > base
Definition: pointer.h:281
pointer< system, value_type, index_type > base
Definition: ldg_wrapper.h:64
static LIFT_HOST_DEVICE T load(const T *pointer)
Definition: ldg_wrapper.h:45
LIFT_HOST_DEVICE ldg_pointer operator-(off_t offset) const
Definition: ldg_wrapper.h:124
LIFT_HOST_DEVICE reference_type operator[](size_type pos)=delete
LIFT_HOST_DEVICE reference_type back()=delete
Lift's tagged pointer class.
Definition: pointer.h:276
LIFT_HOST_DEVICE ldg_pointer range(const size_type offset, size_type len=size_type(-1)) const
Definition: ldg_wrapper.h:98
LIFT_HOST_DEVICE value_type front() const
Definition: ldg_wrapper.h:87
base::reference_type reference_type
Definition: ldg_wrapper.h:66
#define LIFT_HOST_DEVICE
Definition: local_memory.h:40
LIFT_HOST_DEVICE value_type at(size_type pos) const
Definition: ldg_wrapper.h:77
LIFT_HOST_DEVICE const ldg_pointer< system, T, I > make_ldg_pointer(const pointer< system, T, I > p)
Definition: ldg_wrapper.h:145