Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-21 08:26:24

0001 /*
0002  * SPDX-License-Identifier: BSD-3-Clause
0003  * Copyright © 2009 CNRS
0004  * Copyright © 2009-2023 Inria.  All rights reserved.
0005  * Copyright © 2009-2011 Université Bordeaux
0006  * Copyright © 2011 Cisco Systems, Inc.  All rights reserved.
0007  * See COPYING in top-level directory.
0008  */
0009 
0010 /** \file
0011  * \brief Macros to help interaction between hwloc and glibc scheduling routines.
0012  *
0013  * Applications that use both hwloc and glibc scheduling routines such as
0014  * sched_getaffinity() or pthread_attr_setaffinity_np() may want to include
0015  * this file so as to ease conversion between their respective types.
0016  */
0017 
0018 #ifndef HWLOC_GLIBC_SCHED_H
0019 #define HWLOC_GLIBC_SCHED_H
0020 
0021 #include "hwloc.h"
0022 #include "hwloc/helper.h"
0023 
0024 #include <assert.h>
0025 
0026 #if !defined _GNU_SOURCE || (!defined _SCHED_H && !defined _SCHED_H_) || (!defined CPU_SETSIZE && !defined sched_priority)
0027 #error Please make sure to include sched.h before including glibc-sched.h, and define _GNU_SOURCE before any inclusion of sched.h
0028 #endif
0029 
0030 
0031 #ifdef __cplusplus
0032 extern "C" {
0033 #endif
0034 
0035 
0036 #ifdef HWLOC_HAVE_CPU_SET
0037 
0038 
0039 /** \defgroup hwlocality_glibc_sched Interoperability with glibc sched affinity
0040  *
0041  * This interface offers ways to convert between hwloc cpusets and glibc cpusets
0042  * such as those manipulated by sched_getaffinity() or pthread_attr_setaffinity_np().
0043  *
0044  * \note Topology \p topology must match the current machine.
0045  *
0046  * @{
0047  */
0048 
0049 
0050 /** \brief Convert hwloc CPU set \p toposet into glibc sched affinity CPU set \p schedset
0051  *
0052  * This function may be used before calling sched_setaffinity or any other function
0053  * that takes a cpu_set_t as input parameter.
0054  *
0055  * \p schedsetsize should be sizeof(cpu_set_t) unless \p schedset was dynamically allocated with CPU_ALLOC
0056  *
0057  * \return 0.
0058  */
0059 static __hwloc_inline int
0060 hwloc_cpuset_to_glibc_sched_affinity(hwloc_topology_t topology __hwloc_attribute_unused, hwloc_const_cpuset_t hwlocset,
0061                     cpu_set_t *schedset, size_t schedsetsize)
0062 {
0063 #ifdef CPU_ZERO_S
0064   unsigned cpu;
0065   CPU_ZERO_S(schedsetsize, schedset);
0066   hwloc_bitmap_foreach_begin(cpu, hwlocset)
0067     CPU_SET_S(cpu, schedsetsize, schedset);
0068   hwloc_bitmap_foreach_end();
0069 #else /* !CPU_ZERO_S */
0070   unsigned cpu;
0071   CPU_ZERO(schedset);
0072   assert(schedsetsize == sizeof(cpu_set_t));
0073   hwloc_bitmap_foreach_begin(cpu, hwlocset)
0074     CPU_SET(cpu, schedset);
0075   hwloc_bitmap_foreach_end();
0076 #endif /* !CPU_ZERO_S */
0077   return 0;
0078 }
0079 
0080 /** \brief Convert glibc sched affinity CPU set \p schedset into hwloc CPU set
0081  *
0082  * This function may be used before calling sched_setaffinity  or any other function
0083  * that takes a cpu_set_t  as input parameter.
0084  *
0085  * \p schedsetsize should be sizeof(cpu_set_t) unless \p schedset was dynamically allocated with CPU_ALLOC
0086  *
0087  * \return 0 on success.
0088  * \return -1 with errno set to \c ENOMEM if some internal reallocation failed.
0089  */
0090 static __hwloc_inline int
0091 hwloc_cpuset_from_glibc_sched_affinity(hwloc_topology_t topology __hwloc_attribute_unused, hwloc_cpuset_t hwlocset,
0092                                        const cpu_set_t *schedset, size_t schedsetsize)
0093 {
0094   int cpu;
0095 #ifdef CPU_ZERO_S
0096   int count;
0097 #endif
0098   hwloc_bitmap_zero(hwlocset);
0099 #ifdef CPU_ZERO_S
0100   count = CPU_COUNT_S(schedsetsize, schedset);
0101   cpu = 0;
0102   while (count) {
0103     if (CPU_ISSET_S(cpu, schedsetsize, schedset)) {
0104       if (hwloc_bitmap_set(hwlocset, cpu) < 0)
0105         return -1;
0106       count--;
0107     }
0108     cpu++;
0109   }
0110 #else /* !CPU_ZERO_S */
0111   /* sched.h does not support dynamic cpu_set_t (introduced in glibc 2.7),
0112    * assume we have a very old interface without CPU_COUNT (added in 2.6)
0113    */
0114   assert(schedsetsize == sizeof(cpu_set_t));
0115   for(cpu=0; cpu<CPU_SETSIZE; cpu++)
0116     if (CPU_ISSET(cpu, schedset))
0117       if (hwloc_bitmap_set(hwlocset, cpu) < 0)
0118         return -1;
0119 #endif /* !CPU_ZERO_S */
0120   return 0;
0121 }
0122 
0123 /** @} */
0124 
0125 
0126 #endif /* CPU_SET */
0127 
0128 
0129 #ifdef __cplusplus
0130 } /* extern "C" */
0131 #endif
0132 
0133 
0134 #endif /* HWLOC_GLIBC_SCHED_H */