Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- #include<stdlib.h>
- #define n 3
- /*__global__ void kernel(int **support[],int size)
- {
- int i=threadIdx.x;
- int j=threadIdx.y;
- if(i<size)
- support[i*size+j]=10;
- }
- */
- int main()
- {
- long int **matrix=NULL;
- long int **dev_matrix=NULL;
- long int **support=NULL;
- int i,j,er=1000;
- long int **result=NULL;
- int size;
- matrix=(long int **)malloc(sizeof(long int *)*n);
- for(i=0;i<n;i++)
- {
- matrix[i]=(long int *)calloc(n,sizeof(long int));
- }
- result=(long int **)malloc(sizeof(long int *)*n);
- for(i=0;i<n;i++)
- {
- result[i]=(long int *)calloc(n,sizeof(long int));
- }
- for(i=0;i<n;i++)
- {
- for(j=0;j<n;j++)
- {
- result[i][j]=100;
- }
- printf("\n");
- }
- printf("Result before copy:\n");
- for(i=0;i<n;i++)
- {
- for(j=0;j<n;j++)
- {
- printf("%ld\t",result[i][j]);
- }
- printf("\n");
- }
- //pointer array to store list of elements
- er=cudaMalloc((void **) &dev_matrix,n*sizeof(long int *));
- if(er) printf("%d Error in allocation",er);
- //For copying the pointers to GPU this support helps
- support = (long int **) malloc(sizeof(long int*)*n);
- //Copied the support vector of the pointers
- er=cudaMemcpy(support,dev_matrix,n*sizeof(long int*),cudaMemcpyDeviceToHost);
- if(er) printf("%dError in copying the pointers",er);
- for (i=0;i<n;++i)
- {
- //printf("hello");
- er=cudaMalloc((void**)&support[i],sizeof(long int)*n);
- if(er) printf("%dError in allocation",er);
- }
- for(i=0;i<n;i++)
- {
- for(j=0;j<n;j++)
- {
- //printf("I am coming from device to host:\n");
- cudaMemcpy(support[i*n+j],matrix[i*n+j],sizeof(long int),cudaMemcpyHostToDevice);
- }
- }
- size=n;
- /*for(i=0;i<n;i++)
- {
- kernel<<<1,n>>>(dev_matrix[i],size);
- }*/
- for(i=0;i<n;i++)
- {
- for(j=0;j<n;j++)
- {
- //printf("I am coming from device to host:\n");
- cudaMemcpy(result[i*n+j],support[i*n+j],sizeof(long int),cudaMemcpyDeviceToHost);
- }
- }
- printf("Result after copy:\n");
- for(i=0;i<n;i++)
- {
- for(j=0;j<n;j++)
- {
- printf("Element[%d][%d]=%ld\t",i,j,result[i][j]);
- }
- printf("\n");
- }
- return 0;
- }
- /*
- The output that get is
- Result before copy:
- 100 100 100
- 100 100 100
- 100 100 100
- Result after copy:
- Element[0][0]=0 Element[0][1]=100 Element[0][2]=100
- Element[1][0]=0 Element[1][1]=100 Element[1][2]=100
- Element[2][0]=0 Element[2][1]=100 Element[2][2]=100
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement