Welcome to mirror list, hosted at ThFree Co, Russian Federation.

test_ldlt.cpp « fbuild « ode « dist « ode « extern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3b795b9ee7f0118121b241d4332419a5e7859c21 (plain)
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
294
295
296
297
298
299
/*************************************************************************
 *                                                                       *
 * Open Dynamics Engine, Copyright (C) 2001,2002 Russell L. Smith.       *
 * All rights reserved.  Email: russ@q12.org   Web: www.q12.org          *
 *                                                                       *
 * This library is free software; you can redistribute it and/or         *
 * modify it under the terms of EITHER:                                  *
 *   (1) The GNU Lesser General Public License as published by the Free  *
 *       Software Foundation; either version 2.1 of the License, or (at  *
 *       your option) any later version. The text of the GNU Lesser      *
 *       General Public License is included with this library in the     *
 *       file LICENSE.TXT.                                               *
 *   (2) The BSD-style license that is included with this library in     *
 *       the file LICENSE-BSD.TXT.                                       *
 *                                                                       *
 * This library 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 files    *
 * LICENSE.TXT and LICENSE-BSD.TXT for more details.                     *
 *                                                                       *
 *************************************************************************/

#include <stdio.h>
#include <malloc.h>
#include "ode/ode.h"

#define ALLOCA dALLOCA16

//****************************************************************************
// constants

#ifdef dSINGLE
#define TOL (1e-4)
#else
#define TOL (1e-10)
#endif

//****************************************************************************
// test L*X=B solver accuracy.

void testSolverAccuracy (int n)
{
  int i;
  int npad = dPAD(n);
  dReal *L = (dReal*) ALLOCA (n*npad*sizeof(dReal));
  dReal *B = (dReal*) ALLOCA (n*sizeof(dReal));
  dReal *B2 = (dReal*) ALLOCA (n*sizeof(dReal));
  dReal *X = (dReal*) ALLOCA (n*sizeof(dReal));

  // L is a random lower triangular matrix with 1's on the diagonal
  dMakeRandomMatrix (L,n,n,1.0);
  dClearUpperTriangle (L,n);
  for (i=0; i<n; i++) L[i*npad+i] = 1;

  // B is the right hand side
  dMakeRandomMatrix (B,n,1,1.0);
  memcpy (X,B,n*sizeof(dReal));	// copy B to X

  dSolveL1 (L,X,n,npad);

  /*
  dPrintMatrix (L,n,n);
  printf ("\n");
  dPrintMatrix (B,n,1);
  printf ("\n");
  dPrintMatrix (X,n,1);
  printf ("\n");
  */

  dSetZero (B2,n);
  dMultiply0 (B2,L,X,n,n,1);
  dReal error = dMaxDifference (B,B2,1,n);
  if (error > TOL) {
    printf ("error = %e, size = %d\n",error,n);
  }
}

//****************************************************************************
// test L^T*X=B solver accuracy.

void testTransposeSolverAccuracy (int n)
{
  int i;
  int npad = dPAD(n);
  dReal *L = (dReal*) ALLOCA (n*npad*sizeof(dReal));
  dReal *B = (dReal*) ALLOCA (n*sizeof(dReal));
  dReal *B2 = (dReal*) ALLOCA (n*sizeof(dReal));
  dReal *X = (dReal*) ALLOCA (n*sizeof(dReal));

  // L is a random lower triangular matrix with 1's on the diagonal
  dMakeRandomMatrix (L,n,n,1.0);
  dClearUpperTriangle (L,n);
  for (i=0; i<n; i++) L[i*npad+i] = 1;

  // B is the right hand side
  dMakeRandomMatrix (B,n,1,1.0);
  memcpy (X,B,n*sizeof(dReal));	// copy B to X

  dSolveL1T (L,X,n,npad);

  dSetZero (B2,n);
  dMultiply1 (B2,L,X,n,n,1);
  dReal error = dMaxDifference (B,B2,1,n);
  if (error > TOL) {
    printf ("error = %e, size = %d\n",error,n);
  }
}

//****************************************************************************
// test L*D*L' factorizer accuracy.

void testLDLTAccuracy (int n)
{
  int i,j;
  int npad = dPAD(n);
  dReal *A = (dReal*) ALLOCA (n*npad*sizeof(dReal));
  dReal *L = (dReal*) ALLOCA (n*npad*sizeof(dReal));
  dReal *d = (dReal*) ALLOCA (n*sizeof(dReal));
  dReal *Atest = (dReal*) ALLOCA (n*npad*sizeof(dReal));
  dReal *DL = (dReal*) ALLOCA (n*npad*sizeof(dReal));

  dMakeRandomMatrix (A,n,n,1.0);
  dMultiply2 (L,A,A,n,n,n);
  memcpy (A,L,n*npad*sizeof(dReal));
  dSetZero (d,n);

  dFactorLDLT (L,d,n,npad);

  // make L lower triangular, and convert d into diagonal of D
  dClearUpperTriangle (L,n);
  for (i=0; i<n; i++) L[i*npad+i] = 1;
  for (i=0; i<n; i++) d[i] = 1.0/d[i];

  // form Atest = L*D*L'
  dSetZero (Atest,n*npad);
  dSetZero (DL,n*npad);
  for (i=0; i<n; i++) {
    for (j=0; j<n; j++) DL[i*npad+j] = L[i*npad+j] * d[j];
  }
  dMultiply2 (Atest,L,DL,n,n,n);
  dReal error = dMaxDifference (A,Atest,n,n);
  if (error > TOL) {
    printf ("error = %e, size = %d\n",error,n);
  }

  /*
  printf ("\n");
  dPrintMatrix (A,n,n);
  printf ("\n");
  dPrintMatrix (L,n,n);
  printf ("\n");
  dPrintMatrix (d,1,n);
  */
}

//****************************************************************************
// test L*D*L' factorizer speed.

void testLDLTSpeed (int n)
{
  int npad = dPAD(n);

  // allocate A
  dReal *A = (dReal*) ALLOCA (n*npad*sizeof(dReal));

  // make B a symmetric positive definite matrix
  dMakeRandomMatrix (A,n,n,1.0);
  dReal *B = (dReal*) ALLOCA (n*npad*sizeof(dReal));
  dSetZero (B,n*npad);
  dMultiply2 (B,A,A,n,n,n);

  // make d
  dReal *d = (dReal*) ALLOCA (n*sizeof(dReal));
  dSetZero (d,n);

  // time several factorizations, return the minimum timing
  double mintime = 1e100;
  dStopwatch sw;
  for (int i=0; i<100; i++) {
    memcpy (A,B,n*npad*sizeof(dReal));
    dStopwatchReset (&sw);
    dStopwatchStart (&sw);

    dFactorLDLT (A,d,n,npad);

    dStopwatchStop (&sw);
    double time = dStopwatchTime (&sw);
    if (time < mintime) mintime = time;
  }

  printf ("%.0f",mintime * dTimerTicksPerSecond());
}

//****************************************************************************
// test solver speed.

void testSolverSpeed (int n, int transpose)
{
  int i;
  int npad = dPAD(n);

  // allocate L,B,X
  dReal *L = (dReal*) ALLOCA (n*npad*sizeof(dReal));
  dReal *B = (dReal*) ALLOCA (n*sizeof(dReal));
  dReal *X = (dReal*) ALLOCA (n*sizeof(dReal));

  // L is a random lower triangular matrix with 1's on the diagonal
  dMakeRandomMatrix (L,n,n,1.0);
  dClearUpperTriangle (L,n);
  for (i=0; i<n; i++) L[i*npad+i] = 1;

  // B is the right hand side
  dMakeRandomMatrix (B,n,1,1.0);

  // time several factorizations, return the minimum timing
  double mintime = 1e100;
  dStopwatch sw;
  for (int i=0; i<100; i++) {
    memcpy (X,B,n*sizeof(dReal));	// copy B to X

    if (transpose) {
      dStopwatchReset (&sw);
      dStopwatchStart (&sw);
      dSolveL1T (L,X,n,npad);
      dStopwatchStop (&sw);
    }
    else {
      dStopwatchReset (&sw);
      dStopwatchStart (&sw);
      dSolveL1 (L,X,n,npad);
      dStopwatchStop (&sw);
    }

    double time = dStopwatchTime (&sw);
    if (time < mintime) mintime = time;
  }

  printf ("%.0f",mintime * dTimerTicksPerSecond());
}

//****************************************************************************
// the single command line argument is 'f' to test and time the factorizer,
// or 's' to test and time the solver.


void testAccuracy (int n, char type)
{
  if (type == 'f') testLDLTAccuracy (n);
  if (type == 's') testSolverAccuracy (n);
  if (type == 't') testTransposeSolverAccuracy (n);
}


void testSpeed (int n, char type)
{
  if (type == 'f') testLDLTSpeed (n);
  if (type == 's') testSolverSpeed (n,0);
  if (type == 't') testSolverSpeed (n,1);
}


int main (int argc, char **argv)
{
  if (argc != 2 || argv[1][0] == 0 || argv[1][1] != 0 ||
      (argv[1][0] != 'f' && argv[1][0] != 's' && argv[1][0] != 't')) {
    fprintf (stderr,"Usage: test_ldlt [f|s|t]\n");
    exit (1);
  }
  char type = argv[1][0];

  // accuracy test: test all sizes up to 20 then all prime sizes up to 101
  int i;
  for (i=1; i<20; i++) {
    testAccuracy (i,type);
  }
  testAccuracy (23,type);
  testAccuracy (29,type);
  testAccuracy (31,type);
  testAccuracy (37,type);
  testAccuracy (41,type);
  testAccuracy (43,type);
  testAccuracy (47,type);
  testAccuracy (53,type);
  testAccuracy (59,type);
  testAccuracy (61,type);
  testAccuracy (67,type);
  testAccuracy (71,type);
  testAccuracy (73,type);
  testAccuracy (79,type);
  testAccuracy (83,type);
  testAccuracy (89,type);
  testAccuracy (97,type);
  testAccuracy (101,type);

  // test speed on a 127x127 matrix
  testSpeed (127,type);

  return 0;
}