Guest User

Untitled

a guest
Jul 18th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.85 KB | None | 0 0
  1. using BenchmarkDotNet.Attributes;
  2. using BenchmarkDotNet.Running;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Numerics;
  7. using static System.Console;
  8.  
  9. namespace Sum
  10. {
  11. public class Program
  12. {
  13. const int LENGTH = 10_000_000;
  14. private static readonly int[] array1 = Enumerable.Range(0, LENGTH).ToArray();
  15. private static readonly int[] array2 = Enumerable.Range(0, LENGTH).ToArray();
  16.  
  17. static void Main(string[] args)
  18. {
  19. var summary = BenchmarkRunner.Run<Program>();
  20. ReadLine();
  21. }
  22.  
  23. [Benchmark]
  24. public void Sum2ArraySIMD()
  25. {
  26. var result = new int[array1.Length];
  27. int count = 0;
  28. if (Vector.IsHardwareAccelerated)
  29. {
  30. int step = Vector<int>.Count;
  31. Vector<int> vector = Vector<int>.Zero;
  32. for (; count <= result.Length - step; count += step)
  33. {
  34. vector = new Vector<int>(array1, count) + new Vector<int>(array2, count);
  35. vector.CopyTo(result, count);
  36. }
  37. }
  38.  
  39. for (; count < array1.Length; count++)
  40. {
  41. result[count] = array1[count] + array2[count];
  42. }
  43. }
  44.  
  45. [Benchmark]
  46. public void Sum2ArraysNoSIMD()
  47. {
  48. var result = new int[array1.Length];
  49. for (int count = 0; count < array1.Length; count++)
  50. {
  51. result[count] = array1[count] + array2[count];
  52. }
  53. }
  54.  
  55. [Benchmark]
  56. public void MinAndMaxSIMD()
  57. {
  58. int min = int.MaxValue, max = int.MinValue;
  59. var vmin = new Vector<int>(int.MaxValue);
  60. var vmax = new Vector<int>(int.MinValue);
  61. int count = 0;
  62. if (Vector.IsHardwareAccelerated)
  63. {
  64. int step = Vector<int>.Count;
  65. Vector<int> vector = Vector<int>.Zero;
  66. for (; count <= array1.Length - step; count += step)
  67. {
  68. vector = new Vector<int>(array1, count);
  69. vmin = Vector.Min(vmin, vector);
  70. vmax = Vector.Max(vmax, vector);
  71. }
  72.  
  73. for (var j = 0; j < step; ++j)
  74. {
  75. min = Math.Min(min, vmin[j]);
  76. max = Math.Max(max, vmax[j]);
  77. }
  78. }
  79.  
  80. // Process any remaining elements
  81. for (; count < array1.Length; count++)
  82. {
  83. min = Math.Min(min, array1[count]);
  84. max = Math.Max(max, array1[count]);
  85. }
  86. }
  87.  
  88. [Benchmark]
  89. public void NonSIMDMinMax()
  90. {
  91. int min = int.MaxValue;
  92. int max = int.MinValue;
  93. for (int i = 0; i < array1.Length; i++)
  94. {
  95. min = Math.Min(min, array1[i]);
  96. max = Math.Max(max, array1[i]);
  97. }
  98. }
  99.  
  100. [Benchmark]
  101. public void SumSIMD()
  102. {
  103. int count = 0;
  104. int result = 0;
  105. if (Vector.IsHardwareAccelerated)
  106. {
  107. int step = Vector<int>.Count;
  108. var vector = new Vector<int>(array1, 0);
  109. for (; count <= array1.Length - step; count += step)
  110. vector += new Vector<int>(array1, count);
  111.  
  112. var final = new int[step];
  113. vector.CopyTo(final);
  114.  
  115. for (int i = 0; i < final.Length; i++)
  116. result += final[i];
  117. }
  118. for (; count < array1.Length; count++)
  119. result += array1[count];
  120. }
  121.  
  122. [Benchmark]
  123. public void SumNoSIMD()
  124. {
  125. int result = 0;
  126. for (int i = 0; i < array1.Length; i++)
  127. result += array1[i];
  128. }
  129.  
  130. }
  131. }
Add Comment
Please, Sign In to add comment