-
Notifications
You must be signed in to change notification settings - Fork 10
/
_ptxcompilerlib.cpp
290 lines (239 loc) · 8.97 KB
/
_ptxcompilerlib.cpp
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
/*
* Copyright (c) 2021, NVIDIA CORPORATION.
*
* 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.
*/
#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include <new>
#include <nvPTXCompiler.h>
#include <string.h>
static const char *nvPTXGetErrorEnum(nvPTXCompileResult error) {
switch (error) {
case NVPTXCOMPILE_SUCCESS:
return "NVPTXCOMPILE_SUCCESS";
case NVPTXCOMPILE_ERROR_INVALID_COMPILER_HANDLE:
return "NVPTXCOMPILE_ERROR_INVALID_COMPILER_HANDLE";
case NVPTXCOMPILE_ERROR_INVALID_INPUT:
return "NVPTXCOMPILE_ERROR_INVALID_INPUT";
case NVPTXCOMPILE_ERROR_COMPILATION_FAILURE:
return "NVPTXCOMPILE_ERROR_COMPILATION_FAILURE";
case NVPTXCOMPILE_ERROR_INTERNAL:
return "NVPTXCOMPILE_ERROR_INTERNAL";
case NVPTXCOMPILE_ERROR_OUT_OF_MEMORY:
return "NVPTXCOMPILE_ERROR_OUT_OF_MEMORY";
case NVPTXCOMPILE_ERROR_COMPILER_INVOCATION_INCOMPLETE:
return "NVPTXCOMPILE_ERROR_COMPILER_INVOCATION_INCOMPLETE";
case NVPTXCOMPILE_ERROR_UNSUPPORTED_PTX_VERSION:
return "NVPTXCOMPILE_ERROR_UNSUPPORTED_PTX_VERSION";
default:
return "<unknown>";
}
}
void set_exception(PyObject *exception_type,
const char* message_format,
nvPTXCompileResult error) {
char exception_message[256];
sprintf(exception_message, message_format, nvPTXGetErrorEnum(error));
PyErr_SetString(exception_type, exception_message);
}
static PyObject *get_version(PyObject *self) {
unsigned int major, minor;
nvPTXCompileResult res = nvPTXCompilerGetVersion(&major, &minor);
if (res != NVPTXCOMPILE_SUCCESS) {
set_exception(PyExc_RuntimeError,
"%s error when calling nvPTXCompilerGetVersion",
res);
return nullptr;
}
return Py_BuildValue("(II)", major, minor);
}
static PyObject *create(PyObject *self, PyObject *args) {
PyObject *ret = nullptr;
char *ptx_code;
nvPTXCompilerHandle *compiler;
if (!PyArg_ParseTuple(args, "s", &ptx_code))
return nullptr;
try {
compiler = new nvPTXCompilerHandle;
} catch (const std::bad_alloc &) {
PyErr_NoMemory();
return nullptr;
}
nvPTXCompileResult res =
nvPTXCompilerCreate(compiler, strlen(ptx_code), ptx_code);
if (res != NVPTXCOMPILE_SUCCESS) {
set_exception(PyExc_RuntimeError,
"%s error when calling nvPTXCompilerCreate",
res);
goto error;
}
if ((ret = PyLong_FromUnsignedLongLong((unsigned long long)compiler)) ==
nullptr) {
// Attempt to destroy the compiler - since we're already in an error
// condition, there's no point in checking the return code and taking any
// further action based on it though.
nvPTXCompilerDestroy(compiler);
goto error;
}
return ret;
error:
delete compiler;
return nullptr;
}
static PyObject *destroy(PyObject *self, PyObject *args) {
nvPTXCompilerHandle *compiler;
if (!PyArg_ParseTuple(args, "K", &compiler))
return nullptr;
nvPTXCompileResult res = nvPTXCompilerDestroy(compiler);
if (res != NVPTXCOMPILE_SUCCESS) {
set_exception(PyExc_RuntimeError,
"%s error when calling nvPTXCompilerDestroy",
res);
return nullptr;
}
delete compiler;
Py_RETURN_NONE;
}
static PyObject *compile(PyObject *self, PyObject *args) {
nvPTXCompilerHandle *compiler;
PyObject *options;
if (!PyArg_ParseTuple(args, "KO!", &compiler, &PyTuple_Type, &options))
return nullptr;
Py_ssize_t n_options = PyTuple_Size(options);
const char **compile_options = new char const *[n_options];
for (Py_ssize_t i = 0; i < n_options; i++) {
PyObject *item = PyTuple_GetItem(options, i);
compile_options[i] = PyUnicode_AsUTF8AndSize(item, nullptr);
}
nvPTXCompileResult res =
nvPTXCompilerCompile(*compiler, n_options, compile_options);
delete[] compile_options;
if (res != NVPTXCOMPILE_SUCCESS) {
set_exception(PyExc_RuntimeError,
"%s error when calling nvPTXCompilerCompile",
res);
return nullptr;
}
Py_RETURN_NONE;
}
static PyObject *get_error_log(PyObject *self, PyObject *args) {
nvPTXCompilerHandle *compiler;
if (!PyArg_ParseTuple(args, "K", &compiler))
return nullptr;
size_t error_log_size;
nvPTXCompileResult res =
nvPTXCompilerGetErrorLogSize(*compiler, &error_log_size);
if (res != NVPTXCOMPILE_SUCCESS) {
set_exception(PyExc_RuntimeError,
"%s error when calling nvPTXCompilerGetErrorLogSize",
res);
return nullptr;
}
// The size returned doesn't include a trailing null byte
char *error_log = new char[error_log_size + 1];
res = nvPTXCompilerGetErrorLog(*compiler, error_log);
if (res != NVPTXCOMPILE_SUCCESS) {
set_exception(PyExc_RuntimeError,
"%s error when calling nvPTXCompilerGetErrorLog",
res);
return nullptr;
}
PyObject *py_log = PyUnicode_FromStringAndSize(error_log, error_log_size);
// Once we've copied the log to a Python object we can delete it - we don't
// need to check whether creation of the Unicode object succeeded, because we
// delete the log either way.
delete[] error_log;
return py_log;
}
static PyObject *get_info_log(PyObject *self, PyObject *args) {
nvPTXCompilerHandle *compiler;
if (!PyArg_ParseTuple(args, "K", &compiler))
return nullptr;
size_t info_log_size;
nvPTXCompileResult res =
nvPTXCompilerGetInfoLogSize(*compiler, &info_log_size);
if (res != NVPTXCOMPILE_SUCCESS) {
set_exception(PyExc_RuntimeError,
"%s error when calling nvPTXCompilerGetInfoLogSize",
res);
return nullptr;
}
// The size returned doesn't include a trailing null byte
char *info_log = new char[info_log_size + 1];
res = nvPTXCompilerGetInfoLog(*compiler, info_log);
if (res != NVPTXCOMPILE_SUCCESS) {
set_exception(PyExc_RuntimeError,
"%s error when calling nvPTXCompilerGetInfoLog",
res);
return nullptr;
}
PyObject *py_log = PyUnicode_FromStringAndSize(info_log, info_log_size);
// Once we've copied the log to a Python object we can delete it - we don't
// need to check whether creation of the Unicode object succeeded, because we
// delete the log either way.
delete[] info_log;
return py_log;
}
static PyObject *get_compiled_program(PyObject *self, PyObject *args) {
nvPTXCompilerHandle *compiler;
if (!PyArg_ParseTuple(args, "K", &compiler))
return nullptr;
size_t compiled_program_size;
nvPTXCompileResult res =
nvPTXCompilerGetCompiledProgramSize(*compiler, &compiled_program_size);
if (res != NVPTXCOMPILE_SUCCESS) {
set_exception(PyExc_RuntimeError,
"%s error when calling nvPTXCompilerGetCompiledProgramSize",
res);
return nullptr;
}
char *compiled_program = new char[compiled_program_size];
res = nvPTXCompilerGetCompiledProgram(*compiler, compiled_program);
if (res != NVPTXCOMPILE_SUCCESS) {
set_exception(PyExc_RuntimeError,
"%s error when calling nvPTXCompilerGetCompiledProgram",
res);
return nullptr;
}
PyObject *py_prog =
PyBytes_FromStringAndSize(compiled_program, compiled_program_size);
// Once we've copied the compiled program to a Python object we can delete it
// - we don't need to check whether creation of the Unicode object succeeded,
// because we delete the compiled program either way.
delete[] compiled_program;
return py_prog;
}
static PyMethodDef ext_methods[] = {
{"get_version", (PyCFunction)get_version, METH_NOARGS,
"Returns a tuple giving the version"},
{"create", (PyCFunction)create, METH_VARARGS,
"Returns a handle to a new compiler object"},
{"destroy", (PyCFunction)destroy, METH_VARARGS,
"Given a handle, destroy a compiler object"},
{"compile", (PyCFunction)compile, METH_VARARGS,
"Given a handle, compile the PTX"},
{"get_error_log", (PyCFunction)get_error_log, METH_VARARGS,
"Given a handle, return the error log"},
{"get_info_log", (PyCFunction)get_info_log, METH_VARARGS,
"Given a handle, return the info log"},
{"get_compiled_program", (PyCFunction)get_compiled_program, METH_VARARGS,
"Given a handle, return the compiled program"},
{nullptr}};
static struct PyModuleDef moduledef = {
PyModuleDef_HEAD_INIT, "ptxcompiler",
"Provides access to PTX compiler API methods", -1, ext_methods};
PyMODINIT_FUNC PyInit__ptxcompilerlib(void) {
PyObject *m = PyModule_Create(&moduledef);
return m;
}