Skip to content

Commit 9dd68b3

Browse files
committed
dot
1 parent 85bb4b6 commit 9dd68b3

14 files changed

Lines changed: 100 additions & 113 deletions

src/NumSharp.Core/APIs/np.math.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
using NumSharp.Backends;
2+
using System;
23
using System.Collections.Generic;
34
using System.Text;
45

@@ -7,6 +8,6 @@ namespace NumSharp
78
public static partial class np
89
{
910
public static int add(NDArray x, NDArray y)
10-
=> x + y;
11+
=> BackendFactory.GetEngine().Add(x, y);
1112
}
1213
}
Lines changed: 5 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -1,114 +1,22 @@
1-
using NumSharp.Interfaces;
1+
using ArrayFire;
2+
using NumSharp.Interfaces;
23
using System;
34
using System.Collections.Generic;
45
using System.Text;
6+
using Array = ArrayFire.Array;
57

68
namespace NumSharp.Backends
79
{
810
public class ArrayFireEngine : ITensorEngine
911
{
10-
public Type DType => throw new NotImplementedException();
11-
12-
public int DTypeSize => throw new NotImplementedException();
13-
14-
public Shape Shape => throw new NotImplementedException();
15-
16-
public void Allocate(Type dtype, Shape shape)
17-
{
18-
throw new NotImplementedException();
19-
}
20-
21-
public void Allocate(Array values)
22-
{
23-
throw new NotImplementedException();
24-
}
25-
26-
public void ChangeDataType(Type dtype)
27-
{
28-
throw new NotImplementedException();
29-
}
30-
31-
public object Clone()
32-
{
33-
throw new NotImplementedException();
34-
}
35-
36-
public Array CloneData()
37-
{
38-
throw new NotImplementedException();
39-
}
40-
41-
public Array CloneData(Type dtype)
42-
{
43-
throw new NotImplementedException();
44-
}
45-
46-
public T[] CloneData<T>()
12+
public NDArray Add(NDArray x, NDArray y)
4713
{
4814
throw new NotImplementedException();
4915
}
5016

5117
public NDArray Dot(NDArray x, NDArray y)
5218
{
53-
var dtype = x.dtype;
54-
55-
switch (dtype.Name)
56-
{
57-
case "Int32":
58-
break;
59-
}
60-
61-
throw new NotImplementedException("SimdEngine.dot");
62-
}
63-
64-
public Array GetData()
65-
{
66-
throw new NotImplementedException();
67-
}
68-
69-
public Array GetData(Type dtype)
70-
{
71-
throw new NotImplementedException();
72-
}
73-
74-
public T[] GetData<T>()
75-
{
76-
throw new NotImplementedException();
77-
}
78-
79-
public object GetData(params int[] indexes)
80-
{
81-
throw new NotImplementedException();
82-
}
83-
84-
public T GetData<T>(params int[] indexes)
85-
{
86-
throw new NotImplementedException();
87-
}
88-
89-
public void Reshape(params int[] dimensions)
90-
{
91-
throw new NotImplementedException();
92-
}
93-
94-
public void SetData(Array values)
95-
{
96-
throw new NotImplementedException();
97-
}
98-
99-
public void SetData<T>(Array values)
100-
{
101-
throw new NotImplementedException();
102-
}
103-
104-
public void SetData(object value, params int[] indexes)
105-
{
106-
throw new NotImplementedException();
107-
}
108-
109-
public void SetData(Array values, Type dtype)
110-
{
111-
throw new NotImplementedException();
19+
return Vector.Dot(x, y);
11220
}
11321
}
11422
}

src/NumSharp.Core/Backends/DefaultEngine.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ namespace NumSharp.Backends
2222
/// </summary>
2323
public class DefaultEngine : ITensorEngine
2424
{
25+
public NDArray Add(NDArray x, NDArray y)
26+
{
27+
return x + y;
28+
}
29+
2530
public NDArray Dot(NDArray x, NDArray y)
2631
{
2732
var dtype = x.dtype;

src/NumSharp.Core/Backends/ITensorEngine.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@ namespace NumSharp.Backends
77
public interface ITensorEngine
88
{
99
NDArray Dot(NDArray x, NDArray y);
10+
NDArray Add(NDArray x, NDArray y);
1011
}
1112
}

src/NumSharp.Core/Backends/NDStorage.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ public void Allocate(Type dtype, Shape shape)
165165
{
166166
_DType = dtype;
167167
_Shape = shape;
168-
_Shape.ChangeTensorLayout(1);
168+
_Shape.ChangeTensorLayout();
169169
int elementNumber = 1;
170170
for(int idx = 0; idx < shape.Dimensions.Length;idx++)
171171
elementNumber *= shape.Dimensions[idx];

src/NumSharp.Core/Backends/SimdEngine.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,27 @@ namespace NumSharp.Backends
88
{
99
public class SimdEngine : ITensorEngine
1010
{
11+
public NDArray Add(NDArray x, NDArray y)
12+
{
13+
int[] lhs = x.Data<int>();
14+
int[] rhs = x.Data<int>();
15+
16+
var simdLength = Vector<int>.Count;
17+
var result = new int[lhs.Length];
18+
var i = 0;
19+
for (i = 0; i <= lhs.Length - simdLength; i += simdLength)
20+
{
21+
var va = new Vector<int>(lhs, i);
22+
var vb = new Vector<int>(rhs, i);
23+
(va + vb).CopyTo(result, i);
24+
}
25+
26+
for (; i < lhs.Length; ++i)
27+
result[i] = lhs[i] + rhs[i];
28+
29+
return result;
30+
}
31+
1132
public NDArray Dot(NDArray x, NDArray y)
1233
{
1334
var dtype = x.dtype;
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* NumSharp
3+
* Copyright (C) 2018 Haiping Chen
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the Apache License 2.0 as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the Apache License 2.0
16+
* along with this program. If not, see <http://www.apache.org/licenses/LICENSE-2.0/>.
17+
*/
18+
19+
using System;
20+
using System.Collections.Generic;
21+
using System.ComponentModel;
22+
using System.Linq;
23+
using System.Text;
24+
using System.Globalization;
25+
using System.Collections;
26+
using NumSharp;
27+
using System.Text.RegularExpressions;
28+
using Array = ArrayFire.Array;
29+
30+
namespace NumSharp
31+
{
32+
public partial class NDArray
33+
{
34+
public static implicit operator Array(NDArray nd)
35+
{
36+
switch (nd.ndim)
37+
{
38+
case 1:
39+
return ArrayFire.Data.CreateArray(nd.Data<int>());
40+
case 2:
41+
return ArrayFire.Data.CreateArray(nd.ToMuliDimArray<int>() as int[,]);
42+
}
43+
44+
throw new NotImplementedException("");
45+
}
46+
47+
public static implicit operator NDArray(Array array)
48+
{
49+
throw new NotImplementedException("");
50+
}
51+
}
52+
}

src/NumSharp.Core/Casting/NdArrayFromJaggedArr.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public void FromJaggedArray(Array dotNetArray)
5252
int[] dims = dimList.ToArray();
5353

5454
Shape shape = new Shape(dims);
55-
shape.ChangeTensorLayout(1);
55+
shape.ChangeTensorLayout();
5656

5757
NDArray nd = new NDArray(elementType,shape);
5858

src/NumSharp.Core/Casting/NdArrayFromMultiDimArr.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public void FromMultiDimArray(Array dotNetArray)
4545
Array internalStrg = Storage.GetData();
4646

4747
var pufferShape = new Shape(dims);
48-
pufferShape.ChangeTensorLayout(2);
48+
pufferShape.ChangeTensorLayout();
4949

5050
int[] idxDims = null;
5151
object valueIdx = null;

src/NumSharp.Core/Casting/NdArrayToMultiDimArray.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ public partial class NDArray
3131
{
3232
public Array ToMuliDimArray<T>()
3333
{
34-
Array dotNetArray = Array.CreateInstance(typeof(T),this.shape);
34+
Array dotNetArray = Array.CreateInstance(typeof(T), this.shape);
3535

3636
var pufferShape = new Shape(shape);
37-
pufferShape.ChangeTensorLayout(2);
37+
pufferShape.ChangeTensorLayout();
3838

3939
int[] indexes = null;
4040
object idxValue = null;

0 commit comments

Comments
 (0)