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