Guest User

Untitled

a guest
Feb 17th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.95 KB | None | 0 0
  1. const int blockSize = 12;
  2.  
  3. void readBlock(double outputMatrix[], double inputMatrix[], int beginX, int endX, int beginY, int endY, int n) {
  4.  
  5. int i = 0;
  6. for (int x = beginX; x < endX; x++) {
  7. for (int y = beginY; y < endY; y++) {
  8. outputMatrix[i] = inputMatrix[x*n + y];
  9. i++;
  10. }
  11. for (int y = endY-beginY; y < blockSize; y++) {
  12. outputMatrix[i] = 0;
  13. i++;
  14. }
  15.  
  16.  
  17. }
  18.  
  19. for (int x = endX - beginX; x < blockSize; x++) {
  20. for (int y = 0; y < blockSize; y++) {
  21. outputMatrix[i] = 0;
  22. i++;
  23. }
  24.  
  25. }
  26.  
  27. }
  28.  
  29.  
  30. int min(int a, int b) {
  31. if (a < b) return a;
  32. else return b;
  33.  
  34. }
  35.  
  36. void multiplyMatrices(double inputMatrix1[], double inputMatrix2[], double outputMatrix[], int endx, int endy) {
  37. for (int i = 0; i < endx; i++) {
  38. for (int j = 0; j < endy; j++) {
  39. double cvalue = outputMatrix[i*blockSize + j];
  40. for (int l = 0; l < blockSize; l+=2) {
  41. double a1 = inputMatrix1[l*blockSize + j] * inputMatrix2[i*blockSize + l];;
  42. double a2 = inputMatrix1[(l+1)*blockSize + j] * inputMatrix2[i*blockSize + l + 1];
  43. cvalue += a1 + a2;
  44.  
  45. }
  46. outputMatrix[i*blockSize + j] = cvalue;
  47. }
  48. }
  49.  
  50. }
  51.  
  52.  
  53. void writeMatrix(double* inputMatrix, double* outputMatrix, int beginX, int endX, int beginY, int endY, int n) {
  54. int k,l=0;
  55. for (int i = beginX; i < endX; i++) {
  56. k = 0;
  57. for (int j = beginY; j < endY; j++) {
  58. outputMatrix[i*n + j] = inputMatrix[l*blockSize+k];
  59. k++;
  60. }
  61. l++;
  62. }
  63.  
  64. }
  65.  
  66.  
  67. void square_dgemm(int n, double* A, double* B, double* C) {
  68.  
  69.  
  70.  
  71. for (int x = 0; x < n; x += blockSize) {
  72. for (int y = 0; y < n; y += blockSize) {
  73. double CC[blockSize*blockSize];
  74.  
  75. readBlock(CC, C, x, min(x + blockSize, n), y, min(y + blockSize, n), n);
  76.  
  77. for (int z = 0; z < n; z += blockSize) {
  78. double BC[blockSize*blockSize];
  79. double AC[blockSize*blockSize];
  80. int zAdj = min(z + blockSize, n);
  81. readBlock(AC, A, z, zAdj,y, min(y+blockSize, n), n);
  82.  
  83. readBlock(BC, B, x, min(x + blockSize, n), z,zAdj, n);
  84.  
  85. multiplyMatrices(AC, BC, CC, min(x+blockSize,n)-x,min(y+blockSize,n)-y);
  86.  
  87. }
  88. writeMatrix(CC, C, x, min(x + blockSize, n), y, min(y + blockSize, n), n);
  89.  
  90.  
  91.  
  92.  
  93. }
  94.  
  95. }
  96. }
Add Comment
Please, Sign In to add comment