Advertisement
pai

Untitled

pai
Feb 21st, 2012
365
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.39 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<stdlib.h>
  4. #define n 3
  5.  
  6. /*__global__ void kernel(int **support[],int size)
  7. {
  8.    
  9.     int i=threadIdx.x;
  10.     int j=threadIdx.y;
  11.     if(i<size)
  12.         support[i*size+j]=10;
  13. }
  14. */
  15. int main()
  16. {
  17.     long int **matrix=NULL;
  18.     long int **dev_matrix=NULL;
  19.     long int **support=NULL;
  20.     int i,j,er=1000;
  21.     long int **result=NULL;
  22.    int size;
  23.    matrix=(long int **)malloc(sizeof(long int *)*n);
  24.     for(i=0;i<n;i++)
  25.     {
  26.         matrix[i]=(long int *)calloc(n,sizeof(long int));
  27.     }
  28.    
  29.     result=(long int **)malloc(sizeof(long int *)*n);
  30.     for(i=0;i<n;i++)
  31.     {
  32.         result[i]=(long int *)calloc(n,sizeof(long int));
  33.     }
  34.     for(i=0;i<n;i++)
  35.     {
  36.         for(j=0;j<n;j++)
  37.             {
  38.                 result[i][j]=100;
  39.             }
  40.         printf("\n");
  41.     }
  42.     printf("Result before copy:\n");
  43.     for(i=0;i<n;i++)
  44.     {
  45.         for(j=0;j<n;j++)
  46.             {
  47.                 printf("%ld\t",result[i][j]);
  48.             }
  49.         printf("\n");
  50.     }
  51.    
  52.    
  53.     //pointer array to store list of elements
  54.     er=cudaMalloc((void **) &dev_matrix,n*sizeof(long int *));
  55.     if(er) printf("%d Error in allocation",er);
  56.     //For copying the pointers to GPU this support helps
  57.     support = (long int **) malloc(sizeof(long int*)*n);
  58.    
  59.     //Copied the support vector of the pointers
  60.     er=cudaMemcpy(support,dev_matrix,n*sizeof(long int*),cudaMemcpyDeviceToHost);
  61.     if(er) printf("%dError in copying the pointers",er);
  62.     for (i=0;i<n;++i)
  63.     {
  64.         //printf("hello"); 
  65.         er=cudaMalloc((void**)&support[i],sizeof(long int)*n);
  66.         if(er) printf("%dError in allocation",er);
  67.     }
  68.    
  69.     for(i=0;i<n;i++)
  70.     {
  71.    
  72.         for(j=0;j<n;j++)
  73.         {
  74.             //printf("I am coming from device to host:\n");
  75.             cudaMemcpy(support[i*n+j],matrix[i*n+j],sizeof(long int),cudaMemcpyHostToDevice);
  76.         }
  77.     }
  78.     size=n;
  79.     /*for(i=0;i<n;i++)
  80.     {
  81.     kernel<<<1,n>>>(dev_matrix[i],size);
  82.     }*/
  83.     for(i=0;i<n;i++)
  84.     {
  85.        
  86.         for(j=0;j<n;j++)
  87.         {
  88.             //printf("I am coming from device to host:\n");
  89.             cudaMemcpy(result[i*n+j],support[i*n+j],sizeof(long int),cudaMemcpyDeviceToHost);
  90.         }
  91.     }
  92.     printf("Result after copy:\n");
  93.     for(i=0;i<n;i++)
  94.     {
  95.         for(j=0;j<n;j++)
  96.             {
  97.                 printf("Element[%d][%d]=%ld\t",i,j,result[i][j]);
  98.             }
  99.         printf("\n");
  100.     }
  101.    
  102.  
  103. return 0;
  104. }
  105.  
  106.  
  107.  
  108. /*
  109. The output that get is
  110. Result before copy:
  111. 100 100 100
  112. 100 100 100
  113. 100 100 100
  114.  
  115. Result after copy:
  116. Element[0][0]=0 Element[0][1]=100   Element[0][2]=100  
  117. Element[1][0]=0 Element[1][1]=100   Element[1][2]=100  
  118. Element[2][0]=0 Element[2][1]=100   Element[2][2]=100  
  119.  
  120.  
  121.  
  122. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement