Skip to content

Commit 09600a8

Browse files
sharwellOceania2018
authored andcommitted
Implement SafeDeviceListHandle as a wrapper for TF_DeviceList
1 parent 947eef4 commit 09600a8

6 files changed

Lines changed: 65 additions & 27 deletions

File tree

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*****************************************************************************
2+
Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
******************************************************************************/
16+
17+
using System;
18+
using Tensorflow.Util;
19+
20+
namespace Tensorflow.Device
21+
{
22+
public sealed class SafeDeviceListHandle : SafeTensorflowHandle
23+
{
24+
private SafeDeviceListHandle()
25+
{
26+
}
27+
28+
public SafeDeviceListHandle(IntPtr handle)
29+
: base(handle)
30+
{
31+
}
32+
33+
protected override bool ReleaseHandle()
34+
{
35+
c_api.TF_DeleteDeviceList(handle);
36+
SetHandle(IntPtr.Zero);
37+
return true;
38+
}
39+
}
40+
}

‎src/TensorFlowNET.Core/Device/c_api.device.cs‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ limitations under the License.
1616

1717
using System;
1818
using System.Runtime.InteropServices;
19+
using Tensorflow.Device;
1920
using Tensorflow.Eager;
2021

2122
namespace Tensorflow
@@ -36,7 +37,7 @@ public partial class c_api
3637
/// <param name="list">TF_DeviceList*</param>
3738
/// <returns></returns>
3839
[DllImport(TensorFlowLibName)]
39-
public static extern int TF_DeviceListCount(IntPtr list);
40+
public static extern int TF_DeviceListCount(SafeDeviceListHandle list);
4041

4142
/// <summary>
4243
/// Retrieves the type of the device at the given index.
@@ -46,7 +47,7 @@ public partial class c_api
4647
/// <param name="status">TF_Status*</param>
4748
/// <returns></returns>
4849
[DllImport(TensorFlowLibName)]
49-
public static extern IntPtr TF_DeviceListType(IntPtr list, int index, SafeStatusHandle status);
50+
public static extern IntPtr TF_DeviceListType(SafeDeviceListHandle list, int index, SafeStatusHandle status);
5051

5152
/// <summary>
5253
/// Deallocates the device list.
@@ -77,6 +78,6 @@ public partial class c_api
7778
/// <param name="index"></param>
7879
/// <param name="status">TF_Status*</param>
7980
[DllImport(TensorFlowLibName)]
80-
public static extern IntPtr TF_DeviceListName(IntPtr list, int index, SafeStatusHandle status);
81+
public static extern IntPtr TF_DeviceListName(SafeDeviceListHandle list, int index, SafeStatusHandle status);
8182
}
8283
}

‎src/TensorFlowNET.Core/Eager/c_api.eager.cs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Runtime.InteropServices;
3+
using Tensorflow.Device;
34
using Tensorflow.Eager;
45
using TFE_Executor = System.IntPtr;
56

@@ -317,7 +318,7 @@ public delegate void delete_backward_function_callback(string op_name,
317318
/// <param name="status">TF_Status*</param>
318319
/// <returns></returns>
319320
[DllImport(TensorFlowLibName)]
320-
public static extern IntPtr TFE_ContextListDevices(SafeContextHandle ctx, SafeStatusHandle status);
321+
public static extern SafeDeviceListHandle TFE_ContextListDevices(SafeContextHandle ctx, SafeStatusHandle status);
321322

322323
/// <summary>
323324
///

‎test/TensorFlowNET.UnitTest/NativeAPI/CApiTest.cs‎

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
11
using Microsoft.VisualStudio.TestTools.UnitTesting;
22
using System;
33
using Tensorflow;
4+
using Tensorflow.Device;
45
using Tensorflow.Eager;
56
using Buffer = System.Buffer;
67

78
namespace TensorFlowNET.UnitTest
89
{
910
public class CApiTest
1011
{
11-
protected TF_Code TF_OK = TF_Code.TF_OK;
12-
protected TF_DataType TF_FLOAT = TF_DataType.TF_FLOAT;
13-
protected TF_DataType TF_BOOL = TF_DataType.TF_BOOL;
12+
protected static readonly TF_Code TF_OK = TF_Code.TF_OK;
13+
protected static readonly TF_DataType TF_FLOAT = TF_DataType.TF_FLOAT;
14+
protected static readonly TF_DataType TF_BOOL = TF_DataType.TF_BOOL;
1415

1516
protected void EXPECT_TRUE(bool expected, string msg = "")
1617
=> Assert.IsTrue(expected, msg);
1718

18-
protected void EXPECT_EQ(object expected, object actual, string msg = "")
19+
protected static void EXPECT_EQ(object expected, object actual, string msg = "")
1920
=> Assert.AreEqual(expected, actual, msg);
2021

2122
protected void CHECK_EQ(object expected, object actual, string msg = "")
@@ -63,10 +64,10 @@ protected int TFE_TensorHandleNumDims(IntPtr h, SafeStatusHandle status)
6364
protected TF_Code TF_GetCode(Status s)
6465
=> s.Code;
6566

66-
protected TF_Code TF_GetCode(SafeStatusHandle s)
67+
protected static TF_Code TF_GetCode(SafeStatusHandle s)
6768
=> c_api.TF_GetCode(s);
6869

69-
protected string TF_Message(SafeStatusHandle s)
70+
protected static string TF_Message(SafeStatusHandle s)
7071
=> c_api.StringPiece(c_api.TF_Message(s));
7172

7273
protected SafeStatusHandle TF_NewStatus()
@@ -141,21 +142,18 @@ protected string TFE_TensorHandleDeviceName(IntPtr h, SafeStatusHandle status)
141142
protected string TFE_TensorHandleBackingDeviceName(IntPtr h, SafeStatusHandle status)
142143
=> c_api.StringPiece(c_api.TFE_TensorHandleBackingDeviceName(h, status));
143144

144-
protected IntPtr TFE_ContextListDevices(SafeContextHandle ctx, SafeStatusHandle status)
145+
protected SafeDeviceListHandle TFE_ContextListDevices(SafeContextHandle ctx, SafeStatusHandle status)
145146
=> c_api.TFE_ContextListDevices(ctx, status);
146147

147-
protected int TF_DeviceListCount(IntPtr list)
148+
protected int TF_DeviceListCount(SafeDeviceListHandle list)
148149
=> c_api.TF_DeviceListCount(list);
149150

150-
protected string TF_DeviceListType(IntPtr list, int index, SafeStatusHandle status)
151+
protected string TF_DeviceListType(SafeDeviceListHandle list, int index, SafeStatusHandle status)
151152
=> c_api.StringPiece(c_api.TF_DeviceListType(list, index, status));
152153

153-
protected string TF_DeviceListName(IntPtr list, int index, SafeStatusHandle status)
154+
protected string TF_DeviceListName(SafeDeviceListHandle list, int index, SafeStatusHandle status)
154155
=> c_api.StringPiece(c_api.TF_DeviceListName(list, index, status));
155156

156-
protected void TF_DeleteDeviceList(IntPtr list)
157-
=> c_api.TF_DeleteDeviceList(list);
158-
159157
protected IntPtr TFE_TensorHandleCopyToDevice(IntPtr h, SafeContextHandle ctx, string device_name, SafeStatusHandle status)
160158
=> c_api.TFE_TensorHandleCopyToDevice(h, ctx, device_name, status);
161159

‎test/TensorFlowNET.UnitTest/NativeAPI/Eager/CApi.Eager.Context.cs‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using Microsoft.VisualStudio.TestTools.UnitTesting;
2-
using System;
32
using Tensorflow;
3+
using Tensorflow.Device;
44
using Tensorflow.Eager;
55

66
namespace TensorFlowNET.UnitTest.NativeAPI
@@ -21,13 +21,15 @@ static SafeContextHandle NewContext(SafeStatusHandle status)
2121
return c_api.TFE_NewContext(opts, status);
2222
}
2323

24-
IntPtr devices;
25-
using (var ctx = NewContext(status))
24+
static SafeDeviceListHandle ListDevices(SafeStatusHandle status)
2625
{
27-
devices = c_api.TFE_ContextListDevices(ctx, status);
26+
using var ctx = NewContext(status);
27+
var devices = c_api.TFE_ContextListDevices(ctx, status);
2828
EXPECT_EQ(TF_OK, TF_GetCode(status), TF_Message(status));
29+
return devices;
2930
}
3031

32+
using var devices = ListDevices(status);
3133
EXPECT_EQ(TF_OK, TF_GetCode(status), TF_Message(status));
3234

3335
int num_devices = c_api.TF_DeviceListCount(devices);
@@ -37,8 +39,6 @@ static SafeContextHandle NewContext(SafeStatusHandle status)
3739
EXPECT_NE("", c_api.TF_DeviceListName(devices, i, status), TF_Message(status));
3840
EXPECT_EQ(TF_OK, TF_GetCode(status), TF_Message(status));
3941
}
40-
41-
c_api.TF_DeleteDeviceList(devices);
4242
}
4343
}
4444
}

‎test/TensorFlowNET.UnitTest/NativeAPI/Eager/CApi.Eager.cs‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ IntPtr MatMulOp(SafeContextHandle ctx, IntPtr a, IntPtr b)
4343

4444
bool GetDeviceName(SafeContextHandle ctx, ref string device_name, string device_type)
4545
{
46-
var status = TF_NewStatus();
47-
var devices = TFE_ContextListDevices(ctx, status);
46+
using var status = TF_NewStatus();
47+
using var devices = TFE_ContextListDevices(ctx, status);
4848
CHECK_EQ(TF_OK, TF_GetCode(status), TF_Message(status));
4949

5050
int num_devices = TF_DeviceListCount(devices);
@@ -57,12 +57,10 @@ bool GetDeviceName(SafeContextHandle ctx, ref string device_name, string device_
5757
if (dev_type == device_type)
5858
{
5959
device_name = dev_name;
60-
TF_DeleteDeviceList(devices);
6160
return true;
6261
}
6362
}
6463

65-
TF_DeleteDeviceList(devices);
6664
return false;
6765
}
6866

0 commit comments

Comments
 (0)