|
| 1 | +// SIMD Kernel Benchmark Harness |
| 2 | +// Author: Peter Jensen |
| 3 | + |
| 4 | +function Benchmark (config) { |
| 5 | + this.config = config; |
| 6 | + this.initOk = true; // Initialize all properties used on a Benchmark object |
| 7 | + this.cleanupOk = true; |
| 8 | + this.useAutoIterations = true; |
| 9 | + this.autoIterations = 0; |
| 10 | + this.actualIterations = 0; |
| 11 | + this.simdTime = 0; |
| 12 | + this.nonSimdTime = 0; |
| 13 | +} |
| 14 | + |
| 15 | +function Benchmarks () { |
| 16 | + this.benchmarks = []; |
| 17 | +} |
| 18 | + |
| 19 | +Benchmarks.prototype.add = function (benchmark) { |
| 20 | + this.benchmarks.push (benchmark); |
| 21 | + return this.benchmarks.length - 1; |
| 22 | +} |
| 23 | + |
| 24 | +Benchmarks.prototype.runOne = function (benchmark) { |
| 25 | + |
| 26 | + function timeKernel(kernel, iterations) { |
| 27 | + var start, stop; |
| 28 | + start = Date.now(); |
| 29 | + kernel(iterations); |
| 30 | + stop = Date.now(); |
| 31 | + return stop - start; |
| 32 | + } |
| 33 | + |
| 34 | + function computeIterations() { |
| 35 | + var desiredRuntime = 1000; // milliseconds for longest running kernel |
| 36 | + var testIterations = 10; // iterations used to determine time for desiredRuntime |
| 37 | + |
| 38 | + // Make the slowest kernel run for at least 500ms |
| 39 | + var simdTime = timeKernel(benchmark.config.kernelSimd, testIterations); |
| 40 | + var nonSimdTime = timeKernel(benchmark.config.kernelNonSimd, testIterations); |
| 41 | + var maxTime = simdTime > nonSimdTime ? simdTime : nonSimdTime; |
| 42 | + while (maxTime < 500) { |
| 43 | + testIterations *= 2; |
| 44 | + simdTime = timeKernel(benchmark.config.kernelSimd, testIterations); |
| 45 | + nonSimdTime = timeKernel(benchmark.config.kernelNonSimd, testIterations); |
| 46 | + maxTime = simdTime > nonSimdTime ? simdTime : nonSimdTime; |
| 47 | + } |
| 48 | + maxTime = simdTime > nonSimdTime ? simdTime : nonSimdTime; |
| 49 | + |
| 50 | + // Compute iteration count for 1 second run of slowest kernel |
| 51 | + var iterations = Math.ceil(desiredRuntime * testIterations / maxTime); |
| 52 | + return iterations; |
| 53 | + } |
| 54 | + |
| 55 | + // Initialize the kernels and check the correctness status |
| 56 | + if (!benchmark.config.kernelInit()) { |
| 57 | + benchmark.initOk = false; |
| 58 | + return false; |
| 59 | + } |
| 60 | + |
| 61 | + // Determine how many iterations to use. |
| 62 | + if (benchmark.useAutoIterations) { |
| 63 | + benchmark.autoIterations = computeIterations(); |
| 64 | + benchmark.actualIterations = benchmark.autoIterations; |
| 65 | + } |
| 66 | + else { |
| 67 | + benchmark.actualIterations = benchmark.config.kernelIterations; |
| 68 | + } |
| 69 | + |
| 70 | + // Run the SIMD kernel |
| 71 | + benchmark.simdTime = timeKernel(benchmark.config.kernelSimd, benchmark.actualIterations); |
| 72 | + |
| 73 | + // Run the non-SIMD kernel |
| 74 | + benchmark.nonSimdTime = timeKernel(benchmark.config.kernelNonSimd, benchmark.actualIterations); |
| 75 | + |
| 76 | + // Do the final sanity check |
| 77 | + if (!benchmark.config.kernelCleanup()) { |
| 78 | + benchmark.cleanupOk = false; |
| 79 | + return false; |
| 80 | + } |
| 81 | + |
| 82 | + return true; |
| 83 | +} |
| 84 | + |
| 85 | +Benchmarks.prototype.report = function (benchmark, outputFunctions) { |
| 86 | + |
| 87 | + function fillRight(str, width) { |
| 88 | + str += ""; // make sure it's a string |
| 89 | + while (str.length < width) { |
| 90 | + str += " "; |
| 91 | + } |
| 92 | + return str; |
| 93 | + } |
| 94 | + |
| 95 | + function fillLeft(str, width) { |
| 96 | + str += ""; // make sure it's a string |
| 97 | + while (str.length < width) { |
| 98 | + str = " " + str; |
| 99 | + } |
| 100 | + return str; |
| 101 | + } |
| 102 | + |
| 103 | + if (!benchmark.initOk) { |
| 104 | + outputFunctions.notifyError(fillRight(benchmark.config.kernelName + ": ", 23) + "FAILED INIT"); |
| 105 | + return; |
| 106 | + } |
| 107 | + if (!benchmark.cleanupOk) { |
| 108 | + outputFunctions.notifyError(fillRight(benchmark.config.kernelName + ": ", 23) + "FAILED CLEANUP"); |
| 109 | + return; |
| 110 | + } |
| 111 | + |
| 112 | + var ratio = benchmark.nonSimdTime / benchmark.simdTime; |
| 113 | + ratio = ratio.toFixed(2); |
| 114 | + outputFunctions.notifyResult( |
| 115 | + fillRight(benchmark.config.kernelName + ": ", 23) + |
| 116 | + "Iterations(" + fillLeft(benchmark.actualIterations, 10) + ")" + |
| 117 | + ", SIMD(" + fillLeft(benchmark.simdTime + "ms)", 8) + |
| 118 | + ", Non-SIMD(" + fillLeft(benchmark.nonSimdTime + "ms)", 8) + |
| 119 | + ", Speedup(" + ratio + ")"); |
| 120 | + outputFunctions.timeData.labels.push(benchmark.config.kernelName); |
| 121 | + outputFunctions.timeData.datasets[0].data.push(benchmark.simdTime); |
| 122 | + outputFunctions.timeData.datasets[1].data.push(benchmark.nonSimdTime); |
| 123 | + outputFunctions.speedupData.labels.push(benchmark.config.kernelName); |
| 124 | + outputFunctions.speedupData.datasets[0].data.push(ratio); |
| 125 | +} |
| 126 | + |
| 127 | +Benchmarks.prototype.runAll = function (outputFunctions, useAutoIterations) { |
| 128 | + if (typeof useAutoIterations === "undefined") { |
| 129 | + useAutoIterations = false; |
| 130 | + } |
| 131 | + for (var i = 0, n = this.benchmarks.length; i < n; ++i) { |
| 132 | + var benchmark = this.benchmarks[i]; |
| 133 | + benchmark.useAutoIterations = useAutoIterations; |
| 134 | + this.runOne(benchmark); |
| 135 | + this.report(benchmark, outputFunctions); |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +var benchmarks = new Benchmarks (); |
0 commit comments