def build_checker(): if hasattr(build_checker, "dll"): return build_checker.dll checker_code_path = os.path.join( os.path.dirname(inspect.getfile(daceml.onnx)), "include", "op_checker.h") with open(checker_code_path, "r") as f: checker_code = f.read() program = codeobject.CodeObject("onnx_op_checker", checker_code, "cpp", targets.cpu.CPUCodeGen, "ONNXOpChecker", environments={"ONNXRuntime"}) BUILD_PATH = os.path.join('.dacecache', "onnx_op_checker") compiler.generate_program_folder(None, [program], BUILD_PATH) compiler.configure_and_compile(BUILD_PATH) checker_dll = ctypes.CDLL( compiler.get_binary_name(BUILD_PATH, "onnx_op_checker")) build_checker.dll = checker_dll return checker_dll
def cuda_helper(): helper_code = """ #include <dace/dace.h> extern "C" { int host_to_gpu(void* gpu, void* host, size_t size) { auto result = cudaMemcpy(gpu, host, size, cudaMemcpyHostToDevice); DACE_CUDA_CHECK(cudaGetLastError()); DACE_CUDA_CHECK(cudaDeviceSynchronize()); return result; } } """ program = codeobject.CodeObject("cuda_helper", helper_code, "cpp", targets.cpu.CPUCodeGen, "CudaHelper") dummy_cuda_target = codeobject.CodeObject("dummy", "", "cu", targets.cuda.CUDACodeGen, "CudaDummy") build_folder = dace.Config.get('default_build_folder') BUILD_PATH = os.path.join(build_folder, "cuda_helper") compiler.generate_program_folder(None, [program, dummy_cuda_target], BUILD_PATH) compiler.configure_and_compile(BUILD_PATH) checker_dll = compiled_sdfg.ReloadableDLL( compiler.get_binary_name(BUILD_PATH, "cuda_helper"), "cuda_helper") class CudaHelper: def __init__(self): self.dll = checker_dll checker_dll.load() self._host_to_gpu = checker_dll.get_symbol("host_to_gpu") self._host_to_gpu.restype = ctypes.c_int def __del__(self): self.dll.unload() def host_to_gpu(self, gpu_ptr: int, numpy_array: np.ndarray): size = ctypes.sizeof( dtypes._FFI_CTYPES[numpy_array.dtype.type]) * numpy_array.size result = ctypes.c_int( self._host_to_gpu( ctypes.c_void_p(gpu_ptr), ctypes.c_void_p( numpy_array.__array_interface__["data"][0]), ctypes.c_size_t(size))) if result.value != 0: raise ValueError("host_to_gpu returned nonzero result!") return CudaHelper()