File: hwloc-assembler.c

package info (click to toggle)
hwloc 1.4.1-4
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 11,692 kB
  • sloc: ansic: 23,568; sh: 12,105; xml: 1,478; makefile: 1,251; csh: 138; php: 8
file content (128 lines) | stat: -rw-r--r-- 3,328 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
 * Copyright © 2011 inria.  All rights reserved.
 * See COPYING in top-level directory.
 */

#include <private/autogen/config.h>
#include <hwloc.h>

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

static void usage(char *name, FILE *where)
{
  fprintf (where, "Usage: %s [options] <output>.xml [-n <name1] <input1>.xml [-n name2] <input2>.xml ...\n", name);
  fprintf (where, "Options:\n");
  fprintf (where, "  -v --verbose      Show verbose messages\n");
  fprintf (where, "  -f --force        Ignore errors while reading input files\n");
  fprintf (where, "  -n --name <name>  Set the name of the next input topology\n");
}

int main(int argc, char *argv[])
{
  hwloc_topology_t topology;
  char *callname;
  char *output;
  int verbose = 0;
  int force = 0;
  int opt;
  int i, j;

  callname = strrchr(argv[0], '/');
  if (!callname)
    callname = argv[0];
  else
    callname++;
  argc--;
  argv++;

  while (argc >= 1) {
    opt = 0;
    if (!strcmp(argv[0], "-v") || !strcmp(argv[0], "--verbose")) {
      verbose++;
    } else if (!strcmp(argv[0], "-f") || !strcmp(argv[0], "--force")) {
      force = 1;
    } else if (!strcmp(argv[0], "-h") || !strcmp(argv[0], "--help")) {
      usage(callname, stdout);
      exit(EXIT_SUCCESS);
    } else if (!strcmp(argv[0], "--")) {
      argc--;
      argv++;
      break;
    } else if (*argv[0] == '-') {
      fprintf(stderr, "Unrecognized option: %s\n", argv[0]);
      usage(callname, stderr);
      exit(EXIT_FAILURE);
    } else
      break;
    argc -= opt+1;
    argv += opt+1;
  }

  if (!argc) {
    fprintf(stderr, "Missing output file name\n");
    usage(callname, stderr);
    exit(EXIT_FAILURE);
  }
  output = argv[0];
  argc--;
  argv++;

  hwloc_topology_init(&topology);
  hwloc_topology_set_custom(topology);

  for(i=0, j=0; i<argc; i++, j++) {
    hwloc_topology_t input;
    hwloc_obj_t root;
    char index[10];
    char *name = NULL;

    if (!strcmp(argv[i], "-n") || !strcmp(argv[i], "--name")) {
      if (i+2 >= argc) {
	usage(callname, stderr);
	exit(EXIT_FAILURE);
      }
      name = argv[i+1];
      i+=2;
    }

    if (verbose) {
      if (name)
	printf("Importing XML topology %s with name %s ...\n", argv[i], name);
      else
	printf("Importing XML topology %s ...\n", argv[i]);
    }

    hwloc_topology_init(&input);
    if (hwloc_topology_set_xml(input, argv[i])) {
      fprintf(stderr, "Failed to set source XML file %s (%s)\n", argv[i], strerror(errno));
      hwloc_topology_destroy(input);
      if (force)
	continue;
      else
	return EXIT_FAILURE;
    }
    hwloc_topology_load(input);

    root = hwloc_get_root_obj(input);
    hwloc_obj_add_info(root, "AssemblerName", name ? name : argv[i]);
    snprintf(index, sizeof(index), "%d", j);
    hwloc_obj_add_info(root, "AssemblerIndex", index);

    hwloc_custom_insert_topology(topology, hwloc_get_root_obj(topology), input, NULL);
    hwloc_topology_destroy(input);
  }

  if (verbose)
    printf("Assembling global topology...\n");
  hwloc_topology_load(topology);
  if (hwloc_topology_export_xml(topology, output) < 0) {
    fprintf(stderr, "Failed to export XML to %s (%s)\n", output, strerror(errno));
    return EXIT_FAILURE;
  }
  hwloc_topology_destroy(topology);
  if (verbose)
    printf("Exported topology to XML file %s\n", output);
  return 0;
}