Debian Codegraph @

packages/aztecoo/example/azoo_iterate_hb/unary.f

raw
1 c@HEADER
2 c ***********************************************************************
3 c
4 c AztecOO: An Object-Oriented Aztec Linear Solver Package
5 c Copyright (2002) Sandia Corporation
6 c
7 c Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 c license for use of this work by or on behalf of the U.S. Government.
9 c
10 c Redistribution and use in source and binary forms, with or without
11 c modification, are permitted provided that the following conditions are
12 c met:
13 c
14 c 1. Redistributions of source code must retain the above copyright
15 c notice, this list of conditions and the following disclaimer.
16 c
17 c 2. Redistributions in binary form must reproduce the above copyright
18 c notice, this list of conditions and the following disclaimer in the
19 c documentation and/or other materials provided with the distribution.
20 c
21 c 3. Neither the name of the Corporation nor the names of the
22 c contributors may be used to endorse or promote products derived from
23 c this software without specific prior written permission.
24 c
25 c THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 c EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 c IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 c PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 c CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 c EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 c PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 c PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 c LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 c NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 c SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 c
37 c Questions? Contact Michael A. Heroux ([email protected])
38 c
39 c ***********************************************************************
40 c@HEADER
41 c
42 c----------------------------------------------------------------------c
43 c S P A R S K I T c
44 c----------------------------------------------------------------------c
45 c UNARY SUBROUTINES MODULE c
46 c----------------------------------------------------------------------c
47 c contents: c
48 c---------- c
49 c submat : extracts a submatrix from a sparse matrix. c
50 c filter : filters elements from a matrix according to their magnitude.c
51 c filterm: same as above, but for the MSR format c
52 c csort : sorts the elements in increasing order of columns c
53 c clncsr : clean up the CSR format matrix, remove duplicate entry, etc c
54 c transp : in-place transposition routine (see also csrcsc in formats) c
55 c copmat : copy of a matrix into another matrix (both stored csr) c
56 c msrcop : copies a matrix in MSR format into a matrix in MSR format c
57 c getelm : returns a(i,j) for any (i,j) from a CSR-stored matrix. c
58 c getdia : extracts a specified diagonal from a matrix. c
59 c getl : extracts lower triangular part c
60 c getu : extracts upper triangular part c
61 c levels : gets the level scheduling structure for lower triangular c
62 c matrices. c
63 c amask : extracts C = A mask M c
64 c rperm : permutes the rows of a matrix (B = P A) c
65 c cperm : permutes the columns of a matrix (B = A Q) c
66 c dperm : permutes both the rows and columns of a matrix (B = P A Q ) c
67 c dperm1 : general extractiob routine (extracts arbitrary rows) c
68 c dperm2 : general submatrix permutation/extraction routine c
69 c dmperm : symmetric permutation of row and column (B=PAP') in MSR fmt c
70 c dvperm : permutes a real vector (in-place) c
71 c ivperm : permutes an integer vector (in-place) c
72 c retmx : returns the max absolute value in each row of the matrix c
73 c diapos : returns the positions of the diagonal elements in A. c
74 c extbdg : extracts the main diagonal blocks of a matrix. c
75 c getbwd : returns the bandwidth information on a matrix. c
76 c blkfnd : finds the block-size of a matrix. c
77 c blkchk : checks whether a given integer is the block size of A. c
78 c infdia : obtains information on the diagonals of A. c
79 c amubdg : gets number of nonzeros in each row of A*B (as well as NNZ) c
80 c aplbdg : gets number of nonzeros in each row of A+B (as well as NNZ) c
81 c rnrms : computes the norms of the rows of A c
82 c cnrms : computes the norms of the columns of A c
83 c roscal : scales the rows of a matrix by their norms. c
84 c coscal : scales the columns of a matrix by their norms. c
85 c addblk : Adds a matrix B into a block of A. c
86 c get1up : Collects the first elements of each row of the upper c
87 c triangular portion of the matrix. c
88 c xtrows : extracts given rows from a matrix in CSR format. c
89 c csrkvstr: Finds block row partitioning of matrix in CSR format c
90 c csrkvstc: Finds block column partitioning of matrix in CSR format c
91 c kvstmerge: Merges block partitionings, for conformal row/col pattern c
92 c----------------------------------------------------------------------c
93 subroutine submat (n,job,i1,i2,j1,j2,a,ja,ia,nr,nc,ao,jao,iao)
94 integer n,job,i1,i2,j1,j2,nr,nc,ia(*),ja(*),jao(*),iao(*)
95 real*8 a(*),ao(*)
96 c-----------------------------------------------------------------------
97 c extracts the submatrix A(i1:i2,j1:j2) and puts the result in
98 c matrix ao,iao,jao
99 c---- In place: ao,jao,iao may be the same as a,ja,ia.
100 c--------------
101 c on input
102 c---------
103 c n = row dimension of the matrix
104 c i1,i2 = two integers with i2 .ge. i1 indicating the range of rows to be
105 c extracted.
106 c j1,j2 = two integers with j2 .ge. j1 indicating the range of columns
107 c to be extracted.
108 c * There is no checking whether the input values for i1, i2, j1,
109 c j2 are between 1 and n.
110 c a,
111 c ja,
112 c ia = matrix in compressed sparse row format.
113 c
114 c job = job indicator: if job .ne. 1 then the real values in a are NOT
115 c extracted, only the column indices (i.e. data structure) are.
116 c otherwise values as well as column indices are extracted...
117 c
118 c on output
119 c--------------
120 c nr = number of rows of submatrix
121 c nc = number of columns of submatrix
122 c * if either of nr or nc is nonpositive the code will quit.
123 c
124 c ao,
125 c jao,iao = extracted matrix in general sparse format with jao containing
126 c the column indices,and iao being the pointer to the beginning
127 c of the row,in arrays a,ja.
128 c----------------------------------------------------------------------c
129 c Y. Saad, Sep. 21 1989 c
130 c----------------------------------------------------------------------c
131 nr = i2-i1+1
132 nc = j2-j1+1
133 c
134 if ( nr .le. 0 .or. nc .le. 0) return
135 c
136 klen = 0
137 c
138 c simple procedure. proceeds row-wise...
139 c
140 do 100 i = 1,nr
141 ii = i1+i-1
142 k1 = ia(ii)
143 k2 = ia(ii+1)-1
144 iao(i) = klen+1
145 c-----------------------------------------------------------------------
146 do 60 k=k1,k2
147 j = ja(k)
148 if (j .ge. j1 .and. j .le. j2) then
149 klen = klen+1
150 if (job .eq. 1) ao(klen) = a(k)
151 jao(klen) = j - j1+1
152 endif
153 60 continue
154 100 continue
155 iao(nr+1) = klen+1
156 return
157 c------------end-of submat----------------------------------------------
158 c-----------------------------------------------------------------------
159 end
160 c-----------------------------------------------------------------------
161 subroutine filter(n,job,drptol,a,ja,ia,b,jb,ib,len,ierr)
162 real*8 a(*),b(*),drptol
163 integer ja(*),jb(*),ia(*),ib(*),n,job,len,ierr
164 c-----------------------------------------------------------------------
165 c This module removes any elements whose absolute value
166 c is small from an input matrix A and puts the resulting
167 c matrix in B. The input parameter job selects a definition
168 c of small.
169 c-----------------------------------------------------------------------
170 c on entry:
171 c---------
172 c n = integer. row dimension of matrix
173 c job = integer. used to determine strategy chosen by caller to
174 c drop elements from matrix A.
175 c job = 1
176 c Elements whose absolute value is less than the
177 c drop tolerance are removed.
178 c job = 2
179 c Elements whose absolute value is less than the
180 c product of the drop tolerance and the Euclidean
181 c norm of the row are removed.
182 c job = 3
183 c Elements whose absolute value is less that the
184 c product of the drop tolerance and the largest
185 c element in the row are removed.
186 c
187 c drptol = real. drop tolerance used for dropping strategy.
188 c a
189 c ja
190 c ia = input matrix in compressed sparse format
191 c len = integer. the amount of space available in arrays b and jb.
192 c
193 c on return:
194 c----------
195 c b
196 c jb
197 c ib = resulting matrix in compressed sparse format.
198 c
199 c ierr = integer. containing error message.
200 c ierr .eq. 0 indicates normal return
201 c ierr .gt. 0 indicates that there is'nt enough
202 c space is a and ja to store the resulting matrix.
203 c ierr then contains the row number where filter stopped.
204 c note:
205 c------ This module is in place. (b,jb,ib can ne the same as
206 c a, ja, ia in which case the result will be overwritten).
207 c----------------------------------------------------------------------c
208 c contributed by David Day, Sep 19, 1989. c
209 c----------------------------------------------------------------------c
210 c local variables
211 real*8 norm,loctol
212 integer index,row,k,k1,k2
213 c
214 index = 1
215 do 10 row= 1,n
216 k1 = ia(row)
217 k2 = ia(row+1) - 1
218 ib(row) = index
219 goto (100,200,300) job
220 100 norm = 1.0d0
221 goto 400
222 200 norm = 0.0d0
223 do 22 k = k1,k2
224 norm = norm + a(k) * a(k)
225 22 continue
226 norm = sqrt(norm)
227 goto 400
228 300 norm = 0.0d0
229 do 23 k = k1,k2
230 if( abs(a(k)) .gt. norm) then
231 norm = abs(a(k))
232 endif
233 23 continue
234 400 loctol = drptol * norm
235 do 30 k = k1,k2
236 if( abs(a(k)) .gt. loctol)then
237 if (index .gt. len) then
238 ierr = row
239 return
240 endif
241 b(index) = a(k)
242 jb(index) = ja(k)
243 index = index + 1
244 endif
245 30 continue
246 10 continue
247 ib(n+1) = index
248 return
249 c--------------------end-of-filter -------------------------------------
250 c-----------------------------------------------------------------------
251 end
252 c-----------------------------------------------------------------------
253 subroutine filterm (n,job,drop,a,ja,b,jb,len,ierr)
254 real*8 a(*),b(*),drop
255 integer ja(*),jb(*),n,job,len,ierr
256 c-----------------------------------------------------------------------
257 c This subroutine removes any elements whose absolute value
258 c is small from an input matrix A. Same as filter but
259 c uses the MSR format.
260 c-----------------------------------------------------------------------
261 c on entry:
262 c---------
263 c n = integer. row dimension of matrix
264 c job = integer. used to determine strategy chosen by caller to
265 c drop elements from matrix A.
266 c job = 1
267 c Elements whose absolute value is less than the
268 c drop tolerance are removed.
269 c job = 2
270 c Elements whose absolute value is less than the
271 c product of the drop tolerance and the Euclidean
272 c norm of the row are removed.
273 c job = 3
274 c Elements whose absolute value is less that the
275 c product of the drop tolerance and the largest
276 c element in the row are removed.
277 c
278 c drop = real. drop tolerance used for dropping strategy.
279 c a
280 c ja = input matrix in Modifief Sparse Row format
281 c len = integer. the amount of space in arrays b and jb.
282 c
283 c on return:
284 c----------
285 c
286 c b, jb = resulting matrix in Modifief Sparse Row format
287 c
288 c ierr = integer. containing error message.
289 c ierr .eq. 0 indicates normal return
290 c ierr .gt. 0 indicates that there is'nt enough
291 c space is a and ja to store the resulting matrix.
292 c ierr then contains the row number where filter stopped.
293 c note:
294 c------ This module is in place. (b,jb can ne the same as
295 c a, ja in which case the result will be overwritten).
296 c----------------------------------------------------------------------c
297 c contributed by David Day, Sep 19, 1989. c
298 c----------------------------------------------------------------------c
299 c local variables
300 c
301 real*8 norm,loctol
302 integer index,row,k,k1,k2
303 c
304 index = n+2
305 do 10 row= 1,n
306 k1 = ja(row)
307 k2 = ja(row+1) - 1
308 jb(row) = index
309 goto (100,200,300) job
310 100 norm = 1.0d0
311 goto 400
312 200 norm = a(row)**2
313 do 22 k = k1,k2
314 norm = norm + a(k) * a(k)
315 22 continue
316 norm = sqrt(norm)
317 goto 400
318 300 norm = abs(a(row))
319 do 23 k = k1,k2
320 norm = max(abs(a(k)),norm)
321 23 continue
322 400 loctol = drop * norm
323 do 30 k = k1,k2
324 if( abs(a(k)) .gt. loctol)then
325 if (index .gt. len) then
326 ierr = row
327 return
328 endif
329 b(index) = a(k)
330 jb(index) = ja(k)
331 index = index + 1
332 endif
333 30 continue
334 10 continue
335 jb(n+1) = index
336 return
337 c--------------------end-of-filterm-------------------------------------
338 c-----------------------------------------------------------------------
339 end
340 c-----------------------------------------------------------------------
341 subroutine csort (n,a,ja,ia,iwork,values)
342 logical values
343 integer n, ja(*), ia(n+1), iwork(*)
344 real*8 a(*)
345 c-----------------------------------------------------------------------
346 c This routine sorts the elements of a matrix (stored in Compressed
347 c Sparse Row Format) in increasing order of their column indices within
348 c each row. It uses a form of bucket sort with a cost of O(nnz) where
349 c nnz = number of nonzero elements.
350 c requires an integer work array of length 2*nnz.
351 c-----------------------------------------------------------------------
352 c on entry:
353 c---------
354 c n = the row dimension of the matrix
355 c a = the matrix A in compressed sparse row format.
356 c ja = the array of column indices of the elements in array a.
357 c ia = the array of pointers to the rows.
358 c iwork = integer work array of length max ( n+1, 2*nnz )
359 c where nnz = (ia(n+1)-ia(1)) ) .
360 c values= logical indicating whether or not the real values a(*) must
361 c also be permuted. if (.not. values) then the array a is not
362 c touched by csort and can be a dummy array.
363 c
364 c on return:
365 c----------
366 c the matrix stored in the structure a, ja, ia is permuted in such a
367 c way that the column indices are in increasing order within each row.
368 c iwork(1:nnz) contains the permutation used to rearrange the elements.
369 c-----------------------------------------------------------------------
370 c Y. Saad - Feb. 1, 1991.
371 c-----------------------------------------------------------------------
372 c local variables
373 integer i, k, j, ifirst, nnz, next
374 c
375 c count the number of elements in each column
376 c
377 do 1 i=1,n+1
378 iwork(i) = 0
379 1 continue
380 do 3 i=1, n
381 do 2 k=ia(i), ia(i+1)-1
382 j = ja(k)+1
383 iwork(j) = iwork(j)+1
384 2 continue
385 3 continue
386 c
387 c compute pointers from lengths.
388 c
389 iwork(1) = 1
390 do 4 i=1,n
391 iwork(i+1) = iwork(i) + iwork(i+1)
392 4 continue
393 c
394 c get the positions of the nonzero elements in order of columns.
395 c
396 ifirst = ia(1)
397 nnz = ia(n+1)-ifirst
398 do 5 i=1,n
399 do 51 k=ia(i),ia(i+1)-1
400 j = ja(k)
401 next = iwork(j)
402 iwork(nnz+next) = k
403 iwork(j) = next+1
404 51 continue
405 5 continue
406 c
407 c convert to coordinate format
408 c
409 do 6 i=1, n
410 do 61 k=ia(i), ia(i+1)-1
411 iwork(k) = i
412 61 continue
413 6 continue
414 c
415 c loop to find permutation: for each element find the correct
416 c position in (sorted) arrays a, ja. Record this in iwork.
417 c
418 do 7 k=1, nnz
419 ko = iwork(nnz+k)
420 irow = iwork(ko)
421 next = ia(irow)
422 c
423 c the current element should go in next position in row. iwork
424 c records this position.
425 c
426 iwork(ko) = next
427 ia(irow) = next+1
428 7 continue
429 c
430 c perform an in-place permutation of the arrays.
431 c
432 call ivperm (nnz, ja(ifirst), iwork)
433 if (values) call dvperm (nnz, a(ifirst), iwork)
434 c
435 c reshift the pointers of the original matrix back.
436 c
437 do 8 i=n,1,-1
438 ia(i+1) = ia(i)
439 8 continue
440 ia(1) = ifirst
441 c
442 return
443 c---------------end-of-csort--------------------------------------------
444 c-----------------------------------------------------------------------
445 end
446 c-----------------------------------------------------------------------
447 subroutine clncsr(job,value2,nrow,a,ja,ia,indu,iwk)
448 c .. Scalar Arguments ..
449 integer job, nrow, value2
450 c ..
451 c .. Array Arguments ..
452 integer ia(nrow+1),indu(nrow),iwk(nrow+1),ja(*)
453 real*8 a(*)
454 c ..
455 c
456 c This routine performs two tasks to clean up a CSR matrix
457 c -- remove duplicate/zero entries,
458 c -- perform a partial ordering, new order lower triangular part,
459 c main diagonal, upper triangular part.
460 c
461 c On entry:
462 c
463 c job = options
464 c 0 -- nothing is done
465 c 1 -- eliminate duplicate entries, zero entries.
466 c 2 -- eliminate duplicate entries and perform partial ordering.
467 c 3 -- eliminate duplicate entries, sort the entries in the
468 c increasing order of clumn indices.
469 c
470 c value2 -- 0 the matrix is pattern only (a is not touched)
471 c 1 matrix has values too.
472 c nrow -- row dimension of the matrix
473 c a,ja,ia -- input matrix in CSR format
474 c
475 c On return:
476 c a,ja,ia -- cleaned matrix
477 c indu -- pointers to the beginning of the upper triangular
478 c portion if job > 1
479 c
480 c Work space:
481 c iwk -- integer work space of size nrow+1
482 c
483 c .. Local Scalars ..
484 integer i,j,k,ko,ipos,kfirst,klast
485 real*8 tmp
486 c ..
487 c
488 if (job.le.0) return
489 c
490 c .. eliminate duplicate entries --
491 c array INDU is used as marker for existing indices, it is also the
492 c location of the entry.
493 c IWK is used to stored the old IA array.
494 c matrix is copied to squeeze out the space taken by the duplicated
495 c entries.
496 c
497 do 90 i = 1, nrow
498 indu(i) = 0
499 iwk(i) = ia(i)
500 90 continue
501 iwk(nrow+1) = ia(nrow+1)
502 k = 1
503 do 120 i = 1, nrow
504 ia(i) = k
505 ipos = iwk(i)
506 klast = iwk(i+1)
507 100 if (ipos.lt.klast) then
508 j = ja(ipos)
509 if (indu(j).eq.0) then
510 c .. new entry ..
511 if (value2.ne.0) then
512 if (a(ipos) .ne. 0.0D0) then
513 indu(j) = k
514 ja(k) = ja(ipos)
515 a(k) = a(ipos)
516 k = k + 1
517 endif
518 else
519 indu(j) = k
520 ja(k) = ja(ipos)
521 k = k + 1
522 endif
523 else if (value2.ne.0) then
524 c .. duplicate entry ..
525 a(indu(j)) = a(indu(j)) + a(ipos)
526 endif
527 ipos = ipos + 1
528 go to 100
529 endif
530 c .. remove marks before working on the next row ..
531 do 110 ipos = ia(i), k - 1
532 indu(ja(ipos)) = 0
533 110 continue
534 120 continue
535 ia(nrow+1) = k
536 if (job.le.1) return
537 c
538 c .. partial ordering ..
539 c split the matrix into strict upper/lower triangular
540 c parts, INDU points to the the beginning of the upper part.
541 c
542 do 140 i = 1, nrow
543 klast = ia(i+1) - 1
544 kfirst = ia(i)
545 130 if (klast.gt.kfirst) then
546 if (ja(klast).lt.i .and. ja(kfirst).ge.i) then
547 c .. swap klast with kfirst ..
548 j = ja(klast)
549 ja(klast) = ja(kfirst)
550 ja(kfirst) = j
551 if (value2.ne.0) then
552 tmp = a(klast)
553 a(klast) = a(kfirst)
554 a(kfirst) = tmp
555 endif
556 endif
557 if (ja(klast).ge.i)
558 & klast = klast - 1
559 if (ja(kfirst).lt.i)
560 & kfirst = kfirst + 1
561 go to 130
562 endif
563 c
564 if (ja(klast).lt.i) then
565 indu(i) = klast + 1
566 else
567 indu(i) = klast
568 endif
569 140 continue
570 if (job.le.2) return
571 c
572 c .. order the entries according to column indices
573 c burble-sort is used
574 c
575 do 190 i = 1, nrow
576 do 160 ipos = ia(i), indu(i)-1
577 do 150 j = indu(i)-1, ipos+1, -1
578 k = j - 1
579 if (ja(k).gt.ja(j)) then
580 ko = ja(k)
581 ja(k) = ja(j)
582 ja(j) = ko
583 if (value2.ne.0) then
584 tmp = a(k)
585 a(k) = a(j)
586 a(j) = tmp
587 endif
588 endif
589 150 continue
590 160 continue
591 do 180 ipos = indu(i), ia(i+1)-1
592 do 170 j = ia(i+1)-1, ipos+1, -1
593 k = j - 1
594 if (ja(k).gt.ja(j)) then
595 ko = ja(k)
596 ja(k) = ja(j)
597 ja(j) = ko
598 if (value2.ne.0) then
599 tmp = a(k)
600 a(k) = a(j)
601 a(j) = tmp
602 endif
603 endif
604 170 continue
605 180 continue
606 190 continue
607 return
608 c---- end of clncsr ----------------------------------------------------
609 c-----------------------------------------------------------------------
610 end
611 c-----------------------------------------------------------------------
612 subroutine copmat (nrow,a,ja,ia,ao,jao,iao,ipos,job)
613 real*8 a(*),ao(*)
614 integer nrow, ia(*),ja(*),jao(*),iao(*), ipos, job
615 c----------------------------------------------------------------------
616 c copies the matrix a, ja, ia, into the matrix ao, jao, iao.
617 c----------------------------------------------------------------------
618 c on entry:
619 c---------
620 c nrow = row dimension of the matrix
621 c a,
622 c ja,
623 c ia = input matrix in compressed sparse row format.
624 c ipos = integer. indicates the position in the array ao, jao
625 c where the first element should be copied. Thus
626 c iao(1) = ipos on return.
627 c job = job indicator. if (job .ne. 1) the values are not copies
628 c (i.e., pattern only is copied in the form of arrays ja, ia).
629 c
630 c on return:
631 c----------
632 c ao,
633 c jao,
634 c iao = output matrix containing the same data as a, ja, ia.
635 c-----------------------------------------------------------------------
636 c Y. Saad, March 1990.
637 c-----------------------------------------------------------------------
638 c local variables
639 integer kst, i, k
640 c
641 kst = ipos -ia(1)
642 do 100 i = 1, nrow+1
643 iao(i) = ia(i) + kst
644 100 continue
645 c
646 do 200 k=ia(1), ia(nrow+1)-1
647 jao(kst+k)= ja(k)
648 200 continue
649 c
650 if (job .ne. 1) return
651 do 201 k=ia(1), ia(nrow+1)-1
652 ao(kst+k) = a(k)
653 201 continue
654 c
655 return
656 c--------end-of-copmat -------------------------------------------------
657 c-----------------------------------------------------------------------
658 end
659 c-----------------------------------------------------------------------
660 subroutine msrcop (nrow,a,ja,ao,jao,job)
661 real*8 a(*),ao(*)
662 integer nrow, ja(*),jao(*), job
663 c----------------------------------------------------------------------
664 c copies the MSR matrix a, ja, into the MSR matrix ao, jao
665 c----------------------------------------------------------------------
666 c on entry:
667 c---------
668 c nrow = row dimension of the matrix
669 c a,ja = input matrix in Modified compressed sparse row format.
670 c job = job indicator. Values are not copied if job .ne. 1
671 c
672 c on return:
673 c----------
674 c ao, jao = output matrix containing the same data as a, ja.
675 c-----------------------------------------------------------------------
676 c Y. Saad,
677 c-----------------------------------------------------------------------
678 c local variables
679 integer i, k
680 c
681 do 100 i = 1, nrow+1
682 jao(i) = ja(i)
683 100 continue
684 c
685 do 200 k=ja(1), ja(nrow+1)-1
686 jao(k)= ja(k)
687 200 continue
688 c
689 if (job .ne. 1) return
690 do 201 k=ja(1), ja(nrow+1)-1
691 ao(k) = a(k)
692 201 continue
693 do 202 k=1,nrow
694 ao(k) = a(k)
695 202 continue
696 c
697 return
698 c--------end-of-msrcop -------------------------------------------------
699 c-----------------------------------------------------------------------
700 end
701 c-----------------------------------------------------------------------
702 double precision function getelm (i,j,a,ja,ia,iadd,sorted)
703 c-----------------------------------------------------------------------
704 c purpose:
705 c --------
706 c this function returns the element a(i,j) of a matrix a,
707 c for any pair (i,j). the matrix is assumed to be stored
708 c in compressed sparse row (csr) format. getelm performs a
709 c binary search in the case where it is known that the elements
710 c are sorted so that the column indices are in increasing order.
711 c also returns (in iadd) the address of the element a(i,j) in
712 c arrays a and ja when the search is successsful (zero if not).
713 c-----
714 c first contributed by noel nachtigal (mit).
715 c recoded jan. 20, 1991, by y. saad [in particular
716 c added handling of the non-sorted case + the iadd output]
717 c-----------------------------------------------------------------------
718 c parameters:
719 c -----------
720 c on entry:
721 c----------
722 c i = the row index of the element sought (input).
723 c j = the column index of the element sought (input).
724 c a = the matrix a in compressed sparse row format (input).
725 c ja = the array of column indices (input).
726 c ia = the array of pointers to the rows' data (input).
727 c sorted = logical indicating whether the matrix is knonw to
728 c have its column indices sorted in increasing order
729 c (sorted=.true.) or not (sorted=.false.).
730 c (input).
731 c on return:
732 c-----------
733 c getelm = value of a(i,j).
734 c iadd = address of element a(i,j) in arrays a, ja if found,
735 c zero if not found. (output)
736 c
737 c note: the inputs i and j are not checked for validity.
738 c-----------------------------------------------------------------------
739 c noel m. nachtigal october 28, 1990 -- youcef saad jan 20, 1991.
740 c-----------------------------------------------------------------------
741 integer i, ia(*), iadd, j, ja(*)
742 double precision a(*)
743 logical sorted
744 c
745 c local variables.
746 c
747 integer ibeg, iend, imid, k
748 c
749 c initialization
750 c
751 iadd = 0
752 getelm = 0.0
753 ibeg = ia(i)
754 iend = ia(i+1)-1
755 c
756 c case where matrix is not necessarily sorted
757 c
758 if (.not. sorted) then
759 c
760 c scan the row - exit as soon as a(i,j) is found
761 c
762 do 5 k=ibeg, iend
763 if (ja(k) .eq. j) then
764 iadd = k
765 goto 20
766 endif
767 5 continue
768 c
769 c end unsorted case. begin sorted case
770 c
771 else
772 c
773 c begin binary search. compute the middle index.
774 c
775 10 imid = ( ibeg + iend ) / 2
776 c
777 c test if found
778 c
779 if (ja(imid).eq.j) then
780 iadd = imid
781 goto 20
782 endif
783 if (ibeg .ge. iend) goto 20
784 c
785 c else update the interval bounds.
786 c
787 if (ja(imid).gt.j) then
788 iend = imid -1
789 else
790 ibeg = imid +1
791 endif
792 goto 10
793 c
794 c end both cases
795 c
796 endif
797 c
798 20 if (iadd .ne. 0) getelm = a(iadd)
799 c
800 return
801 c--------end-of-getelm--------------------------------------------------
802 c-----------------------------------------------------------------------
803 end
804 c-----------------------------------------------------------------------
805 subroutine getdia (nrow,ncol,job,a,ja,ia,len,diag,idiag,ioff)
806 real*8 diag(*),a(*)
807 integer nrow, ncol, job, len, ioff, ia(*), ja(*), idiag(*)
808 c-----------------------------------------------------------------------
809 c this subroutine extracts a given diagonal from a matrix stored in csr
810 c format. the output matrix may be transformed with the diagonal removed
811 c from it if desired (as indicated by job.)
812 c-----------------------------------------------------------------------
813 c our definition of a diagonal of matrix is a vector of length nrow
814 c (always) which contains the elements in rows 1 to nrow of
815 c the matrix that are contained in the diagonal offset by ioff
816 c with respect to the main diagonal. if the diagonal element
817 c falls outside the matrix then it is defined as a zero entry.
818 c thus the proper definition of diag(*) with offset ioff is
819 c
820 c diag(i) = a(i,ioff+i) i=1,2,...,nrow
821 c with elements falling outside the matrix being defined as zero.
822 c
823 c-----------------------------------------------------------------------
824 c
825 c on entry:
826 c----------
827 c
828 c nrow = integer. the row dimension of the matrix a.
829 c ncol = integer. the column dimension of the matrix a.
830 c job = integer. job indicator. if job = 0 then
831 c the matrix a, ja, ia, is not altered on return.
832 c if job.ne.0 then getdia will remove the entries
833 c collected in diag from the original matrix.
834 c this is done in place.
835 c
836 c a,ja,
837 c ia = matrix stored in compressed sparse row a,ja,ia,format
838 c ioff = integer,containing the offset of the wanted diagonal
839 c the diagonal extracted is the one corresponding to the
840 c entries a(i,j) with j-i = ioff.
841 c thus ioff = 0 means the main diagonal
842 c
843 c on return:
844 c-----------
845 c len = number of nonzero elements found in diag.
846 c (len .le. min(nrow,ncol-ioff)-max(1,1-ioff) + 1 )
847 c
848 c diag = real*8 array of length nrow containing the wanted diagonal.
849 c diag contains the diagonal (a(i,j),j-i = ioff ) as defined
850 c above.
851 c
852 c idiag = integer array of length len, containing the poisitions
853 c in the original arrays a and ja of the diagonal elements
854 c collected in diag. a zero entry in idiag(i) means that
855 c there was no entry found in row i belonging to the diagonal.
856 c
857 c a, ja,
858 c ia = if job .ne. 0 the matrix is unchanged. otherwise the nonzero
859 c diagonal entries collected in diag are removed from the
860 c matrix and therefore the arrays a, ja, ia will change.
861 c (the matrix a, ja, ia will contain len fewer elements)
862 c
863 c----------------------------------------------------------------------c
864 c Y. Saad, sep. 21 1989 - modified and retested Feb 17, 1996. c
865 c----------------------------------------------------------------------c
866 c local variables
867 integer istart, max, iend, i, kold, k, kdiag, ko
868 c
869 istart = max(0,-ioff)
870 iend = min(nrow,ncol-ioff)
871 len = 0
872 do 1 i=1,nrow
873 idiag(i) = 0
874 diag(i) = 0.0d0
875 1 continue
876 c
877 c extract diagonal elements
878 c
879 do 6 i=istart+1, iend
880 do 51 k= ia(i),ia(i+1) -1
881 if (ja(k)-i .eq. ioff) then
882 diag(i)= a(k)
883 idiag(i) = k
884 len = len+1
885 goto 6
886 endif
887 51 continue
888 6 continue
889 if (job .eq. 0 .or. len .eq.0) return
890 c
891 c remove diagonal elements and rewind structure
892 c
893 ko = 0
894 do 7 i=1, nrow
895 kold = ko
896 kdiag = idiag(i)
897 do 71 k= ia(i), ia(i+1)-1
898 if (k .ne. kdiag) then
899 ko = ko+1
900 a(ko) = a(k)
901 ja(ko) = ja(k)
902 endif
903 71 continue
904 ia(i) = kold+1
905 7 continue
906 c
907 c redefine ia(nrow+1)
908 c
909 ia(nrow+1) = ko+1
910 return
911 c------------end-of-getdia----------------------------------------------
912 c-----------------------------------------------------------------------
913 end
914 c-----------------------------------------------------------------------
915 subroutine transp (nrow,ncol,a,ja,ia,iwk,ierr)
916 integer nrow, ncol, ia(*), ja(*), iwk(*), ierr
917 real*8 a(*)
918 c------------------------------------------------------------------------
919 c In-place transposition routine.
920 c------------------------------------------------------------------------
921 c this subroutine transposes a matrix stored in compressed sparse row
922 c format. the transposition is done in place in that the arrays a,ja,ia
923 c of the transpose are overwritten onto the original arrays.
924 c------------------------------------------------------------------------
925 c on entry:
926 c---------
927 c nrow = integer. The row dimension of A.
928 c ncol = integer. The column dimension of A.
929 c a = real array of size nnz (number of nonzero elements in A).
930 c containing the nonzero elements
931 c ja = integer array of length nnz containing the column positions
932 c of the corresponding elements in a.
933 c ia = integer of size n+1, where n = max(nrow,ncol). On entry
934 c ia(k) contains the position in a,ja of the beginning of
935 c the k-th row.
936 c
937 c iwk = integer work array of same length as ja.
938 c
939 c on return:
940 c----------
941 c
942 c ncol = actual row dimension of the transpose of the input matrix.
943 c Note that this may be .le. the input value for ncol, in
944 c case some of the last columns of the input matrix are zero
945 c columns. In the case where the actual number of rows found
946 c in transp(A) exceeds the input value of ncol, transp will
947 c return without completing the transposition. see ierr.
948 c a,
949 c ja,
950 c ia = contains the transposed matrix in compressed sparse
951 c row format. The row dimension of a, ja, ia is now ncol.
952 c
953 c ierr = integer. error message. If the number of rows for the
954 c transposed matrix exceeds the input value of ncol,
955 c then ierr is set to that number and transp quits.
956 c Otherwise ierr is set to 0 (normal return).
957 c
958 c Note:
959 c----- 1) If you do not need the transposition to be done in place
960 c it is preferrable to use the conversion routine csrcsc
961 c (see conversion routines in formats).
962 c 2) the entries of the output matrix are not sorted (the column
963 c indices in each are not in increasing order) use csrcsc
964 c if you want them sorted.
965 c----------------------------------------------------------------------c
966 c Y. Saad, Sep. 21 1989 c
967 c modified Oct. 11, 1989. c
968 c----------------------------------------------------------------------c
969 c local variables
970 real*8 t, t1
971 ierr = 0
972 nnz = ia(nrow+1)-1
973 c
974 c determine column dimension
975 c
976 jcol = 0
977 do 1 k=1, nnz
978 jcol = max(jcol,ja(k))
979 1 continue
980 if (jcol .gt. ncol) then
981 ierr = jcol
982 return
983 endif
984 c
985 c convert to coordinate format. use iwk for row indices.
986 c
987 ncol = jcol
988 c
989 do 3 i=1,nrow
990 do 2 k=ia(i),ia(i+1)-1
991 iwk(k) = i
992 2 continue
993 3 continue
994 c find pointer array for transpose.
995 do 35 i=1,ncol+1
996 ia(i) = 0
997 35 continue
998 do 4 k=1,nnz
999 i = ja(k)
1000 ia(i+1) = ia(i+1)+1
1001 4 continue
1002 ia(1) = 1
1003 c------------------------------------------------------------------------
1004 do 44 i=1,ncol
1005 ia(i+1) = ia(i) + ia(i+1)
1006 44 continue
1007 c
1008 c loop for a cycle in chasing process.
1009 c
1010 init = 1
1011 k = 0
1012 5 t = a(init)
1013 i = ja(init)
1014 j = iwk(init)
1015 iwk(init) = -1
1016 c------------------------------------------------------------------------
1017 6 k = k+1
1018 c current row number is i. determine where to go.
1019 l = ia(i)
1020 c save the chased element.
1021 t1 = a(l)
1022 inext = ja(l)
1023 c then occupy its location.
1024 a(l) = t
1025 ja(l) = j
1026 c update pointer information for next element to be put in row i.
1027 ia(i) = l+1
1028 c determine next element to be chased
1029 if (iwk(l) .lt. 0) goto 65
1030 t = t1
1031 i = inext
1032 j = iwk(l)
1033 iwk(l) = -1
1034 if (k .lt. nnz) goto 6
1035 goto 70
1036 65 init = init+1
1037 if (init .gt. nnz) goto 70
1038 if (iwk(init) .lt. 0) goto 65
1039 c restart chasing --
1040 goto 5
1041 70 continue
1042 do 80 i=ncol,1,-1
1043 ia(i+1) = ia(i)
1044 80 continue
1045 ia(1) = 1
1046 c
1047 return
1048 c------------------end-of-transp ----------------------------------------
1049 c------------------------------------------------------------------------
1050 end
1051 c------------------------------------------------------------------------
1052 subroutine getl (n,a,ja,ia,ao,jao,iao)
1053 integer n, ia(*), ja(*), iao(*), jao(*)
1054 real*8 a(*), ao(*)
1055 c------------------------------------------------------------------------
1056 c this subroutine extracts the lower triangular part of a matrix
1057 c and writes the result ao, jao, iao. The routine is in place in
1058 c that ao, jao, iao can be the same as a, ja, ia if desired.
1059 c-----------
1060 c on input:
1061 c
1062 c n = dimension of the matrix a.
1063 c a, ja,
1064 c ia = matrix stored in compressed sparse row format.
1065 c On return:
1066 c ao, jao,
1067 c iao = lower triangular matrix (lower part of a)
1068 c stored in a, ja, ia, format
1069 c note: the diagonal element is the last element in each row.
1070 c i.e. in a(ia(i+1)-1 )
1071 c ao, jao, iao may be the same as a, ja, ia on entry -- in which case
1072 c getl will overwrite the result on a, ja, ia.
1073 c
1074 c------------------------------------------------------------------------
1075 c local variables
1076 real*8 t
1077 integer ko, kold, kdiag, k, i
1078 c
1079 c inititialize ko (pointer for output matrix)
1080 c
1081 ko = 0
1082 do 7 i=1, n
1083 kold = ko
1084 kdiag = 0
1085 do 71 k = ia(i), ia(i+1) -1
1086 if (ja(k) .gt. i) goto 71
1087 ko = ko+1
1088 ao(ko) = a(k)
1089 jao(ko) = ja(k)
1090 if (ja(k) .eq. i) kdiag = ko
1091 71 continue
1092 if (kdiag .eq. 0 .or. kdiag .eq. ko) goto 72
1093 c
1094 c exchange
1095 c
1096 t = ao(kdiag)
1097 ao(kdiag) = ao(ko)
1098 ao(ko) = t
1099 c
1100 k = jao(kdiag)
1101 jao(kdiag) = jao(ko)
1102 jao(ko) = k
1103 72 iao(i) = kold+1
1104 7 continue
1105 c redefine iao(n+1)
1106 iao(n+1) = ko+1
1107 return
1108 c----------end-of-getl -------------------------------------------------
1109 c-----------------------------------------------------------------------
1110 end
1111 c-----------------------------------------------------------------------
1112 subroutine getu (n,a,ja,ia,ao,jao,iao)
1113 integer n, ia(*), ja(*), iao(*), jao(*)
1114 real*8 a(*), ao(*)
1115 c------------------------------------------------------------------------
1116 c this subroutine extracts the upper triangular part of a matrix
1117 c and writes the result ao, jao, iao. The routine is in place in
1118 c that ao, jao, iao can be the same as a, ja, ia if desired.
1119 c-----------
1120 c on input:
1121 c
1122 c n = dimension of the matrix a.
1123 c a, ja,
1124 c ia = matrix stored in a, ja, ia, format
1125 c On return:
1126 c ao, jao,
1127 c iao = upper triangular matrix (upper part of a)
1128 c stored in compressed sparse row format
1129 c note: the diagonal element is the last element in each row.
1130 c i.e. in a(ia(i+1)-1 )
1131 c ao, jao, iao may be the same as a, ja, ia on entry -- in which case
1132 c getu will overwrite the result on a, ja, ia.
1133 c
1134 c------------------------------------------------------------------------
1135 c local variables
1136 real*8 t
1137 integer ko, k, i, kdiag, kfirst
1138 ko = 0
1139 do 7 i=1, n
1140 kfirst = ko+1
1141 kdiag = 0
1142 do 71 k = ia(i), ia(i+1) -1
1143 if (ja(k) .lt. i) goto 71
1144 ko = ko+1
1145 ao(ko) = a(k)
1146 jao(ko) = ja(k)
1147 if (ja(k) .eq. i) kdiag = ko
1148 71 continue
1149 if (kdiag .eq. 0 .or. kdiag .eq. kfirst) goto 72
1150 c exchange
1151 t = ao(kdiag)
1152 ao(kdiag) = ao(kfirst)
1153 ao(kfirst) = t
1154 c
1155 k = jao(kdiag)
1156 jao(kdiag) = jao(kfirst)
1157 jao(kfirst) = k
1158 72 iao(i) = kfirst
1159 7 continue
1160 c redefine iao(n+1)
1161 iao(n+1) = ko+1
1162 return
1163 c----------end-of-getu -------------------------------------------------
1164 c-----------------------------------------------------------------------
1165 end
1166 c-----------------------------------------------------------------------
1167 subroutine levels (n, jal, ial, nlev, lev, ilev, levnum)
1168 integer jal(*),ial(*), levnum(*), ilev(*), lev(*)
1169 c-----------------------------------------------------------------------
1170 c levels gets the level structure of a lower triangular matrix
1171 c for level scheduling in the parallel solution of triangular systems
1172 c strict lower matrices (e.g. unit) as well matrices with their main
1173 c diagonal are accepted.
1174 c-----------------------------------------------------------------------
1175 c on entry:
1176 c----------
1177 c n = integer. The row dimension of the matrix
1178 c jal, ial =
1179 c
1180 c on return:
1181 c-----------
1182 c nlev = integer. number of levels found
1183 c lev = integer array of length n containing the level
1184 c scheduling permutation.
1185 c ilev = integer array. pointer to beginning of levels in lev.
1186 c the numbers lev(i) to lev(i+1)-1 contain the row numbers
1187 c that belong to level number i, in the level scheduling
1188 c ordering. The equations of the same level can be solved
1189 c in parallel, once those of all the previous levels have
1190 c been solved.
1191 c work arrays:
1192 c-------------
1193 c levnum = integer array of length n (containing the level numbers
1194 c of each unknown on return)
1195 c-----------------------------------------------------------------------
1196 do 10 i = 1, n
1197 levnum(i) = 0
1198 10 continue
1199 c
1200 c compute level of each node --
1201 c
1202 nlev = 0
1203 do 20 i = 1, n
1204 levi = 0
1205 do 15 j = ial(i), ial(i+1) - 1
1206 levi = max (levi, levnum(jal(j)))
1207 15 continue
1208 levi = levi+1
1209 levnum(i) = levi
1210 nlev = max(nlev,levi)
1211 20 continue
1212 c-------------set data structure --------------------------------------
1213 do 21 j=1, nlev+1
1214 ilev(j) = 0
1215 21 continue
1216 c------count number of elements in each level -----------------------
1217 do 22 j=1, n
1218 i = levnum(j)+1
1219 ilev(i) = ilev(i)+1
1220 22 continue
1221 c---- set up pointer for each level ----------------------------------
1222 ilev(1) = 1
1223 do 23 j=1, nlev
1224 ilev(j+1) = ilev(j)+ilev(j+1)
1225 23 continue
1226 c-----determine elements of each level --------------------------------
1227 do 30 j=1,n
1228 i = levnum(j)
1229 lev(ilev(i)) = j
1230 ilev(i) = ilev(i)+1
1231 30 continue
1232 c reset pointers backwards
1233 do 35 j=nlev, 1, -1
1234 ilev(j+1) = ilev(j)
1235 35 continue
1236 ilev(1) = 1
1237 return
1238 c----------end-of-levels------------------------------------------------
1239 C-----------------------------------------------------------------------
1240 end
1241 c-----------------------------------------------------------------------
1242 subroutine amask (nrow,ncol,a,ja,ia,jmask,imask,
1243 * c,jc,ic,iw,nzmax,ierr)
1244 c---------------------------------------------------------------------
1245 real*8 a(*),c(*)
1246 integer ia(nrow+1),ja(*),jc(*),ic(nrow+1),jmask(*),imask(nrow+1)
1247 logical iw(ncol)
1248 c-----------------------------------------------------------------------
1249 c This subroutine builds a sparse matrix from an input matrix by
1250 c extracting only elements in positions defined by the mask jmask, imask
1251 c-----------------------------------------------------------------------
1252 c On entry:
1253 c---------
1254 c nrow = integer. row dimension of input matrix
1255 c ncol = integer. Column dimension of input matrix.
1256 c
1257 c a,
1258 c ja,
1259 c ia = matrix in Compressed Sparse Row format
1260 c
1261 c jmask,
1262 c imask = matrix defining mask (pattern only) stored in compressed
1263 c sparse row format.
1264 c
1265 c nzmax = length of arrays c and jc. see ierr.
1266 c
1267 c On return:
1268 c-----------
1269 c
1270 c a, ja, ia and jmask, imask are unchanged.
1271 c
1272 c c
1273 c jc,
1274 c ic = the output matrix in Compressed Sparse Row format.
1275 c
1276 c ierr = integer. serving as error message.c
1277 c ierr = 1 means normal return
1278 c ierr .gt. 1 means that amask stopped when processing
1279 c row number ierr, because there was not enough space in
1280 c c, jc according to the value of nzmax.
1281 c
1282 c work arrays:
1283 c-------------
1284 c iw = logical work array of length ncol.
1285 c
1286 c note:
1287 c------ the algorithm is in place: c, jc, ic can be the same as
1288 c a, ja, ia in which cas the code will overwrite the matrix c
1289 c on a, ja, ia
1290 c
1291 c-----------------------------------------------------------------------
1292 ierr = 0
1293 len = 0
1294 do 1 j=1, ncol
1295 iw(j) = .false.
1296 1 continue
1297 c unpack the mask for row ii in iw
1298 do 100 ii=1, nrow
1299 c save pointer in order to be able to do things in place
1300 do 2 k=imask(ii), imask(ii+1)-1
1301 iw(jmask(k)) = .true.
1302 2 continue
1303 c add umasked elemnts of row ii
1304 k1 = ia(ii)
1305 k2 = ia(ii+1)-1
1306 ic(ii) = len+1
1307 do 200 k=k1,k2
1308 j = ja(k)
1309 if (iw(j)) then
1310 len = len+1
1311 if (len .gt. nzmax) then
1312 ierr = ii
1313 return
1314 endif
1315 jc(len) = j
1316 c(len) = a(k)
1317 endif
1318 200 continue
1319 c
1320 do 3 k=imask(ii), imask(ii+1)-1
1321 iw(jmask(k)) = .false.
1322 3 continue
1323 100 continue
1324 ic(nrow+1)=len+1
1325 c
1326 return
1327 c-----end-of-amask -----------------------------------------------------
1328 c-----------------------------------------------------------------------
1329 end
1330 c-----------------------------------------------------------------------
1331 subroutine rperm (nrow,a,ja,ia,ao,jao,iao,perm,job)
1332 integer nrow,ja(*),ia(nrow+1),jao(*),iao(nrow+1),perm(nrow),job
1333 real*8 a(*),ao(*)
1334 c-----------------------------------------------------------------------
1335 c this subroutine permutes the rows of a matrix in CSR format.
1336 c rperm computes B = P A where P is a permutation matrix.
1337 c the permutation P is defined through the array perm: for each j,
1338 c perm(j) represents the destination row number of row number j.
1339 c Youcef Saad -- recoded Jan 28, 1991.
1340 c-----------------------------------------------------------------------
1341 c on entry:
1342 c----------
1343 c n = dimension of the matrix
1344 c a, ja, ia = input matrix in csr format
1345 c perm = integer array of length nrow containing the permutation arrays
1346 c for the rows: perm(i) is the destination of row i in the
1347 c permuted matrix.
1348 c ---> a(i,j) in the original matrix becomes a(perm(i),j)
1349 c in the output matrix.
1350 c
1351 c job = integer indicating the work to be done:
1352 c job = 1 permute a, ja, ia into ao, jao, iao
1353 c (including the copying of real values ao and
1354 c the array iao).
1355 c job .ne. 1 : ignore real values.
1356 c (in which case arrays a and ao are not needed nor
1357 c used).
1358 c
1359 c------------
1360 c on return:
1361 c------------
1362 c ao, jao, iao = input matrix in a, ja, ia format
1363 c note :
1364 c if (job.ne.1) then the arrays a and ao are not used.
1365 c----------------------------------------------------------------------c
1366 c Y. Saad, May 2, 1990 c
1367 c----------------------------------------------------------------------c
1368 logical values
1369 values = (job .eq. 1)
1370 c
1371 c determine pointers for output matix.
1372 c
1373 do 50 j=1,nrow
1374 i = perm(j)
1375 iao(i+1) = ia(j+1) - ia(j)
1376 50 continue
1377 c
1378 c get pointers from lengths
1379 c
1380 iao(1) = 1
1381 do 51 j=1,nrow
1382 iao(j+1)=iao(j+1)+iao(j)
1383 51 continue
1384 c
1385 c copying
1386 c
1387 do 100 ii=1,nrow
1388 c
1389 c old row = ii -- new row = iperm(ii) -- ko = new pointer
1390 c
1391 ko = iao(perm(ii))
1392 do 60 k=ia(ii), ia(ii+1)-1
1393 jao(ko) = ja(k)
1394 if (values) ao(ko) = a(k)
1395 ko = ko+1
1396 60 continue
1397 100 continue
1398 c
1399 return
1400 c---------end-of-rperm -------------------------------------------------
1401 c-----------------------------------------------------------------------
1402 end
1403 c-----------------------------------------------------------------------
1404 subroutine cperm (nrow,a,ja,ia,ao,jao,iao,perm,job)
1405 integer nrow,ja(*),ia(nrow+1),jao(*),iao(nrow+1),perm(*), job
1406 real*8 a(*), ao(*)
1407 c-----------------------------------------------------------------------
1408 c this subroutine permutes the columns of a matrix a, ja, ia.
1409 c the result is written in the output matrix ao, jao, iao.
1410 c cperm computes B = A P, where P is a permutation matrix
1411 c that maps column j into column perm(j), i.e., on return
1412 c a(i,j) becomes a(i,perm(j)) in new matrix
1413 c Y. Saad, May 2, 1990 / modified Jan. 28, 1991.
1414 c-----------------------------------------------------------------------
1415 c on entry:
1416 c----------
1417 c nrow = row dimension of the matrix
1418 c
1419 c a, ja, ia = input matrix in csr format.
1420 c
1421 c perm = integer array of length ncol (number of columns of A
1422 c containing the permutation array the columns:
1423 c a(i,j) in the original matrix becomes a(i,perm(j))
1424 c in the output matrix.
1425 c
1426 c job = integer indicating the work to be done:
1427 c job = 1 permute a, ja, ia into ao, jao, iao
1428 c (including the copying of real values ao and
1429 c the array iao).
1430 c job .ne. 1 : ignore real values ao and ignore iao.
1431 c
1432 c------------
1433 c on return:
1434 c------------
1435 c ao, jao, iao = input matrix in a, ja, ia format (array ao not needed)
1436 c
1437 c Notes:
1438 c-------
1439 c 1. if job=1 then ao, iao are not used.
1440 c 2. This routine is in place: ja, jao can be the same.
1441 c 3. If the matrix is initially sorted (by increasing column number)
1442 c then ao,jao,iao may not be on return.
1443 c
1444 c----------------------------------------------------------------------c
1445 c local parameters:
1446 integer k, i, nnz
1447 c
1448 nnz = ia(nrow+1)-1
1449 do 100 k=1,nnz
1450 jao(k) = perm(ja(k))
1451 100 continue
1452 c
1453 c done with ja array. return if no need to touch values.
1454 c
1455 if (job .ne. 1) return
1456 c
1457 c else get new pointers -- and copy values too.
1458 c
1459 do 1 i=1, nrow+1
1460 iao(i) = ia(i)
1461 1 continue
1462 c
1463 do 2 k=1, nnz
1464 ao(k) = a(k)
1465 2 continue
1466 c
1467 return
1468 c---------end-of-cperm--------------------------------------------------
1469 c-----------------------------------------------------------------------
1470 end
1471 c-----------------------------------------------------------------------
1472 subroutine dperm (nrow,a,ja,ia,ao,jao,iao,perm,qperm,job)
1473 integer nrow,ja(*),ia(nrow+1),jao(*),iao(nrow+1),perm(nrow),
1474 + qperm(*),job
1475 real*8 a(*),ao(*)
1476 c-----------------------------------------------------------------------
1477 c This routine permutes the rows and columns of a matrix stored in CSR
1478 c format. i.e., it computes P A Q, where P, Q are permutation matrices.
1479 c P maps row i into row perm(i) and Q maps column j into column qperm(j):
1480 c a(i,j) becomes a(perm(i),qperm(j)) in new matrix
1481 c In the particular case where Q is the transpose of P (symmetric
1482 c permutation of A) then qperm is not needed.
1483 c note that qperm should be of length ncol (number of columns) but this
1484 c is not checked.
1485 c-----------------------------------------------------------------------
1486 c Y. Saad, Sep. 21 1989 / recoded Jan. 28 1991.
1487 c-----------------------------------------------------------------------
1488 c on entry:
1489 c----------
1490 c n = dimension of the matrix
1491 c a, ja,
1492 c ia = input matrix in a, ja, ia format
1493 c perm = integer array of length n containing the permutation arrays
1494 c for the rows: perm(i) is the destination of row i in the
1495 c permuted matrix -- also the destination of column i in case
1496 c permutation is symmetric (job .le. 2)
1497 c
1498 c qperm = same thing for the columns. This should be provided only
1499 c if job=3 or job=4, i.e., only in the case of a nonsymmetric
1500 c permutation of rows and columns. Otherwise qperm is a dummy
1501 c
1502 c job = integer indicating the work to be done:
1503 c * job = 1,2 permutation is symmetric Ao :== P * A * transp(P)
1504 c job = 1 permute a, ja, ia into ao, jao, iao
1505 c job = 2 permute matrix ignoring real values.
1506 c * job = 3,4 permutation is non-symmetric Ao :== P * A * Q
1507 c job = 3 permute a, ja, ia into ao, jao, iao
1508 c job = 4 permute matrix ignoring real values.
1509 c
1510 c on return:
1511 c-----------
1512 c ao, jao, iao = input matrix in a, ja, ia format
1513 c
1514 c in case job .eq. 2 or job .eq. 4, a and ao are never referred to
1515 c and can be dummy arguments.
1516 c Notes:
1517 c-------
1518 c 1) algorithm is in place
1519 c 2) column indices may not be sorted on return even though they may be
1520 c on entry.
1521 c----------------------------------------------------------------------c
1522 c local variables
1523 integer locjob, mod
1524 c
1525 c locjob indicates whether or not real values must be copied.
1526 c
1527 locjob = mod(job,2)
1528 c
1529 c permute rows first
1530 c
1531 call rperm (nrow,a,ja,ia,ao,jao,iao,perm,locjob)
1532 c
1533 c then permute columns
1534 c
1535 locjob = 0
1536 c
1537 if (job .le. 2) then
1538 call cperm (nrow,ao,jao,iao,ao,jao,iao,perm,locjob)
1539 else
1540 call cperm (nrow,ao,jao,iao,ao,jao,iao,qperm,locjob)
1541 endif
1542 c
1543 return
1544 c-------end-of-dperm----------------------------------------------------
1545 c-----------------------------------------------------------------------
1546 end
1547 c-----------------------------------------------------------------------
1548 subroutine dperm1 (i1,i2,a,ja,ia,b,jb,ib,perm,ipos,job)
1549 integer i1,i2,job,ja(*),ia(*),jb(*),ib(*),perm(*)
1550 real*8 a(*),b(*)
1551 c-----------------------------------------------------------------------
1552 c general submatrix extraction routine.
1553 c-----------------------------------------------------------------------
1554 c extracts rows perm(i1), perm(i1+1), ..., perm(i2) (in this order)
1555 c from a matrix (doing nothing in the column indices.) The resulting
1556 c submatrix is constructed in b, jb, ib. A pointer ipos to the
1557 c beginning of arrays b,jb,is also allowed (i.e., nonzero elements
1558 c are accumulated starting in position ipos of b, jb).
1559 c-----------------------------------------------------------------------
1560 c Y. Saad,Sep. 21 1989 / recoded Jan. 28 1991 / modified for PSPARSLIB
1561 c Sept. 1997..
1562 c-----------------------------------------------------------------------
1563 c on entry:
1564 c----------
1565 c n = dimension of the matrix
1566 c a,ja,
1567 c ia = input matrix in CSR format
1568 c perm = integer array of length n containing the indices of the rows
1569 c to be extracted.
1570 c
1571 c job = job indicator. if (job .ne.1) values are not copied (i.e.,
1572 c only pattern is copied).
1573 c
1574 c on return:
1575 c-----------
1576 c b,ja,
1577 c ib = matrix in csr format. b(ipos:ipos+nnz-1),jb(ipos:ipos+nnz-1)
1578 c contain the value and column indices respectively of the nnz
1579 c nonzero elements of the permuted matrix. thus ib(1)=ipos.
1580 c
1581 c Notes:
1582 c-------
1583 c algorithm is NOT in place
1584 c-----------------------------------------------------------------------
1585 c local variables
1586 c
1587 integer ko,irow,k
1588 logical values
1589 c-----------------------------------------------------------------------
1590 values = (job .eq. 1)
1591 ko = ipos
1592 ib(1) = ko
1593 do 900 i=i1,i2
1594 irow = perm(i)
1595 do 800 k=ia(irow),ia(irow+1)-1
1596 if (values) b(ko) = a(k)
1597 jb(ko) = ja(k)
1598 ko=ko+1
1599 800 continue
1600 ib(i-i1+2) = ko
1601 900 continue
1602 return
1603 c--------end-of-dperm1--------------------------------------------------
1604 c-----------------------------------------------------------------------
1605 end
1606 c-----------------------------------------------------------------------
1607 subroutine dperm2 (i1,i2,a,ja,ia,b,jb,ib,cperm,rperm,istart,
1608 * ipos,job)
1609 integer i1,i2,job,istart,ja(*),ia(*),jb(*),ib(*),cperm(*),rperm(*)
1610 real*8 a(*),b(*)
1611 c-----------------------------------------------------------------------
1612 c general submatrix permutation/ extraction routine.
1613 c-----------------------------------------------------------------------
1614 c extracts rows rperm(i1), rperm(i1+1), ..., rperm(i2) and does an
1615 c associated column permutation (using array cperm). The resulting
1616 c submatrix is constructed in b, jb, ib. For added flexibility, the
1617 c extracted elements are put in sequence starting from row 'istart'
1618 c of B. In addition a pointer ipos to the beginning of arrays b,jb,
1619 c is also allowed (i.e., nonzero elements are accumulated starting in
1620 c position ipos of b, jb). In most applications istart and ipos are
1621 c equal to one. However, the generality adds substantial flexiblity.
1622 c EXPLE: (1) to permute msr to msr (excluding diagonals)
1623 c call dperm2 (1,n,a,ja,ja,b,jb,jb,rperm,rperm,1,n+2)
1624 c (2) To extract rows 1 to 10: define rperm and cperm to be
1625 c identity permutations (rperm(i)=i, i=1,n) and then
1626 c call dperm2 (1,10,a,ja,ia,b,jb,ib,rperm,rperm,1,1)
1627 c (3) to achieve a symmetric permutation as defined by perm:
1628 c call dperm2 (1,10,a,ja,ia,b,jb,ib,perm,perm,1,1)
1629 c (4) to get a symmetric permutation of A and append the
1630 c resulting data structure to A's data structure (useful!)
1631 c call dperm2 (1,10,a,ja,ia,a,ja,ia(n+1),perm,perm,1,ia(n+1))
1632 c-----------------------------------------------------------------------
1633 c Y. Saad,Sep. 21 1989 / recoded Jan. 28 1991.
1634 c-----------------------------------------------------------------------
1635 c on entry:
1636 c----------
1637 c n = dimension of the matrix
1638 c i1,i2 = extract rows rperm(i1) to rperm(i2) of A, with i1<i2.
1639 c
1640 c a,ja,
1641 c ia = input matrix in CSR format
1642 c cperm = integer array of length n containing the permutation arrays
1643 c for the columns: cperm(i) is the destination of column j,
1644 c i.e., any column index ja(k) is transformed into cperm(ja(k))
1645 c
1646 c rperm = permutation array for the rows. rperm(i) = origin (in A) of
1647 c row i in B. This is the reverse permutation relative to the
1648 c ones used in routines cperm, dperm,....
1649 c rows rperm(i1), rperm(i1)+1, ... rperm(i2) are
1650 c extracted from A and stacked into B, starting in row istart
1651 c of B.
1652 c istart= starting row for B where extracted matrix is to be added.
1653 c this is also only a pointer of the be beginning address for
1654 c ib , on return.
1655 c ipos = beginning position in arrays b and jb where to start copying
1656 c elements. Thus, ib(istart) = ipos.
1657 c
1658 c job = job indicator. if (job .ne.1) values are not copied (i.e.,
1659 c only pattern is copied).
1660 c
1661 c on return:
1662 c-----------
1663 c b,ja,
1664 c ib = matrix in csr format. positions 1,2,...,istart-1 of ib
1665 c are not touched. b(ipos:ipos+nnz-1),jb(ipos:ipos+nnz-1)
1666 c contain the value and column indices respectively of the nnz
1667 c nonzero elements of the permuted matrix. thus ib(istart)=ipos.
1668 c
1669 c Notes:
1670 c-------
1671 c 1) algorithm is NOT in place
1672 c 2) column indices may not be sorted on return even though they
1673 c may be on entry.
1674 c-----------------------------------------------------------------------
1675 c local variables
1676 c
1677 integer ko,irow,k
1678 logical values
1679 c-----------------------------------------------------------------------
1680 values = (job .eq. 1)
1681 ko = ipos
1682 ib(istart) = ko
1683 do 900 i=i1,i2
1684 irow = rperm(i)
1685 do 800 k=ia(irow),ia(irow+1)-1
1686 if (values) b(ko) = a(k)
1687 jb(ko) = cperm(ja(k))
1688 ko=ko+1
1689 800 continue
1690 ib(istart+i-i1+1) = ko
1691 900 continue
1692 return
1693 c--------end-of-dperm2--------------------------------------------------
1694 c-----------------------------------------------------------------------
1695 end
1696 c-----------------------------------------------------------------------
1697 subroutine dmperm (nrow,a,ja,ao,jao,perm,job)
1698 integer nrow,ja(*),jao(*),perm(nrow),job
1699 real*8 a(*),ao(*)
1700 c-----------------------------------------------------------------------
1701 c This routine performs a symmetric permutation of the rows and
1702 c columns of a matrix stored in MSR format. i.e., it computes
1703 c B = P A transp(P), where P, is a permutation matrix.
1704 c P maps row i into row perm(i) and column j into column perm(j):
1705 c a(i,j) becomes a(perm(i),perm(j)) in new matrix
1706 c (i.e. ao(perm(i),perm(j)) = a(i,j) )
1707 c calls dperm.
1708 c-----------------------------------------------------------------------
1709 c Y. Saad, Nov 15, 1991.
1710 c-----------------------------------------------------------------------
1711 c on entry:
1712 c----------
1713 c n = dimension of the matrix
1714 c a, ja = input matrix in MSR format.
1715 c perm = integer array of length n containing the permutation arrays
1716 c for the rows: perm(i) is the destination of row i in the
1717 c permuted matrix -- also the destination of column i in case
1718 c permutation is symmetric (job .le. 2)
1719 c
1720 c job = integer indicating the work to be done:
1721 c job = 1 permute a, ja, ia into ao, jao, iao
1722 c job = 2 permute matrix ignoring real values.
1723 c
1724 c on return:
1725 c-----------
1726 c ao, jao = output matrix in MSR.
1727 c
1728 c in case job .eq. 2 a and ao are never referred to and can be dummy
1729 c arguments.
1730 c
1731 c Notes:
1732 c-------
1733 c 1) algorithm is NOT in place
1734 c 2) column indices may not be sorted on return even though they may be
1735 c on entry.
1736 c----------------------------------------------------------------------c
1737 c local variables
1738 c
1739 integer n1, n2
1740 n1 = nrow+1
1741 n2 = n1+1
1742 c
1743 call dperm (nrow,a,ja,ja,ao(n2),jao(n2),jao,perm,perm,job)
1744 c
1745 jao(1) = n2
1746 do 101 j=1, nrow
1747 ao(perm(j)) = a(j)
1748 jao(j+1) = jao(j+1)+n1
1749 101 continue
1750 c
1751 c done
1752 c
1753 return
1754 c-----------------------------------------------------------------------
1755 c--------end-of-dmperm--------------------------------------------------
1756 end
1757 c-----------------------------------------------------------------------
1758 subroutine dvperm (n, x, perm)
1759 integer n, perm(n)
1760 real*8 x(n)
1761 c-----------------------------------------------------------------------
1762 c this subroutine performs an in-place permutation of a real vector x
1763 c according to the permutation array perm(*), i.e., on return,
1764 c the vector x satisfies,
1765 c
1766 c x(perm(j)) :== x(j), j=1,2,.., n
1767 c
1768 c-----------------------------------------------------------------------
1769 c on entry:
1770 c---------
1771 c n = length of vector x.
1772 c perm = integer array of length n containing the permutation array.
1773 c x = input vector
1774 c
1775 c on return:
1776 c----------
1777 c x = vector x permuted according to x(perm(*)) := x(*)
1778 c
1779 c----------------------------------------------------------------------c
1780 c Y. Saad, Sep. 21 1989 c
1781 c----------------------------------------------------------------------c
1782 c local variables
1783 real*8 tmp, tmp1
1784 c
1785 init = 1
1786 tmp = x(init)
1787 ii = perm(init)
1788 perm(init)= -perm(init)
1789 k = 0
1790 c
1791 c loop
1792 c
1793 6 k = k+1
1794 c
1795 c save the chased element --
1796 c
1797 tmp1 = x(ii)
1798 x(ii) = tmp
1799 next = perm(ii)
1800 if (next .lt. 0 ) goto 65
1801 c
1802 c test for end
1803 c
1804 if (k .gt. n) goto 101
1805 tmp = tmp1
1806 perm(ii) = - perm(ii)
1807 ii = next
1808 c
1809 c end loop
1810 c
1811 goto 6
1812 c
1813 c reinitilaize cycle --
1814 c
1815 65 init = init+1
1816 if (init .gt. n) goto 101
1817 if (perm(init) .lt. 0) goto 65
1818 tmp = x(init)
1819 ii = perm(init)
1820 perm(init)=-perm(init)
1821 goto 6
1822 c
1823 101 continue
1824 do 200 j=1, n
1825 perm(j) = -perm(j)
1826 200 continue
1827 c
1828 return
1829 c-------------------end-of-dvperm---------------------------------------
1830 c-----------------------------------------------------------------------
1831 end
1832 c-----------------------------------------------------------------------
1833 subroutine ivperm (n, ix, perm)
1834 integer n, perm(n), ix(n)
1835 c-----------------------------------------------------------------------
1836 c this subroutine performs an in-place permutation of an integer vector
1837 c ix according to the permutation array perm(*), i.e., on return,
1838 c the vector x satisfies,
1839 c
1840 c ix(perm(j)) :== ix(j), j=1,2,.., n
1841 c
1842 c-----------------------------------------------------------------------
1843 c on entry:
1844 c---------
1845 c n = length of vector x.
1846 c perm = integer array of length n containing the permutation array.
1847 c ix = input vector
1848 c
1849 c on return:
1850 c----------
1851 c ix = vector x permuted according to ix(perm(*)) := ix(*)
1852 c
1853 c----------------------------------------------------------------------c
1854 c Y. Saad, Sep. 21 1989 c
1855 c----------------------------------------------------------------------c
1856 c local variables
1857 integer tmp, tmp1
1858 c
1859 init = 1
1860 tmp = ix(init)
1861 ii = perm(init)
1862 perm(init)= -perm(init)
1863 k = 0
1864 c
1865 c loop
1866 c
1867 6 k = k+1
1868 c
1869 c save the chased element --
1870 c
1871 tmp1 = ix(ii)
1872 ix(ii) = tmp
1873 next = perm(ii)
1874 if (next .lt. 0 ) goto 65
1875 c
1876 c test for end
1877 c
1878 if (k .gt. n) goto 101
1879 tmp = tmp1
1880 perm(ii) = - perm(ii)
1881 ii = next
1882 c
1883 c end loop
1884 c
1885 goto 6
1886 c
1887 c reinitilaize cycle --
1888 c
1889 65 init = init+1
1890 if (init .gt. n) goto 101
1891 if (perm(init) .lt. 0) goto 65
1892 tmp = ix(init)
1893 ii = perm(init)
1894 perm(init)=-perm(init)
1895 goto 6
1896 c
1897 101 continue
1898 do 200 j=1, n
1899 perm(j) = -perm(j)
1900 200 continue
1901 c
1902 return
1903 c-------------------end-of-ivperm---------------------------------------
1904 c-----------------------------------------------------------------------
1905 end
1906 c-----------------------------------------------------------------------
1907 subroutine retmx (n,a,ja,ia,dd)
1908 real*8 a(*),dd(*)
1909 integer n,ia(*),ja(*)
1910 c-----------------------------------------------------------------------
1911 c returns in dd(*) the max absolute value of elements in row *.
1912 c used for scaling purposes. superseded by rnrms .
1913 c
1914 c on entry:
1915 c n = dimension of A
1916 c a,ja,ia
1917 c = matrix stored in compressed sparse row format
1918 c dd = real*8 array of length n. On output,entry dd(i) contains
1919 c the element of row i that has the largest absolute value.
1920 c Moreover the sign of dd is modified such that it is the
1921 c same as that of the diagonal element in row i.
1922 c----------------------------------------------------------------------c
1923 c Y. Saad, Sep. 21 1989 c
1924 c----------------------------------------------------------------------c
1925 c local variables
1926 integer k2, i, k1, k
1927 real*8 t, t1, t2
1928 c
1929 c initialize
1930 c
1931 k2 = 1
1932 do 11 i=1,n
1933 k1 = k2
1934 k2 = ia(i+1) - 1
1935 t = 0.0d0
1936 do 101 k=k1,k2
1937 t1 = abs(a(k))
1938 if (t1 .gt. t) t = t1
1939 if (ja(k) .eq. i) then
1940 if (a(k) .ge. 0.0) then
1941 t2 = a(k)
1942 else
1943 t2 = - a(k)
1944 endif
1945 endif
1946 101 continue
1947 dd(i) = t2*t
1948 c we do not invert diag
1949 11 continue
1950 return
1951 c---------end of retmx -------------------------------------------------
1952 c-----------------------------------------------------------------------
1953 end
1954 c-----------------------------------------------------------------------
1955 subroutine diapos (n,ja,ia,idiag)
1956 integer ia(n+1), ja(*), idiag(n)
1957 c-----------------------------------------------------------------------
1958 c this subroutine returns the positions of the diagonal elements of a
1959 c sparse matrix a, ja, ia, in the array idiag.
1960 c-----------------------------------------------------------------------
1961 c on entry:
1962 c----------
1963 c
1964 c n = integer. row dimension of the matrix a.
1965 c a,ja,
1966 c ia = matrix stored compressed sparse row format. a array skipped.
1967 c
1968 c on return:
1969 c-----------
1970 c idiag = integer array of length n. The i-th entry of idiag
1971 c points to the diagonal element a(i,i) in the arrays
1972 c a, ja. (i.e., a(idiag(i)) = element A(i,i) of matrix A)
1973 c if no diagonal element is found the entry is set to 0.
1974 c----------------------------------------------------------------------c
1975 c Y. Saad, March, 1990
1976 c----------------------------------------------------------------------c
1977 do 1 i=1, n
1978 idiag(i) = 0
1979 1 continue
1980 c
1981 c sweep through data structure.
1982 c
1983 do 6 i=1,n
1984 do 51 k= ia(i),ia(i+1) -1
1985 if (ja(k) .eq. i) idiag(i) = k
1986 51 continue
1987 6 continue
1988 c----------- -end-of-diapos---------------------------------------------
1989 c-----------------------------------------------------------------------
1990 return
1991 end
1992 c-----------------------------------------------------------------------
1993 subroutine dscaldg (n,a,ja,ia,diag,job)
1994 real*8 a(*), diag(*),t
1995 integer ia(*),ja(*)
1996 c-----------------------------------------------------------------------
1997 c scales rows by diag where diag is either given (job=0)
1998 c or to be computed:
1999 c job = 1 ,scale row i by by +/- max |a(i,j) | and put inverse of
2000 c scaling factor in diag(i),where +/- is the sign of a(i,i).
2001 c job = 2 scale by 2-norm of each row..
2002 c if diag(i) = 0,then diag(i) is replaced by one
2003 c (no scaling)..
2004 c----------------------------------------------------------------------c
2005 c Y. Saad, Sep. 21 1989 c
2006 c----------------------------------------------------------------------c
2007 goto (12,11,10) job+1
2008 10 do 110 j=1,n
2009 k1= ia(j)
2010 k2 = ia(j+1)-1
2011 t = 0.0d0
2012 do 111 k = k1,k2
2013 111 t = t+a(k)*a(k)
2014 110 diag(j) = sqrt(t)
2015 goto 12
2016 11 continue
2017 call retmx (n,a,ja,ia,diag)
2018 c------
2019 12 do 1 j=1,n
2020 if (diag(j) .ne. 0.0d0) then
2021 diag(j) = 1.0d0/diag(j)
2022 else
2023 diag(j) = 1.0d0
2024 endif
2025 1 continue
2026 do 2 i=1,n
2027 t = diag(i)
2028 do 21 k=ia(i),ia(i+1) -1
2029 a(k) = a(k)*t
2030 21 continue
2031 2 continue
2032 return
2033 c--------end of dscaldg -----------------------------------------------
2034 c-----------------------------------------------------------------------
2035 end
2036 c-----------------------------------------------------------------------
2037 subroutine extbdg (n,a,ja,ia,bdiag,nblk,ao,jao,iao)
2038 implicit real*8 (a-h,o-z)
2039 real*8 bdiag(*),a(*),ao(*)
2040 integer ia(*),ja(*),jao(*),iao(*)
2041 c-----------------------------------------------------------------------
2042 c this subroutine extracts the main diagonal blocks of a
2043 c matrix stored in compressed sparse row format and puts the result
2044 c into the array bdiag and the remainder in ao,jao,iao.
2045 c-----------------------------------------------------------------------
2046 c on entry:
2047 c----------
2048 c n = integer. The row dimension of the matrix a.
2049 c a,
2050 c ja,
2051 c ia = matrix stored in csr format
2052 c nblk = dimension of each diagonal block. The diagonal blocks are
2053 c stored in compressed format rowwise,i.e.,we store in
2054 c succession the i nonzeros of the i-th row after those of
2055 c row number i-1..
2056 c
2057 c on return:
2058 c----------
2059 c bdiag = real*8 array of size (n x nblk) containing the diagonal
2060 c blocks of A on return
2061 c ao,
2062 c jao,
2063 C iao = remainder of the matrix stored in csr format.
2064 c----------------------------------------------------------------------c
2065 c Y. Saad, Sep. 21 1989 c
2066 c----------------------------------------------------------------------c
2067 m = 1 + (n-1)/nblk
2068 c this version is sequential -- there is a more parallel version
2069 c that goes through the structure twice ....
2070 ltr = ((nblk-1)*nblk)/2
2071 l = m * ltr
2072 do 1 i=1,l
2073 bdiag(i) = 0.0d0
2074 1 continue
2075 ko = 0
2076 kb = 1
2077 iao(1) = 1
2078 c-------------------------
2079 do 11 jj = 1,m
2080 j1 = (jj-1)*nblk+1
2081 j2 = min0 (n,j1+nblk-1)
2082 do 12 j=j1,j2
2083 do 13 i=ia(j),ia(j+1) -1
2084 k = ja(i)
2085 if (k .lt. j1) then
2086 ko = ko+1
2087 ao(ko) = a(i)
2088 jao(ko) = k
2089 else if (k .lt. j) then
2090 c kb = (jj-1)*ltr+((j-j1)*(j-j1-1))/2+k-j1+1
2091 c bdiag(kb) = a(i)
2092 bdiag(kb+k-j1) = a(i)
2093 endif
2094 13 continue
2095 kb = kb + j-j1
2096 iao(j+1) = ko+1
2097 12 continue
2098 11 continue
2099 return
2100 c---------end-of-extbdg-------------------------------------------------
2101 c-----------------------------------------------------------------------
2102 end
2103 c-----------------------------------------------------------------------
2104 subroutine getbwd(n,a,ja,ia,ml,mu)
2105 c-----------------------------------------------------------------------
2106 c gets the bandwidth of lower part and upper part of A.
2107 c does not assume that A is sorted.
2108 c-----------------------------------------------------------------------
2109 c on entry:
2110 c----------
2111 c n = integer = the row dimension of the matrix
2112 c a, ja,
2113 c ia = matrix in compressed sparse row format.
2114 c
2115 c on return:
2116 c-----------
2117 c ml = integer. The bandwidth of the strict lower part of A
2118 c mu = integer. The bandwidth of the strict upper part of A
2119 c
2120 c Notes:
2121 c ===== ml and mu are allowed to be negative or return. This may be
2122 c useful since it will tell us whether a band is confined
2123 c in the strict upper/lower triangular part.
2124 c indeed the definitions of ml and mu are
2125 c
2126 c ml = max ( (i-j) s.t. a(i,j) .ne. 0 )
2127 c mu = max ( (j-i) s.t. a(i,j) .ne. 0 )
2128 c----------------------------------------------------------------------c
2129 c Y. Saad, Sep. 21 1989 c
2130 c----------------------------------------------------------------------c
2131 real*8 a(*)
2132 integer ja(*),ia(n+1),ml,mu,ldist,i,k
2133 ml = - n
2134 mu = - n
2135 do 3 i=1,n
2136 do 31 k=ia(i),ia(i+1)-1
2137 ldist = i-ja(k)
2138 ml = max(ml,ldist)
2139 mu = max(mu,-ldist)
2140 31 continue
2141 3 continue
2142 return
2143 c---------------end-of-getbwd ------------------------------------------
2144 c-----------------------------------------------------------------------
2145 end
2146 c-----------------------------------------------------------------------
2147 subroutine blkfnd (nrow,ja,ia,nblk)
2148 c-----------------------------------------------------------------------
2149 c This routine attemptps to determine whether or not the input
2150 c matrix has a block structure and finds the blocks size
2151 c if it does. A block matrix is one which is
2152 c comprised of small square dense blocks. If there are zero
2153 c elements within the square blocks and the original data structure
2154 c takes these zeros into account then blkchk may fail to find the
2155 c correct block size.
2156 c-----------------------------------------------------------------------
2157 c on entry
2158 c---------
2159 c nrow = integer equal to the row dimension of the matrix.
2160 c ja = integer array containing the column indices of the entries
2161 c nonzero entries of the matrix stored by row.
2162 c ia = integer array of length nrow + 1 containing the pointers
2163 c beginning of each row in array ja.
2164 c
2165 c nblk = integer containing the assumed value of nblk if job = 0
2166 c
2167 c on return
2168 c----------
2169 c nblk = integer containing the value found for nblk when job = 1.
2170 c if imsg .ne. 0 this value is meaningless however.
2171 c
2172 c----------------------------------------------------------------------c
2173 c Y. Saad, Sep. 21 1989 c
2174 c----------------------------------------------------------------------c
2175 integer ia(nrow+1),ja(*)
2176 c-----------------------------------------------------------------------
2177 c first part of code will find candidate block sizes.
2178 c criterion used here is a simple one: scan rows and determine groups
2179 c of rows that have the same length and such that the first column
2180 c number and the last column number are identical.
2181 c-----------------------------------------------------------------------
2182 minlen = ia(2)-ia(1)
2183 irow = 1
2184 do 1 i=2,nrow
2185 len = ia(i+1)-ia(i)
2186 if (len .lt. minlen) then
2187 minlen = len
2188 irow = i
2189 endif
2190 1 continue
2191 c
2192 c ---- candidates are all dividers of minlen
2193 c
2194 nblk = 1
2195 if (minlen .le. 1) return
2196 c
2197 do 99 iblk = minlen, 1, -1
2198 if (mod(minlen,iblk) .ne. 0) goto 99
2199 len = ia(2) - ia(1)
2200 len0 = len
2201 jfirst = ja(1)
2202 jlast = ja(ia(2)-1)
2203 do 10 jrow = irow+1,irow+nblk-1
2204 i1 = ia(jrow)
2205 i2 = ia(jrow+1)-1
2206 len = i2+1-i1
2207 jf = ja(i1)
2208 jl = ja(i2)
2209 if (len .ne. len0 .or. jf .ne. jfirst .or.
2210 * jl .ne. jlast) goto 99
2211 10 continue
2212 c
2213 c check for this candidate ----
2214 c
2215 call blkchk (nrow,ja,ia,iblk,imsg)
2216 if (imsg .eq. 0) then
2217 c
2218 c block size found
2219 c
2220 nblk = iblk
2221 return
2222 endif
2223 99 continue
2224 c--------end-of-blkfnd -------------------------------------------------
2225 c-----------------------------------------------------------------------
2226 end
2227 c-----------------------------------------------------------------------
2228 subroutine blkchk (nrow,ja,ia,nblk,imsg)
2229 c-----------------------------------------------------------------------
2230 c This routine checks whether the input matrix is a block
2231 c matrix with block size of nblk. A block matrix is one which is
2232 c comprised of small square dense blocks. If there are zero
2233 c elements within the square blocks and the data structure
2234 c takes them into account then blkchk may fail to find the
2235 c correct block size.
2236 c-----------------------------------------------------------------------
2237 c on entry
2238 c---------
2239 c nrow = integer equal to the row dimension of the matrix.
2240 c ja = integer array containing the column indices of the entries
2241 c nonzero entries of the matrix stored by row.
2242 c ia = integer array of length nrow + 1 containing the pointers
2243 c beginning of each row in array ja.
2244 c
2245 c nblk = integer containing the value of nblk to be checked.
2246 c
2247 c on return
2248 c----------
2249 c
2250 c imsg = integer containing a message with the following meaning.
2251 c imsg = 0 means that the output value of nblk is a correct
2252 c block size. nblk .lt. 0 means nblk not correct
2253 c block size.
2254 c imsg = -1 : nblk does not divide nrow
2255 c imsg = -2 : a starting element in a row is at wrong position
2256 c (j .ne. mult*nblk +1 )
2257 c imsg = -3 : nblk does divide a row length -
2258 c imsg = -4 : an element is isolated outside a block or
2259 c two rows in same group have different lengths
2260 c----------------------------------------------------------------------c
2261 c Y. Saad, Sep. 21 1989 c
2262 c----------------------------------------------------------------------c
2263 integer ia(nrow+1),ja(*)
2264 c----------------------------------------------------------------------
2265 c first part of code will find candidate block sizes.
2266 c this is not guaranteed to work . so a check is done at the end
2267 c the criterion used here is a simple one:
2268 c scan rows and determine groups of rows that have the same length
2269 c and such that the first column number and the last column number
2270 c are identical.
2271 c----------------------------------------------------------------------
2272 imsg = 0
2273 if (nblk .le. 1) return
2274 nr = nrow/nblk
2275 if (nr*nblk .ne. nrow) goto 101
2276 c-- main loop ---------------------------------------------------------
2277 irow = 1
2278 do 20 ii=1, nr
2279 c i1= starting position for group of nblk rows in original matrix
2280 i1 = ia(irow)
2281 j2 = i1
2282 c lena = length of each row in that group in the original matrix
2283 lena = ia(irow+1)-i1
2284 c len = length of each block-row in that group in the output matrix
2285 len = lena/nblk
2286 if (len* nblk .ne. lena) goto 103
2287 c
2288 c for each row
2289 c
2290 do 6 i = 1, nblk
2291 irow = irow + 1
2292 if (ia(irow)-ia(irow-1) .ne. lena ) goto 104
2293 c
2294 c for each block
2295 c
2296 do 7 k=0, len-1
2297 jstart = ja(i1+nblk*k)-1
2298 if ( (jstart/nblk)*nblk .ne. jstart) goto 102
2299 c
2300 c for each column
2301 c
2302 do 5 j=1, nblk
2303 if (jstart+j .ne. ja(j2) ) goto 104
2304 j2 = j2+1
2305 5 continue
2306 7 continue
2307 6 continue
2308 20 continue
2309 c went through all loops successfully:
2310 return
2311 101 imsg = -1
2312 return
2313 102 imsg = -2
2314 return
2315 103 imsg = -3
2316 return
2317 104 imsg = -4
2318 c----------------end of chkblk -----------------------------------------
2319 c-----------------------------------------------------------------------
2320 return
2321 end
2322 c-----------------------------------------------------------------------
2323 subroutine infdia (n,ja,ia,ind,idiag)
2324 integer ia(*), ind(*), ja(*)
2325 c-----------------------------------------------------------------------
2326 c obtains information on the diagonals of A.
2327 c-----------------------------------------------------------------------
2328 c this subroutine finds the lengths of each of the 2*n-1 diagonals of A
2329 c it also outputs the number of nonzero diagonals found.
2330 c-----------------------------------------------------------------------
2331 c on entry:
2332 c----------
2333 c n = dimension of the matrix a.
2334 c
2335 c a, ..... not needed here.
2336 c ja,
2337 c ia = matrix stored in csr format
2338 c
2339 c on return:
2340 c-----------
2341 c
2342 c idiag = integer. number of nonzero diagonals found.
2343 c
2344 c ind = integer array of length at least 2*n-1. The k-th entry in
2345 c ind contains the number of nonzero elements in the diagonal
2346 c number k, the numbering beeing from the lowermost diagonal
2347 c (bottom-left). In other words ind(k) = length of diagonal
2348 c whose offset wrt the main diagonal is = - n + k.
2349 c----------------------------------------------------------------------c
2350 c Y. Saad, Sep. 21 1989 c
2351 c----------------------------------------------------------------------c
2352 n2= n+n-1
2353 do 1 i=1,n2
2354 ind(i) = 0
2355 1 continue
2356 do 3 i=1, n
2357 do 2 k=ia(i),ia(i+1)-1
2358 j = ja(k)
2359 ind(n+j-i) = ind(n+j-i) +1
2360 2 continue
2361 3 continue
2362 c count the nonzero ones.
2363 idiag = 0
2364 do 41 k=1, n2
2365 if (ind(k) .ne. 0) idiag = idiag+1
2366 41 continue
2367 return
2368 c done
2369 c------end-of-infdia ---------------------------------------------------
2370 c-----------------------------------------------------------------------
2371 end
2372 c-----------------------------------------------------------------------
2373 subroutine amubdg (nrow,ncol,ncolb,ja,ia,jb,ib,ndegr,nnz,iw)
2374 integer ja(*),jb(*),ia(nrow+1),ib(ncol+1),ndegr(nrow),iw(ncolb)
2375 c-----------------------------------------------------------------------
2376 c gets the number of nonzero elements in each row of A*B and the total
2377 c number of nonzero elements in A*B.
2378 c-----------------------------------------------------------------------
2379 c on entry:
2380 c --------
2381 c
2382 c nrow = integer. row dimension of matrix A
2383 c ncol = integer. column dimension of matrix A = row dimension of
2384 c matrix B.
2385 c ncolb = integer. the colum dimension of the matrix B.
2386 c
2387 c ja, ia= row structure of input matrix A: ja = column indices of
2388 c the nonzero elements of A stored by rows.
2389 c ia = pointer to beginning of each row in ja.
2390 c
2391 c jb, ib= row structure of input matrix B: jb = column indices of
2392 c the nonzero elements of A stored by rows.
2393 c ib = pointer to beginning of each row in jb.
2394 c
2395 c on return:
2396 c ---------
2397 c ndegr = integer array of length nrow containing the degrees (i.e.,
2398 c the number of nonzeros in each row of the matrix A * B
2399 c
2400 c nnz = total number of nonzero elements found in A * B
2401 c
2402 c work arrays:
2403 c-------------
2404 c iw = integer work array of length ncolb.
2405 c-----------------------------------------------------------------------
2406 do 1 k=1, ncolb
2407 iw(k) = 0
2408 1 continue
2409
2410 do 2 k=1, nrow
2411 ndegr(k) = 0
2412 2 continue
2413 c
2414 c method used: Transp(A) * A = sum [over i=1, nrow] a(i)^T a(i)
2415 c where a(i) = i-th row of A. We must be careful not to add the
2416 c elements already accounted for.
2417 c
2418 c
2419 do 7 ii=1,nrow
2420 c
2421 c for each row of A
2422 c
2423 ldg = 0
2424 c
2425 c end-of-linked list
2426 c
2427 last = -1
2428 do 6 j = ia(ii),ia(ii+1)-1
2429 c
2430 c row number to be added:
2431 c
2432 jr = ja(j)
2433 do 5 k=ib(jr),ib(jr+1)-1
2434 jc = jb(k)
2435 if (iw(jc) .eq. 0) then
2436 c
2437 c add one element to the linked list
2438 c
2439 ldg = ldg + 1
2440 iw(jc) = last
2441 last = jc
2442 endif
2443 5 continue
2444 6 continue
2445 ndegr(ii) = ldg
2446 c
2447 c reset iw to zero
2448 c
2449 do 61 k=1,ldg
2450 j = iw(last)
2451 iw(last) = 0
2452 last = j
2453 61 continue
2454 c-----------------------------------------------------------------------
2455 7 continue
2456 c
2457 nnz = 0
2458 do 8 ii=1, nrow
2459 nnz = nnz+ndegr(ii)
2460 8 continue
2461 c
2462 return
2463 c---------------end-of-amubdg ------------------------------------------
2464 c-----------------------------------------------------------------------
2465 end
2466 c-----------------------------------------------------------------------
2467 subroutine aplbdg (nrow,ncol,ja,ia,jb,ib,ndegr,nnz,iw)
2468 integer ja(*),jb(*),ia(nrow+1),ib(nrow+1),iw(ncol),ndegr(nrow)
2469 c-----------------------------------------------------------------------
2470 c gets the number of nonzero elements in each row of A+B and the total
2471 c number of nonzero elements in A+B.
2472 c-----------------------------------------------------------------------
2473 c on entry:
2474 c ---------
2475 c nrow = integer. The row dimension of A and B
2476 c ncol = integer. The column dimension of A and B.
2477 c
2478 c a,
2479 c ja,
2480 c ia = Matrix A in compressed sparse row format.
2481 c
2482 c b,
2483 c jb,
2484 c ib = Matrix B in compressed sparse row format.
2485 c
2486 c on return:
2487 c----------
2488 c ndegr = integer array of length nrow containing the degrees (i.e.,
2489 c the number of nonzeros in each row of the matrix A + B.
2490 c
2491 c nnz = total number of nonzero elements found in A * B
2492 c
2493 c work arrays:
2494 c------------
2495 c iw = integer work array of length equal to ncol.
2496 c
2497 c-----------------------------------------------------------------------
2498 do 1 k=1, ncol
2499 iw(k) = 0
2500 1 continue
2501 c
2502 do 2 k=1, nrow
2503 ndegr(k) = 0
2504 2 continue
2505 c
2506 do 7 ii=1,nrow
2507 ldg = 0
2508 c
2509 c end-of-linked list
2510 c
2511 last = -1
2512 c
2513 c row of A
2514 c
2515 do 5 j = ia(ii),ia(ii+1)-1
2516 jr = ja(j)
2517 c
2518 c add element to the linked list
2519 c
2520 ldg = ldg + 1
2521 iw(jr) = last
2522 last = jr
2523 5 continue
2524 c
2525 c row of B
2526 c
2527 do 6 j=ib(ii),ib(ii+1)-1
2528 jc = jb(j)
2529 if (iw(jc) .eq. 0) then
2530 c
2531 c add one element to the linked list
2532 c
2533 ldg = ldg + 1
2534 iw(jc) = last
2535 last = jc
2536 endif
2537 6 continue
2538 c done with row ii.
2539 ndegr(ii) = ldg
2540 c
2541 c reset iw to zero
2542 c
2543 do 61 k=1,ldg
2544 j = iw(last)
2545 iw(last) = 0
2546 last = j
2547 61 continue
2548 c-----------------------------------------------------------------------
2549 7 continue
2550 c
2551 nnz = 0
2552 do 8 ii=1, nrow
2553 nnz = nnz+ndegr(ii)
2554 8 continue
2555 return
2556 c----------------end-of-aplbdg -----------------------------------------
2557 c-----------------------------------------------------------------------
2558 end
2559 c-----------------------------------------------------------------------
2560 subroutine rnrms (nrow, nrm, a, ja, ia, diag)
2561 real*8 a(*), diag(nrow), scal
2562 integer ja(*), ia(nrow+1)
2563 c-----------------------------------------------------------------------
2564 c gets the norms of each row of A. (choice of three norms)
2565 c-----------------------------------------------------------------------
2566 c on entry:
2567 c ---------
2568 c nrow = integer. The row dimension of A
2569 c
2570 c nrm = integer. norm indicator. nrm = 1, means 1-norm, nrm =2
2571 c means the 2-nrm, nrm = 0 means max norm
2572 c
2573 c a,
2574 c ja,
2575 c ia = Matrix A in compressed sparse row format.
2576 c
2577 c on return:
2578 c----------
2579 c
2580 c diag = real vector of length nrow containing the norms
2581 c
2582 c-----------------------------------------------------------------
2583 do 1 ii=1,nrow
2584 c
2585 c compute the norm if each element.
2586 c
2587 scal = 0.0d0
2588 k1 = ia(ii)
2589 k2 = ia(ii+1)-1
2590 if (nrm .eq. 0) then
2591 do 2 k=k1, k2
2592 scal = max(scal,abs(a(k) ) )
2593 2 continue
2594 elseif (nrm .eq. 1) then
2595 do 3 k=k1, k2
2596 scal = scal + abs(a(k) )
2597 3 continue
2598 else
2599 do 4 k=k1, k2
2600 scal = scal+a(k)**2
2601 4 continue
2602 endif
2603 if (nrm .eq. 2) scal = sqrt(scal)
2604 diag(ii) = scal
2605 1 continue
2606 return
2607 c-----------------------------------------------------------------------
2608 c-------------end-of-rnrms----------------------------------------------
2609 end
2610 c-----------------------------------------------------------------------
2611 subroutine cnrms (nrow, nrm, a, ja, ia, diag)
2612 real*8 a(*), diag(nrow)
2613 integer ja(*), ia(nrow+1)
2614 c-----------------------------------------------------------------------
2615 c gets the norms of each column of A. (choice of three norms)
2616 c-----------------------------------------------------------------------
2617 c on entry:
2618 c ---------
2619 c nrow = integer. The row dimension of A
2620 c
2621 c nrm = integer. norm indicator. nrm = 1, means 1-norm, nrm =2
2622 c means the 2-nrm, nrm = 0 means max norm
2623 c
2624 c a,
2625 c ja,
2626 c ia = Matrix A in compressed sparse row format.
2627 c
2628 c on return:
2629 c----------
2630 c
2631 c diag = real vector of length nrow containing the norms
2632 c
2633 c-----------------------------------------------------------------
2634 do 10 k=1, nrow
2635 diag(k) = 0.0d0
2636 10 continue
2637 do 1 ii=1,nrow
2638 k1 = ia(ii)
2639 k2 = ia(ii+1)-1
2640 do 2 k=k1, k2
2641 j = ja(k)
2642 c update the norm of each column
2643 if (nrm .eq. 0) then
2644 diag(j) = max(diag(j),abs(a(k) ) )
2645 elseif (nrm .eq. 1) then
2646 diag(j) = diag(j) + abs(a(k) )
2647 else
2648 diag(j) = diag(j)+a(k)**2
2649 endif
2650 2 continue
2651 1 continue
2652 if (nrm .ne. 2) return
2653 do 3 k=1, nrow
2654 diag(k) = sqrt(diag(k))
2655 3 continue
2656 return
2657 c-----------------------------------------------------------------------
2658 c------------end-of-cnrms-----------------------------------------------
2659 end
2660 c-----------------------------------------------------------------------
2661 subroutine roscal(nrow,job,nrm,a,ja,ia,diag,b,jb,ib,ierr)
2662 real*8 a(*), b(*), diag(nrow)
2663 integer nrow,job,nrm,ja(*),jb(*),ia(nrow+1),ib(nrow+1),ierr
2664 c-----------------------------------------------------------------------
2665 c scales the rows of A such that their norms are one on return
2666 c 3 choices of norms: 1-norm, 2-norm, max-norm.
2667 c-----------------------------------------------------------------------
2668 c on entry:
2669 c ---------
2670 c nrow = integer. The row dimension of A
2671 c
2672 c job = integer. job indicator. Job=0 means get array b only
2673 c job = 1 means get b, and the integer arrays ib, jb.
2674 c
2675 c nrm = integer. norm indicator. nrm = 1, means 1-norm, nrm =2
2676 c means the 2-nrm, nrm = 0 means max norm
2677 c
2678 c a,
2679 c ja,
2680 c ia = Matrix A in compressed sparse row format.
2681 c
2682 c on return:
2683 c----------
2684 c
2685 c diag = diagonal matrix stored as a vector containing the matrix
2686 c by which the rows have been scaled, i.e., on return
2687 c we have B = Diag*A.
2688 c
2689 c b,
2690 c jb,
2691 c ib = resulting matrix B in compressed sparse row sparse format.
2692 c
2693 c ierr = error message. ierr=0 : Normal return
2694 c ierr=i > 0 : Row number i is a zero row.
2695 c Notes:
2696 c-------
2697 c 1) The column dimension of A is not needed.
2698 c 2) algorithm in place (B can take the place of A).
2699 c-----------------------------------------------------------------
2700 call rnrms (nrow,nrm,a,ja,ia,diag)
2701 ierr = 0
2702 do 1 j=1, nrow
2703 if (diag(j) .eq. 0.0d0) then
2704 ierr = j
2705 return
2706 else
2707 diag(j) = 1.0d0/diag(j)
2708 endif
2709 1 continue
2710 call diamua(nrow,job,a,ja,ia,diag,b,jb,ib)
2711 return
2712 c-------end-of-roscal---------------------------------------------------
2713 c-----------------------------------------------------------------------
2714 end
2715 c-----------------------------------------------------------------------
2716 subroutine coscal(nrow,job,nrm,a,ja,ia,diag,b,jb,ib,ierr)
2717 c-----------------------------------------------------------------------
2718 real*8 a(*),b(*),diag(nrow)
2719 integer nrow,job,ja(*),jb(*),ia(nrow+1),ib(nrow+1),ierr
2720 c-----------------------------------------------------------------------
2721 c scales the columns of A such that their norms are one on return
2722 c result matrix written on b, or overwritten on A.
2723 c 3 choices of norms: 1-norm, 2-norm, max-norm. in place.
2724 c-----------------------------------------------------------------------
2725 c on entry:
2726 c ---------
2727 c nrow = integer. The row dimension of A
2728 c
2729 c job = integer. job indicator. Job=0 means get array b only
2730 c job = 1 means get b, and the integer arrays ib, jb.
2731 c
2732 c nrm = integer. norm indicator. nrm = 1, means 1-norm, nrm =2
2733 c means the 2-nrm, nrm = 0 means max norm
2734 c
2735 c a,
2736 c ja,
2737 c ia = Matrix A in compressed sparse row format.
2738 c
2739 c on return:
2740 c----------
2741 c
2742 c diag = diagonal matrix stored as a vector containing the matrix
2743 c by which the columns have been scaled, i.e., on return
2744 c we have B = A * Diag
2745 c
2746 c b,
2747 c jb,
2748 c ib = resulting matrix B in compressed sparse row sparse format.
2749 c
2750 c ierr = error message. ierr=0 : Normal return
2751 c ierr=i > 0 : Column number i is a zero row.
2752 c Notes:
2753 c-------
2754 c 1) The column dimension of A is not needed.
2755 c 2) algorithm in place (B can take the place of A).
2756 c-----------------------------------------------------------------
2757 call cnrms (nrow,nrm,a,ja,ia,diag)
2758 ierr = 0
2759 do 1 j=1, nrow
2760 if (diag(j) .eq. 0.0) then
2761 ierr = j
2762 return
2763 else
2764 diag(j) = 1.0d0/diag(j)
2765 endif
2766 1 continue
2767 call amudia (nrow,job,a,ja,ia,diag,b,jb,ib)
2768 return
2769 c--------end-of-coscal--------------------------------------------------
2770 c-----------------------------------------------------------------------
2771 end
2772 c-----------------------------------------------------------------------
2773 subroutine addblk(nrowa, ncola, a, ja, ia, ipos, jpos, job,
2774 & nrowb, ncolb, b, jb, ib, nrowc, ncolc, c, jc, ic, nzmx, ierr)
2775 c implicit none
2776 integer nrowa, nrowb, nrowc, ncola, ncolb, ncolc, ipos, jpos
2777 integer nzmx, ierr, job
2778 integer ja(1:*), ia(1:*), jb(1:*), ib(1:*), jc(1:*), ic(1:*)
2779 real*8 a(1:*), b(1:*), c(1:*)
2780 c-----------------------------------------------------------------------
2781 c This subroutine adds a matrix B into a submatrix of A whose
2782 c (1,1) element is located in the starting position (ipos, jpos).
2783 c The resulting matrix is allowed to be larger than A (and B),
2784 c and the resulting dimensions nrowc, ncolc will be redefined
2785 c accordingly upon return.
2786 c The input matrices are assumed to be sorted, i.e. in each row
2787 c the column indices appear in ascending order in the CSR format.
2788 c-----------------------------------------------------------------------
2789 c on entry:
2790 c ---------
2791 c nrowa = number of rows in A.
2792 c bcola = number of columns in A.
2793 c a,ja,ia = Matrix A in compressed sparse row format with entries sorted
2794 c nrowb = number of rows in B.
2795 c ncolb = number of columns in B.
2796 c b,jb,ib = Matrix B in compressed sparse row format with entries sorted
2797 c
2798 c nzmax = integer. The length of the arrays c and jc. addblk will
2799 c stop if the number of nonzero elements in the matrix C
2800 c exceeds nzmax. See ierr.
2801 c
2802 c on return:
2803 c----------
2804 c nrowc = number of rows in C.
2805 c ncolc = number of columns in C.
2806 c c,jc,ic = resulting matrix C in compressed sparse row sparse format
2807 c with entries sorted ascendly in each row.
2808 c
2809 c ierr = integer. serving as error message.
2810 c ierr = 0 means normal return,
2811 c ierr .gt. 0 means that addblk stopped while computing the
2812 c i-th row of C with i=ierr, because the number
2813 c of elements in C exceeds nzmax.
2814 c
2815 c Notes:
2816 c-------
2817 c this will not work if any of the two input matrices is not sorted
2818 c-----------------------------------------------------------------------
2819 logical values
2820 integer i,j1,j2,ka,kb,kc,kamax,kbmax
2821 values = (job .ne. 0)
2822 ierr = 0
2823 nrowc = max(nrowa, nrowb+ipos-1)
2824 ncolc = max(ncola, ncolb+jpos-1)
2825 kc = 1
2826 kbmax = 0
2827 ic(1) = kc
2828 c
2829 do 10 i=1, nrowc
2830 if (i.le.nrowa) then
2831 ka = ia(i)
2832 kamax = ia(i+1)-1
2833 else
2834 ka = ia(nrowa+1)
2835 end if
2836 if ((i.ge.ipos).and.((i-ipos).le.nrowb)) then
2837 kb = ib(i-ipos+1)
2838 kbmax = ib(i-ipos+2)-1
2839 else
2840 kb = ib(nrowb+1)
2841 end if
2842 c
2843 c a do-while type loop -- goes through all the elements in a row.
2844 c
2845 20 continue
2846 if (ka .le. kamax) then
2847 j1 = ja(ka)
2848 else
2849 j1 = ncolc+1
2850 endif
2851 if (kb .le. kbmax) then
2852 j2 = jb(kb) + jpos - 1
2853 else
2854 j2 = ncolc+1
2855 endif
2856 c
2857 c if there are more elements to be added.
2858 c
2859 if ((ka .le. kamax .or. kb .le. kbmax) .and.
2860 & (j1 .le. ncolc .or. j2 .le. ncolc)) then
2861 c
2862 c three cases
2863 c
2864 if (j1 .eq. j2) then
2865 if (values) c(kc) = a(ka)+b(kb)
2866 jc(kc) = j1
2867 ka = ka+1
2868 kb = kb+1
2869 kc = kc+1
2870 else if (j1 .lt. j2) then
2871 jc(kc) = j1
2872 if (values) c(kc) = a(ka)
2873 ka = ka+1
2874 kc = kc+1
2875 else if (j1 .gt. j2) then
2876 jc(kc) = j2
2877 if (values) c(kc) = b(kb)
2878 kb = kb+1
2879 kc = kc+1
2880 endif
2881 if (kc .gt. nzmx) goto 999
2882 goto 20
2883 end if
2884 ic(i+1) = kc
2885 10 continue
2886 return
2887 999 ierr = i
2888 return
2889 c---------end-of-addblk-------------------------------------------------
2890 end
2891 c-----------------------------------------------------------------------
2892 subroutine get1up (n,ja,ia,ju)
2893 integer n, ja(*),ia(*),ju(*)
2894 c----------------------------------------------------------------------
2895 c obtains the first element of each row of the upper triangular part
2896 c of a matrix. Assumes that the matrix is already sorted.
2897 c-----------------------------------------------------------------------
2898 c parameters
2899 c input
2900 c -----
2901 c ja = integer array containing the column indices of aij
2902 c ia = pointer array. ia(j) contains the position of the
2903 c beginning of row j in ja
2904 c
2905 c output
2906 c ------
2907 c ju = integer array of length n. ju(i) is the address in ja
2908 c of the first element of the uper triangular part of
2909 c of A (including rthe diagonal. Thus if row i does have
2910 c a nonzero diagonal element then ju(i) will point to it.
2911 c This is a more general version of diapos.
2912 c-----------------------------------------------------------------------
2913 c local vAriables
2914 integer i, k
2915 c
2916 do 5 i=1, n
2917 ju(i) = 0
2918 k = ia(i)
2919 c
2920 1 continue
2921 if (ja(k) .ge. i) then
2922 ju(i) = k
2923 goto 5
2924 elseif (k .lt. ia(i+1) -1) then
2925 k=k+1
2926 c
2927 c go try next element in row
2928 c
2929 goto 1
2930 endif
2931 5 continue
2932 return
2933 c-----end-of-get1up-----------------------------------------------------
2934 end
2935 c----------------------------------------------------------------------
2936 subroutine xtrows (i1,i2,a,ja,ia,ao,jao,iao,iperm,job)
2937 integer i1,i2,ja(*),ia(*),jao(*),iao(*),iperm(*),job
2938 real*8 a(*),ao(*)
2939 c-----------------------------------------------------------------------
2940 c this subroutine extracts given rows from a matrix in CSR format.
2941 c Specifically, rows number iperm(i1), iperm(i1+1), ...., iperm(i2)
2942 c are extracted and put in the output matrix ao, jao, iao, in CSR
2943 c format. NOT in place.
2944 c Youcef Saad -- coded Feb 15, 1992.
2945 c-----------------------------------------------------------------------
2946 c on entry:
2947 c----------
2948 c i1,i2 = two integers indicating the rows to be extracted.
2949 c xtrows will extract rows iperm(i1), iperm(i1+1),..,iperm(i2),
2950 c from original matrix and stack them in output matrix
2951 c ao, jao, iao in csr format
2952 c
2953 c a, ja, ia = input matrix in csr format
2954 c
2955 c iperm = integer array of length nrow containing the reverse permutation
2956 c array for the rows. row number iperm(j) in permuted matrix PA
2957 c used to be row number j in unpermuted matrix.
2958 c ---> a(i,j) in the permuted matrix was a(iperm(i),j)
2959 c in the inout matrix.
2960 c
2961 c job = integer indicating the work to be done:
2962 c job .ne. 1 : get structure only of output matrix,,
2963 c i.e., ignore real values. (in which case arrays a
2964 c and ao are not used nor accessed).
2965 c job = 1 get complete data structure of output matrix.
2966 c (i.e., including arrays ao and iao).
2967 c------------
2968 c on return:
2969 c------------
2970 c ao, jao, iao = input matrix in a, ja, ia format
2971 c note :
2972 c if (job.ne.1) then the arrays a and ao are not used.
2973 c----------------------------------------------------------------------c
2974 c Y. Saad, revised May 2, 1990 c
2975 c----------------------------------------------------------------------c
2976 logical values
2977 values = (job .eq. 1)
2978 c
2979 c copying
2980 c
2981 ko = 1
2982 iao(1) = ko
2983 do 100 j=i1,i2
2984 c
2985 c ii=iperm(j) is the index of old row to be copied.
2986 c
2987 ii = iperm(j)
2988 do 60 k=ia(ii), ia(ii+1)-1
2989 jao(ko) = ja(k)
2990 if (values) ao(ko) = a(k)
2991 ko = ko+1
2992 60 continue
2993 iao(j-i1+2) = ko
2994 100 continue
2995 c
2996 return
2997 c---------end-of-xtrows-------------------------------------------------
2998 c-----------------------------------------------------------------------
2999 end
3000 c-----------------------------------------------------------------------
3001 subroutine csrkvstr(n, ia, ja, nr, kvstr)
3002 c-----------------------------------------------------------------------
3003 integer n, ia(n+1), ja(*), nr, kvstr(*)
3004 c-----------------------------------------------------------------------
3005 c Finds block row partitioning of matrix in CSR format.
3006 c-----------------------------------------------------------------------
3007 c On entry:
3008 c--------------
3009 c n = number of matrix scalar rows
3010 c ia,ja = input matrix sparsity structure in CSR format
3011 c
3012 c On return:
3013 c---------------
3014 c nr = number of block rows
3015 c kvstr = first row number for each block row
3016 c
3017 c Notes:
3018 c-----------
3019 c Assumes that the matrix is sorted by columns.
3020 c This routine does not need any workspace.
3021 c
3022 c-----------------------------------------------------------------------
3023 c local variables
3024 integer i, j, jdiff
3025 c-----------------------------------------------------------------------
3026 nr = 1
3027 kvstr(1) = 1
3028 c---------------------------------
3029 do i = 2, n
3030 jdiff = ia(i+1)-ia(i)
3031 if (jdiff .eq. ia(i)-ia(i-1)) then
3032 do j = ia(i), ia(i+1)-1
3033 if (ja(j) .ne. ja(j-jdiff)) then
3034 nr = nr + 1
3035 kvstr(nr) = i
3036 goto 299
3037 endif
3038 enddo
3039 299 continue
3040 else
3041 300 nr = nr + 1
3042 kvstr(nr) = i
3043 endif
3044 enddo
3045 kvstr(nr+1) = n+1
3046 c---------------------------------
3047 return
3048 end
3049 c-----------------------------------------------------------------------
3050 c------------------------end-of-csrkvstr--------------------------------
3051 subroutine csrkvstc(n, ia, ja, nc, kvstc, iwk)
3052 c-----------------------------------------------------------------------
3053 integer n, ia(n+1), ja(*), nc, kvstc(*), iwk(*)
3054 c-----------------------------------------------------------------------
3055 c Finds block column partitioning of matrix in CSR format.
3056 c-----------------------------------------------------------------------
3057 c On entry:
3058 c--------------
3059 c n = number of matrix scalar rows
3060 c ia,ja = input matrix sparsity structure in CSR format
3061 c
3062 c On return:
3063 c---------------
3064 c nc = number of block columns
3065 c kvstc = first column number for each block column
3066 c
3067 c Work space:
3068 c----------------
3069 c iwk(*) of size equal to the number of scalar columns plus one.
3070 c Assumed initialized to 0, and left initialized on return.
3071 c
3072 c Notes:
3073 c-----------
3074 c Assumes that the matrix is sorted by columns.
3075 c
3076 c-----------------------------------------------------------------------
3077 c local variables
3078 integer i, j, k, ncol
3079 c
3080 c-----------------------------------------------------------------------
3081 c-----use ncol to find maximum scalar column number
3082 ncol = 0
3083 c-----mark the beginning position of the blocks in iwk
3084 do i = 1, n
3085 if (ia(i) .lt. ia(i+1)) then
3086 j = ja(ia(i))
3087 iwk(j) = 1
3088 do k = ia(i)+1, ia(i+1)-1
3089 j = ja(k)
3090 if (ja(k-1).ne.j-1) then
3091 iwk(j) = 1
3092 iwk(ja(k-1)+1) = 1
3093 endif
3094 enddo
3095 iwk(j+1) = 1
3096 ncol = max0(ncol, j)
3097 endif
3098 enddo
3099 c---------------------------------
3100 nc = 1
3101 kvstc(1) = 1
3102 do i = 2, ncol+1
3103 if (iwk(i).ne.0) then
3104 nc = nc + 1
3105 kvstc(nc) = i
3106 iwk(i) = 0
3107 endif
3108 enddo
3109 nc = nc - 1
3110 c---------------------------------
3111 return
3112 end
3113 c-----------------------------------------------------------------------
3114 c------------------------end-of-csrkvstc--------------------------------
3115 c-----------------------------------------------------------------------
3116 subroutine kvstmerge(nr, kvstr, nc, kvstc, n, kvst)
3117 c-----------------------------------------------------------------------
3118 integer nr, kvstr(nr+1), nc, kvstc(nc+1), n, kvst(*)
3119 c-----------------------------------------------------------------------
3120 c Merges block partitionings, for conformal row/col pattern.
3121 c-----------------------------------------------------------------------
3122 c On entry:
3123 c--------------
3124 c nr,nc = matrix block row and block column dimension
3125 c kvstr = first row number for each block row
3126 c kvstc = first column number for each block column
3127 c
3128 c On return:
3129 c---------------
3130 c n = conformal row/col matrix block dimension
3131 c kvst = conformal row/col block partitioning
3132 c
3133 c Notes:
3134 c-----------
3135 c If matrix is not square, this routine returns without warning.
3136 c
3137 c-----------------------------------------------------------------------
3138 c-----local variables
3139 integer i,j
3140 c---------------------------------
3141 if (kvstr(nr+1) .ne. kvstc(nc+1)) return
3142 i = 1
3143 j = 1
3144 n = 1
3145 200 if (i .gt. nr+1) then
3146 kvst(n) = kvstc(j)
3147 j = j + 1
3148 elseif (j .gt. nc+1) then
3149 kvst(n) = kvstr(i)
3150 i = i + 1
3151 elseif (kvstc(j) .eq. kvstr(i)) then
3152 kvst(n) = kvstc(j)
3153 j = j + 1
3154 i = i + 1
3155 elseif (kvstc(j) .lt. kvstr(i)) then
3156 kvst(n) = kvstc(j)
3157 j = j + 1
3158 else
3159 kvst(n) = kvstr(i)
3160 i = i + 1
3161 endif
3162 n = n + 1
3163 if (i.le.nr+1 .or. j.le.nc+1) goto 200
3164 n = n - 2
3165 c---------------------------------
3166 return
3167 c------------------------end-of-kvstmerge-------------------------------
3168 end