|
| 1 | +/* |
| 2 | +
|
| 3 | + BLIS |
| 4 | + An object-based framework for developing high-performance BLAS-like |
| 5 | + libraries. |
| 6 | +
|
| 7 | + Copyright (C) 2022 NVIDIA |
| 8 | +
|
| 9 | + Redistribution and use in source and binary forms, with or without |
| 10 | + modification, are permitted provided that the following conditions are |
| 11 | + met: |
| 12 | + - Redistributions of source code must retain the above copyright |
| 13 | + notice, this list of conditions and the following disclaimer. |
| 14 | + - Redistributions in binary form must reproduce the above copyright |
| 15 | + notice, this list of conditions and the following disclaimer in the |
| 16 | + documentation and/or other materials provided with the distribution. |
| 17 | + - Neither the name(s) of the copyright holder(s) nor the names of its |
| 18 | + contributors may be used to endorse or promote products derived |
| 19 | + from this software without specific prior written permission. |
| 20 | +
|
| 21 | + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 22 | + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 23 | + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 24 | + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 25 | + HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 26 | + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 27 | + LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 28 | + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 29 | + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 30 | + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 31 | + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 | +
|
| 33 | +*/ |
| 34 | + |
| 35 | +// we need a way to detect oversubscription of the kind where |
| 36 | +// hierarchical parallelism is used and the affinity mask within |
| 37 | +// which BLIS runs does not have enough hardware threads to support |
| 38 | +// the requested software threads. |
| 39 | +// |
| 40 | +// this is motivated by, or related to: |
| 41 | +// https://github.com/flame/blis/issues/588 |
| 42 | +// https://github.com/flame/blis/pull/607 |
| 43 | +// https://github.com/flame/blis/issues/604 |
| 44 | +// https://github.com/flame/blis/issues/603 |
| 45 | + |
| 46 | +#include "bli_affinity.h" |
| 47 | + |
| 48 | +#ifndef BLIS_ENABLE_AFFINITY |
| 49 | + |
| 50 | +// define the symbol for platforms like Windows and MacOS that do not support the Linux affinity API |
| 51 | + |
| 52 | +dim_t bli_affinity_get_hw_size(bli_affinity_scope_t scope) |
| 53 | +{ |
| 54 | + // this is the largest possible value returned by this function |
| 55 | + // and it means that the affinity mask does not constrain the current scope. |
| 56 | + return (dim_t)1024; |
| 57 | +} |
| 58 | + |
| 59 | +#else |
| 60 | + |
| 61 | +// this macro has to come before any other headers |
| 62 | +#define _GNU_SOURCE |
| 63 | + |
| 64 | +#include <sched.h> |
| 65 | +#include <unistd.h> |
| 66 | + |
| 67 | +// scope is either the calling process or the calling thread: |
| 68 | +// 0 = calling process |
| 69 | +// 1 = calling thread |
| 70 | + |
| 71 | +dim_t bli_affinity_get_hw_size(bli_affinity_scope_t scope) |
| 72 | +{ |
| 73 | + int rc; |
| 74 | + int active_cpus; |
| 75 | + pid_t pid; |
| 76 | + cpu_set_t mask; |
| 77 | + |
| 78 | + if (scope == 0) { |
| 79 | + pid = getpid(); |
| 80 | + } else { |
| 81 | + // this means the current thread |
| 82 | + pid = 0; |
| 83 | + } |
| 84 | + |
| 85 | + CPU_ZERO(&mask); |
| 86 | + |
| 87 | + // if the CPU mask is larger than 1024 bits, this needs to change. |
| 88 | + // see https://man7.org/linux/man-pages/man2/sched_getaffinity.2.html for details. |
| 89 | + rc = sched_getaffinity(pid, sizeof(cpu_set_t), &mask); |
| 90 | + if (rc) { |
| 91 | + bli_print_msg( "sched_getaffinity failed", |
| 92 | + __FILE__, __LINE__ ); |
| 93 | + bli_abort(); |
| 94 | + } |
| 95 | + |
| 96 | + active_cpus = 0; |
| 97 | + for (int i=0; i<sizeof(cpu_set_t); i++) { |
| 98 | + const int on = CPU_ISSET(i, &mask); |
| 99 | + if (on) active_cpus++; |
| 100 | + } |
| 101 | + |
| 102 | + return active_cpus; |
| 103 | +} |
| 104 | + |
| 105 | +#endif // BLIS_ENABLE_AFFINITY |
0 commit comments