Skip to content

Commit

Permalink
Restructure and include multi stage docker build
Browse files Browse the repository at this point in the history
  • Loading branch information
Eric Kern committed Apr 6, 2024
1 parent 8f564ff commit 9d6e8fc
Show file tree
Hide file tree
Showing 8 changed files with 233 additions and 28 deletions.
46 changes: 46 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"files.associations": {
"array": "cpp",
"string_view": "cpp",
"initializer_list": "cpp",
"utility": "cpp",
"string": "cpp",
"atomic": "cpp",
"bit": "cpp",
"*.tcc": "cpp",
"cctype": "cpp",
"clocale": "cpp",
"cmath": "cpp",
"compare": "cpp",
"concepts": "cpp",
"cstdarg": "cpp",
"cstddef": "cpp",
"cstdint": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cwchar": "cpp",
"cwctype": "cpp",
"deque": "cpp",
"unordered_map": "cpp",
"vector": "cpp",
"exception": "cpp",
"algorithm": "cpp",
"functional": "cpp",
"iterator": "cpp",
"memory": "cpp",
"memory_resource": "cpp",
"numeric": "cpp",
"random": "cpp",
"system_error": "cpp",
"tuple": "cpp",
"type_traits": "cpp",
"iosfwd": "cpp",
"limits": "cpp",
"new": "cpp",
"numbers": "cpp",
"ostream": "cpp",
"stdexcept": "cpp",
"streambuf": "cpp",
"typeinfo": "cpp"
}
}
9 changes: 0 additions & 9 deletions CMakeLists.txt

This file was deleted.

8 changes: 8 additions & 0 deletions WhatNuma/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
cmake_minimum_required(VERSION 3.22)
project(Numa_Analysis)

find_library(NUMA_LIBRARY numa)
find_package(OpenMP COMPONENTS CXX REQUIRED)

add_executable(cores.out ${CMAKE_CURRENT_SOURCE_DIR}/src/cores.cpp)
target_link_libraries(cores.out PRIVATE OpenMP::OpenMP_CXX ${NUMA_LIBRARY})
78 changes: 78 additions & 0 deletions WhatNuma/src/cores.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#include <cstdio>
#include <omp.h>
#include <numa.h> // for other numa stuff
#include <numaif.h> // for get_mempolicy
#include <sched.h>

#include <vector>
#include <memory>
// Allocator adaptor that interposes construct() calls to
// convert value initialization into default initialization.
template <typename T, typename A=std::allocator<T>>
class default_init_allocator : public A {
typedef std::allocator_traits<A> a_t;
public:
template <typename U> struct rebind {
using other =
default_init_allocator<
U, typename a_t::template rebind_alloc<U>
>;
};

using A::A;

template <typename U>
void construct(U* ptr)
noexcept(std::is_nothrow_default_constructible<U>::value) {
::new(static_cast<void*>(ptr)) U;
}
template <typename U, typename...Args>
void construct(U* ptr, Args&&... args) {
a_t::construct(static_cast<A&>(*this),
ptr, std::forward<Args>(args)...);
}
};

int main(int argc, char const *argv[])
{
if (numa_available() < 0) {
std::printf("NUMA not available on this system\n");
return 1;
}

std::printf("CPU/Thread/NUMA-Node distribution\n\n");
#pragma omp parallel
{
int tid = omp_get_thread_num();
// int cpu = sched_getcpu();
uint cpu0, node0;
getcpu(&cpu0, &node0);
#pragma omp critical
{
std::printf("CPU: %d, Thread: %d, Node: %d", cpu0, tid, node0);
}
}

std::vector<int, default_init_allocator<int>> thread_data{omp_get_max_threads()};
#pragma omp parallel
{
int tid = omp_get_thread_num();
thread_data[tid] = 0;
}
std::printf("Vector initialization complete\n");

for (size_t i = 0; i < thread_data.size(); ++i)
{
int* data = thread_data.data() + i;
int node = -1;
int status = get_mempolicy(&node, nullptr, 0, data, MPOL_F_NODE | MPOL_F_ADDR);
if (status < 0) {
std::printf("Failed to get memory policy");
return 1;
}
std::printf("i: %03ld, NUMA node: %d", i, node);
}


return 0;
}
30 changes: 30 additions & 0 deletions build_container/Docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
FROM gcc:11.4.0 AS builder

ARG REINSTALL_CMAKE_VERSION_FROM_SOURCE="3.29.1"

COPY ./build_container/Docker/reinstall-cmake.sh /tmp/

RUN mkdir -p /tmp/WhatNuma
COPY ./WhatNuma/ /tmp/WhatNuma/

RUN ls /tmp/ && ls /tmp/WhatNuma

RUN if [ "${REINSTALL_CMAKE_VERSION_FROM_SOURCE}" != "none" ]; then \
chmod +x /tmp/reinstall-cmake.sh && /tmp/reinstall-cmake.sh ${REINSTALL_CMAKE_VERSION_FROM_SOURCE}; \
fi \
&& rm -f /tmp/reinstall-cmake.sh

RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
&& apt-get -y install --no-install-recommends numactl libnuma-dev

RUN mkdir build && cmake -S "/tmp/WhatNuma" -B "/build" \
&& cmake --build "/build"

FROM ubuntu:22.04 AS runtime

RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
&& apt-get -y install --no-install-recommends libnuma-dev libgomp1\
&& apt-get autoremove -y && apt-get clean -y && rm -rf /var/lib/apt/lists/*

COPY --from=builder /build/cores.out .
CMD ["/cores.out"]
59 changes: 59 additions & 0 deletions build_container/Docker/reinstall-cmake.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#!/usr/bin/env bash
#-------------------------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See https://go.microsoft.com/fwlink/?linkid=2090316 for license information.
#-------------------------------------------------------------------------------------------------------------
#
set -e

CMAKE_VERSION=${1:-"none"}

if [ "${CMAKE_VERSION}" = "none" ]; then
echo "No CMake version specified, skipping CMake reinstallation"
exit 0
fi

# Cleanup temporary directory and associated files when exiting the script.
cleanup() {
EXIT_CODE=$?
set +e
if [[ -n "${TMP_DIR}" ]]; then
echo "Executing cleanup of tmp files"
rm -Rf "${TMP_DIR}"
fi
exit $EXIT_CODE
}
trap cleanup EXIT


echo "Installing CMake..."
# apt-get -y purge --auto-remove cmake
mkdir -p /opt/cmake

architecture=$(dpkg --print-architecture)
case "${architecture}" in
arm64)
ARCH=aarch64 ;;
amd64)
ARCH=x86_64 ;;
*)
echo "Unsupported architecture ${architecture}."
exit 1
;;
esac

CMAKE_BINARY_NAME="cmake-${CMAKE_VERSION}-linux-${ARCH}.sh"
CMAKE_CHECKSUM_NAME="cmake-${CMAKE_VERSION}-SHA-256.txt"
TMP_DIR=$(mktemp -d -t cmake-XXXXXXXXXX)

echo "${TMP_DIR}"
cd "${TMP_DIR}"

curl -sSL "https://github.com/Kitware/CMake/releases/download/v${CMAKE_VERSION}/${CMAKE_BINARY_NAME}" -O
curl -sSL "https://github.com/Kitware/CMake/releases/download/v${CMAKE_VERSION}/${CMAKE_CHECKSUM_NAME}" -O

sha256sum -c --ignore-missing "${CMAKE_CHECKSUM_NAME}"
sh "${TMP_DIR}/${CMAKE_BINARY_NAME}" --prefix=/opt/cmake --skip-license

ln -s /opt/cmake/bin/cmake /usr/local/bin/cmake
ln -s /opt/cmake/bin/ctest /usr/local/bin/ctest
12 changes: 12 additions & 0 deletions build_container/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash

# Define your variables
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
echo $SCRIPT_DIR

IMAGE_NAME="your_image_name:first"
DOCKERFILE_PATH="${SCRIPT_DIR}/Docker/Dockerfile"
BUILD_CONTEXT="${SCRIPT_DIR}/.."

# Build the Docker image
docker build -t "$IMAGE_NAME" -f "$DOCKERFILE_PATH" "$BUILD_CONTEXT"
19 changes: 0 additions & 19 deletions src/cores.cpp

This file was deleted.

0 comments on commit 9d6e8fc

Please sign in to comment.