summaryrefslogtreecommitdiff
path: root/tests/src/JIT/Performance/CodeQuality/Bytemark/numericsort.cs
blob: 44da6e3a716369b12c2e3174869e7b564f057fb7 (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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
// 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.
/*
** Copyright (c) Microsoft. All rights reserved.
** Licensed under the MIT license. 
** See LICENSE file in the project root for full license information.
** 
** This program was translated to C# and adapted for xunit-performance.
** New variants of several tests were added to compare class versus 
** struct and to compare jagged arrays vs multi-dimensional arrays.
*/

/*
** BYTEmark (tm)
** BYTE Magazine's Native Mode benchmarks
** Rick Grehan, BYTE Magazine
**
** Create:
** Revision: 3/95
**
** DISCLAIMER
** The source, executable, and documentation files that comprise
** the BYTEmark benchmarks are made available on an "as is" basis.
** This means that we at BYTE Magazine have made every reasonable
** effort to verify that the there are no errors in the source and
** executable code.  We cannot, however, guarantee that the programs
** are error-free.  Consequently, McGraw-HIll and BYTE Magazine make
** no claims in regard to the fitness of the source code, executable
** code, and documentation of the BYTEmark.
** 
** Furthermore, BYTE Magazine, McGraw-Hill, and all employees
** of McGraw-Hill cannot be held responsible for any damages resulting
** from the use of this code or the results obtained from using
** this code.
*/

/**************
** DoNumSort **
***************
** This routine performs the CPU numeric sort test.
** NOTE: Last version incorrectly stated that the routine
**  returned result in # of longword sorted per second.
**  Not so; the routine returns # of iterations per sec.
*/

using System;

// #define DEBUG

public class NumericSortJagged : SortStruct
{
    public override string Name()
    {
        return "NUMERIC SORT(jagged)";
    }

    public override double Run()
    {
        /*
        ** Set the error context string.
        */
        int[][] arraybase;     /* Base pointers of array */
        long accumtime;         /* Accumulated time */
        double iterations;      /* Iteration counter */

        /*
        ** See if we need to do self adjustment code.
        */
        if (this.adjust == 0)
        {
            /*
            ** Self-adjustment code.  The system begins by sorting 1
            ** array.  If it does that in no time, then two arrays
            ** are built and sorted.  This process continues until
            ** enough arrays are built to handle the tolerance.
            */
            this.numarrays = 1;
            while (true)
            {
                /*
                ** Allocate space for arrays
                */
                arraybase = new int[this.numarrays][];
                for (int i = 0; i < this.numarrays; i++)
                    arraybase[i] = new int[this.arraysize];

                /*
                ** Do an iteration of the numeric sort.  If the
                ** elapsed time is less than or equal to the permitted
                ** minimum, then allocate for more arrays and
                ** try again.
                */

                if (DoNumSortIteration(arraybase,
                    this.arraysize,
                    this.numarrays) > global.min_ticks)
                    break;          /* We're ok...exit */
                if (this.numarrays++ > global.NUMNUMARRAYS)
                {
                    throw new Exception("CPU:NSORT -- NUMNUMARRAYS hit.");
                }
            }
        }
        else
        {
            /*
            ** Allocate space for arrays
            */
            arraybase = new int[this.numarrays][];
            for (int i = 0; i < this.numarrays; i++)
                arraybase[i] = new int[this.arraysize];
        }

        /*
        ** All's well if we get here.  Repeatedly perform sorts until the
        ** accumulated elapsed time is greater than # of seconds requested.
        */
        accumtime = 0L;
        iterations = (double)0.0;

        do
        {
            accumtime += DoNumSortIteration(arraybase,
                this.arraysize,
                this.numarrays);
            iterations += (double)1.0;
        } while (ByteMark.TicksToSecs(accumtime) < this.request_secs);

        if (this.adjust == 0)
            this.adjust = 1;

        return (iterations * (double)this.numarrays / ByteMark.TicksToFracSecs(accumtime));
    }

    /***********************
    ** DoNumSortIteration **
    ************************
    ** This routine executes one iteration of the numeric
    ** sort benchmark.  It returns the number of ticks
    ** elapsed for the iteration.
    */

    // JTR: The last 2 parms are no longer needed as they
    // can be inferred from the arraybase. <shrug>
    private static int DoNumSortIteration(int[][] arraybase, int arraysize, int numarrays)
    {
        long elapsed;          /* Elapsed ticks */
        int i;
        /*
        ** Load up the array with random numbers
        */
        LoadNumArrayWithRand(arraybase, arraysize, numarrays);

        /*
        ** Start the stopwatch
        */
        elapsed = ByteMark.StartStopwatch();

        /*
        ** Execute a heap of heapsorts
        */
        for (i = 0; i < numarrays; i++)
        {
            //          NumHeapSort(arraybase+i*arraysize,0L,arraysize-1L);
            NumHeapSort(arraybase[i], 0, arraysize - 1);
        }

        /*
        ** Get elapsed time
        */
        elapsed = ByteMark.StopStopwatch(elapsed);
#if DEBUG
        {
            for (i = 0; i < arraysize - 1; i++)
            {   /*
                ** Compare to check for proper
                ** sort.
                */
                if (arraybase[0][i + 1] < arraybase[0][i])
                {
                    Console.Write("Sort Error\n");
                    break;
                }
            }
        }
#endif

        return ((int)elapsed);
    }

    /*************************
    ** LoadNumArrayWithRand **
    **************************
    ** Load up an array with random longs.
    */
    private static void LoadNumArrayWithRand(int[][] array,     /* Pointer to arrays */
            int arraysize,
            int numarrays)         /* # of elements in array */
    {
        int i;                 /* Used for index */

        /*
        ** Initialize the random number generator
        */
        ByteMark.randnum(13);

        /*
        ** Load up first array with randoms
        */
        for (i = 0; i < arraysize; i++)
            array[0][i] = ByteMark.randnum(0);

        /*
        ** Now, if there's more than one array to load, copy the
        ** first into each of the others.
        */
        for (i = 1; i < numarrays; i++)
        {
            // the old code didn't do a memcpy, so I'm not doing
            // an Array.Copy()
            for (int j = 0; j < arraysize; j++)
                array[i][j] = array[0][j];
        }

        return;
    }

    /****************
    ** NumHeapSort **
    *****************
    ** Pass this routine a pointer to an array of long
    ** integers.  Also pass in minimum and maximum offsets.
    ** This routine performs a heap sort on that array.
    */
    private static void NumHeapSort(int[] array,
        int bottom,           /* Lower bound */
        int top)              /* Upper bound */
    {
        int temp;                     /* Used to exchange elements */
        int i;                        /* Loop index */

        /*
        ** First, build a heap in the array
        */
        for (i = (top / 2); i > 0; --i)
            NumSift(array, i, top);

        /*
        ** Repeatedly extract maximum from heap and place it at the
        ** end of the array.  When we get done, we'll have a sorted
        ** array.
        */
        for (i = top; i > 0; --i)
        {
            NumSift(array, bottom, i);
            temp = array[0];                    /* Perform exchange */
            array[0] = array[i];
            array[i] = temp;
        }
        return;
    }

    /************
    ** NumSift **
    *************
    ** Peforms the sift operation on a numeric array,
    ** constructing a heap in the array.
    */
    private static void NumSift(int[] array,     /* Array of numbers */
        int i,                /* Minimum of array */
        int j)                /* Maximum of array */
    {
        int k;
        int temp;                              /* Used for exchange */

        while ((i + i) <= j)
        {
            k = i + i;
            if (k < j)
                if (array[k] < array[k + 1])
                    ++k;
            if (array[i] < array[k])
            {
                temp = array[k];
                array[k] = array[i];
                array[i] = temp;
                i = k;
            }
            else
                i = j + 1;
        }
        return;
    }
}

///////////////////////////////////////////////////////////////////////////////////////
// New class
///////////////////////////////////////////////////////////////////////////////////////
public class NumericSortRect : SortStruct
{
    public override string Name()
    {
        return "NUMERIC SORT(rectangle)";
    }

    public override double Run()
    {
        /*
        ** Set the error context string.
        */
        int[,] arraybase;        /* Base pointers of array */
        long accumtime;         /* Accumulated time */
        double iterations;      /* Iteration counter */

        /*
        ** See if we need to do self adjustment code.
        */
        if (this.adjust == 0)
        {
            /*
            ** Self-adjustment code.  The system begins by sorting 1
            ** array.  If it does that in no time, then two arrays
            ** are built and sorted.  This process continues until
            ** enough arrays are built to handle the tolerance.
            */
            this.numarrays = 1;
            while (true)
            {
                /*
                ** Allocate space for arrays
                */
                arraybase = new int[this.numarrays, this.arraysize];

                /*
                ** Do an iteration of the numeric sort.  If the
                ** elapsed time is less than or equal to the permitted
                ** minimum, then allocate for more arrays and
                ** try again.
                */
                if (DoNumSortIteration(arraybase,
                    this.arraysize,
                    this.numarrays) > global.min_ticks)
                    break;          /* We're ok...exit */
                if (this.numarrays++ > global.NUMNUMARRAYS)
                {
                    throw new Exception("CPU:NSORT -- NUMNUMARRAYS hit.");
                }
            }
        }
        else
        {
            /*
            ** Allocate space for arrays
            */
            arraybase = new int[this.numarrays, this.arraysize];
        }

        /*
        ** All's well if we get here.  Repeatedly perform sorts until the
        ** accumulated elapsed time is greater than # of seconds requested.
        */
        accumtime = 0L;
        iterations = (double)0.0;

        do
        {
            accumtime += DoNumSortIteration(arraybase,
                this.arraysize,
                this.numarrays);
            iterations += (double)1.0;
        } while (ByteMark.TicksToSecs(accumtime) < this.request_secs);

        if (this.adjust == 0)
            this.adjust = 1;

        return (iterations * (double)this.numarrays / ByteMark.TicksToFracSecs(accumtime));
    }

    /***********************
    ** DoNumSortIteration **
    ************************
    ** This routine executes one iteration of the numeric
    ** sort benchmark.  It returns the number of ticks
    ** elapsed for the iteration.
    */

    // JTR: The last 2 parms are no longer needed as they
    // can be inferred from the arraybase. <shrug>
    private static int DoNumSortIteration(int[,] arraybase, int arraysize, int numarrays)
    {
        long elapsed;          /* Elapsed ticks */
        int i;
        /*
        ** Load up the array with random numbers
        */
        LoadNumArrayWithRand(arraybase, arraysize, numarrays);

        /*
        ** Start the stopwatch
        */
        elapsed = ByteMark.StartStopwatch();

        /*
        ** Execute a heap of heapsorts
        */
        for (i = 0; i < numarrays; i++)
        {
            //          NumHeapSort(arraybase+i*arraysize,0L,arraysize-1L);
            NumHeapSort(arraybase, i, arraysize - 1);
        }

        /*
        ** Get elapsed time
        */
        elapsed = ByteMark.StopStopwatch(elapsed);
#if DEBUG
        {
            for (i = 0; i < arraysize - 1; i++)
            {   /*
                ** Compare to check for proper
                ** sort.
                */
                if (arraybase[0, i + 1] < arraybase[0, i])
                {
                    Console.Write("size: {0}, count: {1}, total: {2}\n", arraysize, numarrays, arraybase.Length);
                    Console.Write("Sort Error at index {0}\n", i);
                    break;
                }
            }
        }
#endif

        return ((int)elapsed);
    }

    /*************************
    ** LoadNumArrayWithRand **
    **************************
    ** Load up an array with random longs.
    */
    private static void LoadNumArrayWithRand(int[,] array,     /* Pointer to arrays */
            int arraysize,
            int numarrays)         /* # of elements in array */
    {
        int i;                 /* Used for index */

        /*
        ** Initialize the random number generator
        */
        ByteMark.randnum(13);

        /*
        ** Load up first array with randoms
        */
        for (i = 0; i < arraysize; i++)
            array[0, i] = ByteMark.randnum(0);

        /*
        ** Now, if there's more than one array to load, copy the
        ** first into each of the others.
        */
        while (--numarrays > 0)
        {
            for (int j = 0; j < arraysize; j++, i++)
                array[numarrays, j] = array[0, j];
        }

        return;
    }

    /****************
    ** NumHeapSort **
    *****************
    ** Pass this routine a pointer to an array of long
    ** integers.  Also pass in minimum and maximum offsets.
    ** This routine performs a heap sort on that array.
    */
    private static void NumHeapSort(int[,] array,
        int row,              /* which row */
        int top)              /* Upper bound */
    {
        int temp;                     /* Used to exchange elements */
        int i;                        /* Loop index */

        /*
        ** First, build a heap in the array
        */
        for (i = (top / 2); i > 0; --i)
            NumSift(array, row, i, top);

        /*
        ** Repeatedly extract maximum from heap and place it at the
        ** end of the array.  When we get done, we'll have a sorted
        ** array.
        */
        for (i = top; i > 0; --i)
        {
            NumSift(array, row, 0, i);
            temp = array[row, 0];                    /* Perform exchange */
            array[row, 0] = array[row, i];
            array[row, i] = temp;
        }
        return;
    }

    /************
    ** NumSift **
    *************
    ** Peforms the sift operation on a numeric array,
    ** constructing a heap in the array.
    */
    private static void NumSift(int[,] array,     /* Array of numbers */
        int row,
        int i,                /* Minimum of array */
        int j)                /* Maximum of array */
    {
        int k;
        int temp;                              /* Used for exchange */

        while ((i + i) <= j)
        {
            k = i + i;
            if (k < j)
                if (array[row, k] < array[row, k + 1])
                    ++k;
            if (array[row, i] < array[row, k])
            {
                temp = array[row, k];
                array[row, k] = array[row, i];
                array[row, i] = temp;
                i = k;
            }
            else
                i = j + 1;
        }
        return;
    }
}