Commit 6b241089 authored by gbalduzz's avatar gbalduzz
Browse files

Created and tested set and get affinity methods.

parent 79964d09
Loading
Loading
Loading
Loading
+32 −0
Original line number Diff line number Diff line
// Copyright (C) 2018 ETH Zurich
// Copyright (C) 2018 UT-Battelle, LLC
// All rights reserved.
//
// See LICENSE for terms of usage.
// See CITATION.md for citation guidelines, if DCA++ is used for scientific publications.
//
// Author: Giovanni Balduzzi(gbalduzz@itp.phys.ethz.ch)
//
// These methods allow to retrieve and set the list of cores the current thread has affinity to.

#ifndef DCA_PARALLEL_STDTHREAD_THREAD_POOL_AFFINITY_HPP
#define DCA_PARALLEL_STDTHREAD_THREAD_POOL_AFFINITY_HPP

#include <vector>

namespace dca {
namespace parallel {
// dca::parallel::

// Returns a list of cores id for which the calling thread has affinity.
std::vector<int> get_affinity();

void set_affinity(const std::vector<int>& cores);

// Number of cores used by this process.
int get_core_count();

}  // namespace parallel
}  // namespace dca

#endif  // DCA_PARALLEL_STDTHREAD_THREAD_POOL_AFFINITY_HPP
+3 −0
Original line number Diff line number Diff line
@@ -70,6 +70,9 @@ private:
  std::vector<std::unique_ptr<std::condition_variable>> condition_;
  std::atomic<bool> stop_;
  std::atomic<unsigned int> active_id_;

  int core_count_;
  std::vector<int> master_affinity_;
};

template <class F, class... Args>
+3 −3
Original line number Diff line number Diff line
#// Copyright (C) 2018 ETH Zurich
// Copyright (C) 2018 ETH Zurich
// Copyright (C) 2018 UT-Battelle, LLC
// All rights reserved.
//
@@ -6,9 +6,9 @@
// See CITATION.md for citation guidelines, if DCA++ is used for scientific publications.
//
// Author: Peter Staar (taa@zurich.ibm.com)
//         Giovani Balduzzi(gbalduzz@itp.phys.ethz.ch)
//         Giovanni Balduzzi(gbalduzz@itp.phys.ethz.ch)
//
// This class performs the coarsegraining of single-particle functions.
// This class performs the coarse-graining of single-particle functions.

#ifndef DCA_PHYS_DCA_STEP_CLUSTER_MAPPING_COARSEGRAINING_COARSEGRAINING_SP_HPP
#define DCA_PHYS_DCA_STEP_CLUSTER_MAPPING_COARSEGRAINING_COARSEGRAINING_SP_HPP
+2 −1
Original line number Diff line number Diff line
# parallel stdthread
add_library(parallel_stdthread STATIC stdthread.cpp thread_pool/thread_pool.cpp)
add_library(parallel_stdthread STATIC stdthread.cpp
                                      thread_pool/thread_pool.cpp thread_pool/affinity.cpp)
+74 −0
Original line number Diff line number Diff line
// Copyright (C) 2018 ETH Zurich
// Copyright (C) 2018 UT-Battelle, LLC
// All rights reserved.
//
// See LICENSE for terms of usage.
// See CITATION.md for citation guidelines, if DCA++ is used for scientific publications.
//
// Author: Giovanni Balduzzi(gbalduzz@itp.phys.ethz.ch)
//
// This file implements the methods in affinity.hpp.

#include "dca/parallel/stdthread/thread_pool/affinity.hpp"

#include <iostream>
#include <cstdlib>

// GNU extensions are required for linux-specific features for querying affinity
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif

#include <sched.h>
#include <stdexcept>

namespace dca {
namespace parallel {
// dca::parallel::

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

  auto status = sched_getaffinity(0, sizeof(cpu_set_t), &cpu_set_mask);

  if (status == -1) {
    throw(std::runtime_error("Unable to get thread affinity."));
  }

  auto cpu_count = CPU_COUNT(&cpu_set_mask);

  std::vector<int> cores;
  cores.reserve(cpu_count);

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

  if (cores.size() != cpu_count) {
    throw(std::logic_error("Core count mismatch."));
  }

  return cores;
}

void set_affinity(const std::vector<int>& cores) {
  cpu_set_t cpu_set_mask;
  CPU_ZERO(&cpu_set_mask);

  for (int core : cores) {
    CPU_SET(core, &cpu_set_mask);
  }

  sched_setaffinity(0, sizeof(cpu_set_t), &cpu_set_mask);
}

int get_core_count() {
  cpu_set_t cpu_set_mask;
  sched_getaffinity(0, sizeof(cpu_set_t), &cpu_set_mask);
  return CPU_COUNT(&cpu_set_mask);
}

}  // namespace parallel
}  // namespace dca
Loading