summaryrefslogtreecommitdiff
path: root/tests/src/JIT/Performance/CodeQuality/Burgers/Burgers.cs
blob: 9880ee511fcc49e9a935b388316a7e1b75e01363 (plain)
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
//
// .NET SIMD to solve Burgers' equation
//
// Benchmark based on
// https://github.com/taumuon/SIMD-Vectorisation-Burgers-Equation-CSharp
// http://www.taumuon.co.uk/2014/10/net-simd-to-solve-burgers-equation.html

using Microsoft.Xunit.Performance;
using System;
using System.Linq;
using System.Numerics;
using System.Runtime.CompilerServices;

[assembly: OptimizeForBenchmarks]

public class Burgers
{
    private static double BurgersAnalytical(double t, double x, double nu)
    {
        return -2 * nu * (-(-8 * t + 2 * x) * Math.Exp(-Math.Pow((-4 * t + x), 2) / (4 * nu * (t + 1))) / (4 * nu * (t + 1)) - (-8 * t + 2 * x - 12.5663706143592) * Math.Exp(-Math.Pow(-4 * t + x - 6.28318530717959, 2) / (4 * nu * (t + 1))) / (4 * nu * (t + 1))) / (Math.Exp(-Math.Pow(-4 * t + x - 6.28318530717959, 2) / (4 * nu * (t + 1))) + Math.Exp(-Math.Pow(-4 * t + x, 2) / (4 * nu * (t + 1)))) + 4;
    }

    private static double[] linspace(double first, double last, int num)
    {
        var step = (last - first) / (double)num;
        return Enumerable.Range(0, num).Select(v => (v * step) + first).ToArray();
    }

    private static double[] GetAnalytical(double[] x, double t, double nu)
    {
        double[] u = new double[x.Length];

        for (int i = 0; i < x.Length; ++i)
        {
            u[i] = BurgersAnalytical(t, x[i], nu);
        }

        return u;
    }

    private static double[] GetCalculated0(int nt, int nx, double dx, double dt, double nu, double[] initial)
    {
        double[] u = new double[nx];
        Array.Copy(initial, u, u.Length);

        for (int tStep = 0; tStep < nt; tStep++)
        {
            double[] un = new double[nx];
            Array.Copy(u, un, u.Length);

            for (int i = 1; i < nx - 1; i++)
            {
                u[i] = un[i] - un[i] * dt / dx * (un[i] - un[i - 1]) + Math.Pow(nu * dt / dx, 2.0) *
                        (un[i + 1] - 2 * un[i] + un[i - 1]);
            }

            u[0] = un[0] - un[0] * dt / dx * (un[0] - un[nx - 1]) + Math.Pow(nu * dt / dx, 2.0) *
                        (un[1] - 2 * un[0] + un[nx - 1]);

            u[nx - 1] = un[nx - 1] - un[nx - 1] * dt / dx * (un[nx - 1] - un[nx - 2]) + Math.Pow(nu * dt / dx, 2.0) *
                        (un[0] - 2 * un[nx - 1] + un[nx - 2]);
        }

        return u;
    }

    // Reduce new array allocation and copying, ping-pong between them
    private static double[] GetCalculated1(int nt, int nx, double dx, double dt, double nu, double[] initial)
    {
        double[] u = new double[nx];
        double[] un = new double[nx];
        Array.Copy(initial, un, un.Length);

        for (int tStep = 0; tStep < nt; tStep++)
        {
            for (int i = 1; i < nx - 1; i++)
            {
                u[i] = un[i] - un[i] * dt / dx * (un[i] - un[i - 1]) + Math.Pow(nu * dt / dx, 2.0) *
                        (un[i + 1] - 2 * un[i] + un[i - 1]);
            }

            u[0] = un[0] - un[0] * dt / dx * (un[0] - un[nx - 1]) + Math.Pow(nu * dt / dx, 2.0) *
                        (un[1] - 2 * un[0] + un[nx - 1]);

            u[nx - 1] = un[nx - 1] - un[nx - 1] * dt / dx * (un[nx - 1] - un[nx - 2]) + Math.Pow(nu * dt / dx, 2.0) *
                        (un[0] - 2 * un[nx - 1] + un[nx - 2]);

            double[] swap = u;
            u = un;
            un = swap;
        }

        return un;
    }

    // Pull calculation of (nu * dt / dx)^2 out into a variable
    private static double[] GetCalculated2(int nt, int nx, double dx, double dt, double nu, double[] initial)
    {
        double[] u = new double[nx];
        double[] un = new double[nx];
        Array.Copy(initial, un, un.Length);

        double factor = Math.Pow(nu * dt / dx, 2.0);

        for (int tStep = 0; tStep < nt; tStep++)
        {
            for (int i = 1; i < nx - 1; i++)
            {
                u[i] = un[i] - un[i] * dt / dx * (un[i] - un[i - 1]) + factor *
                        (un[i + 1] - 2 * un[i] + un[i - 1]);
            }

            u[0] = un[0] - un[0] * dt / dx * (un[0] - un[nx - 1]) + factor *
                        (un[1] - 2 * un[0] + un[nx - 1]);

            u[nx - 1] = un[nx - 1] - un[nx - 1] * dt / dx * (un[nx - 1] - un[nx - 2]) + factor *
                        (un[0] - 2 * un[nx - 1] + un[nx - 2]);

            double[] swap = u;
            u = un;
            un = swap;
        }

        return un;
    }

    // SIMD
    private static double[] GetCalculated3(int nt, int nx, double dx, double dt, double nu, double[] initial)
    {
        var nx2 = nx + (Vector<double>.Count - (nx % Vector<double>.Count));

        double[] u = new double[nx2];
        double[] un = new double[nx2];
        Array.Copy(initial, un, initial.Length);

        double factor = Math.Pow(nu * dt / dx, 2.0);

        for (int tStep = 0; tStep < nt; tStep++)
        {
            for (int i = 1; i < nx2 - Vector<double>.Count + 1; i += Vector<double>.Count)
            {
                var vectorIn0 = new Vector<double>(un, i);
                var vectorInPrev = new Vector<double>(un, i - 1);
                var vectorInNext = new Vector<double>(un, i + 1);

                var vectorOut = vectorIn0 - vectorIn0 * (dt / dx) * (vectorIn0 - vectorInPrev) + factor *
                    (vectorInNext - 2.0 * vectorIn0 + vectorInPrev);

                vectorOut.CopyTo(u, i);
            }

            u[0] = un[0] - un[0] * dt / dx * (un[0] - un[nx - 1]) + factor *
                        (un[1] - 2 * un[0] + un[nx - 1]);

            u[nx - 1] = un[nx - 1] - un[nx - 1] * dt / dx * (un[nx - 1] - un[nx - 2]) + factor *
                        (un[0] - 2 * un[nx - 1] + un[nx - 2]);

            double[] swap = u;
            u = un;
            un = swap;
        }

        return un;
    }

    public static int Main()
    {
        if (!Vector.IsHardwareAccelerated)
        {
            Console.WriteLine("Not hardware accelerated!");
        }
        else
        {
            Console.WriteLine("Vector<double>.Length: " + Vector<double>.Count);
        }

        int nx = 10001;

#if DEBUG
        int nt = 10;
#else
        int nt = 10000;
#endif

        double dx = 2.0 * Math.PI / (nx - 1.0);
        double nu = 0.07;
        double dt = dx * nu;
        double[] x = linspace(0.0, 2.0 * Math.PI, nx);
        double[] initial = GetAnalytical(x, 0.0, nu);

        // Warmup

        GetCalculated0(1, nx, dx, dt, nu, initial);
        GetCalculated1(1, nx, dx, dt, nu, initial);
        GetCalculated2(1, nx, dx, dt, nu, initial);
        GetCalculated3(1, nx, dx, dt, nu, initial);

        double[][] results = new double[4][];

        var stopwatch = new System.Diagnostics.Stopwatch();

        stopwatch.Start();
        results[0] = GetCalculated0(nt, nx, dx, dt, nu, initial);
        stopwatch.Stop();
        Console.WriteLine("Baseline: " + stopwatch.ElapsedMilliseconds);
        stopwatch.Reset();

        stopwatch.Start();
        results[1] = GetCalculated1(nt, nx, dx, dt, nu, initial);
        stopwatch.Stop();
        Console.WriteLine("Reduce copy: " + stopwatch.ElapsedMilliseconds);
        stopwatch.Reset();

        stopwatch.Start();
        results[2] = GetCalculated2(nt, nx, dx, dt, nu, initial);
        stopwatch.Stop();
        Console.WriteLine("CSE of Math.Pow: " + stopwatch.ElapsedMilliseconds);
        stopwatch.Reset();

        stopwatch.Start();
        results[3] = GetCalculated3(nt, nx, dx, dt, nu, initial);
        stopwatch.Stop();
        Console.WriteLine("SIMD: " + stopwatch.ElapsedMilliseconds);
        stopwatch.Reset();

        for (int i = 0; i < x.Length; i += 33)
        {
            double expected = results[0][i];
            for (int j = 1; j < results.Length; j++)
            {
                bool valid = Math.Abs(expected - results[j][i]) < 1e-4;
                if (!valid)
                {
                    Console.WriteLine("Failed to validate");
                    return -1;
                }
            }
        }

        return 100;
    }

    static volatile object VolatileObject;

    [MethodImpl(MethodImplOptions.NoInlining)]
    static void Escape(object obj)
    {
        VolatileObject = obj;
    }

    [Benchmark]
    public static void Test0()
    {
        int nx = 10001;
        int nt = 10000;
        double dx = 2.0 * Math.PI / (nx - 1.0);
        double nu = 0.07;
        double dt = dx * nu;
        double[] x = linspace(0.0, 2.0 * Math.PI, nx);
        double[] initial = GetAnalytical(x, 0.0, nu);

        foreach (var iteration in Benchmark.Iterations)
        {
            using (iteration.StartMeasurement())
            {
                double[] results = GetCalculated0(nt, nx, dx, dt, nu, initial);
                Escape(results);
            }
        }
    }

    [Benchmark]
    public static void Test1()
    {
        int nx = 10001;
        int nt = 10000;
        double dx = 2.0 * Math.PI / (nx - 1.0);
        double nu = 0.07;
        double dt = dx * nu;
        double[] x = linspace(0.0, 2.0 * Math.PI, nx);
        double[] initial = GetAnalytical(x, 0.0, nu);

        foreach (var iteration in Benchmark.Iterations)
        {
            using (iteration.StartMeasurement())
            {
                double[] results = GetCalculated1(nt, nx, dx, dt, nu, initial);
                Escape(results);
            }
        }
    }

    [Benchmark]
    public static void Test2()
    {
        int nx = 10001;
        int nt = 10000;
        double dx = 2.0 * Math.PI / (nx - 1.0);
        double nu = 0.07;
        double dt = dx * nu;
        double[] x = linspace(0.0, 2.0 * Math.PI, nx);
        double[] initial = GetAnalytical(x, 0.0, nu);

        foreach (var iteration in Benchmark.Iterations)
        {
            using (iteration.StartMeasurement())
            {
                double[] results = GetCalculated2(nt, nx, dx, dt, nu, initial);
                Escape(results);
            }
        }
    }

    [Benchmark]
    public static void Test3()
    {
        // Make SIMD version work a bit harder....
        int nx = 10001;
        int nt = 2 * 10000;
        double dx = 2.0 * Math.PI / (nx - 1.0);
        double nu = 0.07;
        double dt = dx * nu;
        double[] x = linspace(0.0, 2.0 * Math.PI, nx);
        double[] initial = GetAnalytical(x, 0.0, nu);

        foreach (var iteration in Benchmark.Iterations)
        {
            using (iteration.StartMeasurement())
            {
                double[] results = GetCalculated3(nt, nx, dx, dt, nu, initial);
                Escape(results);
            }
        }
    }
}