OpenMPI  0.1.1
glibc-sched.h
Go to the documentation of this file.
1 /*
2  * Copyright © 2009 CNRS
3  * Copyright © 2009-2010 inria. All rights reserved.
4  * Copyright © 2009-2011 Université Bordeaux 1
5  * Copyright © 2011 Cisco Systems, Inc. All rights reserved.
6  * See COPYING in top-level directory.
7  */
8 
9 /** \file
10  * \brief Macros to help interaction between hwloc and glibc scheduling routines.
11  *
12  * Applications that use both hwloc and glibc scheduling routines such as
13  * sched_getaffinity may want to include this file so as to ease conversion
14  * between their respective types.
15  */
16 
17 #ifndef HWLOC_GLIBC_SCHED_H
18 #define HWLOC_GLIBC_SCHED_H
19 
20 #include <hwloc.h>
21 #include <hwloc/helper.h>
22 #include <assert.h>
23 
24 #if !defined _GNU_SOURCE || !defined _SCHED_H || (!defined CPU_SETSIZE && !defined sched_priority)
25 #error Please make sure to include sched.h before including glibc-sched.h, and define _GNU_SOURCE before any inclusion of sched.h
26 #endif
27 
28 
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32 
33 
34 #ifdef HWLOC_HAVE_CPU_SET
35 
36 
37 /** \defgroup hwlocality_glibc_sched Helpers for manipulating glibc sched affinity
38  * @{
39  */
40 
41 
42 /** \brief Convert hwloc CPU set \p toposet into glibc sched affinity CPU set \p schedset
43  *
44  * This function may be used before calling sched_setaffinity or any other function
45  * that takes a cpu_set_t as input parameter.
46  *
47  * \p schedsetsize should be sizeof(cpu_set_t) unless \p schedset was dynamically allocated with CPU_ALLOC
48  */
49 static __hwloc_inline int
50 hwloc_cpuset_to_glibc_sched_affinity(hwloc_topology_t topology __hwloc_attribute_unused, hwloc_const_cpuset_t hwlocset,
51  cpu_set_t *schedset, size_t schedsetsize)
52 {
53 #ifdef CPU_ZERO_S
54  unsigned cpu;
55  CPU_ZERO_S(schedsetsize, schedset);
56  hwloc_bitmap_foreach_begin(cpu, hwlocset)
57  CPU_SET_S(cpu, schedsetsize, schedset);
58  hwloc_bitmap_foreach_end();
59 #else /* !CPU_ZERO_S */
60  unsigned cpu;
61  CPU_ZERO(schedset);
62  assert(schedsetsize == sizeof(cpu_set_t));
63  hwloc_bitmap_foreach_begin(cpu, hwlocset)
64  CPU_SET(cpu, schedset);
65  hwloc_bitmap_foreach_end();
66 #endif /* !CPU_ZERO_S */
67  return 0;
68 }
69 
70 /** \brief Convert glibc sched affinity CPU set \p schedset into hwloc CPU set
71  *
72  * This function may be used before calling sched_setaffinity or any other function
73  * that takes a cpu_set_t as input parameter.
74  *
75  * \p schedsetsize should be sizeof(cpu_set_t) unless \p schedset was dynamically allocated with CPU_ALLOC
76  */
77 static __hwloc_inline int
78 hwloc_cpuset_from_glibc_sched_affinity(hwloc_topology_t topology __hwloc_attribute_unused, hwloc_cpuset_t hwlocset,
79  const cpu_set_t *schedset, size_t schedsetsize)
80 {
81 #ifdef CPU_ZERO_S
82  int cpu, count;
83 #endif
84  hwloc_bitmap_zero(hwlocset);
85 #ifdef CPU_ZERO_S
86  count = CPU_COUNT_S(schedsetsize, schedset);
87  cpu = 0;
88  while (count) {
89  if (CPU_ISSET_S(cpu, schedsetsize, schedset)) {
90  hwloc_bitmap_set(hwlocset, cpu);
91  count--;
92  }
93  cpu++;
94  }
95 #else /* !CPU_ZERO_S */
96  /* sched.h does not support dynamic cpu_set_t (introduced in glibc 2.7),
97  * assume we have a very old interface without CPU_COUNT (added in 2.6)
98  */
99  int cpu;
100  assert(schedsetsize == sizeof(cpu_set_t));
101  for(cpu=0; cpu<CPU_SETSIZE; cpu++)
102  if (CPU_ISSET(cpu, schedset))
103  hwloc_bitmap_set(hwlocset, cpu);
104 #endif /* !CPU_ZERO_S */
105  return 0;
106 }
107 
108 /** @} */
109 
110 
111 #endif /* CPU_SET */
112 
113 
114 #ifdef __cplusplus
115 } /* extern "C" */
116 #endif
117 
118 
119 #endif /* HWLOC_GLIBC_SCHED_H */
HWLOC_DECLSPEC void hwloc_bitmap_zero(hwloc_bitmap_t bitmap)
Empty the bitmap bitmap.
#define hwloc_bitmap_foreach_begin(id, bitmap)
Loop macro iterating on bitmap bitmap.
Definition: bitmap.h:265
HWLOC_DECLSPEC void hwloc_bitmap_set(hwloc_bitmap_t bitmap, unsigned id)
Add index id in bitmap bitmap.
Definition: private.h:56
Definition: cpuset.c:38
High-level hwloc traversal helpers.