1

I am a Fortran newbie. I attempted to write fortran code for matrix operations, where I was stuck in designing the prototype(interface) of my functions.

My C/C++ programming experience tells me that to write a code that may apply to matrices of all dimensions one needs to pass the array and dimensions separately to the function. Example(probably not the best) :

int * matx_op(int *mat_a, int arows, int acols, int *mat_b, int brows, int bcols);

But it seems that in fortran matmul() function does this automatically. Want to understand hows that done.

        print *, 'Enter 16 elements of matrix A'

        do i=1,4
            do j=1,4
                read *, ma(i,j)
            end do
        end do

        print *, 'Enter 16 elements of matrix B'

        do i=1,4
            do j=1,4
                read *, mb(i,j)
            end do
        end do

        mr = matmul(ma,mb)
8
  • 1
    Based on the features that the Fortran language provides, one deduces that Fortran arrays will be implemented in compilers as structures that contain the array and information about sizes of the dimensions. That is how intrinsic and user-written functions don't need explicit passing of dimensions. Commented Apr 8, 2015 at 18:21
  • @francescalus: I saw this before asking my question. Though to my dumb mind it appears as slightly different problem. The size() tells me the overall capacity of the array but not about its dimensions. Please explain a little more on this. Commented Apr 8, 2015 at 18:31
  • 1
    @RIPUNJAYTRIPATHI You can pass another argument to size() to get size in a single dimension. You can also use lbound(), ubound() and shape(). Be sure to study your Fortran manual gcc.gnu.org/onlinedocs/gfortran/SIZE.html#SIZE. Commented Apr 8, 2015 at 18:33
  • 1
    Yes, as above, size(..,dim=..) gives size along one dimension rather than the overall size. My motivation for suggesting the duplicate was the discussion around assumed shape (etc.). It's this latter facility that is the visible aspect of the passing. It may also be worth noting for other things that intrinsics needn't have a Fortran implementation. Commented Apr 8, 2015 at 18:38
  • 2
    software.intel.com/en-us/node/510871 describes how Intel Fortran implements what they call 'array descriptors' (like @VladimirF below I use the old-fashioned term 'dope vector'). The standard doesn't mandate this level of detail but other compilers will follow the same general approach. Commented Apr 8, 2015 at 18:48

1 Answer 1

2

Matmul is a generic procedure. The same name refers to several different specific procedures. You should know this from C++ as function name overloading. These specific procedures have different types, kinds (aka precisions) and ranks (aka dimensions) of their arguments.

The compiler has access to the interfaces ("headers") of those specific functions and chooses the correct one automatically.

You can also write your own generic procedures in Fortran.

Another option, is to use generic procedures for different types and kinds only and check for the rank inside of the procedure. If I understand the code in the libgfortran's matmull.m4 correctly, it is what actually happens.

Be aware that Fortran uses various mechanisms for passing array arguments, not just addresses as in C and C++. The procedure can receive an array descriptor (aka dope vector). The array rank and its shape are stored in the descriptor and are available to the library function.

Notice the following in the source

if (GFC_DESCRIPTOR_RANK (a) == 1)
{

}
else
{

}

the macro GFC_DESCRIPTOR_RANK() checks the relevant part of he array descriptor.

You can study the libgfortran implementations in matmul.m4 and matmull.m4. There are two versions, because the first one calls the optimized BLAS library for large matrices, whereas the other has its own implementation for smaller matrices.

Sign up to request clarification or add additional context in comments.

4 Comments

If I understand the meaning of rank, it denotes the number of dimensions of the array. But I am more interested in knowing the exact number of rows and columns of a two dimensional matrix. I will check the source code as well.
Seems I misunderstood your question. You were asking for the shape? The shape information is in the very same descriptor. It is available to you inside Fortran, that C source may actually confuse you. You should study the duplicate suggested by francescalus.
Actually, matmull.m4 is the version specialized for arguments of LOGICAL type. For all other primitive types matmul.m4 is used.
Ah, yeah, there were many versions I went through, for different architectures, until I found the right one. Then I didn't locate the generic non-BLAS code in the matmul.m4, but now I can see it. And after I closed the question I didn't care any more.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.