From 5e35fbe42d4a2b3938bf4d1b593121697979b68c Mon Sep 17 00:00:00 2001
From: Christophe Picard <Christophe.Picard@imag.fr>
Date: Sat, 27 Apr 2013 05:37:46 +0000
Subject: [PATCH] Push parse function to OpenCLEnvironment

---
 .../gpu/tests/test_opencl_environment.py      |  56 ++++-----
 HySoP/hysop/gpu/tools.py                      | 108 +++++++++---------
 2 files changed, 85 insertions(+), 79 deletions(-)

diff --git a/HySoP/hysop/gpu/tests/test_opencl_environment.py b/HySoP/hysop/gpu/tests/test_opencl_environment.py
index bb652ea10..517224281 100644
--- a/HySoP/hysop/gpu/tests/test_opencl_environment.py
+++ b/HySoP/hysop/gpu/tests/test_opencl_environment.py
@@ -1,5 +1,5 @@
 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
 
 
@@ -19,41 +19,43 @@ def test_queue_unique_creation():
 
 
 def test_parse_src_expand_floatN():
-    """
-    """
-    import StringIO
-    str_as_src = """
-    vstore__N__((float__N__)(gscal_loc[noBC_id(i+__NN__,nb_part)],
-                              ), (i + gidY*WIDTH)/__N__, 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)]
-                              ), (i + gidY*WIDTH)/4, gscal);
-"""
-    buf = StringIO.StringIO(str_as_src)
-    res = parse_file(buf, n=4)
-    assert len(parsed_str_as_src) == len(res)
-    for s1, s2 in zip(parsed_str_as_src, res):
-        assert s1 == s2
+   """
+   """
+   import StringIO
+   cl_env = get_opencl_environment(0, 0, 'gpu',
+                                   PARMES_REAL_GPU)
+   str_as_src = """
+   vstore__N__((float__N__)(gscal_loc[noBC_id(i+__NN__,nb_part)],
+   ), (i + gidY*WIDTH)/__N__, 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)]
+   ), (i + gidY*WIDTH)/4, gscal);
+   """
+   buf = StringIO.StringIO(str_as_src)
+   res = cl_env.parse_file(buf, n=4)
+   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():
     """
     """
     import StringIO
+    cl_env = get_opencl_environment(0, 0, 'gpu',
+                                    PARMES_REAL_GPU)
     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 = """
-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+2,nb_part)] = v.s2;
-gvelo_loc[noBC_id(i+3,nb_part)] = v.s3;
-"""
+    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+2,nb_part)] = v.s2;
+    gvelo_loc[noBC_id(i+3,nb_part)] = v.s3;
+    """
     buf = StringIO.StringIO(str_as_src)
-    res = parse_file(buf, n=4)
-    print res
-    print parsed_str_as_src
+    res = cl_env.parse_file(buf, n=4)
     assert len(parsed_str_as_src) == len(res)
     for s1, s2 in zip(parsed_str_as_src, res):
         assert s1 == s2
diff --git a/HySoP/hysop/gpu/tools.py b/HySoP/hysop/gpu/tools.py
index 373646df8..0458238c2 100644
--- a/HySoP/hysop/gpu/tools.py
+++ b/HySoP/hysop/gpu/tools.py
@@ -297,10 +297,10 @@ class OpenCLEnvironment(object):
             file_list = files
         else:
             file_list = [files]
-        if __VERBOSE__:
-            print "=== Kernel sources compiling ==="
-            for sf in file_list:
-                print "   - ", sf
+        #if __VERBOSE__:
+        print "=== Kernel sources compiling ==="
+        for sf in file_list:
+            print "   - ", sf
             print "   - Build options : ", self.default_build_opts + options
         for sf in file_list:
             try:
@@ -315,9 +315,11 @@ class OpenCLEnvironment(object):
         if self.precision is PARMES_REAL_GPU:
             prg = cl.Program(self.ctx, gpu_src.replace('.0', '.0f'))
         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
-        build = prg.build(self.default_build_opts + options)
+        #build = prg.build(self.default_build_opts + options)
+        build = prg.build(options)
         if __VERBOSE__:
             print options
             print "Compiler options : ",
@@ -332,6 +334,54 @@ class OpenCLEnvironment(object):
             print "===\n"
         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,
                                   precision):
@@ -444,49 +494,3 @@ def explore():
             for dvc_info in devices_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
-
-- 
GitLab