Caffe2 - C++ API
A deep learning, cross platform ML framework
cuda_lazy_init.cpp
1 #include <torch/csrc/utils/cuda_lazy_init.h>
2 
3 #include <torch/csrc/python_headers.h>
4 #include <mutex>
5 
6 #include <torch/csrc/Exceptions.h>
7 #include <torch/csrc/utils/object_ptr.h>
8 
9 namespace torch {
10 namespace utils {
11 
12 void cuda_lazy_init() {
13  AutoGIL g;
14  // Protected by the GIL. We don't use call_once because under ASAN it
15  // has a buggy implementation that deadlocks if an instance throws an
16  // exception. In any case, call_once isn't necessary, because we
17  // have taken a lock.
18  static bool run_yet = false;
19  if (!run_yet) {
20  auto module = THPObjectPtr(PyImport_ImportModule("torch.cuda"));
21  if (!module) throw python_error();
22  auto res = THPObjectPtr(PyObject_CallMethod(module.get(), "_lazy_init", ""));
23  if (!res) throw python_error();
24  run_yet = true;
25  }
26 }
27 
28 }
29 }
Definition: jit_type.h:17