EVSL  1.1.0
EigenValues Slicing Library
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
MMPLanN.c
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <sys/stat.h>
5 #include <math.h>
6 #include "evsl.h"
7 #include "io.h"
8 
9 #define max(a, b) ((a) > (b) ? (a) : (b))
10 #define min(a, b) ((a) < (b) ? (a) : (b))
11 #define TRIV_SLICER 0
12 
13 int main () {
14  int ierr = 0;
15  /*--------------------------------------------------------------
16  * this tests the spectrum slicing idea for a generic matrix
17  * read in sparse matrix format
18  *-------------------------------------------------------------*/
19  int n=0, sl, i, j, mlan, nev, totcnt;
20  double a, b, ecount, xintv[4];
21  double lmin, lmax;
22  double *alleigs;
23  int n_intv; // number of subintervals (slices)
24  int npts; // number of integration points for eigenvalue count
25  //--------------------related to kpmdos
26  //int Mdeg = 180, nvec = 100;
27  /*-------------------- matrix A: coo format and csr format */
28  cooMat Acoo;
29  csrMat Acsr;
30  /*-------------------- tolerance to accept ev */
31  double tol;
32  /* stats */
33  /* #ev computed; the computed eig val/vec,
34  * residuals of the computed eig pairs */
35  int nevOut;
36  // double *lam=NULL, *Y=NULL, *res=NULL ;
37  /*-------------------- Max degree of polynomial, or max Lanczos steps for DOS; numbr smpling vectors */
38  int Mdeg=80, nvec=30;
39  /*-------------------- IO */
40  FILE *flog = stdout, *fmat = NULL, *fstats = NULL;
41  io_t io;
42  int numat, mat;
43  char line[MAX_LINE];
44  /* initial vector: random */
45  double *vinit, *xdos, *ydos;
46  polparams pol;
47  //-------------------- tolerance for stopping criterion
48  tol = 1e-6;
49  /*-------------------- start EVSL */
50  EVSLStart();
51  //-------------------- interior eigensolver parameters
52  int *counts;
53  double *sli;
54  /*------------------ file "matfile" contains paths to matrices */
55  if( NULL == ( fmat = fopen( "matfile", "r" ) ) ) {
56  fprintf( flog, "Can't open matfile...\n" );
57  exit(2);
58  }
59  /*-------------------- read number of matrices ..*/
60  memset( line, 0, MAX_LINE );
61  if (NULL == fgets( line, MAX_LINE, fmat )) {
62  fprintf( flog, "error in reading matfile...\n" );
63  exit(2);
64  }
65  if( ( numat = atoi( line ) ) <= 0 ) {
66  fprintf( flog, "Invalid count of matrices...\n" );
67  exit(3);
68  }
69  /*-------------------- LOOP through matrices -*/
70  for(mat = 1; mat <= numat; mat++) {
71  if(get_matrix_info(fmat, &io) != 0) {
72  fprintf(flog, "Invalid format in matfile ...\n");
73  exit(5);
74  }
75  /*----------------input matrix and interval information -*/
76  fprintf(flog, "MATRIX: %s...\n", io.MatNam);
77  a = io.a;
78  b = io.b;
79  n_intv = io.n_intv;
80 
81  struct stat st = {0}; /* Make sure OUT directory exists */
82  if (stat("OUT", &st) == -1) {
83  mkdir("OUT", 0750);
84  }
85 
86  /*-------------------- path to write the output files*/
87  char path[1024] ;
88  strcpy( path, "OUT/MMPLanN_");
89  strcat( path, io.MatNam);
90  fstats = fopen(path,"w"); // write all the output to the file io.MatNam
91  if (!fstats) {
92  printf(" failed in opening output file in OUT/\n");
93  fstats = stdout;
94  }
95  fprintf(fstats, "MATRIX: %s...\n", io.MatNam);
96  fprintf(fstats,"Partition the interval of interest [%f,%f] into %d slice (s)\n", a,b,n_intv);
97  counts = malloc(n_intv*sizeof(int));
98  sli = malloc((n_intv+1)*sizeof(double));
99  /*-------------------- Read matrix - case: COO/MatrixMarket formats */
100  if (io.Fmt > HB) {
101  ierr =read_coo_MM(io.Fname, 1, 0, &Acoo);
102  if (ierr == 0) {
103  fprintf(fstats,"matrix read successfully\n");
104  n = Acoo.nrows;
105  }
106  else {
107  fprintf(flog, "read_coo error = %d\n", ierr);
108  exit(6);
109  }
110  /*-------------------- conversion from COO to CSR format */
111  ierr = cooMat_to_csrMat(0, &Acoo, &Acsr);
112  }
113  if (io.Fmt == HB) {
114  fprintf(flog, "HB FORMAT not supported (yet) * \n");
115  exit(7);
116  }
117  /*-------------------- set the left-hand side matrix A */
118  SetAMatrix(&Acsr);
119  /*-------------------- define ChebLanTr parameters */
120  alleigs = (double *) malloc(n*sizeof(double));
121  vinit = (double *) malloc(n*sizeof(double));
122  rand_double(n, vinit);
123  /*-------------------- get lambda_min lambda_max estimates */
124  ierr = LanTrbounds(50, 200, 1e-8, vinit, 1, &lmin, &lmax, fstats);
125  fprintf(fstats, "Step 0: Eigenvalue bounds for A: [%.15e, %.15e]\n", lmin, lmax);
126  /*-------------------- define [a b] now so we can get estimates now
127  on number of eigenvalues in [a b] from kpmdos */
128  fprintf(fstats," --> interval: a %9.3e b %9.3e \n",a, b);
129  /*-------------------- define LanczosDOS parameters */
130  xintv[0] = a; xintv[1] = b;
131  xintv[2] = lmin; xintv[3] = lmax;
132  //-------------------- landos or triv_slicer
133  if (TRIV_SLICER) {
134  linspace(a, b, n_intv+1, sli);
135  } else {
136  double t = evsl_timer();
137  npts = 100*n_intv;
138  xdos = (double *) malloc((npts)*sizeof(double));
139  ydos = (double *) malloc((npts)*sizeof(double));
140 
141  //fprintf(stdout," %d %d \n",npts,n_intv);
142  LanDos(nvec, Mdeg, npts, xdos, ydos, &ecount, xintv);
143  //fprintf(stdout," %f \n",ecount);
144  t = evsl_timer() - t;
145 
146  fprintf(fstats, " Time to build DOS (Landos) was : %10.2f \n",t);
147  fprintf(fstats, " estimated eig count in interval: %.15e \n",ecount);
148  //-------------------- call splicer to slice the spectrum
149  if ((ecount <0) | (ecount > n)) {
150  printf(" e-count estimate is incorrect \n ");
151  exit(7);
152  }
153  /*-------------------- error in ecount */
154  if ((ecount <1) | (ecount>n))
155  exit(6);
156  /*-------------------- define slicer parameters */
157 
158  fprintf(fstats,"DOS parameters: Mdeg = %d, nvec = %d, npnts = %d\n",
159  Mdeg, nvec, npts);
160  spslicer2(xdos, ydos, n_intv, npts, sli);
161  free (xdos);
162  free (ydos);
163  }
164  /*-------------------- Dos determined+ slicing done */
165  fprintf(fstats,"DOS parameters: Mdeg = %d, nvec = %d, npts = %d\n",
166  Mdeg, nvec, npts);
167  fprintf(fstats, "Step 1b: Slices found: \n");
168  for (j=0; j<n_intv;j++) {
169  fprintf(fstats, " %2d: [%.15e , %.15e]\n", j+1, sli[j],sli[j+1]);
170  }
171  //-------------------- # eigs per slice
172  //-------------------- approximate number of eigenvalues wanted
173  double fac = 1.2; // this factor ensures that # of eigs per slice is slightly overestimated
174  nev = (int) (1 + ecount / ((double) n_intv)); // # eigs per slice
175  nev = (int)(fac*nev); // want an overestimate of ev_int
176  fprintf(fstats,"Step 2: In each slice compute %d eigenvalues ... \n", nev);
177  /*-------------------- MAIN intv LOOP: for each sclice Do: */
178  totcnt = 0;
179  for (sl =0; sl<n_intv; sl++) {
180  fprintf(fstats,"======================================================\n");
181  double *lam, *Y, *res;
182  int *ind;
183  //--------------------
184  a = sli[sl];
185  b = sli[sl+1];
186  StatsReset();
187  fprintf(fstats, " subinterval: [% 12.4e , % 12.4e]\n", a, b);
188  //-------------------- Parameters for ChebLanTr
189  mlan = max(5*nev,300); mlan = min(mlan, n);
190  //mlan = 2000; // max number of Lanczos iterations
191  fprintf(fstats, "Non-Restarted Lanczos with dimension %d\n", mlan);
192  xintv[0] = a;
193  xintv[1] = b;
194  //-------------------- set up default parameters for pol.
195  set_pol_def(&pol);
196  // can change default values here e.g.
197  pol.damping = 2; // use lanczos damping
198  pol.thresh_int = 0.8; // change thresholds for getting a sharper
199  pol.thresh_ext = 0.2; // [higher deg] polynomial
200  //-------------------- Now determine polymomial
201  find_pol(xintv, &pol);
202 
203  fprintf(fstats, " polynomial [type = %d], deg %d, bar %e gam %e\n",
204  pol.type, pol.deg, pol.bar, pol.gam);
205  //-------------------- Call ChebLanNr
206  ierr = ChebLanNr(xintv, mlan, tol, vinit, &pol, &nevOut, &lam, &Y,
207  &res, fstats);
208  if (ierr) {
209  printf("ChebLanNr error %d\n", ierr);
210  return 1;
211  }
212  /* sort the eigenvals: ascending order
213  * ind: keep the orginal indices */
214  ind = (int*) malloc(nevOut*sizeof(int));
215  sort_double(nevOut, lam, ind);
216 
217  /* print eigenvalues */
218  fprintf(fstats, "- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\n");
219  fprintf(fstats, " Eigenvalues in [%f, %f]\n",a,b);
220  fprintf(fstats, "- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\n");
221  fprintf(fstats, " Computed [%d out of %d estimated] ||Res|| ", nevOut, nev);
222  fprintf(fstats, "\n");
223  for (i=0; i<nevOut; i++) {
224  fprintf(fstats, " % .15e %.1e", lam[i], res[ind[i]]);
225  fprintf(fstats,"\n");
226  }
227  fprintf(fstats, "- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\n");
228  memcpy(&alleigs[totcnt],lam,nevOut*sizeof(double));
229  totcnt += nevOut;
230  counts[sl] = nevOut;
231  StatsPrint(stdout);
232  //-------------------- free memory allocated within this loop
233  if (lam) free(lam);
234  if (Y) free(Y);
235  if (res) free(res);
236  free(ind);
237  free_pol(&pol);
238  /*-------------------- end slice loop */
239  }
240  fprintf(fstats," --> Total eigenvalues found = %d\n",totcnt);
241  sprintf(path, "OUT/EigsOut_PLanN_%s", io.MatNam);
242  FILE *fmtout = fopen(path,"w");
243  if (fmtout) {
244  for (j=0; j<totcnt; j++)
245  fprintf(fmtout, "%.15e\n", alleigs[j]);
246  fclose(fmtout);
247  }
248  /*-------------------- free memory */
249  free(counts);
250  free(sli);
251  free(vinit);
252  free_coo(&Acoo);
253  free_csr(&Acsr);
254  free(alleigs);
255  if (fstats != stdout) fclose(fstats);
256  /*-------------------- end matrix loop */
257  }
258  //-------------------- free rest of memory
259  if( flog != stdout ) fclose ( flog );
260  fclose( fmat );
261  /*-------------------- finalize EVSL */
262  EVSLFinish();
263 
264  return 0;
265 }
int deg
Definition: struct.h:70
void free_pol(polparams *pol)
Definition: chebpoly.c:488
void free_coo(cooMat *coo)
memory deallocation for coo matrix
Definition: spmat.c:126
int get_matrix_info(FILE *fmat, io_t *pio)
Definition: io.c:22
double b
Definition: io.h:25
#define TRIV_SLICER
Definition: MMPLanN.c:11
#define MAX_LINE
Definition: io.h:5
int LanDos(const int nvec, int msteps, int npts, double *xdos, double *ydos, double *neig, const double *const intv)
Definition: landos.c:40
int SetAMatrix(csrMat *A)
Set the matrix A.
Definition: evsl.c:68
void StatsPrint(FILE *fstats)
Definition: stats.c:14
void rand_double(int n, double *v)
Definition: vect.c:11
int Fmt
Definition: io.h:20
#define max(a, b)
Definition: MMPLanN.c:9
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
int find_pol(double *intv, polparams *pol)
Sets the values in pol.
Definition: chebpoly.c:361
double gam
Definition: struct.h:64
void sort_double(int n, double *v, int *ind)
Definition: vect.c:92
void spslicer2(double *xi, double *yi, int n_int, int npts, double *sli)
Definition: spslice2.c:29
int EVSLStart()
Initialize evslData.
Definition: evsl.c:27
void free_csr(csrMat *csr)
memory deallocation for csr matrix
Definition: spmat.c:91
double thresh_ext
Definition: struct.h:51
#define HB
Definition: io.h:7
char Fname[MAX_LINE]
Definition: io.h:17
char MatNam[MaxNamLen]
Definition: io.h:18
void set_pol_def(polparams *pol)
set default values for polparams struct.
Definition: chebpoly.c:18
#define min(a, b)
Definition: MMPLanN.c:10
double thresh_int
Definition: struct.h:52
int type
Definition: struct.h:59
int read_coo_MM(const char *matfile, int idxin, int idxout, cooMat *Acoo)
Definition: io.c:66
sparse matrix format: the compressed sparse row (CSR) format, 0-based
Definition: struct.h:31
This file contains function prototypes and constant definitions for EVSL.
Definition: io.h:14
int main()
Definition: MMPLanN.c:18
int EVSLFinish()
Finish EVSL.
Definition: evsl.c:48
int ChebLanNr(double *intv, int maxit, double tol, double *vinit, polparams *pol, int *nevOut, double **lamo, double **Wo, double **reso, FILE *fstats)
Definition: cheblanNr.c:60
double a
Definition: io.h:24
int damping
Definition: struct.h:50
int nrows
Definition: struct.h:17
void StatsReset()
Definition: stats.c:79
parameters for polynomial filter
Definition: struct.h:45
void linspace(double a, double b, int num, double *arr)
Definition: vect.c:54
sparse matrix format: the coordinate (COO) format, 0-based
Definition: struct.h:16
double evsl_timer()
evsl timer for mac
Definition: mactime.c:14
double bar
Definition: struct.h:65
int n_intv
Definition: io.h:23