My code does not work probably missing headers

I downloaded the windows version of Cuda SDK under the option 10. It says it is version 12. I am using Visual Studio. I am new to Cuda. I have looked under header files and I don’t know why NVIDIA if they are going to change the header files say what they are with each version.

this is my code:

#include <cuda_runtime.h>
#include <cuda.h>

#include <iostream>
#include <stdlib.h>
#include <ctime>
#include <math.h>
#include <assert.h>

using namespace std;

__global__ void AddInts(int* a, int* b, int count)
{
	int id = blockIdx.x * blockDim.x + threadIdx.x; //<- none of these are recognized.
	if (id < count) {
		a[id] += b[id];
	}
}

int main() {
	srand(time(NULL));

	int count = 100;
	int* h_a = new int[count];
	int* h_b = new int[count];
	for (int i = 0; i < count; i++) {
		h_a[i] = rand() % 1000;
		h_b[i] = rand() % 1000;
	}
	cout << "before run.\n";
	for (int i = 0; i < 5; i++) cout << h_a << " " << h_b << "\n";

	int* d_a, * d_b;
	if (cudaMalloc(&d_a, sizeof(int) * count) != cudaSuccess) {
		cout << "a failed\n";
		return 0;
	}
	if (cudaMalloc(&d_b, sizeof(int) * count) != cudaSuccess) {
		cout << "b failed\n";
		cudaFree(d_a);
		return 0;
	}
	if (cudaMemcpy(d_a, h_a, sizeof(int) * count, cudaMemcpyHostToDevice) != cudaSuccess) {
		cout << "a did not copy\n";
		cudaFree(d_a);
		cudaFree(d_b);
		return 0;
	}
	if (cudaMemcpy(d_b, h_b, sizeof(int) * count, cudaMemcpyHostToDevice) != cudaSuccess) {
		cout << "b did not copy\n";
		cudaFree(d_a);
		cudaFree(d_b);
		return 0;
	}

	AddInts<<<count / 256 + 1, 256 >>>(d_a, d_b); //<- this is not recognized 

	if (cudaMemcpy(d_a, h_a, sizeof(int) * count, cudaMemcpyDeviceToHost) != cudaSuccess) {
		cudaFree(d_a);
		cudaFree(d_b);
		delete[] h_a;
		delete[] h_b;
		cout << "could not copy back/n";
	}

	cout << "after run.\n";
	for (int i = 0; i < 5; i++) cout << h_a << "\n";

	cudaFree(d_a);
	cudaFree(d_b);

	delete[] h_a;
	delete[] h_b;
	return 0;
}

It does build, but when I run it it says it can’t find a file.

I did make a mistake but I faxed it and it still doesn’t work.

AddInts<<<count / 256 + 1, 256 >>>(d_a, d_b, count);

This topic may be better suited for the CUDA forums. I will move it over for you.

Thanks, where can I find this?

It is located here: Latest Accelerated Computing/CUDA topics - NVIDIA Developer Forums

can you provide the exact output?

it says “unable to start program” then the build path then it says “system could not find file specified.”

You are using visual studio incorrectly. You may want to start by learning how to build one of the sample projects. There are also various tutorial videos available.

So I just created a Cuda Runtime project. Then ran it and it worked.

1 Like

Alternatively, you can right click the object (or what ever the sub solution thing is called, usually the project name) and set a build customization under build dependencies. Then when you add a file to the project, you add it of type cuda.