23 #include <OpenImageIO/argparse.h>
24 #include <OpenImageIO/filesystem.h>
66 const char *headers[] = {
"stdlib.h",
"float.h",
"math.h",
"stdio.h",
"stddef.h"};
67 const char *header_content[] = {
"\n",
"\n",
"\n",
"\n",
"\n"};
69 printf(
"Building %s\n", settings.
input_file.c_str());
72 if (!OIIO::Filesystem::read_text_file(settings.
input_file, code)) {
73 fprintf(stderr,
"Error: unable to read %s\n", settings.
input_file.c_str());
78 for (
size_t i = 0; i < settings.
includes.size(); i++) {
82 for (
size_t i = 0; i < settings.
defines.size(); i++) {
87 options.push_back(
"--device-as-default-execution-space");
88 options.push_back(
"-DCYCLES_CUBIN_CC");
89 options.push_back(
"--std=c++11");
91 options.push_back(
"--use_fast_math");
94 nvrtcResult
result = nvrtcCreateProgram(&prog,
97 sizeof(headers) /
sizeof(
void *),
101 if (
result != NVRTC_SUCCESS) {
102 fprintf(stderr,
"Error: nvrtcCreateProgram failed (%d)\n\n", (
int)
result);
108 for (
size_t i = 0; i <
options.size(); i++) {
112 result = nvrtcCompileProgram(prog,
options.size(), &opts[0]);
114 if (
result != NVRTC_SUCCESS) {
115 fprintf(stderr,
"Error: nvrtcCompileProgram failed (%d)\n\n", (
int)
result);
118 nvrtcGetProgramLogSize(prog, &log_size);
121 nvrtcGetProgramLog(prog, &
log[0]);
122 fprintf(stderr,
"%s\n", &
log[0]);
129 result = nvrtcGetPTXSize(prog, &ptx_size);
130 if (
result != NVRTC_SUCCESS) {
131 fprintf(stderr,
"Error: nvrtcGetPTXSize failed (%d)\n\n", (
int)
result);
136 result = nvrtcGetPTX(prog, &ptx_code[0]);
137 if (
result != NVRTC_SUCCESS) {
138 fprintf(stderr,
"Error: nvrtcGetPTX failed (%d)\n\n", (
int)
result);
146 settings.
ptx_file = OIIO::Filesystem::temp_directory_path() +
"/" +
147 OIIO::Filesystem::unique_path();
149 FILE *f = fopen(settings.
ptx_file.c_str(),
"wb");
150 fwrite(&ptx_code[0], 1, ptx_size, f);
158 string cudapath =
"";
162 string ptx =
"\"" + cudapath +
"ptxas\" " + settings.
ptx_file +
" -o " + settings.
output_file +
168 printf(
"%s\n", ptx.c_str());
171 int pxresult = system(ptx.c_str());
173 fprintf(stderr,
"Error: ptxas failed (%d)\n\n", pxresult);
177 if (!OIIO::Filesystem::remove(settings.
ptx_file)) {
178 fprintf(stderr,
"Error: removing %s\n\n", settings.
ptx_file.c_str());
194 int cuewresult = cuewInit(CUEW_INIT_NVRTC);
195 if (cuewresult != CUEW_SUCCESS) {
196 fprintf(stderr,
"Error: cuew init fialed (0x%d)\n\n", cuewresult);
200 if (cuewNvrtcVersion() < 80) {
201 fprintf(stderr,
"Error: only cuda 8 and higher is supported, %d\n\n", cuewCompilerVersion());
205 if (!nvrtcCreateProgram) {
206 fprintf(stderr,
"Error: nvrtcCreateProgram not resolved\n");
210 if (!nvrtcCompileProgram) {
211 fprintf(stderr,
"Error: nvrtcCompileProgram not resolved\n");
215 if (!nvrtcGetProgramLogSize) {
216 fprintf(stderr,
"Error: nvrtcGetProgramLogSize not resolved\n");
220 if (!nvrtcGetProgramLog) {
221 fprintf(stderr,
"Error: nvrtcGetProgramLog not resolved\n");
225 if (!nvrtcGetPTXSize) {
226 fprintf(stderr,
"Error: nvrtcGetPTXSize not resolved\n");
231 fprintf(stderr,
"Error: nvrtcGetPTX not resolved\n");
241 ap.options(
"Usage: cycles_cubin_cc [options]",
244 "target shader model",
247 "Cuda architecture bits",
250 "Input source filename",
253 "Output cubin filename",
256 "Add additional includepath",
259 "Add additional defines",
265 "Use verbose logging",
269 "-cuda-toolkit-dir %s",
271 "path to the cuda toolkit binary directory",
274 if (ap.parse(argc, argv) < 0) {
275 fprintf(stderr,
"%s\n", ap.geterror().c_str());
281 fprintf(stderr,
"Error: Output file not set(-o), required\n\n");
286 fprintf(stderr,
"Error: Input file not set(-i, required\n\n");
291 fprintf(stderr,
"Error: target shader model not set (-target), required\n\n");
298 int main(
int argc,
const char **argv)
303 fprintf(stderr,
"Error: invalid parameters, exiting\n");
307 if (!
init(settings)) {
308 fprintf(stderr,
"Error: initialization error, exiting\n");
313 fprintf(stderr,
"Error: compilation error, exiting\n");
vector< string > includes
int main(int argc, const char **argv)
static bool init(CompilationSettings &settings)
static bool link_ptxas(CompilationSettings &settings)
static bool parse_parameters(int argc, const char **argv, CompilationSettings &settings)
static bool compile_cuda(CompilationSettings &settings)
CCL_NAMESPACE_BEGIN struct Options options
INLINE Rall1d< T, V, S > log(const Rall1d< T, V, S > &arg)
std::vector< ElementType, Eigen::aligned_allocator< ElementType > > vector
std::string to_string(const T &n)