Actual source code: relax.h
2: /*
3: This is included by sbaij.c to generate unsigned short and regular versions of these two functions
4: */
6: #if defined(USESHORT)
8: PetscErrorCode MatMult_SeqSBAIJ_1_Hermitian_ushort(Mat A,Vec xx,Vec zz)
9: #else
11: PetscErrorCode MatMult_SeqSBAIJ_1_Hermitian(Mat A,Vec xx,Vec zz)
12: #endif
13: {
14: Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ*)A->data;
15: const PetscScalar *x;
16: PetscScalar *z,x1,sum;
17: const MatScalar *v;
18: MatScalar vj;
19: PetscErrorCode ierr;
20: PetscInt mbs=a->mbs,i,j,nz;
21: const PetscInt *ai=a->i;
22: #if defined(USESHORT)
23: const unsigned short *ib=a->jshort;
24: unsigned short ibt;
25: #else
26: const PetscInt *ib=a->j;
27: PetscInt ibt;
28: #endif
29: PetscInt nonzerorow = 0;
32: VecSet(zz,0.0);
33: VecGetArrayRead(xx,&x);
34: VecGetArray(zz,&z);
36: v = a->a;
37: for (i=0; i<mbs; i++) {
38: nz = ai[i+1] - ai[i]; /* length of i_th row of A */
39: if (!nz) continue; /* Move to the next row if the current row is empty */
40: nonzerorow++;
41: x1 = x[i];
42: sum = v[0]*x1; /* diagonal term */
43: for (j=1; j<nz; j++) {
44: ibt = ib[j];
45: vj = v[j];
46: sum += vj * x[ibt]; /* (strict upper triangular part of A)*x */
47: z[ibt] += PetscConj(v[j]) * x1; /* (strict lower triangular part of A)*x */
48: }
49: z[i] += sum;
50: v += nz;
51: ib += nz;
52: }
54: VecRestoreArrayRead(xx,&x);
55: VecRestoreArray(zz,&z);
56: PetscLogFlops(2.0*(2.0*a->nz - nonzerorow) - nonzerorow);
57: return(0);
58: }
61: #if defined(USESHORT)
63: PetscErrorCode MatMult_SeqSBAIJ_1_ushort(Mat A,Vec xx,Vec zz)
64: #else
66: PetscErrorCode MatMult_SeqSBAIJ_1(Mat A,Vec xx,Vec zz)
67: #endif
68: {
69: Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ*)A->data;
70: const PetscScalar *x;
71: PetscScalar *z,x1,sum;
72: const MatScalar *v;
73: MatScalar vj;
74: PetscErrorCode ierr;
75: PetscInt mbs=a->mbs,i,j,nz;
76: const PetscInt *ai=a->i;
77: #if defined(USESHORT)
78: const unsigned short *ib=a->jshort;
79: unsigned short ibt;
80: #else
81: const PetscInt *ib=a->j;
82: PetscInt ibt;
83: #endif
84: PetscInt nonzerorow=0;
87: VecSet(zz,0.0);
88: VecGetArrayRead(xx,&x);
89: VecGetArray(zz,&z);
91: v = a->a;
92: for (i=0; i<mbs; i++) {
93: nz = ai[i+1] - ai[i]; /* length of i_th row of A */
94: if (!nz) continue; /* Move to the next row if the current row is empty */
95: nonzerorow++;
96: x1 = x[i];
97: sum = v[0]*x1; /* diagonal term */
98: PetscPrefetchBlock(ib+nz,nz,0,PETSC_PREFETCH_HINT_NTA); /* Indices for the next row (assumes same size as this one) */
99: PetscPrefetchBlock(v+nz,nz,0,PETSC_PREFETCH_HINT_NTA); /* Entries for the next row */
100: for (j=1; j<nz; j++) {
101: ibt = ib[j];
102: vj = v[j];
103: z[ibt] += vj * x1; /* (strict lower triangular part of A)*x */
104: sum += vj * x[ibt]; /* (strict upper triangular part of A)*x */
105: }
106: z[i] += sum;
107: v += nz;
108: ib += nz;
109: }
111: VecRestoreArrayRead(xx,&x);
112: VecRestoreArray(zz,&z);
113: PetscLogFlops(2.0*(2.0*a->nz - nonzerorow) - nonzerorow);
114: return(0);
115: }
118: #if defined(USESHORT)
120: PetscErrorCode MatSOR_SeqSBAIJ_ushort(Mat A,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,PetscInt its,PetscInt lits,Vec xx)
121: #else
123: PetscErrorCode MatSOR_SeqSBAIJ(Mat A,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,PetscInt its,PetscInt lits,Vec xx)
124: #endif
125: {
126: Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ*)A->data;
127: const MatScalar *aa=a->a,*v,*v1,*aidiag;
128: PetscScalar *x,*t,sum;
129: const PetscScalar *b;
130: MatScalar tmp;
131: PetscErrorCode ierr;
132: PetscInt m=a->mbs,bs=A->rmap->bs,j;
133: const PetscInt *ai=a->i;
134: #if defined(USESHORT)
135: const unsigned short *aj=a->jshort,*vj,*vj1;
136: #else
137: const PetscInt *aj=a->j,*vj,*vj1;
138: #endif
139: PetscInt nz,nz1,i;
142: if (flag & SOR_EISENSTAT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support yet for Eisenstat");
144: its = its*lits;
145: if (its <= 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Relaxation requires global its %D and local its %D both positive",its,lits);
147: if (bs > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"SSOR for block size > 1 is not yet implemented");
149: VecGetArray(xx,&x);
150: VecGetArrayRead(bb,&b);
152: if (!a->idiagvalid) {
153: if (!a->idiag) {
154: PetscMalloc(m*sizeof(PetscScalar),&a->idiag);
155: }
156: for (i=0; i<a->mbs; i++) a->idiag[i] = 1.0/a->a[a->i[i]];
157: a->idiagvalid = PETSC_TRUE;
158: }
160: if (!a->sor_work) {
161: PetscMalloc(m*sizeof(PetscScalar),&a->sor_work);
162: }
163: t = a->sor_work;
165: aidiag = a->idiag;
167: if (flag == SOR_APPLY_UPPER) {
168: /* apply (U + D/omega) to the vector */
169: PetscScalar d;
170: for (i=0; i<m; i++) {
171: d = fshift + aa[ai[i]];
172: nz = ai[i+1] - ai[i] - 1;
173: vj = aj + ai[i] + 1;
174: v = aa + ai[i] + 1;
175: sum = b[i]*d/omega;
176: PetscSparseDensePlusDot(sum,b,v,vj,nz);
177: x[i] = sum;
178: }
179: PetscLogFlops(a->nz);
180: }
182: if (flag & SOR_ZERO_INITIAL_GUESS) {
183: if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP){
184: PetscMemcpy(t,b,m*sizeof(PetscScalar));
186: v = aa + 1;
187: vj = aj + 1;
188: for (i=0; i<m; i++){
189: nz = ai[i+1] - ai[i] - 1;
190: tmp = - (x[i] = omega*t[i]*aidiag[i]);
191: for (j=0; j<nz; j++) {
192: t[vj[j]] += tmp*v[j];
193: }
194: v += nz + 1;
195: vj += nz + 1;
196: }
197: PetscLogFlops(2*a->nz);
198: }
200: if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP){
201: int nz2;
202: if (!(flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP)){
203: #if defined(PETSC_USE_BACKWARD_LOOP)
204: v = aa + ai[m] - 1;
205: vj = aj + ai[m] - 1;
206: for (i=m-1; i>=0; i--){
207: sum = b[i];
208: nz = ai[i+1] - ai[i] - 1;
209: {PetscInt __i;for(__i=0;__i<nz;__i++) sum -= v[-__i] * x[vj[-__i]];}
210: #else
211: v = aa + ai[m-1] + 1;
212: vj = aj + ai[m-1] + 1;
213: nz = 0;
214: for (i=m-1; i>=0; i--){
215: sum = b[i];
216: nz2 = ai[i] - ai[i-1] - 1;
217: PETSC_Prefetch(v-nz2-1,0,PETSC_PREFETCH_HINT_NTA);
218: PETSC_Prefetch(vj-nz2-1,0,PETSC_PREFETCH_HINT_NTA);
219: PetscSparseDenseMinusDot(sum,x,v,vj,nz);
220: nz = nz2;
221: #endif
222: x[i] = omega*sum*aidiag[i];
223: v -= nz + 1;
224: vj -= nz + 1;
225: }
226: PetscLogFlops(2*a->nz);
227: } else {
228: v = aa + ai[m-1] + 1;
229: vj = aj + ai[m-1] + 1;
230: nz = 0;
231: for (i=m-1; i>=0; i--){
232: sum = t[i];
233: nz2 = ai[i] - ai[i-1] - 1;
234: PETSC_Prefetch(v-nz2-1,0,PETSC_PREFETCH_HINT_NTA);
235: PETSC_Prefetch(vj-nz2-1,0,PETSC_PREFETCH_HINT_NTA);
236: PetscSparseDenseMinusDot(sum,x,v,vj,nz);
237: x[i] = (1-omega)*x[i] + omega*sum*aidiag[i];
238: nz = nz2;
239: v -= nz + 1;
240: vj -= nz + 1;
241: }
242: PetscLogFlops(2*a->nz);
243: }
244: }
245: its--;
246: }
248: while (its--) {
249: /*
250: forward sweep:
251: for i=0,...,m-1:
252: sum[i] = (b[i] - U(i,:)x )/d[i];
253: x[i] = (1-omega)x[i] + omega*sum[i];
254: b = b - x[i]*U^T(i,:);
255:
256: */
257: if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP){
258: PetscMemcpy(t,b,m*sizeof(PetscScalar));
260: for (i=0; i<m; i++){
261: v = aa + ai[i] + 1; v1=v;
262: vj = aj + ai[i] + 1; vj1=vj;
263: nz = ai[i+1] - ai[i] - 1; nz1=nz;
264: sum = t[i];
265: PetscLogFlops(4.0*nz-2);
266: while (nz1--) sum -= (*v1++)*x[*vj1++];
267: x[i] = (1-omega)*x[i] + omega*sum*aidiag[i];
268: while (nz--) t[*vj++] -= x[i]*(*v++);
269: }
270: }
271:
272: if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP){
273: /*
274: backward sweep:
275: b = b - x[i]*U^T(i,:), i=0,...,n-2
276: for i=m-1,...,0:
277: sum[i] = (b[i] - U(i,:)x )/d[i];
278: x[i] = (1-omega)x[i] + omega*sum[i];
279: */
280: /* if there was a forward sweep done above then I thing the next two for loops are not needed */
281: PetscMemcpy(t,b,m*sizeof(PetscScalar));
282:
283: for (i=0; i<m-1; i++){ /* update rhs */
284: v = aa + ai[i] + 1;
285: vj = aj + ai[i] + 1;
286: nz = ai[i+1] - ai[i] - 1;
287: PetscLogFlops(2.0*nz-1);
288: while (nz--) t[*vj++] -= x[i]*(*v++);
289: }
290: for (i=m-1; i>=0; i--){
291: v = aa + ai[i] + 1;
292: vj = aj + ai[i] + 1;
293: nz = ai[i+1] - ai[i] - 1;
294: PetscLogFlops(2.0*nz-1);
295: sum = t[i];
296: while (nz--) sum -= x[*vj++]*(*v++);
297: x[i] = (1-omega)*x[i] + omega*sum*aidiag[i];
298: }
299: }
300: }
302: VecRestoreArray(xx,&x);
303: VecRestoreArrayRead(bb,&b);
304: return(0);
305: }