Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2020
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.54 KB | None | 0 0
  1. In Makefile:
  2.  
  3. CC = gcc
  4. CFLAGS = -O4 -Wall -mavx2 -I $(TIMER_DIR)
  5. TIMER_DIR = /home/class_projects/cycle-counter
  6. TIMER = $(TIMER_DIR)/clock.o $(TIMER_DIR)/fcycmm.o
  7.  
  8. all: mm
  9.  
  10. mm: mm.c mm.h
  11. $(CC) $(CFLAGS) -o mm mm.c $(TIMER)
  12.  
  13. clean:
  14. rm -f *.o mm *~
  15.  
  16. In mm.c:
  17.  
  18. // HEADER HERE :)
  19.  
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include "mm.h"
  23. #include "fcycmm.h"
  24. #include "clock.h"
  25.  
  26. /* whether or not fcyc should clear the cache */
  27. #define CLEARCACHE 1
  28.  
  29. /* global arrays [ ok for this kind of hacking, but not in production ] */
  30. array ga, gb, gc;
  31.  
  32. /* purpose: check the result array for correctness
  33. * input: the product array, c, and its size, n
  34. * returns: nothing (complains in an error message if result is wrong)
  35. */
  36. void checkresult(array c, int n)
  37. {
  38. int i, j;
  39.  
  40. for (i = 0; i < n; i++)
  41. for (j = 0; j < n; j++)
  42. if (c[i][j] != (double)n)
  43. {
  44. printf("Error: bad number (%f) in result matrix (%d,%d)\n",
  45. c[i][j], i, j);
  46. fflush(stdout);
  47. exit(0);
  48. }
  49. }
  50.  
  51.  
  52. /* purpose: Run function f and return clocks per inner loop iteration
  53. * input: a function, f, and the matrix size, n
  54. * returns: the number of cycles taken per inner loop iteration
  55. */
  56. double run(test_funct f, int n)
  57. {
  58. double cpi;
  59.  
  60. cpi = fcyc(f, n, CLEARCACHE) / (n*n*n);
  61. checkresult(gc, n);
  62. return cpi;
  63. }
  64.  
  65.  
  66. /* purpose: reset result array to zero
  67. * input: the array, c, and its size, n
  68. * returns: the array updated to all zeros.
  69. */
  70. void reset(array c, int n)
  71. {
  72. int i,j;
  73.  
  74. for (i = 0; i < n; i++)
  75. for (j = 0; j < n; j++)
  76. c[i][j] = 0.0;
  77. }
  78.  
  79.  
  80. /* purpose: initialize input arrays to 1
  81. * input: the input arrays, a and b, and their size, n
  82. * returns: the arrays updated to all ones.
  83. */
  84. void init(array a, array b, int n)
  85. {
  86. int i,j;
  87.  
  88. for (i = 0; i < n; i++)
  89. {
  90. for (j = 0; j < n; j++)
  91. {
  92. a[i][j] = 1.0;
  93. b[i][j] = 1.0;
  94. }
  95. }
  96. }
  97.  
  98.  
  99. /* purpose: print an array
  100. * input: an array, a, and its size, n
  101. * returns: nothing (printing is a side effect)
  102. */
  103. void printarray(array a, int n)
  104. {
  105. int i, j;
  106.  
  107. for (i = 0; i < n; i++)
  108. {
  109. for (j = 0; j < n; j++)
  110. printf("%5.1f ", a[i][j]);
  111.  
  112. printf("\n");
  113. }
  114. }
  115.  
  116.  
  117. /* purpose:
  118. * input:
  119. * returns:
  120. */
  121. void multiply(array A, array B, array C, int n)
  122. {
  123. // ...
  124. }
  125.  
  126.  
  127. /* purpose: test the multiplcatoin of two 2x2 matricies
  128. * input: nothing (the test is hard coded)
  129. * returns: nothing (prints result to standard out)
  130. */
  131. void test()
  132. {
  133. ga[0][0] = 1;
  134. ga[0][1] = 2;
  135. ga[1][0] = 3;
  136. ga[1][1] = 4;
  137. gb[0][0] = 5;
  138. gb[0][1] = 6;
  139. gb[1][0] = 7;
  140. gb[1][1] = 8;
  141. printf("product of \n");
  142. printarray(ga, 2);
  143. printf("and\n");
  144. printarray(gb, 2);
  145. multiply(ga, gb, gc, 2);
  146. printf("=\n");
  147. printarray(gc, 2);
  148.  
  149. if (gc[0][0] == 19 && gc[0][1] == 22 && gc[1][0] == 43 && gc[1][1] == 50)
  150. printf("Passed!!\n");
  151. else
  152. printf("Fail!!\n");
  153. }
  154.  
  155.  
  156.  
  157. /*
  158. * Run matrix multiply and display performance
  159. * as clock cycles per inner loop iteration.
  160. */
  161. int main()
  162. {
  163. int n;
  164.  
  165. init(ga, gb, MAXN);
  166. // test();
  167. // exit(0);
  168.  
  169. printf("matmult cycles/loop iteration\n");
  170. fflush(stdout);
  171. for (n = MINN; n <= MAXN; n += INCN)
  172. {
  173. printf("%3d ", n);
  174.  
  175. printf("%9.2f ", run(multiply, n));
  176. printf("\n");
  177. fflush(stdout);
  178. }
  179.  
  180. exit(0);
  181. }
  182.  
  183. In mm.h:
  184.  
  185. #define MINN 50
  186. #define MAXN 700
  187. #define INCN 50
  188.  
  189. typedef double array[MAXN][MAXN+513];
  190.  
  191. #define min(x,y) (x<y?x:y)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement