-
Notifications
You must be signed in to change notification settings - Fork 33
/
ParallelAlgorithm.cs
83 lines (71 loc) · 3.11 KB
/
ParallelAlgorithm.cs
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
using Hast.Layer;
using Hast.Synthesis.Attributes;
using Hast.Transformer.SimpleMemory;
using System.Threading.Tasks;
namespace Hast.Samples.SampleAssembly;
/// <summary>
/// An embarrassingly parallel algorithm that is well suited to be accelerated with Hastlayer. Also see
/// <c>ParallelAlgorithmSampleRunner</c> on what to configure to make this work.
/// </summary>
public class ParallelAlgorithm
{
private const int RunInputInt32Index = 0;
private const int RunOutputInt32Index = 0;
// While 270 will also fit with ~77% of the resources being used that's very slow to compile in the Xilinx toolchain
// for the Nexys A7. The [Replaceable] enables the substitution of this static readonly field into constant literals
// wherever it is used. Check out the xmldoc of ReplaceableAttribute for further instructions.
// Warning: the outcome of the algorithm depends on this value so if you are running a prepared binary using the
// HardwareGenerationConfiguration.SingleBinaryPath make sure you are running with the same value it was built with.
// Otherwise you'll get mismatches.
[Replaceable(nameof(ParallelAlgorithm) + "." + nameof(MaxDegreeOfParallelism))]
private static readonly int MaxDegreeOfParallelism = 260;
public virtual void Run(SimpleMemory memory)
{
var input = memory.ReadInt32(RunInputInt32Index);
var tasks = new Task<int>[MaxDegreeOfParallelism];
// Hastlayer will figure out how many Tasks you want to start if you kick them off in a loop like this. If this
// is more involved then you'll need to tell Hastlayer the level of parallelism, see the comment in
// ParallelAlgorithmSampleRunner.
for (int i = 0; i < MaxDegreeOfParallelism; i++)
{
tasks[i] = Task.Factory.StartNew(
indexObject =>
{
var index = (int)indexObject;
int result = input + (index * 2);
var even = true;
for (int j = 2; j < 9_999_999; j++)
{
if (even)
{
result += index;
}
else
{
result -= index;
}
even = !even;
}
return result;
},
i);
}
// Task.WhenAny() can be used too.
Task.WhenAll(tasks).Wait();
int output = 0;
for (int i = 0; i < MaxDegreeOfParallelism; i++)
{
output += tasks[i].Result;
}
memory.WriteInt32(RunOutputInt32Index, output);
}
public int Run(int input, IHastlayer hastlayer = null, IHardwareGenerationConfiguration configuration = null)
{
var memory = hastlayer is null
? SimpleMemory.CreateSoftwareMemory(1)
: hastlayer.CreateMemory(configuration, 1);
memory.WriteInt32(RunInputInt32Index, input);
Run(memory);
return memory.ReadInt32(RunOutputInt32Index);
}
}