Cogs.Core
Utilities.cpp
1#include "Utilities.h"
2
3#include <fstream>
4#include <map>
5#include <mutex>
6#include <regex>
7#include <sstream>
8
9namespace
10{
11 const std::regex includeStatement("^#include[ ]+[\"<](.*)[\">].*");
12
13 struct GPUResourceInfo {
14 void* mAddress;
15 uint64_t mSize;
16 uint8_t mType;
17 };
18
19#if defined(OSOMP_EnableProfiler)
20 std::map<void*, GPUResourceInfo> gGPUResources;
21 std::mutex gMutex;
22 uint8_t* gNextGPUAddress = reinterpret_cast<uint8_t*>(0x1000000000000ULL);
23#endif
24}
25
26void Cogs::RegisterGPUResource(void* handle, size_t size, MemBlockType type, const OsoMPAttribute* attributes, uint32_t noOfAttributes) {
27#if defined(OSOMP_EnableProfiler)
28 std::lock_guard<std::mutex> lock(gMutex);
29
30 assert(gGPUResources.find(handle) == gGPUResources.end());
31
32 OsoMP_Allocate(uint8_t(type), gNextGPUAddress, size, attributes, noOfAttributes);
33 gGPUResources[handle] = GPUResourceInfo{gNextGPUAddress, size, uint8_t(type)};
34 gNextGPUAddress += size;
35#else
36 (void)handle;
37 (void)size;
38 (void)type;
39 (void)attributes;
40 (void)noOfAttributes;
41#endif
42}
43
44void Cogs::RegisterGPUResourceAttributes(void* handle, const OsoMPAttribute* attributes, uint32_t noOfAttributes) {
45#if defined(OSOMP_EnableProfiler)
46 std::lock_guard<std::mutex> lock(gMutex);
47 auto i = gGPUResources.find(handle);
48
49 if (i != gGPUResources.end()) {
50 OsoMP_Attributes(i->second.mAddress, attributes, noOfAttributes);
51 }
52#else
53 (void)handle;
54 (void)attributes;
55 (void)noOfAttributes;
56#endif
57}
58
59void Cogs::UnregisterGPUResource(void* handle) {
60#if defined(OSOMP_EnableProfiler)
61 std::lock_guard<std::mutex> lock(gMutex);
62 auto i = gGPUResources.find(handle);
63
64 if (i != gGPUResources.end()) {
65 OsoMP_Free(i->second.mType, i->second.mAddress, nullptr, 0);
66 gGPUResources.erase(i);
67 }
68#else
69 (void)handle;
70#endif
71}
72
73bool Cogs::Utilities::preprocessFile(IIOHandler * handler, const StringView & filename, ProcessedContent & content)
74{
75 content.origin = std::string(filename);
76
77 std::string source;
78 return Utilities::readFile(handler, filename, content);
79}
80
81bool Cogs::Utilities::preprocessContent(IIOHandler * handler , const StringView & input, ProcessedContent & content)
82{
83 if (input.empty()) {
84 content = ProcessedContent();
85 return true;
86 }
87
88 std::istringstream stream(std::string(input.data(), input.length()));
89 return preprocessContent(handler, StringView(), stream, content);
90}
91
92bool Cogs::Utilities::preprocessContent(IIOHandler * handler, const StringView & input, std::string & content)
93{
94 if (input == StringView()) return true;
95
96 std::istringstream stream(std::string(input.data(), input.length()));
97 return preprocessContent(handler, StringView(), stream, content);
98}
99
100bool Cogs::Utilities::preprocessContent(IIOHandler * handler, const StringView & fileName, std::istream & stream, ProcessedContent & content)
101{
102 std::string line;
103 std::smatch results;
104
105 bool contentAdded = false;
106 while (stream.good()) {
107 std::getline(stream, line);
108
109 if (line.size() && line[0] == '#' && std::regex_search(line, results, includeStatement)) {
110 if (handler) {
111 std::string includeContent;
112
113 if (handler->resolveFile(fileName, results[1].str(), includeContent)) {
114 if (contentAdded) {
115 content.sourceStop.push_back(content.content.size());
116 content.sourceName.push_back(std::string(fileName));
117 contentAdded = false;
118 }
119
120 std::istringstream includeStream(includeContent);
121
122 if (!preprocessContent(handler, results[1].str(), includeStream, content)) {
123 return false;
124 }
125 } else {
126 return false;
127 }
128 } else {
129 return false;
130 }
131 } else {
132 content.content += line;
133 content.content += "\n";
134 contentAdded = true;
135 }
136 }
137
138 if (contentAdded) {
139 content.sourceStop.push_back(content.content.size());
140
141 if (fileName.empty()) {
142 content.sourceName.push_back("anonymous");
143 } else {
144 content.sourceName.push_back(std::string(fileName));
145 }
146 }
147
148 return true;
149}
150
151bool Cogs::Utilities::preprocessContent(IIOHandler * handler, const StringView & fileName, std::istream & stream, std::string & content)
152{
153 std::string line;
154 std::smatch results;
155
156 while (stream.good()) {
157 std::getline(stream, line);
158
159 if (line.size() && line[0] == '#' && std::regex_search(line, results, includeStatement)) {
160 if (handler) {
161 std::string includeContent;
162
163 if (handler->resolveFile(fileName, results[1].str(), includeContent)) {
164 if (!preprocessContent(handler, includeContent, content)) {
165 return false;
166 }
167 } else {
168 return false;
169 }
170 } else {
171 return false;
172 }
173 } else {
174 content += line;
175 content += "\n";
176 }
177 }
178
179 return true;
180}
181
182bool Cogs::Utilities::readFile(IIOHandler * handler, const StringView & fileName, ProcessedContent & content)
183{
184 std::string fileContents;
185 if (!handler->openFile(fileName, fileContents)) {
186 return false;
187 }
188
189 preprocessContent(handler, fileContents, content);
190
191 return true;
192}
193
194bool Cogs::Utilities::resolveFile(IIOHandler * handler, const StringView & origin, const StringView & fileName, ProcessedContent & content)
195{
196 auto source = std::string(origin);
197 auto path = source.substr(0, source.find_last_of("/") + 1);
198
199 if (!readFile(handler, path + std::string(fileName), content)) {
200 return false;
201 }
202
203 return true;
204}