-
Notifications
You must be signed in to change notification settings - Fork 1
/
dgemm.f
313 lines (313 loc) · 9.62 KB
/
dgemm.f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
SUBROUTINE DGEMM ( TRANSA, TRANSB, M, N, K, ALPHA, A, LDA, B, LDB,
$ BETA, C, LDC )
* .. SCALAR ARGUMENTS ..
CHARACTER*1 TRANSA, TRANSB
INTEGER M, N, K, LDA, LDB, LDC
DOUBLE PRECISION ALPHA, BETA
* .. ARRAY ARGUMENTS ..
DOUBLE PRECISION A( LDA, * ), B( LDB, * ), C( LDC, * )
* ..
*
* PURPOSE
* =======
*
* DGEMM PERFORMS ONE OF THE MATRIX-MATRIX OPERATIONS
*
* C := ALPHA*OP( A )*OP( B ) + BETA*C,
*
* WHERE OP( X ) IS ONE OF
*
* OP( X ) = X OR OP( X ) = X',
*
* ALPHA AND BETA ARE SCALARS, AND A, B AND C ARE MATRICES, WITH OP( A )
* AN M BY K MATRIX, OP( B ) A K BY N MATRIX AND C AN M BY N MATRIX.
*
* PARAMETERS
* ==========
*
* TRANSA - CHARACTER*1.
* ON ENTRY, TRANSA SPECIFIES THE FORM OF OP( A ) TO BE USED IN
* THE MATRIX MULTIPLICATION AS FOLLOWS:
*
* TRANSA = 'N' OR 'N', OP( A ) = A.
*
* TRANSA = 'T' OR 'T', OP( A ) = A'.
*
* TRANSA = 'C' OR 'C', OP( A ) = A'.
*
* UNCHANGED ON EXIT.
*
* TRANSB - CHARACTER*1.
* ON ENTRY, TRANSB SPECIFIES THE FORM OF OP( B ) TO BE USED IN
* THE MATRIX MULTIPLICATION AS FOLLOWS:
*
* TRANSB = 'N' OR 'N', OP( B ) = B.
*
* TRANSB = 'T' OR 'T', OP( B ) = B'.
*
* TRANSB = 'C' OR 'C', OP( B ) = B'.
*
* UNCHANGED ON EXIT.
*
* M - INTEGER.
* ON ENTRY, M SPECIFIES THE NUMBER OF ROWS OF THE MATRIX
* OP( A ) AND OF THE MATRIX C. M MUST BE AT LEAST ZERO.
* UNCHANGED ON EXIT.
*
* N - INTEGER.
* ON ENTRY, N SPECIFIES THE NUMBER OF COLUMNS OF THE MATRIX
* OP( B ) AND THE NUMBER OF COLUMNS OF THE MATRIX C. N MUST BE
* AT LEAST ZERO.
* UNCHANGED ON EXIT.
*
* K - INTEGER.
* ON ENTRY, K SPECIFIES THE NUMBER OF COLUMNS OF THE MATRIX
* OP( A ) AND THE NUMBER OF ROWS OF THE MATRIX OP( B ). K MUST
* BE AT LEAST ZERO.
* UNCHANGED ON EXIT.
*
* ALPHA - DOUBLE PRECISION.
* ON ENTRY, ALPHA SPECIFIES THE SCALAR ALPHA.
* UNCHANGED ON EXIT.
*
* A - DOUBLE PRECISION ARRAY OF DIMENSION ( LDA, KA ), WHERE KA IS
* K WHEN TRANSA = 'N' OR 'N', AND IS M OTHERWISE.
* BEFORE ENTRY WITH TRANSA = 'N' OR 'N', THE LEADING M BY K
* PART OF THE ARRAY A MUST CONTAIN THE MATRIX A, OTHERWISE
* THE LEADING K BY M PART OF THE ARRAY A MUST CONTAIN THE
* MATRIX A.
* UNCHANGED ON EXIT.
*
* LDA - INTEGER.
* ON ENTRY, LDA SPECIFIES THE FIRST DIMENSION OF A AS DECLARED
* IN THE CALLING (SUB) PROGRAM. WHEN TRANSA = 'N' OR 'N' THEN
* LDA MUST BE AT LEAST MAX( 1, M ), OTHERWISE LDA MUST BE AT
* LEAST MAX( 1, K ).
* UNCHANGED ON EXIT.
*
* B - DOUBLE PRECISION ARRAY OF DIMENSION ( LDB, KB ), WHERE KB IS
* N WHEN TRANSB = 'N' OR 'N', AND IS K OTHERWISE.
* BEFORE ENTRY WITH TRANSB = 'N' OR 'N', THE LEADING K BY N
* PART OF THE ARRAY B MUST CONTAIN THE MATRIX B, OTHERWISE
* THE LEADING N BY K PART OF THE ARRAY B MUST CONTAIN THE
* MATRIX B.
* UNCHANGED ON EXIT.
*
* LDB - INTEGER.
* ON ENTRY, LDB SPECIFIES THE FIRST DIMENSION OF B AS DECLARED
* IN THE CALLING (SUB) PROGRAM. WHEN TRANSB = 'N' OR 'N' THEN
* LDB MUST BE AT LEAST MAX( 1, K ), OTHERWISE LDB MUST BE AT
* LEAST MAX( 1, N ).
* UNCHANGED ON EXIT.
*
* BETA - DOUBLE PRECISION.
* ON ENTRY, BETA SPECIFIES THE SCALAR BETA. WHEN BETA IS
* SUPPLIED AS ZERO THEN C NEED NOT BE SET ON INPUT.
* UNCHANGED ON EXIT.
*
* C - DOUBLE PRECISION ARRAY OF DIMENSION ( LDC, N ).
* BEFORE ENTRY, THE LEADING M BY N PART OF THE ARRAY C MUST
* CONTAIN THE MATRIX C, EXCEPT WHEN BETA IS ZERO, IN WHICH
* CASE C NEED NOT BE SET ON ENTRY.
* ON EXIT, THE ARRAY C IS OVERWRITTEN BY THE M BY N MATRIX
* ( ALPHA*OP( A )*OP( B ) + BETA*C ).
*
* LDC - INTEGER.
* ON ENTRY, LDC SPECIFIES THE FIRST DIMENSION OF C AS DECLARED
* IN THE CALLING (SUB) PROGRAM. LDC MUST BE AT LEAST
* MAX( 1, M ).
* UNCHANGED ON EXIT.
*
*
* LEVEL 3 BLAS ROUTINE.
*
* -- WRITTEN ON 8-FEBRUARY-1989.
* JACK DONGARRA, ARGONNE NATIONAL LABORATORY.
* IAIN DUFF, AERE HARWELL.
* JEREMY DU CROZ, NUMERICAL ALGORITHMS GROUP LTD.
* SVEN HAMMARLING, NUMERICAL ALGORITHMS GROUP LTD.
*
*
* .. EXTERNAL FUNCTIONS ..
LOGICAL LSAME
EXTERNAL LSAME
* .. EXTERNAL SUBROUTINES ..
EXTERNAL XERBLA
* .. INTRINSIC FUNCTIONS ..
INTRINSIC MAX
* .. LOCAL SCALARS ..
LOGICAL NOTA, NOTB
INTEGER I, INFO, J, L, NCOLA, NROWA, NROWB
DOUBLE PRECISION TEMP
* .. PARAMETERS ..
DOUBLE PRECISION ONE , ZERO
PARAMETER ( ONE = 1.0D+0, ZERO = 0.0D+0 )
* ..
* .. EXECUTABLE STATEMENTS ..
*
* SET NOTA AND NOTB AS TRUE IF A AND B RESPECTIVELY ARE NOT
* TRANSPOSED AND SET NROWA, NCOLA AND NROWB AS THE NUMBER OF ROWS
* AND COLUMNS OF A AND THE NUMBER OF ROWS OF B RESPECTIVELY.
*
NOTA = LSAME( TRANSA, 'N' )
NOTB = LSAME( TRANSB, 'N' )
IF( NOTA )THEN
NROWA = M
NCOLA = K
ELSE
NROWA = K
NCOLA = M
END IF
IF( NOTB )THEN
NROWB = K
ELSE
NROWB = N
END IF
*
* TEST THE INPUT PARAMETERS.
*
INFO = 0
IF( ( .NOT.NOTA ).AND.
$ ( .NOT.LSAME( TRANSA, 'C' ) ).AND.
$ ( .NOT.LSAME( TRANSA, 'T' ) ) )THEN
INFO = 1
ELSE IF( ( .NOT.NOTB ).AND.
$ ( .NOT.LSAME( TRANSB, 'C' ) ).AND.
$ ( .NOT.LSAME( TRANSB, 'T' ) ) )THEN
INFO = 2
ELSE IF( M .LT.0 )THEN
INFO = 3
ELSE IF( N .LT.0 )THEN
INFO = 4
ELSE IF( K .LT.0 )THEN
INFO = 5
ELSE IF( LDA.LT.MAX( 1, NROWA ) )THEN
INFO = 8
ELSE IF( LDB.LT.MAX( 1, NROWB ) )THEN
INFO = 10
ELSE IF( LDC.LT.MAX( 1, M ) )THEN
INFO = 13
END IF
IF( INFO.NE.0 )THEN
CALL XERBLA( 'DGEMM ', INFO )
RETURN
END IF
*
* QUICK RETURN IF POSSIBLE.
*
IF( ( M.EQ.0 ).OR.( N.EQ.0 ).OR.
$ ( ( ( ALPHA.EQ.ZERO ).OR.( K.EQ.0 ) ).AND.( BETA.EQ.ONE ) ) )
$ RETURN
*
* AND IF ALPHA.EQ.ZERO.
*
IF( ALPHA.EQ.ZERO )THEN
IF( BETA.EQ.ZERO )THEN
DO 20, J = 1, N
DO 10, I = 1, M
C( I, J ) = ZERO
10 CONTINUE
20 CONTINUE
ELSE
DO 40, J = 1, N
DO 30, I = 1, M
C( I, J ) = BETA*C( I, J )
30 CONTINUE
40 CONTINUE
END IF
RETURN
END IF
*
* START THE OPERATIONS.
*
IF( NOTB )THEN
IF( NOTA )THEN
*
* FORM C := ALPHA*A*B + BETA*C.
*
DO 90, J = 1, N
IF( BETA.EQ.ZERO )THEN
DO 50, I = 1, M
C( I, J ) = ZERO
50 CONTINUE
ELSE IF( BETA.NE.ONE )THEN
DO 60, I = 1, M
C( I, J ) = BETA*C( I, J )
60 CONTINUE
END IF
DO 80, L = 1, K
IF( B( L, J ).NE.ZERO )THEN
TEMP = ALPHA*B( L, J )
DO 70, I = 1, M
C( I, J ) = C( I, J ) + TEMP*A( I, L )
70 CONTINUE
END IF
80 CONTINUE
90 CONTINUE
ELSE
*
* FORM C := ALPHA*A'*B + BETA*C
*
DO 120, J = 1, N
DO 110, I = 1, M
TEMP = ZERO
DO 100, L = 1, K
TEMP = TEMP + A( L, I )*B( L, J )
100 CONTINUE
IF( BETA.EQ.ZERO )THEN
C( I, J ) = ALPHA*TEMP
ELSE
C( I, J ) = ALPHA*TEMP + BETA*C( I, J )
END IF
110 CONTINUE
120 CONTINUE
END IF
ELSE
IF( NOTA )THEN
*
* FORM C := ALPHA*A*B' + BETA*C
*
DO 170, J = 1, N
IF( BETA.EQ.ZERO )THEN
DO 130, I = 1, M
C( I, J ) = ZERO
130 CONTINUE
ELSE IF( BETA.NE.ONE )THEN
DO 140, I = 1, M
C( I, J ) = BETA*C( I, J )
140 CONTINUE
END IF
DO 160, L = 1, K
IF( B( J, L ).NE.ZERO )THEN
TEMP = ALPHA*B( J, L )
DO 150, I = 1, M
C( I, J ) = C( I, J ) + TEMP*A( I, L )
150 CONTINUE
END IF
160 CONTINUE
170 CONTINUE
ELSE
*
* FORM C := ALPHA*A'*B' + BETA*C
*
DO 200, J = 1, N
DO 190, I = 1, M
TEMP = ZERO
DO 180, L = 1, K
TEMP = TEMP + A( L, I )*B( J, L )
180 CONTINUE
IF( BETA.EQ.ZERO )THEN
C( I, J ) = ALPHA*TEMP
ELSE
C( I, J ) = ALPHA*TEMP + BETA*C( I, J )
END IF
190 CONTINUE
200 CONTINUE
END IF
END IF
*
RETURN
*
* END OF DGEMM .
*
END