Advertisement
Guest User

Untitled

a guest
Dec 18th, 2014
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.98 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. //#include <stdint.h>
  4.  
  5. // ---------------------------------------------------------------------------
  6. // allocate space for empty matrix A[row][col]
  7. // access to matrix elements possible with:
  8. // - A[row][col]
  9. // - A[0][row*col]
  10.  
  11.  
  12. float **alloc_mat(int row, int col)
  13. {
  14. float **A1, *A2;
  15.  
  16. A1 = (float **)calloc(row, sizeof(float *)); // pointer on rows
  17. A2 = (float *)calloc(row*col, sizeof(float)); // all matrix elements
  18.  
  19.  
  20. for (int i=0; i<row; i++)
  21. A1[i] = A2 + i*col;
  22.  
  23. return A1;
  24. }
  25.  
  26. // ---------------------------------------------------------------------------
  27. // random initialisation of matrix with values [0..9]
  28.  
  29. void init_mat(float **A, int row, int col)
  30. {
  31.  
  32. for (int i = 0; i < row*col; i++)
  33. A[0][i] = (float)(rand() % 10);
  34. }
  35.  
  36. // ---------------------------------------------------------------------------
  37. // DEBUG FUNCTION: printout of all matrix elements
  38.  
  39. void print_mat(float **A, int row, int col, char *tag)
  40. {
  41. int i, j;
  42.  
  43. printf("Matrix %s:n", tag);
  44. for (i = 0; i < row; i++)
  45. {
  46.  
  47. for (j=0; j<col; j++)
  48. printf("%6.1f ", A[i][j]);
  49. printf("n");
  50. }
  51. }
  52.  
  53. // ---------------------------------------------------------------------------
  54.  
  55. int main(int argc, char *argv[])
  56. {
  57. float **A, **B, **C; // matrices
  58. int d1, d2, d3; // dimensions of matrices
  59. int i, j, k; // loop variables
  60.  
  61.  
  62. double start, end;
  63. start = omp_get_wtime();
  64.  
  65. /* print user instruction */
  66. if (argc != 4)
  67. {
  68. printf ("Matrix multiplication: C = A x Bn");
  69. printf ("Usage: %s <NumRowA>; <NumColA> <NumColB>n",argv[0]);
  70. return 0;
  71. }
  72.  
  73. /* read user input */
  74. d1 = atoi(argv[1]); // rows of A and C
  75. d2 = atoi(argv[2]); // cols of A and rows of B
  76. d3 = atoi(argv[3]); // cols of B and C
  77.  
  78. printf("Matrix sizes C[%d][%d] = A[%d][%d] x B[%d][%d]n",
  79. d1, d3, d1, d2, d2, d3);
  80.  
  81. /* prepare matrices */
  82. A = alloc_mat(d1, d2);
  83. init_mat(A, d1, d2);
  84. B = alloc_mat(d2, d3);
  85. init_mat(B, d2, d3);
  86. C = alloc_mat(d1, d3); // no initialisation of C,
  87. //because it gets filled by matmult
  88.  
  89. /* serial version of matmult */
  90. printf("Perform matrix multiplication...n");
  91.  
  92.  
  93.  
  94. int sum;
  95. //#pragma omp parallel
  96. //{
  97. #pragma omp parallel for collapse(3) schedule(guided)
  98. for (i = 0; i < d1; i++)
  99. for (j = 0; j < d3; j++)
  100. for (k = 0; k < d2; k++){
  101. C[i][j] = C[i][j] + A[i][k] * B[k][j];
  102. }
  103. //}
  104.  
  105.  
  106. end = omp_get_wtime();
  107.  
  108.  
  109. /* test output */
  110. print_mat(A, d1, d2, "A");
  111. print_mat(B, d2, d3, "B");
  112. print_mat(C, d1, d3, "C");
  113.  
  114. printf("This task took %f secondsn", end-start);
  115. printf ("nDone.n");
  116.  
  117. return 0;
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement