Hi!
I made a small CUDA program but my problem is that the tid is sometimes different and as I see it changes itself in a small range. I’ve already googled this problem but found nothing… Please help me!
Here’s my code:
#include "cuda_runtime.h"
#include "device_launch_parameters.h"
#include "device_functions.h"
#include <iostream>
using namespace std;
__global__ void kernelFunc(int* nums) {
int threadsPerBlock = blockDim.x * blockDim.y * blockDim.z;
int threadPosInBlock = threadIdx.x +
blockDim.x * threadIdx.y +
blockDim.x * blockDim.y * threadIdx.z;
int blockPosInGrid = blockIdx.x +
gridDim.x * blockIdx.y +
gridDim.x * gridDim.y * blockIdx.z;
int tid = blockPosInGrid * threadsPerBlock + threadPosInBlock;
for(i = 0; i<10; i++) {
nums[i] = tid;
}
__syncthreads();
}
void main() {
const int cudaSize = 10;
int csakazertis[cudaSize];
int* d_csakazertis;
cudaMalloc((void**)&d_csakazertis, cudaSize * sizeof(int));
dim3 block = dim3(8, 8, 8);
dim3 grid = dim3(16, 16);
kernelFunc<<<grid,block>>>(d_csakazertis, d_result2, 1, d_result);
cudaMemcpy(csakazertis, d_csakazertis, cudaSize * sizeof(int), cudaMemcpyDeviceToHost);
cudaFree(d_csakazertis);
for(int i=0; i<cudaSize; i++) {
cout << csakazertis[i] << ", ";
}
getchar();
}
My output usually: 130303, 130303, 130303, 130431, 130431, 130431, 130431, … (changed after the 3rd value) WHY???
BUT sometimes: 131007, 131007, 131007, … (no change) NOW WHY???
As you see the values are always changing and two different run produces different values…
Does anyone know why is this? How could I fix it??? (During debugging the Warp Watch shows the correct values…I don’t understand why is this happening…)
Thanks in advance!
Zoli :)