gf_vect_dot_prod_perf.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /**********************************************************************
  2. Copyright(c) 2011-2015 Intel Corporation All rights reserved.
  3. Redistribution and use in source and binary forms, with or without
  4. modification, are permitted provided that the following conditions
  5. are met:
  6. * Redistributions of source code must retain the above copyright
  7. notice, this list of conditions and the following disclaimer.
  8. * Redistributions in binary form must reproduce the above copyright
  9. notice, this list of conditions and the following disclaimer in
  10. the documentation and/or other materials provided with the
  11. distribution.
  12. * Neither the name of Intel Corporation nor the names of its
  13. contributors may be used to endorse or promote products derived
  14. from this software without specific prior written permission.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  16. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  17. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  18. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  19. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  21. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  22. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  23. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  25. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. **********************************************************************/
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h> // for memset, memcmp
  30. #include "erasure_code.h"
  31. #include "test.h"
  32. #ifndef FUNCTION_UNDER_TEST
  33. # define FUNCTION_UNDER_TEST gf_vect_dot_prod
  34. #endif
  35. #define str(s) #s
  36. #define xstr(s) str(s)
  37. #ifndef GT_L3_CACHE
  38. # define GT_L3_CACHE 32*1024*1024 /* some number > last level cache */
  39. #endif
  40. #if !defined(COLD_TEST) && !defined(TEST_CUSTOM)
  41. // Cached test, loop many times over small dataset
  42. # define TEST_SOURCES 10
  43. # define TEST_LEN 8*1024
  44. # define TEST_TYPE_STR "_warm"
  45. #elif defined (COLD_TEST)
  46. // Uncached test. Pull from large mem base.
  47. # define TEST_SOURCES 10
  48. # define TEST_LEN ((GT_L3_CACHE / TEST_SOURCES) & ~(64-1))
  49. # define TEST_TYPE_STR "_cold"
  50. #elif defined (TEST_CUSTOM)
  51. # define TEST_TYPE_STR "_cus"
  52. #endif
  53. typedef unsigned char u8;
  54. void dump(unsigned char *buf, int len)
  55. {
  56. int i;
  57. for (i = 0; i < len;) {
  58. printf(" %2x", 0xff & buf[i++]);
  59. if (i % 32 == 0)
  60. printf("\n");
  61. }
  62. printf("\n");
  63. }
  64. void dump_matrix(unsigned char **s, int k, int m)
  65. {
  66. int i, j;
  67. for (i = 0; i < k; i++) {
  68. for (j = 0; j < m; j++) {
  69. printf(" %2x", s[i][j]);
  70. }
  71. printf("\n");
  72. }
  73. printf("\n");
  74. }
  75. void vect_dot_prod_perf(void (*fun_ptr)
  76. (int, int, unsigned char *, unsigned char **, unsigned char *),
  77. u8 * g, u8 * g_tbls, u8 ** buffs, u8 * dest_ref)
  78. {
  79. int j;
  80. for (j = 0; j < TEST_SOURCES; j++)
  81. gf_vect_mul_init(g[j], &g_tbls[j * 32]);
  82. (*fun_ptr) (TEST_LEN, TEST_SOURCES, &g_tbls[0], buffs, dest_ref);
  83. }
  84. int main(int argc, char *argv[])
  85. {
  86. int i, j;
  87. void *buf;
  88. u8 g[TEST_SOURCES], g_tbls[TEST_SOURCES * 32], *dest, *dest_ref;
  89. u8 *temp_buff, *buffs[TEST_SOURCES];
  90. struct perf start;
  91. printf(xstr(FUNCTION_UNDER_TEST) ": %dx%d\n", TEST_SOURCES, TEST_LEN);
  92. // Allocate the arrays
  93. for (i = 0; i < TEST_SOURCES; i++) {
  94. if (posix_memalign(&buf, 64, TEST_LEN)) {
  95. printf("alloc error: Fail");
  96. return -1;
  97. }
  98. buffs[i] = buf;
  99. }
  100. if (posix_memalign(&buf, 64, TEST_LEN)) {
  101. printf("alloc error: Fail");
  102. return -1;
  103. }
  104. dest = buf;
  105. if (posix_memalign(&buf, 64, TEST_LEN)) {
  106. printf("alloc error: Fail");
  107. return -1;
  108. }
  109. dest_ref = buf;
  110. if (posix_memalign(&buf, 64, TEST_LEN)) {
  111. printf("alloc error: Fail");
  112. return -1;
  113. }
  114. temp_buff = buf;
  115. // Performance test
  116. for (i = 0; i < TEST_SOURCES; i++)
  117. for (j = 0; j < TEST_LEN; j++)
  118. buffs[i][j] = rand();
  119. memset(dest, 0, TEST_LEN);
  120. memset(temp_buff, 0, TEST_LEN);
  121. memset(dest_ref, 0, TEST_LEN);
  122. memset(g, 0, TEST_SOURCES);
  123. for (i = 0; i < TEST_SOURCES; i++)
  124. g[i] = rand();
  125. #ifdef DO_REF_PERF
  126. BENCHMARK(&start, BENCHMARK_TIME,
  127. vect_dot_prod_perf(&gf_vect_dot_prod_base, g, g_tbls, buffs, dest_ref)
  128. );
  129. printf("gf_vect_dot_prod_base" TEST_TYPE_STR ": ");
  130. perf_print(start, (long long)TEST_LEN * (TEST_SOURCES + 1));
  131. #else
  132. vect_dot_prod_perf(&gf_vect_dot_prod_base, g, g_tbls, buffs, dest_ref);
  133. #endif
  134. BENCHMARK(&start, BENCHMARK_TIME,
  135. vect_dot_prod_perf(&FUNCTION_UNDER_TEST, g, g_tbls, buffs, dest));
  136. printf(xstr(FUNCTION_UNDER_TEST) TEST_TYPE_STR ": ");
  137. perf_print(start, (long long)TEST_LEN * (TEST_SOURCES + 1));
  138. if (0 != memcmp(dest_ref, dest, TEST_LEN)) {
  139. printf("Fail zero " xstr(FUNCTION_UNDER_TEST) " test\n");
  140. dump_matrix(buffs, 5, TEST_SOURCES);
  141. printf("dprod_base:");
  142. dump(dest_ref, 25);
  143. printf("dprod:");
  144. dump(dest, 25);
  145. return -1;
  146. }
  147. printf("pass perf check\n");
  148. return 0;
  149. }