NVIDIA / cuda-samples

Samples for CUDA Developers which demonstrates features in CUDA Toolkit
Other
6.44k stars 1.83k forks source link

why this code can not complie? #141

Open cqray1990 opened 2 years ago

cqray1990 commented 2 years ago

/**

include

include

static const int WORK_SIZE = 256;

/**

int main(int argc, char **argv) { CUmodule module; CUcontext context; CUdevice device; CUdeviceptr deviceArray; CUfunction process;

void *kernelArguments[] = { &deviceArray };
int deviceCount;
unsigned int idata[WORK_SIZE], odata[WORK_SIZE];

for (int i = 0; i < WORK_SIZE; ++i) {
    idata[i] = i;
}

CHECK_CUDA_RESULT(cuInit(0));
CHECK_CUDA_RESULT(cuDeviceGetCount(&deviceCount));
if (deviceCount == 0) {
    printf("No CUDA-compatible devices found\n");
    exit(1);
}
CHECK_CUDA_RESULT(cuDeviceGet(&device, 0));
CHECK_CUDA_RESULT(cuCtxCreate(&context, 0, device));

CHECK_CUDA_RESULT(cuModuleLoad(&module, "bitreverse.fatbin"));
CHECK_CUDA_RESULT(cuModuleGetFunction(&process, module, "bitreverse"));

CHECK_CUDA_RESULT(cuMemAlloc(&deviceArray, sizeof(int) * WORK_SIZE));
CHECK_CUDA_RESULT(
        cuMemcpyHtoD(deviceArray, idata, sizeof(int) * WORK_SIZE));

CHECK_CUDA_RESULT(
        cuLaunchKernel(process, 1, 1, 1, WORK_SIZE, 1, 1, 0, NULL, kernelArguments, NULL));

CHECK_CUDA_RESULT(
        cuMemcpyDtoH(odata, deviceArray, sizeof(int) * WORK_SIZE));

for (int i = 0; i < WORK_SIZE; ++i) {
    printf("Input value: %u, output value: %u\n", idata[i], odata[i]);
}

CHECK_CUDA_RESULT(cuMemFree(deviceArray));
CHECK_CUDA_RESULT(cuCtxDestroy(context));

return 0;

}

ingowald commented 1 year ago

That code compiled just fine on my machine. Note that the markdown "swallowed" some underscores and backslashes, i added those back (in case that's what the problem is). In particular the LINE should be __LINE__, and the macro should have backslashes (\) at the end; but other than that the sample compiled just fine (ubuntu 20.04). What error(s) were you seeing?