-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCL_Embedder.py
105 lines (65 loc) · 2.43 KB
/
CL_Embedder.py
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
import os
def exc_path():
return os.path.dirname(os.path.abspath("__file__"))+"/cross_gpgpu/OpenCL/kernel/"
def embed_head(entry_name:str)->str:
head:str="""
#pragma once
//generated with python code
#include <string>
class okl_embed {
public:
"""
return head
def embed_ifndef_open(text:str)->str:
return text+"#ifndef NO_EMBEDDED_okl\n"
def embed_def_close(text:str)->str:
return text+"#endif\n"
def embed_ifdef_open(text:str)->str:
return text+"#ifdef NO_EMBEDDED_okl\n"
def embed_tail(entry_name:str)->str:
return entry_name + "\n};"
def get_all_kernels(embed_:str)->str:
for i in os.listdir(exc_path()):
if i.endswith(".cl"):
title:str = ""
clfile=open(exc_path()+"/"+i, "r",encoding="utf-8")
#read_and_embed(clfile, embed_)
# embed_=embed_ifndef_open(embed_)
embed_, title = read_and_embed(clfile, embed_, i)
# embed_=embed_def_close(embed_)
# embed_=embed_ifdef_open(embed_)
# embed_ = read_and_set_path(i,embed_,title)
# embed_ = embed_def_close(embed_)
clfile.close()
return embed_tail(embed_)
pass
def get_title(text:str)->str:
void_loc = text.find("void ") + 5
arg_loc = text.find("(")
return text[void_loc:arg_loc]
def add_title(title:str, main_text:str)->str:
return "std::string "+title+" = \n"+main_text + "\t;\n"
def read_and_set_path(file_path, got_str:str, title)->str:
line="\t\"/"+file_path+"\\n\""+"\n"
return got_str + add_title(title, line)
def read_and_embed(file, got_str:str, path):
main_text:str=""
title:str = ""
for i in file.readlines():
line:str=i[:]
if line.endswith("\n"):
line=line[:-1]
if line.find("//-ne")!=-1 or line.find("//No_Embed")!=-1 or line.find("printf")!=-1:
pass
else:
# if line.find("__kernel")!=-1:
# title = get_title(line)
title = path[:-3]
line="\t\""+line+"\\n\""+"\n"
main_text+=line
return got_str + add_title(title, main_text) , title
embed_string:str=""
embed_string = embed_head(embed_string)
out_file=open(exc_path() + "/okl_embedded.h","w", encoding="utf-8")
out_file.write(get_all_kernels(embed_string))
out_file.close()