Program not printing anything

I am a newbie so please don’t use much jargon. My graphic card is Nvdia Geforce 940MX , my cuda version is CUDA 11.4. And I am running this code from visual studio 2019.

#include “cuda_runtime.h”
#include “device_launch_parameters.h”

#include <stdio.h>

global void unique_gid_calculation_2d(int* data)
{
int tid = threadIdx.x;
int block_offset = blockIdx.x * blockDim.x;

int row_offset = blockDim.x * gridDim.x * blockIdx.y;

int gid = row_offset + block_offset + tid;
printf("blockIdx.x : %d, blockIdx.y : %d, threadIdx.x : %d, gid : %d - data : %d \n",
	blockIdx.x, blockIdx.y, tid, gid, data[gid]);

}

int main() {

int array_size = 16;
int array_byte_size = sizeof(int) * array_size;
int h_data[] = { 23,9,4,53,65,12,1,33,22,43,56,4,76,81,94,32 };

int* d_data;
cudaMalloc((void**)&d_data, array_byte_size);
cudaMemcpy(d_data, h_data, array_byte_size, cudaMemcpyHostToDevice);
dim3 block(4);
dim3 grid(2,2);
unique_gid_calculation_2d << < grid, block >> >(d_data);
cudaDeviceSynchronize();
cudaDeviceReset();
return 0;

}

problem is that this code doesn’t print anything. Any reason why this the case?

What happens when you run the executable under control of cuda-memcheck? I built and ran the program and it outputs this:

blockIdx.x : 1, blockIdx.y : 0, threadIdx.x : 0, gid : 4 - data : 65
blockIdx.x : 1, blockIdx.y : 0, threadIdx.x : 1, gid : 5 - data : 12
blockIdx.x : 1, blockIdx.y : 0, threadIdx.x : 2, gid : 6 - data : 1
blockIdx.x : 1, blockIdx.y : 0, threadIdx.x : 3, gid : 7 - data : 33
blockIdx.x : 1, blockIdx.y : 1, threadIdx.x : 0, gid : 12 - data : 76
blockIdx.x : 1, blockIdx.y : 1, threadIdx.x : 1, gid : 13 - data : 81
blockIdx.x : 1, blockIdx.y : 1, threadIdx.x : 2, gid : 14 - data : 94
blockIdx.x : 1, blockIdx.y : 1, threadIdx.x : 3, gid : 15 - data : 32
blockIdx.x : 0, blockIdx.y : 1, threadIdx.x : 0, gid : 8 - data : 22
blockIdx.x : 0, blockIdx.y : 1, threadIdx.x : 1, gid : 9 - data : 43
blockIdx.x : 0, blockIdx.y : 1, threadIdx.x : 2, gid : 10 - data : 56
blockIdx.x : 0, blockIdx.y : 1, threadIdx.x : 3, gid : 11 - data : 4
blockIdx.x : 0, blockIdx.y : 0, threadIdx.x : 0, gid : 0 - data : 23
blockIdx.x : 0, blockIdx.y : 0, threadIdx.x : 1, gid : 1 - data : 9
blockIdx.x : 0, blockIdx.y : 0, threadIdx.x : 2, gid : 2 - data : 4
blockIdx.x : 0, blockIdx.y : 0, threadIdx.x : 3, gid : 3 - data : 53

I am using visual studio…I don’t know how to do that, here on my device it isn’t printing

I don’t use IDEs. As I recall, for console apps Visual Studio will open a console window to receive the output and by default this window closes as soon as the program terminates. Which in this case would be immediately. Could that be the issue? What happens if you just use a “Hello, World” program without any device code?

Visual Studio has an option somewhere (under Debugging Options?) to keep the console window open on program termination.

Well in that case… Can you give a source or website where I can find all command line commands for cuda compilation and also suggest a good text editor

Everybody has different ideas as to what makes for a good editor. I have been using Emacs for the past 30 years.

How to operate nvcc from the command line is described in the official docs for it:

For your example code I simply used this:

nvcc -o doesnt_print.exe doesnt_print.cu

Any time you are having trouble with a CUDA code, I recommend using proper CUDA error checking.

Google “proper CUDA error checking”, and take the first hit, and apply it to your code, then compile and run again.

My guess is you are running into this. Please don’t just read the first sentence of that and start asking questions. The entire answer may be useful. I’m not suggesting your device is of compute capability 3.0 or below. Specifically your device is compute capability 5.0 and CUDA 11 compiles for compute capability 5.2 by default. All of these topics (“what is compute capability?”, “how do I set compute capability in visual studio?” etc.) are covered in questions in various forum threads, so a bit of searching will likely produce useful info.