MoDeNa  1.0
Software framework facilitating sequential multi-scale modelling
dgemm.f
1  SUBROUTINE dgemm(TRANSA,TRANSB,M,N,K,ALPHA,A,LDA,B,LDB,BETA,C,LDC)
2 * .. Scalar Arguments ..
3  DOUBLE PRECISION alpha,beta
4  INTEGER k,lda,ldb,ldc,m,n
5  CHARACTER transa,transb
6 * ..
7 * .. Array Arguments ..
8  DOUBLE PRECISION a(lda,*),b(ldb,*),c(ldc,*)
9 * ..
10 *
11 * Purpose
12 * =======
13 *
14 * DGEMM performs one of the matrix-matrix operations
15 *
16 * C := alpha*op( A )*op( B ) + beta*C,
17 *
18 * where op( X ) is one of
19 *
20 * op( X ) = X or op( X ) = X**T,
21 *
22 * alpha and beta are scalars, and A, B and C are matrices, with op( A )
23 * an m by k matrix, op( B ) a k by n matrix and C an m by n matrix.
24 *
25 * Arguments
26 * ==========
27 *
28 * TRANSA - CHARACTER*1.
29 * On entry, TRANSA specifies the form of op( A ) to be used in
30 * the matrix multiplication as follows:
31 *
32 * TRANSA = 'N' or 'n', op( A ) = A.
33 *
34 * TRANSA = 'T' or 't', op( A ) = A**T.
35 *
36 * TRANSA = 'C' or 'c', op( A ) = A**T.
37 *
38 * Unchanged on exit.
39 *
40 * TRANSB - CHARACTER*1.
41 * On entry, TRANSB specifies the form of op( B ) to be used in
42 * the matrix multiplication as follows:
43 *
44 * TRANSB = 'N' or 'n', op( B ) = B.
45 *
46 * TRANSB = 'T' or 't', op( B ) = B**T.
47 *
48 * TRANSB = 'C' or 'c', op( B ) = B**T.
49 *
50 * Unchanged on exit.
51 *
52 * M - INTEGER.
53 * On entry, M specifies the number of rows of the matrix
54 * op( A ) and of the matrix C. M must be at least zero.
55 * Unchanged on exit.
56 *
57 * N - INTEGER.
58 * On entry, N specifies the number of columns of the matrix
59 * op( B ) and the number of columns of the matrix C. N must be
60 * at least zero.
61 * Unchanged on exit.
62 *
63 * K - INTEGER.
64 * On entry, K specifies the number of columns of the matrix
65 * op( A ) and the number of rows of the matrix op( B ). K must
66 * be at least zero.
67 * Unchanged on exit.
68 *
69 * ALPHA - DOUBLE PRECISION.
70 * On entry, ALPHA specifies the scalar alpha.
71 * Unchanged on exit.
72 *
73 * A - DOUBLE PRECISION array of DIMENSION ( LDA, ka ), where ka is
74 * k when TRANSA = 'N' or 'n', and is m otherwise.
75 * Before entry with TRANSA = 'N' or 'n', the leading m by k
76 * part of the array A must contain the matrix A, otherwise
77 * the leading k by m part of the array A must contain the
78 * matrix A.
79 * Unchanged on exit.
80 *
81 * LDA - INTEGER.
82 * On entry, LDA specifies the first dimension of A as declared
83 * in the calling (sub) program. When TRANSA = 'N' or 'n' then
84 * LDA must be at least max( 1, m ), otherwise LDA must be at
85 * least max( 1, k ).
86 * Unchanged on exit.
87 *
88 * B - DOUBLE PRECISION array of DIMENSION ( LDB, kb ), where kb is
89 * n when TRANSB = 'N' or 'n', and is k otherwise.
90 * Before entry with TRANSB = 'N' or 'n', the leading k by n
91 * part of the array B must contain the matrix B, otherwise
92 * the leading n by k part of the array B must contain the
93 * matrix B.
94 * Unchanged on exit.
95 *
96 * LDB - INTEGER.
97 * On entry, LDB specifies the first dimension of B as declared
98 * in the calling (sub) program. When TRANSB = 'N' or 'n' then
99 * LDB must be at least max( 1, k ), otherwise LDB must be at
100 * least max( 1, n ).
101 * Unchanged on exit.
102 *
103 * BETA - DOUBLE PRECISION.
104 * On entry, BETA specifies the scalar beta. When BETA is
105 * supplied as zero then C need not be set on input.
106 * Unchanged on exit.
107 *
108 * C - DOUBLE PRECISION array of DIMENSION ( LDC, n ).
109 * Before entry, the leading m by n part of the array C must
110 * contain the matrix C, except when beta is zero, in which
111 * case C need not be set on entry.
112 * On exit, the array C is overwritten by the m by n matrix
113 * ( alpha*op( A )*op( B ) + beta*C ).
114 *
115 * LDC - INTEGER.
116 * On entry, LDC specifies the first dimension of C as declared
117 * in the calling (sub) program. LDC must be at least
118 * max( 1, m ).
119 * Unchanged on exit.
120 *
121 * Further Details
122 * ===============
123 *
124 * Level 3 Blas routine.
125 *
126 * -- Written on 8-February-1989.
127 * Jack Dongarra, Argonne National Laboratory.
128 * Iain Duff, AERE Harwell.
129 * Jeremy Du Croz, Numerical Algorithms Group Ltd.
130 * Sven Hammarling, Numerical Algorithms Group Ltd.
131 *
132 * =====================================================================
133 *
134 * .. External Functions ..
135  LOGICAL lsame
136  EXTERNAL lsame
137 * ..
138 * .. External Subroutines ..
139  EXTERNAL xerbla
140 * ..
141 * .. Intrinsic Functions ..
142  INTRINSIC max
143 * ..
144 * .. Local Scalars ..
145  DOUBLE PRECISION temp
146  INTEGER i,info,j,l,ncola,nrowa,nrowb
147  LOGICAL nota,notb
148 * ..
149 * .. Parameters ..
150  DOUBLE PRECISION one,zero
151  parameter(one=1.0d+0,zero=0.0d+0)
152 * ..
153 *
154 * Set NOTA and NOTB as true if A and B respectively are not
155 * transposed and set NROWA, NCOLA and NROWB as the number of rows
156 * and columns of A and the number of rows of B respectively.
157 *
158  nota = lsame(transa,'N')
159  notb = lsame(transb,'N')
160  IF (nota) THEN
161  nrowa = m
162  ncola = k
163  ELSE
164  nrowa = k
165  ncola = m
166  END IF
167  IF (notb) THEN
168  nrowb = k
169  ELSE
170  nrowb = n
171  END IF
172 *
173 * Test the input parameters.
174 *
175  info = 0
176  IF ((.NOT.nota) .AND. (.NOT.lsame(transa,'C')) .AND.
177  + (.NOT.lsame(transa,'T'))) THEN
178  info = 1
179  ELSE IF ((.NOT.notb) .AND. (.NOT.lsame(transb,'C')) .AND.
180  + (.NOT.lsame(transb,'T'))) THEN
181  info = 2
182  ELSE IF (m.LT.0) THEN
183  info = 3
184  ELSE IF (n.LT.0) THEN
185  info = 4
186  ELSE IF (k.LT.0) THEN
187  info = 5
188  ELSE IF (lda.LT.max(1,nrowa)) THEN
189  info = 8
190  ELSE IF (ldb.LT.max(1,nrowb)) THEN
191  info = 10
192  ELSE IF (ldc.LT.max(1,m)) THEN
193  info = 13
194  END IF
195  IF (info.NE.0) THEN
196  CALL xerbla('DGEMM ',info)
197  RETURN
198  END IF
199 *
200 * Quick return if possible.
201 *
202  IF ((m.EQ.0) .OR. (n.EQ.0) .OR.
203  + (((alpha.EQ.zero).OR. (k.EQ.0)).AND. (beta.EQ.one))) RETURN
204 *
205 * And if alpha.eq.zero.
206 *
207  IF (alpha.EQ.zero) THEN
208  IF (beta.EQ.zero) THEN
209  DO 20 j = 1,n
210  DO 10 i = 1,m
211  c(i,j) = zero
212  10 CONTINUE
213  20 CONTINUE
214  ELSE
215  DO 40 j = 1,n
216  DO 30 i = 1,m
217  c(i,j) = beta*c(i,j)
218  30 CONTINUE
219  40 CONTINUE
220  END IF
221  RETURN
222  END IF
223 *
224 * Start the operations.
225 *
226  IF (notb) THEN
227  IF (nota) THEN
228 *
229 * Form C := alpha*A*B + beta*C.
230 *
231  DO 90 j = 1,n
232  IF (beta.EQ.zero) THEN
233  DO 50 i = 1,m
234  c(i,j) = zero
235  50 CONTINUE
236  ELSE IF (beta.NE.one) THEN
237  DO 60 i = 1,m
238  c(i,j) = beta*c(i,j)
239  60 CONTINUE
240  END IF
241  DO 80 l = 1,k
242  IF (b(l,j).NE.zero) THEN
243  temp = alpha*b(l,j)
244  DO 70 i = 1,m
245  c(i,j) = c(i,j) + temp*a(i,l)
246  70 CONTINUE
247  END IF
248  80 CONTINUE
249  90 CONTINUE
250  ELSE
251 *
252 * Form C := alpha*A**T*B + beta*C
253 *
254  DO 120 j = 1,n
255  DO 110 i = 1,m
256  temp = zero
257  DO 100 l = 1,k
258  temp = temp + a(l,i)*b(l,j)
259  100 CONTINUE
260  IF (beta.EQ.zero) THEN
261  c(i,j) = alpha*temp
262  ELSE
263  c(i,j) = alpha*temp + beta*c(i,j)
264  END IF
265  110 CONTINUE
266  120 CONTINUE
267  END IF
268  ELSE
269  IF (nota) THEN
270 *
271 * Form C := alpha*A*B**T + beta*C
272 *
273  DO 170 j = 1,n
274  IF (beta.EQ.zero) THEN
275  DO 130 i = 1,m
276  c(i,j) = zero
277  130 CONTINUE
278  ELSE IF (beta.NE.one) THEN
279  DO 140 i = 1,m
280  c(i,j) = beta*c(i,j)
281  140 CONTINUE
282  END IF
283  DO 160 l = 1,k
284  IF (b(j,l).NE.zero) THEN
285  temp = alpha*b(j,l)
286  DO 150 i = 1,m
287  c(i,j) = c(i,j) + temp*a(i,l)
288  150 CONTINUE
289  END IF
290  160 CONTINUE
291  170 CONTINUE
292  ELSE
293 *
294 * Form C := alpha*A**T*B**T + beta*C
295 *
296  DO 200 j = 1,n
297  DO 190 i = 1,m
298  temp = zero
299  DO 180 l = 1,k
300  temp = temp + a(l,i)*b(j,l)
301  180 CONTINUE
302  IF (beta.EQ.zero) THEN
303  c(i,j) = alpha*temp
304  ELSE
305  c(i,j) = alpha*temp + beta*c(i,j)
306  END IF
307  190 CONTINUE
308  200 CONTINUE
309  END IF
310  END IF
311 *
312  RETURN
313 *
314 * End of DGEMM .
315 *
316  END