test2DMatrixMultiplication.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. function asmModule(stdlib, imports, buffer) {
  6. "use asm";
  7. var log = stdlib.Math.log;
  8. var toF = stdlib.Math.fround;
  9. var imul = stdlib.Math.imul;
  10. var i4 = stdlib.SIMD.Int32x4;
  11. var i4store = i4.store;
  12. var i4swizzle = i4.swizzle;
  13. var i4check = i4.check;
  14. var f4 = stdlib.SIMD.Float32x4;
  15. var f4equal = f4.equal;
  16. var f4lessThan = f4.lessThan;
  17. var f4splat = f4.splat;
  18. var f4store = f4.store;
  19. var f4load = f4.load;
  20. var f4check = f4.check;
  21. var f4abs = f4.abs;
  22. var f4add = f4.add;
  23. var f4sub = f4.sub;
  24. var Float32Heap = new stdlib.Float32Array(buffer);
  25. var Int32Heap = new stdlib.Int32Array(buffer);
  26. var BLOCK_SIZE = 4;
  27. function matrixMultiplication(aIndex, bIndex, cIndex) {
  28. aIndex = aIndex | 0;
  29. bIndex = bIndex | 0;
  30. cIndex = cIndex | 0;
  31. var i = 0, j = 0, dim1 = 0, dim2 = 0, intersectionNum = 0, matrixSize = 0;
  32. var newPiece = f4(0.0, 0.0, 0.0, 0.0), cPiece = f4(0.0, 0.0, 0.0, 0.0);
  33. //array dimensions don't match
  34. if ((Int32Heap[aIndex + 1 << 2 >> 2] | 0) != (Int32Heap[bIndex << 2 >> 2] | 0)) {
  35. return -1;
  36. }
  37. dim1 = Int32Heap[aIndex << 2 >> 2] | 0;
  38. dim2 = Int32Heap[bIndex + 1 << 2 >> 2] | 0;
  39. intersectionNum = Int32Heap[bIndex << 2 >> 2] | 0;
  40. matrixSize = imul(dim1, dim2);
  41. Int32Heap[cIndex << 2 >> 2] = dim1;
  42. Int32Heap[cIndex + 1 << 2 >> 2] = dim2;
  43. while ((i|0) < (matrixSize|0)) {
  44. cPiece = f4(0.0, 0.0, 0.0, 0.0);
  45. j = 0;
  46. while ((j|0) < (intersectionNum|0)) {
  47. newPiece = f4(toF(getIntersectionPiece(aIndex, bIndex, dim2, i, 0, j)),
  48. toF(getIntersectionPiece(aIndex, bIndex, dim2, i, 1, j)),
  49. toF(getIntersectionPiece(aIndex, bIndex, dim2, i, 2, j)),
  50. toF(getIntersectionPiece(aIndex, bIndex, dim2, i, 3, j)));
  51. cPiece = f4add(cPiece, newPiece);
  52. j = (j + 1)|0;
  53. }
  54. f4store(Float32Heap, cIndex + 2 + i << 2 >> 2, cPiece);
  55. i = (i + BLOCK_SIZE)|0;
  56. }
  57. return 0;
  58. }
  59. function getIntersectionPiece(aIndex, bIndex, dim2, resultBlock, resultIndex, intersectionNum) {
  60. aIndex = aIndex | 0;
  61. bIndex = bIndex | 0;
  62. dim2 = dim2 | 0;
  63. resultBlock = resultBlock | 0;
  64. resultIndex = resultIndex | 0;
  65. intersectionNum = intersectionNum | 0;
  66. var aElem = 0.0, bElem = 0.0, cElem = 0.0;
  67. aElem = +toF(getElement(aIndex, ((resultBlock | 0) / (dim2 | 0))|0, intersectionNum));
  68. bElem = +toF(getElement(bIndex, intersectionNum, (resultBlock + resultIndex)|0));
  69. //return toF(getElement(aIndex, ((resultBlock|0) / (dim2|0)), intersectionNum));
  70. return toF(aElem * bElem);
  71. }
  72. function getElement(start, row, column) {
  73. start = start | 0;
  74. row = row | 0;
  75. column = column | 0;
  76. var dim1 = 0, dim2 = 0;
  77. dim2 = Int32Heap[start << 2 >> 2] | 0;
  78. dim1 = Int32Heap[start + 1 << 2 >> 2] | 0;
  79. //return toF(Float32Heap[(602 + imul(row, dim1) + column) << 2 >> 2]);
  80. return toF(Float32Heap[(start + 2 + imul(row, dim1) + column) << 2 >> 2]);
  81. }
  82. function new2DMatrix(startIndex, dim1, dim2) {
  83. startIndex = startIndex | 0;
  84. dim1 = dim1 | 0;
  85. dim2 = dim2 | 0;
  86. var i = 0, matrixSize = 0;
  87. matrixSize = imul(dim1, dim2);
  88. Int32Heap[startIndex << 2 >> 2] = dim1;
  89. Int32Heap[startIndex + 1 << 2 >> 2] = dim2;
  90. for (i = 0; (i|0) < ((matrixSize - BLOCK_SIZE)|0); i = (i + BLOCK_SIZE)|0) {
  91. f4store(Float32Heap, startIndex + 2 + i << 2 >> 2, f4(toF((i + 1)|0), toF((i + 2)|0), toF((i + 3)|0), toF((i + 4)|0)));
  92. }
  93. for (; (i|0) < (matrixSize|0); i = (i + 1)|0) {
  94. Float32Heap[(startIndex + 2 + i) << 2 >> 2] = toF((i + 1)|0);
  95. }
  96. return (startIndex + 2 + i) | 0;
  97. }
  98. return {
  99. new2DMatrix: new2DMatrix,
  100. matrixMultiplication: matrixMultiplication
  101. };
  102. }
  103. function print2DMatrix(buffer, start) {
  104. var IntHeap32 = new Int32Array(buffer);
  105. var FloatHeap32 = new Float32Array(buffer);
  106. var f4;
  107. var dim1 = IntHeap32[start];
  108. var dim2 = IntHeap32[start + 1];
  109. print(dim1 + " by " + dim2 + " matrix");
  110. for (var i = 0; i < Math.imul(dim1, dim2) ; i += 4) {
  111. f4 = SIMD.Float32x4.load(FloatHeap32, i + start + 2);
  112. print(f4.toString());
  113. }
  114. }
  115. var buffer = new ArrayBuffer(16 * 1024 * 1024);
  116. var m = asmModule(this, null, buffer);
  117. print("2D Matrix Multiplication");
  118. m.new2DMatrix(0, 4, 8);
  119. m.new2DMatrix(200, 8, 12);
  120. m.new2DMatrix(400, 4, 4);
  121. m.new2DMatrix(600, 4, 4);
  122. m.matrixMultiplication(0, 200, 800);
  123. m.matrixMultiplication(400, 600, 1000);
  124. print2DMatrix(buffer, 800);
  125. print2DMatrix(buffer, 1000);