Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 88 additions & 0 deletions kokkos-dev-container/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
## Kokkos with OpenMP parallel host backend in VS Code with a Dev Container

We will see how to develop in VS Code using a Docker container based development environment that involves C++ development using the Kokkos library with OpenMP as the parallel programming backend on the CPU.

#### Steps to follow
* Installation of Visual Studio Code.
* Installation of necessary extensions in VS Code. See below for details.
* Installation of a container management tool like Docker or Podman.
* Opening the directory `devc_hellow` in VS Code.
* VS Code may automatically ask you to reopen the project in a container. Otherwise, you could manually do so by opening the Command Palette (Ctrl+Shift+P) and entering ">Dev Containers: Reopen in Container".
* If all goes well, all the requirements would be installed including a C++ compiler, CMake and Kokkos. A "build" directory would also be generated by CMake within the C++ project.

#### The development environment
* Ubuntu 22.04 is the base image of the container.
* The C++ compiler is GCC, installed via `build-essential`.
* Kokkos version is 4.6.01, with OpenMP parallel host backend enabled.

#### Here are further notes to get a working system

The steps are for a system with Ubuntu 22.04 as the OS.

##### <ins>Installation and use of Docker
[How to install and use Docker on Ubuntu 22.04](https://www.digitalocean.com/community/tutorials/how-to-install-and-use-docker-on-ubuntu-22-04)

Give the user [permission](https://stackoverflow.com/questions/48957195/how-to-fix-docker-permission-denied) to run Docker.

Ensure that you haven’t installed different parts of Docker in different ways (say, some via snap and some via apt) or have multiple installations of Docker (one via snap and one via apt). This will lead to error while trying to rebuild the container from inside VS Code. A [few](https://forums.docker.com/t/can-not-stop-docker-container-permission-denied-error/41142) [references](https://stackoverflow.com/questions/47223280/docker-containers-can-not-be-stopped-or-removed-permission-denied-error) [about](https://stackoverflow.com/questions/71477749/error-response-from-daemon-cannot-kill-container-permission-denied-how-to-kil) this point.

Docker is the default container management tool expected by VS Code. Make sure the Docker daemon is running in order to use Docker.

##### <ins>Installation and use of Podman

Docker requires the daemon to be running in the background. In order to start the daemon, one has to either be administrative privileges or have [rootless mode](https://docs.docker.com/engine/security/rootless/) enabled. Podman presents a solution for a user to use a system in which the necessary software already installed (VS Code and Podman), without administrative privileges.

See [here](https://code.visualstudio.com/remote/advancedcontainers/docker-options#_podman) for further information on using Podman with VS Code. The steps involved are:
* [Installation](https://podman.io/docs/installation) of Podman.
* In VS Code: In Settings, changing the setting `Dev > Containers: Docker Path` from `docker` to `podman`.

See [other](https://phoenixnap.com/kb/install-podman-on-ubuntu) resources also about installing and using Podman.

Ensure that you have stopped the Docker daemon, in case you have Docker also installed in your system.

[User namespaces have to be enabled](https://stackoverflow.com/questions/56296932/run-privileged-podman-without-sudo-and-without-usernamespace/) in order to fix the following error:

cannot clone: No space left on device
user namespaces are not enabled in /proc/sys/user/max_user_namespaces
Error: cannot re-exec process

##### <ins>About the Dev Container

The relevant files are `Dockerfile` and `devcontainer.json` in the directory `.devcontainer`. `Dockerfile` describes the Docker image to be built. `devcontainer.json` is used to describe the settings of the Dev Container beyond what is in the image. For example, the settings needed to build the image (like the network connection to access the internet), the settings needed to run the container and what extensions to install in VS Code while working inside the container.

##### <ins>Installation of extensions

The user needs to install the following extensions in host VS Code:
* “Dev Containers” extension. Note that the “Remote Development” extension contains “Dev Containers” too.
* “Docker” extension. Currently this extension will be installed in the Dev Container environment also. Note that the "Docker" extension is going to become "Container Tools" extension.

Note: “C/C++ Extension Pack” will be installed in the Dev Container environment. No harm if they are both in the host and the remote.

##### <ins>About CMake based project

There is a simple C++ project that links with [Kokkos](https://cexa-project.org/news/2025-03-19-ninth-kokkos-tea-time/) to be built and run in `devc_hellow`. One can see in the `CMakeLists.txt` file that CMake expects Kokkos to be installed, in order to build the project. The container image takes care of this requirement.

In `devcontainer.json`, one can specify a "postStartCommand". For e.g., it is defined as "cmake -DKokkos_ROOT=/opt/kokkos-install -DKokkos_ENABLE_OPENMP=ON -B build" in our case. This ensures that a `build` directory is generated by CMake. See below for some observations with Docker and Podman regarding "postStartCommand".

#### Observations

* Currently, "postStartCommand" works only with Docker and not with Podman. One gets an error like the following with Podman:

Error: error parsing environment variables: name "\afbfa9712-93dc-4bca-8ecc-99ffb315ae27\x1b]0;vscode@mdlspc111.extra.cea.fr: cat /proc/self/environ\aREMOTE_CONTAINERS_IPC" has white spaces, poorly formatted name
[147463 ms] postStartCommand from devcontainer.json failed with exit code 125. Skipping any further user-provided commands.
* Podman takes more time to build and run the container. Probably, it takes more time to restart a container also.
* With Podman, VS Code disconnects and reconnects to the container from time to time.

Probably, these issues with Podman can be resolved by using a more recent version of Podman. For example, VS Code recommends version 5+. However, Podman offers much better support for Red Hat distributions like Fedora.

#### Some technical points
* In order to enable networking while building the container, define `"build"` as `"build": { …, "options": ["--network=host"], …}`. This may be needed in order to `git clone` the Kokkos repository. Also, note that `“runArgs”: [“--network=host”]` is for enabling network connection while running the container.
* Setting the environment variable `CPATH` as `CPATH="/opt/kokkos-install/include:$CPATH"` helps with IntelliSense recognizing Kokkos header files.
* In the Command Palette enter ">Dev Containers: Rebuild Without Cache and Reopen in Container" whenever you need to rebuild the container after making changes to either `devcontainer.json` or `Dockerfile`.

#### To explore further
* Kokkos functions are incorrectly flagged by IntelliSense.
* Defining a non-root [USER](https://www.docker.com/blog/understanding-the-docker-user-instruction/) in `Dockerfile`.
* "postStartCommand" vs "postCreateCommand" to generate `build` directory.
* Try installing the following extensions in VS Code to check for a better experience with Podman.
* "Container Tools" and "Pod Manager". Note: These extensions have been released relatively recently.
22 changes: 22 additions & 0 deletions kokkos-dev-container/devc_hellow/.devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
FROM mcr.microsoft.com/vscode/devcontainers/cpp:ubuntu-22.04

# Install C++ development tools, Git and CMake
RUN apt-get update
RUN apt-get -y install \
build-essential \
gdb \
git \
cmake

RUN git clone https://github.com/kokkos/kokkos.git \
--branch release-candidate-4.6.01 --depth=1 /opt/kokkos && \
cd /opt/kokkos && \
mkdir build && cd build && \
cmake .. -DCMAKE_INSTALL_PREFIX=/opt/kokkos-install \
-DKokkos_ENABLE_OPENMP=ON \
-DKokkos_ENABLE_SERIAL=ON \
-DCMAKE_CXX_STANDARD=17 && \
cmake --build . --parallel $(( $(nproc) / 2)) && \
cmake --build . --target install

ENV CPATH="/opt/kokkos-install/include:$CPATH"
24 changes: 24 additions & 0 deletions kokkos-dev-container/devc_hellow/.devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "Kokkos with OpenMP backend",
"build": {
"dockerfile": "Dockerfile",
"options": ["--network=host"]
},
"runArgs": [
"--network=host"
],
"customizations": {
"vscode": {
"extensions": [
"ms-vscode.cpptools-extension-pack",
"ms-azuretools.vscode-docker"
],
"settings": {
"C_Cpp.default.includePath": [
"/opt/kokkos-install/include/"
]
}
}
},
"postStartCommand": "cmake -DKokkos_ROOT=/opt/kokkos-install -DKokkos_ENABLE_OPENMP=ON -B build"
}
13 changes: 13 additions & 0 deletions kokkos-dev-container/devc_hellow/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
cmake_minimum_required(VERSION 3.21)

project(KokkosWithOpenMP)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(OpenMP REQUIRED)

find_package(Kokkos 4.6 REQUIRED CONFIG)

add_executable(toy_kokkos src/toy_kokkos.cpp)
target_link_libraries(toy_kokkos PRIVATE Kokkos::kokkos)
15 changes: 15 additions & 0 deletions kokkos-dev-container/devc_hellow/src/toy_kokkos.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include <iostream>

#include <Kokkos_Core.hpp>

int main() {
Kokkos::initialize();
{
Kokkos::parallel_for(10, KOKKOS_LAMBDA (int i) {
std::cout << "Hello world: " << i << std::endl;
});
}
Kokkos::finalize();

return 0;
}