root/opal/mca/hwloc/hwloc201/hwloc/include/hwloc/glibc-sched.h

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. hwloc_cpuset_to_glibc_sched_affinity
  2. hwloc_cpuset_from_glibc_sched_affinity

   1 /*
   2  * Copyright © 2009 CNRS
   3  * Copyright © 2009-2013 inria.  All rights reserved.
   4  * Copyright © 2009-2011 Université Bordeaux
   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() or pthread_attr_setaffinity_np() may want to include
  14  * this file so as to ease conversion 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 Interoperability with glibc sched affinity
  38  *
  39  * This interface offers ways to convert between hwloc cpusets and glibc cpusets
  40  * such as those manipulated by sched_getaffinity() or pthread_attr_setaffinity_np().
  41  *
  42  * \note Topology \p topology must match the current machine.
  43  *
  44  * @{
  45  */
  46 
  47 
  48 /** \brief Convert hwloc CPU set \p toposet into glibc sched affinity CPU set \p schedset
  49  *
  50  * This function may be used before calling sched_setaffinity or any other function
  51  * that takes a cpu_set_t as input parameter.
  52  *
  53  * \p schedsetsize should be sizeof(cpu_set_t) unless \p schedset was dynamically allocated with CPU_ALLOC
  54  */
  55 static __hwloc_inline int
  56 hwloc_cpuset_to_glibc_sched_affinity(hwloc_topology_t topology __hwloc_attribute_unused, hwloc_const_cpuset_t hwlocset,
  57                                     cpu_set_t *schedset, size_t schedsetsize)
  58 {
  59 #ifdef CPU_ZERO_S
  60   unsigned cpu;
  61   CPU_ZERO_S(schedsetsize, schedset);
  62   hwloc_bitmap_foreach_begin(cpu, hwlocset)
  63     CPU_SET_S(cpu, schedsetsize, schedset);
  64   hwloc_bitmap_foreach_end();
  65 #else /* !CPU_ZERO_S */
  66   unsigned cpu;
  67   CPU_ZERO(schedset);
  68   assert(schedsetsize == sizeof(cpu_set_t));
  69   hwloc_bitmap_foreach_begin(cpu, hwlocset)
  70     CPU_SET(cpu, schedset);
  71   hwloc_bitmap_foreach_end();
  72 #endif /* !CPU_ZERO_S */
  73   return 0;
  74 }
  75 
  76 /** \brief Convert glibc sched affinity CPU set \p schedset into hwloc CPU set
  77  *
  78  * This function may be used before calling sched_setaffinity  or any other function
  79  * that takes a cpu_set_t  as input parameter.
  80  *
  81  * \p schedsetsize should be sizeof(cpu_set_t) unless \p schedset was dynamically allocated with CPU_ALLOC
  82  */
  83 static __hwloc_inline int
  84 hwloc_cpuset_from_glibc_sched_affinity(hwloc_topology_t topology __hwloc_attribute_unused, hwloc_cpuset_t hwlocset,
  85                                        const cpu_set_t *schedset, size_t schedsetsize)
  86 {
  87   int cpu;
  88 #ifdef CPU_ZERO_S
  89   int count;
  90 #endif
  91   hwloc_bitmap_zero(hwlocset);
  92 #ifdef CPU_ZERO_S
  93   count = CPU_COUNT_S(schedsetsize, schedset);
  94   cpu = 0;
  95   while (count) {
  96     if (CPU_ISSET_S(cpu, schedsetsize, schedset)) {
  97       hwloc_bitmap_set(hwlocset, cpu);
  98       count--;
  99     }
 100     cpu++;
 101   }
 102 #else /* !CPU_ZERO_S */
 103   /* sched.h does not support dynamic cpu_set_t (introduced in glibc 2.7),
 104    * assume we have a very old interface without CPU_COUNT (added in 2.6)
 105    */
 106   assert(schedsetsize == sizeof(cpu_set_t));
 107   for(cpu=0; cpu<CPU_SETSIZE; cpu++)
 108     if (CPU_ISSET(cpu, schedset))
 109       hwloc_bitmap_set(hwlocset, cpu);
 110 #endif /* !CPU_ZERO_S */
 111   return 0;
 112 }
 113 
 114 /** @} */
 115 
 116 
 117 #endif /* CPU_SET */
 118 
 119 
 120 #ifdef __cplusplus
 121 } /* extern "C" */
 122 #endif
 123 
 124 
 125 #endif /* HWLOC_GLIBC_SCHED_H */

/* [<][>][^][v][top][bottom][index][help] */