Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Himawan Go
- * 4943425
- * Section 006
- *
- * trans.c - Matrix transpose B = A^T
- *
- * Each transpose function must have a prototype of the form:
- * void trans(int M, int N, int A[N][M], int B[M][N]);
- *
- * A transpose function is evaluated by counting the number of misses
- * on a 1KB direct mapped cache with a block size of 32 bytes.
- */
- #include <stdio.h>
- #include "cachelab.h"
- int is_transpose(int M, int N, int A[N][M], int B[M][N]);
- /*
- * transpose_submit - This is the solution transpose function that you
- * will be graded on for Part B of the assignment. Do not change
- * the description string "Transpose submission", as the driver
- * searches for that string to identify the transpose function to
- * be graded.
- */
- char transpose_submit_desc[] = "Transpose submission";
- void transpose_submit(int M, int N, int A[N][M], int B[M][N])
- {
- // Temporary variables
- int temp1, temp2, temp3, temp4, temp5, temp6, temp7, temp8;
- // Variables for iterations
- int row, column, i, j;
- // For 64 x 64 matrix
- if(M == 64 && N == 64){
- // Block size of 8
- for (row = 0; row < 64; row += 8) {
- for (column = 0; column < 64; column += 8) {
- // In case of diagonal
- if (row == column) {
- // Block size of 4
- for (i = 0; i < 8; i += 4) {
- for (j = 0; j < 8; j += 4) {
- // Save values of upper half of the block
- temp1 = A[row + i][column + j];
- temp2 = A[row + i][column + j + 1];
- temp3 = A[row + i][column + j + 2];
- temp4 = A[row + i][column + j + 3];
- temp5 = A[row + i + 1][column + j];
- temp6 = A[row + i + 1][column + j + 1];
- temp7 = A[row + i + 1][column + j + 2];
- temp8 = A[row + i + 1][column + j + 3];
- // Transposing upper half of the block
- B[column + j][row + i] = temp1;
- B[column + j][row + i + 1] = temp5;
- B[column + j + 1][row + i] = temp2;
- B[column + j + 1][row + i + 1] = temp6;
- B[column + j + 2][row + i] = temp3;
- B[column + j + 2][row + i + 1] = temp7;
- B[column + j + 3][row + i] = temp4;
- B[column + j + 3][row + i + 1] = temp8;
- // Save values of bottom half of the block
- temp1 = A[row + i + 2][column + j ];
- temp2 = A[row + i + 2][column + j + 1];
- temp3 = A[row + i + 2][column + j + 2];
- temp4 = A[row + i + 2][column + j + 3];
- temp5 = A[row + i + 3][column + j];
- temp6 = A[row + i + 3][column + j + 1];
- temp7 = A[row + i + 3][column + j + 2];
- temp8 = A[row + i + 3][column + j + 3];
- // Transposing bottom half of the block
- B[column + j][row + i + 2] = temp1;
- B[column + j][row + i + 3] = temp5;
- B[column + j + 1][row + i + 2] = temp2;
- B[column + j + 1][row + i + 3] = temp6;
- B[column + j + 2][row + i + 2] = temp3;
- B[column + j + 2][row + i + 3] = temp7;
- B[column + j + 3][row + i + 2] = temp4;
- B[column + j + 3][row + i + 3] = temp8;
- }
- }
- }
- else {
- // Transposing Row 1 Column 1 to Row 4 Column 4
- for (i = 0; i < 4; i++){
- for (j = 0; j < 4; j++){
- B[column + j][row + i] = A[row + i][column + j];
- }
- }
- // Save values of Row 1 Column 5 to Row 2 Column 8
- temp1 = A[row][column + 4];
- temp2 = A[row][column + 5];
- temp3 = A[row][column + 6];
- temp4 = A[row][column + 7];
- temp5 = A[row + 1][column + 4];
- temp6 = A[row + 1][column + 5];
- temp7 = A[row + 1][column + 6];
- temp8 = A[row + 1][column + 7];
- // Transposing Row 5 Column 1 to Row 8 Column 4
- for (i = 4; i < 8; i++){
- for (j = 0; j < 4; j++){
- B[column + j][row + i] = A[row + i][column + j];
- }
- }
- // Transposing Row 5 Column 5 to Row 8 Column 8
- for (i = 4; i < 8; i++){
- for (j = 4; j < 8; j++){
- B[column + j][row + i] = A[row + i][column + j];
- }
- }
- // Transposing Row 1 Column 5 to Row 2 Column 8
- B[column + 4][row] = temp1;
- B[column + 5][row] = temp2;
- B[column + 6][row] = temp3;
- B[column + 7][row] = temp4;
- B[column + 4][row + 1] = temp5;
- B[column + 5][row + 1] = temp6;
- B[column + 6][row + 1] = temp7;
- B[column + 7][row + 1] = temp8;
- // Transposing Row 3 Column 5 to Row 4 Column 8
- for (i = 2; i < 4; i++){
- for (j = 4; j < 8; j++){
- B[column + j][row + i] = A[row + i][column + j];
- }
- }
- }
- }
- }
- }
- // For 61 x 67 matrix
- else if(M == 61 && N == 67){
- // Block size of 14
- for(column = 0; column < N ; column += 14){
- for(row = 0; row < M; row += 14){
- for(i = column; i < column + 14; i++){
- for(j = row; j < row + 14; j++){
- // Limit operation to the size of matrix
- if(i < 67 && j < 61){
- B[j][i] = A[i][j];
- }
- }
- }
- }
- }
- }
- }
- /*
- * You can define additional transpose functions below. We've defined
- * a simple one below to help you get started.
- */
- /*
- * trans - A simple baseline transpose function, not optimized for the cache.
- */
- char trans_desc[] = "Simple row-wise scan transpose";
- void trans(int M, int N, int A[N][M], int B[M][N])
- {
- int i, j, tmp;
- for (i = 0; i < N; i++) {
- for (j = 0; j < M; j++) {
- tmp = A[i][j];
- B[j][i] = tmp;
- }
- }
- }
- /*
- * registerFunctions - This function registers your transpose
- * functions with the driver. At runtime, the driver will
- * evaluate each of the registered functions and summarize their
- * performance. This is a handy way to experiment with different
- * transpose strategies.
- */
- void registerFunctions()
- {
- /* Register your solution function */
- registerTransFunction(transpose_submit, transpose_submit_desc);
- /* Register any additional transpose functions */
- registerTransFunction(trans, trans_desc);
- }
- /*
- * is_transpose - This helper function checks if B is the transpose of
- * A. You can check the correctness of your transpose by calling
- * it before returning from the transpose function.
- */
- int is_transpose(int M, int N, int A[N][M], int B[M][N])
- {
- int i, j;
- for (i = 0; i < N; i++) {
- for (j = 0; j < M; ++j) {
- if (A[i][j] != B[j][i]) {
- return 0;
- }
- }
- }
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement