File: ldlmain.c

package info (click to toggle)
ufsparse 1.2-7
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 27,536 kB
  • ctags: 5,848
  • sloc: ansic: 89,328; makefile: 4,721; fortran: 1,991; csh: 207; sed: 162; awk: 33; java: 30; sh: 8
file content (387 lines) | stat: -rw-r--r-- 12,238 bytes parent folder | download
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/* ========================================================================== */
/* === ldlmain.c: LDL main program, for demo and testing ==================== */
/* ========================================================================== */

/* LDLMAIN:  this main program has two purposes.  It provides an example of how
 * to use the LDL routines, and it tests the package.  The output of this
 * program is in ldlmain.out (without AMD) and ldlamd.out (with AMD).  If you
 * did not download and install AMD, then the compilation of this program will
 * not work with USE_AMD defined.  Compare your output with ldlmain.out and
 * ldlamd.out.
 *
 * The program reads in a set of matrices, in the Matrix/ subdirectory.
 * The format of the files is as follows:
 *
 *	one line with the matrix description
 *	one line with 2 integers: n jumbled
 *	n+1 lines, containing the Ap array of size n+1 (column pointers)
 *	nz lines, containing the Ai array of size nz (row indices)
 *	nz lines, containing the Ax array of size nz (numerical values)
 *	n lines, containing the P permutation array of size n
 *
 * The Ap, Ai, Ax, and P data structures are described in ldl.c.
 * The jumbled flag is 1 if the matrix could contain unsorted columns and/or
 * duplicate entries, and 0 otherwise.  It is only used to determine whether or
 * not AMD can be called, since AMD does not work for jumbled matrices (but LDL
 * does).
 *
 * Once the matrix is read in, it is checked to see if it is valid.  Some
 * matrices are invalid by design, to test the error-checking routines.  If
 * valid, the matrix factorized twice (A and P*A*P').  A linear
 * system Ax=b is set up and solved, and the residual computed.
 * If any system is not solved accurately, this test will fail.
 *
 * This program can also be compiled as a MATLAB mexFunction, with the command
 * "mex ldlmain.c ldl.c".  You can then run the program in MATLAB, with the
 * command "ldlmain".
 *
 * LDL Version 1.2, Copyright (c) 2005 by Timothy A Davis,
 * University of Florida.  All Rights Reserved.  See README for the License.
 */

#include <stdio.h>
#include <stdlib.h>
#include "ldl.h"

#define NMATRICES 30	    /* number of test matrices in Matrix/ directory */
#define LEN 200		    /* string length */

#ifdef USE_AMD		    /* get AMD include file, if using AMD */
#include "amd.h"
#define PROGRAM "ldlamd"
#else
#define PROGRAM "ldlmain"
#endif

#ifdef MATLAB_MEX_FILE
#include "mex.h"
#include "matrix.h"
#define EXIT_ERROR mexErrMsgTxt ("failure") ;
#define EXIT_OK
#else
#define EXIT_ERROR exit (EXIT_FAILURE) ;
#define EXIT_OK exit (EXIT_SUCCESS) ;
#endif

/* -------------------------------------------------------------------------- */
/* ALLOC_MEMORY: allocate a block of memory */
/* -------------------------------------------------------------------------- */

#define ALLOC_MEMORY(p,type,size) \
p = (type *) malloc ((((size) <= 0) ? 1 : (size)) * sizeof (type)) ; \
if (p == (type *) NULL) \
{ \
    printf (PROGRAM ": out of memory\n") ; \
    EXIT_ERROR ; \
}

/* -------------------------------------------------------------------------- */
/* FREE_MEMORY: free a block of memory */
/* -------------------------------------------------------------------------- */

#define FREE_MEMORY(p,type) \
if (p != (type *) NULL) \
{ \
    free (p) ; \
    p = (type *) NULL ; \
}

/* -------------------------------------------------------------------------- */
/* stand-alone main program, or MATLAB mexFunction */
/* -------------------------------------------------------------------------- */

#ifdef MATLAB_MEX_FILE
void mexFunction
(
    int	nargout,
    mxArray *pargout[ ],
    int	nargin,
    const mxArray *pargin[ ]
)
#else
int main (int argc, char **argv)
#endif
{

    /* ---------------------------------------------------------------------- */
    /* local variables */
    /* ---------------------------------------------------------------------- */

#ifdef USE_AMD
    double Info [AMD_INFO] ;
#endif
    double r, rnorm, flops, maxrnorm = 0. ;
    double *Ax, *Lx, *B, *D, *X, *Y ;
    int matrix, *Ai, *Ap, *Li, *Lp, *P, *Pinv, *Perm, *PermInv, n, i, j, p, nz,
	*Flag, *Pattern, *Lnz, *Parent, trial, lnz, d, jumbled ;
    FILE *f ;
    char s [LEN] ;

    /* ---------------------------------------------------------------------- */
    /* check the error-checking routines with null matrices */
    /* ---------------------------------------------------------------------- */

    i = 1 ;
    if (ldl_valid_perm (-1, (int *) NULL, &i)
	|| !ldl_valid_perm (0, (int *) NULL, &i)
	|| ldl_valid_matrix (-1, (int *) NULL, (int *) NULL)
	|| ldl_valid_matrix (0, &i, &i))
    {
	printf (PROGRAM ": ldl error-checking routine failed\n") ;
	EXIT_ERROR ;
    }

    /* ---------------------------------------------------------------------- */
    /* read in a factorize a set of matrices */
    /* ---------------------------------------------------------------------- */

    for (matrix = 1 ; matrix <= NMATRICES ; matrix++)
    {

	/* ------------------------------------------------------------------ */
	/* read in the matrix and the permutation */
	/* ------------------------------------------------------------------ */

	sprintf (s, "Matrix/A%02d", matrix) ;
	if ((f = fopen (s, "r")) == (FILE *) NULL)
	{
	    printf (PROGRAM ": could not open file: %s\n", s) ;
	    EXIT_ERROR ;
	}
	fgets (s, LEN, f) ;
	printf ("\n\n--------------------------------------------------------");
	printf ("\nInput matrix: %s", s) ;
	printf ("--------------------------------------------------------\n\n");
	fscanf (f, "%d %d", &n, &jumbled) ;
	n = (n < 0) ? (0) : (n) ;
	ALLOC_MEMORY (P, int, n) ;
	ALLOC_MEMORY (Ap, int, n+1) ;
	for (j = 0 ; j <= n ; j++)
	{
	    fscanf (f, "%d", &Ap [j]) ;
	}
	nz = Ap [n] ;
	ALLOC_MEMORY (Ai, int, nz) ;
	ALLOC_MEMORY (Ax, double, nz) ;
	for (p = 0 ; p < nz ; p++)
	{
	    fscanf (f, "%d" , &Ai [p]) ;
	}
	for (p = 0 ; p < nz ; p++)
	{
	    fscanf (f, "%lg", &Ax [p]) ;
	}
	for (j = 0 ; j < n  ; j++)
	{
	    fscanf (f, "%d" , &P  [j]) ;
	}
	fclose (f) ;

	/* ------------------------------------------------------------------ */
	/* check the matrix A and the permutation P */
	/* ------------------------------------------------------------------ */

	ALLOC_MEMORY (Flag, int, n) ;

	/* To test the error-checking routines, some of the input matrices
	 * are not valid.  So this error is expected to occur. */
	if (!ldl_valid_matrix (n, Ap, Ai) || !ldl_valid_perm (n, P, Flag))
	{
	    printf (PROGRAM ": invalid matrix and/or permutation\n") ;
	    FREE_MEMORY (P, int) ;
	    FREE_MEMORY (Ap, int) ;
	    FREE_MEMORY (Ai, int) ;
	    FREE_MEMORY (Ax, double) ;
	    FREE_MEMORY (Flag, int) ;
	    continue ;
	}

	/* ------------------------------------------------------------------ */
	/* get the AMD permutation, if available */
	/* ------------------------------------------------------------------ */

#ifdef USE_AMD
	if (!jumbled)
	{
	    /* recompute the permutation with AMD, but only with non-jumbled
	     * matrices, since AMD requires sorted columns and no duplicates.
	     * Assume that AMD produces a valid permutation P. */
	    if (amd_order (n, Ap, Ai, P, (double *) NULL, Info) != AMD_OK)
	    {
		printf (PROGRAM ": call to AMD failed\n") ;
		EXIT_ERROR ;
	    }
	    amd_control ((double *) NULL) ;
	    amd_info (Info) ;
	}
	else
	{
	    /* the matrix has duplicate entries, and unsorted columns */ 
	    printf ("Skipping call to AMD, since input matrix is jumbled;\n") ;
	    printf ("using permutation from input file instead.\n") ;
	}
#endif

	/* ------------------------------------------------------------------ */
	/* allocate workspace and the first part of LDL factorization */
	/* ------------------------------------------------------------------ */

	ALLOC_MEMORY (Pinv, int, n) ;
	ALLOC_MEMORY (Y, double, n) ;
	ALLOC_MEMORY (Pattern, int, n) ;
	ALLOC_MEMORY (Lnz, int, n) ;
	ALLOC_MEMORY (Lp, int, n+1) ;
	ALLOC_MEMORY (Parent, int, n) ;
	ALLOC_MEMORY (D, double, n) ;
	ALLOC_MEMORY (B, double, n) ;
	ALLOC_MEMORY (X, double, n) ;

	/* ------------------------------------------------------------------ */
	/* factorize twice, with and without permutation */
	/* ------------------------------------------------------------------ */

	for (trial = 1 ; trial <= 2 ; trial++)
	{

	    if (trial == 1)
	    {
		printf ("Factorize PAP'=LDL' and solve Ax=b\n") ;
		Perm = P ;
		PermInv = Pinv ;
	    }
	    else
	    {
		printf ("Factorize A=LDL' and solve Ax=b\n") ;
		Perm = (int *) NULL ;
		PermInv = (int *) NULL ;
	    }

	    /* -------------------------------------------------------------- */
	    /* symbolic factorization to get Lp, Parent, Lnz, and Pinv */
	    /* -------------------------------------------------------------- */

	    ldl_symbolic (n, Ap, Ai, Lp, Parent, Lnz, Flag, Perm, PermInv) ;
	    lnz = Lp [n] ;

	    /* find # of nonzeros in L, and flop count for ldl_numeric */
	    flops = 0 ;
	    for (j = 0 ; j < n ; j++)
	    {
		flops += ((double) Lnz [j]) * (Lnz [j] + 2) ;
	    }
	    printf ("Nz in L: %d  Flop count: %g\n", lnz, flops) ;

	    /* -------------------------------------------------------------- */
	    /* allocate remainder of L, of size lnz */
	    /* -------------------------------------------------------------- */

	    ALLOC_MEMORY (Li, int, lnz) ;
	    ALLOC_MEMORY (Lx, double, lnz) ;

	    /* -------------------------------------------------------------- */
	    /* numeric factorization to get Li, Lx, and D */
	    /* -------------------------------------------------------------- */

	    d = ldl_numeric (n, Ap, Ai, Ax, Lp, Parent, Lnz, Li, Lx, D,
		Y, Flag, Pattern, Perm, PermInv) ;

	    /* -------------------------------------------------------------- */
	    /* solve, or report singular case */
	    /* -------------------------------------------------------------- */

	    if (d != n)
	    {
		printf ("Ax=b not solved since D(%d,%d) is zero.\n", d, d) ;
	    }
	    else
	    {
		/* construct the right-hand-side, B */
		for (i = 0 ; i < n ; i++)
		{
		    B [i] = 1 + ((double) i) / 100 ;
		}

		/* solve Ax=b */
		if (trial == 1)
		{
		    /* the factorization is LDL' = PAP' */
		    ldl_perm (n, Y, B, P) ;			/* y = Pb */
		    ldl_lsolve (n, Y, Lp, Li, Lx) ;		/* y = L\y */
		    ldl_dsolve (n, Y, D) ;			/* y = D\y */
		    ldl_ltsolve (n, Y, Lp, Li, Lx) ;		/* y = L'\y */
		    ldl_permt (n, X, Y, P) ;			/* x = P'y */
		}
		else
		{
		    /* the factorization is LDL' = A */
		    for (i = 0 ; i < n ; i++)			/* x = b */
		    {
			X [i] = B [i] ;
		    }
		    ldl_lsolve (n, X, Lp, Li, Lx) ;		/* x = L\x */
		    ldl_dsolve (n, X, D) ;			/* x = D\x */
		    ldl_ltsolve (n, X, Lp, Li, Lx) ;		/* x = L'\x */
		}

		/* compute the residual y = Ax-b */
		/* note that this code can tolerate a jumbled matrix */
		for (i = 0 ; i < n ; i++)
		{
		    Y [i] = -B [i] ;
		}
		for (j = 0 ; j < n ; j++)
		{
		    for (p = Ap [j] ; p < Ap [j+1] ; p++)
		    {
			Y [Ai [p]] += Ax [p] * X [j] ;
		    }
		}
		/* rnorm = norm (y, inf) */
		rnorm = 0 ;
		for (i = 0 ; i < n ; i++)
		{
		    r = (Y [i] > 0) ? (Y [i]) : (-Y [i]) ;
		    rnorm = (r > rnorm) ? (r) : (rnorm) ;
		}
		maxrnorm = (rnorm > maxrnorm) ? (rnorm) : (maxrnorm) ;
		printf ("relative maxnorm of residual: %g\n", rnorm) ;
	    }

	    /* -------------------------------------------------------------- */
	    /* free the size-lnz part of L */
	    /* -------------------------------------------------------------- */

	    FREE_MEMORY (Li, int) ;
	    FREE_MEMORY (Lx, double) ;

	}

	/* free everything */
	FREE_MEMORY (P, int) ;
	FREE_MEMORY (Ap, int) ;
	FREE_MEMORY (Ai, int) ;
	FREE_MEMORY (Ax, double) ;
	FREE_MEMORY (Pinv, int) ;
	FREE_MEMORY (Y, double) ;
	FREE_MEMORY (Flag, int) ;
	FREE_MEMORY (Pattern, int) ;
	FREE_MEMORY (Lnz, int) ;
	FREE_MEMORY (Lp, int) ;
	FREE_MEMORY (Parent, int) ;
	FREE_MEMORY (D, double) ;
	FREE_MEMORY (B, double) ;
	FREE_MEMORY (X, double) ;
    }

    printf ("\nLargest residual during all tests: %g\n", maxrnorm) ;
    if (maxrnorm < 1e-8)
    {
	printf ("\n" PROGRAM ": all tests passed\n") ;
	EXIT_OK ;
    }
    else
    {
	printf ("\n" PROGRAM ": one more tests failed (residual too high)\n") ;
	EXIT_ERROR ;
    }
}