top of page

Efficient Containerized Fortran and CUDA Development with Docker in VS Code

  • Writer: Kasturi Murthy
    Kasturi Murthy
  • Oct 15
  • 4 min read

In the current high-performance computing environment, developers require tools that are both powerful and easy to configure. Fortran and CUDA remain essential for scientific and engineering applications but integrating them can be complex and error prone. Docker addresses this issue by offering a consistent, portable environment that packages code, compilers, libraries, and system tools, thus resolving the typical “it works on my machine” problem. This blog post builds on recent research supporting Fortran’s ongoing importance in scientific computing [1]. Another reason for undertaking this is to revive the code I used for my PhD thesis.

This document demonstrates the application of NVIDIA's `nvfortran` compiler to interface with GPUs through structured JSON and YAML configuration files. The entire process is conducted within a containerized development workflow, facilitated by Docker and Visual Studio Code, and is initiated seamlessly from a Windows Subsystem for Linux (WSL) prompt.

Understanding Docker and Its Benefits

Docker is a platform that streamlines the deployment of applications within lightweight, portable containers. These containers encompass everything required for your application to function, such as code, runtime, libraries, and system tools. Here are some key benefits of using Docker:

  • Code and Dependencies Together: Typically, running code on different computers can lead to issues due to variations in library versions, tools, or even the operating system. Docker addresses this by allowing you to “package” your code along with all its dependencies into a single container.

  • Lightweight and Portable: Unlike virtual machines, containers do not require a full operating system. They share the host system’s kernel, making them much more efficient and quicker to start.

  • Consistency Across Environments: Whether you run your container on your laptop, a colleague’s computer, or a cloud server, it will function exactly the same way. This eliminates the classic “it works on my machine” problem.

  • Scientific Sandbox: The term “scientific sandbox” refers to a controlled, isolated environment where you can experiment, develop, and test your scientific code without affecting other projects or system settings. You determine precisely what goes into the container—specific compilers, libraries, tools, and even data files.

Setting Up Docker for Fortran and CUDA Development

To get started with Docker, you need to install it on your machine. Follow these straightforward steps:

  1. Download Docker: Visit the Docker website and download the version suitable for your operating system. Windows 11 with WSL used in this demonstration

  2. Install Docker: Follow the installation instructions specific to your OS. If you are using Windows, enable the WSL 2 feature during installation.

  3. Verify Installation: Open a terminal and run `docker --version` to confirm that Docker is correctly installed.

The docker-compose.yml file:

  • For establishing the nvfortran-dev service as defined in your .devcontainer folder

  • Defining the base Docker image as nvcr.io/nvidia/nvhpc:24.3-devel-cuda12.3-ubuntu22.04, which includes the Ubuntu OS, the nvfortran compiler, and all required NVIDIA libraries.

  • Setting up GPU access by configuring the runtime: NVidia and environment variables such as NVIDIA_VISIBLE_DEVICES.

  • Linking your local project folder to the container's workspace directory with a volume, allowing local file editing and execution within the container.

  • Ensuring the container remains active so that VS Code stays connected to it.

services:
  nvfortran-dev:
    image: nvcr.io/nvidia/nvhpc:24.3-devel-cuda12.3-ubuntu22.04
    container_name: nvfortran-dev
    runtime: nvidia
    environment:
      - NVIDIA_VISIBLE_DEVICES=all
      - NVIDIA_DRIVER_CAPABILITIES=compute,utility
    volumes:
      - ..:/workspace
    working_dir: /workspace
    command: /bin/bash
    stdin_open: true
    tty: true

Connecting to VS Code: devcontainer.json

To enable this container for use within Visual Studio Code, I create a devcontainer.json file. This file instructs VS Code on how to:

  • Start the container

  • Mount my workspace

  • Install useful extensions

The content of the devcontainer.json file is as follows:

{
  "name": "NVIDIA Fortran Dev",
  "dockerComposeFile": "docker-compose.yml",
  "service": "nvfortran-dev",
  "workspaceFolder": "/workspace",
  "settings": {
    "files.associations": {
      "*.f90": "fortran",
      "*.f95": "fortran",
      "*.F95": "fortran",
      "*.for": "fortran",
      "*.FOR": "fortran",
      "*.f": "fortran",
      "*.F": "fortran"
    }
  },
  "extensions": [
    "fortran-lang.fortran",
     "ms-vscode.remote-containers"
  ],
  "remoteUser": "root"
}

This devcontainer.json file sets up a Visual Studio Code development container for Fortran programming using NVIDIA's tools.

Here is an explanation of each key in the JSON file:

  • name: "NVIDIA Fortran Dev" is the display name for the development environment that will show up in the VS Code interface.

  • dockerComposeFile: Indicates that the container configuration is defined in the docker-compose.yml file located in the same directory.

  • service: Tells VS Code to connect to the nvfortran-dev service specified in your docker-compose.yml file.

  • workspaceFolder: Defines the default directory that will open in VS Code when you connect to the container, which is workspace.

  • settings: Configures VS Code settings that apply only when working inside this container.

    • files.associations: Ensures that files ending in .f90 and .f95 are identified as Fortran files, allowing for proper syntax highlighting and language features.

  • extensions: A list of VS Code extensions that will be automatically installed and activated inside the container.

    • hansec.fortran: Offers Fortran language support (syntax highlighting, snippets, etc.). However, I do not see syntax highlights working for me.

    • ms-vscode.remote-containers: The essential extension that enables the development container feature.

  • remoteUser: Specifies that commands and processes inside the container will be executed as the root user.

Proceed with the following steps

  • Modify your devcontainer.json file located in [.devcontainer/devcontainer.json] by adding the extended files associations block. Please refer to IDE figure below

  • Access the Command Palette using Ctrl+Shift+P.

  • Execute the Dev Containers: Rebuild Container command in VS Code, which will recreate and reconnect your development environment using the configuration from your .devcontainer folder. During this process, it will:

    • Initiate the nvfortran-dev service (utilizing the NVIDIA HPC SDK image with nvfortran and CUDA support)

    • Configure the container with all designated settings and extensions, and link your VS Code session to this active container, providing a fully configured nvfortran environment within Docker.

    • Consequently, after executing the Dev Containers: Rebuild Container command, you will be operating within the Docker-based nvfortran environment as outlined by your configuration files.

VS Code IDE on a Windows system using WSL, displaying a Fortran program set up for GPU benchmarking. The terminal confirms (refer figure below) successful GPU passthrough, showing one available OpenMP device with default device ID zero. Refer the following screenshot of Terminal Output
VS Code IDE on a Windows system using WSL, displaying a Fortran program set up for GPU benchmarking. The terminal confirms (refer figure below) successful GPU passthrough, showing one available OpenMP device with default device ID zero. Refer the following screenshot of Terminal Output
Details from the Terminal Output
Details from the Terminal Output

References

  1. McKevitt, James, Vorobyov, Eduard I., and Kulikov, Igor. “Accelerating Fortran codes: A method for integrating Coarray Fortran with CUDA Fortran and OpenMP.” Journal of Parallel and Distributed Computing, vol. 195, Elsevier BV, January 2025, article 104977. DOI: 10.1016/j.jpdc.2024.104977. Also available as a preprint at arXiv:2409.02294.

Comments


  • Facebook
  • Twitter
  • LinkedIn

©2018 by Indriya. Proudly created with Wix.com

bottom of page