Skip to content
Snippets Groups Projects
Commit 5e35fbe4 authored by Christophe Picard's avatar Christophe Picard
Browse files

Push parse function to OpenCLEnvironment

parent aea25a67
No related branches found
No related tags found
No related merge requests found
from parmepy.gpu.tools import get_opencl_environment, \ from parmepy.gpu.tools import get_opencl_environment, \
get_opengl_shared_environment, parse_file get_opengl_shared_environment#, parse_file
from parmepy.gpu import PARMES_REAL_GPU, PARMES_DOUBLE_GPU from parmepy.gpu import PARMES_REAL_GPU, PARMES_DOUBLE_GPU
...@@ -19,41 +19,43 @@ def test_queue_unique_creation(): ...@@ -19,41 +19,43 @@ def test_queue_unique_creation():
def test_parse_src_expand_floatN(): def test_parse_src_expand_floatN():
""" """
""" """
import StringIO import StringIO
str_as_src = """ cl_env = get_opencl_environment(0, 0, 'gpu',
vstore__N__((float__N__)(gscal_loc[noBC_id(i+__NN__,nb_part)], PARMES_REAL_GPU)
), (i + gidY*WIDTH)/__N__, gscal); str_as_src = """
""" vstore__N__((float__N__)(gscal_loc[noBC_id(i+__NN__,nb_part)],
parsed_str_as_src = """ ), (i + gidY*WIDTH)/__N__, gscal);
vstore4((float4)(gscal_loc[noBC_id(i+0,nb_part)],gscal_loc[noBC_id(i+1,nb_part)],gscal_loc[noBC_id(i+2,nb_part)],gscal_loc[noBC_id(i+3,nb_part)] """
), (i + gidY*WIDTH)/4, gscal); parsed_str_as_src = """
""" vstore4((float4)(gscal_loc[noBC_id(i+0,nb_part)],gscal_loc[noBC_id(i+1,nb_part)],gscal_loc[noBC_id(i+2,nb_part)],gscal_loc[noBC_id(i+3,nb_part)]
buf = StringIO.StringIO(str_as_src) ), (i + gidY*WIDTH)/4, gscal);
res = parse_file(buf, n=4) """
assert len(parsed_str_as_src) == len(res) buf = StringIO.StringIO(str_as_src)
for s1, s2 in zip(parsed_str_as_src, res): res = cl_env.parse_file(buf, n=4)
assert s1 == s2 assert len(parsed_str_as_src) == len(res)
for s1, s2 in zip(parsed_str_as_src, res):
assert s1 == s2
def test_parse_src_expand(): def test_parse_src_expand():
""" """
""" """
import StringIO import StringIO
cl_env = get_opencl_environment(0, 0, 'gpu',
PARMES_REAL_GPU)
str_as_src = """ str_as_src = """
gvelo_loc[noBC_id(i+__NN__,nb_part)] = v.s__NN__; gvelo_loc[noBC_id(i+__NN__,nb_part)] = v.s__NN__;
""" """
parsed_str_as_src = """ parsed_str_as_src = """
gvelo_loc[noBC_id(i+0,nb_part)] = v.s0; gvelo_loc[noBC_id(i+0,nb_part)] = v.s0;
gvelo_loc[noBC_id(i+1,nb_part)] = v.s1; gvelo_loc[noBC_id(i+1,nb_part)] = v.s1;
gvelo_loc[noBC_id(i+2,nb_part)] = v.s2; gvelo_loc[noBC_id(i+2,nb_part)] = v.s2;
gvelo_loc[noBC_id(i+3,nb_part)] = v.s3; gvelo_loc[noBC_id(i+3,nb_part)] = v.s3;
""" """
buf = StringIO.StringIO(str_as_src) buf = StringIO.StringIO(str_as_src)
res = parse_file(buf, n=4) res = cl_env.parse_file(buf, n=4)
print res
print parsed_str_as_src
assert len(parsed_str_as_src) == len(res) assert len(parsed_str_as_src) == len(res)
for s1, s2 in zip(parsed_str_as_src, res): for s1, s2 in zip(parsed_str_as_src, res):
assert s1 == s2 assert s1 == s2
......
...@@ -297,10 +297,10 @@ class OpenCLEnvironment(object): ...@@ -297,10 +297,10 @@ class OpenCLEnvironment(object):
file_list = files file_list = files
else: else:
file_list = [files] file_list = [files]
if __VERBOSE__: #if __VERBOSE__:
print "=== Kernel sources compiling ===" print "=== Kernel sources compiling ==="
for sf in file_list: for sf in file_list:
print " - ", sf print " - ", sf
print " - Build options : ", self.default_build_opts + options print " - Build options : ", self.default_build_opts + options
for sf in file_list: for sf in file_list:
try: try:
...@@ -315,9 +315,11 @@ class OpenCLEnvironment(object): ...@@ -315,9 +315,11 @@ class OpenCLEnvironment(object):
if self.precision is PARMES_REAL_GPU: if self.precision is PARMES_REAL_GPU:
prg = cl.Program(self.ctx, gpu_src.replace('.0', '.0f')) prg = cl.Program(self.ctx, gpu_src.replace('.0', '.0f'))
else: else:
prg = cl.Program(self.ctx, gpu_src.replace('float', 'double')) prg = cl.Program(self.ctx, gpu_src.replace('.0', '.0f'))
# prg = cl.Program(self.ctx, gpu_src.replace('float', 'double'))
# OpenCL program # OpenCL program
build = prg.build(self.default_build_opts + options) #build = prg.build(self.default_build_opts + options)
build = prg.build(options)
if __VERBOSE__: if __VERBOSE__:
print options print options
print "Compiler options : ", print "Compiler options : ",
...@@ -332,6 +334,54 @@ class OpenCLEnvironment(object): ...@@ -332,6 +334,54 @@ class OpenCLEnvironment(object):
print "===\n" print "===\n"
return build return build
def parse_file(self,f, n=8):
"""
Parse a file containing OpenCL sources.
@param f : source file
@param n : vector width
@return parsed sources as string.
- \__N__ is expanded as an integer corresponding to a vector with
- \__NN__, instruction is duplicated to operate on each vector component:
- if line ends with ';', the whole instruciton is duplicated.
- if line ends with ',' and contains '(float__N__)(', the float element
is duplicated
Examples with a 4-width vector:\n
\verbatim
float__N__ x; -> float4 x;
x.s__NN__ = 1.0f; -> x.s0 = 1.0f;
x.s1 = 1.0f;
x.s2 = 1.0f;
x.s3 = 1.0f;
x = (int__N__)(\__NN__, -> x = (int4)(0,
); 1,
2,
3,
);
\endverbatim
"""
src = ""
for l in f.readlines():
if l.find('(float__N__)(') >= 0 and l.find('__NN__') >= 0 and \
l[-2] == ',':
sl = l.split("(float__N__)(")
l = sl[0] + "(float" + str(n) + ")("
el = sl[1].rsplit(',', 1)[0]
for i in xrange(n):
l += el.replace('__NN__', str(i)) + ','
l = l[:-1] + '\n'
elif l.find('__NN__') >= 0 and l[-2] == ';':
el = ""
for i in xrange(n):
el += l.replace('__NN__', str(i))
l = el
src += l.replace('__N__', str(n))
print "====", len(src)
return src
def get_opengl_shared_environment(platform_id, device_id, device_type, def get_opengl_shared_environment(platform_id, device_id, device_type,
precision): precision):
...@@ -444,49 +494,3 @@ def explore(): ...@@ -444,49 +494,3 @@ def explore():
for dvc_info in devices_info: for dvc_info in devices_info:
print " |-", dvc_info, ':', eval("dvc." + dvc_info) print " |-", dvc_info, ':', eval("dvc." + dvc_info)
def parse_file(f, n=8):
"""
Parse a file containing OpenCL sources.
@param f : source file
@param n : vector width
@return parsed sources as string.
- \__N__ is expanded as an integer corresponding to a vector with
- \__NN__, instruction is duplicated to operate on each vector component:
- if line ends with ';', the whole instruciton is duplicated.
- if line ends with ',' and contains '(float__N__)(', the float element
is duplicated
Examples with a 4-width vector:\n
\verbatim
float__N__ x; -> float4 x;
x.s__NN__ = 1.0f; -> x.s0 = 1.0f;
x.s1 = 1.0f;
x.s2 = 1.0f;
x.s3 = 1.0f;
x = (int__N__)(\__NN__, -> x = (int4)(0,
); 1,
2,
3,
);
\endverbatim
"""
src = ""
for l in f.readlines():
if l.find('(float__N__)(') >= 0 and l.find('__NN__') >= 0 and \
l[-2] == ',':
sl = l.split("(float__N__)(")
l = sl[0] + "(float" + str(n) + ")("
el = sl[1].rsplit(',', 1)[0]
for i in xrange(n):
l += el.replace('__NN__', str(i)) + ','
l = l[:-1] + '\n'
elif l.find('__NN__') >= 0 and l[-2] == ';':
el = ""
for i in xrange(n):
el += l.replace('__NN__', str(i))
l = el
src += l.replace('__N__', str(n))
return src
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment