int test_trsm_upper_right (int m, int n, int offset, const char* description){ printf("upper_right: %s m: %4d n: %4d offset: %4d ... ",description, m, n, offset); mzd_t* Ubase = mzd_init (2048,2048); mzd_t* Bbase = mzd_init (2048,2048); mzd_randomize (Ubase); mzd_randomize (Bbase); mzd_t* Bbasecopy = mzd_copy (NULL, Bbase); mzd_t* U = mzd_init_window (Ubase, 0, offset, n, offset + n); mzd_t* B = mzd_init_window (Bbase, 0, offset, m, offset + n); mzd_t* W = mzd_copy (NULL, B); size_t i,j; for (i=0; i<n; ++i){ for (j=0; j<i;++j) mzd_write_bit(U,i,j, 0); mzd_write_bit(U,i,i, 1); } mzd_trsm_upper_right (U, B, 2048); mzd_addmul(W, B, U, 2048); int status = 0; for ( i=0; i<m; ++i) for ( j=0; j<n; ++j){ if (mzd_read_bit (W,i,j)){ status = 1; } } // Verifiying that nothing has been changed around the submatrices mzd_addmul(W, B, U, 2048); mzd_copy (B, W); for ( i=0; i<2048; ++i) for ( j=0; j<2048/RADIX; ++j){ if (Bbase->rows[i][j] != Bbasecopy->rows[i][j]){ status = 1; } } mzd_free_window (U); mzd_free_window (B); mzd_free (W); mzd_free(Ubase); mzd_free(Bbase); mzd_free(Bbasecopy); if (!status) printf("passed\n"); else printf("FAILED\n"); return status; }
int test_trsm_lower_left (int m, int n, int offsetL, int offsetB){ mzd_t* Lbase = mzd_init (2048,2048); mzd_t* Bbase = mzd_init (2048,2048); mzd_randomize (Lbase); mzd_randomize (Bbase); mzd_t* Bbasecopy = mzd_copy (NULL, Bbase); mzd_t* L = mzd_init_window (Lbase, 0, offsetL, m, offsetL + m); mzd_t* B = mzd_init_window (Bbase, 0, offsetB, m, offsetB + n); mzd_t* W = mzd_copy (NULL, B); size_t i,j; for (i=0; i<m; ++i){ for (j=i+1; j<m;++j) mzd_write_bit(L,i,j, 0); mzd_write_bit(L,i,i, 1); } mzd_trsm_lower_left(L, B, 2048); mzd_addmul(W, L, B, 2048); int status = 0; for ( i=0; i<m; ++i) for ( j=0; j<n; ++j){ if (mzd_read_bit (W,i,j)){ status = 1; } } // Verifiying that nothing has been changed around the submatrices mzd_addmul(W, L, B, 2048); mzd_copy (B, W); for ( i=0; i<2048; ++i) for ( j=0; j<2048/RADIX; ++j){ if (Bbase->rows[i][j] != Bbasecopy->rows[i][j]){ status = 1; } } mzd_free_window (L); mzd_free_window (B); mzd_free_window (W); mzd_free(Lbase); mzd_free(Bbase); mzd_free(Bbasecopy); if (!status) printf(" ... passed\n"); else printf(" ... FAILED\n"); return status; }
int test_pluq_structured(rci_t m, rci_t n) { printf("pluq: testing structured m: %5d, n: %5d", m, n); mzd_t* A = mzd_init(m, n); mzd_t* L = mzd_init(m, m); mzd_t* U = mzd_init(m, n); for(rci_t i = 0; i < m; i += 2) for (rci_t j = i; j < n; ++j) mzd_write_bit(A, i, j, 1); mzd_t* Acopy = mzd_copy (NULL,A); mzp_t* P = mzp_init(m); mzp_t* Q = mzp_init(n); rci_t r = mzd_pluq(A, P, Q, 0); printf(", rank: %5d ",r); for (rci_t i = 0; i < r; ++i){ for (rci_t j = 0; j < i; ++j) mzd_write_bit(L, i, j, mzd_read_bit(A,i,j)); for (rci_t j = i + 1; j < n; ++j) mzd_write_bit(U, i, j, mzd_read_bit(A,i,j)); } for (rci_t i = r; i < m; ++i) for (rci_t j = 0; j < r; ++j) mzd_write_bit(L, i, j, mzd_read_bit(A,i,j)); for (rci_t i = 0; i < r; ++i){ mzd_write_bit(L,i,i, 1); mzd_write_bit(U,i,i, 1); } mzd_apply_p_left(Acopy, P); mzd_apply_p_right_trans(Acopy, Q); mzd_addmul(Acopy, L, U, 0); int status = 0; for (rci_t i = 0; i < m; ++i) for (rci_t j = 0; j < n; ++j){ if (mzd_read_bit (Acopy,i,j)){ status = 1; break; } } if (status) { printf("\n"); printf(" ... FAILED\n"); } else printf (" ... passed\n"); mzd_free(U); mzd_free(L); mzd_free(A); mzd_free(Acopy); mzp_free(P); mzp_free(Q); return status; }
int addmul_test_equality(rci_t m, rci_t l, rci_t n, int k, int cutoff) { int ret = 0; mzd_t *A, *B, *C, *D, *E, *F; printf("addmul: m: %4d, l: %4d, n: %4d, k: %2d, cutoff: %4d", m, l, n, k, cutoff); /* we create two random matrices */ A = mzd_init(m, l); B = mzd_init(l, n); C = mzd_init(m, n); mzd_randomize(A); mzd_randomize(B); mzd_randomize(C); /* D = C + A*B via M4RM, temporary buffers are managed internally */ D = mzd_copy(NULL, C); D = mzd_addmul_m4rm(D, A, B, k); /* E = C + A*B via naiv cubic multiplication */ E = mzd_mul_m4rm(NULL, A, B, k); mzd_add(E, E, C); /* F = C + A*B via naiv cubic multiplication */ F = mzd_copy(NULL, C); F = mzd_addmul(F, A, B, cutoff); mzd_free(A); mzd_free(B); mzd_free(C); if (mzd_equal(D, E) != TRUE) { printf(" M4RM != add,mul"); ret -=1; } if (mzd_equal(E, F) != TRUE) { printf(" add,mul = addmul"); ret -=1; } if (mzd_equal(F, D) != TRUE) { printf(" M4RM != addmul"); ret -=1; } if (ret==0) printf(" ... passed\n"); else printf(" ... FAILED\n"); mzd_free(D); mzd_free(E); mzd_free(F); return ret; }
int _mzd_pluq_solve_left(mzd_t const *A, rci_t rank, mzp_t const *P, mzp_t const *Q, mzd_t *B, int const cutoff, int const inconsistency_check) { /** A is supposed to store L lower triangular and U upper triangular * B is modified in place * (Bi's in the comments are just modified versions of B) * PLUQ = A * 1) P B2 = B1 * 2) L B3 = B2 * 3) U B4 = B3 * 4) Q B5 = B4 */ int retval = 0; /* P B2 = B1 or B2 = P^T B1 */ mzd_apply_p_left(B, P); /* L B3 = B2 */ /* view on the upper part of L */ mzd_t const *LU = mzd_init_window_const(A, 0, 0, rank, rank); mzd_t *Y1 = mzd_init_window(B, 0, 0, rank, B->ncols); mzd_trsm_lower_left(LU, Y1, cutoff); if (inconsistency_check) { /* Check for inconsistency */ /** FASTER without this check; update with the lower part of L */ mzd_t const *H = mzd_init_window_const(A, rank, 0, A->nrows, rank); mzd_t *Y2 = mzd_init_window(B, rank, 0, A->nrows, B->ncols); if(A->nrows < B->nrows) { mzd_t *Y3 = mzd_init_window(B, A->nrows, 0, B->nrows, B->ncols); mzd_set_ui(Y3, 0); mzd_free_window(Y3); } mzd_addmul(Y2, H, Y1, cutoff); /* * test whether Y2 is the zero matrix */ if(!mzd_is_zero(Y2)) { retval = -1; } mzd_free_window((mzd_t*)H); mzd_free_window(Y2); } /* U B4 = B3 */ mzd_trsm_upper_left(LU, Y1, cutoff); mzd_free_window((mzd_t*)LU); mzd_free_window(Y1); if (!inconsistency_check) { /** Default is to set the undefined bits to zero if inconsistency * has been checked then Y2 bits are already all zeroes thus this * clearing is not needed */ for(rci_t i = rank; i < B->nrows; ++i) { for(rci_t j = 0; j < B->ncols; j += m4ri_radix) { mzd_clear_bits(B, i, j, MIN(m4ri_radix, B->ncols - j)); } } } /* Q B5 = B4 or B5 = Q^T B4 */ mzd_apply_p_left_trans(B, Q); /* P L U Q B5 = B1 */ __M4RI_DD_MZD(B); __M4RI_DD_INT(retval); return retval; }
int test_lqup_half_rank(size_t m, size_t n) { printf("pluq: testing half rank m: %5zd, n: %5zd",m,n); mzd_t* U = mzd_init(m, n); mzd_t* L = mzd_init(m, m); mzd_t* U2 = mzd_init(m, n); mzd_t* L2 = mzd_init(m, m); mzd_t* A = mzd_init(m, n); mzd_randomize (U); mzd_randomize (L); size_t i,j; for (i=0; i<m && i<n; ++i){ mzd_write_bit(U,i,i, 1); for (j=0; j<i;++j) mzd_write_bit(U,i,j, 0); if (i%2) for (j=i; j<n;++j) mzd_write_bit(U,i,j, 0); for (j=i+1; j<m;++j) mzd_write_bit(L,i,j, 0); mzd_write_bit(L,i,i, 1); } mzd_mul(A, L, U, 0); mzd_t* Acopy = mzd_copy (NULL,A); mzp_t* Pt = mzp_init(m); mzp_t* Q = mzp_init(n); int r = mzd_pluq(A, Pt, Q, 0); for (i=0; i<r; ++i){ for (j=0; j<i;++j) mzd_write_bit (L2, i, j, mzd_read_bit(A,i,j)); for (j=i+1; j<n;++j) mzd_write_bit (U2, i, j, mzd_read_bit(A,i,j)); } for (i=r; i<m; i++) for (j=0; j<r;++j) mzd_write_bit (L2, i, j, mzd_read_bit(A,i,j)); for (i=0; i<r; ++i){ mzd_write_bit(L2,i,i, 1); mzd_write_bit(U2,i,i, 1); } mzd_apply_p_left(Acopy, Pt); mzd_apply_p_right_trans(Acopy, Q); mzd_addmul(Acopy,L2,U2,0); int status = 0; for ( i=0; i<m; ++i) { for ( j=0; j<n; ++j){ if (mzd_read_bit(Acopy,i,j)){ status = 1; } } if(status) break; } if (status) printf(" ... FAILED\n"); else printf (" ... passed\n"); mzd_free(U); mzd_free(L); mzd_free(U2); mzd_free(L2); mzd_free(A); mzd_free(Acopy); mzp_free(Pt); mzp_free(Q); return status; }
int test_lqup_full_rank (size_t m, size_t n){ printf("pluq: testing full rank m: %5zu, n: %5zu",m,n); mzd_t* U = mzd_init (m,n); mzd_t* L = mzd_init (m,m); mzd_t* U2 = mzd_init (m,n); mzd_t* L2 = mzd_init (m,m); mzd_t* A = mzd_init (m,n); mzd_randomize (U); mzd_randomize (L); size_t i,j; for (i=0; i<m; ++i){ for (j=0; j<i && j<n;++j) mzd_write_bit(U,i,j, 0); for (j=i+1; j<m;++j) mzd_write_bit(L,i,j, 0); if(i<n) mzd_write_bit(U,i,i, 1); mzd_write_bit(L,i,i, 1); } mzd_mul(A, L, U, 2048); mzd_t* Acopy = mzd_copy (NULL,A); mzp_t* P = mzp_init(m); mzp_t* Q = mzp_init(n); mzd_pluq(A, P, Q, 2048); for (i=0; i<m; ++i){ for (j=0; j<i && j <n;++j) mzd_write_bit (L2, i, j, mzd_read_bit(A,i,j)); for (j=i+1; j<n;++j) mzd_write_bit (U2, i, j, mzd_read_bit(A,i,j)); } for (i=0; i<n && i<m; ++i){ mzd_write_bit(L2,i,i, 1); mzd_write_bit(U2,i,i, 1); } mzd_addmul(Acopy,L2,U2,0); int status = 0; for ( i=0; i<m; ++i) for ( j=0; j<n; ++j){ if (mzd_read_bit (Acopy,i,j)){ status = 1; } } if (status){ printf(" ... FAILED\n"); } else printf (" ... passed\n"); mzd_free(U); mzd_free(L); mzd_free(U2); mzd_free(L2); mzd_free(A); mzd_free(Acopy); mzp_free(P); mzp_free(Q); return status; }
int smallops_test_add(rci_t M, rci_t N, rci_t m, rci_t n, rci_t offset, word pattern) { int ret = 0; printf(" mzd_add: M: %4d, N: %4d, m: %4d, n: %4d, offset: %4d, pattern: 0x%" PRIx64 " ", M, N, m, n, offset, pattern); mzd_t *AA; mzd_t *A = mzd_init_test_matrix_random(M, N, m, n, offset, pattern, &AA); mzd_t *BB; mzd_t *B = mzd_init_test_matrix_random(M, N, m, n, offset, pattern, &BB); mzd_t *CC; mzd_t *C = mzd_init_test_matrix_random(M, N, m, n, offset, pattern, &CC); mzd_t *DD; mzd_t *D = mzd_init_test_matrix_random(M, N, m, n, offset, pattern, &DD); /* Creation went okay? */ ret += mzd_check_pattern(AA, m, n, offset, pattern); ret += mzd_check_pattern(BB, m, n, offset, pattern); ret += mzd_check_pattern(CC, m, n, offset, pattern); ret += mzd_check_pattern(DD, m, n, offset, pattern); /* Testing equality A+A == 0 */ mzd_add(C, A, A); if(!mzd_is_zero(C)) { ret +=1; } ret += mzd_check_pattern(AA, m, n, offset, pattern); ret += mzd_check_pattern(BB, m, n, offset, pattern); ret += mzd_check_pattern(CC, m, n, offset, pattern); /* Testing equality A+A == 0 but this time C is already zero */ mzd_add(C, B, B); if(!mzd_is_zero(C)) { ret +=1; } ret += mzd_check_pattern(AA, m, n, offset, pattern); ret += mzd_check_pattern(BB, m, n, offset, pattern); ret += mzd_check_pattern(CC, m, n, offset, pattern); /* Testing in place add. C is zero, so afterwards C == A */ mzd_add(C, C, A); if(!mzd_equal(C,A)) { ret +=1; } ret += mzd_check_pattern(AA, m, n, offset, pattern); ret += mzd_check_pattern(BB, m, n, offset, pattern); ret += mzd_check_pattern(CC, m, n, offset, pattern); /* Testing equality C (== A) + A == 0 */ mzd_add(B, C, A); if(!mzd_is_zero(B)) { ret +=1; } if(m == n) { /* Testing equality (A + B)^2 == A^2 + BA + AB + B^2 */ mzd_randomize(A); mzd_randomize(B); mzd_add(C,A,B); mzd_mul(D,C,C, 0); // (A+B)^2 mzd_mul(C,A,A, 0); mzd_addmul(C, B, A, 0); mzd_addmul(C, A, B, 0); mzd_addmul(C, B, B, 0); if(!mzd_equal(C,D)) { ret += 1; } ret += mzd_check_pattern(AA, m, n, offset, pattern); ret += mzd_check_pattern(BB, m, n, offset, pattern); ret += mzd_check_pattern(CC, m, n, offset, pattern); ret += mzd_check_pattern(DD, m, n, offset, pattern); } mzd_free_test_matrix_random(AA, A); mzd_free_test_matrix_random(BB, B); mzd_free_test_matrix_random(CC, C); mzd_free_test_matrix_random(DD, D); if(ret == 0) { printf(" ... passed\n"); } else { printf(" ... FAILED\n"); } #ifdef ABORT_ON_FAIL if (ret) abort(); #endif return ret; }