Fortran What is the most optimized way to transpose a matrix in Fortran?

Click For Summary
The discussion centers on optimizing the transposition of a large matrix in Fortran 77. The initial approach involves nested loops to swap elements, which is effective for in-place transposition but may not be the most efficient method. An alternative suggested is to use BLAS (Basic Linear Algebra Subprograms) for potentially better performance, although specific implementation details were not provided. It is noted that the current method is optimal in terms of memory usage since it only modifies the upper triangular part of the matrix while leaving the diagonal unchanged. If memory constraints are not an issue, creating a separate transposed matrix is recommended, which simplifies the operation to a single assignment per element. Additionally, a suggestion is made to implement logic that allows the program to treat the matrix as transposed without physically transposing it, thereby improving efficiency in matrix multiplication operations. This approach could significantly reduce computational complexity from O(n^2) to O(1) in certain scenarios, enhancing performance for large matrices.
d4n1el
Messages
2
Reaction score
1
Hi!
I'm working on a programming project(fortran 77).
and I need to transpose a big matrix, and for the moment I'm doing it by to do-loops:

DO 20 J = 2,NP
DO 10 I = 1,J-1
T = P(I,J)
P(I,J) = P(J,I)
P(J,I) = T
10 CONTINUE
20 CONTINUE

But is this the most optimized way? is it any way you could do it by using for example BLAS?
I've been googleing, but could't find out how to do it..

/Daniel
 
  • Like
Likes M Naeem Tahir
Technology news on Phys.org
d4n1el said:
Hi!
I'm working on a programming project(fortran 77).
and I need to transpose a big matrix, and for the moment I'm doing it by to do-loops:

DO 20 J = 2,NP
DO 10 I = 1,J-1
T = P(I,J)
P(I,J) = P(J,I)
P(J,I) = T
10 CONTINUE
20 CONTINUE

But is this the most optimized way? is it any way you could do it by using for example BLAS?
I've been googleing, but could't find out how to do it..

/Daniel
It looks like one is marching by column then rows on the top triangular array. Then the above code exchanges top triangle element with the diagonally symmetric member and leaves the diagonal alone, since the diagonal does not change. That is the optimal way if memory (size) is constrained. On has to calculate the upper limit of I as J-1 which is correct.

If memory is not a problem, then one could simply copy and transpose the entire matrix A to a new one AT (A transpose), which preserves A, i.e. AT(J,I) = A(I,J), and still use two DO loops. There is just the one = operation.

Software optimization for high-performance computing
 
Just a suggestion... but you could probably use logic to indicate whether the matrix is transposed, and then branch when you use the matrix. For instance...

M : large matrix, N x M array of integers
T : true if M is to be interpreted as being transposed, false otherwise.

Then (provided you don't need to multiply M by transpose M) you could do this...

MatrixMultiply(A, false, B, false, C, false); // multiplies A by B and put it into C
MatrixMultiply(A, true, D, false, E, false); // take the transpose of A, multiply by D, and put it into E.

You see what I'm saying? It may complicate the functions themselves (but not by too much) and you won't have to actually take the transpose of anything.

And O(1) is a lot faster than O(n^2). Will this work for you?
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 4 ·
Replies
4
Views
8K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 20 ·
Replies
20
Views
4K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 15 ·
Replies
15
Views
5K
  • · Replies 22 ·
Replies
22
Views
5K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 5 ·
Replies
5
Views
5K