Skip to content

Commit aa40578

Browse files
committed
Progress in making NDArray unmanaged.
1 parent 96f5d2b commit aa40578

24 files changed

Lines changed: 5233 additions & 3743 deletions

src/NumSharp.Core/Backends/IStorage.cs

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System;
44
using System.Numerics;
55
using System.Runtime.InteropServices;
6+
using NumSharp.Backends.Unmanaged;
67

78
namespace NumSharp
89
{
@@ -75,6 +76,24 @@ public interface IStorage : ICloneable
7576
/// <remarks>Does not copy <paramref name="values"/></remarks>
7677
void Allocate(Array values);
7778

79+
/// <summary>
80+
/// Assign this <see cref="ArraySlice{T}"/> as the internal array storage and assign <see cref="shape"/> to it.
81+
/// </summary>
82+
/// <param name="values">The array to set as internal data storage</param>
83+
/// <param name="shape">The shape of the array.</param>
84+
/// <param name="copy">Should perform a copy of <paramref name="values"/></param>
85+
/// <remarks>Does not copy <paramref name="values"/></remarks>
86+
void Allocate<T>(ArraySlice<T> values, Shape shape, bool copy = false) where T : unmanaged;
87+
88+
/// <summary>
89+
/// Allocate <paramref name="values"/> into memory.
90+
/// </summary>
91+
/// <param name="values">The array to set as internal data storage</param>
92+
/// <param name="shape">The shape of the array.</param>
93+
/// <param name="copy">Should perform a copy of <paramref name="values"/></param>
94+
/// <remarks>Does not copy <paramref name="values"/></remarks>
95+
void Allocate(IArraySlice values, Shape shape, bool copy = false);
96+
7897
/// <summary>
7998
/// Allocate <paramref name="values"/> into memory.
8099
/// </summary>
@@ -88,39 +107,39 @@ public interface IStorage : ICloneable
88107
/// </summary>
89108
/// <param name="values">The array to set as internal data storage</param>
90109
/// <remarks>Does not copy <paramref name="values"/></remarks>
91-
void Allocate<T>(T[] values);
110+
void Allocate<T>(T[] values) where T : unmanaged;
92111

93112
/// <summary>
94113
/// Get reference to internal data storage
95114
/// </summary>
96115
/// <returns>reference to internal storage as System.Array</returns>
97-
Array GetData();
116+
IArraySlice GetData();
98117

99118
/// <summary>
100119
/// Clone internal storage and returns reference to it
101120
/// </summary>
102121
/// <returns>reference to cloned storage as System.Array</returns>
103-
Array CloneData();
122+
IArraySlice CloneData();
104123

105124
/// <summary>
106125
/// Get reference to internal data storage and if necessary: copy and cast elements to new dtype of <typeparamref name="T"/>.
107126
/// </summary>
108127
/// <typeparam name="T">new storage data type</typeparam>
109128
/// <returns>reference to internal (casted) storage as T[]</returns>
110-
T[] GetData<T>();
129+
ArraySlice<T> GetData<T>() where T : unmanaged;
111130

112131
/// <summary>
113132
/// Attempts to cast internal storage to an array of type <typeparamref name="T"/> and returns the result, therefore results can be null.
114133
/// </summary>
115134
/// <typeparam name="T">The type that is expected.</typeparam>
116-
T[] AsArray<T>();
135+
ArraySlice<T> AsArray<T>() where T : unmanaged;
117136

118137
/// <summary>
119138
/// Get all elements from cloned storage as T[] and cast dtype
120139
/// </summary>
121140
/// <typeparam name="T">cloned storgae dtype</typeparam>
122141
/// <returns>reference to cloned storage as T[]</returns>
123-
T[] CloneData<T>();
142+
ArraySlice<T> CloneData<T>() where T : unmanaged;
124143

125144
/// <summary>
126145
/// Get single value from internal storage as type T and cast dtype to T
@@ -146,6 +165,13 @@ public interface IStorage : ICloneable
146165
/// <remarks>Does not copy values and doesn't change shape.</remarks>
147166
void ReplaceData(Array values);
148167

168+
/// <summary>
169+
/// Sets <see cref="values"/> as the internal data source and changes the internal storage data type to <see cref="values"/> type.
170+
/// </summary>
171+
/// <param name="values"></param>
172+
/// <remarks>Does not copy values and doesn't change shape.</remarks>
173+
void ReplaceData(IArraySlice values);
174+
149175
/// <summary>
150176
/// Set an Array to internal storage, cast it to new dtype and if necessary change dtype
151177
/// </summary>
@@ -175,9 +201,9 @@ public interface IStorage : ICloneable
175201
/// <param name="dimensions"></param>
176202
void Reshape(params int[] dimensions);
177203

178-
Span<T> View<T>(Slice slice = null);
204+
ArraySlice<T> View<T>(Slice slice = null) where T : unmanaged;
179205

180-
Span<T> GetSpanData<T>(Slice slice, params int[] indice);
206+
ArraySlice<T> GetSpanData<T>(Slice slice, params int[] indice) where T : unmanaged;
181207

182208
#region Get Methods
183209

src/NumSharp.Core/Backends/NDArray.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ protected NDArray(IStorage storage)
4646
TensorEngine = storage.Engine;
4747
}
4848

49-
5049
/// <summary>
5150
/// Constructor for init data type
5251
/// internal storage is 1D with 1 element
@@ -274,7 +273,7 @@ internal Array Array
274273
// NumPy Signature: ndarray.astype(dtype, order='K', casting='unsafe', subok=True, copy=True)
275274
public NDArray astype(Type dtype, bool copy = false)
276275
{
277-
return BackendFactory.GetEngine().Cast(this, dtype, copy);
276+
return TensorEngine.Cast(this, dtype, copy);
278277
}
279278

280279
/// <summary>

src/NumSharp.Core/Backends/StorageType.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,14 @@ namespace NumSharp
77
{
88
public enum StorageType
99
{
10+
/// <summary>
11+
/// <see cref="UnmanagedByteStorage{T}"/>
12+
/// </summary>
13+
Unmanaged = 0,
14+
1015
/// <summary>
1116
/// <see cref="TypedArrayStorage"/>
1217
/// </summary>
13-
TypedArray = 1
18+
TypedArray = 1,
1419
}
1520
}

0 commit comments

Comments
 (0)