forked from SciSharp/TensorFlow.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathc_api.cs
More file actions
107 lines (92 loc) · 3.72 KB
/
c_api.cs
File metadata and controls
107 lines (92 loc) · 3.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*****************************************************************************
Copyright 2020 Haiping Chen. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
******************************************************************************/
using System;
using System.Runtime.InteropServices;
using static Tensorflow.CppShapeInferenceResult.Types;
namespace Tensorflow
{
/// <summary>
/// C API for TensorFlow.
/// Port from tensorflow\c\c_api.h
///
/// The API leans towards simplicity and uniformity instead of convenience
/// since most usage will be by language specific wrappers.
///
/// The params type mapping between c_api and .NET
/// TF_XX** => ref IntPtr (TF_Operation** op) => (ref IntPtr op)
/// TF_XX* => IntPtr (TF_Graph* graph) => (IntPtr graph)
/// struct => struct (TF_Output output) => (TF_Output output)
/// struct* => struct[] (TF_Output* output) => (TF_Output[] output)
/// struct* => struct* for ref
/// const char* => string
/// int32_t => int
/// int64_t* => long[]
/// size_t* => ulong[]
/// size_t* => ref ulong
/// void* => IntPtr
/// string => IntPtr c_api.StringPiece(IntPtr)
/// unsigned char => byte
/// </summary>
public partial class c_api
{
public const string TensorFlowLibName = "tensorflow";
public static string StringPiece(IntPtr handle)
{
return handle == IntPtr.Zero ? String.Empty : Marshal.PtrToStringAnsi(handle);
}
public unsafe static byte[] ByteStringPiece(Buffer? handle)
{
if (handle is null)
{
return new byte[0];
}
var data = handle.ToArray();
return data;
}
public unsafe static byte[] ByteStringPieceFromNativeString(IntPtr handle)
{
if (handle == IntPtr.Zero)
{
return new byte[0];
}
byte* str_data = (byte*)handle.ToPointer();
List<byte> bytes = new List<byte>();
byte current = 255;
while (current != ((byte)'\0'))
{
current = *(str_data++);
bytes.Add(current);
}
var data = bytes.ToArray();
return data;
}
[UnmanagedFunctionPointer(CallingConvention.Winapi)]
public delegate void Deallocator(IntPtr data, IntPtr size, ref DeallocatorArgs args);
[UnmanagedFunctionPointer(CallingConvention.Winapi)]
public delegate void DeallocatorV2(IntPtr data, long size, IntPtr args);
public struct DeallocatorArgs
{
internal static unsafe c_api.DeallocatorArgs* EmptyPtr;
internal static unsafe IntPtr Empty;
static unsafe DeallocatorArgs()
{
Empty = new IntPtr(EmptyPtr = (DeallocatorArgs*)Marshal.AllocHGlobal(Marshal.SizeOf<DeallocatorArgs>()));
*EmptyPtr = new DeallocatorArgs() { gc_handle = IntPtr.Zero, deallocator_called = false };
}
public bool deallocator_called;
public IntPtr gc_handle;
}
[DllImport(TensorFlowLibName)]
internal static extern IntPtr TF_Version();
}
}