-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
348 lines (268 loc) · 9.88 KB
/
Program.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
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication5
{
class Program
{
public const int Repetitions = 10;
public static long ArraySize = 10000000;
public static int[] A;
public static int[] B;
public static int[] C;
static List<Tuple<string, Action>> Methods = new List<Tuple<string, Action>>();
static void Main(string[] args)
{
string aFileName = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().FullName), "eval.csv");
File.Delete(aFileName);
Methods.Add(new Tuple<string, Action>("Plain Add 2", PlainAdd2));
Methods.Add(new Tuple<string, Action>("Plain Add", PlainAdd));
Methods.Add(new Tuple<string, Action>("Plain Add with const length" , PlainAddWidthConstLength));
Methods.Add(new Tuple<string, Action>("Plain Add unsafe", PlainAddUnsafe));
Methods.Add(new Tuple<string, Action>("Plain Add unsafe const Length", PlainAddUnsafeConstLength));
Methods.Add(new Tuple<string, Action>("Plain Add unsafe const Length (Index)", PlainAddUnsafeConstLengthIndexed));
Methods.Add(new Tuple<string, Action>("Plain Add unsafe const Length with block", PlainAddUnsafeConstLengthWithBlock));
Methods.Add(new Tuple<string, Action>("Plain Add unsafe const Length with block index", PlainAddUnsafeConstLengthWithBlockIndexed));
// Methods.Add(new Tuple<string, Action>("Plain Add Par For", PlainParallelFor));
Methods.Add(new Tuple<string, Action>("Plain Add ParFor (Partitioner)", PlainParallelForPartitioner));
Methods.Add(new Tuple<string, Action>("Plain Add ParFor (Partitioner, Unsafe)", PlainParallelForPartitionerUnsafe));
// Methods.Add(new Tuple<string, Action>("System.Numerics.Vector<>", Add_Vector));
List<long> ArraySizes = new List<long>();
for (int i = 10; i < 10000000; i+=1000000)
{
ArraySizes.Add(i);
}
File.AppendAllLines(aFileName, new string[] { ";" + string.Join(";", ArraySizes) });
foreach (var aMethod in Methods)
{
List<string> aValues = new List<string>();
foreach(var arsz in ArraySizes)
{
ArraySize = arsz;
A = new int[ArraySize];
B = new int[ArraySize];
C = new int[ArraySize];
for (int i = 0; i < A.Length; i++)
{
A[i] = i;
B[i] = i;
C[i] = i;
}
long aElapsed = Run(aMethod.Item1, aMethod.Item2);
aValues.Add(aElapsed.ToString());
}
File.AppendAllLines(aFileName, new string[] {aMethod.Item1+ ";" + string.Join(";", aValues) });
}
Console.ReadKey();
}
public static void PlainAdd2()
{
for (int i = 0; i < A.Length; i++)
{
C[i] = Math.Add<int>(A[i], B[i]);
}
}
public static void PlainAdd()
{
for (int i = 0; i < A.Length; i++)
{
C[i] = A[i] + B[i];
}
}
public static void PlainAddWidthConstLength()
{
int aLength = A.Length;
for (int i = 0; i < aLength; i++)
{
C[i] = A[i] + B[i];
}
}
public static void PlainAddUnsafe()
{
unsafe
{
fixed (int* apinned = A, bpinned = B, cpinned = C)
{
int* a = apinned;
int* b = bpinned;
int* c = cpinned;
for (int i = 0; i < A.Length; i++, a++, b++, c++)
{
*c = *a + *b;
}
}
}
}
public static void PlainAddUnsafeConstLength()
{
unsafe
{
int aLength = A.Length;
fixed (int* apinned = A, bpinned = B, cpinned = C)
{
int* a = apinned;
int* b = bpinned;
int* c = cpinned;
for (int i = 0; i < aLength; i++, a++, b++, c++)
{
*c = *a + *b;
}
}
}
}
public static void PlainAddUnsafeConstLengthIndexed()
{
unsafe
{
int aLength = A.Length;
fixed (int* apinned = A, bpinned = B, cpinned = C)
{
int* a = apinned;
int* b = bpinned;
int* c = cpinned;
for (int i = 0; i < aLength; i++)
{
c [i]= a[i] + b[i];
}
}
}
}
public static void PlainAddUnsafeConstLengthWithBlock()
{
unsafe
{
int aLength = A.Length;
int nBlock = aLength / 10;
int nRest = aLength % 10;
fixed (int *apinned = A, bpinned=B, cpinned = C)
{
int* a = apinned;
int* b = bpinned;
int* c = cpinned;
for (int i = 0; i < nBlock; i++, a+=10, b+=10, c+=10)
{
*(c + 0) = *(a + 0) + *(b + 0);
*(c + 1) = *(a + 1) + *(b + 1);
*(c + 2) = *(a + 2) + *(b + 2);
*(c + 3) = *(a + 3) + *(b + 3);
*(c + 4) = *(a + 4) + *(b + 4);
*(c + 5) = *(a + 5) + *(b + 5);
*(c + 6) = *(a + 6) + *(b + 6);
*(c + 7) = *(a + 7) + *(b + 7);
*(c + 8) = *(a + 8) + *(b + 8);
*(c + 9) = *(a + 9) + *(b + 9);
}
for (int i = 0; i < nRest; i++, a++, b++, c++)
{
*c = *a + *b;
}
}
}
}
public static void PlainAddUnsafeConstLengthWithBlockIndexed()
{
unsafe
{
int aLength = A.Length;
int nBlock = aLength / 10;
int nRest = aLength % 10;
fixed (int* apinned = A, bpinned = B, cpinned = C)
{
int* a = apinned;
int* b = bpinned;
int* c = cpinned;
for (int i = 0; i < nBlock; i++, a += 10, b += 10, c += 10)
{
c[0] = a[0] + b[0];
c[1] = a[1] + b[1];
c[2] = a[2] + b[2];
c[3] = a[3] + b[3];
c[4] = a[4] + b[4];
c[5] = a[5] + b[5];
c[6] = a[6] + b[6];
c[7] = a[7] + b[7];
c[8] = a[8] + b[8];
c[9] = a[9] + b[9];
}
for (int i = 0; i < nRest; i++, a++, b++, c++)
{
*c = *a + *b;
}
}
}
}
public static void PlainParallelFor()
{
unsafe
{
int aLength = A.Length;
int nBlock = aLength / 10;
int nRest = aLength % 10;
Parallel.For(0, A.Length, i =>
{
C[i] = A[i] + B[i];
});
}
}
public static void PlainParallelForPartitioner()
{
var aPart = Partitioner.Create(0, A.Length);
Parallel.ForEach(aPart, x =>
{
for (int i = x.Item1; i < x.Item2; i++)
{
C[i] = A[i] + B[i];
}
});
}
public static void PlainParallelForPartitionerUnsafe()
{
var aPart = Partitioner.Create(0, A.Length);
Parallel.ForEach(aPart, x =>
{
unsafe
{
int aLength = A.Length;
fixed (int* apinned = A, bpinned = B, cpinned = C)
{
for (int i = x.Item1; i < x.Item2; i++)
{
cpinned[i] = apinned[i] + bpinned[i];
}
}
}
});
}
static long Run(string type_in, Action action)
{
Console.Write("{0:30} ({1}): ", type_in, ArraySize);
Stopwatch w = Stopwatch.StartNew();
for (int i = 0; i < Repetitions; i++)
{
Array.Clear(C, 0, C.Length);
action();
}
w.Stop();
long aElapsedMilliseconds = w.ElapsedMilliseconds / Repetitions;
Console.WriteLine("{0}ms", aElapsedMilliseconds);
for (int i = 0; i < A.Length; i++)
{
int c = A[i] + B[i];
if (c != C[i])
{
Console.WriteLine(" Error ");
break;
}
}
return aElapsedMilliseconds;
}
}
}