NTrace
GPU ray tracing framework
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
CudaModule.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2009-2011, NVIDIA Corporation
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  * * Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * * Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * * Neither the name of NVIDIA Corporation nor the
13  * names of its contributors may be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #pragma once
29 #include "gpu/GLContext.hpp"
30 #include "gpu/CudaKernel.hpp"
31 
32 namespace FW
33 {
34 //------------------------------------------------------------------------
35 
37 {
38 public:
39  CudaModule (const void* cubin);
40  CudaModule (const String& cubinFile);
41  ~CudaModule (void);
42 
43  CUmodule getHandle (void) { return m_module; }
44 
45  bool hasKernel (const String& name);
47  int setParami (CUfunction kernel, int offset, S32 value);
48  int setParamf (CUfunction kernel, int offset, F32 value);
49  int setParamPtr (CUfunction kernel, int offset, CUdeviceptr value);
50 
51  Buffer& getGlobal (const String& name);
52  void updateGlobals (bool async = false, CUstream stream = NULL); // copy to the device if modified
53 
54  CUtexref getTexRef (const String& name);
55  void setTexRefMode (CUtexref texRef, bool wrap = true, bool bilinear = true, bool normalizedCoords = true, bool readAsInt = false);
56  void setTexRef (const String& name, Buffer& buf, CUarray_format format, int numComponents);
57  void setTexRef (const String& name, CUdeviceptr ptr, S64 size, CUarray_format format, int numComponents);
58  void setTexRef (const String& name, CUarray cudaArray, bool wrap = true, bool bilinear = true, bool normalizedCoords = true, bool readAsInt = false);
59  void setTexRef (const String& name, const Image& image, bool wrap = true, bool bilinear = true, bool normalizedCoords = true, bool readAsInt = false);
60  void unsetTexRef (const String& name);
61  void updateTexRefs (CUfunction kernel);
62 
64  void setSurfRef (const String& name, CUarray cudaArray);
65 
66  static void staticInit (void);
67  static void staticDeinit (void);
68  static bool isAvailable (void) { staticInit(); return s_available; }
69  static S64 getMemoryUsed (void);
70  static void sync (bool yield = true); // False = low latency but keeps the CPU busy. True = long latency but relieves the CPU.
71  static void checkError (const char* funcName, CUresult res);
72  static const char* decodeError (CUresult res);
73 
74  static CUdevice getDeviceHandle (void) { staticInit(); return s_device; }
75  static int getDriverVersion (void); // e.g. 23 = 2.3
76  static int getComputeCapability(void); // e.g. 13 = 1.3
77  static int getDeviceAttribute (CUdevice_attribute attrib);
78  static CUevent getStartEvent (void) { staticInit(); return s_startEvent; }
79  static CUevent getEndEvent (void) { staticInit(); return s_endEvent; }
80 
81 private:
82  static CUdevice selectDevice (void);
83  static void printDeviceInfo (CUdevice device);
84  static Vec2i selectGridSize (int numBlocks); // TODO: remove
85  CUfunction findKernel (const String& name);
86 
87 private:
88  CudaModule (const CudaModule&); // forbidden
89  CudaModule& operator= (const CudaModule&); // forbidden
90 
91 private:
92  static bool s_inited;
93  static bool s_available;
94  static CUdevice s_device;
95  static CUcontext s_context;
96  static CUevent s_startEvent;
97  static CUevent s_endEvent;
98 
99  CUmodule m_module;
100  Hash<String, CUfunction> m_kernels;
101  Array<Buffer*> m_globals;
102  Hash<String, S32> m_globalHash;
103  Array<CUtexref> m_texRefs;
104  Hash<String, S32> m_texRefHash;
105 };
106 
107 //------------------------------------------------------------------------
108 }
static CUevent getEndEvent(void)
Definition: CudaModule.hpp:79
CUdevice int ordinal char int CUdevice dev CUdevprop CUdevice dev CUcontext ctx CUcontext ctx CUcontext pctx CUmodule const void image CUmodule const void fatCubin CUfunction CUmodule const char name void p CUfunction unsigned int bytes CUtexref pTexRef CUtexref CUarray unsigned int Flags CUtexref int CUaddress_mode am CUtexref unsigned int Flags CUaddress_mode CUtexref int dim CUarray_format int CUtexref hTexRef CUfunction unsigned int numbytes CUfunction int offset
Definition: DLLImports.inl:84
#define NULL
Definition: Defs.hpp:39
static S64 getMemoryUsed(void)
Definition: CudaModule.cpp:384
static int getDriverVersion(void)
Definition: CudaModule.cpp:495
static void staticDeinit(void)
Definition: CudaModule.cpp:361
const char * name
Definition: DLLImports.cpp:42
void ** ptr
Definition: DLLImports.cpp:74
void * CUsurfref
Definition: DLLImports.hpp:117
CUmodule getHandle(void)
Definition: CudaModule.hpp:43
CudaModule(const void *cubin)
Definition: CudaModule.cpp:47
CudaKernel getKernel(const String &name)
Definition: CudaModule.cpp:80
int setParami(CUfunction kernel, int offset, S32 value)
Definition: CudaModule.cpp:90
CUtexref getTexRef(const String &name)
Definition: CudaModule.cpp:145
void setTexRef(const String &name, Buffer &buf, CUarray_format format, int numComponents)
Definition: CudaModule.cpp:193
void setSurfRef(const String &name, CUarray cudaArray)
Definition: CudaModule.cpp:298
static void checkError(const char *funcName, CUresult res)
Definition: CudaModule.cpp:487
static int getComputeCapability(void)
Definition: CudaModule.cpp:508
float F32
Definition: Defs.hpp:89
int setParamf(CUfunction kernel, int offset, F32 value)
Definition: CudaModule.cpp:99
signed int S32
Definition: Defs.hpp:88
static bool isAvailable(void)
Definition: CudaModule.hpp:68
signed __int64 S64
Definition: Defs.hpp:98
Buffer & getGlobal(const String &name)
Definition: CudaModule.cpp:117
void setTexRefMode(CUtexref texRef, bool wrap=true, bool bilinear=true, bool normalizedCoords=true, bool readAsInt=false)
Definition: CudaModule.cpp:161
CUdevice int ordinal char int CUdevice dev CUdevprop CUdevice dev CUcontext ctx CUcontext ctx CUcontext pctx CUmodule const void image CUmodule const void fatCubin CUfunction CUmodule const char name void p CUfunction unsigned int bytes CUtexref pTexRef CUtexref CUarray unsigned int Flags CUtexref int CUaddress_mode am CUtexref unsigned int Flags CUaddress_mode CUtexref int dim CUarray_format int CUtexref hTexRef CUfunction unsigned int numbytes CUfunction int float value
Definition: DLLImports.inl:84
void unsetTexRef(const String &name)
Definition: CudaModule.cpp:256
CUdevice int ordinal char int CUdevice dev CUdevprop CUdevice dev CUcontext ctx CUcontext ctx CUcontext pctx CUmodule const void image CUmodule const void fatCubin CUfunction CUmodule const char name void p CUfunction unsigned int bytes CUtexref pTexRef CUtexref CUarray unsigned int Flags CUtexref int CUaddress_mode am CUtexref unsigned int Flags CUaddress_mode CUtexref int dim CUarray_format int CUtexref hTexRef CUfunction unsigned int numbytes CUfunction int float value CUfunction int CUtexref hTexRef CUfunction int int grid_height CUevent unsigned int Flags CUevent hEvent CUevent hEvent CUstream unsigned int Flags CUstream hStream GLuint bufferobj unsigned int CUdevice dev CUdeviceptr unsigned int CUmodule const char name CUdeviceptr unsigned int bytesize CUdeviceptr dptr void unsigned int bytesize void CUdeviceptr unsigned int ByteCount CUarray unsigned int CUdeviceptr unsigned int ByteCount CUarray unsigned int const void unsigned int ByteCount CUarray unsigned int CUarray unsigned int unsigned int ByteCount void CUarray unsigned int unsigned int CUstream hStream const CUDA_MEMCPY2D pCopy CUdeviceptr const void unsigned int CUstream hStream const CUDA_MEMCPY2D CUstream hStream CUdeviceptr unsigned char unsigned int N CUdeviceptr unsigned int unsigned int N CUdeviceptr unsigned int unsigned short unsigned int unsigned int Height CUarray const CUDA_ARRAY_DESCRIPTOR pAllocateArray CUarray const CUDA_ARRAY3D_DESCRIPTOR pAllocateArray unsigned int CUtexref CUdeviceptr unsigned int bytes CUcontext unsigned int CUdevice device GLenum texture GLenum GLuint buffer GLenum GLuint renderbuffer GLenum GLsizeiptr const GLvoid GLenum usage GLuint shader GLenum type GLsizei const GLuint framebuffers GLsizei const GLuint renderbuffers GLuint v GLuint v GLenum GLenum GLenum GLuint GLint level GLsizei GLuint framebuffers GLuint const GLchar name GLenum GLintptr GLsizeiptr GLvoid data GLuint GLenum GLint param GLuint GLenum GLint param GLhandleARB programObj GLenum GLenum GLsizei GLsizei height GLenum GLint GLint GLsizei GLsizei GLsizei GLint GLenum format
Definition: DLLImports.inl:349
void updateGlobals(bool async=false, CUstream stream=NULL)
Definition: CudaModule.cpp:137
static void sync(bool yield=true)
Definition: CudaModule.cpp:398
static CUevent getStartEvent(void)
Definition: CudaModule.hpp:78
bool hasKernel(const String &name)
Definition: CudaModule.cpp:73
static int getDeviceAttribute(CUdevice_attribute attrib)
Definition: CudaModule.cpp:522
CUsurfref getSurfRef(const String &name)
Definition: CudaModule.cpp:283
int setParamPtr(CUfunction kernel, int offset, CUdeviceptr value)
Definition: CudaModule.cpp:108
static const char * decodeError(CUresult res)
Definition: CudaModule.cpp:426
static void staticInit(void)
Definition: CudaModule.cpp:311
CUdevice int ordinal char int CUdevice dev CUdevprop CUdevice dev CUcontext ctx CUcontext ctx CUcontext pctx CUmodule const void * image
Definition: DLLImports.inl:60
CUdevice int ordinal char int CUdevice dev CUdevprop CUdevice dev CUcontext ctx CUcontext ctx CUcontext pctx CUmodule const void image CUmodule const void fatCubin CUfunction CUmodule const char name void p CUfunction unsigned int bytes CUtexref pTexRef CUtexref CUarray unsigned int Flags CUtexref int CUaddress_mode am CUtexref unsigned int Flags CUaddress_mode CUtexref int dim CUarray_format int CUtexref hTexRef CUfunction unsigned int numbytes CUfunction int float value CUfunction int CUtexref hTexRef CUfunction int int grid_height CUevent unsigned int Flags CUevent hEvent CUevent hEvent CUstream unsigned int Flags CUstream hStream GLuint bufferobj unsigned int CUdevice dev CUdeviceptr unsigned int CUmodule const char name CUdeviceptr unsigned int bytesize CUdeviceptr dptr void unsigned int bytesize void CUdeviceptr unsigned int ByteCount CUarray unsigned int CUdeviceptr unsigned int ByteCount CUarray unsigned int const void unsigned int ByteCount CUarray unsigned int CUarray unsigned int unsigned int ByteCount void CUarray unsigned int unsigned int CUstream hStream const CUDA_MEMCPY2D pCopy CUdeviceptr const void unsigned int CUstream hStream const CUDA_MEMCPY2D CUstream hStream CUdeviceptr unsigned char unsigned int N CUdeviceptr unsigned int unsigned int N CUdeviceptr unsigned int unsigned short unsigned int unsigned int Height CUarray const CUDA_ARRAY_DESCRIPTOR pAllocateArray CUarray const CUDA_ARRAY3D_DESCRIPTOR pAllocateArray unsigned int CUtexref CUdeviceptr unsigned int bytes CUcontext unsigned int CUdevice device GLenum texture GLenum GLuint buffer GLenum GLuint renderbuffer GLenum GLsizeiptr size
Definition: DLLImports.inl:319
CUdevice * device
Definition: DLLImports.inl:46
static CUdevice getDeviceHandle(void)
Definition: CudaModule.hpp:74
void updateTexRefs(CUfunction kernel)
Definition: CudaModule.cpp:264