everyone! I am starting to study CUDA programming and I noticed one thing when studying the 1D stencil problem implementation.
To run the function at the device one must write fun_name<<<blocks,threads>>>(arguments)
. I wrote a test code to print the vector values considering the global index:
#include<stdio.h>
#include<stdlib.h>
#define size 6
#define num_threads 3
__global__ void test(int *d_v){
int gidx = threadIdx.x + blockIdx.x * blockDim.x;
printf("gidx = %d; v[gidx] = %d \n", gidx, d_v[gidx]);
}
int main(){
// defining a pointer in host
int *v;
// defining a pointer in device
int *d_v;
v = (int*)malloc(size*sizeof(int));
cudaMalloc((void**) &d_v, sizeof(int)*size);
for(int i = 0; i < size; i++){
v[i] = (size - i);
printf("%d ", v[i]);
}
printf("\n");
cudaMemcpy(d_v, v, size*sizeof(int), cudaMemcpyHostToDevice);
test<<<size/num_threads,num_threads>>>(d_v);
cudaDeviceSynchronize();
free(v);
cudaFree(d_v);
}
My question is: If I run the test function as test<<<size/num_threads,num_threads>>>(d_v + number);
the values in vector d_v
will shift by the same amount of number
. Why?
In Python and MATLAB, when we add a constant to an array, the value is summed to the array elements. Thus, my intuition says, at first, that calling d_v + number
it would add the value number
to the elements of d_v
(I know that d_v is a pointer and things may work differently).
edit: I gave the example of adding a constant to an array in Python and MATLAB just to ilustrate. I do not want to sum a constant to the array defined as a pointer, just to know why the indexes changed. Thus, the suggested reply concerning pointers arithmetics does not offer a answer to my question.