|
| 1 | +/* |
| 2 | + * Example file for using PMRRR to compute all eigenpairs. |
| 3 | + * Example run: mpiexec -np 4 -env PMR_NUM_THREADS 2 ./main_all.x |
| 4 | + * |
| 5 | + */ |
| 6 | + |
| 7 | +#include <stdlib.h> |
| 8 | +#include <stdio.h> |
| 9 | +#include <string.h> |
| 10 | +#include <math.h> /* ceil */ |
| 11 | +#include <assert.h> |
| 12 | +#include "mpi.h" |
| 13 | +#include "pmrrr.h" |
| 14 | + |
| 15 | +static int read_tri_mat(char*, double**, double**); |
| 16 | +static void print_vector(char*, double*, char*, int); |
| 17 | +static void print_matrix(char*, double*, int, int, int); |
| 18 | + |
| 19 | + |
| 20 | +int main(int argc, char **argv) |
| 21 | +{ |
| 22 | + /* Input parameter to 'pmrrr' */ |
| 23 | + int n; /* Matrix size */ |
| 24 | + int il, iu; |
| 25 | + int tryRAC = 1; /* Try high rel. accuracy */ |
| 26 | + double *D, *E; /* Diagonal and off-diagonal elements */ |
| 27 | + double vl, vu; |
| 28 | + |
| 29 | + double *W; /* eigenvalues */ |
| 30 | + int nz; /* # local eigenvectors */ |
| 31 | + int offset; |
| 32 | + double *Z; /* eigenvectors; stored by colums */ |
| 33 | + int ldz; |
| 34 | + int *Zsupp; /* eigenvector support */ |
| 35 | + |
| 36 | + /* Others */ |
| 37 | + int pid, nproc, info, status; |
| 38 | + int i; |
| 39 | + |
| 40 | + MPI_Init_thread(&argc, &argv, MPI_THREAD_MULTIPLE, &status); |
| 41 | + MPI_Comm_rank(MPI_COMM_WORLD, &pid); |
| 42 | + MPI_Comm_size(MPI_COMM_WORLD, &nproc); |
| 43 | + |
| 44 | + /* Read in data from file, space for D and E will |
| 45 | + * be allocated and needs to be freed at the end */ |
| 46 | + n = read_tri_mat("./Wilkinson21.data", &D, &E); |
| 47 | + |
| 48 | + /* Print input */ |
| 49 | + if (pid == 0) { |
| 50 | + printf("\n%% Input matrix:\n\n"); |
| 51 | + printf("n = %d;\n", n); |
| 52 | + print_vector("D=[", D, "];", n ); |
| 53 | + print_vector("E=[", E, "];", n-1); |
| 54 | + } |
| 55 | + |
| 56 | + /* Allocate memory */ |
| 57 | + W = (double *) malloc( n * sizeof(double) ); |
| 58 | + Zsupp = (int *) malloc( 2*n * sizeof(int) ); |
| 59 | + |
| 60 | + nz = (int) ceil(n / (double) nproc); |
| 61 | + ldz = n; |
| 62 | + |
| 63 | + Z = (double *) malloc((size_t) n * nz * sizeof(double) ); |
| 64 | + |
| 65 | + /* Use MRRR to compute eigenvalues and -vectors */ |
| 66 | + info = pmrrr("Vectors", "All", &n, D, E, &vl, &vu, &il, |
| 67 | + &iu, &tryRAC, MPI_COMM_WORLD, &nz, &offset, |
| 68 | + W, Z, &ldz, Zsupp); |
| 69 | + assert(info == 0); |
| 70 | + |
| 71 | + /* Possibly communicate eigenvalues */ |
| 72 | + /* PMR_comm_eigvals(MPI_COMM_WORLD, &nz, &offset, W); */ |
| 73 | + |
| 74 | + /* Print results */ |
| 75 | + for (i=0; i<nproc; i++) { |
| 76 | + MPI_Barrier(MPI_COMM_WORLD); |
| 77 | + if (i == pid) { |
| 78 | + printf("\n\n%% Results of process %d:\n", pid); |
| 79 | + if (i == 0) printf("W = [];"); |
| 80 | + print_vector("W = [W;", W, "];", nz); |
| 81 | + print_matrix("Z", Z, n, nz, offset); |
| 82 | + fflush(stdout); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + /* Free allocated memory */ |
| 87 | + free(D); |
| 88 | + free(E); |
| 89 | + free(W); |
| 90 | + free(Z); |
| 91 | + free(Zsupp); |
| 92 | + |
| 93 | + MPI_Finalize(); |
| 94 | + |
| 95 | + return(0); |
| 96 | +} |
| 97 | + |
| 98 | + |
| 99 | + |
| 100 | + |
| 101 | +/* |
| 102 | + * Reads the triadiagonal matrix from a file. |
| 103 | + */ |
| 104 | +static int read_tri_mat(char *filename, double **Dp, double **Ep) |
| 105 | +{ |
| 106 | + int i, n; |
| 107 | + FILE *filedes; |
| 108 | + |
| 109 | + filedes = fopen(filename, "r"); |
| 110 | + assert(filedes != NULL); |
| 111 | + |
| 112 | + fscanf(filedes, "%d", &n); |
| 113 | + |
| 114 | + *Dp = (double *) malloc( n * sizeof(double) ); |
| 115 | + assert(*Dp != NULL); |
| 116 | + |
| 117 | + *Ep = (double *) malloc( n * sizeof(double) ); |
| 118 | + assert(*Ep != NULL); |
| 119 | + |
| 120 | + for (i=0; i<n; i++) { |
| 121 | + fscanf(filedes, "%le %le", *Dp+i, *Ep+i); |
| 122 | + } |
| 123 | + (*Ep)[n-1] = 0.0; |
| 124 | + |
| 125 | + fclose(filedes); |
| 126 | + |
| 127 | + return(n); |
| 128 | +} |
| 129 | + |
| 130 | + |
| 131 | + |
| 132 | +static void print_vector(char *pre, double *v, char *post, int n) |
| 133 | +{ |
| 134 | + int i; |
| 135 | + |
| 136 | + printf("\n%s\n", pre); |
| 137 | + for (i=0; i<n; i++) { |
| 138 | + printf("%.17e", v[i]); |
| 139 | + printf("\n"); |
| 140 | + } |
| 141 | + printf("%s\n", post); |
| 142 | +} |
| 143 | + |
| 144 | + |
| 145 | + |
| 146 | +static void print_matrix(char *name, double *A, int lda, |
| 147 | + int numcols, int offset) |
| 148 | +{ |
| 149 | + int j; |
| 150 | + char str1[20], str2[20]; |
| 151 | + |
| 152 | + for (j=1; j<=numcols; j++) { |
| 153 | + str1[0] = '\0'; |
| 154 | + strcat(str1,name); |
| 155 | + strcat(str1,"(:,"); |
| 156 | + sprintf(str2, "%d",j+offset); |
| 157 | + strcat(str1, str2); |
| 158 | + strcat(str1, ")=["); |
| 159 | + print_vector(str1, &A[(j-1)*lda],"];", lda); |
| 160 | + } |
| 161 | +} |
0 commit comments