Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
28543 Discussions

How do I tell the iFortran compiler to check the dimensions of two matrices before multiplying them?

divsigma
Beginner
519 Views

I have learned that when you use matmul to multiply two matrices A and B as A*B, there is no restriction on the dimensions of the matrices. I want to admiss only matrices which fulfill the classical matrix multiplication criteria

no. of columns in A = no. of rows in B

If this criteria is violated, I want the compiler to throw a warning. How do I achieve this?

I'm using iFortan 18.0.1.

0 Kudos
3 Replies
Arjen_Markus
Honored Contributor I
512 Views

That is impossible to check at compile time: you can pass two matrices to a general routine and in that routine you multiply the matrices. How would you require the compiler to check that at that point the two matrices have the right sizes? The routine can be compiled independently and it is the call to that routine where the check has to be made.

What you can do alternatively is:

  • Create a derived type that holds the matrix
  • Define a matmul routine for specifically this derived type

That way you guarantee that only arguments of the mutually right size can be passed. You might try to make this a bit more general via parametrised types, but soon you will lose the strictness you require at compile time.

andrew_4619
Honored Contributor III
505 Views

Well there are also intrinsic functions RANK, SHAPE and SIZE the can tell you all you need to know about and array if you need to so you could so some checks on the arrays at runtime before calling matmul.

0 Kudos
Steve_Lionel
Honored Contributor III
478 Views

The standard places this restriction on the programmer, saying, "The size of the first (or only) dimension of MATRIX_B shall equal the size of the last (or only) dimension of MATRIX_A."; the compiler is not required to have the ability to check this. It might be a nice thing to add in the future under /check:shape, but traditionally the Intel compiler doesn't do that level of additional checking.

0 Kudos
Reply