python regex-based preprocessor proof of concept
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@@ -10,6 +10,9 @@ docs/doxygen
|
|||||||
docs/*.html
|
docs/*.html
|
||||||
tests*/*cpp
|
tests*/*cpp
|
||||||
tests*/*run
|
tests*/*run
|
||||||
|
tests*/*.o
|
||||||
|
tests_ispcpp/*.h
|
||||||
|
tests_ispcpp/*pre*
|
||||||
logs/
|
logs/
|
||||||
notify_log.log
|
notify_log.log
|
||||||
alloy_results_*
|
alloy_results_*
|
||||||
|
|||||||
189
prepro.py
Normal file
189
prepro.py
Normal file
@@ -0,0 +1,189 @@
|
|||||||
|
import re
|
||||||
|
import sys
|
||||||
|
|
||||||
|
def floating2float(function, idx):
|
||||||
|
typ = 'floating<' + str(idx) + '>'
|
||||||
|
return function.replace(typ, 'float')
|
||||||
|
|
||||||
|
def floating2double(function, idx):
|
||||||
|
typ = 'floating<' + str(idx) + '>'
|
||||||
|
return function.replace(typ, 'double')
|
||||||
|
|
||||||
|
def number2float(function, idx):
|
||||||
|
typ = 'number<' + str(idx) + '>'
|
||||||
|
return function.replace(typ, 'float')
|
||||||
|
|
||||||
|
def number2double(function, idx):
|
||||||
|
typ = 'number<' + str(idx) + '>'
|
||||||
|
return function.replace(typ, 'double')
|
||||||
|
|
||||||
|
def number2int(function, idx):
|
||||||
|
typ = 'number<' + str(idx) + '>'
|
||||||
|
return function.replace(typ, 'int')
|
||||||
|
|
||||||
|
def number2long(function, idx):
|
||||||
|
typ = 'number<' + str(idx) + '>'
|
||||||
|
return function.replace(typ, 'long')
|
||||||
|
|
||||||
|
def number2short(function, idx):
|
||||||
|
typ = 'number<' + str(idx) + '>'
|
||||||
|
return function.replace(typ, 'short')
|
||||||
|
|
||||||
|
def number2char(function, idx):
|
||||||
|
typ = 'number<' + str(idx) + '>'
|
||||||
|
return function.replace(typ, 'char')
|
||||||
|
|
||||||
|
def integer2int(function, idx):
|
||||||
|
typ = 'integer<' + str(idx) + '>'
|
||||||
|
return function.replace(typ, 'int')
|
||||||
|
|
||||||
|
def integer2long(function, idx):
|
||||||
|
typ = 'integer<' + str(idx) + '>'
|
||||||
|
return function.replace(typ, 'long')
|
||||||
|
|
||||||
|
def integer2short(function, idx):
|
||||||
|
typ = 'integer<' + str(idx) + '>'
|
||||||
|
return function.replace(typ, 'short')
|
||||||
|
|
||||||
|
def integer2char(function, idx):
|
||||||
|
typ = 'integer<' + str(idx) + '>'
|
||||||
|
return function.replace(typ, 'char')
|
||||||
|
|
||||||
|
def strip_types(fun, i):
|
||||||
|
return fun.replace('export', '').replace('void', '').replace('float', '') \
|
||||||
|
.replace('double', '').replace('char','').replace('short','') \
|
||||||
|
.replace('int','').replace('long','').replace('[]','') \
|
||||||
|
.replace('*','').replace('(','_' + str(i) + '(') \
|
||||||
|
.replace('uniform','')
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
if (len(sys.argv) != 2):
|
||||||
|
print 'usage:\n\tpython ',
|
||||||
|
print sys.argv[0],
|
||||||
|
print '[file.ispc]'
|
||||||
|
exit(1)
|
||||||
|
|
||||||
|
f = open(sys.argv[1], 'r')
|
||||||
|
function = f.read()
|
||||||
|
fun_def = re.findall(r'export [^{]*\)', function)[0]
|
||||||
|
print fun_def
|
||||||
|
f.close()
|
||||||
|
|
||||||
|
floating = 0
|
||||||
|
number = 0
|
||||||
|
integer = 0
|
||||||
|
|
||||||
|
while ('floating<' + str(floating) + '>' in function):
|
||||||
|
floating += 1
|
||||||
|
|
||||||
|
while ('number<' + str(number) + '>' in function):
|
||||||
|
number += 1
|
||||||
|
|
||||||
|
while ('integer<' + str(integer) + '>' in function):
|
||||||
|
integer += 1
|
||||||
|
|
||||||
|
for i in range(len(re.findall(r'floating[\s+]', function))):
|
||||||
|
function = function.replace('floating ',
|
||||||
|
'floating<' + str(floating) + '> ', 1)
|
||||||
|
fun_def = fun_def.replace('floating ',
|
||||||
|
'floating<' + str(floating) + '> ', 1)
|
||||||
|
floating += 1
|
||||||
|
|
||||||
|
for i in range(len(re.findall(r'number[\s+]', function))):
|
||||||
|
function = function.replace('number ',
|
||||||
|
'number<' + str(number) + '> ', 1)
|
||||||
|
fun_def = fun_def.replace('number ',
|
||||||
|
'number<' + str(number) + '> ', 1)
|
||||||
|
number += 1
|
||||||
|
|
||||||
|
for i in range(len(re.findall(r'integer[\s+]', function))):
|
||||||
|
function = function.replace('integer ',
|
||||||
|
'integer<' + str(integer) + '> ', 1)
|
||||||
|
fun_def = fun_def.replace('integer ',
|
||||||
|
'integer<' + str(integer) + '> ', 1)
|
||||||
|
integer += 1
|
||||||
|
|
||||||
|
floats = [[(fun_def, function)]]
|
||||||
|
|
||||||
|
for i in range(floating):
|
||||||
|
floats.append([])
|
||||||
|
for (h, f) in floats[i]:
|
||||||
|
floats[i+1].append((floating2float(h, i),
|
||||||
|
floating2float(f, i)
|
||||||
|
))
|
||||||
|
floats[i+1].append((floating2double(h, i),
|
||||||
|
floating2double(f, i)
|
||||||
|
))
|
||||||
|
|
||||||
|
numbers = [[]]
|
||||||
|
|
||||||
|
for f in floats[floating]:
|
||||||
|
numbers[0].append(f)
|
||||||
|
|
||||||
|
for i in range(number):
|
||||||
|
numbers.append([])
|
||||||
|
for (h, f) in numbers[i]:
|
||||||
|
numbers[i+1].append((number2float(h, i),
|
||||||
|
number2float(f, i)
|
||||||
|
))
|
||||||
|
numbers[i+1].append((number2double(h, i),
|
||||||
|
number2double(f, i)
|
||||||
|
))
|
||||||
|
numbers[i+1].append((number2int(h, i),
|
||||||
|
number2int(f, i)
|
||||||
|
))
|
||||||
|
numbers[i+1].append((number2long(h, i),
|
||||||
|
number2long(f, i)
|
||||||
|
))
|
||||||
|
numbers[i+1].append((number2short(h, i),
|
||||||
|
number2short(f, i)
|
||||||
|
))
|
||||||
|
numbers[i+1].append((number2char(h, i),
|
||||||
|
number2char(f, i)
|
||||||
|
))
|
||||||
|
|
||||||
|
integers = [[]]
|
||||||
|
|
||||||
|
for f in numbers[number]:
|
||||||
|
integers[0].append(f)
|
||||||
|
|
||||||
|
for i in range(integer):
|
||||||
|
integers.append([])
|
||||||
|
for (h, f) in integers[i]:
|
||||||
|
integers[i+1].append((integer2int(h, i),
|
||||||
|
integer2int(f, i)
|
||||||
|
))
|
||||||
|
integers[i+1].append((integer2long(h, i),
|
||||||
|
integer2long(f, i)
|
||||||
|
))
|
||||||
|
integers[i+1].append((integer2short(h, i),
|
||||||
|
integer2short(f, i)
|
||||||
|
))
|
||||||
|
integers[i+1].append((integer2char(h, i),
|
||||||
|
integer2char(f, i)
|
||||||
|
))
|
||||||
|
|
||||||
|
o = open(sys.argv[1] + '.pre.ispc', 'w')
|
||||||
|
hdr = open(sys.argv[1] + '.h', 'w')
|
||||||
|
|
||||||
|
hdr.write('#include "' + '.'.join(sys.argv[1].split('.')[:-1]) + '.h"\n\n')
|
||||||
|
hdr.write('#ifndef _' + sys.argv[1].upper().replace('.', '_') + '_H_\n')
|
||||||
|
hdr.write('#define _' + sys.argv[1].upper().replace('.', '_') + '_H_\n')
|
||||||
|
hdr.write('namespace ispc {\n')
|
||||||
|
|
||||||
|
fun_name = re.findall(r'[a-zA-Z_][a-zA-Z0-9_]*\(', function)[0][:-1]
|
||||||
|
|
||||||
|
i = 0
|
||||||
|
for (h, f) in integers[integer]:
|
||||||
|
o.write(f.replace(fun_name, fun_name+'_'+str(i), 1))
|
||||||
|
hdr.write(h.replace('uniform','').replace('export','') \
|
||||||
|
+ '\n{\n\treturn ' + strip_types(h, i) + ';\n}\n\n')
|
||||||
|
|
||||||
|
i += 1
|
||||||
|
|
||||||
|
o.close()
|
||||||
|
hdr.write('}\n')
|
||||||
|
hdr.write('#endif\n')
|
||||||
|
hdr.close()
|
||||||
|
|
||||||
|
exit(0)
|
||||||
23
tests_ispcpp/hello.cpp
Normal file
23
tests_ispcpp/hello.cpp
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#include "hello.ispc.h"
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
float A[100];
|
||||||
|
float B[100];
|
||||||
|
double result[100];
|
||||||
|
|
||||||
|
for (int i=0; i<100; i++) {
|
||||||
|
A[i] = 100 - i;
|
||||||
|
B[i] = i*i;
|
||||||
|
}
|
||||||
|
|
||||||
|
ispc::saxpy(100, 3.1415926535, (float*)&A, (float*)&B, (double*)&result);
|
||||||
|
|
||||||
|
for (int i=0; i<100; i++) {
|
||||||
|
printf("%.6f\n", result[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
11
tests_ispcpp/hello.ispc
Normal file
11
tests_ispcpp/hello.ispc
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
export void saxpy(uniform int N,
|
||||||
|
uniform floating<0> scale,
|
||||||
|
uniform floating<1> X[],
|
||||||
|
uniform floating<1> Y[],
|
||||||
|
uniform floating<2> result[])
|
||||||
|
{
|
||||||
|
foreach (i = 0 ... N) {
|
||||||
|
floating<2> tmp = scale * X[i] + Y[i];
|
||||||
|
result[i] = tmp;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user