49 #include "Petra_Comm.h"
50 #include "Petra_Map.h"
51 #include "Petra_RDP_MultiVector.h"
52 #include "Petra_RDP_Vector.h"
53 #include "Petra_RDP_DCRS_Matrix.h"
61 int main(
int argc,
char *argv[])
66 MPI_Init(&argc,&argv);
72 Petra_Comm& comm = *
new Petra_Comm(MPI_COMM_WORLD);
74 Petra_Comm& comm = *
new Petra_Comm();
77 int NumProc = comm.getNumProc();
78 int MyPID = comm.getMyPID();
79 cout <<
"Processor " << MyPID <<
" of " << NumProc <<
" is alive." << endl;
84 if (MyPID==0) cout <<
"Usage: " << argv[0] <<
" number_of_equations" << endl;
87 int numGlobalEquations = atoi(argv[1]);
89 if (numGlobalEquations < NumProc)
92 cout <<
"numGlobalBlocks = " << numGlobalEquations
93 <<
" cannot be < number of processors = " << NumProc << endl;
99 Petra_Map& map = *
new Petra_Map(numGlobalEquations, comm);
102 int * UpdateList = map.getUpdateList();
103 int numLocalEquations = map.numLocalEquations();
108 int * numNz =
new int[numLocalEquations];
113 for (i=0; i<numLocalEquations; i++)
114 if (UpdateList[i]==0 || UpdateList[i] == numGlobalEquations-1)
121 Petra_RDP_DCRS_Matrix&
A = *
new Petra_RDP_DCRS_Matrix(map);
125 assert(A.allocate(numNz)==0);
132 double *values =
new double[2];
133 values[0] = -1.0; values[1] = -1.0;
134 int *indices =
new int[2];
138 for (i=0; i<numLocalEquations; i++)
140 if (UpdateList[i]==0)
145 else if (UpdateList[i] == numGlobalEquations-1)
147 indices[0] = numGlobalEquations-2;
152 indices[0] = UpdateList[i]-1;
153 indices[1] = UpdateList[i]+1;
156 assert(A.putRow(UpdateList[i], numEntries, values, indices)==0);
157 assert(A.putRow(UpdateList[i], 1, &two, UpdateList+i)==0);
161 assert(A.fillComplete()==0);
165 Petra_RDP_Vector& q = *
new Petra_RDP_Vector(map);
166 Petra_RDP_Vector& z = *
new Petra_RDP_Vector(map);
167 Petra_RDP_Vector& resid = *
new Petra_RDP_Vector(map);
173 double normz, lambda, residual;
176 int niters = 500*numGlobalEquations;
177 double tolerance = 1.0e-10;
178 for (
int iter = 0; iter < niters; iter++)
181 q.scaleCopy(z, 1.0/normz);
183 q.dotProd(z, &lambda);
184 if (iter%100==0 || iter+1==niters)
186 resid.linComb(z, -lambda, q);
187 resid.norm2(&residual);
188 if (MyPID==0) cout <<
"Iter = " << iter <<
" Lambda = " << lambda
189 <<
" Residual of A*q - lambda*q = " << residual << endl;
191 if (residual < tolerance)
break;
int main(int argc, char *argv[])