这是一个本人学习 csapp 的 learning 库

84 linhas
2.3 KiB

  1. /*
  2. * trans.c - Matrix transpose B = A^T
  3. *
  4. * Each transpose function must have a prototype of the form:
  5. * void trans(int M, int N, int A[N][M], int B[M][N]);
  6. *
  7. * A transpose function is evaluated by counting the number of misses
  8. * on a 1KB direct mapped cache with a block size of 32 bytes.
  9. */
  10. #include <stdio.h>
  11. #include "cachelab.h"
  12. int is_transpose(int M, int N, int A[N][M], int B[M][N]);
  13. /*
  14. * transpose_submit - This is the solution transpose function that you
  15. * will be graded on for Part B of the assignment. Do not change
  16. * the description string "Transpose submission", as the driver
  17. * searches for that string to identify the transpose function to
  18. * be graded.
  19. */
  20. char transpose_submit_desc[] = "Transpose submission";
  21. void transpose_submit(int M, int N, int A[N][M], int B[M][N])
  22. {
  23. }
  24. /*
  25. * You can define additional transpose functions below. We've defined
  26. * a simple one below to help you get started.
  27. */
  28. /*
  29. * trans - A simple baseline transpose function, not optimized for the cache.
  30. */
  31. char trans_desc[] = "Simple row-wise scan transpose";
  32. void trans(int M, int N, int A[N][M], int B[M][N])
  33. {
  34. int i, j, tmp;
  35. for (i = 0; i < N; i++) {
  36. for (j = 0; j < M; j++) {
  37. tmp = A[i][j];
  38. B[j][i] = tmp;
  39. }
  40. }
  41. }
  42. /*
  43. * registerFunctions - This function registers your transpose
  44. * functions with the driver. At runtime, the driver will
  45. * evaluate each of the registered functions and summarize their
  46. * performance. This is a handy way to experiment with different
  47. * transpose strategies.
  48. */
  49. void registerFunctions()
  50. {
  51. /* Register your solution function */
  52. registerTransFunction(transpose_submit, transpose_submit_desc);
  53. /* Register any additional transpose functions */
  54. registerTransFunction(trans, trans_desc);
  55. }
  56. /*
  57. * is_transpose - This helper function checks if B is the transpose of
  58. * A. You can check the correctness of your transpose by calling
  59. * it before returning from the transpose function.
  60. */
  61. int is_transpose(int M, int N, int A[N][M], int B[M][N])
  62. {
  63. int i, j;
  64. for (i = 0; i < N; i++) {
  65. for (j = 0; j < M; ++j) {
  66. if (A[i][j] != B[j][i]) {
  67. return 0;
  68. }
  69. }
  70. }
  71. return 1;
  72. }