EVSL  1.1.0
EigenValues Slicing Library
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
LanDosG.c
Go to the documentation of this file.
1 #include <errno.h>
2 #include <fcntl.h>
3 #include <math.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <sys/stat.h>
8 #include <sys/types.h>
9 #include <sys/utsname.h>
10 #include <time.h>
11 #include <unistd.h>
12 #include "evsl.h"
13 #include "io.h"
14 
15 /*-------------------- protos */
16 int exDOS(double *vals, int n, int npts, double *x, double *y, double *intv);
17 int read_coo_MM(const char *matfile, int idxin, int idxout, cooMat *Acoo);
18 int get_matrix_info(FILE *fmat, io_t *pio);
19 int findarg(const char *argname, ARG_TYPE type, void *val, int argc,
20  char **argv);
21 
22 #define max(a, b) ((a) > (b) ? (a) : (b))
23 #define min(a, b) ((a) < (b) ? (a) : (b))
24 
32 int readVec(const char *filename, int *npts, double **vec) {
33  int i;
34  FILE *ifp = fopen(filename, "r");
35  fscanf(ifp, "%i", npts);
36  *vec = (double *)malloc(sizeof(double) * *npts);
37  for (i = 0; i < (*npts); i++) {
38  fscanf(ifp, "%lf", (&(*vec)[i]));
39  }
40  fclose(ifp);
41  return 0;
42 }
43 
53 int main(int argc, char *argv[]) {
54  srand(time(NULL));
55  const int msteps = 30; /* Number of steps */
56  const int degB = 40; /* Degree to aproximate B with */
57  const int npts = 200; /* Number of points */
58  const int nvec = 30; /* Number of random vectors to use */
59  const double tau = 1e-4; /* Tolerance in polynomial approximation */
60  /* ---------------- Intervals of interest
61  intv[0] and intv[1] are the input interval of interest [a. b]
62  intv[2] and intv[3] are the smallest and largest eigenvalues of (A,B)
63  */
64  double intv[4];
65  int n = 0, i, ierr;
66 
67  cooMat Acoo, Bcoo; /* A, B */
68  csrMat Acsr, Bcsr; /* A, B */
69  double *sqrtdiag = NULL;
70 
71  FILE *flog = stdout, *fmat = NULL;
72  FILE *fstats = NULL;
73  io_t io;
74  int numat, mat;
75  char line[MAX_LINE];
76  int graph_exact_dos = 0;
77  findarg("graph_exact_dos", INT, &graph_exact_dos, argc, argv);
78  /*-------------------- start EVSL */
79  EVSLStart();
80  /*------------------ file "matfile" contains paths to matrices */
81  if (NULL == (fmat = fopen("matfileG", "r"))) {
82  fprintf(flog, "Can't open matfileG...\n");
83  exit(2);
84  } else {
85  printf("Read matfileG \n");
86  }
87  /*-------------------- read number of matrices ..*/
88  memset(line, 0, MAX_LINE);
89  if (NULL == fgets(line, MAX_LINE, fmat)) {
90  fprintf(flog, "error in reading matfile...\n");
91  exit(2);
92  }
93  if ((numat = atoi(line)) <= 0) {
94  fprintf(flog, "Invalid count of matrices...\n");
95  exit(3);
96  }
97  for (mat = 1; mat <= numat; mat++) {
98  if (get_matrix_info(fmat, &io) != 0) {
99  fprintf(flog, "Invalid format in matfile ...\n");
100  exit(5);
101  }
102  /*----------------input matrix and interval information -*/
103  fprintf(flog, "MATRIX A: %s...\n", io.MatNam1);
104  fprintf(flog, "MATRIX B: %s...\n", io.MatNam2);
105  char path[1024]; /* path to write the output files */
106  strcpy(path, "OUT/LanDosG_");
107  strcat(path, io.MatNam1);
108  strcat(path, ".log");
109  fstats = fopen(path, "w"); /* write all the output to the file io.MatNam */
110  if (!fstats) {
111  printf(" failed in opening output file in OUT/\n");
112  fstats = stdout;
113  }
114  fprintf(fstats, "MATRIX A: %s...\n", io.MatNam1);
115  fprintf(fstats, "MATRIX B: %s...\n", io.MatNam2);
116  /*-------------------- Read matrix - case: COO/MatrixMarket formats */
117  if (io.Fmt > HB) {
118  ierr = read_coo_MM(io.Fname1, 1, 0, &Acoo);
119  if (ierr == 0) {
120  fprintf(fstats, "matrix read successfully\n");
121  /* nnz = Acoo.nnz; */
122  n = Acoo.nrows;
123  if (n <= 0) {
124  fprintf(stderr, "non-positive number of rows");
125  exit(7);
126  }
127 
128  /* printf("size of A is %d\n", n);
129  printf("nnz of A is %d\n", nnz); */
130  } else {
131  fprintf(flog, "read_coo error for A = %d\n", ierr);
132  exit(6);
133  }
134  ierr = read_coo_MM(io.Fname2, 1, 0, &Bcoo);
135  if (ierr == 0) {
136  fprintf(fstats, "matrix read successfully\n");
137  if (Bcoo.nrows != n) {
138  return 1;
139  }
140  } else {
141  fprintf(flog, "read_coo error for B = %d\n", ierr);
142  exit(6);
143  }
144  /*------------------ diagonal scaling for Acoo and Bcoo */
145  sqrtdiag = (double *)calloc(n, sizeof(double));
146  /*-------------------- conversion from COO to CSR format */
147  ierr = cooMat_to_csrMat(0, &Acoo, &Acsr);
148  if (ierr) {
149  fprintf(flog, "Could not convert matrix to csr: %i", ierr);
150  exit(8);
151  }
152  ierr = cooMat_to_csrMat(0, &Bcoo, &Bcsr);
153  if (ierr) {
154  fprintf(flog, "Could not convert matrix to csr: %i", ierr);
155  exit(9);
156  }
157  } else if (io.Fmt == HB) {
158  fprintf(flog, "HB FORMAT not supported (yet) * \n");
159  exit(7);
160  }
161 
162  /*-------------------- diagonal scaling for L-S poly. approx.
163  * of B^{-1} and B^{-1/2},
164  * which will be used in the DOS */
165  /*-------------------- sqrt of diag(B) */
166  extrDiagCsr(&Bcsr, sqrtdiag);
167  for (i = 0; i < n; i++) {
168  sqrtdiag[i] = sqrt(sqrtdiag[i]);
169  }
170  diagScalCsr(&Acsr, sqrtdiag);
171  diagScalCsr(&Bcsr, sqrtdiag);
172 
173  /*---------------- Set EVSL to solve std eig problem to
174  * compute the range of the spectrum of B */
175  SetStdEig();
176  SetAMatrix(&Bcsr);
177  double *vinit = (double *)malloc(n * sizeof(double));
178  rand_double(n, vinit);
179  double lmin = 0.0, lmax = 0.0;
180  ierr = LanTrbounds(50, 200, 1e-8, vinit, 1, &lmin, &lmax, fstats);
181  if (ierr) {
182  fprintf(flog, "Could not run LanTrBounds: %i", ierr);
183  exit(10);
184  }
185  /*-------------------- Use polynomial to solve B and sqrt(B) */
186  BSolDataPol BInv, BSqrtInv;
187  /*-------------------- Setup the Bsol and Bsqrtsol struct */
188  SetupPolRec(n, degB, tau, lmin, lmax, &BInv);
189  SetupPolSqrt(n, degB, tau, lmin, lmax, &BSqrtInv);
190  SetBSol(BSolPol, (void *)&BInv);
191  SetLTSol(BSolPol, (void *)&BSqrtInv);
192  printf(
193  "The degree for LS polynomial approximations to B^{-1} and B^{-1/2} "
194  "are %d and %d\n",
195  BInv.deg, BSqrtInv.deg);
196  /*-------------------- set the left-hand side matrix A */
197  SetAMatrix(&Acsr);
198  /*-------------------- set the right-hand side matrix B */
199  SetBMatrix(&Bcsr);
200  /*-------------------- Solve Gen Eig problem */
201  SetGenEig();
202  /*-------------------- eig bounds for B\A */
203  rand_double(n, vinit);
204  ierr = LanTrbounds(40, 200, 1e-10, vinit, 1, &lmin, &lmax, fstats);
205  if (ierr) {
206  fprintf(flog, "Could not run LanTrBounds: %i", ierr);
207  exit(10);
208  }
209  /*----------------- plotting the DOS on [a, b] ---------*/
210  intv[0] = lmin;
211  intv[1] = lmax;
212  /*----------------- get the bounds for (A, B) ---------*/
213  intv[2] = lmin;
214  intv[3] = lmax;
215  /*-------------------- Read in the eigenvalues */
216  double *ev = NULL;
217  int numev;
218  if (graph_exact_dos) {
219  readVec("NM1AB_eigenvalues.dat", &numev, &ev);
220  }
221 
222  int ret;
223  double neig;
224  double *xHist = NULL;
225  double *yHist = NULL;
226  /*-------------------- exact histogram and computed DOS */
227  if (graph_exact_dos) {
228  xHist = (double *)malloc(npts * sizeof(double));
229  yHist = (double *)malloc(npts * sizeof(double));
230  }
231  double *xdos = (double *)malloc(npts * sizeof(double));
232  double *ydos = (double *)malloc(npts * sizeof(double));
233 
234  double t0 = evsl_timer();
235  /* ------------------- Calculate the computed DOS */
236  ret = LanDosG(nvec, msteps, npts, xdos, ydos, &neig, intv);
237  double t1 = evsl_timer();
238  fprintf(stdout, " LanDos ret %d in %0.04fs\n", ret, t1 - t0);
239 
240  /* -------------------- Calculate the exact DOS */
241  if (graph_exact_dos) {
242  ret = exDOS(ev, numev, npts, xHist, yHist, intv);
243  fprintf(stdout, " exDOS ret %d \n", ret);
244  }
245 
246  free_coo(&Acoo);
247  free_coo(&Bcoo);
248  free_csr(&Acsr);
249  free_csr(&Bcsr);
250  free(vinit);
251  FreeBSolPolData(&BInv);
252  FreeBSolPolData(&BSqrtInv);
253 
254  /*--------------------Make OUT dir if it doesn't exist */
255  struct stat st = {0};
256  if (stat("OUT", &st) == -1) {
257  mkdir("OUT", 0750);
258  }
259 
260  /*-------------------- Write to output files */
261  char computed_path[1024];
262  strcpy(computed_path, "OUT/LanDosG_Approx_DOS_");
263  strcat(computed_path, io.MatNam1);
264  FILE *ofp = fopen(computed_path, "w");
265  for (i = 0; i < npts; i++)
266  fprintf(ofp, " %10.4f %10.4f\n", xdos[i], ydos[i]);
267  fclose(ofp);
268  printf("Wrote to:%s \n", computed_path);
269 
270  if (graph_exact_dos) {
271  /*-------------------- save exact DOS */
272  strcpy(path, "OUT/LanDosG_Exact_DOS_");
273  strcat(path, io.MatNam1);
274  ofp = fopen(path, "w");
275  for (i = 0; i < npts; i++)
276  fprintf(ofp, " %10.4f %10.4f\n", xHist[i], yHist[i]);
277  fclose(ofp);
278  }
279 
280  printf("The data output is located in OUT/ \n");
281  struct utsname buffer;
282  errno = 0;
283  if (uname(&buffer) != 0) {
284  perror("uname");
285  exit(EXIT_FAILURE);
286  }
287 
288  /*-------------------- invoke gnuplot for plotting ... */
289  char command[1024];
290  strcpy(command, "gnuplot ");
291  strcat(command, " -e \"filename='");
292  strcat(command, computed_path);
293 
294  if (graph_exact_dos) {
295  strcat(command, "';exact_dos='");
296  strcat(command, path);
297  strcat(command, "'\" testerG_ex.gnuplot");
298  ierr = system(command);
299  } else {
300  strcat(command, "'\" testerG.gnuplot");
301  ierr = system(command);
302  }
303 
304  if (ierr) {
305  fprintf(stderr,
306  "Error using 'gnuplot < testerG.gnuplot', \n"
307  "postscript plot could not be generated \n");
308  } else {
309  printf("A postscript graph has been placed in %s%s\n", computed_path,
310  ".eps");
311  /*-------------------- and gv for visualizing / */
312  if (!strcmp(buffer.sysname, "Linux")) {
313  strcpy(command, "gv ");
314  strcat(command, computed_path);
315  strcat(command, ".eps &");
316  ierr = system(command);
317  if (ierr) {
318  fprintf(stderr, "Error using 'gv %s' \n", command);
319  printf(
320  "To view the postscript graph use a postcript viewer such as "
321  "gv \n");
322  }
323  } else {
324  printf(
325  "To view the postscript graph use a postcript viewer such as "
326  "gv \n");
327  }
328  }
329  if (graph_exact_dos) {
330  free(xHist);
331  free(yHist);
332  }
333  free(xdos);
334  free(ydos);
335  if (ev) free(ev);
336  fclose(fstats);
337  if (sqrtdiag) {
338  free(sqrtdiag);
339  }
340  } /* matrix loop */
341 
342  fclose(fmat);
343 
344  EVSLFinish();
345  return 0;
346 }
int get_matrix_info(FILE *fmat, io_t *pio)
Definition: io.c:22
void free_coo(cooMat *coo)
memory deallocation for coo matrix
Definition: spmat.c:126
#define MAX_LINE
Definition: io.h:5
ARG_TYPE
Definition: io.h:33
void extrDiagCsr(csrMat *B, double *d)
Definition: spmat.c:406
int SetGenEig()
Set the problem to generalized eigenvalue problem.
Definition: evsl.c:158
int SetAMatrix(csrMat *A)
Set the matrix A.
Definition: evsl.c:68
void rand_double(int n, double *v)
Definition: vect.c:11
int Fmt
Definition: io.h:20
int cooMat_to_csrMat(int cooidx, cooMat *coo, csrMat *csr)
convert coo to csr
Definition: spmat.c:135
int LanTrbounds(int lanm, int maxit, double tol, double *vinit, int bndtype, double *lammin, double *lammax, FILE *fstats)
Lanczos process for eigenvalue bounds [Thick restart version].
Definition: lanTrbounds.c:38
Definition: io.h:34
int read_coo_MM(const char *matfile, int idxin, int idxout, cooMat *Acoo)
Definition: io.c:66
int SetBMatrix(csrMat *B)
Set the B matrix.
Definition: evsl.c:83
char Fname1[MAX_LINE]
Definition: io.h:17
int EVSLStart()
Initialize evslData.
Definition: evsl.c:27
char Fname2[MAX_LINE]
Definition: io.h:19
void free_csr(csrMat *csr)
memory deallocation for csr matrix
Definition: spmat.c:91
char MatNam2[MaxNamLen]
Definition: io.h:20
int readVec(const char *filename, int *npts, double **vec)
Definition: LanDosG.c:32
#define HB
Definition: io.h:7
void diagScalCsr(csrMat *A, double *d)
Definition: spmat.c:390
void SetupPolSqrt(int n, int max_deg, double tol, double lmin, double lmax, BSolDataPol *data)
Definition: dos_utils.c:45
int findarg(const char *argname, ARG_TYPE type, void *val, int argc, char **argv)
Definition: io.c:171
int LanDosG(const int nvec, const int msteps, int npts, double *xdos, double *ydos, double *neig, const double *const intv)
Definition: landosG.c:43
int main(int argc, char *argv[])
Definition: LanDosG.c:53
int SetLTSol(SolFuncR func, void *data)
Set the solve routine for LT.
Definition: evsl.c:185
int SetStdEig()
Set the problem to standard eigenvalue problem.
Definition: evsl.c:148
sparse matrix format: the compressed sparse row (CSR) format, 0-based
Definition: struct.h:31
int exDOS(double *vals, int n, int npts, double *x, double *y, double *intv)
Definition: exDOS.c:22
This file contains function prototypes and constant definitions for EVSL.
Definition: io.h:14
int EVSLFinish()
Finish EVSL.
Definition: evsl.c:48
int SetBSol(SolFuncR func, void *data)
Set the solve routine and the associated data for B.
Definition: evsl.c:132
int nrows
Definition: struct.h:17
void BSolPol(double *b, double *x, void *data)
Definition: dos_utils.c:61
void SetupPolRec(int n, int max_deg, double tol, double lmin, double lmax, BSolDataPol *data)
Definition: dos_utils.c:37
void FreeBSolPolData(BSolDataPol *data)
Definition: dos_utils.c:53
sparse matrix format: the coordinate (COO) format, 0-based
Definition: struct.h:16
double evsl_timer()
evsl timer for mac
Definition: mactime.c:14
char MatNam1[MaxNamLen]
Definition: io.h:18