CUV  0.9.201304091348
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
texture.h
1 //*LB*
2 // Copyright (c) 2010, University of Bonn, Institute for Computer Science VI
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 // * Redistributions of source code must retain the above copyright notice,
9 // this list of conditions and the following disclaimer.
10 // * 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 // * Neither the name of the University of Bonn
14 // nor the names of its contributors may be used to endorse or promote
15 // products derived from this software without specific prior written
16 // permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 //*LE*
29 
30 
31 
32 
33 
34 /* Copyright 2008 NVIDIA Corporation. All Rights Reserved */
35 #ifndef __TEXTURE_H__
36 #define __TEXTURE_H__
37 
38 #include <cstdio>
39 #include "cuda.h"
40 #include "cuv_general.hpp"
41 
42 
43 /*
44  * These textures are (optionally) used to cache the 'x' vector in y += A*x
45  */
46 texture<float> tex_x_float;
47 texture<uchar4> tex_x_uchar4;
48 texture<uchar1> tex_x_uchar1;
49 
50 inline size_t bind_x(const uchar1 * x, const unsigned int len)
51 {
52  size_t offset;
53  cuvSafeCall(cudaBindTexture(&offset, tex_x_uchar1, (const void *)x, sizeof(uchar1)*len));
54  cuvAssert(offset % sizeof(uchar1) == 0 );
55  return offset/sizeof(uchar1);
56 }
57 inline size_t bind_x(const uchar4 * x, const unsigned int len)
58 {
59  size_t offset;
60  cuvSafeCall(cudaBindTexture(&offset, tex_x_uchar4, (const void *)x, sizeof(uchar4)*len));
61  cuvAssert(offset % sizeof(uchar4) == 0 );
62  return offset/sizeof(uchar4);
63 }
64 inline size_t bind_x(const float * x, const unsigned int len)
65 {
66  size_t offset;
67  cuvSafeCall(cudaBindTexture(&offset, tex_x_float, (const void *)x, sizeof(float)*len));
68  cuvAssert(offset % sizeof(float) == 0 );
69  return offset/sizeof(float);
70 }
71 
72 // Note: x is unused, but distinguishes the functions
73 inline void unbind_x(const float * x)
74 { cuvSafeCall(cudaUnbindTexture(tex_x_float)); }
75 inline void unbind_x(const uchar4 * x)
76 { cuvSafeCall(cudaUnbindTexture(tex_x_uchar4)); }
77 inline void unbind_x(const uchar1 * x)
78 { cuvSafeCall(cudaUnbindTexture(tex_x_uchar1)); }
79 
80 template <bool UseCache>
81 inline __device__ float fetch_x(const float* x, const int& i)
82 {
83  if (UseCache) return tex1Dfetch(tex_x_float, i);
84  else return x[i];
85 }
86 template <bool UseCache>
87 inline __device__ uchar4 fetch_x(const uchar4* x, const int& i)
88 {
89  if (UseCache) return tex1Dfetch(tex_x_uchar4, i);
90  else return x[i];
91 }
92 template <bool UseCache>
93 inline __device__ uchar1 fetch_x(const uchar1* x, const int& i)
94 {
95  if (UseCache) return tex1Dfetch(tex_x_uchar1, i);
96  else return x[i];
97 }
98 
99 
100 
101 #endif /* __TEXTURE_H__ */