Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Getting dgemm or dgemms to work in C

Getting dgemm or dgemms to work in C

Getting dgemm or dgemms to work in C

Hi there,

I cannot for the life of me get dgemm to spit out the correct answer when I call it from C. I saw your posts on a similar question and hoped you might have an answer.

I would love to have a function that that was some thing like:

void matmult(float ** A, float ** B, float ** C, int Arows, int innerdim, int Bcols)

where you could pass it three arrays float ** or float * form (I don't care, I just want something to work) the rows of the A matrix, the shared inner dimension, and the columns of the B matrix and have the answer dumped in to the C matrix.

Any thoughts? Figuring out how to correct for the column major order and the leading dimensions is difficult.

Thanks so much,

Here's what I have so far, and it only works for an NxN times and NxN matrix:


#include <stdio.h>
#include <stdlib.h>
#include <math.h>

float* alloc_float_1d(int ni){
  int i;
  float* a = (float*) malloc(ni*sizeof(float));
  return a;

int aind(int row, int col, int numcols)
  return (numcols*row)+col;

void aprint1d(float * A, int rows, int cols)
 for(int i=0;i<rows;i++)
      for(int j=0;j<cols;j++)
    printf("%1.4f ", A[aind(i,j,cols)]);

     Multiply 2 2d(float *) ARRAYS
void matmult(float * A, float * B, float * C, int arows, int idim, int bcols)

  //Perform the matrix multiplication
  sgemms(A, &arows, "N", B, &bcols, "N", C, &bcols, &arows, &bcols, &idim, 0, 0);

  float temp;
  for(int i=0;i<(int)floor(arows/2.0);i++)
      for(int j=0;j<bcols;j++)
  for(int i=0;i<(int)floor(arows/2.0);i++)
      for(int j=0;j<bcols;j++)

//             MAIN
void main()
  //Square Array size
  int  i, j;
  int arows=3;  
  int idim=3;
  int bcols=3;
  //Allocate the arrays
  float * A=alloc_float_1d(arows*idim);
  float * B=alloc_float_1d(idim*bcols);
  float * C=alloc_float_1d(arows*bcols);
  int c=1;
   for(i=0; i<arows; i++){
    for(j=0; j<idim; j++){
      A[aind(i,j,idim)] = c++;
  for(i=0; i<idim; i++){
    for(j=0; j<bcols; j++){
      B[aind(i,j,bcols)] = (idim*bcols+1)-c++;


RE: Getting dgemm or dgemms to work in C

Maybe I misunderstood your question. I don't know why you had trouble in this:

"Figuring out how to correct for the column major order and the leading dimensions is difficult".

The leading dimension is the input matrix's row dimension. You have full control over them. For example,


sgemms(a, lda, transa, b, ldb, transb, c, ldc, l, m, n, aux, naux);

to compute C = A * B.

A is an 8x6 matrix and B is 7x4 matrix. You want C = A x B be 6x4 matrix. You can specify l=6,m=6,n=4 as a set or l=6,m=4,n=4 as another. "m" specifies how many columns in A will be multiply-and-add in B's row. The reason of having varied "m" is because A and B can have different sizes.

I guess the reason that you only had square matrix working is l,m,n arguments specification I described above. The code seems ok.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close