Skip to content
Snippets Groups Projects
  • Sam Yates's avatar
    Split out 'arborenv' as an installable library from the sup library. (#679) · f4b2e034
    Sam Yates authored and Benjamin Cumming's avatar Benjamin Cumming committed
    Make a new installed library `libarborenv.a` covering a subset of the `sup` library functionality, with corresponding installed CMake target `arbor::arborenv`.
    
    * Move NVML or CUDA 10 API decision for GPU UUID discovery to top level CMake.
    * Move affinity, concurrency, MPI init guard, and gpu detection and negotiation functionality out of `sup` and into new library `arborenv`.
    * Move `include/arbor` in project tree to `arbor/include/arbor` (for consistency across `sup`, `arbor`, and `arborenv` subdirectories.)
    * Wrangle more explicit library dependency adding CMake code into the installed `arbor-config.cmake`, to help mitigate [CMake issue #18614](https://gitlab.kitware.com/cmake/cmake/issues/18614).
    * Have `arborenv` code throw `std::runtime_exception` instead of `arb::arbor_error`. (We are still using `arb::mpi_error` though for a failure in `with_mpi`.)
    * Move `scope_exit` into the `arb::util` namespace.
    * Merge `affinity.hpp` into `concurrency.hpp`.
    * Rename `gpu.hpp` to `gpu_env.hpp` in `arborenv` includes.
    
    Fixes #647.
    f4b2e034
affinity.cpp 793 B
#include <cstdlib>
#include <system_error>
#include <vector>

#ifdef __linux__

#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif

extern "C" {
#include <sched.h>
}

namespace arbenv {

std::vector<int> get_affinity() {
    std::vector<int> cores;
    cpu_set_t cpu_set_mask;

    int status = sched_getaffinity(0, sizeof(cpu_set_t), &cpu_set_mask);
    if (status) {
        throw std::system_error(errno, std::generic_category());
    }

    for (int i=0; i<CPU_SETSIZE; ++i) {
        if (CPU_ISSET(i, &cpu_set_mask)) {
            cores.push_back(i);
        }
    }

    return cores;
}

} // namespace arbenv

#else // def __linux__

// No support for non-linux systems.
namespace arbenv {

std::vector<int> get_affinity() {
    return {};
}

} // namespace arbenv

#endif // def __linux__