From bae291e6b51289745cc1a94388e644f6e1a01577 Mon Sep 17 00:00:00 2001
From: Andreas Kloeckner <inform@tiker.net>
Date: Sun, 23 Sep 2012 10:25:49 -0500
Subject: [PATCH] Add transform-arg-to-image.

---
 MEMO              | 12 ++++++++++++
 doc/reference.rst |  6 ++++--
 loopy/__init__.py | 16 ++++++++++++++++
 loopy/kernel.py   | 29 ++++++++++++++++-------------
 4 files changed, 48 insertions(+), 15 deletions(-)

diff --git a/MEMO b/MEMO
index 05cc2f804..06f585339 100644
--- a/MEMO
+++ b/MEMO
@@ -49,6 +49,16 @@ To-do
 
 - Make tests run on GPUs
 
+- Streamline arg
+
+- Fix timer / call code
+
+- variant_prefetch_fields in test_dg
+
+- make sure simple side effects in global work
+
+- syntax for linear array access
+
 Fixes:
 
 - Group instructions by dependency/inames for scheduling, to
@@ -62,6 +72,8 @@ Fixes:
 Future ideas
 ^^^^^^^^^^^^
 
+- Put all OpenCL functions into mangler
+
 - Fuse: store/fetch elimination?
 
 - Expose iname-duplicate-and-rename as a primitive.
diff --git a/doc/reference.rst b/doc/reference.rst
index 1e77eeb73..0131a1494 100644
--- a/doc/reference.rst
+++ b/doc/reference.rst
@@ -168,8 +168,8 @@ Dealing with Substitution Rules
 
 .. autofunction:: expand_subst
 
-Precomputation and Prefetching
-------------------------------
+Caching, Precomputation and Prefetching
+---------------------------------------
 
 .. autofunction:: precompute
 
@@ -177,6 +177,8 @@ Precomputation and Prefetching
 
     Uses :func:`extract_subst` and :func:`precompute`.
 
+.. autofunction:: change_arg_to_image
+
 Padding
 -------
 
diff --git a/loopy/__init__.py b/loopy/__init__.py
index ca443db75..219cc50d8 100644
--- a/loopy/__init__.py
+++ b/loopy/__init__.py
@@ -609,6 +609,22 @@ def add_dependency(kernel, insn_match, dependency):
 
 # }}}
 
+# {{{ change variable kinds
+
+def change_arg_to_image(knl, name):
+    new_args = []
+    for arg in knl.args:
+        if arg.name == name:
+            assert arg.offset == 0
+            assert arg.shape is not None
+            new_args.append(ImageArg(arg.name, dtype=arg.dtype, shape=arg.shape))
+        else:
+            new_args.append(arg)
+
+    return knl.copy(args=new_args)
+
+# }}}
+
 
 
 
diff --git a/loopy/kernel.py b/loopy/kernel.py
index d1eb4eddd..a3f2cbee1 100644
--- a/loopy/kernel.py
+++ b/loopy/kernel.py
@@ -198,30 +198,33 @@ class ConstantArg(_ShapedArg):
         return "<ConstantArg '%s' of type %s and shape (%s)>" % (
                 self.name, self.dtype, ",".join(str(i) for i in self.shape))
 
-class ImageArg(object):
+class ImageArg(Record):
     def __init__(self, name, dtype, dimensions=None, shape=None):
-        self.name = name
-        self.dtype = np.dtype(dtype)
+        dtype = np.dtype(dtype)
         if shape is not None:
-            if dimensions is not None:
-                raise RuntimeError("cannot specify both shape and dimensions "
-                        "in ImageArg")
-            self.dimensions = len(shape)
-            self.shape = shape
+            if dimensions is not None and dimensions != len(shape):
+                raise RuntimeError("cannot specify both shape and "
+                        "disagreeing dimensions in ImageArg")
+            dimensions = len(shape)
         else:
             if not isinstance(dimensions, int):
                 raise RuntimeError("ImageArg: dimensions must be an integer")
-            self.dimensions = dimensions
+
+        Record.__init__(self,
+                dimensions=dimensions,
+                shape=shape,
+                dtype=dtype,
+                name=name)
+
 
     def __repr__(self):
         return "<ImageArg '%s' of type %s>" % (self.name, self.dtype)
 
 
-class ValueArg(object):
+class ValueArg(Record):
     def __init__(self, name, dtype, approximately=None):
-        self.name = name
-        self.dtype = np.dtype(dtype)
-        self.approximately = approximately
+        Record.__init__(self, name=name, dtype=np.dtype(dtype),
+                approximately=approximately)
 
     def __repr__(self):
         return "<ValueArg '%s' of type %s>" % (self.name, self.dtype)
-- 
GitLab