| 1 |
macro ffddmgenerateOperatorfunctions(pr,preffe) |
| 2 |
IFMACRO (pr#myA) |
| 3 |
pr#myA |
| 4 |
ENDIFMACRO |
| 5 |
IFMACRO (!pr#myA) |
| 6 |
func preffe#K[int] pr#A(preffe#K[int] &u) |
| 7 |
{ |
| 8 |
/* Matrix vector product with the global matrix */ |
| 9 |
preffe#K[int] res(pr#isrect ? pr#prfe2#Vhi.ndof : u.n); |
| 10 |
if (preffe#prmesh#isincomm) { |
| 11 |
preffe#prmesh#tic(pr#tmvi) |
| 12 |
if (!bpara) |
| 13 |
res = pr#Aglobal*u; |
| 14 |
else if (!preffe#prmesh#excluded){ |
| 15 |
IFMACRO(pr#bem,1) |
| 16 |
int mrhs = u.n / pr#prfe#Vhi.ndof; /* multi rhs */ |
| 17 |
preffe#K[int] ug(preffe#Vhglob.ndof*mrhs), scaledug(preffe#Vhglob.ndof*mrhs); |
| 18 |
ug = 0; scaledug = 0; |
| 19 |
for (int i=0; i<mrhs; i++) { |
| 20 |
preffe#K[int] scaledu = preffe#Dih[mpiRank(preffe#prmesh#commddm)]*u(i*pr#prfe#Vhi.ndof:(i+1)*pr#prfe#Vhi.ndof-1); |
| 21 |
scaledug(i*preffe#Vhglob.ndof:(i+1)*preffe#Vhglob.ndof-1) = preffe#Rih[mpiRank(preffe#prmesh#commddm)]'*scaledu; |
| 22 |
} |
| 23 |
mpiAllReduce(scaledug,ug,preffe#prmesh#commddm,mpiSUM); |
| 24 |
preffe#K[int] resg = pr#Aglobal*ug; |
| 25 |
for (int i=0; i<mrhs; i++) { |
| 26 |
res(i*pr#prfe#Vhi.ndof:(i+1)*pr#prfe#Vhi.ndof-1) = preffe#Rih[mpiRank(preffe#prmesh#commddm)]*resg(i*preffe#Vhglob.ndof:(i+1)*preffe#Vhglob.ndof-1); |
| 27 |
} |
| 28 |
ENDIFMACRO |
| 29 |
|
| 30 |
IFMACRO(!pr#bem) |
| 31 |
if (preffe#prmesh#overlap > 0) { |
| 32 |
res = pr#aRd[mpiRank(preffe#prmesh#commddm)]*u; |
| 33 |
pr#prfe2#update(res,true); |
| 34 |
} |
| 35 |
else { |
| 36 |
res = pr#matN*u; |
| 37 |
pr#prfe2#update(res,false); |
| 38 |
} |
| 39 |
ENDIFMACRO |
| 40 |
} |
| 41 |
preffe#prmesh#toc(pr#tmvi,"",pr#tmv) |
| 42 |
} |
| 43 |
return res; |
| 44 |
} |
| 45 |
|
| 46 |
func preffe#K[int] pr#AT(preffe#K[int] &u) |
| 47 |
{ |
| 48 |
/* Matrix vector product with the transpose of the global matrix */ |
| 49 |
preffe#K[int] res(pr#isrect ? pr#prfe#Vhi.ndof : u.n); |
| 50 |
IFMACRO(!pr#bem) |
| 51 |
if (preffe#prmesh#isincomm) { |
| 52 |
preffe#prmesh#tic(pr#tmvi) |
| 53 |
if (!bpara) |
| 54 |
res = pr#Aglobal'*u; |
| 55 |
else if (!preffe#prmesh#excluded){ |
| 56 |
if (preffe#prmesh#overlap > 0) { |
| 57 |
res = pr#aRd[mpiRank(preffe#prmesh#commddm)]'*u; |
| 58 |
pr#prfe#update(res,true); |
| 59 |
} |
| 60 |
else { |
| 61 |
res = pr#matN'*u; |
| 62 |
pr#prfe#update(res,false); |
| 63 |
} |
| 64 |
} |
| 65 |
preffe#prmesh#toc(pr#tmvi,"",pr#tmv) |
| 66 |
} |
| 67 |
ENDIFMACRO |
| 68 |
IFMACRO(pr#bem,1) |
| 69 |
if (mpirank == 0) cout << "TODO" << endl; |
| 70 |
assert(0); |
| 71 |
ENDIFMACRO |
| 72 |
return res; |
| 73 |
} |
| 74 |
ENDIFMACRO |
| 75 |
|
| 76 |
func preffe#K[int] pr#directsolve(preffe#K[int]& rhs) |
| 77 |
{ |
| 78 |
preffe#K[int] res(rhs.n); |
| 79 |
|
| 80 |
IFMACRO(pr#bem,1) |
| 81 |
if (mpirank == 0) |
| 82 |
cout << "No way to use a direct solver for BEM for now !" << endl; |
| 83 |
assert(0); |
| 84 |
ENDIFMACRO |
| 85 |
|
| 86 |
IFMACRO(!pr#bem) |
| 87 |
preffe#prmesh#tic(pr#tloc) |
| 88 |
|
| 89 |
if (preffe#prmesh#isincomm) { |
| 90 |
if (!bpara) { |
| 91 |
set(pr#Aglobal,solver = sparsesolver,commworld=preffe#prmesh#commself); |
| 92 |
res = pr#Aglobal^-1*rhs; |
| 93 |
} |
| 94 |
else if (!preffe#prmesh#excluded) { |
| 95 |
if (pr#matdirect.n == 1) { |
| 96 |
if (preffe#prmesh#overlap > 0) { |
| 97 |
pr#matdirect = preffe#Dih[mpiRank(preffe#prmesh#commddm)]*pr#aRd[mpiRank(preffe#prmesh#commddm)]; |
| 98 |
pr#matdirect = preffe#Rih[mpiRank(preffe#prmesh#commddm)]'*pr#matdirect; |
| 99 |
} |
| 100 |
else |
| 101 |
pr#matdirect = preffe#Rih[mpiRank(preffe#prmesh#commddm)]'*pr#matN; |
| 102 |
pr#matdirect = pr#matdirect*preffe#Rih[mpiRank(preffe#prmesh#commddm)]; |
| 103 |
set(pr#matdirect,solver = sparsesolver,commworld=preffe#prmesh#commddm,master=-1); |
| 104 |
} |
| 105 |
preffe#K[int] scaledrhs = preffe#Dih[mpiRank(preffe#prmesh#commddm)]*rhs; |
| 106 |
preffe#K[int] scaledrhsg(preffe#Vhglob.ndof); |
| 107 |
scaledrhsg = preffe#Rih[mpiRank(preffe#prmesh#commddm)]'*scaledrhs; |
| 108 |
preffe#K[int] resg = pr#matdirect^-1*scaledrhsg; |
| 109 |
res = preffe#Rih[mpiRank(preffe#prmesh#commddm)]*resg; |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
preffe#prmesh#toc(pr#tloc,"direct solver",pr#tdirect) |
| 114 |
ENDIFMACRO |
| 115 |
|
| 116 |
return res; |
| 117 |
} |
| 118 |
// EOM |
| 119 |
|
| 120 |
macro ffddmgeneratePrecondfunctions(pr,preffe) |
| 121 |
|
| 122 |
func preffe#K[int] pr#AEprec(preffe#K[int] &x) |
| 123 |
{ |
| 124 |
preffe#K[int] res(x.n); |
| 125 |
if (preffe#prmesh#isincomm) { |
| 126 |
IFMACRO(!pr#bem) |
| 127 |
if (!bpara) { |
| 128 |
if (pr#AglobEprec.n > 0) |
| 129 |
res = pr#AglobEprec*x; |
| 130 |
else |
| 131 |
res = pr#Aglobal*x; |
| 132 |
} |
| 133 |
else if (!preffe#prmesh#excluded){ |
| 134 |
if (pr#aRdEprec.n > 0) |
| 135 |
res = pr#aRdEprec*x; |
| 136 |
else |
| 137 |
res = pr#aRd[mpiRank(preffe#prmesh#commddm)]*x; |
| 138 |
pr#prfe#update(res,true); |
| 139 |
} |
| 140 |
ENDIFMACRO |
| 141 |
IFMACRO(pr#bem,1) |
| 142 |
res = pr#A(x); |
| 143 |
ENDIFMACRO |
| 144 |
} |
| 145 |
return res; |
| 146 |
} |
| 147 |
|
| 148 |
IFMACRO (pr#myQ) |
| 149 |
pr#myQ |
| 150 |
ENDIFMACRO |
| 151 |
IFMACRO (!pr#myQ) |
| 152 |
func preffe#K[int] pr#Q(preffe#K[int] &l, int mrhs) /* Q = Z*E^-1*Z^T */ |
| 153 |
{ |
| 154 |
preffe#K[int] res(l.n); |
| 155 |
res=0.; |
| 156 |
if (preffe#prmesh#isincomm) { |
| 157 |
if(pr#bCM){ |
| 158 |
preffe#K[int] vaux(1), tmp(1), zaux(1); |
| 159 |
|
| 160 |
/* Z^T l */ |
| 161 |
if (!bpara) { |
| 162 |
vaux.resize(pr#ncoarsespace); tmp.resize(pr#ncoarsespace); zaux.resize(pr#ncoarsespace); |
| 163 |
vaux = pr#ZCM'*l; |
| 164 |
} |
| 165 |
else if (!preffe#prmesh#binexactCS){ |
| 166 |
vaux.resize(pr#ncoarsespace); tmp.resize(pr#ncoarsespace); zaux.resize(pr#ncoarsespace); |
| 167 |
if (!preffe#prmesh#excluded) { |
| 168 |
preffe#K[int] ls = l .* preffe#Dk[mpiRank(preffe#prmesh#commddm)]; /* Z = sum_i D_i Z_i */ |
| 169 |
tmp = pr#ZCMi'*ls; |
| 170 |
} |
| 171 |
else |
| 172 |
tmp = 0; |
| 173 |
vaux = 0; |
| 174 |
mpiReduce(tmp,vaux,processor(0,preffe#prmesh#mpicomm),mpiSUM); |
| 175 |
} |
| 176 |
else { |
| 177 |
NewMacro localmacroinexact preffe#prmesh#minexactCS EndMacro |
| 178 |
IFMACRO (localmacroinexact,1) |
| 179 |
vaux.resize(mrhs*preffe#CoarseVhi.ndof); tmp.resize(mrhs*preffe#CoarseVhi.ndof); zaux.resize(mrhs*preffe#CoarseVhi.ndof); |
| 180 |
if (!preffe#prmesh#excluded) { |
| 181 |
for (int m=0; m<mrhs; m++) |
| 182 |
tmp(m*preffe#CoarseVhi.ndof:(m+1)*preffe#CoarseVhi.ndof-1) = pr#ZCMi'*l(m*preffe#Vhi.ndof:(m+1)*preffe#Vhi.ndof-1); |
| 183 |
} |
| 184 |
/*exchange(pr#Addmc,tmp,scaled=true);*/ |
| 185 |
for (int m=0; m<mrhs; m++) |
| 186 |
pr#prfe#Coarseupdate(tmp(m*preffe#CoarseVhi.ndof:(m+1)*preffe#CoarseVhi.ndof-1),true); |
| 187 |
ENDIFMACRO |
| 188 |
; |
| 189 |
} |
| 190 |
|
| 191 |
if (!preffe#prmesh#binexactCS) { |
| 192 |
if (!bpara || mpiRank(preffe#prmesh#mpicomm) < preffe#prmesh#pCS) { |
| 193 |
preffe#prmesh#tic(pr#tcsi) |
| 194 |
if (mpiRank(preffe#prmesh#mpicomm) == 0) |
| 195 |
zaux = pr#ECM^-1*vaux; |
| 196 |
else { |
| 197 |
preffe#K[int] dumx(pr#ECM.n), dumb(pr#ECM.n); |
| 198 |
dumx = pr#ECM^-1*dumb; |
| 199 |
} |
| 200 |
preffe#prmesh#toccomm(pr#tcsi,"",pr#tcs,preffe#prmesh#commCS) |
| 201 |
} |
| 202 |
|
| 203 |
if (bpara) |
| 204 |
broadcast(processor(0,preffe#prmesh#mpicomm),zaux); |
| 205 |
|
| 206 |
if (!bpara) { |
| 207 |
res = pr#ZCM*zaux; |
| 208 |
} |
| 209 |
else if (!preffe#prmesh#excluded) { |
| 210 |
res = pr#ZCMi*zaux; |
| 211 |
} |
| 212 |
} |
| 213 |
else { |
| 214 |
NewMacro localmacroinexact preffe#prmesh#minexactCS EndMacro |
| 215 |
IFMACRO (localmacroinexact,1) |
| 216 |
preffe#K[int] solc(mrhs*preffe#CoarseVhi.ndof); |
| 217 |
solc = 0; |
| 218 |
preffe#prmesh#tic(pr#tcsi) |
| 219 |
/* |
| 220 |
DestroyRecycling(Addmc); |
| 221 |
DDM(Addmc, tmp, solc, O=Optc); |
| 222 |
solc = 0; |
| 223 |
*/ |
| 224 |
|
| 225 |
/*DDM(pr#Addmc, tmp, solc, O=pr#Optc);*/ |
| 226 |
solc = pr#CoarsefGMRES(solc, tmp, ffddminexactCStol, ffddmmaxit, ffddmvariant); |
| 227 |
preffe#prmesh#toc(pr#tcsi,"",pr#tcs) |
| 228 |
if (!preffe#prmesh#excluded) |
| 229 |
for (int m=0; m<mrhs; m++) |
| 230 |
res(m*preffe#Vhi.ndof:(m+1)*preffe#Vhi.ndof-1) = pr#ZCMi*solc(m*preffe#CoarseVhi.ndof:(m+1)*preffe#CoarseVhi.ndof-1); |
| 231 |
ENDIFMACRO |
| 232 |
; |
| 233 |
} |
| 234 |
} |
| 235 |
else{ |
| 236 |
|
| 237 |
preffe#K[int] vaux(pr#ncoarsespace), zaux(pr#ncoarsespace), zauxg(pr#sg); |
| 238 |
|
| 239 |
/* Z^T l */ |
| 240 |
if (!bpara) { |
| 241 |
int cpt = 0; |
| 242 |
for (int jj=0; jj<preffe#prmesh#npart; jj++) { |
| 243 |
preffe#K[int] tmp(preffe#Rih[jj].n); |
| 244 |
tmp = preffe#Rih[jj]*l; |
| 245 |
for (int ii=0; ii < pr#sizelg[jj]; ii++) { |
| 246 |
vaux[cpt]=pr#Z[cpt]'*tmp; |
| 247 |
cpt++; |
| 248 |
} |
| 249 |
} |
| 250 |
/* global deflation vectors */ |
| 251 |
for (int jj=0; jj<pr#sg; jj++) |
| 252 |
vaux[cpt]=pr#Z[cpt++]'*l; |
| 253 |
|
| 254 |
zaux=pr#E^-1*vaux; |
| 255 |
cpt = 0; |
| 256 |
/* the 2 fors loop over the columns of Z subdomainwise */ |
| 257 |
for (int jj=0; jj<preffe#prmesh#npart; jj++) |
| 258 |
for (int ii=0; ii < pr#sizelg[jj]; ii++) { |
| 259 |
preffe#K[int] tmp(preffe#Rih[jj].m); |
| 260 |
tmp = preffe#Rih[jj]'*pr#Z[cpt]; |
| 261 |
res += zaux[cpt]*tmp; |
| 262 |
cpt++; /* keeps track of the global numbering of the columns of Z */ |
| 263 |
/* we multiply 'column by element' */ |
| 264 |
} |
| 265 |
/* global deflation vectors */ |
| 266 |
for (int jj=0; jj<pr#sg; jj++) |
| 267 |
res += zaux[cpt]*pr#Z[cpt++]; |
| 268 |
} |
| 269 |
else { |
| 270 |
preffe#K[int] tmp(pr#si); |
| 271 |
for (int i=0;i<pr#si;i++) |
| 272 |
tmp[i]=pr#Z[i]'*l; /* local vector Z'_i l_i */ |
| 273 |
if(!preffe#prmesh#binexactCS) { |
| 274 |
IFMACRO(pr#CSwithpetsc) |
| 275 |
if (mpiRank(preffe#prmesh#mpicomm) < preffe#prmesh#pCS) |
| 276 |
preffe#prmesh#tic(pr#tcsi) |
| 277 |
tmp = pr#petscE^-1*tmp; |
| 278 |
preffe#prmesh#toccomm(pr#tcsi,"",pr#tcs,preffe#prmesh#commCS) |
| 279 |
ENDIFMACRO |
| 280 |
IFMACRO(!pr#CSwithpetsc) |
| 281 |
if (pr#ncoarsespace > pr#sg) |
| 282 |
mpiGatherv(tmp,vaux(0:pr#ncoarsespace-pr#sg-1),processor(0,preffe#prmesh#mpicomm),pr#sizelgworld,pr#offsetiworld); |
| 283 |
/* mpiGatherv puts together inside vaux the local vectors (whose sizes are in sizelg) */ |
| 284 |
for (int ii=0; ii < pr#sg; ii++) |
| 285 |
vaux[pr#ncoarsespace-pr#sg+ii] = pr#prfe#scalprod(pr#Z[ii+pr#si], l); |
| 286 |
|
| 287 |
if (mpiRank(preffe#prmesh#mpicomm) < preffe#prmesh#pCS) { |
| 288 |
preffe#prmesh#tic(pr#tcsi) |
| 289 |
if (mpiRank(preffe#prmesh#mpicomm) == 0) |
| 290 |
zaux = pr#E^-1*vaux; |
| 291 |
else { |
| 292 |
preffe#K[int] dumx(pr#E.n), dumb(pr#E.n); |
| 293 |
dumx = pr#E^-1*dumb; |
| 294 |
} |
| 295 |
preffe#prmesh#toccomm(pr#tcsi,"",pr#tcs,preffe#prmesh#commCS) |
| 296 |
} |
| 297 |
if (pr#ncoarsespace > pr#sg) |
| 298 |
mpiScatterv(zaux(0:pr#ncoarsespace-pr#sg-1),tmp,processor(0,preffe#prmesh#mpicomm),pr#sizelgworld,pr#offsetiworld); |
| 299 |
|
| 300 |
if (pr#sg > 0) { |
| 301 |
if (mpiRank(preffe#prmesh#mpicomm) == 0) |
| 302 |
zauxg = zaux(pr#ncoarsespace-pr#sg:pr#ncoarsespace-1); |
| 303 |
broadcast(processor(0,preffe#prmesh#mpicomm),zauxg); |
| 304 |
} |
| 305 |
|
| 306 |
ENDIFMACRO |
| 307 |
/* inverse of mpiGatherv, cut the global zaux into the local tmp[i] */ |
| 308 |
for (int i=0;i<pr#si;i++) { |
| 309 |
res += tmp[i]*pr#Z[i]; |
| 310 |
} |
| 311 |
/* sum_j R_i Z_j zaux_j */ |
| 312 |
} |
| 313 |
else { |
| 314 |
NewMacro localmacroinexact preffe#prmesh#minexactgeneoCS EndMacro |
| 315 |
IFMACRO (localmacroinexact,1) |
| 316 |
|
| 317 |
mpiRequest[int] rq(pr#prfe#prmesh#listofsupers.n-pr#prfe#prmesh#issuper+pr#prfe#prmesh#issuper*(pr#prfe#prmesh#insuperwithoverlap.n-!pr#prfe#prmesh#excluded)); |
| 318 |
int cpt = 0; |
| 319 |
pr#prfe#K[int][int] tres(pr#prfe#prmesh#insuperwithoverlap.n); |
| 320 |
for (int is=0; is < pr#prfe#prmesh#listofsupers.n; is++) |
| 321 |
if (!pr#prfe#prmesh#issuper || pr#prfe#prmesh#mysuperrank != pr#prfe#prmesh#listofsupers[is]) |
| 322 |
Isend(processor(pr#prfe#prmesh#rankofsuper(pr#prfe#prmesh#listofsupers[is]),pr#prfe#prmesh#mpicomm,rq[cpt++]),tmp); |
| 323 |
if (pr#prfe#prmesh#issuper) { |
| 324 |
vaux.resize(pr#prfe#superDk[mpiRank(pr#prfe#prmesh#commsuper)].n); |
| 325 |
if (!pr#prfe#prmesh#excluded) |
| 326 |
vaux(pr#superlocalnuminv[pr#offseti[mpiRank(pr#prfe#prmesh#commddm)]]:pr#superlocalnuminv[pr#offseti[mpiRank(pr#prfe#prmesh#commddm)]]+pr#sizelg[mpiRank(pr#prfe#prmesh#commddm)]-1) = tmp; |
| 327 |
for (int i=!pr#prfe#prmesh#excluded; i < pr#prfe#prmesh#insuperwithoverlap.n; i++) { |
| 328 |
tres[i].resize(pr#sizelg[pr#prfe#prmesh#insuperwithoverlap[i]]); |
| 329 |
Irecv(processor(pr#prfe#prmesh#insuperwithoverlap[i]+pr#prfe#prmesh#pCS*pr#prfe#prmesh#exclude,pr#prfe#prmesh#mpicomm,rq[cpt++]),tres[i]); |
| 330 |
} |
| 331 |
} |
| 332 |
for (int i=0;i < cpt;i++) |
| 333 |
int k = mpiWaitAny(rq); |
| 334 |
|
| 335 |
if (pr#prfe#prmesh#issuper) |
| 336 |
for (int i=!pr#prfe#prmesh#excluded; i < pr#prfe#prmesh#insuperwithoverlap.n; i++) |
| 337 |
vaux(pr#superlocalnuminv[pr#offseti[pr#prfe#prmesh#insuperwithoverlap[i]]]:pr#superlocalnuminv[pr#offseti[pr#prfe#prmesh#insuperwithoverlap[i]]]+pr#sizelg[pr#prfe#prmesh#insuperwithoverlap[i]]-1) = tres[i]; |
| 338 |
|
| 339 |
if (preffe#prmesh#issuper) { |
| 340 |
preffe#prmesh#tic(pr#tcsi) |
| 341 |
|
| 342 |
if (preffe#prmesh#binexactgeneoCS) { |
| 343 |
preffe#K[int] solc(vaux.n); |
| 344 |
solc = 0; |
| 345 |
solc = pr#superfGMRES(solc, vaux, ffddminexactCStol, ffddmmaxit, ffddmvariant); |
| 346 |
solc = solc .* preffe#superDk[mpiRank(preffe#prmesh#commsuper)]; |
| 347 |
vaux.resize(pr#ncoarsespace); |
| 348 |
vaux = pr#RsuperCS'*solc; |
| 349 |
} |
| 350 |
else { |
| 351 |
preffe#K[int] tmp = vaux .* preffe#superDk[mpiRank(preffe#prmesh#commsuper)]; |
| 352 |
preffe#K[int] tmp2(pr#RsuperCS.m); |
| 353 |
tmp2 = pr#RsuperCS'*tmp; |
| 354 |
vaux.resize(pr#ncoarsespace); |
| 355 |
vaux = pr#E^-1*tmp2; |
| 356 |
/* |
| 357 |
tmp = pr#RsuperCS*vaux; |
| 358 |
tmp = tmp .* preffe#superDk[mpiRank(preffe#prmesh#commsuper)]; |
| 359 |
vaux = pr#RsuperCS'*tmp; |
| 360 |
*/ |
| 361 |
} |
| 362 |
|
| 363 |
preffe#prmesh#toccomm(pr#tcsi,"",pr#tcs,preffe#prmesh#commsuper) |
| 364 |
} |
| 365 |
|
| 366 |
if (pr#prfe#prmesh#issuper) { |
| 367 |
if (!pr#prfe#prmesh#excluded) |
| 368 |
tmp = vaux(pr#offseti[mpiRank(pr#prfe#prmesh#commddm)]:pr#offseti[mpiRank(pr#prfe#prmesh#commddm)]+pr#sizelg[mpiRank(pr#prfe#prmesh#commddm)]-1); |
| 369 |
mpiRequest[int] rq(pr#prfe#prmesh#insuperinterior.n-!pr#prfe#prmesh#excluded); |
| 370 |
pr#prfe#K[int][int] tres(pr#prfe#prmesh#insuperinterior.n); |
| 371 |
for (int i=!pr#prfe#prmesh#excluded; i < pr#prfe#prmesh#insuperinterior.n; i++) { |
| 372 |
tres[i].resize(pr#sizelg[pr#prfe#prmesh#insuperinterior[i]]); |
| 373 |
tres[i] = vaux(pr#offseti[pr#prfe#prmesh#insuperinterior[i]]:pr#offseti[pr#prfe#prmesh#insuperinterior[i]]+pr#sizelg[pr#prfe#prmesh#insuperinterior[i]]-1); |
| 374 |
} |
| 375 |
for (int i=!pr#prfe#prmesh#excluded; i < pr#prfe#prmesh#insuperinterior.n; i++) { |
| 376 |
Isend(processor(pr#prfe#prmesh#insuperinterior[i]+pr#prfe#prmesh#pCS*pr#prfe#prmesh#exclude,pr#prfe#prmesh#mpicomm,rq[i-!pr#prfe#prmesh#excluded]),tres[i]); |
| 377 |
} |
| 378 |
for (int i=0;i < pr#prfe#prmesh#insuperinterior.n-!pr#prfe#prmesh#excluded;i++) |
| 379 |
int k = mpiWaitAny(rq); |
| 380 |
} |
| 381 |
else { |
| 382 |
mpiRequest rq; |
| 383 |
Irecv(processor(pr#prfe#prmesh#mysuper,pr#prfe#prmesh#mpicomm,rq),tmp); |
| 384 |
mpiWait(rq); |
| 385 |
} |
| 386 |
|
| 387 |
for (int i=0;i<pr#si;i++) { |
| 388 |
res += tmp[i]*pr#Z[i]; |
| 389 |
} |
| 390 |
/* sum_j R_i Z_j zaux_j */ |
| 391 |
ENDIFMACRO |
| 392 |
; |
| 393 |
} |
| 394 |
pr#prfe#update(res,false); /* recover the Z_j zaux_j (in the overlap) computed by neighbouring subdomains */ |
| 395 |
|
| 396 |
if (!pr#prfe#prmesh#excluded) |
| 397 |
for (int i=0;i<pr#sg;i++) |
| 398 |
res += zauxg[i]*pr#Z[pr#si+i]; |
| 399 |
} |
| 400 |
} |
| 401 |
} |
| 402 |
return res; |
| 403 |
} |
| 404 |
ENDIFMACRO |
| 405 |
|
| 406 |
func preffe#K[int] pr#Q(preffe#K[int] &l) /* Q = Z*E^-1*Z^T */ |
| 407 |
{ |
| 408 |
return pr#Q(l, 1); |
| 409 |
} |
| 410 |
|
| 411 |
IFMACRO (pr#myPREC1) |
| 412 |
pr#myPREC1 |
| 413 |
ENDIFMACRO |
| 414 |
IFMACRO (!pr#myPREC1) |
| 415 |
func preffe#K[int] pr#PREC1(preffe#K[int] &l, int mrhs) |
| 416 |
{ |
| 417 |
/* Application of the 1 level preconditioner |
| 418 |
M^{-1}*y = \sum Ri^T*Di*Ai^{-1}*Ri*y |
| 419 |
Ri restriction operators, Ai local matrices */ |
| 420 |
|
| 421 |
preffe#K[int] s(l.n); |
| 422 |
s = 0; |
| 423 |
|
| 424 |
if (preffe#prmesh#isincomm) { |
| 425 |
preffe#prmesh#tic(pr#tpreci) |
| 426 |
|
| 427 |
if (pr#prec == "none") { |
| 428 |
s = l; |
| 429 |
} |
| 430 |
else { |
| 431 |
IFMACRO(!pr#superwithchunk) |
| 432 |
if (!bpara) { |
| 433 |
assert(mrhs == 1); |
| 434 |
for(int i=0; i<preffe#prmesh#npart; ++i) { |
| 435 |
preffe#K[int] bi(preffe#Rih[i].n); |
| 436 |
bi = preffe#Rih[i]*l; /* restricts rhs */ |
| 437 |
if(pr#prec == "soras") |
| 438 |
bi = bi .* preffe#Dk[i]; |
| 439 |
preffe#K[int] ui = pr#aR[i] ^-1 * bi; /* local solves */ |
| 440 |
if(pr#prec != "asm") |
| 441 |
ui = ui .* preffe#Dk[i]; /* partition of unity */ |
| 442 |
s += preffe#Rih[i]'*ui; /* prolongation */ |
| 443 |
} |
| 444 |
} |
| 445 |
else if (!preffe#prmesh#excluded){ |
| 446 |
preffe#K[int] aux(mrhs*pr#aR[mpiRank(preffe#prmesh#commddm)].n); |
| 447 |
/* for bem, the local HMatrix is defined on the Augmented mesh, so we need to extend -> solve -> restrict */ |
| 448 |
if (pr#aR[mpiRank(preffe#prmesh#commddm)].n == preffe#Auginterp.m) { |
| 449 |
for (int m=0; m<mrhs; m++) |
| 450 |
aux(m*preffe#Auginterp.m:(m+1)*preffe#Auginterp.m-1) = preffe#Auginterp'*l(m*preffe#Vhi.ndof:(m+1)*preffe#Vhi.ndof-1); |
| 451 |
} |
| 452 |
else |
| 453 |
aux = l; |
| 454 |
|
| 455 |
if(pr#prec == "soras") |
| 456 |
for (int m=0; m<mrhs; m++) |
| 457 |
aux(m*preffe#Vhi.ndof:(m+1)*preffe#Vhi.ndof-1) = aux(m*preffe#Vhi.ndof:(m+1)*preffe#Vhi.ndof-1) .* preffe#Dk[mpiRank(preffe#prmesh#commddm)]; |
| 458 |
|
| 459 |
/* for bem, the local HMatrix is defined on the Augmented mesh, so we need to extend -> solve -> restrict */ |
| 460 |
if (pr#aR[mpiRank(preffe#prmesh#commddm)].n == preffe#Auginterp.m) { |
| 461 |
preffe#K[int] saug = pr#aR[mpiRank(preffe#prmesh#commddm)]^-1 * aux; |
| 462 |
for (int m=0; m<mrhs; m++) |
| 463 |
s(m*preffe#Vhi.ndof:(m+1)*preffe#Vhi.ndof-1) = preffe#Auginterp*saug(m*preffe#Auginterp.m:(m+1)*preffe#Auginterp.m-1); |
| 464 |
} |
| 465 |
else |
| 466 |
s = pr#aR[mpiRank(preffe#prmesh#commddm)]^-1 * aux; |
| 467 |
|
| 468 |
if(pr#prec != "asm"){ |
| 469 |
for (int m=0; m<mrhs; m++) |
| 470 |
pr#prfe#update(s(m*preffe#Vhi.ndof:(m+1)*preffe#Vhi.ndof-1),true); |
| 471 |
} |
| 472 |
else{ |
| 473 |
for (int m=0; m<mrhs; m++) { |
| 474 |
pr#prfe#update(s(m*preffe#Vhi.ndof:(m+1)*preffe#Vhi.ndof-1),false); |
| 475 |
pr#prfe#update(s(m*preffe#Vhi.ndof:(m+1)*preffe#Vhi.ndof-1),true); |
| 476 |
} |
| 477 |
} |
| 478 |
} |
| 479 |
ENDIFMACRO |
| 480 |
IFMACRO(pr#superwithchunk) |
| 481 |
preffe#K[int] chunkl(pr#superchunkinterp.m); |
| 482 |
if(pr#prec == "soras") { |
| 483 |
preffe#K[int] lD = l .* pr#chunksuperDk; |
| 484 |
chunkl = pr#superchunkinterp'*lD; |
| 485 |
} |
| 486 |
else |
| 487 |
chunkl = pr#superchunkinterp'*l; |
| 488 |
|
| 489 |
pr#chunkupdate(chunkl,true); |
| 490 |
|
| 491 |
preffe#K[int] chunku(chunkl.n); |
| 492 |
chunku = 0; |
| 493 |
|
| 494 |
chunku = pr#chunkfGMRES(chunku, chunkl, ffddmtol, ffddmmaxit, ffddmvariant); |
| 495 |
/* chunku = pr#chunkdirectsolve(chunkl); */ |
| 496 |
|
| 497 |
chunku = chunku .* pr#chunkDk[mpiRank(pr#chunkcommddm)]; |
| 498 |
|
| 499 |
preffe#K[int] s = pr#superchunkinterp*chunku; |
| 500 |
|
| 501 |
if(pr#prec != "asm") |
| 502 |
s = s .* pr#chunksuperDk; |
| 503 |
|
| 504 |
pr#prfe#update(s,false); |
| 505 |
return s; |
| 506 |
ENDIFMACRO |
| 507 |
} |
| 508 |
preffe#prmesh#toc(pr#tpreci,"",pr#tprec) |
| 509 |
} |
| 510 |
return s; |
| 511 |
} |
| 512 |
|
| 513 |
func preffe#K[int] pr#PREC1(preffe#K[int] &l) |
| 514 |
{ |
| 515 |
int mrhs = 1; |
| 516 |
if (preffe#prmesh#isincomm && !preffe#prmesh#excluded) { |
| 517 |
int ndof = bpara ? preffe#Dk[mpiRank(preffe#prmesh#commddm)].n : preffe#Vhglob.ndof; |
| 518 |
if (l.n > 0) { |
| 519 |
assert(l.n%ndof == 0); |
| 520 |
mrhs = l.n/ndof; |
| 521 |
} |
| 522 |
} |
| 523 |
return pr#PREC1(l, mrhs); |
| 524 |
} |
| 525 |
ENDIFMACRO |
| 526 |
|
| 527 |
func preffe#K[int] pr#PREC2level(preffe#K[int] &l) |
| 528 |
{ |
| 529 |
preffe#K[int] ltemp(l.n); |
| 530 |
ltemp = 0; |
| 531 |
if (preffe#prmesh#isincomm) { |
| 532 |
|
| 533 |
preffe#prmesh#tic(pr#tpreci) |
| 534 |
|
| 535 |
preffe#K[int] Ql; |
| 536 |
preffe#K[int] aux; |
| 537 |
|
| 538 |
/* Q */ |
| 539 |
if (pr#corr == "AD" || pr#corr == "ADEF1" || pr#corr == "ADEF2" || pr#corr == "BNN" || pr#corr == "RBNN1") { |
| 540 |
Ql.resize(l.n); |
| 541 |
Ql = pr#Q(l); |
| 542 |
} |
| 543 |
|
| 544 |
/* P */ |
| 545 |
if (pr#corr == "ADEF1" || pr#corr == "BNN" || pr#corr == "RBNN1") { |
| 546 |
aux.resize(l.n); |
| 547 |
if (pr#bCM) |
| 548 |
aux = pr#AEprec(Ql); |
| 549 |
else |
| 550 |
aux = pr#A(Ql); |
| 551 |
ltemp = l-aux; |
| 552 |
} |
| 553 |
else |
| 554 |
ltemp = l; |
| 555 |
|
| 556 |
preffe#prmesh#toc(pr#tpreci,"",pr#tprec) |
| 557 |
|
| 558 |
/* M^-1 */ |
| 559 |
preffe#K[int] s(l.n); |
| 560 |
s = pr#PREC1(ltemp); |
| 561 |
|
| 562 |
preffe#prmesh#tic(pr#tpreci) |
| 563 |
|
| 564 |
/* P^T */ |
| 565 |
if (pr#corr == "ADEF2" || pr#corr == "BNN" || pr#corr == "RBNN1" || pr#corr == "RBNN2") { |
| 566 |
if (pr#bCM) |
| 567 |
ltemp = pr#AEprec(s); |
| 568 |
else |
| 569 |
ltemp = pr#A(s); |
| 570 |
aux.resize(l.n); |
| 571 |
aux = pr#Q(ltemp); |
| 572 |
ltemp = s-aux; |
| 573 |
} |
| 574 |
else |
| 575 |
ltemp = s; |
| 576 |
|
| 577 |
/* + Q */ |
| 578 |
if (pr#corr == "AD" || pr#corr == "ADEF1" || pr#corr == "ADEF2" || pr#corr == "BNN") |
| 579 |
ltemp += Ql; |
| 580 |
|
| 581 |
preffe#prmesh#toc(pr#tpreci,"",pr#tprec) |
| 582 |
} |
| 583 |
return ltemp; |
| 584 |
} |
| 585 |
|
| 586 |
/* |
| 587 |
func preffe#K[int] pr#PRECTEST(preffe#K[int] &l) |
| 588 |
{ |
| 589 |
preffe#K[int] res(l.n),x0(l.n); |
| 590 |
IFMACRO (pr#minexactCS,1) |
| 591 |
x0 = 0; |
| 592 |
res = pr#CoarsefGMRES(x0, l, 1.e-6, 5, "right"); |
| 593 |
ENDIFMACRO |
| 594 |
return res; |
| 595 |
} |
| 596 |
*/ |
| 597 |
|
| 598 |
func preffe#K[int] pr#PRECTEST(preffe#K[int] &l) |
| 599 |
{ |
| 600 |
/* |
| 601 |
preffe#K[int] dd = pr#aRd[0].diag; |
| 602 |
dd = 1./dd; |
| 603 |
dd = l.*dd; |
| 604 |
return dd; |
| 605 |
*/ |
| 606 |
return l; |
| 607 |
} |
| 608 |
|
| 609 |
/* |
| 610 |
func preffe#K[int] pr#PRECTEST(preffe#K[int] &l) |
| 611 |
{ |
| 612 |
preffe#K[int] res(l.n); |
| 613 |
preffe#K[int] ls = l .* preffe#Dk[mpiRank(preffe#prmesh#commddm)]; |
| 614 |
preffe#K[int] tmp = pr#ZCMi'*ls; |
| 615 |
|
| 616 |
IFMACRO (preffe#prmesh#minexactCS,1) |
| 617 |
preffe#K[int] rr = pr#CoarsePREC1(tmp); |
| 618 |
res = pr#ZCMi*rr; |
| 619 |
ENDIFMACRO |
| 620 |
|
| 621 |
return res; |
| 622 |
} |
| 623 |
*/ |
| 624 |
|
| 625 |
/* CMCG */ |
| 626 |
/* |
| 627 |
func pr#K[int] pr#PRECTEST(pr#K[int] &l) |
| 628 |
{ |
| 629 |
if (cpt%2 >3) { |
| 630 |
Vhi<pr#K> pr#mdef(u0),pr#mdef(ucmcg); |
| 631 |
u0[] = 0; |
| 632 |
ffcmcgrhs(Vhi,Thi,ThAugmented,l,u0,ucmcg) |
| 633 |
cpt++; |
| 634 |
return ucmcg[]; |
| 635 |
} |
| 636 |
else{ |
| 637 |
cpt++; |
| 638 |
return pr#PREC1(l); |
| 639 |
} |
| 640 |
} |
| 641 |
*/ |
| 642 |
|
| 643 |
IFMACRO (pr#myPREC) |
| 644 |
pr#myPREC |
| 645 |
ENDIFMACRO |
| 646 |
IFMACRO (!pr#myPREC) |
| 647 |
func preffe#K[int] pr#PREC(preffe#K[int] &l) |
| 648 |
{ |
| 649 |
if (pr#corr == "test") |
| 650 |
return pr#PRECTEST(l); |
| 651 |
else if (pr#corr == "none") |
| 652 |
return pr#PREC1(l); |
| 653 |
else |
| 654 |
return pr#PREC2level(l); |
| 655 |
} |
| 656 |
ENDIFMACRO |
| 657 |
|
| 658 |
func preffe#K[int] pr#fGMRES(preffe#K[int]& x0, preffe#K[int]& rhs, real eps, int maxit, string sprec) |
| 659 |
{ |
| 660 |
if (preffe#prmesh#isincomm) { |
| 661 |
preffe#prmesh#tic(pr#tgmresi) |
| 662 |
|
| 663 |
string prefix = Stringification(pr)+"_"; |
| 664 |
|
| 665 |
preffe#K[int] uni(x0.n); |
| 666 |
|
| 667 |
int dim = min(maxit,ffddmrestart); |
| 668 |
|
| 669 |
IFMACRO(pr#withhpddmkrylov) |
| 670 |
load "hpddm" |
| 671 |
if (!preffe#prmesh#excluded) { |
| 672 |
uni = x0; |
| 673 |
pr#nbiter = IterativeMethod(pr#A, rhs,uni, precon = pr#PREC, sparams = "-hpddm_tol "+eps+" -hpddm_variant "+(preffe#prmesh#binexactCS ? "flexible" : sprec)+" -hpddm_gmres_restart "+dim+" -hpddm_max_it "+maxit+(mpirank == 0 ? " -hpddm_verbosity "+pr#verbosity : "")+" -hpddm_orthogonalization mgs",communicator = preffe#prmesh#mpicomm); |
| 674 |
} |
| 675 |
else |
| 676 |
pr#nbiter = IterativeMethod(pr#A, uni,uni, precon = pr#PREC, sparams = "-hpddm_tol "+eps+" -hpddm_variant "+(preffe#prmesh#binexactCS ? "flexible" : sprec)+" -hpddm_gmres_restart "+dim+" -hpddm_max_it "+maxit+(mpirank == 0 ? " -hpddm_verbosity "+pr#verbosity : "")+" -hpddm_orthogonalization mgs",communicator = preffe#prmesh#mpicomm); |
| 677 |
ENDIFMACRO |
| 678 |
|
| 679 |
IFMACRO(pr#withhpddm) |
| 680 |
uni = x0; |
| 681 |
real[int] timings(1); |
| 682 |
set(pr#hpddmOP, sparams = "-hpddm_"+prefix+"schwarz_method "+pr#prec+" -hpddm_"+prefix+"tol "+eps+" -hpddm_"+prefix+"variant "+(preffe#prmesh#binexactCS ? "flexible" : sprec)+" -hpddm_"+prefix+"gmres_restart "+dim+" -hpddm_"+prefix+"max_it "+maxit+" -hpddm_"+prefix+"verbosity "+pr#verbosity+" -hpddm_"+prefix+"orthogonalization mgs", prefix=prefix); |
| 683 |
if (pr#corr == "AD") |
| 684 |
set(pr#hpddmOP, sparams = "-hpddm_"+prefix+"schwarz_coarse_correction additive", prefix=prefix); |
| 685 |
else if (pr#corr == "BNN") |
| 686 |
set(pr#hpddmOP, sparams = "-hpddm_"+prefix+"schwarz_coarse_correction balanced", prefix=prefix); |
| 687 |
else if (pr#corr == "ADEF1") |
| 688 |
set(pr#hpddmOP, sparams = "-hpddm_"+prefix+"schwarz_coarse_correction deflated", prefix=prefix); |
| 689 |
|
| 690 |
if (pr#prec == "oras" || pr#prec == "soras") |
| 691 |
DDM(pr#hpddmOP,rhs,uni,O=pr#aR[mpiRank(preffe#prmesh#commddm)],communicator=preffe#prmesh#commddm,timing=timings); |
| 692 |
else |
| 693 |
DDM(pr#hpddmOP,rhs,uni,communicator=preffe#prmesh#commddm,timing=timings); |
| 694 |
|
| 695 |
/* |
| 696 |
real[int] timingsg(timings.n); |
| 697 |
mpiAllReduce(timings,timingsg,mpiCommWorld,mpiMAX); |
| 698 |
*/ |
| 699 |
pr#tgmres -= timings[0]; |
| 700 |
pr#tfacto += timings[0]; |
| 701 |
ENDIFMACRO |
| 702 |
|
| 703 |
IFMACRO(pr#withpetsc) |
| 704 |
uni = x0; |
| 705 |
uni = pr#petscOP^-1*rhs; |
| 706 |
ENDIFMACRO |
| 707 |
|
| 708 |
IFMACRO(pr#withpetscksp) |
| 709 |
preffe#K[int] lff(x0.n), resff(x0.n), respetsc(pr#petscOP.n); |
| 710 |
func preffe#K[int] pr#petscA(preffe#K[int] &l){ |
| 711 |
ChangeNumbering(pr#petscOP, lff, l, inverse=true, exchange=true); /* petsc -> ffddm numbering */ |
| 712 |
resff = pr#A(lff); |
| 713 |
ChangeNumbering(pr#petscOP, resff, respetsc); /* ffddm -> petsc numbering */ |
| 714 |
return respetsc; |
| 715 |
} |
| 716 |
|
| 717 |
func preffe#K[int] pr#petscPREC(preffe#K[int] &l){ |
| 718 |
ChangeNumbering(pr#petscOP, lff, l, inverse=true, exchange=true); /* petsc -> ffddm numbering */ |
| 719 |
resff = pr#PREC(lff); |
| 720 |
ChangeNumbering(pr#petscOP, resff, respetsc); /* ffddm -> petsc numbering */ |
| 721 |
return respetsc; |
| 722 |
} |
| 723 |
|
| 724 |
Mat<preffe#K> tmpmat(pr#petscOP, pr#petscA); |
| 725 |
pr#petsckspOP = tmpmat; |
| 726 |
set(pr#petsckspOP, precon=pr#petscPREC, sparams = "-prefix_push "+prefix+" -ksp_rtol "+eps+" -ksp_pc_side "+sprec+ " -ksp_gmres_restart "+dim+" -ksp_max_it "+maxit+ (pr#verbosity >= 3 ? " -ksp_monitor_true_residual":"") + " -prefix_pop",prefix = prefix); |
| 727 |
|
| 728 |
uni = x0; |
| 729 |
uni = pr#petsckspOP^-1*rhs; |
| 730 |
ENDIFMACRO |
| 731 |
|
| 732 |
IFMACRO((!pr#withhpddm)&(!pr#withhpddmkrylov)&(!pr#withpetsc)&(!pr#withpetscksp)) |
| 733 |
preffe#K[int,int] Hn(dim+2,dim+1); /* Hessenberg matrix */ |
| 734 |
Hn = 0.; |
| 735 |
|
| 736 |
IFMACRO(pr#withritz) |
| 737 |
preffe#K[int,int] Hessenberg(dim+2,dim+1); /* copy of Hessenberg matrix for Ritz values/harmonics */ |
| 738 |
Hessenberg=0.; |
| 739 |
pr#RitzValues.resize(dim+1); |
| 740 |
for (int i=0;i<dim+1;i++) |
| 741 |
pr#RitzValues[i].resize(i+1); |
| 742 |
pr#RitzHarmonicValues.resize(dim+1); |
| 743 |
for (int i=0;i<dim+1;i++) |
| 744 |
pr#RitzHarmonicValues[i].resize(i+1); |
| 745 |
ENDIFMACRO |
| 746 |
|
| 747 |
preffe#K[int,int] rot(2,dim+2); |
| 748 |
rot = 0.; |
| 749 |
preffe#K[int] g(dim+1),g1(dim+1); |
| 750 |
g = 0.; g1 = 0.; |
| 751 |
|
| 752 |
preffe#K[int] ri(x0.n),zi(x0.n),vi(x0.n),wi(x0.n),x0i(x0.n); |
| 753 |
preffe#K[int][int] Vi(dim+1), Vpi(dim+1); |
| 754 |
for (int i=0;i<dim+1;i++){ |
| 755 |
Vi[i].resize(x0.n); |
| 756 |
Vpi[i].resize(x0.n); |
| 757 |
} |
| 758 |
|
| 759 |
real normb = 0; |
| 760 |
|
| 761 |
if (!preffe#prmesh#excluded) { |
| 762 |
wi = rhs; |
| 763 |
for [i,bi: wi] bi = (abs(bi) > 1.e+20 ? 0 : bi); |
| 764 |
} |
| 765 |
|
| 766 |
if (sprec == "right") |
| 767 |
normb = sqrt(real(pr#prfe#scalprod(wi,wi))); |
| 768 |
else{ |
| 769 |
vi = pr#PREC(wi); |
| 770 |
normb = sqrt(real(pr#prfe#scalprod(vi,vi))); |
| 771 |
} |
| 772 |
if (normb < 1.e-20 || eps < 0) normb = 1.; |
| 773 |
|
| 774 |
real relres = 0; |
| 775 |
|
| 776 |
pr#nbiter = 1; |
| 777 |
pr#reshistory.resize(maxit); |
| 778 |
|
| 779 |
x0i = x0; |
| 780 |
|
| 781 |
while (pr#nbiter <= maxit) { |
| 782 |
|
| 783 |
if (pr#corr=="ADEF2" || pr#corr=="RBNN1" || pr#corr=="RBNN2") |
| 784 |
/* TODO handle restart */ |
| 785 |
x0i = pr#Q(rhs); |
| 786 |
|
| 787 |
ri = pr#A(x0i); |
| 788 |
|
| 789 |
if (!preffe#prmesh#excluded) |
| 790 |
ri -= rhs; |
| 791 |
ri *= -1.0; |
| 792 |
|
| 793 |
if (sprec == "right") |
| 794 |
zi = ri; |
| 795 |
else |
| 796 |
zi = pr#PREC(ri); |
| 797 |
|
| 798 |
g[0] = sqrt(real(pr#prfe#scalprod(zi,zi))); |
| 799 |
|
| 800 |
if(abs(g[0])/normb < abs(eps)) { |
| 801 |
if (pr#verbosity && (mpiRank(preffe#prmesh#mpicomm)==0)) { |
| 802 |
cout << "["+Stringification(pr)+"] GMRES has converged in 0 iteration " << endl; |
| 803 |
cout << "["+Stringification(pr)+"] The relative residual is " + abs(g[0])/normb << endl; |
| 804 |
} |
| 805 |
pr#nbiter = 0; |
| 806 |
uni = x0; |
| 807 |
break; |
| 808 |
} |
| 809 |
|
| 810 |
Vi[0]=1/g[0]*zi; /* first basis vector */ |
| 811 |
|
| 812 |
for(int it=0; it<dim; it++) { |
| 813 |
if (sprec == "right") { |
| 814 |
Vpi[it] = pr#PREC(Vi[it]); |
| 815 |
vi=Vpi[it]; |
| 816 |
wi=pr#A(vi); |
| 817 |
} |
| 818 |
else { |
| 819 |
vi=pr#A(Vi[it]); |
| 820 |
wi = pr#PREC(vi); |
| 821 |
} |
| 822 |
|
| 823 |
for(int i=0; i<it+1; i++) { |
| 824 |
Hn(i,it) = pr#prfe#scalprod(wi,Vi[i]); |
| 825 |
IFMACRO(pr#withritz) |
| 826 |
Hessenberg(i,it)=Hn(i,it); |
| 827 |
ENDIFMACRO |
| 828 |
wi -= conj(Hn(i,it))*Vi[i]; |
| 829 |
} |
| 830 |
Hn(it+1,it) = sqrt(real(pr#prfe#scalprod(wi,wi))); |
| 831 |
IFMACRO(pr#withritz) |
| 832 |
Hessenberg(it+1,it)=Hn(it+1,it); |
| 833 |
ENDIFMACRO |
| 834 |
preffe#K aux = Hn(it+1,it); |
| 835 |
Vi[it+1]=1/aux*wi; |
| 836 |
|
| 837 |
IFMACRO(pr#withritz) |
| 838 |
load "lapack" |
| 839 |
preffe#K[int,int] Hnb1=Hessenberg(0:it,0:it); |
| 840 |
preffe#K[int,int] subHn=Hessenberg(0:it+1,0:it); |
| 841 |
preffe#K[int,int] Hnb1trans=Hnb1'; |
| 842 |
preffe#K[int,int] subHntrans=subHn'; |
| 843 |
preffe#K[int,int] Hnprod = subHntrans*subHn; |
| 844 |
preffe#K[int,int] Hnbinv = Hnb1trans^-1; |
| 845 |
preffe#K[int,int] S = Hnbinv*Hnprod; |
| 846 |
preffe#K[int] ritzharmonicvalues(it+1); |
| 847 |
preffe#K[int,int] ritzharmonicvectors(it+1,it+1); |
| 848 |
IFMACRO(preffe#K,complex) |
| 849 |
zgeev(S,pr#RitzHarmonicValues[it],ritzharmonicvectors); |
| 850 |
ENDIFMACRO |
| 851 |
IFMACRO(preffe#K,real) |
| 852 |
dgeev(S,pr#RitzHarmonicValues[it],ritzharmonicvectors); |
| 853 |
ENDIFMACRO |
| 854 |
pr#RitzHarmonicValues[it].im *= -1; |
| 855 |
preffe#K[int,int] Hnb2=Hessenberg(0:it,0:it); |
| 856 |
preffe#K[int] ritzvalues(it+1); |
| 857 |
preffe#K[int,int] ritzvectors(it+1,it+1); |
| 858 |
IFMACRO(preffe#K,complex) |
| 859 |
zgeev(Hnb2,pr#RitzValues[it],ritzvectors); |
| 860 |
ENDIFMACRO |
| 861 |
IFMACRO(preffe#K,real) |
| 862 |
dgeev(Hnb2,pr#RitzValues[it],ritzvectors); |
| 863 |
ENDIFMACRO |
| 864 |
pr#RitzValues[it].im *= -1; |
| 865 |
ENDIFMACRO |
| 866 |
|
| 867 |
for(int i=0; i<it; i++){ /* QR decomposition of Hn */ |
| 868 |
preffe#K aa = conj(rot(0,i))*Hn(i,it)+conj(rot(1,i))*Hn(i+1,it); |
| 869 |
preffe#K bb = -rot(1,i)*Hn(i,it)+rot(0,i)*Hn(i+1,it); |
| 870 |
Hn(i,it) = aa; |
| 871 |
Hn(i+1,it) = bb; |
| 872 |
} |
| 873 |
|
| 874 |
preffe#K sq = sqrt( conj(Hn(it,it))*Hn(it,it) + Hn(it+1,it)*Hn(it+1,it) ); |
| 875 |
|
| 876 |
rot(0,it) = Hn(it,it)/sq; |
| 877 |
rot(1,it) = Hn(it+1,it)/sq; |
| 878 |
|
| 879 |
Hn(it,it) = conj(rot(0,it))*Hn(it,it)+conj(rot(1,it))*Hn(it+1,it); |
| 880 |
Hn(it+1,it) = 0.; |
| 881 |
g[it+1] = -rot(1,it)*g[it]; |
| 882 |
g[it] = conj(rot(0,it))*g[it]; |
| 883 |
|
| 884 |
preffe#K[int] y(it+1); /* Reconstruct the solution */ |
| 885 |
for(int i=it; i>=0; i--) { |
| 886 |
g1[i] = g[i]; |
| 887 |
for(int j=i+1; j<it+1; j++){ |
| 888 |
g1[i] = g1[i]-Hn(i,j)*y[j]; |
| 889 |
} |
| 890 |
y[i]=g1[i]/Hn(i,i); |
| 891 |
} |
| 892 |
|
| 893 |
uni = x0i; |
| 894 |
for(int i=0;i<it+1;i++){ |
| 895 |
if (sprec == "right") |
| 896 |
uni= uni+ conj(y[i])*Vpi[i]; |
| 897 |
else |
| 898 |
uni= uni+ conj(y[i])*Vi[i]; |
| 899 |
} |
| 900 |
|
| 901 |
real relerr = 0; |
| 902 |
relres = abs(g[it+1]); |
| 903 |
|
| 904 |
/* |
| 905 |
if (pr#bdirect){ |
| 906 |
if (!bpara) { |
| 907 |
pr#ver[] = uni[] - pr#uglob[]; |
| 908 |
relerr = pr#ver[].l2/pr#uglob[].l2; |
| 909 |
} |
| 910 |
else { |
| 911 |
if (!preffe#prmesh#excluded) { |
| 912 |
vi[] = preffe#Rih[mpiRank(preffe#prmesh#commddm)] * pr#uglob[]; |
| 913 |
vi[] -= uni[]; |
| 914 |
} |
| 915 |
relerr = sqrt(real(pr#prfe#scalprod(vi[],vi[]))); |
| 916 |
relerr /= pr#uglob[].l2; |
| 917 |
} |
| 918 |
} |
| 919 |
*/ |
| 920 |
|
| 921 |
if (pr#verbosity >= 3 && (mpiRank(preffe#prmesh#mpicomm)==0)) { |
| 922 |
if (pr#bdirect) |
| 923 |
cout << "["+Stringification(pr)+"] It: "<< pr#nbiter << " Residual = " << relres << " Rel res = " << relres/normb << " Relative L2 Error = "<< relerr << endl; |
| 924 |
else |
| 925 |
cout << "["+Stringification(pr)+"] It: "<< pr#nbiter << " Residual = " << relres << " Rel res = " << relres/normb << endl; |
| 926 |
} |
| 927 |
|
| 928 |
if (pr#verbosity >= 10){ |
| 929 |
IFMACRO(preffe#defplot) |
| 930 |
preffe#Vhi<preffe#K> def(unih); |
| 931 |
unih[] = uni; |
| 932 |
preffe#mplot(preffe#defplot(unih), "["+Stringification(pr)+"] Approximate solution at step " + pr#nbiter) |
| 933 |
ENDIFMACRO |
| 934 |
; |
| 935 |
} |
| 936 |
|
| 937 |
pr#reshistory[pr#nbiter-1] = relres/normb; |
| 938 |
if(relres/normb < abs(eps)) { |
| 939 |
if (pr#verbosity && (mpiRank(preffe#prmesh#mpicomm)==0)) { |
| 940 |
cout << "["+Stringification(pr)+"] GMRES has converged in " + (pr#nbiter) + " iterations " << endl; |
| 941 |
cout << "["+Stringification(pr)+"] The relative residual is " + relres/normb << endl; |
| 942 |
} |
| 943 |
break; |
| 944 |
} |
| 945 |
pr#nbiter++; |
| 946 |
if (pr#nbiter > maxit) |
| 947 |
break; |
| 948 |
} |
| 949 |
if(relres/normb < abs(eps) || pr#nbiter > maxit) |
| 950 |
break; |
| 951 |
|
| 952 |
x0i = uni; |
| 953 |
if (pr#verbosity && (mpiRank(preffe#prmesh#mpicomm)==0)) |
| 954 |
cout << "["+Stringification(pr)+"] GMRES restart(" + dim +")" << endl; |
| 955 |
} |
| 956 |
if(relres/normb >= abs(eps)) |
| 957 |
if (pr#verbosity && (mpiRank(preffe#prmesh#mpicomm)==0)) { |
| 958 |
cout << "["+Stringification(pr)+"] GMRES did not converge in " + maxit + " iterations " << endl; |
| 959 |
cout << "["+Stringification(pr)+"] The relative residual is " + relres/normb << endl; |
| 960 |
} |
| 961 |
ENDIFMACRO |
| 962 |
pr#reshistory.resize(min(maxit,pr#nbiter)); |
| 963 |
preffe#prmesh#toc(pr#tgmresi,"",pr#tgmres) |
| 964 |
return uni; |
| 965 |
} |
| 966 |
else |
| 967 |
return x0; /* dummy, not in comm */ |
| 968 |
} |
| 969 |
/*# finGMRESsolve #*/ |
| 970 |
|
| 971 |
// EOM |
| 972 |
|