Transforming Matrix Operations: Row-Major to Column-Major

  • Thread starter swartzism
  • Start date
In summary, there is no straightforward way to switch matrix operations from row-major to column-major. However, doing so can be done by taking into account the dimensions of betaP and betaM.
  • #1
swartzism
103
0
Is there a straightforward way to switch matrix operations from row-major to column-major? From wikipedia,
Treating a row-major array as a column-major array is the same as transposing it. Because performing a transpose requires data movement, and is quite difficult to do in-place for non-square matrices, such transpositions are rarely performed explicitly. For example, software libraries for linear algebra, such as the BLAS, typically provide options to specify that certain matrices are to be interpreted in transposed order to avoid the necessity of data movement.
But applying this to the nested do-loop I am working with isn't so straightforward.

Code:
! ncol = 666
! nmu = 18
! ncomp = 4
 
      do 120 j=1,ncol
         do 130 i=1,nmu
            temppp = 0.
            temppm = 0.
            do 140 k=1,ncomp
               temppp = temppp + (bcomp(k)/btotal)*betatP(i,j,k)
               temppm = temppm + (bcomp(k)/btotal)*betatM(i,j,k)
 140        continue
            totlpp(i,j) = temppp
            totlpm(i,j) = temppm
 130     continue
 120  continue

betaP and betaM should have dimensions (k,i,j) for this to be column-major, but I'm not sure how to go about changing the computations to do so.

Any ideas?
 
Technology news on Phys.org
  • #2
Hhhmmm, I have never concerned myself with these kind of things, my data is not that large and computers are fast enough, by now.

Is your data that large? Are you really concerned about speed? performance? Because making sure that the fastest varying index takes you from one memory address to the adjacent one, it's all that will be achieved if you manage to line up your memory and your calculations.

How did betaP and betaM come about? Can you make the switch back there in the first place? Then you can come back to these nested loops and do the right thing.
 
Last edited:
  • #3
Is this the kind of thing you are looking for?
Code:
totlpp=0.0  ! array operation, I presume you are using Fortran 90
totlpm=0.0  !
do k = 1, ncomp
   coeff = bcomp(k)/btotal
   do j = 1, ncol
      do i = 1, nmu
         totlpp(i,j) = totlpp(i,j) + coeff*betatP(i,j,k)
         totlpm(i,j) = totlpm(i,j) + coeff*betatM(i,j,k)
      end do
   end do
end do
I don't have a fortran compiler at the moment and have not tested whether this produces the same results or not or whether it is what you are looking for or not...I still am not quite sure what you want to achieve...
 

FAQ: Transforming Matrix Operations: Row-Major to Column-Major

What is the difference between row-major and column-major order?

Row-major and column-major are two different ways of organizing data in a multi-dimensional array. In row-major order, the elements of the array are stored sequentially by rows, while in column-major order, the elements are stored sequentially by columns.

Why is it important to understand row-major and column-major order?

Understanding row-major and column-major order is important for efficient data access and manipulation. Different programming languages and applications may use different conventions, and knowing how data is stored can help with optimizing performance.

How do I convert from row-major to column-major order?

To convert from row-major to column-major order, you need to rearrange the elements of the array. For a 2D array, you can simply swap the rows and columns. For higher dimensional arrays, you may need to use nested loops to iterate through the elements and rearrange them.

Is there a preferred order for storing data, row-major or column-major?

There is no universally preferred order for storing data. It often depends on the specific application or programming language. Some languages, like Fortran, use column-major order by default, while others, like C, use row-major order. It's important to know which order is used in your particular case.

Can row-major and column-major order be used interchangeably?

No, row-major and column-major order cannot be used interchangeably. The order in which data is stored affects how it is accessed and manipulated. Using the wrong order can lead to incorrect results or poor performance.

Back
Top