Line data Source code
1 : #include <iostream>
2 : #include <sstream>
3 :
4 : #include "Tools/Exception/exception.hpp"
5 : #include <Tools/Thread/Thread_pinning/Thread_pinning_utils.hpp>
6 :
7 : using namespace spu;
8 : using namespace spu::tools;
9 :
10 : #ifdef SPU_HWLOC
11 : static std::map<std::string, hwloc_obj_type_t> object_map = {
12 : { "NUMA", HWLOC_OBJ_NUMANODE }, { "PACKAGE", HWLOC_OBJ_PACKAGE }, { "CORE", HWLOC_OBJ_CORE },
13 : { "PU", HWLOC_OBJ_PU }, { "L1D", HWLOC_OBJ_L1CACHE }, { "L2D", HWLOC_OBJ_L2CACHE },
14 : { "L3D", HWLOC_OBJ_L3CACHE }, { "L4D", HWLOC_OBJ_L4CACHE }, { "L5D", HWLOC_OBJ_L5CACHE },
15 : { "L1I", HWLOC_OBJ_L1ICACHE }, { "L2I", HWLOC_OBJ_L2ICACHE }, { "L3I", HWLOC_OBJ_L3ICACHE },
16 : { "GROUP", HWLOC_OBJ_GROUP },
17 : };
18 : #endif
19 :
20 : std::vector<std::string>
21 13 : Thread_pinning_utils::pipeline_parser_unpacker(std::string const& hwloc_objects_pipeline, const size_t number_of_stages)
22 : {
23 13 : std::vector<std::string> vector_stages = {};
24 13 : std::string tmp;
25 13 : size_t i = 0;
26 :
27 : // Parsing part
28 166 : while (i < hwloc_objects_pipeline.size())
29 : {
30 153 : if (hwloc_objects_pipeline[i] == '|')
31 : {
32 9 : vector_stages.push_back(tmp);
33 9 : tmp.clear();
34 : }
35 144 : else if (hwloc_objects_pipeline[i] != ' ')
36 : {
37 118 : tmp.push_back(hwloc_objects_pipeline[i]);
38 : }
39 153 : i++;
40 : }
41 13 : vector_stages.push_back(tmp);
42 :
43 : // Unpacking part : pipeline=>stages
44 13 : if (vector_stages.size() == 1 && number_of_stages > 1)
45 : {
46 18 : for (size_t j = 0; j < number_of_stages - 1; ++j)
47 12 : vector_stages.push_back(vector_stages[0]);
48 : }
49 7 : else if (vector_stages.size() != number_of_stages)
50 : {
51 0 : std::stringstream message;
52 0 : message << "The number of objects is not equal to the number of stages ('vector_stages.size()' = "
53 0 : << vector_stages.size() << " and 'number_of_stages' = " << number_of_stages << ").";
54 0 : throw tools::runtime_error(__FILE__, __LINE__, __func__, message.str());
55 0 : }
56 :
57 26 : return vector_stages;
58 13 : }
59 :
60 : std::vector<std::string>
61 6 : Thread_pinning_utils::stage_parser_unpacker(std::string const& hwloc_objects_stage, const size_t number_of_threads)
62 : {
63 6 : std::vector<std::string> vector_threads = {};
64 6 : std::string tmp;
65 6 : size_t i = 0;
66 50 : while (i < hwloc_objects_stage.size())
67 : {
68 44 : if (hwloc_objects_stage[i] == ';')
69 : {
70 4 : vector_threads.push_back(tmp);
71 4 : tmp.clear();
72 : }
73 40 : else if (hwloc_objects_stage[i] != ' ')
74 : {
75 40 : tmp.push_back(hwloc_objects_stage[i]);
76 : }
77 44 : i++;
78 : }
79 6 : vector_threads.push_back(tmp); // Last thread push
80 :
81 : // Unpacking part : stage=>threads
82 6 : if (vector_threads.size() == 1 && number_of_threads > 1)
83 : {
84 0 : for (size_t j = 0; j < number_of_threads - 1; ++j)
85 0 : vector_threads.push_back(vector_threads[0]);
86 : }
87 6 : else if (vector_threads.size() != number_of_threads)
88 : {
89 0 : std::stringstream message;
90 0 : message << "The number of objects is not equal to the number of threads ('vector_threads.size()' = "
91 0 : << vector_threads.size() << " and 'number_of_threads' = " << number_of_threads << ").";
92 0 : throw tools::runtime_error(__FILE__, __LINE__, __func__, message.str());
93 0 : }
94 12 : return vector_threads;
95 6 : }
96 :
97 : std::vector<std::string>
98 54 : Thread_pinning_utils::thread_parser(std::string const& hwloc_objects_thread)
99 : {
100 54 : std::vector<std::string> vector_objets = {};
101 54 : std::string tmp;
102 54 : size_t i = 0;
103 :
104 270 : while (i < hwloc_objects_thread.size())
105 : {
106 216 : if (hwloc_objects_thread[i] == ',')
107 : {
108 0 : vector_objets.push_back(tmp);
109 0 : tmp.clear();
110 : }
111 216 : else if (hwloc_objects_thread[i] != ' ')
112 : {
113 216 : tmp.push_back(hwloc_objects_thread[i]);
114 : }
115 216 : i++;
116 : }
117 54 : vector_objets.push_back(tmp); // Last thread push
118 108 : return vector_objets;
119 54 : }
120 :
121 : #ifdef SPU_HWLOC
122 : std::pair<hwloc_obj_type_t, int>
123 54 : Thread_pinning_utils::str_to_hwloc_object(std::string& str_object)
124 : {
125 54 : std::pair<hwloc_obj_type_t, int> result;
126 54 : std::stringstream ss(str_object);
127 54 : char delim = '_';
128 54 : std::string item;
129 54 : std::getline(ss, item, delim);
130 54 : if (object_map.find(item) == object_map.end())
131 : {
132 0 : std::stringstream msg;
133 0 : msg << "'";
134 0 : msg << item;
135 0 : msg << "' is not a valid hwloc object.";
136 0 : throw tools::invalid_argument(__FILE__, __LINE__, __func__, msg.str());
137 0 : }
138 54 : result.first = object_map[item];
139 54 : std::getline(ss, item, delim);
140 54 : result.second = std::stoi(item);
141 54 : return result;
142 54 : }
143 : #endif
|