Skip to content

Commit 047a279

Browse files
authored
Merge pull request #3702 from martin-frbg/issue3687
Add openblas_getaffinity() extension (Linux-only)
2 parents daca016 + 30473b6 commit 047a279

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

cblas.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ char* openblas_get_corename(void);
2828
#ifdef OPENBLAS_OS_LINUX
2929
/* Sets thread affinity for OpenBLAS threads. `thread_idx` is in [0, openblas_get_num_threads()-1]. */
3030
int openblas_setaffinity(int thread_idx, size_t cpusetsize, cpu_set_t* cpu_set);
31+
/* Queries thread affinity for OpenBLAS threads. `thread_idx` is in [0, openblas_get_num_threads()-1]. */
32+
int openblas_getaffinity(int thread_idx, size_t cpusetsize, cpu_set_t* cpu_set);
3133
#endif
3234

3335
/* Get the parallelization type which is used by OpenBLAS */

driver/others/blas_server.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,20 @@ int openblas_setaffinity(int thread_idx, size_t cpusetsize, cpu_set_t* cpu_set)
352352

353353
return pthread_setaffinity_np(thread, cpusetsize, cpu_set);
354354
}
355+
int openblas_getaffinity(int thread_idx, size_t cpusetsize, cpu_set_t* cpu_set) {
356+
const int active_threads = openblas_get_num_threads();
357+
358+
if (thread_idx < 0 || thread_idx >= active_threads) {
359+
errno = EINVAL;
360+
return -1;
361+
}
362+
363+
pthread_t thread = (thread_idx == active_threads - 1)
364+
? pthread_self()
365+
: blas_threads[thread_idx];
366+
367+
return pthread_getaffinity_np(thread, cpusetsize, cpu_set);
368+
}
355369
#endif
356370

357371
static void* blas_thread_server(void *arg){

0 commit comments

Comments
 (0)