forked from SciSharp/NumSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNDArray.Unmanaged.cs
More file actions
291 lines (253 loc) · 12.1 KB
/
NDArray.Unmanaged.cs
File metadata and controls
291 lines (253 loc) · 12.1 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
using System;
using System.Globalization;
using NumSharp.Backends;
using NumSharp.Backends.Unmanaged;
using CompilerUnsafe = System.Runtime.CompilerServices.Unsafe;
namespace NumSharp
{
public partial class NDArray
{
/// <summary>
/// Provides an interface for unsafe methods in NDArray.
/// </summary>
public unsafe _Unsafe Unsafe => new _Unsafe(this);
public readonly unsafe struct _Unsafe
{
private readonly NDArray _this;
internal _Unsafe(NDArray @this)
{
_this = @this;
}
/// <exception cref="InvalidOperationException">When this NDArray is a slice.</exception>
public ref byte GetPinnableReference()
{
if (_this.Shape.IsSliced || _this.Shape.IsBroadcasted)
throw new InvalidOperationException("Can't pin reference when NDArray is sliced or broadcasted.");
unsafe
{
return ref CompilerUnsafe.AsRef<byte>(Address);
}
}
/// <summary>
/// Returns the memory address to the start of this block of memory.
/// </summary>
/// <returns></returns>
/// <exception cref="InvalidOperationException">When this NDArray is a slice.</exception>
public void* Address
{
get
{
if (_this.Shape.IsSliced || _this.Shape.IsBroadcasted)
throw new InvalidOperationException("Can't return a memory address when NDArray is sliced or broadcasted.");
return _this.Address;
}
}
/// <summary>
/// Get: Gets internal storage array by calling <see cref="IStorage.GetData"/><br></br>
/// Set: Replace internal storage by calling <see cref="IStorage.ReplaceData(System.Array)"/>
/// </summary>
/// <remarks>Setting does not replace internal storage array.</remarks>
internal IArraySlice Array
{
get
{
if (_this.Shape.IsSliced || _this.Shape.IsBroadcasted)
throw new InvalidOperationException("Can't access a memory address when NDArray is sliced or broadcasted.");
return _this.Storage.InternalArray;
}
}
/// <summary>
/// Provides access to the internal <see cref="UnmanagedStorage"/>.
/// </summary>
public UnmanagedStorage Storage => _this.Storage;
/// A Span representing this slice.
/// <remarks>Does not perform copy.</remarks>
public Span<T> AsSpan<T>()
{
return Array.AsSpan<T>();
}
/// <summary>
/// The size of a single item stored in <see cref="Address"/>.
/// </summary>
/// <remarks>Equivalent to <see cref="NPTypeCode.SizeOf"/> extension.</remarks>
public int ItemLength => Array.ItemLength;
/// <summary>
/// How many bytes are stored in this memory block.
/// </summary>
/// <remarks>Calculated by <see cref="Count"/>*<see cref="ItemLength"/></remarks>
public int BytesLength => ((IConvertible)Array.BytesLength).ToInt32(CultureInfo.InvariantCulture);
/// <summary>
/// How many items are stored in <see cref="Address"/>.
/// </summary>
/// <remarks>Not to confuse with <see cref="BytesLength"/></remarks>
public int Count => (int) Array.Count;
/// <summary>
/// Fills all indexes with <paramref name="value"/>.
/// </summary>
/// <param name="value"></param>
public void Fill(object value)
{
Array.Fill(value);
}
public T GetIndex<T>(int index) where T : unmanaged
{
return Array.GetIndex<T>(index);
}
public object GetIndex(int index)
{
return Array.GetIndex(index);
}
/// <summary>
/// Gets pinnable reference of the first item in the memory block storage.
/// </summary>
public ref T GetPinnableReference<T>() where T : unmanaged
{
return ref Array.GetPinnableReference<T>();
}
#region Pinning
/// <summary>
/// Provides the ability to return a pin to the memory address of NDArray.
/// </summary>
/// <remarks>Possible only when the ndarray is not sliced.</remarks>
public readonly unsafe _Pinning Pin => new _Pinning(_this);
public readonly struct _Pinning
{
private readonly NDArray _this;
internal _Pinning(NDArray @this) => _this = @this;
/// <exception cref="InvalidOperationException">When this NDArray is a slice.</exception>
public unsafe ref T GetPin<T>()
{
if (_this.Shape.IsSliced || _this.Shape.IsBroadcasted) throw new InvalidOperationException("Can't pin reference when NDArray is sliced or broadcasted.");
return ref CompilerUnsafe.AsRef<T>(_this.Address);
}
#if _REGEN
#region Compute
%foreach supported_dtypes,supported_dtypes_lowercase%
/// <exception cref="InvalidOperationException">When this NDArray is a slice.</exception>
public unsafe ref #2 #1
{
get
{
if (_this.Shape.IsSliced || _this.Shape.IsBroadcasted) throw new InvalidOperationException("Can't pin reference when NDArray is sliced or broadcasted.");
return ref CompilerUnsafe.AsRef<#2>(_this.Address);
}
}
%
#endregion
#else
#region Compute
/// <exception cref="InvalidOperationException">When this NDArray is a slice.</exception>
public unsafe ref bool Boolean
{
get
{
if (_this.Shape.IsSliced || _this.Shape.IsBroadcasted) throw new InvalidOperationException("Can't pin reference when NDArray is sliced or broadcasted.");
return ref CompilerUnsafe.AsRef<bool>(_this.Address);
}
}
/// <exception cref="InvalidOperationException">When this NDArray is a slice.</exception>
public unsafe ref byte Byte
{
get
{
if (_this.Shape.IsSliced || _this.Shape.IsBroadcasted) throw new InvalidOperationException("Can't pin reference when NDArray is sliced or broadcasted.");
return ref CompilerUnsafe.AsRef<byte>(_this.Address);
}
}
/// <exception cref="InvalidOperationException">When this NDArray is a slice.</exception>
public unsafe ref short Int16
{
get
{
if (_this.Shape.IsSliced || _this.Shape.IsBroadcasted) throw new InvalidOperationException("Can't pin reference when NDArray is sliced or broadcasted.");
return ref CompilerUnsafe.AsRef<short>(_this.Address);
}
}
/// <exception cref="InvalidOperationException">When this NDArray is a slice.</exception>
public unsafe ref ushort UInt16
{
get
{
if (_this.Shape.IsSliced || _this.Shape.IsBroadcasted) throw new InvalidOperationException("Can't pin reference when NDArray is sliced or broadcasted.");
return ref CompilerUnsafe.AsRef<ushort>(_this.Address);
}
}
/// <exception cref="InvalidOperationException">When this NDArray is a slice.</exception>
public unsafe ref int Int32
{
get
{
if (_this.Shape.IsSliced || _this.Shape.IsBroadcasted) throw new InvalidOperationException("Can't pin reference when NDArray is sliced or broadcasted.");
return ref CompilerUnsafe.AsRef<int>(_this.Address);
}
}
/// <exception cref="InvalidOperationException">When this NDArray is a slice.</exception>
public unsafe ref uint UInt32
{
get
{
if (_this.Shape.IsSliced || _this.Shape.IsBroadcasted) throw new InvalidOperationException("Can't pin reference when NDArray is sliced or broadcasted.");
return ref CompilerUnsafe.AsRef<uint>(_this.Address);
}
}
/// <exception cref="InvalidOperationException">When this NDArray is a slice.</exception>
public unsafe ref long Int64
{
get
{
if (_this.Shape.IsSliced || _this.Shape.IsBroadcasted) throw new InvalidOperationException("Can't pin reference when NDArray is sliced or broadcasted.");
return ref CompilerUnsafe.AsRef<long>(_this.Address);
}
}
/// <exception cref="InvalidOperationException">When this NDArray is a slice.</exception>
public unsafe ref ulong UInt64
{
get
{
if (_this.Shape.IsSliced || _this.Shape.IsBroadcasted) throw new InvalidOperationException("Can't pin reference when NDArray is sliced or broadcasted.");
return ref CompilerUnsafe.AsRef<ulong>(_this.Address);
}
}
/// <exception cref="InvalidOperationException">When this NDArray is a slice.</exception>
public unsafe ref char Char
{
get
{
if (_this.Shape.IsSliced || _this.Shape.IsBroadcasted) throw new InvalidOperationException("Can't pin reference when NDArray is sliced or broadcasted.");
return ref CompilerUnsafe.AsRef<char>(_this.Address);
}
}
/// <exception cref="InvalidOperationException">When this NDArray is a slice.</exception>
public unsafe ref double Double
{
get
{
if (_this.Shape.IsSliced || _this.Shape.IsBroadcasted) throw new InvalidOperationException("Can't pin reference when NDArray is sliced or broadcasted.");
return ref CompilerUnsafe.AsRef<double>(_this.Address);
}
}
/// <exception cref="InvalidOperationException">When this NDArray is a slice.</exception>
public unsafe ref float Single
{
get
{
if (_this.Shape.IsSliced || _this.Shape.IsBroadcasted) throw new InvalidOperationException("Can't pin reference when NDArray is sliced or broadcasted.");
return ref CompilerUnsafe.AsRef<float>(_this.Address);
}
}
/// <exception cref="InvalidOperationException">When this NDArray is a slice.</exception>
public unsafe ref decimal Decimal
{
get
{
if (_this.Shape.IsSliced || _this.Shape.IsBroadcasted) throw new InvalidOperationException("Can't pin reference when NDArray is sliced or broadcasted.");
return ref CompilerUnsafe.AsRef<decimal>(_this.Address);
}
}
#endregion
#endif
}
#endregion
}
}
}