Advertisement
pai

CUDA MATRIX MULTIPLICATION PROGRAM

pai
Jun 30th, 2011
514
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.54 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #define SIZE 3
  4. int matrixA[SIZE][SIZE],matrixB[SIZE][SIZE],matrixC[SIZE][SIZE];
  5.  
  6. __global__ void matrix_mult(int *dA,int *dB,int *dC,int dim)
  7. {
  8. // int e1,e2;
  9. int i=threadIdx.x;
  10. int j=threadIdx.y;
  11. int product=0,k;
  12. for(k=0;k<dim;k++)
  13. {
  14. product=product+dA[i*dim+k]*dB[k*dim+j];
  15. }
  16. dC[i*dim+j]=product;
  17.  
  18. }
  19.  
  20. void read_matrix_from_file(FILE *fp,int m,int n,int matrix[SIZE][SIZE])
  21. {
  22. int i,j;
  23. for(i=0;i<m;i++)
  24. {
  25. for(j=0;j<n;j++)
  26. {
  27. fscanf(fp,"%d",&matrix[i][j]);
  28. }
  29. }
  30. }
  31.  
  32. void print_matrix(int matrix[SIZE][SIZE],int m,int n)
  33. {
  34. int i,j;
  35. for(i=0;i<n;i++)
  36. {
  37. for(j=0;j<m;j++)
  38. {
  39. printf("%d ",matrix[i][j]);
  40. }
  41. printf("\n");
  42. }
  43. }
  44. int main(int argc,char *argv[])
  45. {
  46. FILE *fp1=NULL,*fp2=NULL;
  47. int m,n,o,p,size;
  48. int *dA=NULL,*dB=NULL,*dC=NULL;
  49. enum cudaError error;
  50. if(argc!=7)
  51. {
  52. printf("\n(7 Parameters)./a.out matrixfile1 m n matrixfile2 o p");
  53. exit(0);
  54. }
  55. else
  56. {
  57. fp1=fopen(argv[1],"r+");
  58. fp2=fopen(argv[4],"r+");
  59. if(fp1==NULL||fp2==NULL)
  60. {
  61. printf("\nError in opening the file");
  62. exit(0);
  63. }
  64. else
  65. {
  66. m=atoi(argv[2]);
  67. n=atoi(argv[3]);
  68. o=atoi(argv[5]);
  69. p=atoi(argv[6]);
  70. size=sizeof(int)*m*m;
  71. // Reading the matrix from file
  72. read_matrix_from_file(fp1,m,n,matrixA);
  73. read_matrix_from_file(fp2,o,p,matrixB);
  74. //Print the matrix on the console
  75. printf("\n\nMatrixA is:\n");
  76. print_matrix(matrixA,m,n);
  77. printf("\n\nMatrixB is:\n");
  78. print_matrix(matrixB,o,p);
  79. //The first parameter of the cudaMalloc() function is the address of a pointer variable that must point to the allocated object after allocation.
  80.  
  81. error=cudaMalloc((void**)&dA,size);
  82. if(error) printf("\nError in allocation");
  83. error=cudaMalloc((void**)&dB,size);
  84. if(error)printf("\nError in allocation");
  85. cudaMalloc((void**)&dC,size);
  86. if(error)printf("\nError in allocation");
  87. error=cudaMemcpy(dA,matrixA,size,cudaMemcpyHostToDevice);
  88. if(error)printf("\nError in copying data from host to device");
  89. error=cudaMemcpy(dB,matrixB,size,cudaMemcpyHostToDevice);
  90. if(error)printf("\nError in copying data from host to device");
  91.  
  92. dim3 dimBlock(m,m);
  93. dim3 dimGrid(1,1);
  94. matrix_mult<<<dimGrid,dimBlock>>>(dA,dB,dC,m);
  95.  
  96. error=cudaMemcpy(matrixC,dC,size,cudaMemcpyDeviceToHost);
  97. if(error)printf("\nError in copying data from device to host");
  98. cudaFree(dA);
  99. cudaFree(dB);
  100. cudaFree(dC);
  101. printf("\n\nThe Product matrix is:(C=A*B):\n");
  102. print_matrix(matrixC,m,m);
  103.  
  104. }
  105. }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement