def load_from_file(sdfg, binary_filename): if not os.path.isfile(binary_filename): raise FileNotFoundError('File not found: ' + binary_filename) # Load the generated library lib = csd.ReloadableDLL(binary_filename, sdfg.name) # Load and return the compiled function return csd.CompiledSDFG(sdfg, lib)
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()
def load_precompiled_sdfg(folder: str): """ Loads a pre-compiled SDFG from an output folder (e.g. ".dacecache/program"). Folder must contain a file called "program.sdfg" and a subfolder called "build" with the shared object. :param folder: Path to SDFG output folder. :return: A callable CompiledSDFG object. """ from dace.codegen import compiled_sdfg as csdfg sdfg = SDFG.from_file(os.path.join(folder, 'program.sdfg')) suffix = config.Config.get('compiler', 'library_extension') return csdfg.CompiledSDFG( sdfg, csdfg.ReloadableDLL( os.path.join(folder, 'build', f'lib{sdfg.name}.{suffix}'), sdfg.name))
def get_program_handle(library_path, sdfg): lib = csd.ReloadableDLL(library_path, sdfg.name) # Load and return the compiled function return csd.CompiledSDFG(sdfg, lib, sdfg.arg_names)