Personal blog of James Bryan Graves
Computer science (CS), quantum information science (QIS),
& software development & engineering (SDE)

2024-06-10 QIS/QCS Simulator in Nvidia CUDA, p.1 (en_US)

Working on a quantum universal gate simulator for educational purposes, non-production ready, designed to run on Nvidia hardware via CUDA.

Source code will be developing on Github, with some of the process documented on this blog.

This is a simple CUDA PoC: simply adding 2 numbers together


// add.cu

#include <iostream>
#include <cuda_runtime.h>

// CUDA kernel function to add two numbers
__global__ void add(int* a, int* b, int* c) {
    *c = *a + *b;
}

int main() {
    // Host variables
    int a = 3;
    int b = 5;
    int c = 0;

    // Device variables
    int *d_a, *d_b, *d_c;

    // Allocate memory on the device
    cudaMalloc((void**)&d_a, sizeof(int));
    cudaMalloc((void**)&d_b, sizeof(int));
    cudaMalloc((void**)&d_c, sizeof(int));

    // Copy host variables to device
    cudaMemcpy(d_a, &a, sizeof(int), cudaMemcpyHostToDevice);
    cudaMemcpy(d_b, &b, sizeof(int), cudaMemcpyHostToDevice);

    // Launch the kernel with one block and one thread
    add<<<1, 1>>>(d_a, d_b, d_c);

    // Copy the result back to the host
    cudaMemcpy(&c, d_c, sizeof(int), cudaMemcpyDeviceToHost);

    // Print the result
    std::cout << "Result: " << c << std::endl;

    // Free device memory
    cudaFree(d_a);
    cudaFree(d_b);
    cudaFree(d_c);

    return 0;
}

Compiling

Normally you would compile as follows


> nvcc -o add add.cu

The following ERROR occurs


nvcc fatal   : Cannot find compiler 'cl.exe' in PATH

Added the following to the PATH environment variable


C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.40.33807\bin\Hostx64\x64

The following error occurs


C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.3\include\crt/host_config.h(164): fatal error C1189: #error:  -- unsupported Microsoft Visual Studio version! Only the versions between 2017 and 2022 (inclusive) are supported! The nvcc flag '-allow-unsupported-compiler' can be used to override this version check; however, using an unsupported host compiler may cause compilation failure or incorrect run time execution. Use at your own risk.

Just take the advice


$ nvcc -o add add.cu -allow-unsupported-compiler

This successfully compiles and runs.


> .\add

Nvidia CUDA version

At the time of writing my Nvidia CUDA toolchain


> nvcc --versionnvcc --version

Output:


nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2023 NVIDIA Corporation
Built on Wed_Nov_22_10:30:42_Pacific_Standard_Time_2023
Cuda compilation tools, release 12.3, V12.3.107
Build cuda_12.3.r12.3/compiler.33567101_0

Upgrading to CUDA to 12.5

Upgrading 12.3 → 12.5 fixed the need for -allow-unsupported-compiler


> nvcc --versionnvcc --version

Output:


nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2024 NVIDIA Corporation
Built on Wed_Apr_17_19:36:51_Pacific_Daylight_Time_2024
Cuda compilation tools, release 12.5, V12.5.40
Build cuda_12.5.r12.5/compiler.34177558_0