'CUDA Unified memory compilation error type mismatched

Problem statement:

There are three variables a and b and nk. interval is another variable which store the value of (b-a)/nk. There is an array named, interval_length whose starting value is a and ending value is b. This array stores each value in between the range a and b and increment the array value each time with (b-a)/n. Now, if b = 1 and a= 0 then interval = (b-a)/n =(1-0)/4 = 0.25 where n=4. The interval_length[] array then store 0.0, 0.25, 0.50, 0.75, 1.0.

I implemented this problem statement in CUDA programming using CUDA unified memory structure.

Here is my code:

 #include<stdio.h>
 #include<cuda.h>

#define N 4
__global__ void midpoint( double *interval_length, int nk){

        int tid;
        tid = blockDim.x*blockIdx.x+threadIdx.x;
        double interval;
        double a =0.0, b=1.0;
        interval = (b-a)/(double)nk;
   if(tid<nk){
        interval_length[tid] = a;
        a = a + interval;
}

}

int main(int argc, char* argv[]){

        int thread_count;
        thread_count = strtol(argv[1], NULL, 10);
        
        double *interval_length;
        
        cudaMallocManaged(&interval_length, N*sizeof(double));
        

        //Launch Kernel
        midpoint<<<1,thread_count>>>(interval_length,N);

//Synchronize threads
        cudaDeviceSynchronize();

for(int i=0; i<=N; i++){
                printf("%lf\n", interval_length[i]);
}
                        cudaFree(interval_length);
                        
        return 0;
}

I got the output for interval_length[] array is

0.000000
0.000000
0.000000
0.000000
0.000000

which means there is nothing store in this array. It should store 0.000000, 0.250000, 0.500000, 0.750000, 1.000000

I cannot figure out this errors.

Any help would be appreciated



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source