Skip to content

Commit c7687bd

Browse files
....
1 parent 1482117 commit c7687bd

26 files changed

+883
-538
lines changed

Examples/SuperchargedArray.Testing/BasicExample.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,10 @@ public static void RunArraySimplified()
5252
{
5353
var K = Global.OP;
5454
//Create an array with values
55-
NDArray a = new float[,] { { 1, 2 }, { 3, 4 }, { 5, 6 } };
55+
SuperArray a = new float[,] { { 1, 2 }, { 3, 4 }, { 5, 6 } };
5656

5757
//Create a array with constant value 2
58-
NDArray b = new float[3, 2];
58+
SuperArray b = new float[3, 2];
5959
//NDArray b = new NDArray(3, 2);
6060
b.Fill(2);
6161

Examples/SuperchargedArray.Testing/PerfTest.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class PerfTest
1212
{
1313
public void Run()
1414
{
15-
int count = 10000000;
15+
int count = 30000000;
1616
Random rnd = new Random();
1717

1818
//Create variable A with random values
@@ -60,7 +60,7 @@ public void RunStandardLoop(int count, Array a, Array b)
6060
Console.WriteLine(".NET For Loop Time (in ms): " + sw.ElapsedMilliseconds);
6161
}
6262

63-
public void RunArrayDefault(int count, NDArray a, NDArray b, int cpu)
63+
public void RunArrayDefault(int count, SuperArray a, SuperArray b, int cpu)
6464
{
6565
Global.UseDefault(cpu);
6666
Stopwatch sw = new Stopwatch();
@@ -74,7 +74,7 @@ public void RunArrayDefault(int count, NDArray a, NDArray b, int cpu)
7474
Console.WriteLine("With Parallel Thread Time (in ms): {1}", cpu, sw.ElapsedMilliseconds);
7575
}
7676

77-
public void RunArrayAccelerated(int count, NDArray a, NDArray b, int deviceid)
77+
public void RunArrayAccelerated(int count, SuperArray a, SuperArray b, int deviceid)
7878
{
7979
try
8080
{
@@ -92,7 +92,7 @@ public void RunArrayAccelerated(int count, NDArray a, NDArray b, int deviceid)
9292
}
9393
catch(Exception ex)
9494
{
95-
//Console.WriteLine("Error: " + ex.Message);
95+
Console.WriteLine("Error: " + ex.Message);
9696
}
9797
}
9898
}

Examples/SuperchargedArray.Testing/SuperchargedArray.Testing.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>netcoreapp2.2</TargetFramework>
5+
<TargetFramework>netcoreapp2.1</TargetFramework>
66
<Platforms>AnyCPU;x64</Platforms>
77
</PropertyGroup>
88

README.md

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# System.ArrayExtension
1+
# SuperchargedArray
22

33
The .NET default built-in System.Array is very limited in terms of processing and usability. Here is the extended version of the Array with accelerated speed to execute operations on almost any hardware supporting OpenCL like Intel CPU, NVIDIA, AMD, Intel GPU, FPGA etc.
44

@@ -48,15 +48,15 @@ for (int i = 0; i < r.GetLength(0); i++)
4848
}
4949
```
5050

51-
A super simplified version with System.ArrayExtension
51+
A super simplified version with SuperchargedArray
5252
```csharp
5353
var K = Global.OP;
5454
//Create an array with values
55-
NDArray a = new float[,] { { 1, 2 }, { 3, 4 }, { 5, 6 } };
55+
SuperArray a = new float[,] { { 1, 2 }, { 3, 4 }, { 5, 6 } };
5656

5757
//Create a array with constant value 2
58-
NDArray b = new float[3, 2];
59-
//NDArray b = new NDArray(3, 2);
58+
SuperArray b = new float[3, 2];
59+
//SuperArray b = new SuperArray(3, 2);
6060
b.Fill(2);
6161

6262
//Perform Math operation on the array: 2A + Log(B) + Exp(A)
@@ -68,7 +68,7 @@ r.Print();
6868

6969

7070
## Accelerated Version
71-
System.ArrayExtension is not all about simplicity, it is well defined to execute the code on special hardwares like Intel GPU, NVIDIA, AMD cards. Below is the simple performance test done to process 100 million executions. And see the result:
71+
SuperchargedArray is not all about simplicity, it is well defined to execute the code on special hardwares like Intel GPU, NVIDIA, AMD cards. Below is the simple performance test done to process 100 million executions. And see the result:
7272

7373
```csharp
7474
public void Run()
@@ -121,29 +121,29 @@ public void RunStandardLoop(int count, Array a, Array b)
121121
Console.WriteLine(".NET For Loop Time (in ms): " + sw.ElapsedMilliseconds);
122122
}
123123

124-
public void RunArrayDefault(int count, NDArray a, NDArray b, int cpu)
124+
public void RunArrayDefault(int count, SuperArray a, SuperArray b, int cpu)
125125
{
126-
System.ArrayExtension.Accelerated.Global.UseDefault(cpu);
126+
SuperchargedArray.Accelerated.Global.UseDefault(cpu);
127127
Stopwatch sw = new Stopwatch();
128128
sw.Start();
129129

130-
var K = System.ArrayExtension.Global.OP;
130+
var K = SuperchargedArray.Global.OP;
131131

132132
var r = K.Trunc(a * K.Sin(b) + K.Cos(a) * K.Exp(b));
133133
sw.Stop();
134134

135135
Console.WriteLine("With Parallel Thread Time (in ms): {1}", cpu, sw.ElapsedMilliseconds);
136136
}
137137

138-
public void RunArrayAccelerated(int count, NDArray a, NDArray b, int deviceid)
138+
public void RunArrayAccelerated(int count, SuperArray a, SuperArray b, int deviceid)
139139
{
140140
try
141141
{
142142
Accelerator.UseDevice(deviceid);
143143
Stopwatch sw = new Stopwatch();
144144
sw.Start();
145145

146-
var K = System.ArrayExtension.Accelerated.Global.OP;
146+
var K = SuperchargedArray.Accelerated.Global.OP;
147147

148148
var r = K.Trunc(a * K.Sin(b) + K.Cos(a) * K.Exp(b));
149149
sw.Stop();
@@ -160,19 +160,21 @@ public void RunArrayAccelerated(int count, NDArray a, NDArray b, int deviceid)
160160

161161
### Execution Result:
162162

163+
Test result to execute math ops y = trunc(a * Sin(b) + cos(a) * exp(b)) for 100 million elements
164+
163165
.NET For Loop Time (in ms): 22317
164166

165-
Selected device: GeForce GTX 1080 Ti, With Accelerator (in ms): 8741
167+
GeForce GTX 1080 Ti : 8741 ms
166168

167-
Selected device: Intel(R) UHD Graphics 630, With Accelerator (in ms): 6723
169+
Intel(R) UHD Graphics 630 : 6723 ms
168170

169-
Selected device: Intel(R) Core(TM) i7-8700 CPU @ 3.20GHz, With Accelerator (in ms): 7380
171+
Intel(R) Core(TM) i7-8700 CPU @ 3.20GHz: 7380 ms
170172

171-
Selecting 33% of CPU processing, With Parallel Thread Time (in ms): 14332
173+
33% of CPU parallel processing : 14332 ms
172174

173-
Selecting 66% of CPU processing, With Parallel Thread Time (in ms): 7716
175+
66% of CPU parallel processing : 7716 ms
174176

175-
Selecting 100% of CPU processing, With Parallel Thread Time (in ms): 6420
177+
100% of CPU parallel processing: : 6420 ms
176178

177179
The .NET standard loop takes about 22 seconds, whereas with 100% CPU parallel approach using ArrayExtension it finishes off in 6 sec which is one-fourth of time taken. But for long running process using full CPU is not ideal. With ArrayExtension.Accelerated, which internally uses SIMD (Single Instruction Multiple Data), you can achive greater speed without using 100% of the hardware. But you can always fine-tune to use 100% of the hardware and achieve ultimate speed.
178180

SuperchargedArray.sln

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
3-
# Visual Studio Version 16
4-
VisualStudioVersion = 16.0.28922.388
3+
# Visual Studio 15
4+
VisualStudioVersion = 15.0.28307.489
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "examples", "examples", "{D6D26AE3-3049-476D-8014-2256876294B8}"
77
EndProject
88
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SuperchargedArray", "SuperchargedArray\SuperchargedArray.csproj", "{450BC48F-70F8-4E72-A837-CC4BBC85ED11}"
99
EndProject
1010
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SuperchargedArray.Testing", "Examples\SuperchargedArray.Testing\SuperchargedArray.Testing.csproj", "{B87AA65D-77BF-47A8-B3F9-AC4ED142B89B}"
1111
EndProject
12+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "__", "__", "{C707FDE0-1468-4C6B-BB05-97F7243FA3DA}"
13+
ProjectSection(SolutionItems) = preProject
14+
.gitignore = .gitignore
15+
LICENSE = LICENSE
16+
README.md = README.md
17+
EndProjectSection
18+
EndProject
1219
Global
1320
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1421
Debug|Any CPU = Debug|Any CPU

SuperchargedArray/Accelerated/Accelerator.cs

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,35 @@
1-
using SuperchargedArray.Accelerated.OpenCL;
2-
using SuperchargedArray.Accelerated.OpenCL.Bindings;
3-
using System;
4-
using System.Collections.Generic;
5-
using System.Diagnostics;
6-
using System.IO;
7-
using System.Linq;
8-
1+
/*
2+
MIT License
3+
4+
Copyright (c) 2019 Tech Quantum
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in all
14+
copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
SOFTWARE.
23+
24+
*/
925
namespace SuperchargedArray.Accelerated
1026
{
27+
using SuperchargedArray.Accelerated.OpenCL;
28+
using System;
29+
using System.Collections.Generic;
30+
using System.IO;
31+
using System.Linq;
32+
1133
/// <summary>
1234
/// Accelerator is a high level API for building and executing built-in or your own kernels.
1335
/// </summary>
@@ -165,12 +187,12 @@ public static void LoadKernals(string folderWithSourceCodes, string filter = "*.
165187
/// <param name="inputs">The inputs.</param>
166188
/// <param name="outputShape">The output shape.</param>
167189
/// <returns></returns>
168-
public static NDArray Execute(string functionName, object[] inputs, long[] outputShape = null, int? returnResult = null)
190+
public static SuperArray Execute(string functionName, object[] inputs, long[] outputShape = null, int? returnResult = null)
169191
{
170192
DType dType = DType.Single;
171193
if (outputShape == null)
172194
{
173-
var ndobject = (NDArray)inputs.FirstOrDefault(x => (x.GetType() == typeof(NDArray)));
195+
var ndobject = (SuperArray)inputs.FirstOrDefault(x => (x.GetType() == typeof(SuperArray)));
174196
if (ndobject == null)
175197
{
176198
outputShape = new long[] { 1 };
@@ -189,16 +211,16 @@ public static NDArray Execute(string functionName, object[] inputs, long[] outpu
189211
else if (dType == DType.Double)
190212
resultArray = ExecuteInternalArray<double>(functionName, inputs, dType, returnResult);
191213

192-
NDArray result = null;
214+
SuperArray result = null;
193215

194216
if (!returnResult.HasValue)
195217
{
196-
result = new NDArray(outputShape, dType);
218+
result = new SuperArray(outputShape, dType);
197219
result.LoadFrom(resultArray);
198220
}
199221
else
200222
{
201-
result = (NDArray)inputs[returnResult.Value];
223+
result = (SuperArray)inputs[returnResult.Value];
202224
result.LoadFrom(resultArray);
203225
}
204226

@@ -269,7 +291,7 @@ private static Array ExecuteInternalArray<TSource>(string functionName, object[]
269291

270292
try
271293
{
272-
var ndobject = (NDArray)inputs.FirstOrDefault(x => (x.GetType() == typeof(NDArray)));
294+
var ndobject = (SuperArray)inputs.FirstOrDefault(x => (x.GetType() == typeof(SuperArray)));
273295

274296
long length = ndobject != null ? ndobject.Elements : 1;
275297
Array resultArray = null;
@@ -304,14 +326,14 @@ private static ComputeBuffer<TSource> BuildKernelArguments<TSource>(object[] inp
304326

305327
foreach (var item in inputs)
306328
{
307-
if (item.GetType() == typeof(NDArray))
329+
if (item.GetType() == typeof(SuperArray))
308330
if (returnResult.HasValue && returnResult.Value == i)
309331
{
310-
result = new ComputeBuffer<TSource>(_context, ComputeMemoryFlags.ReadWrite | ComputeMemoryFlags.CopyHostPointer, ((NDArray)item).Data<TSource>());
332+
result = new ComputeBuffer<TSource>(_context, ComputeMemoryFlags.ReadWrite | ComputeMemoryFlags.CopyHostPointer, ((SuperArray)item).Data<TSource>());
311333
kernel.SetMemoryArgument(i, result);
312334
}
313335
else
314-
kernel.SetMemoryArgument(i, new ComputeBuffer<TSource>(_context, ComputeMemoryFlags.ReadWrite | ComputeMemoryFlags.CopyHostPointer, ((NDArray)item).Data<TSource>()));
336+
kernel.SetMemoryArgument(i, new ComputeBuffer<TSource>(_context, ComputeMemoryFlags.ReadWrite | ComputeMemoryFlags.CopyHostPointer, ((SuperArray)item).Data<TSource>()));
315337
else if (item.GetType().IsPrimitive)
316338
kernel.SetValueArgument(i, (float)item);
317339

0 commit comments

Comments
 (0)