2
c author: travis e. oliphant, 2004
4
c computational tools for sparse matrices in <_t>
6
c add two matrices in compressed sparse column format
7
c this assumes that the rowa indices are sorted for each column
8
c and indexes are 0-based
10
subroutine <_c>cscadd(n,a,rowa,ptra,nnzamax,b,rowb,ptrb,nnzbmax,
11
$ c,rowc,ptrc,nnzcmax,ierr)
12
<_t> a(0:nnzamax-1), b(0:nnzbmax-1), c(0:nnzcmax-1), val
13
integer n, rowa(0:nnzamax-1), rowb(0:nnzbmax-1), rowc(0:nnzcmax-1)
14
integer ptra(0:n), ptrb(0:n), ptrc(0:n), nnzamax, nnzbmax
15
integer ia, ib, ra, rb, j, nnzcmax
23
c is there a need to initialize the output arrays? assume no for now.
35
do while ((ia.lt.iamax).and.(ib.lt.ibmax))
39
c this is a common element
43
if (val.eq.0.0) goto 10
44
if (nnzc.ge.nnzcmax) goto 999
47
else if (ra .lt. rb) then
48
c a has this but not b
51
if (val.eq.0.0) goto 10
52
if (nnzc.ge.nnzcmax) goto 999
56
c b has this but not a
59
if (val.eq.0.0) goto 10
60
if (nnzc.ge.nnzcmax) goto 999
64
ptrc(j+1) = ptrc(j+1) + 1
69
if (ia .eq. iamax) then
70
c all finished with a for this column just copy the rest from b
71
do while (ib .lt. ibmax)
76
if (nnzc.ge.nnzcmax) goto 999
79
ptrc(j+1) = ptrc(j+1) + 1
83
else if (ib .eq. ibmax) then
84
c all finished with b for this column just copy the rest from a
85
do while (ia .lt. iamax)
90
if (nnzc.ge.nnzcmax) goto 999
93
ptrc(j+1) = ptrc(j+1) + 1
100
c successful completion (fix the ptr array)
103
cumsum = cumsum + ptrc(k)
112
end subroutine <_c>cscadd
114
c element-by-element multiplication
115
c can have at most the minimum of nnzamax and nnzbmax
117
subroutine <_c>cscmul(n,a,rowa,ptra,nnzamax,b,rowb,ptrb,nnzbmax,
118
$ c,rowc,ptrc,nnzcmax,ierr)
119
<_t> a(0:nnzamax-1), b(0:nnzbmax-1), c(0:nnzcmax-1), val
120
integer n, rowa(0:nnzamax-1), rowb(0:nnzbmax-1), rowc(0:nnzcmax-1)
121
integer ptra(0:n), ptrb(0:n), ptrc(0:n), nnzamax, nnzbmax
122
integer ia, ib, ra, rb, j, nnzcmax
123
integer ierr, k, cumsum
132
do while ((ia.lt.iamax).and.(ib.lt.ibmax))
136
c this is a common element
140
if (val.eq.0.0) goto 10
141
if (nnzc.ge.nnzcmax) goto 999
144
ptrc(j+1) = ptrc(j+1) + 1
146
else if (ra .lt. rb) then
147
c a has this but not b
150
c b has this but not a
157
c successful completion (fix the ptr array)
160
cumsum = cumsum + ptrc(k)
169
end subroutine <_c>cscmul
172
c matrix-vector multiplication
175
subroutine <_c>cscmux(a,rowa,ptra,nnzamax,ncol,x,mrow,y)
176
<_t> a(0:nnzamax-1),x(0:ncol-1),y(0:mrow-1)
177
integer rowa(0:nnzamax-1), ptra(0:ncol)
178
integer nnzamax, mrow, ncol
186
do ia = ptra(j), ptra(j+1)-1
188
y(ra) = y(ra) + a(ia)*x(j)
193
end subroutine <_c>cscmux
196
subroutine <_c>csrmux(a,cola,ptra,nnzamax,ncol,x,mrow,y)
197
<_t> a(0:nnzamax-1),x(0:ncol-1),y(0:mrow-1)
198
integer cola(0:nnzamax-1), ptra(0:mrow)
199
integer nnzamax, mrow, ncol
204
do ja = ptra(i), ptra(i+1)-1
206
y(i) = y(i) + a(ja)*x(ca)
211
end subroutine <_c>csrmux
214
c matrix multiplication
217
c where c is mxn csc matrix
218
c a is mxk csc matrix
219
c b is kxn csr matrix
221
c irow and kcol give position to start
222
c nnzc gives current last element in output array (initialize to 0)
223
c intended to recall so that calculation can continue
224
c if memory ran-out at last attempt
228
subroutine <_c>cscmucsr(m,k,n, a,rowa,ptra,nnzamax, b,colb,ptrb,
229
$ nnzbmax, c,rowc,ptrc,nnzcmax, irow,kcol,ierr)
231
<_t> a(0:nnzamax-1), b(0:nnzbmax-1), c(0:nnzcmax-1)
232
integer rowa(0:nnzamax-1), colb(0:nnzbmax-1), rowc(0:nnzcmax-1)
233
integer ptra(0:k), ptrb(0:k), ptrc(0:n)
234
integer nnzamax, nnzbmax, nnzcmax
235
integer m, k, n, irow, kcol, ierr
237
integer kk, ii, jjb, jb, cb, ia, nnzc
244
if (nnzc.ge.nnzcmax) goto 999
246
c loop through the column array of b using the ptrb array
247
c so that we know which row we are on.
249
do jb = ptrb(jjb), ptrb(jjb+1)-1
252
c see if aij is nonzero and if so add to val
253
do ia = ptra(jjb), ptra(jjb+1)-1
254
if (rowa(ia).eq.ii) then
255
val = val + a(ia)*b(jb)
262
c there is a non-zero value for this value of i and k
265
ptrc(kk+1) = ptrc(kk+1) + 1
279
end subroutine <_c>cscmucsr
283
c matrix multiplication
286
c where c is mxn csc matrix
287
c a is mxk csr matrix
288
c b is kxn csc matrix
290
c irow and jcol give position to start
291
c bnum gives current size of output array
292
c intended to recall so that calculation can continue
293
c where it left off if memory runs-out during calculation
295
subroutine <_c>csrmucsc(m,n,a,cola,ptra,nnzamax,b,rowb,ptrb,
296
$ nnzbmax,c,rowc,ptrc,nnzcmax,irow,kcol,ierr)
298
<_t> a(0:nnzamax-1), b(0:nnzbmax-1), c(0:nnzcmax-1)
299
integer cola(0:nnzamax-1), rowb(0:nnzbmax-1), rowc(0:nnzcmax-1)
300
integer ptra(0:m), ptrb(0:n), ptrc(0:n)
301
integer nnzamax, nnzbmax, nnzcmax
302
integer m, n, irow, kcol, ierr
304
integer kk, ii, jjb, jb, cb, ia, nnzc
311
if (nnzc.ge.nnzcmax) goto 999
313
c loop through the non-zero rows of b
314
do ib = ptrb(kk), ptrb(kk+1)-1
316
do ja = ptra(ii), ptra(ii+1)-1
317
if (cola(ja).eq.rb) then
318
val = val + a(ja)*b(ib)
323
c there is a non-zero value for this value of i and k
326
ptrc(kk+1) = ptrc(kk+1) + 1
340
end subroutine <_c>csrmucsc
343
c matrix-matrix multiplication
345
c c = a * b where a, b, and c are
346
c compressed sparse column matrices
350
c c = b * a where b, a, and c are
351
c compressed sparse row matrices
353
c intentended for re-entry if nnzcmax is not big enough
355
subroutine <_c>cscmucsc(m,k,n,a,rowa,ptra,nnzamax,b,rowb,ptrb,
356
$ nnzbmax,c,rowc,ptrc,nnzcmax,irow,kcol,ierr)
358
<_t> a(0:nnzamax-1), b(0:nnzbmax-1), c(0:nnzcmax-1)
359
integer rowa(0:nnzamax-1), rowb(0:nnzbmax-1), rowc(0:nnzcmax-1)
360
integer ptra(0:k), ptrb(0:n), ptrc(0:n)
361
integer nnzamax, nnzbmax, nnzcmax
362
integer m, k, n, irow, kcol, ierr
364
integer kk, ii, jjb, jb, cb, ia, nnzc, cumsum
372
c loop through the row array of b for this column
373
do jb = ptrb(kk), ptrb(kk+1)-1
375
do ia = ptra(ra), ptra(ra+1)-1
376
c see if aij is nonzero and if so add to val
377
if (rowa(ia).eq.ii) then
378
val = val + a(ia)*b(jb)
383
c there is a non-zero value for this value of i and k
384
if (nnzc.ge.nnzcmax) goto 999
387
ptrc(kk+1) = ptrc(kk+1) + 1
393
c successful completion (fix the ptr array)
396
cumsum = cumsum + ptrc(kk)
407
end subroutine <_c>cscmucsc