NTrace
GPU ray tracing framework
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
CudaCompiler.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/CudaModule.hpp"
30 
31 namespace FW
32 {
33 //------------------------------------------------------------------------
34 
35 class Window;
36 
37 //------------------------------------------------------------------------
38 // Using inline CUDA:
39 //
40 // CudaCompiler::setFrameworkPath("../../tkarras/framework"); // required once per application
41 //
42 // CudaModule* module = FW_COMPILE_INLINE_CUDA
43 // (
44 // //#include "myHeader.hpp"\n
45 // __global__ void myKernel(Vec2i myParam)
46 // {
47 // printf("myParam.x = %d, myParam.y = %d, globalThreadIdx = %d\n",
48 // myParam.x, myParam.y, globalThreadIdx);
49 // }
50 // );
51 // module->getKernel("myKernel").setParams(Vec2i(123, 456)).launch(128);
52 //
53 // Longer but more generic variant:
54 //
55 // CudaCompiler compiler;
56 // compiler.setInlineSource(FW_INLINE_CUDA(...));
57 // CudaModule* module = compiler.compile();
58 //------------------------------------------------------------------------
59 
60 #define FW_INLINE_CUDA(X) FW::formatInlineCuda(__FILE__, __LINE__, #X)
61 #define FW_COMPILE_INLINE_CUDA(X) FW::compileInlineCuda(__FILE__, __LINE__, #X)
62 
63 String formatInlineCuda (const char* file, int line, const char* code);
64 CudaModule* compileInlineCuda (const char* file, int line, const char* code);
65 
66 //------------------------------------------------------------------------
67 
69 {
70 public:
71  CudaCompiler (void);
72  ~CudaCompiler (void);
73 
74  void setCachePath (const String& path) { m_cachePath = path; }
75  void setSourceFile (const String& path) { m_sourceFile = path; m_inlineSource = ""; m_sourceHashValid = false; m_memHashValid = false; }
76  void setInlineSource (const String& source, const String& origin = "") { m_inlineSource = source; m_inlineOrigin = origin; m_sourceFile = ""; m_sourceHashValid = false; m_memHashValid = false; }
77  void overrideSMArch (int arch) { m_overriddenSMArch = arch; }
78 
79  void clearOptions (void) { m_options = ""; m_optionHashValid = false; m_memHashValid = false; }
80  void addOptions (const String& options) { m_options += options + " "; m_optionHashValid = false; m_memHashValid = false; }
81  void include (const String& path) { addOptions(sprintf("-I\"%s\"", path.getPtr())); }
82 
83  void clearDefines (void) { m_defines.clear(); m_defineHashValid = false; m_memHashValid = false; }
84  void undef (const String& key) { if (m_defines.contains(key)) { m_defines.remove(key); m_defineHashValid = false; m_memHashValid = false; } }
85  void define (const String& key, const String& value = "") { undef(key); m_defines.add(key, value); m_defineHashValid = false; m_memHashValid = false; }
86  void define (const String& key, int value) { define(key, sprintf("%d", value)); }
87 
88  void clearPreamble (void) { m_preamble = ""; m_preambleHashValid = false; m_memHashValid = false; }
89  void addPreamble (const String& preamble) { m_preamble += preamble + "\n"; m_preambleHashValid = false; m_memHashValid = false; }
90 
91  void setMessageWindow(Window* window) { m_window = window; }
92  CudaModule* loadDbgCubin (bool enablePrints = true); // loads cubin file compiled by cuda rules
93  CudaModule* compile (bool enablePrints = true, bool autoFail = true);
94  const Array<U8>* compileCubin (bool enablePrints = true, bool autoFail = true); // returns data in cubin file, padded with a zero
95  String compileCubinFile(bool enablePrints = true, bool autoFail = true); // returns file name, empty string on error
96 
97  static void setFrameworkPath(const String& path) { s_frameworkPath = path; }
98  static const String& getFrameworkPath(void) { return s_frameworkPath; }
99 
100  static void setStaticCudaBinPath(const String& path) { FW_ASSERT(!s_inited); s_staticCudaBinPath = path; }
101  static void setStaticOptions(const String& options) { FW_ASSERT(!s_inited); s_staticOptions = options; }
102  static void setStaticPreamble(const String& preamble) { FW_ASSERT(!s_inited); s_staticPreamble = preamble; } // e.g. "#include \"myheader.h\""
103  static void setStaticBinaryFormat(const String& format) { FW_ASSERT(!s_inited); s_staticBinaryFormat = format; } // e.g. "-ptx"
104 
105  static void staticInit (void);
106  static void staticDeinit (void);
107  static void flushMemCache (void);
108 
109 private:
110  CudaCompiler (const CudaCompiler&); // forbidden
111  CudaCompiler& operator= (const CudaCompiler&); // forbidden
112 
113 private:
114  static String queryEnv (const String& name);
115  static void splitPathList (Array<String>& res, const String& value);
116  static bool fileExists (const String& name);
117  static String removeOption (const String& opts, const String& tag, bool hasParam);
118 
119  U64 getMemHash (void);
120  void createCacheDir (void);
121  void writeDefineFile (void);
122  void initLogFile (const String& name, const String& firstLine);
123 
124  void runPreprocessor (String& cubinFile, String& finalOpts);
125  void runCompiler (const String& cubinFile, const String& finalOpts);
126 
127  String fixOptions (String opts);
128  String saveSource (void);
129  void setLoggedError (const String& description, const String& logFile);
130 
131 private:
132  static String s_frameworkPath;
133  static String s_staticCudaBinPath;
134  static String s_staticOptions;
135  static String s_staticPreamble;
136  static String s_staticBinaryFormat;
137 
138  static bool s_inited;
139  static Hash<U64, Array<U8>*> s_cubinCache;
140  static Hash<U64, CudaModule*> s_moduleCache;
141  static U32 s_nvccVersionHash;
142  static String s_nvccCommand;
143 
144  String m_cachePath;
145  String m_sourceFile;
146  String m_inlineSource;
147  String m_inlineOrigin;
148  S32 m_overriddenSMArch;
149 
150  String m_options;
151  Hash<String, String> m_defines;
152  String m_preamble;
153 
154  U32 m_sourceHash;
155  U32 m_optionHash;
156  U64 m_defineHash;
157  U32 m_preambleHash;
158  U64 m_memHash;
159  bool m_sourceHashValid;
160  bool m_optionHashValid;
161  bool m_defineHashValid;
162  bool m_preambleHashValid;
163  bool m_memHashValid;
164 
165  Window* m_window;
166 };
167 
168 //------------------------------------------------------------------------
169 }
const Array< U8 > * compileCubin(bool enablePrints=true, bool autoFail=true)
const char * getPtr(void) const
Definition: String.hpp:51
static const String & getFrameworkPath(void)
void include(const String &path)
void define(const String &key, int value)
CudaModule * compile(bool enablePrints=true, bool autoFail=true)
void setMessageWindow(Window *window)
CudaModule * loadDbgCubin(bool enablePrints=true)
String formatInlineCuda(const char *file, int line, const char *code)
const char * name
Definition: DLLImports.cpp:42
static void setStaticOptions(const String &options)
unsigned __int64 U64
Definition: Defs.hpp:97
void overrideSMArch(int arch)
static void staticDeinit(void)
void clearOptions(void)
String compileCubinFile(bool enablePrints=true, bool autoFail=true)
void define(const String &key, const String &value="")
static void setFrameworkPath(const String &path)
static void setStaticCudaBinPath(const String &path)
void addPreamble(const String &preamble)
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 GLenum const GLvoid pixels GLint GLsizei const GLfloat value GLint GLfloat GLfloat v1 GLint GLfloat GLfloat GLfloat v2 GLint GLsizei const GLfloat value GLint GLsizei GLboolean const GLfloat value GLuint program GLuint GLfloat GLfloat GLfloat z GLuint GLint GLenum GLboolean GLsizei const GLvoid pointer GLuint GLuint const GLchar name GLenum GLsizei GLenum GLsizei GLsizei height GLenum GLuint renderbuffer GLenum GLenum GLint params GLuint path
Definition: DLLImports.inl:382
static void flushMemCache(void)
#define FW_ASSERT(X)
Definition: Defs.hpp:67
signed int S32
Definition: Defs.hpp:88
static void staticInit(void)
String sprintf(const char *fmt,...)
Definition: Defs.cpp:241
unsigned int U32
Definition: Defs.hpp:85
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
static void setStaticPreamble(const String &preamble)
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
CudaModule * compileInlineCuda(const char *file, int line, const char *code)
void setInlineSource(const String &source, const String &origin="")
void clearPreamble(void)
void addOptions(const String &options)
static void setStaticBinaryFormat(const String &format)
void clearDefines(void)
void setSourceFile(const String &path)
void setCachePath(const String &path)
void undef(const String &key)