@@ -236,50 +236,6 @@ PyTypeObject CallingConvention::pytype = {
236236};
237237
238238
239- // RAII wrapper around CUlibrary
240- namespace { class CudaLibrary {
241- public:
242- explicit CudaLibrary (const DriverApi* driver, CUlibrary lib) : driver_(driver), lib_(lib) {}
243-
244- CudaLibrary (CudaLibrary&& other) : driver_(other.driver_), lib_(other.lib_) {
245- other.lib_ = nullptr ;
246- }
247-
248- CudaLibrary (const CudaLibrary&) = delete ;
249- void operator =(const CudaLibrary&) = delete ;
250-
251- ~CudaLibrary () {
252- if (lib_) {
253- CUresult res = driver_->cuLibraryUnload (lib_);
254- CHECK (res == CUDA_SUCCESS );
255- }
256- }
257-
258- const CUlibrary& get () const {
259- return lib_;
260- }
261-
262- private:
263- const DriverApi* driver_;
264- CUlibrary lib_;
265- }; }
266-
267- static Result<CudaLibrary> load_cuda_library (const DriverApi* driver, const void * code) {
268- CUlibrary lib;
269- CUresult res = driver->cuLibraryLoadData (&lib, code, nullptr , nullptr , 0 ,
270- nullptr , nullptr , 0 );
271- if (res == CUDA_SUCCESS )
272- return CudaLibrary (driver, lib);
273-
274- return raise (PyExc_RuntimeError, " Failed to load CUDA library: %s" ,
275- get_cuda_error (driver, res));
276- }
277-
278- struct CudaKernel {
279- CudaLibrary lib;
280- CUkernel kernel;
281- };
282-
283239static Status enable_maximum_dynamic_shared_memory (const DriverApi *driver,
284240 const CUkernel kernel,
285241 const char *func_name) {
@@ -340,26 +296,6 @@ static Status enable_maximum_dynamic_shared_memory(const DriverApi *driver,
340296 return OK ;
341297}
342298
343- static Result<CudaKernel> load_cuda_kernel (const DriverApi* driver,
344- const char * cubin_data,
345- size_t cubin_size,
346- const char * func_name) {
347- (void ) cubin_size;
348-
349- Result<CudaLibrary> lib = load_cuda_library (driver, cubin_data);
350- if (!lib.is_ok ()) return ErrorRaised;
351-
352- CUkernel kernel;
353- CUresult res = driver->cuLibraryGetKernel (&kernel, lib->get (), func_name);
354- if (res != CUDA_SUCCESS ) {
355- return raise (PyExc_RuntimeError, " Failed to get kernel %s from library: %s" ,
356- func_name, get_cuda_error (driver, res));
357- }
358-
359- return CudaKernel{std::move (*lib), kernel};
360- }
361-
362-
363299// X(Name, #Attrs, MinStack, StackEffect)
364300#define FOREACH_SIZE_OPCODE (X ) \
365301 X (Const, 1 , 0 , 1 ) \
@@ -2308,6 +2244,7 @@ static Result<CUstream> parse_stream(PyObject* py_stream) {
23082244 return stream;
23092245}
23102246
2247+
23112248using StreamBufferPoolMap = HashMap<unsigned long long , StreamBufferPool*>;
23122249
23132250// Protected by GIL or g_launch_mutex.
@@ -2340,47 +2277,6 @@ static Result<StreamBufferPool*> get_stream_buffer_pool(const DriverApi* driver,
23402277 }
23412278}
23422279
2343- namespace { struct ContextGuard {
2344- bool need_to_pop;
2345- const DriverApi* driver_;
2346-
2347- ContextGuard (const DriverApi* driver) : need_to_pop(false ), driver_(driver) {}
2348-
2349- ContextGuard () = delete ;
2350- ContextGuard (const ContextGuard&) = delete ;
2351- void operator =(const ContextGuard&) = delete ;
2352-
2353- ~ContextGuard () {
2354- if (need_to_pop) {
2355- CUcontext old;
2356- CUresult res = driver_->cuCtxPopCurrent (&old);
2357- CHECK (res == CUDA_SUCCESS );
2358- }
2359- }
2360- }; }
2361-
2362- static Status maybe_switch_context (const DriverApi* driver, CUcontext target, ContextGuard& guard) {
2363- if (!target) return OK ;
2364-
2365- CUcontext current;
2366- CUresult res = driver->cuCtxGetCurrent (¤t);
2367- if (res != CUDA_SUCCESS ) {
2368- return raise (PyExc_RuntimeError, " Failed to get current CUDA context: %s" ,
2369- get_cuda_error (driver, res));
2370- }
2371-
2372- if (current == target) return OK ;
2373-
2374- res = driver->cuCtxPushCurrent (target);
2375- if (res != CUDA_SUCCESS ) {
2376- return raise (PyExc_RuntimeError, " Failed to switch CUDA context: %s" ,
2377- get_cuda_error (driver, res));
2378- }
2379-
2380- guard.need_to_pop = true ;
2381- return OK ;
2382- }
2383-
23842280struct Grid {
23852281 enum { Len = 3 };
23862282 unsigned dims[Len];
@@ -2541,7 +2437,7 @@ static Result<PreparedLaunch> prepare_launch(
25412437 bool capture_kernel_image,
25422438 bool stage_list_args,
25432439 StreamBufferTransaction& tx,
2544- ContextGuard & ctx_guard) {
2440+ CudaContextGuard & ctx_guard) {
25452441
25462442 LaunchHelperPtr helper = launch_helper_get ();
25472443
@@ -2623,7 +2519,7 @@ static Result<PreparedLaunch> prepare_launch(
26232519 kernel_item = kernel_map.insert (std::move (helper->constants ), std::move (*res));
26242520 }
26252521
2626- if (!maybe_switch_context (driver, helper->cuda_context , ctx_guard ))
2522+ if (!ctx_guard. switch_to ( helper->cuda_context ))
26272523 return ErrorRaised;
26282524
26292525 if (stage_list_args
@@ -2661,7 +2557,7 @@ static Status launch(const DriverApi* driver,
26612557 PyObject* const * pyargs,
26622558 Py_ssize_t num_pyargs
26632559 ) {
2664- ContextGuard ctx_guard (driver);
2560+ CudaContextGuard ctx_guard (driver);
26652561 StreamBufferTransaction tx;
26662562 Result<PreparedLaunch> prep = prepare_launch (
26672563 driver, dispatcher_pyobj, launch_stream, pyargs, num_pyargs,
@@ -2974,6 +2870,9 @@ parse_parameter_annotation_nodes_seq(PyObject* nodes_seq) {
29742870 return result;
29752871}
29762872
2873+
2874+
2875+
29772876static int TileContext_init (PyObject* self, PyObject* args, PyObject* kwargs) {
29782877 const char * keywords[] = {" config" , nullptr };
29792878 PyObject* config = nullptr ;
@@ -3330,7 +3229,7 @@ static PyObject* cuda_tile_benchmark(PyObject* mod, PyObject* const* args, Py_ss
33303229 Result<const DriverApi*> driver = get_driver_api ();
33313230 if (!driver.is_ok ()) return nullptr ;
33323231
3333- ContextGuard ctx_guard (*driver);
3232+ CudaContextGuard ctx_guard (*driver);
33343233 StreamBufferTransaction tx;
33353234 Result<PreparedLaunch> prep = prepare_launch (
33363235 *driver, launch_args.dispatcher , launch_args.stream ,
@@ -3363,7 +3262,7 @@ static PyObject* cuda_tile_export_ipc_benchmark_payload(PyObject*, PyObject* con
33633262 Result<const DriverApi*> driver = get_driver_api ();
33643263 if (!driver.is_ok ()) return nullptr ;
33653264
3366- ContextGuard ctx_guard (*driver);
3265+ CudaContextGuard ctx_guard (*driver);
33673266 StreamBufferTransaction tx;
33683267 Result<PreparedLaunch> prep = prepare_launch (
33693268 *driver, launch_args.dispatcher , launch_args.stream ,
@@ -3390,6 +3289,7 @@ static PyObject* cuda_tile_export_ipc_benchmark_payload(PyObject*, PyObject* con
33903289 const char * symbol = PyUnicode_AsUTF8AndSize (kernel_image.symbol .get (), &symbol_size);
33913290 if (!symbol) return nullptr ;
33923291
3292+
33933293 CUdevice device;
33943294 CUresult res = (*driver)->cuCtxGetDevice (&device);
33953295 if (res != CUDA_SUCCESS ) {
@@ -3512,8 +3412,8 @@ static PyObject* cuda_tile_benchmark_with_ipc_payload(PyObject*, PyObject* const
35123412 return nullptr ;
35133413 }
35143414
3515- ContextGuard ctx_guard (*driver);
3516- if (!maybe_switch_context (*driver, ctx, ctx_guard ))
3415+ CudaContextGuard ctx_guard (*driver);
3416+ if (!ctx_guard. switch_to ( ctx))
35173417 return nullptr ;
35183418
35193419 IpcHandleCreator ipc_mem_handle (*driver);
@@ -3797,4 +3697,3 @@ Status tile_kernel_init(PyObject* m) {
37973697
37983698 return OK ;
37993699}
3800-
0 commit comments