initialize static method
Initialises the GPUEngine. If no target platform is provided initialising with cuda is attempted.
debug enables full printout of every interaction for in debugging. Initialize has to be called at least once before using the GPUEngine.
Implementation
static Future<void> initialize(
{bool debug = false, Target target = Target.cuda}) async {
String libName = "";
if (target == Target.cuda) {
if (Platform.isWindows) {
libName = "cuda_executor.dll";
} else if (Platform.isLinux) {
libName = "libcuda_executor.so";
}
} else if (target == Target.androidArm64) {
libName = "libandroid_arm64.so";
} else if (target == Target.androidX8664) {
libName = "libandroid_x86_64.so";
}
DynamicLibrary dylib;
try {
// Windows and Linux automatically check the directory of the executable for the DLL/SO and its dependencies.
if (Platform.isAndroid || Platform.isWindows || Platform.isLinux) {
dylib = DynamicLibrary.open(libName);
} else {
throw Exception("Unsupported platform");
}
Logger.green("GPUEngine: Successfully loaded ${target.name} backend.");
} catch (e) {
Logger.red(
"CRITICAL FFI ERROR: Failed to open library $libName. Error: $e");
rethrow;
}
_create = dylib.lookupFunction<NativeCreate, DartCreate>('create_executor');
_free = dylib.lookupFunction<NativeFree, DartFree>('free_executor');
_load = dylib.lookupFunction<NativeLoad, DartLoad>('load_tensor_h2d');
_retrieve = dylib.lookupFunction<NativeRetrieve, DartRetrieve>(
'retrieve_tensor_d2h_into');
_run = dylib.lookupFunction<NativeRun, DartRun>('run_tape');
_freeTensor = dylib.lookupFunction<NativeFreeT, DartFreeT>('free_tensor');
_getTensorNames =
dylib.lookupFunction<NativeGetNames, DartGetNames>('get_tensor_names');
_freeString =
dylib.lookupFunction<NativeFreeStr, DartFreeStr>('free_string');
_addPointers = dylib
.lookupFunction<NativeAddPointers, DartAddPointers>('add_pointers');
_initRandom = dylib.lookupFunction<NativeInitRandom, DartInitRandom>(
'init_random_uniform');
handle = _create(debug);
}