That’s probably because there is no variable ‘tx’ defined inside the function ‘fact’ that txbob posted above. If by ‘tx’ you mean ‘threadIdx.x’, you can either use that directly, or define your own variable:
outside your function and pass it as a parameter to the function, or you could define it inside the function. If you define it inside the function, you can’t decorate your function with host anymore, as threadIdx.x is undefined in host code.
Here’s an example of the second approach (defining it inside the recursive function):
#include <stdio.h>
__device__ int fact(volatile int *t){
int tx = threadIdx.x;
int temp = t[tx];
if (temp == 2) return temp;
else {
t[tx]=temp-1;
return temp * fact(t);}
}
__global__ void test(){
__shared__ int data[64];
data[threadIdx.x] = threadIdx.x+2;
printf("x = %d, fact(x) = %d\n", threadIdx.x+2, fact(data));
}
int main(){
test<<<1,5>>>();
cudaDeviceSynchronize();
}