Add API to set thread affinity on Linux.

Issue: #2545
This commit is contained in:
Sharvil Nanavati 2020-04-08 12:47:41 -07:00
parent 69f277f8ee
commit 7b4773b24d
3 changed files with 28 additions and 0 deletions

View File

@ -25,6 +25,11 @@ char* openblas_get_config(void);
/*Get the CPU corename on runtime.*/
char* openblas_get_corename(void);
#ifdef OPENBLAS_OS_LINUX
/* Sets thread affinity for OpenBLAS threads. `thread_idx` is in [0, openblas_get_num_threads()-1]. */
int openblas_setaffinity(int thread_idx, size_t cpusetsize, cpu_set_t* cpu_set);
#endif
/* Get the parallelization type which is used by OpenBLAS */
int openblas_get_parallel(void);
/* OpenBLAS is compiled for sequential use */

View File

@ -72,6 +72,7 @@ USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "common.h"
#if defined(OS_LINUX) || defined(OS_NETBSD) || defined(OS_DARWIN) || defined(OS_ANDROID) || defined(OS_SUNOS) || defined(OS_FREEBSD) || defined(OS_OPENBSD) || defined(OS_DRAGONFLY) || defined(OS_HAIKU)
#include <dlfcn.h>
#include <errno.h>
#include <signal.h>
#include <sys/resource.h>
#include <sys/time.h>
@ -279,6 +280,23 @@ int get_node(void);
static int increased_threads = 0;
#ifdef OS_LINUX
int openblas_setaffinity(int thread_idx, size_t cpusetsize, cpu_set_t* cpu_set) {
const int active_threads = openblas_get_num_threads();
if (thread_idx < 0 || thread_idx >= active_threads) {
errno = EINVAL;
return -1;
}
pthread_t thread = (thread_idx == active_threads - 1)
? pthread_self()
: blas_threads[thread_idx];
return pthread_setaffinity_np(thread, cpusetsize, cpu_set);
}
#endif
static void* blas_thread_server(void *arg){
/* Thread identifier */

View File

@ -91,3 +91,8 @@ typedef int blasint;
#define openblas_complex_xdouble_real(z) ((z).real)
#define openblas_complex_xdouble_imag(z) ((z).imag)
#endif
/* Inclusion of Linux-specific header is needed for definition of cpu_set_t. */
#ifdef OPENBLAS_OS_LINUX
#include <sched.h>
#endif