Exemple #1
0
 public virtual void Register(SafeContextHandle context)
 {
     unsafe
     {
         UnsafeNativeMethods.gvAddLibrary(context, (gvplugin_library_t *)_library.DangerousGetHandle());
     }
 }
Exemple #2
0
 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;
 }
Exemple #3
0
 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;
     }
 }
Exemple #4
0
        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);
        }
Exemple #6
0
        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);
        }
Exemple #7
0
        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);
            }
        }
Exemple #8
0
        /// <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);
        }
Exemple #10
0
 public static extern void TFE_ContextSetExecutorForThread(SafeContextHandle ctx, SafeExecutorHandle executor);
Exemple #11
0
 public static extern TF_AttrType TFE_OpNameGetAttrType(SafeContextHandle ctx, string op_or_function_name, string attr_name, ref byte is_list, SafeStatusHandle status);
Exemple #12
0
 public static extern SafeDeviceListHandle TFE_ContextListDevices(SafeContextHandle ctx, SafeStatusHandle status);
Exemple #13
0
 public static extern SafeOpHandle TFE_NewOp(SafeContextHandle ctx, string op_or_function_name, SafeStatusHandle status);
Exemple #14
0
 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);
Exemple #16
0
 protected SafeExecutorHandle TFE_ContextGetExecutorForThread(SafeContextHandle ctx)
 => c_api.TFE_ContextGetExecutorForThread(ctx);
Exemple #17
0
        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]);
        }
Exemple #18
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);
Exemple #21
0
 public static extern SafeExecutorHandle TFE_ContextGetExecutorForThread(SafeContextHandle ctx);
 public static extern void TFE_ContextAddFunction(SafeContextHandle ctx, IntPtr function, SafeStatusHandle status);
Exemple #23
0
 public static extern void TFE_ContextEndStep(SafeContextHandle ctx);
 public static extern void TFE_ContextRemoveFunction(SafeContextHandle ctx, string name, SafeStatusHandle status);
Exemple #25
0
 protected SafeDeviceListHandle TFE_ContextListDevices(SafeContextHandle ctx, SafeStatusHandle status)
 => c_api.TFE_ContextListDevices(ctx, status);
Exemple #26
0
 protected SafeOpHandle TFE_NewOp(SafeContextHandle ctx, string op_or_function_name, SafeStatusHandle status)
 => c_api.TFE_NewOp(ctx, op_or_function_name, status);
Exemple #27
0
 protected SafeTensorHandleHandle TFE_TensorHandleCopyToDevice(SafeTensorHandleHandle h, SafeContextHandle ctx, string device_name, SafeStatusHandle status)
 => c_api.TFE_TensorHandleCopyToDevice(h, ctx, device_name, status);
Exemple #28
0
 public static extern IntPtr TFE_TensorHandleCopyToDevice(IntPtr h, SafeContextHandle ctx, string device_name, SafeStatusHandle status);