Skip to content

Commit 6b8ae2b

Browse files
committed
Move param_packer.cpp -> param_packer.h
1 parent 459bbcb commit 6b8ae2b

2 files changed

Lines changed: 155 additions & 167 deletions

File tree

cuda_bindings/cuda/bindings/_lib/param_packer.cpp

Lines changed: 0 additions & 159 deletions
This file was deleted.
Lines changed: 155 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,159 @@
11
// SPDX-FileCopyrightText: Copyright (c) 2021-2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
22
// SPDX-License-Identifier: LicenseRef-NVIDIA-SOFTWARE-LICENSE
3-
//
4-
// Please refer to the NVIDIA end user license agreement (EULA) associated
5-
// with this source code for terms and conditions that govern your use of
6-
// this software. Any use, reproduction, disclosure, or distribution of
7-
// this software and related documentation outside the terms of the EULA
8-
// is strictly prohibited.
9-
#pragma once
3+
104
#include <Python.h>
5+
#include "param_packer.h"
6+
7+
#include <map>
8+
#include <functional>
9+
#include <stdexcept>
10+
#include <string>
11+
12+
PyObject* enum_module = nullptr;
13+
PyTypeObject* enum_Enum = nullptr;
14+
15+
PyObject* ctypes_module = nullptr;
16+
PyObject* ctypes_addressof = nullptr;
17+
PyObject* addressof_param_tuple = nullptr;
18+
19+
PyTypeObject* ctypes_c_char = nullptr;
20+
PyTypeObject* ctypes_c_bool = nullptr;
21+
PyTypeObject* ctypes_c_wchar = nullptr;
22+
PyTypeObject* ctypes_c_byte = nullptr;
23+
PyTypeObject* ctypes_c_ubyte = nullptr;
24+
PyTypeObject* ctypes_c_short = nullptr;
25+
PyTypeObject* ctypes_c_ushort = nullptr;
26+
PyTypeObject* ctypes_c_int = nullptr;
27+
PyTypeObject* ctypes_c_uint = nullptr;
28+
PyTypeObject* ctypes_c_long = nullptr;
29+
PyTypeObject* ctypes_c_ulong = nullptr;
30+
PyTypeObject* ctypes_c_longlong = nullptr;
31+
PyTypeObject* ctypes_c_ulonglong = nullptr;
32+
PyTypeObject* ctypes_c_size_t = nullptr;
33+
PyTypeObject* ctypes_c_float = nullptr;
34+
PyTypeObject* ctypes_c_double = nullptr;
35+
PyTypeObject* ctypes_c_void_p = nullptr;
36+
37+
PyTypeObject* ctypes_c_ssize_t = nullptr;
38+
PyTypeObject* ctypes_c_longdouble = nullptr;
39+
PyTypeObject* ctypes_c_char_p = nullptr;
40+
PyTypeObject* ctypes_c_wchar_p = nullptr;
41+
PyTypeObject* ctypes_c_structure = nullptr;
42+
43+
void fetch_ctypes()
44+
{
45+
ctypes_module = PyImport_ImportModule("ctypes");
46+
if (ctypes_module == nullptr)
47+
throw std::runtime_error("Cannot import ctypes module");
48+
// get method addressof
49+
PyObject* ctypes_dict = PyModule_GetDict(ctypes_module);
50+
if (ctypes_dict == nullptr)
51+
throw std::runtime_error(std::string("FAILURE @ ") + std::string(__FILE__) + " : " + std::to_string(__LINE__));
52+
// supportedtypes
53+
ctypes_c_int = (PyTypeObject*) PyDict_GetItemString(ctypes_dict, "c_int");
54+
ctypes_c_char = (PyTypeObject*) PyDict_GetItemString(ctypes_dict, "c_char");
55+
ctypes_c_bool = (PyTypeObject*) PyDict_GetItemString(ctypes_dict, "c_bool");
56+
ctypes_c_wchar = (PyTypeObject*) PyDict_GetItemString(ctypes_dict, "c_wchar");
57+
ctypes_c_byte = (PyTypeObject*) PyDict_GetItemString(ctypes_dict, "c_byte");
58+
ctypes_c_ubyte = (PyTypeObject*) PyDict_GetItemString(ctypes_dict, "c_ubyte");
59+
ctypes_c_short = (PyTypeObject*) PyDict_GetItemString(ctypes_dict, "c_short");
60+
ctypes_c_ushort = (PyTypeObject*) PyDict_GetItemString(ctypes_dict, "c_ushort");
61+
ctypes_c_int = (PyTypeObject*) PyDict_GetItemString(ctypes_dict, "c_int");
62+
ctypes_c_uint = (PyTypeObject*) PyDict_GetItemString(ctypes_dict, "c_uint");
63+
ctypes_c_long = (PyTypeObject*) PyDict_GetItemString(ctypes_dict, "c_long");
64+
ctypes_c_ulong = (PyTypeObject*) PyDict_GetItemString(ctypes_dict, "c_ulong");
65+
ctypes_c_longlong = (PyTypeObject*) PyDict_GetItemString(ctypes_dict, "c_longlong");
66+
ctypes_c_ulonglong = (PyTypeObject*) PyDict_GetItemString(ctypes_dict, "c_ulonglong");
67+
ctypes_c_size_t = (PyTypeObject*) PyDict_GetItemString(ctypes_dict, "c_size_t");
68+
ctypes_c_float = (PyTypeObject*) PyDict_GetItemString(ctypes_dict, "c_float");
69+
ctypes_c_double = (PyTypeObject*) PyDict_GetItemString(ctypes_dict, "c_double");
70+
ctypes_c_void_p = (PyTypeObject*) PyDict_GetItemString(ctypes_dict, "c_void_p"); // == c_voidp
71+
}
72+
73+
74+
// (target type, source type)
75+
std::map<std::pair<PyTypeObject*,PyTypeObject*>, std::function<int(void*, PyObject*)>> m_feeders;
76+
77+
void populate_feeders(PyTypeObject* target_t, PyTypeObject* source_t)
78+
{
79+
if (target_t == ctypes_c_int)
80+
{
81+
if (source_t == &PyLong_Type)
82+
{
83+
m_feeders[{target_t,source_t}] = [](void* ptr, PyObject* value) -> int
84+
{
85+
*((int*)ptr) = (int)PyLong_AsLong(value);
86+
return sizeof(int);
87+
};
88+
return;
89+
}
90+
} else if (target_t == ctypes_c_bool) {
91+
if (source_t == &PyBool_Type)
92+
{
93+
m_feeders[{target_t,source_t}] = [](void* ptr, PyObject* value) -> int
94+
{
95+
*((bool*)ptr) = (value == Py_True);
96+
return sizeof(bool);
97+
};
98+
return;
99+
}
100+
} else if (target_t == ctypes_c_byte) {
101+
if (source_t == &PyLong_Type)
102+
{
103+
m_feeders[{target_t,source_t}] = [](void* ptr, PyObject* value) -> int
104+
{
105+
*((int8_t*)ptr) = (int8_t)PyLong_AsLong(value);
106+
return sizeof(int8_t);
107+
};
108+
return;
109+
}
110+
} else if (target_t == ctypes_c_double) {
111+
if (source_t == &PyFloat_Type)
112+
{
113+
m_feeders[{target_t,source_t}] = [](void* ptr, PyObject* value) -> int
114+
{
115+
*((double*)ptr) = (double)PyFloat_AsDouble(value);
116+
return sizeof(double);
117+
};
118+
return;
119+
}
120+
} else if (target_t == ctypes_c_float) {
121+
if (source_t == &PyFloat_Type)
122+
{
123+
m_feeders[{target_t,source_t}] = [](void* ptr, PyObject* value) -> int
124+
{
125+
*((float*)ptr) = (float)PyFloat_AsDouble(value);
126+
return sizeof(float);
127+
};
128+
return;
129+
}
130+
} else if (target_t == ctypes_c_longlong) {
131+
if (source_t == &PyLong_Type)
132+
{
133+
m_feeders[{target_t,source_t}] = [](void* ptr, PyObject* value) -> int
134+
{
135+
*((long long*)ptr) = (long long)PyLong_AsLongLong(value);
136+
return sizeof(long long);
137+
};
138+
return;
139+
}
140+
}
141+
}
11142

12-
int feed(void* ptr, PyObject* value, PyObject* type);
143+
int feed(void* ptr, PyObject* value, PyObject* type)
144+
{
145+
PyTypeObject* pto = (PyTypeObject*)type;
146+
if (ctypes_c_int == nullptr)
147+
fetch_ctypes();
148+
auto found = m_feeders.find({pto,value->ob_type});
149+
if (found == m_feeders.end())
150+
{
151+
populate_feeders(pto, value->ob_type);
152+
found = m_feeders.find({pto,value->ob_type});
153+
}
154+
if (found != m_feeders.end())
155+
{
156+
return found->second(ptr, value);
157+
}
158+
return 0;
159+
}

0 commit comments

Comments
 (0)