Advertisement
pai

Cuda Program

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