public virtual void Register(SafeContextHandle context) { unsafe { UnsafeNativeMethods.gvAddLibrary(context, (gvplugin_library_t *)_library.DangerousGetHandle()); } }
public Context(ContextOptions opts, Status status) { Handle = c_api.TFE_NewContext(opts.Handle, status.Handle); status.Check(true); context_switches = new ContextSwitchStack(defaultExecutionMode == EAGER_MODE); initialized = true; }
public override void Register(SafeContextHandle context) { base.Register(context); unsafe { GVC_t * gvc = (GVC_t *)context.DangerousGetHandle(); PluginEngine textLayoutEngine = Engines.Single(i => i.Api == api_t.TextLayout); gvc->textlayout.engine = (gvtextlayout_engine_t *)textLayoutEngine.InstalledPluginData->engine; } }
private static void drawGraph(SafeContextHandle context, GraphHandle g) { int result = UnsafeNativeMethods.gvRenderFilename(context, g, "wpf", "test.dot"); if (result != 0) { string message = UnsafeNativeMethods.aglasterr(); throw new Exception(message); } }
IntPtr ShapeOp(SafeContextHandle ctx, IntPtr a) { using var status = TF_NewStatus(); var op = TFE_NewOp(ctx, "Shape", status); CHECK_EQ(TF_OK, TF_GetCode(status), TF_Message(status)); TFE_OpAddInput(op, a, status); CHECK_EQ(TF_OK, TF_GetCode(status), TF_Message(status)); TFE_OpSetAttrType(op, "T", TFE_TensorHandleDataType(a)); return(op); }
SafeOpHandle MatMulOp(SafeContextHandle ctx, SafeTensorHandleHandle a, SafeTensorHandleHandle b) { using var status = TF_NewStatus(); var op = TFE_NewOp(ctx, "MatMul", status); CHECK_EQ(TF_OK, TF_GetCode(status), TF_Message(status)); TFE_OpAddInput(op, a, status); CHECK_EQ(TF_OK, TF_GetCode(status), TF_Message(status)); TFE_OpAddInput(op, b, status); CHECK_EQ(TF_OK, TF_GetCode(status), TF_Message(status)); TFE_OpSetAttrType(op, "T", TFE_TensorHandleDataType(a)); return(op); }
private static void Main(string[] args) { // initialize using (SafeContextHandle gvc = UnsafeNativeMethods.gvContext()) { //UnsafeNativeMethods.aginit(); //UnsafeNativeMethods.agnodeattr(null, "label", "\\N"); //GVC_t* gvc = UnsafeNativeMethods.gvNEWcontext(null, true); //UnsafeNativeMethods.gvconfig(gvc, false); //DotLibrary library = new DotLibrary(gvc); //library.Register(); WpfLibrary library = new WpfLibrary(); library.Register(gvc); int result; string[] layoutPlugins = UnsafeNativeMethods.gvPluginList(gvc, "layout"); string[] renderPlugins = UnsafeNativeMethods.gvPluginList(gvc, "render"); // create graph GraphHandle graph = createGraph2(); //GraphHandle graph = createRailroadGraph(); result = UnsafeNativeMethods.gvLayout(gvc, graph, "dot"); if (result != 0) { string message = UnsafeNativeMethods.aglasterr(); throw new Exception(message); } //result = UnsafeNativeMethods.gvLayout(gvc, graph, "nop"); //if (result != 0) //{ // string message = UnsafeNativeMethods.aglasterr(); // throw new Exception(message); //} drawGraph(gvc, graph); UnsafeNativeMethods.gvFreeLayout(gvc, graph); UnsafeNativeMethods.agclose(graph); } }
/// <summary> /// Initialize handle and devices if not already done so. /// </summary> public void ensure_initialized() { if (initialized) { return; } Config = MergeConfig(); FunctionCallOptions.Config = Config; var config_str = Config.ToByteArray(); using var opts = new ContextOptions(); using var status = new Status(); c_api.TFE_ContextOptionsSetConfig(opts.Handle, config_str, (ulong)config_str.Length, status.Handle); status.Check(true); c_api.TFE_ContextOptionsSetDevicePlacementPolicy(opts.Handle, _device_policy); _handle = c_api.TFE_NewContext(opts.Handle, status.Handle); status.Check(true); initialized = true; }
bool GetDeviceName(SafeContextHandle ctx, ref string device_name, string device_type) { using var status = TF_NewStatus(); using var devices = TFE_ContextListDevices(ctx, status); CHECK_EQ(TF_OK, TF_GetCode(status), TF_Message(status)); int num_devices = TF_DeviceListCount(devices); for (int i = 0; i < num_devices; ++i) { var dev_type = TF_DeviceListType(devices, i, status); CHECK_EQ(TF_GetCode(status), TF_OK, TF_Message(status)); var dev_name = TF_DeviceListName(devices, i, status); CHECK_EQ(TF_GetCode(status), TF_OK, TF_Message(status)); if (dev_type == device_type) { device_name = dev_name; return(true); } } return(false); }
public static extern void TFE_ContextSetExecutorForThread(SafeContextHandle ctx, SafeExecutorHandle executor);
public static extern TF_AttrType TFE_OpNameGetAttrType(SafeContextHandle ctx, string op_or_function_name, string attr_name, ref byte is_list, SafeStatusHandle status);
public static extern SafeDeviceListHandle TFE_ContextListDevices(SafeContextHandle ctx, SafeStatusHandle status);
public static extern SafeOpHandle TFE_NewOp(SafeContextHandle ctx, string op_or_function_name, SafeStatusHandle status);
public static extern SafeEagerTensorHandle TFE_TensorHandleCopyToDevice(SafeEagerTensorHandle h, SafeContextHandle ctx, string device_name, SafeStatusHandle status);
public static extern bool TFE_ContextHasFunction(SafeContextHandle ctx, string name);
protected SafeExecutorHandle TFE_ContextGetExecutorForThread(SafeContextHandle ctx) => c_api.TFE_ContextGetExecutorForThread(ctx);
unsafe IntPtr CreateVariable(SafeContextHandle ctx, float value, SafeStatusHandle status) { var op = TFE_NewOp(ctx, "VarHandleOp", status); if (TF_GetCode(status) != TF_OK) { return(IntPtr.Zero); } TFE_OpSetAttrType(op, "dtype", TF_FLOAT); TFE_OpSetAttrShape(op, "shape", new long[0], 0, status); TFE_OpSetAttrString(op, "container", "", 0); TFE_OpSetAttrString(op, "shared_name", "", 0); if (TF_GetCode(status) != TF_OK) { return(IntPtr.Zero); } var var_handle = new IntPtr[1]; int num_retvals = 1; TFE_Execute(op, var_handle, ref num_retvals, status); TFE_DeleteOp(op); if (TF_GetCode(status) != TF_OK) { return(IntPtr.Zero); } CHECK_EQ(1, num_retvals); // Assign 'value' to it. op = TFE_NewOp(ctx, "AssignVariableOp", status); if (TF_GetCode(status) != TF_OK) { return(IntPtr.Zero); } TFE_OpSetAttrType(op, "dtype", TF_FLOAT); TFE_OpAddInput(op, var_handle[0], status); // Convert 'value' to a TF_Tensor then a TFE_TensorHandle. var t = c_api.TF_AllocateTensor(TF_DataType.TF_FLOAT, new long[0], 0, sizeof(float)); tf.memcpy(TF_TensorData(t).ToPointer(), &value, TF_TensorByteSize(t)); var value_handle = c_api.TFE_NewTensorHandle(t, status); if (TF_GetCode(status) != TF_OK) { return(IntPtr.Zero); } TFE_OpAddInput(op, value_handle, status); if (TF_GetCode(status) != TF_OK) { return(IntPtr.Zero); } num_retvals = 0; c_api.TFE_Execute(op, null, ref num_retvals, status); TFE_DeleteOp(op); if (TF_GetCode(status) != TF_OK) { return(IntPtr.Zero); } CHECK_EQ(0, num_retvals); return(var_handle[0]); }
protected IntPtr TFE_TensorHandleCopyToDevice(IntPtr h, SafeContextHandle ctx, string device_name, SafeStatusHandle status) => c_api.TFE_TensorHandleCopyToDevice(h, ctx, device_name, status);
public static extern void TFE_ContextSetLogDevicePlacement(SafeContextHandle ctx, bool enable, SafeStatusHandle status);
public static extern void TFE_ContextClearCaches(SafeContextHandle ctx);
public static extern SafeExecutorHandle TFE_ContextGetExecutorForThread(SafeContextHandle ctx);
public static extern void TFE_ContextAddFunction(SafeContextHandle ctx, IntPtr function, SafeStatusHandle status);
public static extern void TFE_ContextEndStep(SafeContextHandle ctx);
public static extern void TFE_ContextRemoveFunction(SafeContextHandle ctx, string name, SafeStatusHandle status);
protected SafeDeviceListHandle TFE_ContextListDevices(SafeContextHandle ctx, SafeStatusHandle status) => c_api.TFE_ContextListDevices(ctx, status);
protected SafeOpHandle TFE_NewOp(SafeContextHandle ctx, string op_or_function_name, SafeStatusHandle status) => c_api.TFE_NewOp(ctx, op_or_function_name, status);
protected SafeTensorHandleHandle TFE_TensorHandleCopyToDevice(SafeTensorHandleHandle h, SafeContextHandle ctx, string device_name, SafeStatusHandle status) => c_api.TFE_TensorHandleCopyToDevice(h, ctx, device_name, status);
public static extern IntPtr TFE_TensorHandleCopyToDevice(IntPtr h, SafeContextHandle ctx, string device_name, SafeStatusHandle status);