Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:01:16

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