The LAPACK forum has moved to https://github.com/Reference-LAPACK/lapack/discussions.

Help needed using BLAS SGEMM

Open discussion regarding features, bugs, issues, vendors, etc.

Help needed using BLAS SGEMM

Postby Jokin » Mon Sep 24, 2007 10:54 am

Hello all,
I am trying to understang the use of tha BLAS/LAPACK environmet and subroutines. My very first example deals with the matrix multiplication, which I could program i a simple code, but I prefer using the SGEMM subroutine. I have written an example to read A.txt and B.txt and produce an AB.txt output with the results.
In all the files, the first two numbers shor the Rows/Columns to be read. My only proble is that I do not get any good result. Can somabody help me please?

I attach the code below.

Thankyou very much,
J. Zurutuza


The code I have written is:
--------------------------START---------------------------
c Programa en fortran77 que resuelve un producto de
c dos matrices, usando BLAS
program mul_mat
implicit none
c
c Declaracion de variables
real ALPHA, BETA
integer I,J
integer K0, LDA0,LDB0,LDC0, M0, N0
real A0(1500,1500), B0(1500,1500), C0(1500,1500)

c Reading A.txt.......
open (10, File='A.txt')
read (10,*) LDA0, K0
write (*,*) 'Rows and Cols of A: ',LDA0,'*', K0
READ (10,*) ((A0(I,J),J=1,K0),I=1,LDA0)
c write (*,*) ((A0(I,J), J=1,K0), I=1,LDA0)
write (*,*) 'Finished reading A.txt. Now B.txt'
close (10)
c Leyendo los elementos de B:
open (10, File='B.txt')
read (10,*) LDB0, N0
write (*,*) 'Rows and Cols of B: ',LDB0,'*', N0
READ (10,*) ((B0(I,J),J=1,N0),I=1,LDB0)
c write (*,*) ((B0(I,J), J=1,N0), I=1,LDB0)
write (*,*) 'Finished reading B.txt. Now C=AB'
write (*,*) 'Dimensions of C: ', LDA0,'*', N0
close (10)
M0 = LDA0
LDC0 = LDA0
ALPHA = 1.0
BETA = 0.0

c Calling SGEMM
call SGEMM('No transpuesta de A','No transpuesta de B',
$ M0, N0, K0, ALPHA, A0, LDA0, B0, LDB0, BETA,C0, LDC0)


write (*,*) 'B, M, N, K, ALPHA, LDA, LDB, BETA,LDC:'
write (*,*) M0, N0, K0, ALPHA, LDA0, LDB0, BETA,LDC0


print 1000
print 1010, ((A0(I,J), J=1,K0), I=1,LDA0)
print 1020
print 1030, ((B0(I,J), J=1,N0), I=1,LDB0)

print 1040
print 1030, ((C0(I,J), J=1,N0), I=1,M0)
open (10, File='AB.txt')
write (10,*) M0, N0
write (10,1030) ((C0(I,J), J=1,N0), I=1,M0)
close (10)
1000 format (1X, 'A:')
1010 format (4(3X, F5.1))
1020 format (/1X, 'B:')
1030 format (2(3X, F5.1))
1040 format (/1X, 'AB:')

end
-----------------------------END---------------------------

The data I use are:
Rows and Cols of A: 3* 4
Finished reading A.txt. Now B.txt
Rows and Cols of B: 4* 2
Finished reading B.txt. Now C=AB
Dimensions of C: 3* 2
B, M, N, K, ALPHA, LDA, LDB, BETA,LDC:
3 2 4 1. 3 4 0. 3
A:
1.0 2.0 6.0 -1.0
2.0 -1.0 1.0 1.0
3.0 -2.0 2.0 -3.0

B:
2.0 1.0
-1.0 3.0
1.0 1.0
3.0 -2.0

AB:
2.0 0.0
4.0 0.0
6.0 0.0
Jokin
 
Posts: 5
Joined: Mon Oct 09, 2006 10:08 am

Postby sven » Mon Sep 24, 2007 12:48 pm

Since you have declared each of A, B and C to have a leading dimension of 1500 you need to pass each of LDA, LDB and LDC as 1500.

M, N and K are the matrix dimensions (problem sizes), LDA, LDB and LDC are the first dimensions of the arrays, as actually declared.

Hope that helps,

Sven Hammarling.
sven
 
Posts: 146
Joined: Wed Dec 22, 2004 4:28 am

Postby Jokin » Mon Sep 24, 2007 1:24 pm

It works.
Thankyou very much,
J. Zurutuza
Jokin
 
Posts: 5
Joined: Mon Oct 09, 2006 10:08 am


Return to User Discussion

Who is online

Users browsing this forum: No registered users and 5 guests