root/orte/test/system/opal_hwloc.c

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

DEFINITIONS

This source file includes following definitions.
  1. fill_cache_line_size
  2. main

   1 /* -*- C -*-
   2  *
   3  * $HEADER$
   4  *
   5  */
   6 #include <stdio.h>
   7 #include <unistd.h>
   8 
   9 #include "opal/mca/hwloc/base/base.h"
  10 #include "opal/runtime/opal.h"
  11 
  12 
  13 static hwloc_topology_t my_topology;
  14 
  15 static void fill_cache_line_size(void)
  16 {
  17     int i = 0;
  18     unsigned size;
  19     hwloc_obj_t obj;
  20     bool found = false;
  21 
  22     /* Look for the smallest L2 cache size */
  23     size = 4096;
  24     while (1) {
  25         obj = opal_hwloc_base_get_obj_by_type(my_topology,
  26                                               HWLOC_OBJ_CACHE, 2,
  27                                               i, OPAL_HWLOC_LOGICAL);
  28         if (NULL == obj) {
  29             break;
  30         } else {
  31             found = true;
  32             if (NULL != obj->attr &&
  33                 size > obj->attr->cache.linesize) {
  34                 size = obj->attr->cache.linesize;
  35             }
  36         }
  37         ++i;
  38     }
  39 
  40     /* If we found an L2 cache size in the hwloc data, save it in
  41        opal_cache_line_size.  Otherwise, we'll leave whatever default
  42        was set in opal_init.c */
  43     if (found) {
  44         opal_cache_line_size = (int) size;
  45     }
  46 }
  47 
  48 int main(int argc, char* argv[])
  49 {
  50     hwloc_obj_t obj;
  51     unsigned j, k;
  52     struct hwloc_topology_support *support;
  53     int rc;
  54 
  55     if (2 != argc) {
  56         fprintf(stderr, "Usage: opal_hwloc <topofile>\n");
  57         exit(1);
  58     }
  59 
  60     if (0 > (rc = opal_init(&argc, &argv))) {
  61         fprintf(stderr, "opal_hwloc: couldn't init opal - error code %d\n", rc);
  62         return rc;
  63     }
  64 
  65     if (0 != hwloc_topology_init(&my_topology)) {
  66         return OPAL_ERR_NOT_SUPPORTED;
  67     }
  68     if (0 != hwloc_topology_set_xml(my_topology, argv[1])) {
  69         hwloc_topology_destroy(my_topology);
  70         return OPAL_ERR_NOT_SUPPORTED;
  71     }
  72     /* since we are loading this from an external source, we have to
  73      * explicitly set a flag so hwloc sets things up correctly
  74      */
  75     if (0 != opal_hwloc_base_topology_set_flags(my_topology,
  76                                                 HWLOC_TOPOLOGY_FLAG_IS_THISSYSTEM)) {
  77         hwloc_topology_destroy(my_topology);
  78         return OPAL_ERR_NOT_SUPPORTED;
  79     }
  80     if (0 != hwloc_topology_load(my_topology)) {
  81         hwloc_topology_destroy(my_topology);
  82         return OPAL_ERR_NOT_SUPPORTED;
  83     }
  84     /* remove the hostname from the topology. Unfortunately, hwloc
  85      * decided to add the source hostname to the "topology", thus
  86      * rendering it unusable as a pure topological description. So
  87      * we remove that information here.
  88      */
  89     obj = hwloc_get_root_obj(my_topology);
  90     for (k=0; k < obj->infos_count; k++) {
  91         if (NULL == obj->infos[k].name ||
  92             NULL == obj->infos[k].value) {
  93             continue;
  94         }
  95         if (0 == strncmp(obj->infos[k].name, "HostName", strlen("HostName"))) {
  96             free(obj->infos[k].name);
  97             free(obj->infos[k].value);
  98             /* left justify the array */
  99             for (j=k; j < obj->infos_count-1; j++) {
 100                 obj->infos[j] = obj->infos[j+1];
 101             }
 102             obj->infos[obj->infos_count-1].name = NULL;
 103             obj->infos[obj->infos_count-1].value = NULL;
 104             obj->infos_count--;
 105             break;
 106         }
 107     }
 108     /* unfortunately, hwloc does not include support info in its
 109      * xml output :-(( We default to assuming it is present as
 110      * systems that use this option are likely to provide
 111      * binding support
 112      */
 113     support = (struct hwloc_topology_support*)hwloc_topology_get_support(my_topology);
 114     support->cpubind->set_thisproc_cpubind = true;
 115 
 116     /* filter the cpus thru any default cpu set */
 117     opal_hwloc_base_filter_cpus(my_topology);
 118 
 119     /* fill opal_cache_line_size global with the smallest L1 cache
 120        line size */
 121     fill_cache_line_size();
 122 
 123     /* test it */
 124     if (NULL == hwloc_get_obj_by_type(my_topology, HWLOC_OBJ_CORE, 0)) {
 125         fprintf(stderr, "DIDN'T FIND A CORE\n");
 126     }
 127 
 128     hwloc_topology_destroy(my_topology);
 129 
 130     opal_finalize();
 131 
 132     return 0;
 133 }

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