File: ple.c

package info (click to toggle)
libm4ri 20200125-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid, trixie
  • size: 2,560 kB
  • sloc: ansic: 12,633; sh: 4,304; makefile: 137
file content (293 lines) | stat: -rw-r--r-- 7,860 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*******************************************************************
*
*                 M4RI: Linear Algebra over GF(2)
*
*    Copyright (C) 2008 Clement Pernet <clement.pernet@gmail.com>
*
*  Distributed under the terms of the GNU General Public License (GPL)
*  version 2 or higher.
*
*    This code is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
*    General Public License for more details.
*
*  The full text of the GPL is available at:
*
*                  http://www.gnu.org/licenses/
*
********************************************************************/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdio.h>
#include "mzd.h"
#include "triangular.h"
#include "parity.h"
#include "ple_russian.h"
#include "strassen.h"
#include "ple.h"

rci_t mzd_ple(mzd_t *A, mzp_t *P, mzp_t *Q, int const cutoff) {
  if (P->length != A->nrows)
    m4ri_die("mzd_ple: Permutation P length (%d) must match A nrows (%d)\n", P->length, A->nrows);
  if (Q->length != A->ncols)
    m4ri_die("mzd_ple: Permutation Q length (%d) must match A ncols (%d)\n", Q->length, A->ncols);
  return _mzd_ple(A, P, Q, cutoff);
}


rci_t mzd_pluq (mzd_t *A, mzp_t *P, mzp_t *Q, int const cutoff) {
  if (P->length != A->nrows)
    m4ri_die("mzd_pluq: Permutation P length (%d) must match A nrows (%d)\n", P->length, A->nrows);
  if (Q->length != A->ncols)
    m4ri_die("mzd_pluq: Permutation Q length (%d) must match A ncols (%d)\n", Q->length, A->ncols);
  rci_t r = _mzd_pluq(A, P, Q, cutoff);
  return r;
}


rci_t _mzd_pluq(mzd_t *A, mzp_t *P, mzp_t *Q, int const cutoff) {
  rci_t r = _mzd_ple(A, P, Q, cutoff);
  if(r && r < A->nrows) {
    mzd_t *A0 = mzd_init_window(A, 0, 0, r, A->ncols);
    mzd_apply_p_right_trans_tri(A0, Q);
    mzd_free_window(A0);
  } else {
    mzd_apply_p_right_trans_tri(A, Q);
  }
  return r;
}

rci_t _mzd_ple(mzd_t *A, mzp_t *P, mzp_t *Q, int const cutoff) {
  rci_t ncols = A->ncols;

#if 1
  rci_t nrows = mzd_first_zero_row(A);
  for(rci_t i = nrows; i < A->nrows; ++i)
    P->values[i] = i;
  for(rci_t i = 0; i < A->ncols; ++i)
    Q->values[i] = i;
  if(!nrows) {
    return 0;
  }
#else
  rci_t nrows = A->nrows;
#endif

  if (ncols <= m4ri_radix || A->width * A->nrows <= __M4RI_PLE_CUTOFF) {
    /* this improves data locality and runtime considerably */
    mzd_t *Abar = mzd_copy(NULL, A);
    rci_t r = _mzd_ple_russian(Abar, P, Q, 0);
    mzd_copy(A, Abar);
    mzd_free(Abar);
    return r;
  }

  {
    /* Block divide and conquer algorithm */
    
    /*                     n1
     *   ------------------------------------------
     *   | A0              | A1                   |
     *   |                 |                      |
     *   |                 |                      |
     *   |                 |                      |
     *   ------------------------------------------
     */

    rci_t n1 = (((ncols - 1) / m4ri_radix + 1) >> 1) * m4ri_radix;

    mzd_t *A0  = mzd_init_window(A,  0,  0, nrows,    n1);
    mzd_t *A1  = mzd_init_window(A,  0, n1, nrows, ncols);

    /* First recursive call */

    mzp_t *P1 = mzp_init_window(P, 0, nrows);
    mzp_t *Q1 = mzp_init_window(Q, 0, A0->ncols);
    rci_t r1 = _mzd_ple(A0, P1, Q1, cutoff);

    /*           r1           n1
     *   ------------------------------------------
     *   | A00    |           | A01               |
     *   |        |           |                   |
     * r1------------------------------------------ 
     * 
     *   | A10    |           | A11               |
     *   |        |           |                   |
     *   ------------------------------------------
     */

    mzd_t *A00 = mzd_init_window(A,  0,  0, r1, r1);
    mzd_t *A10 = mzd_init_window(A, r1,  0, nrows, r1);
    mzd_t *A01 = mzd_init_window(A,  0, n1, r1, ncols);
    mzd_t *A11 = mzd_init_window(A, r1, n1, nrows, ncols);


    if (r1) {
      /* Computation of the Schur complement */
      mzd_apply_p_left(A1, P1);
      _mzd_trsm_lower_left(A00, A01, cutoff);
      mzd_addmul(A11, A10, A01, cutoff);
    }
    mzp_free_window(P1);
    mzp_free_window(Q1);

    /* Second recursive call */
    mzp_t *P2 = mzp_init_window(P, r1, nrows);
    mzp_t *Q2 = mzp_init_window(Q, n1, ncols);

    rci_t r2 = _mzd_ple(A11, P2, Q2, cutoff);

    /*           n
     *   -------------------
     *   |      A0b        |
     *   |                 |
     *   r1-----------------
     *   |      A1b        |
     *   |                 |
     *   -------------------
     */

    /* Update A10 */
    mzd_apply_p_left(A10, P2);

    /* Update P */
    for (rci_t i = 0; i < nrows - r1; ++i)
      P2->values[i] += r1;
    
    // Update the A0b block (permutation + rotation)
    for(rci_t i = 0, j = n1; j < ncols; ++i, ++j)
      Q2->values[i] += n1;
    
    for(rci_t i = n1, j = r1; i < n1 + r2; ++i, ++j)
      Q->values[j] = Q->values[i];

    /* Compressing L */

    _mzd_compress_l(A, r1, n1, r2);

    mzp_free_window(Q2);
    mzp_free_window(P2);

    mzd_free_window(A0);
    mzd_free_window(A1);
    mzd_free_window(A00);
    mzd_free_window(A01);
    mzd_free_window(A10);
    mzd_free_window(A11);

    __M4RI_DD_MZD(A);
    __M4RI_DD_MZP(P);
    __M4RI_DD_MZP(Q);
    __M4RI_DD_RCI(r1 + r2);
    return r1 + r2;
  }
}

rci_t _mzd_pluq_naive(mzd_t *A, mzp_t *P, mzp_t *Q)  {
  rci_t curr_pos = 0;
  for (curr_pos = 0; curr_pos < A->ncols; ) {
    int found = 0;
    /* search for some pivot */
    rci_t i, j;
    for (j = curr_pos; j < A->ncols; ++j) {
      for (i = curr_pos; i< A->nrows; ++i ) {
	if (mzd_read_bit(A, i, j)) {
          found = 1;
          break;
        }
      }
      if(found)
        break;
    }
    
    if(found) {
      P->values[curr_pos] = i;
      Q->values[curr_pos] = j;
      mzd_row_swap(A, curr_pos, i);
      mzd_col_swap(A, curr_pos, j);
          
      /* clear below but preserve transformation matrix */
      if (curr_pos +1 < A->ncols){
	for(rci_t l = curr_pos + 1; l < A->nrows; ++l) {
	  if (mzd_read_bit(A, l, curr_pos)) {
	    mzd_row_add_offset(A, l, curr_pos, curr_pos + 1);
	  }
	}
      }
      ++curr_pos;
    } else {
      break;
    }
  }
  for (rci_t i = curr_pos; i < A->nrows; ++i)
    P->values[i] = i;
  for (rci_t i = curr_pos; i < A->ncols; ++i)
    Q->values[i] = i;

  __M4RI_DD_MZD(A);
  __M4RI_DD_MZP(P);
  __M4RI_DD_MZP(Q);
  __M4RI_DD_RCI(curr_pos);
  return curr_pos;
}
 
rci_t _mzd_ple_naive(mzd_t *A, mzp_t *P, mzp_t *Q)  {
  rci_t col_pos = 0;
  rci_t row_pos = 0;
  /* search for some pivot */
  while (row_pos < A->nrows && col_pos < A->ncols) {
    int found = 0;
    rci_t i, j;
    for (j = col_pos; j < A->ncols; ++j) {
      for (i = row_pos; i < A->nrows; ++i) {
	if (mzd_read_bit(A, i, j)) {
          found = 1;
          break;
        }
      }
      if(found)
        break;
    }
    if(found) {
      P->values[row_pos] = i;
      Q->values[row_pos] = j;
      mzd_row_swap(A, row_pos, i);
      //mzd_col_swap(A, curr_pos, j);
          
      /* clear below but preserve transformation matrix */
      if (j + 1 < A->ncols){
	for(rci_t l = row_pos + 1; l < A->nrows; ++l) {
	  if (mzd_read_bit(A, l, j)) {
	    mzd_row_add_offset(A, l, row_pos, j + 1);
	  }
	}
      }
      ++row_pos;
      col_pos = j + 1;
    } else {
      break;
    }
  }
  for (rci_t i = row_pos; i < A->nrows; ++i)
    P->values[i] = i;
  for (rci_t i = row_pos; i < A->ncols; ++i)
    Q->values[i] = i;

  /* Now compressing L */
  for (rci_t j = 0; j < row_pos; ++j){
    if (Q->values[j] > j) {
      // To be optimized by a copy_row function
      mzd_col_swap_in_rows (A,Q->values[j], j, j, A->nrows);
    }
  }

  __M4RI_DD_MZD(A);
  __M4RI_DD_MZP(P);
  __M4RI_DD_MZP(Q);
  __M4RI_DD_RCI(row_pos);
  return row_pos;
}