CommunityTips and Tricks

Introduction to CUDA development + How to set up with Visual Studio

Introduction

Think about this. Have you ever thought about two things at once? If you reflect a bit, our brains are super complex but they only focus on one train of thought. Sure, a lot can happen subconsciously, but you can only be conscious about a single thing—you can’t focus on two things at once simultaneously.

But what if you could? This opens up a wide array of possibilities. Imagine learning from multiple sources, or solving three math equations in your head simultaneously, or literally multitasking with each hand doing something different.

That’s the idea behind how graphics processing units (GPUs) are being utilized to fast track development time for a few specialized technologies. With its capability to process significantly more threads (vs CPUs), they can execute tasks that require heavy parallel processing, such as rendering graphics, training machine learning models, and running complex simulations.

And one of the ways to program your GPUs to spit out data that isn’t just graphics is via a framework called CUDA. And that’s what we’re talking about in this blog today.

Why is CUDA being used now

CUDA, which stands for Compute Unified Device Architecture, speeds up computing tasks by using the power of graphics processing units (GPUs). It is a framework developed by NVIDIA in 2006. CUDA allows developers to write programs that divide large computing tasks into smaller ones using parallel computing. 

This uses the many cores of a GPU to perform multiple calculations simultaneously—unlike a CPU, which uses a few powerful cores optimized for sequential processing. This parallel processing capability significantly speeds up tasks that involve large datasets or complex computations, such as those found in graphics rendering, scientific simulations, and machine learning.

Nvidia’s CUDA has been around for more than two decades and due to the popularity and inherent compatibility with its parent company’s physical video cards, it has emerged as one of the leaders in the industry. And even though CUDA’s chokehold on the space is breaking, it remains a top choice for accelerating training for machine learning models.

Industries using CUDA 

We’ve talked about the advantages of using GPUs and how you can use CUDA to program them to work on specific tasks. The most popular use case now is the rise of machine learning and AI, but we’ve listed down a couple of other industries that you may not know about that can also utilize the advantage of GPU computing power.

IndustryTask / Work NeededHow CUDA-enabled programs help
Data Science & AIDeep learning training, NLP, recommendation systemsSpeeds up training of AI models exponentially, helping with things like chatbots and recommendation algorithms.
High-Performance Computing (HPC)Scientific simulations, physics calculationsSpeeds up complex science experiments and research.
FinanceRisk modeling, high-frequency trading (HFT), portfolio optimizationComputes complex financial calculations much faster which helps traders make quick decisions.
Autonomous VehiclesObject detection, sensor fusion, path planningHelps self-driving cars “see” and react to their surroundings in real time.
Manufacturing & Industrial AutomationPredictive maintenance, defect detection, robotic controlHelps machines spot problems before they happen and improves automation.
Weather & Climate ScienceClimate modeling, hurricane prediction, data assimilationRuns weather simulations much faster to improve forecasts.
CybersecurityAnomaly detection, encryption/decryption, threat analysisHelps detect hackers and secure data faster.
RoboticsReal-time sensor processing, AI-based control, SLAM (Simultaneous Localization and Mapping)Helps robots process what they see and move more accurately.
Blockchain & CryptographyCryptocurrency mining, transaction validationMakes mining cryptocurrencies and securing transactions faster.

Challenges in learning CUDA development

While utilizing GPUs and programming them with CUDA is a rising framework, there is still a significant barrier to becoming a skilled CUDA programmer. Its biggest strength is also one of its complicating factors in learning. CUDA is designed for parallel computing, which is fundamentally different from traditional serial programming. Programmers need to grasp concepts like threads, blocks, and grids, and how they map to GPU hardware.

In addition to that, C/C++, a lower level language usually suited for intermediate developers, is arguably the language to learn if you want to maximize programming in CUDA (You can also opt for Python using PyTorch or Jax).

Lastly, CUDA requires a deeper knowledge on physical hardware (aka what NVIDIA GPU/s you’re using). There is extra setup involved both in hardware and software toolkits to access basic development and testing. Achieving high performance will also require studying the GPU architecture and careful optimization of code and tight memory management.

Setting up your first CUDA programming project

A CUDA .cu file with proper syntax highlighting and code analysis features opened in Visual Studio.

Starting with your first ever CUDA project may seem daunting but with the right directions, you can easily configure Visual Studio for CUDA programming projects in just an hour. Follow these steps below to get started:

Installing Visual Studio

Visual Studio is a good first option for an IDE if you are familiar with C++ already. It is compatible with the integration of the NVIDIA CUDA Toolkit which allows you to compile, debug, and optimize CUDA applications within the same platform.

  • Download Visual Studio

    First, download Visual Studio from Microsoft. Choose whatever edition you prefer. For our installation, we downloaded a community version of Visual Studio 2022 for as it’s the latest supported version for our Windows 11 system. 
  • Run the installer to complete the installation

    Follow the succeeding prompts until you get to the Visual Studio installer. It will ask you for a couple of things such as install directory and will check a couple of dependencies. Afterwards, you should be able to launch Visual Studio from this Window or from a shortcut.

Installing the CUDA Toolkit

With Visual Studio now installed, you will need the CUDA Toolkit download for Visual Studio. It provides the tools, libraries, and compiler (nvcc) needed to develop and run CUDA applications within Visual Studio. It enables integration for GPU-accelerated computing, which allows use of NVIDIA GPUs for high-performance tasks.

  • Verify you have a CUDA-compatible GPU
    To ensure smooth operations, first check if your current GPU is a supported device. You can do this by navigating to the Display Adapters section in the Windows Device Manager. For more information, visit NVIDIA’s install guide. 
  • Download CUDA Toolkit from NVIDIA

    Visit NVIDIA’s website to download and learn more about the toolkit. Before downloading, ensure that you have chosen the correct OS, version, etc. The download file in our case is 3.2 GB but please ensure you have at least 10 GB of free space as you still need to temporarily extract the installation files before running the installer.

  • Run the installer

    After downloading, run the installer. It will scan your device for any missing dependencies or pre-existing installs and adjust your installation files accordingly. Afterwards, you will now have the CUDA Toolkit installed on your system. Additionally, NSIGHT which provides debugging and profiling features specific for CUDA applications will also be installed.

    If you encounter any issues with installing the toolkit, consult NVIDIA’s installation and troubleshooting guide.

    Bonus tip: If you prefer Visual Studio Code, you should install Nsight from this link instead. It’s an application development environment for “heterogeneous platforms that brings CUDA development for GPUs” into Microsoft’s Visual Studio code instead.

Getting started with your first CUDA project in Visual Studio

After installing both Visual Studio and the CUDA toolkit, you are now ready to initialize your first project within Visual Studio.

  • Creating a new project.
    Start by opening Visual Studio and create a new project or clone an existing repository to start your first project file.
  • Initializing your project.

    At this point you have two options: either start a completely blank console/project or choose the CUDA 12.8 project. The main difference is that the CUDA Runtime comes pre-equipped with the usual workloads, sample code, and use cases.However, starting from scratch allows you to configure your project with only what you need and it also familiarizes you with the workspace. For this project, we’ll start with a completely blank project.
  • Setting your build configuration

    On the top of the Visual Studio window, choose Release and x64 (if you’re running a 64-bit system). This tells VS that we’re trying to build a version of an app that can be deployed, as opposed to debugging. 
  • Adjusting build dependencies

    You need to ensure that Visual Studio knows that you’re trying to build and execute CUDA files. To configure this, right click on your project name (“CUDA Sample”) and click on Build Dependencies ? Build Customizations. A new window will pop up that lists down available build customization files—be sure to tick CUDA 12.8 and hit ok.

  • Adding a CUDA C++ or Header file

    To add new source files, simply add new items as you would add any normal .cpp or .header file. Right click on a folder and click on AddNew Item to access your file options. 
  • Verifying file and project setup is correct
    At this point, we suggest trying to build a solution to ensure that everything is working smoothly. If nothing breaks, congratulations! You can now start working on your first CUDA file inside VS. NVIDIA also provides a few sample projects so you can test, debug, and familiarize with the setup using existing projects before creating a new one entirely.

Optimizing your setup

VS and NVIDIA have made giant strides in making CUDA development easier to access and set up. However, as CUDA is a proprietary language, there may still be some missing syntax highlighting or confused prompts from VS’s IntelliSense. 

To alleviate this, it is recommended to install supplementary plugins from the Visual Studio marketplace that can help with properly highlighting symbols. For example, you can download and install the Visual Assist plugin which adds support for CUDA-specific code that Visual Studio’s IntelliSense might not recognize yet. It also comes with the added benefit of providing its core features of navigation, refactoring, code assistance, and more, on top of the added support for .cu and .cuh files.

visual assist for C++ CUDA development

The Visual Assist plugin adds support for recognizing CUDA-specific code. VA recognizes you are using a symbol that references a missing header file and adds it for you.

Conclusion

While CUDA is a powerful tool that is likely to remain significant in the near future, the landscape of parallel computing is dynamic, and its dominance will depend on technological advancements and shifts in industry needs. But given the rapid growth of AI and machine learning, CUDA is likely to remain relevant due to its optimization for deep learning tasks, especially as NVIDIA continues to innovate in this space. 

In summary, if you’re looking to expand on your software development skills into a growing and forthcoming space, then learning CUDA could be it for you. 

Related posts
Build Announcements

Visual Assist 2024.9 release post

Webinar Recap

Test Driven-Development and UI/UX Design: A Practical Guide [Webinar Recap]

Success Story

Success Story: Visual Assist for modeling and simulation software for automotive C++

Tips and Tricks

Installing Virtual Machines to use Visual Studio on Mac

Leave a Reply

%d