From 6333fdecf3a4687ddab3bccec6d78072bd67faf9 Mon Sep 17 00:00:00 2001 From: David Yat Sin Date: Fri, 1 Dec 2023 20:42:33 +0000 Subject: [PATCH] Use pthread_setaffinity_np On some systems, pthread_addr_setaffinity_np does not exist, so we need to use pthread_setaffinity_np on thread after pthread_create Provided by Julian Samaroo on github https: //github.com/RadeonOpenCompute/ROCR-Runtime/pull/143 Change-Id: I4649f94333f2d7b0a5993b370a4bfc48d92acecb --- .../hsa-runtime/core/util/lnx/os_linux.cpp | 40 +++++++++---------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/runtime/hsa-runtime/core/util/lnx/os_linux.cpp b/runtime/hsa-runtime/core/util/lnx/os_linux.cpp index bdf76657eb..54a9c99ffc 100644 --- a/runtime/hsa-runtime/core/util/lnx/os_linux.cpp +++ b/runtime/hsa-runtime/core/util/lnx/os_linux.cpp @@ -110,26 +110,7 @@ class os_thread { return; } } - - if (core::Runtime::runtime_singleton_->flag().override_cpu_affinity()) { - int cores = get_nprocs_conf(); - cpu_set_t* cpuset = CPU_ALLOC(cores); - if (cpuset == nullptr) { - fprintf(stderr, "CPU_ALLOC failed: %s\n", strerror(errno)); - return; - } - CPU_ZERO_S(CPU_ALLOC_SIZE(cores), cpuset); - for (int i = 0; i < cores; i++) { - CPU_SET_S(i, CPU_ALLOC_SIZE(cores), cpuset); - } - err = pthread_attr_setaffinity_np(&attrib, CPU_ALLOC_SIZE(cores), cpuset); - CPU_FREE(cpuset); - if (err != 0) { - fprintf(stderr, "pthread_attr_setaffinity_np failed: %s\n", strerror(err)); - return; - } - } - +\ err = pthread_create(&thread, &attrib, ThreadTrampoline, args.get()); // Probably a stack size error since system limits can be different from PTHREAD_STACK_MIN @@ -148,6 +129,25 @@ class os_thread { } } + if (core::Runtime::runtime_singleton_->flag().override_cpu_affinity()) { + int cores = get_nprocs_conf(); + cpu_set_t* cpuset = CPU_ALLOC(cores); + if (cpuset == nullptr) { + fprintf(stderr, "CPU_ALLOC failed: %s\n", strerror(errno)); + return; + } + CPU_ZERO_S(CPU_ALLOC_SIZE(cores), cpuset); + for (int i = 0; i < cores; i++) { + CPU_SET_S(i, CPU_ALLOC_SIZE(cores), cpuset); + } + err = pthread_setaffinity_np(thread, CPU_ALLOC_SIZE(cores), cpuset); + CPU_FREE(cpuset); + if (err != 0) { + fprintf(stderr, "pthread_attr_setaffinity_np failed: %s\n", strerror(err)); + return; + } + } + if (err == 0) args.release(); else