Q: How to resolve undefined reference errors to threadIdx.x, blockDim.x, and blockIdx.x in CUDA? I'm a beginner working on a parallel list ranking algorithm in CUDA. I have no idea why I'm getting undefined reference errors to threadIdx.x, blockDim.x, and blockIdx.x, with the message "ld returned exit 1 status", when compiling with 'nvcc ParallelListRanking.cu -o ParallelListRanking'. Adding #include "device_launch_parameters.h" and #include "cuda.h" did not help. Any ideas to resolve this?
#include<stdio.h>
#include <device_launch_parameters.h>
#include <cuda.h>

__global__ void set_up(unsigned long int n, unsigned long int *dS, unsigned long long int *dQR){

        unsigned long int i = threadIdx.x + blockDim.x * blockIdx.x;

        if (i < n) {
                dS[i] = i + 1;
                dS[n-1] = 0;

                if (dS[i] != 0) {
                        dQR[i] = (dQR[i]%(1ull<<32)) + (1ull<<32) * 1ull;
                }
                else {
                        dQR[i] = (dQR[i]%(1ull<<32)) + (1ull<<32) * 0ull;
                }

                dQR[i] = ((unsigned long long int) dS[i])%(1ull<<32) + (1ull<<32)*(dQR[i]/(1ull<<32));
        }
}

__global__ void update(unsigned long int n, unsigned long long int *dQR) {

        unsigned long int i = threadIdx.x + blockDim.x * blockIdx.x;

        if (i < n) {
                unsigned long int tempdQ = (unsigned long int) (dQR[i]%(1ull<<32));
                unsigned long int tempdQQ = (unsigned long int) (dQR[tempdQ]%(1ull<<32));

                if (tempdQ != 0 && tempdQQ != 0) {
                        unsigned long long int tmpdQRQ = dQR[tempdQ];

                        unsigned long int tempdR = (unsigned long int) dQR[i]/(1ull<<32);
                        unsigned long long int tempdRQ = tmpdQRQ/(1ull<<32);

                        dQR[i] = ((unsigned long long int) tempdQQ)%(1ull<<32) + (((unsigned long long int) tempdR) + tempdRQ)*(1ull<<32);

                        tempdQ = tempdQQ;
                        tempdQQ = (unsigned long int) dQR[tempdQ]%(1ull<<32);
                }
        }
}

int main() {

        unsigned long int n = 1000;
        unsigned long int *dS =(unsigned long int *) cudaMalloc((int **)n, sizeof(unsigned long int));
        unsigned long long int *dQR = (unsigned long long int *) cudaMalloc((int **)n, sizeof(unsigned long long int));
        unsigned long int i = threadIdx.x + blockDim.x * blockIdx.x;

        set_up<<< 100, 100 >>>(n, dS, dQR);

        for (int t = 1; t < n; t++) {
                update<<< 100, 100>>>(n, dQR);
        }

        printf("%lu", (unsigned long int) (dQR[i]%(1ull<<32)));
        printf("\n");
        printf("%lu", (unsigned long int) (dQR[i]/(1ull<<32)));

        cudaFree(dQR);
        cudaFree(dS);

        return 0;
}


A: You can't do this:
unsigned long int i = threadIdx.x + blockDim.x * blockIdx.x;

in host code (i.e. in main()).
Those variables are automatically defined in device code only (the routines decorated with __global__, for example).
The remainder of your host code from that point should not depend on variables like threadIdx.x because they are not defined in host code and make no sense to use there.
So just delete that line entirely (from main()) and come up with another method to use an index like that in your host code.
The only place you seem to use it is in your printf statements, so I guess if you are trying to print out all the variables you will need a loop in host code:
for (int i = 0; i < n; i++){
    printf("%lu", (unsigned long int) (dQR[i]%(1ull<<32)));
    printf("\n");
    printf("%lu", (unsigned long int) (dQR[i]/(1ull<<32)));}

That is the proximal cause of the error you are reporting, however your code has other issues.
This is not correct usage of cudaMalloc:
    unsigned long int *dS =(unsigned long int *) cudaMalloc((int **)n, sizeof(unsigned long int));

you want to do something like this:
    cudaMallocManaged(&dS, sizeof(unsigned long int)*n);

and likewise for dQR.  (I'm switching from cudaMalloc to cudaMallocManaged here for a reason related to your attempt to print those variables from host code.)
There are likely other problems with your code.  Before attempting anything like this I would suggest you have a firm grasp of how to write a proper, simple vector add application in CUDA.
To get a basic grasp of CUDA you might want to study a simple sample code like vectorAdd and perhaps study one of the introductory blogs.
