Advertisement
pai

Matrix_Mult

pai
Feb 21st, 2012
378
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.56 KB | None | 0 0
  1.  
  2. #include<stdio.h>
  3. #include<stdlib.h>
  4.  
  5.  
  6. __global__ void matrix_mult(long int *dA,long int *dB,long int *dC,long int m)
  7. {
  8.    
  9.     long int i=blockIdx.x*blockDim.x+threadIdx.x;
  10.     long int j=blockIdx.y*blockDim.y+threadIdx.y;
  11.    
  12.     long int product=0,k;
  13.     for(k=0;k<m;k++)
  14.     {
  15.         product=product+dA[i*m+k]*dB[k*m+j];
  16.     }
  17.     dC[i*m+j]=product;
  18.     i+=blockDim.x*gridDim.x;
  19.     j+=blockDim.y*gridDim.y;
  20. }
  21.  
  22.  
  23. void read_matrix_from_file(FILE *fp,long int *matrix,long int x,long int y)
  24. {
  25. long int i,j;
  26.     for(i=0;i<x;i++)
  27.     {
  28.         for(j=0;j<y;j++)
  29.         {
  30.             fscanf(fp,"%ld",&matrix[i*x+j]);
  31.         }
  32.     }
  33. }
  34.  
  35.  
  36. void print_matrix_file(FILE *fp,long int *matrix,long int x,long int y)
  37. {
  38. long int i,j;
  39.     for(i=0;i<x;i++)
  40.     {
  41.         for(j=0;j<y;j++)
  42.         {
  43.             fprintf(fp,"%ld ",matrix[i*x+j]);
  44.         }
  45.         fprintf(fp,"\n");
  46.     }
  47. }
  48. void print_matrix(long int *matrix,long int x,long int y)
  49. {
  50.     long int i,j;
  51.     for(i=0;i<x;i++)
  52.     {
  53.         for(j=0;j<y;j++)
  54.         {
  55.             printf("%ld ",matrix[i*x+j]);
  56.         }
  57.         printf("\n");
  58.     }
  59. }
  60.  
  61.  
  62.  
  63. int main(int argc,char *argv[])
  64. {
  65.     FILE *fp1=NULL,*fp2=NULL,*fp3=NULL;
  66.     long int m,n,o,p;
  67.     float elapsedTime;
  68.     long int *dA=NULL,*dB=NULL,*dC=NULL;
  69.     long int *matrixA=NULL,*matrixB=NULL,*matrixC=NULL;
  70.     enum cudaError error;
  71.     if(argc!=8)
  72.     {
  73.         printf("\n(8 Parameters)./a.out matrixfile1 m n matrixfile2 o p outputfile.txt");
  74.         exit(0);
  75.     }
  76.    
  77.    
  78.         fp1=fopen(argv[1],"r+");
  79.         fp2=fopen(argv[4],"r+");
  80.         fp3=fopen(argv[7],"w+");
  81.         if(fp1==NULL||fp2==NULL||fp3==NULL)
  82.         {
  83.             printf("\nError in opening the file"); 
  84.             exit(0);
  85.         }
  86.        
  87.        
  88.             m=atol(argv[2]);
  89.             n=atol(argv[3]);
  90.             o=atol(argv[5]);
  91.             p=atol(argv[6]);
  92.    
  93.             matrixA=(long int *)malloc(m*n*sizeof(long int));
  94.             matrixB=(long int *)malloc(o*p*sizeof(long int));
  95.             matrixC=(long int *)malloc(m*p*sizeof(long int));
  96.             if(matrixA==NULL||matrixB==NULL||matrixC==NULL)
  97.             {
  98.                 printf("Error Memory allocation for matrix in host\n");
  99.                 exit(0);
  100.             }
  101.             //size=m*m;
  102.             // Reading the matrix from file
  103.             read_matrix_from_file(fp1,matrixA,m,n);
  104.             read_matrix_from_file(fp2,matrixB,o,p);        
  105.             //Print the matrix on the console
  106.             //printf("\n\nMatrixA is:\n");
  107.             //print_matrix(matrixA,m,n);
  108.             //printf("\n\nMatrixB is:\n");
  109.             //print_matrix(matrixB,o,p);
  110.             //The first parameter of the cudaMalloc() function is the address of a pointer variable that must point to the allocated object after allocation.
  111.             //MatrixA GPU Memory allocation
  112.             error=cudaMalloc((void**)&dA,m*n*sizeof(long int));
  113.             if(error) printf("\nError in allocation");
  114.  
  115.             //MatrixB GPU Memory allocation
  116.             error=cudaMalloc((void**)&dB,p*o*sizeof(long int));
  117.             if(error)printf("\nError in allocation");
  118.  
  119.             //MatrixC GPU Memory allocation
  120.             error=cudaMalloc((void**)&dC,m*p*sizeof(long int));
  121.             if(error)printf("\nError in allocation");
  122.  
  123.             //m GPU Memory allocation
  124.             /*error=cudaMalloc((void **)&dM,sizeof(long int));
  125.             if(error)printf("Error in allocation");*/
  126.  
  127.             //Copying matrixA to GPU Memory from CPU
  128.             error=cudaMemcpy(dA,matrixA,m*n*sizeof(long int),cudaMemcpyHostToDevice);
  129.             if(error)printf("\nError in copying data from host to device");
  130.  
  131.             //Copying matrixB to GPU Memory from CPU
  132.             error=cudaMemcpy(dB,matrixB,o*p*sizeof(long int),cudaMemcpyHostToDevice);
  133.             if(error)printf("\nError in copying data from host to device");
  134.  
  135.             //Copying m to GPU Memory from CPU
  136.             /*error=cudaMemcpy(dM,&m,sizeof(long int),cudaMemcpyHostToDevice);
  137.             if(error) printf("\nError in copying m:");*/
  138.  
  139.             dim3 dimBlock(10,10);
  140.             dim3 dimGrid(100,100);
  141.  
  142.             cudaEvent_t start,stop;//To compute the elapsed time
  143.             cudaEventCreate(&start);//Creating a start event. It marks the starting of an event
  144.             cudaEventCreate(&stop);//Creating a stop event. It marks the stoping of an event
  145.             cudaEventRecord(start,0);
  146.             matrix_mult<<<dimGrid,dimBlock>>>(dA,dB,dC,m);
  147.             cudaEventRecord(stop,0);//Same as above 'start' event
  148.             cudaEventSynchronize(stop);//Blocks until the event has actually been recorded
  149.             cudaEventElapsedTime(&elapsedTime,start,stop);
  150.             cudaEventDestroy(start);//Destroys the specified start object.
  151.             cudaEventDestroy(stop);//Destroys the specified stop object.
  152.             printf("\n\nElapsed Time for computation on GPU(Seconds)=%lf\n",(double)(elapsedTime/1000));// Prints the computation time
  153.             error=cudaMemcpy(matrixC,dC,m*p*sizeof(long int),cudaMemcpyDeviceToHost);
  154.             if(error)printf("\nError in copying data from device to host");
  155.             cudaFree(dA);
  156.             cudaFree(dB);
  157.             cudaFree(dC);
  158.             printf("\n\nThe Product matrix is:(C=A*B):\n");
  159.             //print_matrix(matrixC,m,p);
  160.             print_matrix_file(fp3,matrixC,m,p);
  161.            
  162.        
  163.    
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement