Welcome to mirror list, hosted at ThFree Co, Russian Federation.

cycles_cubin_cc.cpp « app « cycles « intern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e5d256dd113a3ed0bb5367ad20bf2bf37bb25f90 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/*
 * Copyright 2017 Blender Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stdint.h>
#include <stdio.h>

#include <string>
#include <vector>

#include <OpenImageIO/argparse.h>
#include <OpenImageIO/filesystem.h>

#include "cuew.h"

#ifdef _MSC_VER
#  include <Windows.h>
#endif

using std::string;
using std::vector;

namespace std {
template<typename T> std::string to_string(const T &n)
{
  std::ostringstream s;
  s << n;
  return s.str();
}
}  // namespace std

class CompilationSettings {
 public:
  CompilationSettings()
      : target_arch(0), bits(64), verbose(false), fast_math(false), ptx_only(false)
  {
  }

  string cuda_toolkit_dir;
  string input_file;
  string output_file;
  string ptx_file;
  vector<string> defines;
  vector<string> includes;
  int target_arch;
  int bits;
  bool verbose;
  bool fast_math;
  bool ptx_only;
};

static bool compile_cuda(CompilationSettings &settings)
{
  const char *headers[] = {"stdlib.h", "float.h", "math.h", "stdio.h", "stddef.h"};
  const char *header_content[] = {"\n", "\n", "\n", "\n", "\n"};

  printf("Building %s\n", settings.input_file.c_str());

  string code;
  if (!OIIO::Filesystem::read_text_file(settings.input_file, code)) {
    fprintf(stderr, "Error: unable to read %s\n", settings.input_file.c_str());
    return false;
  }

  vector<string> options;
  for (size_t i = 0; i < settings.includes.size(); i++) {
    options.push_back("-I" + settings.includes[i]);
  }

  for (size_t i = 0; i < settings.defines.size(); i++) {
    options.push_back("-D" + settings.defines[i]);
  }
  options.push_back("-D__KERNEL_CUDA_VERSION__=" + std::to_string(cuewNvrtcVersion()));
  options.push_back("-arch=compute_" + std::to_string(settings.target_arch));
  options.push_back("--device-as-default-execution-space");
  options.push_back("-DCYCLES_CUBIN_CC");
  options.push_back("--std=c++11");
  if (settings.fast_math)
    options.push_back("--use_fast_math");

  nvrtcProgram prog;
  nvrtcResult result = nvrtcCreateProgram(&prog,
                                          code.c_str(),                      // buffer
                                          NULL,                              // name
                                          sizeof(headers) / sizeof(void *),  // numHeaders
                                          header_content,                    // headers
                                          headers);                          // includeNames

  if (result != NVRTC_SUCCESS) {
    fprintf(stderr, "Error: nvrtcCreateProgram failed (%d)\n\n", (int)result);
    return false;
  }

  /* Transfer options to a classic C array. */
  vector<const char *> opts(options.size());
  for (size_t i = 0; i < options.size(); i++) {
    opts[i] = options[i].c_str();
  }

  result = nvrtcCompileProgram(prog, options.size(), &opts[0]);

  if (result != NVRTC_SUCCESS) {
    fprintf(stderr, "Error: nvrtcCompileProgram failed (%d)\n\n", (int)result);

    size_t log_size;
    nvrtcGetProgramLogSize(prog, &log_size);

    vector<char> log(log_size);
    nvrtcGetProgramLog(prog, &log[0]);
    fprintf(stderr, "%s\n", &log[0]);

    return false;
  }

  /* Retrieve the ptx code. */
  size_t ptx_size;
  result = nvrtcGetPTXSize(prog, &ptx_size);
  if (result != NVRTC_SUCCESS) {
    fprintf(stderr, "Error: nvrtcGetPTXSize failed (%d)\n\n", (int)result);
    return false;
  }

  vector<char> ptx_code(ptx_size);
  result = nvrtcGetPTX(prog, &ptx_code[0]);
  if (result != NVRTC_SUCCESS) {
    fprintf(stderr, "Error: nvrtcGetPTX failed (%d)\n\n", (int)result);
    return false;
  }
  if (settings.ptx_only) {
    settings.ptx_file = settings.output_file;
  }
  else {
    /* Write a file in the temp folder with the ptx code. */
    settings.ptx_file = OIIO::Filesystem::temp_directory_path() + "/" +
                        OIIO::Filesystem::unique_path();
  }
  FILE *f = fopen(settings.ptx_file.c_str(), "wb");
  fwrite(&ptx_code[0], 1, ptx_size, f);
  fclose(f);

  return true;
}

static bool link_ptxas(CompilationSettings &settings)
{
  string cudapath = "";
  if (settings.cuda_toolkit_dir.size())
    cudapath = settings.cuda_toolkit_dir + "/bin/";

  string ptx = "\"" + cudapath + "ptxas\" " + settings.ptx_file + " -o " + settings.output_file +
               " --gpu-name sm_" + std::to_string(settings.target_arch) + " -m" +
               std::to_string(settings.bits);

  if (settings.verbose) {
    ptx += " --verbose";
    printf("%s\n", ptx.c_str());
  }

  int pxresult = system(ptx.c_str());
  if (pxresult) {
    fprintf(stderr, "Error: ptxas failed (%d)\n\n", pxresult);
    return false;
  }

  if (!OIIO::Filesystem::remove(settings.ptx_file)) {
    fprintf(stderr, "Error: removing %s\n\n", settings.ptx_file.c_str());
  }

  return true;
}

static bool init(CompilationSettings &settings)
{
#ifdef _MSC_VER
  if (settings.cuda_toolkit_dir.size()) {
    SetDllDirectory((settings.cuda_toolkit_dir + "/bin").c_str());
  }
#else
  (void)settings;
#endif

  int cuewresult = cuewInit(CUEW_INIT_NVRTC);
  if (cuewresult != CUEW_SUCCESS) {
    fprintf(stderr, "Error: cuew init fialed (0x%d)\n\n", cuewresult);
    return false;
  }

  if (cuewNvrtcVersion() < 80) {
    fprintf(stderr, "Error: only cuda 8 and higher is supported, %d\n\n", cuewCompilerVersion());
    return false;
  }

  if (!nvrtcCreateProgram) {
    fprintf(stderr, "Error: nvrtcCreateProgram not resolved\n");
    return false;
  }

  if (!nvrtcCompileProgram) {
    fprintf(stderr, "Error: nvrtcCompileProgram not resolved\n");
    return false;
  }

  if (!nvrtcGetProgramLogSize) {
    fprintf(stderr, "Error: nvrtcGetProgramLogSize not resolved\n");
    return false;
  }

  if (!nvrtcGetProgramLog) {
    fprintf(stderr, "Error: nvrtcGetProgramLog not resolved\n");
    return false;
  }

  if (!nvrtcGetPTXSize) {
    fprintf(stderr, "Error: nvrtcGetPTXSize not resolved\n");
    return false;
  }

  if (!nvrtcGetPTX) {
    fprintf(stderr, "Error: nvrtcGetPTX not resolved\n");
    return false;
  }

  return true;
}

static bool parse_parameters(int argc, const char **argv, CompilationSettings &settings)
{
  OIIO::ArgParse ap;
  ap.options("Usage: cycles_cubin_cc [options]",
             "-target %d",
             &settings.target_arch,
             "target shader model",
             "-m %d",
             &settings.bits,
             "Cuda architecture bits",
             "-i %s",
             &settings.input_file,
             "Input source filename",
             "-o %s",
             &settings.output_file,
             "Output cubin filename",
             "-I %L",
             &settings.includes,
             "Add additional includepath",
             "-D %L",
             &settings.defines,
             "Add additional defines",
             "-ptx",
             &settings.ptx_only,
             "emit PTX code",
             "-v",
             &settings.verbose,
             "Use verbose logging",
             "--use_fast_math",
             &settings.fast_math,
             "Use fast math",
             "-cuda-toolkit-dir %s",
             &settings.cuda_toolkit_dir,
             "path to the cuda toolkit binary directory",
             NULL);

  if (ap.parse(argc, argv) < 0) {
    fprintf(stderr, "%s\n", ap.geterror().c_str());
    ap.usage();
    return false;
  }

  if (!settings.output_file.size()) {
    fprintf(stderr, "Error: Output file not set(-o), required\n\n");
    return false;
  }

  if (!settings.input_file.size()) {
    fprintf(stderr, "Error: Input file not set(-i, required\n\n");
    return false;
  }

  if (!settings.target_arch) {
    fprintf(stderr, "Error: target shader model not set (-target), required\n\n");
    return false;
  }

  return true;
}

int main(int argc, const char **argv)
{
  CompilationSettings settings;

  if (!parse_parameters(argc, argv, settings)) {
    fprintf(stderr, "Error: invalid parameters, exiting\n");
    exit(EXIT_FAILURE);
  }

  if (!init(settings)) {
    fprintf(stderr, "Error: initialization error, exiting\n");
    exit(EXIT_FAILURE);
  }

  if (!compile_cuda(settings)) {
    fprintf(stderr, "Error: compilation error, exiting\n");
    exit(EXIT_FAILURE);
  }

  if (!settings.ptx_only) {
    if (!link_ptxas(settings)) {
      exit(EXIT_FAILURE);
    }
  }

  return 0;
}