]>
git.proxmox.com Git - ceph.git/blob - ceph/src/isa-l/erasure_code/gf_vect_dot_prod_base_test.c
1 /**********************************************************************
2 Copyright(c) 2011-2015 Intel Corporation All rights reserved.
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions
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
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.
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 **********************************************************************/
32 #include <string.h> // for memset, memcmp
33 #include "erasure_code.h"
37 #define TEST_SIZE (TEST_LEN/2)
40 # define TEST_SOURCES 250
46 #define MMAX TEST_SOURCES
47 #define KMAX TEST_SOURCES
49 typedef unsigned char u8
;
51 void dump(unsigned char *buf
, int len
)
54 for (i
= 0; i
< len
;) {
55 printf(" %2x", 0xff & buf
[i
++]);
62 void dump_matrix(unsigned char **s
, int k
, int m
)
65 for (i
= 0; i
< k
; i
++) {
66 for (j
= 0; j
< m
; j
++) {
67 printf(" %2x", s
[i
][j
]);
74 void dump_u8xu8(unsigned char *s
, int k
, int m
)
77 for (i
= 0; i
< k
; i
++) {
78 for (j
= 0; j
< m
; j
++) {
79 printf(" %2x", 0xff & s
[j
+ (i
* m
)]);
86 int main(int argc
, char *argv
[])
88 int i
, j
, rtest
, m
, k
, nerrs
, r
, err
;
90 u8 g
[TEST_SOURCES
], g_tbls
[TEST_SOURCES
* 32], src_in_err
[TEST_SOURCES
];
91 u8
*dest
, *dest_ref
, *temp_buff
, *buffs
[TEST_SOURCES
];
92 u8 a
[MMAX
* KMAX
], b
[MMAX
* KMAX
], d
[MMAX
* KMAX
];
93 u8 src_err_list
[TEST_SOURCES
], *recov
[TEST_SOURCES
];
95 printf("gf_vect_dot_prod_base: %dx%d ", TEST_SOURCES
, TEST_LEN
);
97 // Allocate the arrays
98 for (i
= 0; i
< TEST_SOURCES
; i
++) {
99 if (posix_memalign(&buf
, 64, TEST_LEN
)) {
100 printf("alloc error: Fail");
106 if (posix_memalign(&buf
, 64, TEST_LEN
)) {
107 printf("alloc error: Fail");
112 if (posix_memalign(&buf
, 64, TEST_LEN
)) {
113 printf("alloc error: Fail");
118 if (posix_memalign(&buf
, 64, TEST_LEN
)) {
119 printf("alloc error: Fail");
125 for (i
= 0; i
< TEST_SOURCES
; i
++)
126 memset(buffs
[i
], 0, TEST_LEN
);
128 memset(dest
, 0, TEST_LEN
);
129 memset(temp_buff
, 0, TEST_LEN
);
130 memset(dest_ref
, 0, TEST_LEN
);
131 memset(g
, 0, TEST_SOURCES
);
133 // Test erasure code using gf_vect_dot_prod
137 if (m
> MMAX
|| k
> KMAX
)
140 gf_gen_cauchy1_matrix(a
, m
, k
);
143 for (i
= 0; i
< k
; i
++)
144 for (j
= 0; j
< TEST_LEN
; j
++)
145 buffs
[i
][j
] = rand();
148 for (i
= k
; i
< m
; i
++) {
149 for (j
= 0; j
< k
; j
++)
150 gf_vect_mul_init(a
[k
* i
+ j
], &g_tbls
[j
* 32]);
152 gf_vect_dot_prod_base(TEST_LEN
, k
, g_tbls
, buffs
, buffs
[i
]);
155 // Random buffers in erasure
156 memset(src_in_err
, 0, TEST_SOURCES
);
157 for (i
= 0, nerrs
= 0; i
< k
&& nerrs
< m
- k
; i
++) {
161 src_err_list
[nerrs
++] = i
;
164 // construct b by removing error rows
165 for (i
= 0, r
= 0; i
< k
; i
++, r
++) {
166 while (src_in_err
[r
]) {
170 for (j
= 0; j
< k
; j
++)
171 b
[k
* i
+ j
] = a
[k
* r
+ j
];
174 if (gf_invert_matrix((u8
*) b
, (u8
*) d
, k
) < 0)
175 printf("BAD MATRIX\n");
177 for (i
= 0, r
= 0; i
< k
; i
++, r
++) {
178 while (src_in_err
[r
]) {
186 for (i
= 0; i
< nerrs
; i
++) {
187 for (j
= 0; j
< k
; j
++)
188 gf_vect_mul_init(d
[k
* src_err_list
[i
] + j
], &g_tbls
[j
* 32]);
190 gf_vect_dot_prod_base(TEST_LEN
, k
, g_tbls
, recov
, temp_buff
);
192 if (0 != memcmp(temp_buff
, buffs
[src_err_list
[i
]], TEST_LEN
)) {
193 printf("Fail error recovery (%d, %d, %d)\n", m
, k
, nerrs
);
194 printf("recov %d:", src_err_list
[i
]);
197 dump(buffs
[src_err_list
[i
]], 25);
202 // Do more random tests
204 for (rtest
= 0; rtest
< RANDOMS
; rtest
++) {
205 while ((m
= (rand() % MMAX
)) < 2) ;
206 while ((k
= (rand() % KMAX
)) >= m
|| k
< 1) ;
208 if (m
> MMAX
|| k
> KMAX
)
211 gf_gen_cauchy1_matrix(a
, m
, k
);
214 for (i
= 0; i
< k
; i
++)
215 for (j
= 0; j
< TEST_LEN
; j
++)
216 buffs
[i
][j
] = rand();
219 for (i
= k
; i
< m
; i
++) {
220 for (j
= 0; j
< k
; j
++)
221 gf_vect_mul_init(a
[k
* i
+ j
], &g_tbls
[j
* 32]);
223 gf_vect_dot_prod_base(TEST_LEN
, k
, g_tbls
, buffs
, buffs
[i
]);
227 memset(src_in_err
, 0, TEST_SOURCES
);
228 for (i
= 0, nerrs
= 0; i
< k
&& nerrs
< m
- k
; i
++) {
232 src_err_list
[nerrs
++] = i
;
234 if (nerrs
== 0) { // should have at least one error
235 while ((err
= (rand() % KMAX
)) >= k
) ;
236 src_err_list
[nerrs
++] = err
;
239 // construct b by removing error rows
240 for (i
= 0, r
= 0; i
< k
; i
++, r
++) {
241 while (src_in_err
[r
]) {
245 for (j
= 0; j
< k
; j
++)
246 b
[k
* i
+ j
] = a
[k
* r
+ j
];
249 if (gf_invert_matrix((u8
*) b
, (u8
*) d
, k
) < 0)
250 printf("BAD MATRIX\n");
252 for (i
= 0, r
= 0; i
< k
; i
++, r
++) {
253 while (src_in_err
[r
]) {
261 for (i
= 0; i
< nerrs
; i
++) {
262 for (j
= 0; j
< k
; j
++)
263 gf_vect_mul_init(d
[k
* src_err_list
[i
] + j
], &g_tbls
[j
* 32]);
265 gf_vect_dot_prod_base(TEST_LEN
, k
, g_tbls
, recov
, temp_buff
);
267 if (0 != memcmp(temp_buff
, buffs
[src_err_list
[i
]], TEST_LEN
)) {
268 printf("Fail error recovery (%d, %d, %d) - ", m
, k
, nerrs
);
269 printf(" - erase list = ");
270 for (i
= 0; i
< nerrs
; i
++)
271 printf(" %d", src_err_list
[i
]);
273 dump_u8xu8((u8
*) a
, m
, k
);
275 dump_u8xu8((u8
*) d
, k
, k
);
276 printf("orig data:\n");
277 dump_matrix(buffs
, m
, 25);
279 dump(buffs
[src_err_list
[i
]], 25);
280 printf("recov %d:", src_err_list
[i
]);
288 printf("done all: Pass\n");