| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /*********************************************************************************/ | ||
| 2 | /* Copyright 2009-2026 Barcelona Supercomputing Center */ | ||
| 3 | /* */ | ||
| 4 | /* This file is part of the DLB library. */ | ||
| 5 | /* */ | ||
| 6 | /* DLB is free software: you can redistribute it and/or modify */ | ||
| 7 | /* it under the terms of the GNU Lesser General Public License as published by */ | ||
| 8 | /* the Free Software Foundation, either version 3 of the License, or */ | ||
| 9 | /* (at your option) any later version. */ | ||
| 10 | /* */ | ||
| 11 | /* DLB is distributed in the hope that it will be useful, */ | ||
| 12 | /* but WITHOUT ANY WARRANTY; without even the implied warranty of */ | ||
| 13 | /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ | ||
| 14 | /* GNU Lesser General Public License for more details. */ | ||
| 15 | /* */ | ||
| 16 | /* You should have received a copy of the GNU Lesser General Public License */ | ||
| 17 | /* along with DLB. If not, see <https://www.gnu.org/licenses/>. */ | ||
| 18 | /*********************************************************************************/ | ||
| 19 | |||
| 20 | #ifndef GPU_MASK_UTILS_H | ||
| 21 | #define GPU_MASK_UTILS_H | ||
| 22 | |||
| 23 | #include <stdint.h> | ||
| 24 | |||
| 25 | /* More than enough in any real case, | ||
| 26 | * predefined to match local uint64_t bitset and ease MPI comms. */ | ||
| 27 | enum { MAX_NODE_GPUS = 64 }; | ||
| 28 | |||
| 29 | 5982 | static inline int gm_count(uint64_t x) { | |
| 30 | #if defined(__GNUC__) || defined(__clang__) | ||
| 31 | 5982 | return __builtin_popcountll(x); | |
| 32 | #else | ||
| 33 | /* http://en.wikipedia.org/wiki/Hamming_weight | ||
| 34 | * (Good approach for few nonzero bits) */ | ||
| 35 | int count; | ||
| 36 | for (count=0; x; count++) | ||
| 37 | x &= x - 1; | ||
| 38 | return count; | ||
| 39 | #endif | ||
| 40 | } | ||
| 41 | |||
| 42 | /* Returns the number of trailing 0-bits in x, starting at the least significant bit position. */ | ||
| 43 | ✗ | static inline int gm_ctz(uint64_t x) | |
| 44 | { | ||
| 45 | #if defined(__GNUC__) || defined(__clang__) | ||
| 46 | ✗ | return x ? __builtin_ctzll(x) : -1; | |
| 47 | #else | ||
| 48 | if (x == 0) | ||
| 49 | return -1; | ||
| 50 | |||
| 51 | int n = 0; | ||
| 52 | while ((x & 1) == 0) { | ||
| 53 | x >>= 1; | ||
| 54 | ++n; | ||
| 55 | } | ||
| 56 | return n; | ||
| 57 | #endif | ||
| 58 | } | ||
| 59 | |||
| 60 | /* Clear least significat bit */ | ||
| 61 | ✗ | static inline uint64_t gm_clear_lsb(uint64_t x) | |
| 62 | { | ||
| 63 | ✗ | return x & (x - 1); | |
| 64 | } | ||
| 65 | |||
| 66 | /* Check if bit is set */ | ||
| 67 | static inline int gm_isset(uint32_t bit, uint64_t x) { | ||
| 68 | return (x & (1ULL << bit)) ? 1 : 0; | ||
| 69 | } | ||
| 70 | |||
| 71 | #endif /* GPU_MASK_UTILS_H */ | ||
| 72 |