A Cache Efficient Matrix Transpose Program?

前端 未结 6 655
無奈伤痛
無奈伤痛 2020-11-30 20:19

So the obvious way to transpose a matrix is to use :

  for( int i = 0; i < n; i++ )

    for( int j = 0; j < n; j++ )

      destination[j+i*n] = sourc         


        
6条回答
  •  北荒
    北荒 (楼主)
    2020-11-30 20:37

    Steve Jessop mentioned a cache oblivious matrix transpose algorithm. For the record, I want to share an possible implementation of a cache oblivious matrix transpose.

    public class Matrix {
        protected double data[];
        protected int rows, columns;
    
        public Matrix(int rows, int columns) {
            this.rows = rows;
            this.columns = columns;
            this.data = new double[rows * columns];
        }
    
        public Matrix transpose() {
            Matrix C = new Matrix(columns, rows);
            cachetranspose(0, rows, 0, columns, C);
            return C;
        }
    
        public void cachetranspose(int rb, int re, int cb, int ce, Matrix T) {
            int r = re - rb, c = ce - cb;
            if (r <= 16 && c <= 16) {
                for (int i = rb; i < re; i++) {
                    for (int j = cb; j < ce; j++) {
                        T.data[j * rows + i] = data[i * columns + j];
                    }
                }
            } else if (r >= c) {
                cachetranspose(rb, rb + (r / 2), cb, ce, T);
                cachetranspose(rb + (r / 2), re, cb, ce, T);
            } else {
                cachetranspose(rb, re, cb, cb + (c / 2), T);
                cachetranspose(rb, re, cb + (c / 2), ce, T);
            }
        }
    }
    

    More details on cache oblivious algorithms can be found here.

提交回复
热议问题