33import re
44import subprocess
55import tempfile
6+ import textwrap
67import uuid
78
89import ninetoothed .dtype
@@ -33,7 +34,7 @@ def aot(
3334 output_contents = _aot (func , caller , kernel_name , num_warps , num_stages )
3435
3536 for output_name , output_content in output_contents .items ():
36- output_path = output_dir / f"{ kernel_name } { output_name [ - 2 :] } "
37+ output_path = output_dir / f"{ kernel_name } { pathlib . Path ( output_name ). suffix } "
3738
3839 with open (output_path , "w" ) as f :
3940 f .write (output_content )
@@ -121,24 +122,48 @@ def _find_tensor_by_source_name(tensors, name):
121122 pattern = rf"\({ ', ' .join (rf'(.*) { param } ' for param in param_strings )} \)"
122123 c_param_type_strings = re .search (pattern , c_header_file ).groups ()
123124
125+ kernel_name_with_hash = f"{ kernel_name } _{ signature_hash } "
126+
124127 unparser = _Unparser (c_param_type_strings )
125128
126129 launch_func_unparsed = unparser .unparse (launch_func )
130+ launch_func_unparsed_lines = launch_func_unparsed .splitlines ()
131+ launch_func_unparsed_lines .insert (1 , f"{ _INDENTATION } cuCtxGetId(NULL, &ctx_id);\n " )
132+ launch_func_unparsed_lines .insert (1 , f"{ _INDENTATION } unsigned long long ctx_id;" )
133+ launch_func_unparsed = "\n " .join (launch_func_unparsed_lines )
127134 launch_func_unparsed = launch_func_unparsed .replace (
128- func .__name__ , f"{ kernel_name } _ { signature_hash } "
135+ func .__name__ , f"kernels[ctx_id]. { kernel_name_with_hash } "
129136 )
130137
131- c_source_file = f"{ c_source_file } \n { launch_func_unparsed } \n "
132138 c_source_file = c_source_file .replace ("<stdint.h>" , f'"{ _HEADER_PATH } "' )
133139 output_contents [c_source_file_name ] = c_source_file
134140
135141 c_header_file = f'{ c_header_file } \n #ifdef __cplusplus\n extern "C" { unparser .header } ;\n #else\n { unparser .header } ;\n #endif\n '
136142 c_header_file = c_header_file .replace ("<stdint.h>" , f'"{ _HEADER_PATH } "' )
137143 output_contents [c_header_file_name ] = c_header_file
138144
145+ kernel_start = c_source_file .find ("//" )
146+ kernel_end = len (c_source_file )
147+ cpp_source_file = (
148+ c_source_file [:kernel_start ]
149+ + f"namespace { kernel_name_with_hash } {{\n "
150+ + "struct Kernel {\n "
151+ + textwrap .indent (c_source_file [kernel_start :kernel_end ], _INDENTATION )
152+ + "};\n "
153+ + textwrap .indent (c_source_file [kernel_end :], _INDENTATION )
154+ + "}\n "
155+ + f"\n static ThreadSafeUnorderedMap<unsigned long long, { kernel_name_with_hash } ::Kernel> kernels;\n "
156+ + f'\n extern "C" { launch_func_unparsed } \n '
157+ )
158+ cpp_source_file_name = f"{ kernel_name } .{ signature_hash } .cpp"
159+ output_contents [cpp_source_file_name ] = cpp_source_file
160+ output_contents .pop (c_source_file_name )
161+
139162 return output_contents
140163
141164
165+ _INDENTATION = " "
166+
142167_MACRO_MAPPING = {True : ("NINETOOTHED_TRUE" , 1 ), False : ("NINETOOTHED_FALSE" , 0 )}
143168
144169_DTYPE_MAPPING = {
@@ -163,6 +188,41 @@ def _find_tensor_by_source_name(tensors, name):
163188
164189_DATA_TYPE_BODY_CONTENT = ",\n " .join (_DTYPE_MAPPING .values ())
165190
191+ _THREAD_SAFE_UNORDERED_MAP_CONTENT = """#include <mutex>
192+ #include <shared_mutex>
193+ #include <unordered_map>
194+
195+ template<typename Key, typename Value>
196+ class ThreadSafeUnorderedMap {
197+ public:
198+ Value& operator[](const Key& key) {
199+ {
200+ std::shared_lock lock{mutex};
201+
202+ auto iter = map.find(key);
203+
204+ if (iter != map.end()) {
205+ return iter->second;
206+ }
207+ }
208+
209+ std::unique_lock lock{mutex};
210+
211+ auto iter = map.find(key);
212+
213+ if (iter == map.end()) {
214+ iter = map.emplace(key, Value{}).first;
215+ }
216+
217+ return iter->second;
218+ }
219+
220+ private:
221+ std::unordered_map<Key, Value> map;
222+
223+ mutable std::shared_mutex mutex;
224+ };"""
225+
166226_HEADER_CONTENT = f"""#ifndef NINETOOTHED_H
167227#define NINETOOTHED_H
168228
@@ -184,6 +244,10 @@ def _find_tensor_by_source_name(tensors, name):
184244
185245typedef int NineToothedResult;
186246
247+ #ifdef __cplusplus
248+ { _THREAD_SAFE_UNORDERED_MAP_CONTENT }
249+ #endif
250+
187251#endif // NINETOOTHED_H
188252"""
189253
0 commit comments