Copying part of 2D array to device

Hello,

I am trying to write some code which in loop copies few columns of 2d array starts from some offset.

For example array A is:

0 1 2 3 4 5 6 7 8 9 10

0 1 2 3 4 5 6 7 8 9 10

0 1 2 3 4 5 6 7 8 9 10

0 1 2 3 4 5 6 7 8 9 10

0 1 2 3 4 5 6 7 8 9 10

0 1 2 3 4 5 6 7 8 9 10

0 1 2 3 4 5 6 7 8 9 10

0 1 2 3 4 5 6 7 8 9 10

0 1 2 3 4 5 6 7 8 9 10

0 1 2 3 4 5 6 7 8 9 10

and i want to copy in loop

4 5 6 7

4 5 6 7

4 5 6 7

4 5 6 7

4 5 6 7

4 5 6 7

4 5 6 7

4 5 6 7

4 5 6 7

part of my code:

size_t pitch;

        int n =  20;

        size_t size = n*n*sizeof(float);

        flaot *ah = (float*)malloc(size);

        float *bh = (float*)malloc(size);

        float *ad;

        for(int i = 0 ; i < n ; ++i)

        {

                for(int j = 0 ; j < n;j++)

                {

                        ah[i*n+j] = j;

                        bh[i*n+j] = 0;

                }

        }

        int part = 4;

        cudaMallocPitch((void**)&ad,&pitch,part*sizeof(float),n);

        for(int i = 0 ; i < n; i+=part)

        {

                cudaMemcpy2D(ad,pitch,ah,part*sizeof(float),part*sizeof(float),n,cudaMemcpyHostToDevice);

                //kernel

                cudaMemcpy2D(bh,n*sizeof(float),ad,pitch,part*sizeof(float),n,cudaMemcpyDeviceToHost);

                for(int j = 0;j<n;j++)

                {

                        for(int k = 0; k < n; k++)

                        {

                                printf("%3.0f",bh[j*n+k]);

                        }

                        printf("\n");

                }

                printf("*************************************************************\n");

        }

These easiest (and best performance) was to do this is through ArrayFire’s subscripted indexing. Then it become a simple:

a = randu(10,10);

b = a.cols(4,7); // 5th-8th columns

That’ll end up just moving the pointer, essentially a noop (no work, no memory transfer). Very fast!

Cheers and good luck!

The ArrayFire :: Quick Ref also has great examples on how to index into arrays.