-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.c
132 lines (107 loc) · 3.25 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/* UCSB CS240A, Fall Quarter 2016
* * Main and supporting functions for the Conjugate Gradient Solver on a 5-point stencil
* *
* * NAMES: Lan Liu ; Pritha Doddahosahally Narayanappa;
* * PERMS: 7956394 ; 9727371
* * DATE: 10/17/2016
* */
#include"mpi.h"
//#include "hw2harness.c"
#include <stdio.h>
#include <stdlib.h>
#include "cgsolve.c"
double* load_vec( char* filename, int* k );
void save_vec( int k, double* x );
int main( int argc, char* argv[] ) {
int writeOutX = 0;
int n, k, p, rank, i;
int maxiterations = 10000;
int niters=0;
double norm=1;
double* b;
double* x;
double time;
double t1, t2;
double* x_initial;
MPI_Init( &argc, &argv );
MPI_Comm_size(MPI_COMM_WORLD,&p);
MPI_Comm_rank(MPI_COMM_WORLD,&rank);
if ( argc == 3 ) {
k = atoi( argv[1] );
n = k*k;
b = malloc(n/p * sizeof(double));
for(i=rank*n/p;i < (rank+1)*n/p;i++){
b[i-rank*n/p] = cs240_getB(i,n);
}
} else if ( !strcmp( argv[1], "-i" ) && argc == 4 ) {
b = load_vec( argv[2], &k );
} else {
printf( "\nCGSOLVE Usage: \n\t"
"Model Problem:\tmpirun -np [number_procs] cgsolve [k] [output_1=y_0=n]\n\t"
"Custom Input:\tmpirun -np [number_procs] cgsolve -i [input_filename] [output_1=y_0=n]\n\n");
exit(0);
}
writeOutX = atoi( argv[argc-1] ); // Write X to file if true, do not write if unspecified.
t1 = MPI_Wtime();
x = cgsolve(p, n, rank, b, &niters, maxiterations, &norm);
x_initial=malloc(n* sizeof(double));
MPI_Gather(x,n/p,MPI_DOUBLE, x_initial, n/p, MPI_DOUBLE, 0, MPI_COMM_WORLD);
x=x_initial;
t2 = MPI_Wtime();
if(niters>0 && rank ==0){
printf( "Norm of the residual after %d iterations: %lf\n",niters,norm);
}
if ( writeOutX ) {
save_vec( k, x );
}
if(rank == 0){printf( "Problem size (k): %d\n",k);}
if(niters>0 && rank ==0){
printf( "Norm of the residual after %d iterations: %lf\n",niters,norm);
}
printf( "Elapsed time during CGSOLVE: %lf\n", t2-t1);
if(rank ==0)
printf( "result %d \n",cs240_verify(x_initial, k, t2-t1));
if(niters > 0){
free(b);
}
if(niters > 0){
free(x);
}
MPI_Finalize();
return 0;
}
double* load_vec( char* filename, int* k ) {
FILE* iFile = fopen(filename, "r");
int nScan;
int nTotal = 0;
int n;
if ( iFile == NULL ) {
printf("Error reading file.\n");
exit(0);
}
nScan = fscanf( iFile, "k=%d\n", k );
if ( nScan != 1 ) {
printf("Error reading dimensions.\n");
exit(0);
}
n = (*k)*(*k);
double* vec = (double *)malloc( n * sizeof(double) );
do {
nScan = fscanf( iFile, "%lf", &vec[nTotal++] );
} while ( nScan >= 0 );
if ( nTotal != n+1 ) {
printf("Incorrect number of values scanned n=%d, nTotal=%d.\n",n,nTotal);
exit(0);
}
return vec;
}
void save_vec( int k, double* x ) {
FILE* oFile;
int i;
oFile = fopen("xApprox.txt","w");
fprintf( oFile, "k=%d\n", k );
for (i = 0; i < k*k; i++) {
fprintf( oFile, "%lf\n", x[i]);
}
fclose( oFile );
}