暫無描述
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PerfNotes.cs 9.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. using System;
  2. using System.Runtime.CompilerServices;
  3. using Unity.Collections;
  4. using Unity.Jobs;
  5. using Unity.Mathematics;
  6. [assembly: InternalsVisibleTo("Burst.Benchmarks")]
  7. namespace UnityBenchShared
  8. {
  9. // These test cases are from the `doc/perf-notes.md` document
  10. internal static class PerfNotes
  11. {
  12. public static int BenchArraySize = 16 * 1024 * 1024;
  13. }
  14. internal struct PartialWrite : IJob, IDisposable
  15. {
  16. [ReadOnly] public NativeArray<int> a;
  17. public NativeArray<int> b;
  18. public struct Provider : IArgumentProvider
  19. {
  20. public object Value
  21. {
  22. get
  23. {
  24. var length = PerfNotes.BenchArraySize;
  25. var job = new PartialWrite()
  26. {
  27. a = new NativeArray<int>(length, Allocator.Persistent),
  28. b = new NativeArray<int>(length, Allocator.Persistent)
  29. };
  30. var random = new System.Random(0);
  31. for (int i = 0; i < length; i++)
  32. {
  33. job.a[i] = random.Next(200);
  34. job.b[i] = random.Next();
  35. }
  36. return job;
  37. }
  38. }
  39. }
  40. public void Execute()
  41. {
  42. for (int i = 0; i < a.Length; ++i)
  43. {
  44. int av = a[i];
  45. if (av > 100)
  46. {
  47. b[i] = av;
  48. }
  49. }
  50. }
  51. public void Dispose()
  52. {
  53. a.Dispose();
  54. b.Dispose();
  55. }
  56. }
  57. internal struct PartialWriteWorkaround : IJob, IDisposable
  58. {
  59. [ReadOnly] public NativeArray<int> a;
  60. public NativeArray<int> b;
  61. public struct Provider : IArgumentProvider
  62. {
  63. public object Value
  64. {
  65. get
  66. {
  67. var length = PerfNotes.BenchArraySize;
  68. var job = new PartialWriteWorkaround()
  69. {
  70. a = new NativeArray<int>(length, Allocator.Persistent),
  71. b = new NativeArray<int>(length, Allocator.Persistent)
  72. };
  73. var random = new System.Random(0);
  74. for (int i = 0; i < length; i++)
  75. {
  76. job.a[i] = random.Next(200);
  77. job.b[i] = random.Next();
  78. }
  79. return job;
  80. }
  81. }
  82. }
  83. public void Execute()
  84. {
  85. for (int i = 0; i < a.Length; ++i)
  86. {
  87. int av = a[i];
  88. int v = b[i];
  89. if (av > 100)
  90. {
  91. v = av;
  92. }
  93. b[i] = v;
  94. }
  95. }
  96. public void Dispose()
  97. {
  98. a.Dispose();
  99. b.Dispose();
  100. }
  101. }
  102. internal struct IntToFloatPenalty : IJob, IDisposable
  103. {
  104. [ReadOnly] public NativeArray<float> b;
  105. public struct Provider : IArgumentProvider
  106. {
  107. public object Value
  108. {
  109. get
  110. {
  111. var length = PerfNotes.BenchArraySize * 4;
  112. var job = new IntToFloatPenalty()
  113. {
  114. b = new NativeArray<float>(length, Allocator.Persistent)
  115. };
  116. return job;
  117. }
  118. }
  119. }
  120. public void Execute()
  121. {
  122. int k = 100;
  123. for (int i = 0; i < b.Length; ++i)
  124. {
  125. b[i] = k;
  126. ++k;
  127. }
  128. }
  129. public void Dispose()
  130. {
  131. b.Dispose();
  132. }
  133. }
  134. internal struct DivisionByScalar : IJob, IDisposable
  135. {
  136. float divisor;
  137. [ReadOnly] public NativeArray<float> a;
  138. public NativeArray<float> b;
  139. public struct Provider : IArgumentProvider
  140. {
  141. public object Value
  142. {
  143. get
  144. {
  145. var length = PerfNotes.BenchArraySize;
  146. var job = new DivisionByScalar()
  147. {
  148. divisor = 13,
  149. a = new NativeArray<float>(length, Allocator.Persistent),
  150. b = new NativeArray<float>(length, Allocator.Persistent)
  151. };
  152. var random = new System.Random(0);
  153. for (int i = 0; i < length; i++)
  154. {
  155. job.a[i] = random.Next();
  156. }
  157. return job;
  158. }
  159. }
  160. }
  161. public void Execute()
  162. {
  163. for (int i = 0; i < a.Length; ++i)
  164. {
  165. b[i] = a[i] / divisor;
  166. }
  167. }
  168. public void Dispose()
  169. {
  170. a.Dispose();
  171. b.Dispose();
  172. }
  173. }
  174. internal struct SquareRoot : IJob, IDisposable
  175. {
  176. [ReadOnly] public NativeArray<float> a;
  177. public NativeArray<float> b;
  178. public struct Provider : IArgumentProvider
  179. {
  180. public object Value
  181. {
  182. get
  183. {
  184. var length = PerfNotes.BenchArraySize;
  185. var job = new SquareRoot()
  186. {
  187. a = new NativeArray<float>(length, Allocator.Persistent),
  188. b = new NativeArray<float>(length, Allocator.Persistent)
  189. };
  190. var random = new System.Random(0);
  191. for (int i = 0; i < length; i++)
  192. {
  193. job.a[i] = random.Next();
  194. }
  195. return job;
  196. }
  197. }
  198. }
  199. public void Execute()
  200. {
  201. for (int i = 0; i < a.Length; ++i)
  202. {
  203. b[i] = math.sqrt(a[i]);
  204. }
  205. }
  206. public void Dispose()
  207. {
  208. a.Dispose();
  209. b.Dispose();
  210. }
  211. }
  212. internal struct SquareRootRecip : IJob, IDisposable
  213. {
  214. [ReadOnly] public NativeArray<float> a;
  215. public NativeArray<float> b;
  216. public struct Provider : IArgumentProvider
  217. {
  218. public object Value
  219. {
  220. get
  221. {
  222. var length = PerfNotes.BenchArraySize / 2;
  223. var job = new SquareRootRecip()
  224. {
  225. a = new NativeArray<float>(length, Allocator.Persistent),
  226. b = new NativeArray<float>(length, Allocator.Persistent)
  227. };
  228. var random = new System.Random(0);
  229. for (int i = 0; i < length; i++)
  230. {
  231. job.a[i] = random.Next();
  232. }
  233. return job;
  234. }
  235. }
  236. }
  237. public void Execute()
  238. {
  239. for (int i = 0; i < a.Length; ++i)
  240. {
  241. b[i] = math.rsqrt(a[i]);
  242. }
  243. }
  244. public void Dispose()
  245. {
  246. a.Dispose();
  247. b.Dispose();
  248. }
  249. }
  250. internal struct RedundantStore : IJob, IDisposable
  251. {
  252. public int sum;
  253. public NativeArray<int> a;
  254. public struct Provider : IArgumentProvider
  255. {
  256. public object Value
  257. {
  258. get
  259. {
  260. var length = PerfNotes.BenchArraySize * 2;
  261. var job = new RedundantStore()
  262. {
  263. a = new NativeArray<int>(length, Allocator.Persistent)
  264. };
  265. var random = new System.Random(0);
  266. for (int i = 0; i < length; i++)
  267. {
  268. job.a[i] = random.Next();
  269. }
  270. return job;
  271. }
  272. }
  273. }
  274. public void Execute()
  275. {
  276. for (int i = 0; i < a.Length; ++i)
  277. {
  278. sum += a[i];
  279. }
  280. }
  281. public void Dispose()
  282. {
  283. a.Dispose();
  284. }
  285. }
  286. internal struct RedundantStoreWorkaround : IJob, IDisposable
  287. {
  288. public int sum;
  289. public NativeArray<int> a;
  290. public struct Provider : IArgumentProvider
  291. {
  292. public object Value
  293. {
  294. get
  295. {
  296. var length = PerfNotes.BenchArraySize * 2;
  297. var job = new RedundantStoreWorkaround()
  298. {
  299. a = new NativeArray<int>(length, Allocator.Persistent)
  300. };
  301. var random = new System.Random(0);
  302. for (int i = 0; i < length; i++)
  303. {
  304. job.a[i] = random.Next();
  305. }
  306. return job;
  307. }
  308. }
  309. }
  310. public void Execute()
  311. {
  312. for (int i = 0; i < a.Length; ++i)
  313. {
  314. sum += a[i];
  315. }
  316. }
  317. public void Dispose()
  318. {
  319. a.Dispose();
  320. }
  321. }
  322. /* TODO: Is from the notes, but we don't have have a mock of IComponentData
  323. public struct SimpleComponentData : IComponentData
  324. {
  325. public int val;
  326. }
  327. public struct SimpleComponentDataTest : IJob
  328. {
  329. public ComponentDataArray<SimpleComponentData> a;
  330. public void Execute()
  331. {
  332. for (int i = 0; i < a.Length; ++i)
  333. {
  334. var item = a[i];
  335. item.val += 9999;
  336. a[i] = item;
  337. }
  338. }
  339. }
  340. */
  341. }