11 const std::regex includeStatement(
"^#include[ ]+[\"<](.*)[\">].*");
13 struct GPUResourceInfo {
19#if defined(OSOMP_EnableProfiler)
20 std::map<void*, GPUResourceInfo> gGPUResources;
22 uint8_t* gNextGPUAddress =
reinterpret_cast<uint8_t*
>(0x1000000000000ULL);
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);
30 assert(gGPUResources.find(handle) == gGPUResources.end());
32 OsoMP_Allocate(uint8_t(type), gNextGPUAddress, size, attributes, noOfAttributes);
33 gGPUResources[handle] = GPUResourceInfo{gNextGPUAddress, size, uint8_t(type)};
34 gNextGPUAddress += size;
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);
49 if (i != gGPUResources.end()) {
50 OsoMP_Attributes(i->second.mAddress, attributes, noOfAttributes);
59void Cogs::UnregisterGPUResource(
void* handle) {
60#if defined(OSOMP_EnableProfiler)
61 std::lock_guard<std::mutex> lock(gMutex);
62 auto i = gGPUResources.find(handle);
64 if (i != gGPUResources.end()) {
65 OsoMP_Free(i->second.mType, i->second.mAddress,
nullptr, 0);
66 gGPUResources.erase(i);
73bool Cogs::Utilities::preprocessFile(IIOHandler * handler,
const StringView & filename, ProcessedContent & content)
75 content.origin = std::string(filename);
78 return Utilities::readFile(handler, filename, content);
81bool Cogs::Utilities::preprocessContent(IIOHandler * handler ,
const StringView & input, ProcessedContent & content)
84 content = ProcessedContent();
88 std::istringstream stream(std::string(input.data(), input.length()));
89 return preprocessContent(handler, StringView(), stream, content);
92bool Cogs::Utilities::preprocessContent(IIOHandler * handler,
const StringView & input, std::string & content)
94 if (input == StringView())
return true;
96 std::istringstream stream(std::string(input.data(), input.length()));
97 return preprocessContent(handler, StringView(), stream, content);
100bool Cogs::Utilities::preprocessContent(IIOHandler * handler,
const StringView & fileName, std::istream & stream, ProcessedContent & content)
105 bool contentAdded =
false;
106 while (stream.good()) {
107 std::getline(stream, line);
109 if (line.size() && line[0] ==
'#' && std::regex_search(line, results, includeStatement)) {
111 std::string includeContent;
113 if (handler->resolveFile(fileName, results[1].str(), includeContent)) {
115 content.sourceStop.push_back(content.content.size());
116 content.sourceName.push_back(std::string(fileName));
117 contentAdded =
false;
120 std::istringstream includeStream(includeContent);
122 if (!preprocessContent(handler, results[1].str(), includeStream, content)) {
132 content.content += line;
133 content.content +=
"\n";
139 content.sourceStop.push_back(content.content.size());
141 if (fileName.empty()) {
142 content.sourceName.push_back(
"anonymous");
144 content.sourceName.push_back(std::string(fileName));
151bool Cogs::Utilities::preprocessContent(IIOHandler * handler,
const StringView & fileName, std::istream & stream, std::string & content)
156 while (stream.good()) {
157 std::getline(stream, line);
159 if (line.size() && line[0] ==
'#' && std::regex_search(line, results, includeStatement)) {
161 std::string includeContent;
163 if (handler->resolveFile(fileName, results[1].str(), includeContent)) {
164 if (!preprocessContent(handler, includeContent, content)) {
182bool Cogs::Utilities::readFile(IIOHandler * handler,
const StringView & fileName, ProcessedContent & content)
184 std::string fileContents;
185 if (!handler->openFile(fileName, fileContents)) {
189 preprocessContent(handler, fileContents, content);
194bool Cogs::Utilities::resolveFile(IIOHandler * handler,
const StringView & origin,
const StringView & fileName, ProcessedContent & content)
196 auto source = std::string(origin);
197 auto path = source.substr(0, source.find_last_of(
"/") + 1);
199 if (!readFile(handler, path + std::string(fileName), content)) {