1 //------------------------------------------------------------------------------
2 // SLIP_LU/Demo/example2.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 #include "demos.h"
12
13 // This example shows how to use SLIP LU within your code
14 // Unlike example1, the input matrix here is directly read in from the
15 // triplet formmat. Also, differs from example1 in that the output solution
16 // is given in mpq_t precision
17
18 // usage:
19 // example2 mat_file rhs_file > out
20 // mat_file is the Matrix Market file containing the A matrix
21 // rhs_file is a list of entries for right hand side dense matrix
22 // if input file names are not specified, they are defaulted to
23 // ../ExampleMats/10teams.mat and ../ExampleMats/10teams.v, respectively.
24 // out is file for output calculated result
25
26 #define FREE_WORKSPACE \
27 SLIP_LU_analysis_free(&S, option);\
28 SLIP_matrix_free(&A, option); \
29 SLIP_FREE(option); \
30 SLIP_matrix_free(&b, option); \
31 SLIP_matrix_free(&x, option); \
32 SLIP_finalize();
33
main(int argc,char ** argv)34 int main (int argc, char **argv)
35 {
36 //--------------------------------------------------------------------------
37 // Prior to using SLIP LU, its environment must be initialized. This is
38 // done by calling the SLIP_initialize() function.
39 //--------------------------------------------------------------------------
40 SLIP_initialize();
41
42 //--------------------------------------------------------------------------
43 // Get matrix and right hand side file names
44 //--------------------------------------------------------------------------
45 char *mat_name, *rhs_name;
46 mat_name = "../ExampleMats/10teams_mat.txt";
47 rhs_name = "../ExampleMats/10teams_v.txt";
48 if (argc > 2)
49 {
50 mat_name = argv[1];
51 rhs_name = argv[2];
52 }
53
54 //--------------------------------------------------------------------------
55 // Declare our data structures
56 //--------------------------------------------------------------------------
57 SLIP_info ok;
58 SLIP_matrix *A = NULL ; // input matrix
59 SLIP_matrix *b = NULL ; // Right hand side vector
60 SLIP_matrix *x = NULL ; // Solution vectors
61 SLIP_LU_analysis *S = NULL ; // Column permutation
62 SLIP_options *option = SLIP_create_default_options();
63 if (option == NULL)
64 {
65 fprintf (stderr, "Error! OUT of MEMORY!\n");
66 FREE_WORKSPACE;
67 return 0;
68 }
69
70 //--------------------------------------------------------------------------
71 // Allocate memory, read in A and b
72 //--------------------------------------------------------------------------
73
74 // Read in A. The output of this demo function is A in CSC format with
75 // mpz_t entries.
76 FILE* mat_file = fopen(mat_name,"r");
77 if( mat_file == NULL )
78 {
79 perror("Error while opening the file");
80 FREE_WORKSPACE;
81 return 0;
82 }
83 OK(SLIP_tripread(&A, mat_file, option));
84 fclose(mat_file);
85
86 // Read in b. The output of this demo function is b in dense format with
87 // mpz_t entries
88 FILE* rhs_file = fopen(rhs_name,"r");
89 if( rhs_file == NULL )
90 {
91 perror("Error while opening the file");
92 FREE_WORKSPACE;
93 return 0;
94 }
95 OK(SLIP_read_dense(&b, rhs_file, option));
96 fclose(rhs_file);
97
98 // Check if the size of A matches b
99 if (A->n != b->m)
100 {
101 printf("%"PRId64" %"PRId64" \n", A->m,b->m);
102 fprintf (stderr, "Error! Size of A and b do not match!\n");
103 FREE_WORKSPACE;
104 return 0;
105 }
106
107 //--------------------------------------------------------------------------
108 // solve
109 //--------------------------------------------------------------------------
110
111 clock_t start_s = clock();
112
113 // SLIP LU has an optional check, to enable it, one can set the following
114 // parameter to be true.
115 option->check = true;
116
117 // Solve the system and give MPQ solution
118 OK(SLIP_backslash( &x, SLIP_MPQ, A, b, option));
119
120 clock_t end_s = clock();
121
122 double t_s = (double) (end_s - start_s) / CLOCKS_PER_SEC;
123
124 printf("\nSLIP LU Factor & Solve time: %lf\n", t_s);
125
126 //--------------------------------------------------------------------------
127 // Free memory
128 //--------------------------------------------------------------------------
129
130 FREE_WORKSPACE;
131
132 printf ("\n%s: all tests passed\n\n", __FILE__) ;
133 return 0;
134 }
135
136