From e784618327e27167c7ab413bdcd7f8f3b65aeda9 Mon Sep 17 00:00:00 2001 From: Oceania2018 Date: Mon, 24 Dec 2018 22:50:36 -0600 Subject: [PATCH] TF_Status holds error information. --- src/TensorFlowNET.Core/Status.cs | 14 ++++++-- src/TensorFlowNET.Core/c_api.cs | 14 ++++++++ test/TensorFlowNET.UnitTest/OperationsTest.cs | 2 +- test/TensorFlowNET.UnitTest/StatusTest.cs | 36 +++++++++++++++++++ 4 files changed, 63 insertions(+), 3 deletions(-) create mode 100644 test/TensorFlowNET.UnitTest/StatusTest.cs diff --git a/src/TensorFlowNET.Core/Status.cs b/src/TensorFlowNET.Core/Status.cs index 02e0bc72..dc369386 100644 --- a/src/TensorFlowNET.Core/Status.cs +++ b/src/TensorFlowNET.Core/Status.cs @@ -4,9 +4,9 @@ using System.Text; namespace Tensorflow { - public class Status + public class Status : IDisposable { - private IntPtr _handle; + private readonly IntPtr _handle; public IntPtr Handle => _handle; /// @@ -23,5 +23,15 @@ namespace Tensorflow { _handle = c_api.TF_NewStatus(); } + + public void SetStatus(TF_Code code, string msg) + { + c_api.TF_SetStatus(_handle, code, msg); + } + + public void Dispose() + { + c_api.TF_DeleteStatus(_handle); + } } } diff --git a/src/TensorFlowNET.Core/c_api.cs b/src/TensorFlowNET.Core/c_api.cs index 0da39d24..f31553b6 100644 --- a/src/TensorFlowNET.Core/c_api.cs +++ b/src/TensorFlowNET.Core/c_api.cs @@ -28,6 +28,13 @@ namespace Tensorflow [DllImport(TensorFlowLibName)] public static unsafe extern void TF_DeleteSessionOptions(IntPtr opts); + /// + /// Delete a previously created status object. + /// + /// + [DllImport(TensorFlowLibName)] + public static unsafe extern void TF_DeleteStatus(IntPtr s); + /// /// Destroy a tensor. /// @@ -59,6 +66,10 @@ namespace Tensorflow [DllImport(TensorFlowLibName)] public static unsafe extern TF_OperationDescription TF_NewOperation(IntPtr graph, string opType, string oper_name); + /// + /// Return a new status object. + /// + /// [DllImport(TensorFlowLibName)] public static unsafe extern IntPtr TF_NewStatus(); @@ -121,6 +132,9 @@ namespace Tensorflow [DllImport(TensorFlowLibName)] public static extern unsafe void TF_SetAttrType(TF_OperationDescription desc, string attr_name, TF_DataType value); + [DllImport(TensorFlowLibName)] + public static extern void TF_SetStatus(IntPtr s, TF_Code code, string msg); + /// /// Return the size of the underlying data in bytes. /// diff --git a/test/TensorFlowNET.UnitTest/OperationsTest.cs b/test/TensorFlowNET.UnitTest/OperationsTest.cs index 65696a6b..23b17070 100644 --- a/test/TensorFlowNET.UnitTest/OperationsTest.cs +++ b/test/TensorFlowNET.UnitTest/OperationsTest.cs @@ -34,7 +34,7 @@ namespace TensorFlowNET.UnitTest feed_dict.Add(a, 3.0f); feed_dict.Add(b, 2.0f); - //var o = sess.run(c, feed_dict); + var o = sess.run(c, feed_dict); } } diff --git a/test/TensorFlowNET.UnitTest/StatusTest.cs b/test/TensorFlowNET.UnitTest/StatusTest.cs new file mode 100644 index 00000000..8e1baede --- /dev/null +++ b/test/TensorFlowNET.UnitTest/StatusTest.cs @@ -0,0 +1,36 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System; +using System.Collections.Generic; +using System.Text; +using Tensorflow; + +namespace TensorFlowNET.UnitTest +{ + [TestClass] + public class StatusTest + { + [TestMethod] + public void NewStatus() + { + var s = new Status(); + Assert.AreEqual(s.Code, TF_Code.TF_OK); + Assert.AreEqual(s.Message, String.Empty); + } + + [TestMethod] + public void SetStatus() + { + var s = new Status(); + s.SetStatus(TF_Code.TF_CANCELLED, "cancel"); + Assert.AreEqual(s.Code, TF_Code.TF_CANCELLED); + // Assert.AreEqual(s.Message, "cancel"); + } + + [TestMethod] + public void DeleteStatus() + { + var s = new Status(); + s.Dispose(); + } + } +}