1 //------------------------------------------------------------------------------
2 // SLIP_LU/Demo/example.c: example main program for SLIP_LU
3 //------------------------------------------------------------------------------
4
5 // SLIP_LU: (c) 2019-2020, Chris Lourenco, Jinhao Chen, Erick Moreno-Centeno,
6 // Timothy A. Davis, Texas A&M University. All Rights Reserved. See
7 // SLIP_LU/License for the license.
8
9 //------------------------------------------------------------------------------
10
11
12 #include "demos.h"
13
14
15 /* This example shows how to use SLIP LU with a given input matrix and a double
16 output. The input is a randomly generate dense matrix */
17
18 // usage:
19 // example > out
20 // out is file for output calculated result
21
22 #define FREE_WORKSPACE \
23 SLIP_matrix_free(&A,option); \
24 SLIP_matrix_free(&x,option); \
25 SLIP_matrix_free(&b,option); \
26 SLIP_matrix_free(&Rb,option); \
27 SLIP_matrix_free(&R,option); \
28 SLIP_FREE(option); \
29 SLIP_finalize() ; \
30
31
32
main(void)33 int main (void)
34 {
35
36 //--------------------------------------------------------------------------
37 // Prior to using SLIP LU, its environment must be initialized. This is done
38 // by calling the SLIP_initialize() function.
39 //--------------------------------------------------------------------------
40
41 SLIP_initialize();
42
43 //--------------------------------------------------------------------------
44 // Declare and initialize essential variables
45 //--------------------------------------------------------------------------
46
47 SLIP_info ok;
48 int64_t n = 50, nz = 2500, num=0;
49 SLIP_matrix *A = NULL ; // input matrix
50 SLIP_matrix *R = NULL ; // Random matrix to create A
51 SLIP_matrix *Rb = NULL; // Random matrix to create b
52 SLIP_matrix *b = NULL ; // Right hand side vector
53 SLIP_matrix *x = NULL ; // Solution vectors
54 SLIP_options *option = SLIP_create_default_options();
55 if (!option)
56 {
57 fprintf (stderr, "Error! OUT of MEMORY!\n");
58 FREE_WORKSPACE;
59 return 0;
60 }
61
62 //--------------------------------------------------------------------------
63 // Generate a random dense 50*50 matrix
64 //--------------------------------------------------------------------------
65
66 // R is a n*n triplet matrix whose entries are FP64 Note that the first
67 // boolean parameter says that the matrix is not shallow, so that A->i,
68 // A->j, and A->x are calloc'd. The second boolean parameter is meaningless
69 // for FP64 matrices, but it tells SLIP LU to allocate the values of A->x
70 // for the mpz_t, mpq_t, and mpfr_t entries
71 SLIP_matrix_allocate(&R, SLIP_TRIPLET, SLIP_FP64, n, n, nz,
72 false, true, option);
73
74 // Rb is a n*1 dense matrix whose entries are FP64
75 SLIP_matrix_allocate(&Rb, SLIP_DENSE, SLIP_FP64, n, 1, n,
76 false, true, option);
77
78 // Randomly generate the input
79 unsigned int seed = 10;
80 srand(seed);
81 for (int64_t k = 0; k < n; k++)
82 {
83 Rb->x.fp64[k] = rand();
84 for (int64_t p = 0; p < n; p++)
85 {
86 R->i[num] = k;
87 R->j[num] = p;
88 R->x.fp64[num] = rand();
89 num+=1;
90 }
91 }
92
93 R->nz = n*n;
94
95 //--------------------------------------------------------------------------
96 // Build A and b
97 //--------------------------------------------------------------------------
98
99 // A is a copy of the R matrix. A is a CSC matrix with mpz_t entries
100 OK ( SLIP_matrix_copy(&A, SLIP_CSC, SLIP_MPZ, R, option));
101 // b is a copy of the Rb matrix. b is dense with mpz_t entries.
102 OK ( SLIP_matrix_copy(&b, SLIP_DENSE, SLIP_MPZ, Rb, option));
103
104 //--------------------------------------------------------------------------
105 // Solve
106 //--------------------------------------------------------------------------
107
108 clock_t start_s = clock();
109
110 // SLIP LU has an optional check, to enable it, one can set the following
111 // parameter to be true.
112 option->check = true;
113 // Solve the system and give double solution
114 OK(SLIP_backslash( &x, SLIP_FP64, A, b, option));
115
116 clock_t end_s = clock();
117
118 double t_s = (double) (end_s - start_s) / CLOCKS_PER_SEC;
119
120 printf("\nSLIP LU Factor & Solve time: %lf\n", t_s);
121
122 //--------------------------------------------------------------------------
123 // Free memory
124 //--------------------------------------------------------------------------
125
126 FREE_WORKSPACE;
127 printf ("\n%s: all tests passed\n\n", __FILE__) ;
128 return 0;
129 }
130
131