Community
cancel
Showing results for 
Search instead for 
Did you mean: 
pawan_k_
Beginner
198 Views

Example of submatrix multiplication

Hi,

Are there any example of using dgemm for multiplying submatrices of two matrices?

 

Thanks,

Pawan

0 Kudos
5 Replies
Sridevi_A_Intel
Employee
198 Views

Pawan,

Can you look into the examples directory inside MKL Installation folder?

-Sridevi

pawan_k_
Beginner
198 Views

Hi,

I looked into the example on dgemm. It is not quiet clear how to set lda etc for subblocks.

Here is an example I create.

A = [ 0  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

]

B  =  [

         0  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

]

 

C  = [ 0  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

]

All right! Now I want to operate on submatrices. Let us say I want to multiply a 4x3 submatrix of A as

follows

  

sub_matrix_A = [ 

                            14 15 16

                            24 25 26

                            34 35 36

                            44 45 46

                          ]

 

with a 3x2 sub-matrix of B as follows

sub_matrix_B = [

                      7  8 

                    13 14

                    19 20

                ]

to put in the 4x2 sub-matrix of C as follows

sub_matrix_C = [

          12 13 

           20 21 

          28 29 

          36 37     

       ]

Could someone work out this example for me and future MKL users ? Below are

the parameters to be filled.

cblas_dgemm(CblasRowMajor, CblasNoTrans, CblasNoTrans, 
                m, n, k, alpha, A, k, B, n, beta, C, n);

I want to know the right values of m, n, k to perform

   sub_matrix_C = sub_matrix_A * sub_matrix_B

for the example above.

Regards,

Pawan

 

pawan_k_
Beginner
198 Views

Could anyone workout this example?

Regards,

Pawan

mecej4
Black Belt
198 Views

I suggest that you perform this operation in two steps. Keeping in mind that CBLAS functions are wrappers around the underlying Fortran BLAS routines, and that terms such as "leading dimension" are confusing and possibly incorrect if the C convention of storing matrices as a 1-D array obtained from concatenation of the rows, examine the attached Fortran example and see if it does what you want.

After noting that the results are as expected, we can move on to replicating the same using C and CBLAS, as in the second attached file.

Ying_H_Intel
Employee
198 Views

Hi Pawan, 

You can calculate the sub_matrix multiply by the below parameters

    double alpha = 1.0, beta = 0.;
    int m=4; int k=3; int n=2;
    int lda=10; // the stride of row
    int ldb=6;
    int ldc=8;
    double * subA=&A[0][0]+14;
    double * subB=&B[0][0]+7;
    double * subC=&C[0][0]+12;

I attached the cpp file for you reference. 

Best Regards,

Ying 

 

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <mkl.h>

void init_arr(int N, double* a);

int main(int argc, char* argv[])
{
    double A[6][10]; 
    double B[5][6];
    double C[6][8]; 
    
    init_arr(60,&A[0][0]);
    init_arr(30,&B[0][0]);
    init_arr(48,&C[0][0]);

    double alpha = 1.0, beta = 0.;
    int m=4; int k=3; int n=2;
    int lda=10; // the stride of row
    int ldb=6;
    int ldc=8;
    double * subA=&A[0][0]+14;
    double * subB=&B[0][0]+7;
    double * subC=&C[0][0]+12;
    
    cblas_dgemm(CblasRowMajor,CblasNoTrans,CblasNoTrans,m,n,k,alpha,subA,lda,subB,ldb,beta,subC,ldc);

    printf("Sub Matrix C(mxn): %d x %d\n", m, n);

    for (int i=0;i<m;i++){
    for (int j=0;j<2;j++) {
            printf("%g\t",subC[ldc*i+j]);
        }
    printf("\n");}
    return 1;
}


void init_arr(int N, double* a)

    int i=0;
    for(i=0; i<N;i++) 
            a = i; 
}

build it with mkl library and get 

The result is as below 

Sub Matrix C(mxn): 4 x 2
597     642
987     1062
1377    1482
1767    1902
Press any key to continue . . .

 

Reply