summaryrefslogtreecommitdiff
path: root/tests/src/JIT/Performance/CodeQuality/HWIntrinsic/X86/PacketTracer/Program.cs
blob: 3ae923a9b1cd57c61f4b1f0bf1098e05a4f0e837 (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
// 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.
//

using System;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using System.Collections.Concurrent;
using System.Runtime.Intrinsics.X86;
//using Microsoft.Xunit.Performance;

//[assembly: OptimizeForBenchmarks]

class Program
{
#if DEBUG

    private const int RunningTime = 200;
    private const int Width = 248;
    private const int Height = 248;
    private const int Iterations = 1;
    private const int MaxIterations = 1000;

#else

    private const int RunningTime = 1000;
    private const int Width = 248;
    private const int Height = 248;
    private const int Iterations = 7;
    private const int MaxIterations = 1000;

#endif

    private double _framesPerSecond;
    private bool _parallel;
    private bool _showThreads;
    private static int _width, _height;
    private int _degreeOfParallelism = Environment.ProcessorCount;
    private int _frames;
    private CancellationTokenSource _cancellation;
    private ObjectPool<int[]> _freeBuffers;

    public Program()
    {
        _width = Width;
        _height = Height;
        _parallel = false;
        _showThreads = false;
        _freeBuffers = new ObjectPool<int[]>(() => new int[_width * 3 * _height]); // Each pixel has 3 fields (RGB)
    }

    static unsafe int Main(string[] args)
    {
        if (Avx2.IsSupported)
        {
            var r = new Program();
            // We can use `RenderTo` to generate a picture in a PPM file for debugging
            // r.RenderTo("./pic.ppm", true);
            bool result = r.Run();
            return (result ? 100 : -1);
        }
        return 100;
    }

    private void RenderTest()
    {
        _cancellation = new CancellationTokenSource(RunningTime);
        RenderLoop(MaxIterations);
    }

    private void RenderBench()
    {
        _cancellation = new CancellationTokenSource();
        RenderLoop(Iterations);
    }

    private unsafe void RenderLoop(int iterations)
    {
        // Create a ray tracer, and create a reference to "sphere2" that we are going to bounce
        var packetTracer = new Packet256Tracer(_width, _height);
        var scene = packetTracer.DefaultScene;
        var sphere2 = (SpherePacket256)scene.Things[0]; // The first item is assumed to be our sphere
        var baseY = sphere2.Radiuses;
        sphere2.Centers.Ys = sphere2.Radiuses;

        // Timing determines how fast the ball bounces as well as diagnostics frames/second info
        var renderingTime = new Stopwatch();
        var totalTime = Stopwatch.StartNew();

        // Keep rendering until the iteration count is hit
        for (_frames = 0; _frames < iterations; _frames++)
        {
            // Or the rendering task has been canceled
            if (_cancellation.IsCancellationRequested)
            {
                break;
            }

            // Get the next buffer
            var rgbBuffer = _freeBuffers.GetObject();

            // Determine the new position of the sphere based on the current time elapsed
            float dy2 = 0.8f * MathF.Abs(MathF.Sin((float)(totalTime.ElapsedMilliseconds * Math.PI / 3000)));
            sphere2.Centers.Ys = Avx.Add(baseY, Avx.SetAllVector256(dy2));

            // Render the scene
            renderingTime.Reset();
            renderingTime.Start();
            ParallelOptions options = new ParallelOptions
            {
                MaxDegreeOfParallelism = _degreeOfParallelism,
                CancellationToken = _cancellation.Token
            };
            fixed (int* ptr = rgbBuffer)
            {
                packetTracer.RenderVectorized(scene, ptr);
            }

            renderingTime.Stop();

            _framesPerSecond = (1000.0 / renderingTime.ElapsedMilliseconds);
            _freeBuffers.PutObject(rgbBuffer);
        }
    }

    public bool Run()
    {
        RenderTest();
        Console.WriteLine("{0} frames, {1} frames/sec",
            _frames,
            _framesPerSecond.ToString("F2"));
        return true;
    }

    private unsafe void RenderTo(string fileName, bool wirteToFile)
    {
        var packetTracer = new Packet256Tracer(_width, _height);
        var scene = packetTracer.DefaultScene;
        var rgb = new int[_width * 3 * _height];
        Stopwatch stopWatch = new Stopwatch();
        stopWatch.Start();
        fixed (int* ptr = rgb)
        {
            packetTracer.RenderVectorized(scene, ptr);
        }
        stopWatch.Stop();
        TimeSpan ts = stopWatch.Elapsed;
        string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
           ts.Hours, ts.Minutes, ts.Seconds,
           ts.Milliseconds / 10);
        Console.WriteLine("RunTime " + elapsedTime);

        if (wirteToFile)
        {
            using (var file = new System.IO.StreamWriter(fileName))
            {
                file.WriteLine("P3");
                file.WriteLine(_width + " " + _height);
                file.WriteLine("255");

                for (int i = 0; i < _height; i++)
                {
                    for (int j = 0; j < _width; j++)
                    {
                        // Each pixel has 3 fields (RGB)
                        int pos = (i * _width + j) * 3;
                        file.Write(rgb[pos] + " " + rgb[pos + 1] + " " + rgb[pos + 2] + " ");
                    }
                    file.WriteLine();
                }
            }

        }
    }
}