Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 

222 wiersze
6.5 KiB

  1. //
  2. // Lol Engine - Benchmark program
  3. //
  4. // Copyright: (c) 2005-2011 Sam Hocevar <sam@hocevar.net>
  5. // This program is free software; you can redistribute it and/or
  6. // modify it under the terms of the Do What The Fuck You Want To
  7. // Public License, Version 2, as published by Sam Hocevar. See
  8. // http://sam.zoy.org/projects/COPYING.WTFPL for more details.
  9. //
  10. #if defined HAVE_CONFIG_H
  11. # include "config.h"
  12. #endif
  13. #include <cstdio>
  14. #include "core.h"
  15. #include "loldebug.h"
  16. using namespace std;
  17. using namespace lol;
  18. static size_t const MATRIX_TABLE_SIZE = 64 * 1024;
  19. static size_t const MATRIX_RUNS = 100;
  20. static size_t const HALF_TABLE_SIZE = 1024 * 1024;
  21. static size_t const HALF_RUNS = 50;
  22. static void bench_matrix(int mode);
  23. static void bench_half(int mode);
  24. int main(int argc, char **argv)
  25. {
  26. Log::Info("----------------------------\n");
  27. Log::Info(" Float matrices [-2.0, 2.0]\n");
  28. Log::Info("----------------------------\n");
  29. bench_matrix(1);
  30. Log::Info("-------------------------------------\n");
  31. Log::Info(" Half precision floats (random bits)\n");
  32. Log::Info("-------------------------------------\n");
  33. bench_half(1);
  34. Log::Info("-----------------------------------\n");
  35. Log::Info(" Half precision floats [-2.0, 2.0]\n");
  36. Log::Info("-----------------------------------\n");
  37. bench_half(2);
  38. return EXIT_SUCCESS;
  39. }
  40. static void bench_matrix(int mode)
  41. {
  42. float result[5] = { 0.0f };
  43. Timer timer;
  44. /* Set up tables */
  45. mat4 *pm = new mat4[MATRIX_TABLE_SIZE + 1];
  46. float *pf = new float[MATRIX_TABLE_SIZE];
  47. for (size_t run = 0; run < MATRIX_RUNS; run++)
  48. {
  49. switch (mode)
  50. {
  51. case 1:
  52. for (size_t i = 0; i < MATRIX_TABLE_SIZE; i++)
  53. for (int j = 0; j < 4; j++)
  54. for (int k = 0; k < 4; k++)
  55. pm[i][j][k] = RandF(-2.0f, 2.0f);
  56. break;
  57. }
  58. /* Copy matrices */
  59. timer.GetMs();
  60. for (size_t i = 0; i < MATRIX_TABLE_SIZE; i++)
  61. pm[i] = pm[i + 1];
  62. result[0] += timer.GetMs();
  63. /* Determinant */
  64. timer.GetMs();
  65. for (size_t i = 0; i < MATRIX_TABLE_SIZE; i++)
  66. pf[i] = pm[i].det();
  67. result[1] += timer.GetMs();
  68. /* Multiply matrices */
  69. timer.GetMs();
  70. for (size_t i = 0; i < MATRIX_TABLE_SIZE; i++)
  71. pm[i] *= pm[i + 1];
  72. result[2] += timer.GetMs();
  73. /* Add matrices */
  74. timer.GetMs();
  75. for (size_t i = 0; i < MATRIX_TABLE_SIZE; i++)
  76. pm[i] += pm[i + 1];
  77. result[3] += timer.GetMs();
  78. /* Invert matrix */
  79. timer.GetMs();
  80. for (size_t i = 0; i < MATRIX_TABLE_SIZE; i++)
  81. pm[i] = pm[i].invert();
  82. result[4] += timer.GetMs();
  83. }
  84. delete[] pm;
  85. delete[] pf;
  86. for (size_t i = 0; i < sizeof(result) / sizeof(*result); i++)
  87. result[i] *= 1000000.0f / (MATRIX_TABLE_SIZE * MATRIX_RUNS);
  88. Log::Info(" ns/elem\n");
  89. Log::Info("mat4 = mat4 %7.3f\n", result[0]);
  90. Log::Info("float = mat4.det() %7.3f\n", result[1]);
  91. Log::Info("mat4 *= mat4 %7.3f\n", result[2]);
  92. Log::Info("mat4 += mat4 %7.3f\n", result[3]);
  93. Log::Info("mat4 = mat4.invert() %7.3f\n", result[4]);
  94. }
  95. static void bench_half(int mode)
  96. {
  97. float result[10] = { 0.0f };
  98. Timer timer;
  99. /* Set up tables */
  100. float *pf = new float[HALF_TABLE_SIZE + 1];
  101. half *ph = new half[HALF_TABLE_SIZE + 1];
  102. for (size_t run = 0; run < HALF_RUNS; run++)
  103. {
  104. switch (mode)
  105. {
  106. case 1:
  107. for (size_t i = 0; i < HALF_TABLE_SIZE + 1; i++)
  108. ph[i] = half::makebits(rand());
  109. break;
  110. case 2:
  111. for (size_t i = 0; i < HALF_TABLE_SIZE + 1; i++)
  112. ph[i] = RandF(-2.0f, 2.0f);
  113. break;
  114. }
  115. #if defined __EXCEPTIONS
  116. #error lol
  117. #endif
  118. /* Copy float */
  119. timer.GetMs();
  120. for (size_t i = 0; i < HALF_TABLE_SIZE; i++)
  121. pf[i] = pf[i + 1];
  122. result[0] += timer.GetMs();
  123. /* Convert half to float (array) */
  124. timer.GetMs();
  125. half::convert(pf, ph, HALF_TABLE_SIZE);
  126. result[1] += timer.GetMs();
  127. /* Convert half to float (fast) */
  128. timer.GetMs();
  129. for (size_t i = 0; i < HALF_TABLE_SIZE; i++)
  130. pf[i] = (float)ph[i];
  131. result[2] += timer.GetMs();
  132. /* Add a half to every float */
  133. timer.GetMs();
  134. for (size_t i = 0; i < HALF_TABLE_SIZE; i++)
  135. pf[i] += ph[i];
  136. result[3] += timer.GetMs();
  137. /* Copy half */
  138. timer.GetMs();
  139. for (size_t i = 0; i < HALF_TABLE_SIZE; i++)
  140. ph[i] = ph[i + 1];
  141. result[4] += timer.GetMs();
  142. /* Change sign of every half */
  143. timer.GetMs();
  144. for (size_t i = 0; i < HALF_TABLE_SIZE; i++)
  145. ph[i] = -ph[i];
  146. result[5] += timer.GetMs();
  147. /* Convert float to half (array) */
  148. timer.GetMs();
  149. half::convert(ph, pf, HALF_TABLE_SIZE);
  150. result[6] += timer.GetMs();
  151. /* Convert float to half (fast) */
  152. timer.GetMs();
  153. for (size_t i = 0; i < HALF_TABLE_SIZE; i++)
  154. ph[i] = (half)pf[i];
  155. result[7] += timer.GetMs();
  156. /* Convert float to half (accurate) */
  157. timer.GetMs();
  158. for (size_t i = 0; i < HALF_TABLE_SIZE; i++)
  159. ph[i] = half::makeaccurate(pf[i]);
  160. result[8] += timer.GetMs();
  161. /* Add a float to every half */
  162. timer.GetMs();
  163. for (size_t i = 0; i < HALF_TABLE_SIZE; i++)
  164. ph[i] += pf[i];
  165. result[9] += timer.GetMs();
  166. }
  167. delete[] pf;
  168. delete[] ph;
  169. for (size_t i = 0; i < sizeof(result) / sizeof(*result); i++)
  170. result[i] *= 1000000.0f / (HALF_TABLE_SIZE * HALF_RUNS);
  171. Log::Info(" ns/elem\n");
  172. Log::Info("float = float %7.3f\n", result[0]);
  173. Log::Info("float = half (array) %7.3f\n", result[1]);
  174. Log::Info("float = half (fast) %7.3f\n", result[2]);
  175. Log::Info("float += half %7.3f\n", result[3]);
  176. Log::Info("half = half %7.3f\n", result[4]);
  177. Log::Info("half = -half %7.3f\n", result[5]);
  178. Log::Info("half = float (array) %7.3f\n", result[6]);
  179. Log::Info("half = float (fast) %7.3f\n", result[7]);
  180. Log::Info("half = float (accurate) %7.3f\n", result[8]);
  181. Log::Info("half += float %7.3f\n", result[9]);
  182. }