You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mxnet.apache.org by zh...@apache.org on 2019/07/19 03:42:21 UTC

[incubator-mxnet] 01/28: add tutorial for exp template

This is an automated email from the ASF dual-hosted git repository.

zhasheng pushed a commit to tag v1.1
in repository https://gitbox.apache.org/repos/asf/incubator-mxnet.git

commit 07fe6f3b45ee27433a451cca310b4189b0a9af91
Author: tqchen <ti...@gmail.com>
AuthorDate: Thu Apr 10 12:17:03 2014 -0700

    add tutorial for exp template
---
 example/Makefile                  |  1 -
 example/exp-template/Makefile     | 18 ++++++++++++++++++
 example/exp-template/README.md    |  4 ++++
 example/exp-template/exp_lazy.cpp | 39 +++++++++++++++++++++++++++++++++++++++
 4 files changed, 61 insertions(+), 1 deletion(-)

diff --git a/example/Makefile b/example/Makefile
index b0f0c36..44e8152 100644
--- a/example/Makefile
+++ b/example/Makefile
@@ -1,5 +1,4 @@
 # set LD_LIBRARY_PATH
-
 export CC  = gcc
 export CXX = g++
 export NVCC =nvcc
diff --git a/example/exp-template/Makefile b/example/exp-template/Makefile
new file mode 100644
index 0000000..1e516ba
--- /dev/null
+++ b/example/exp-template/Makefile
@@ -0,0 +1,18 @@
+# set LD_LIBRARY_PATH
+export CC  = gcc
+export CXX = g++
+export CFLAGS = -Wall -O3 
+# specify tensor path
+BIN = exp_lazy
+
+.PHONY: clean all
+
+all: $(BIN) 
+
+exp_lazy: exp_lazy.cpp
+
+$(BIN) :
+	$(CXX) $(CFLAGS) -o $@ $(filter %.cpp %.o %.c, $^)
+
+clean:
+	rm -rf $(BIN) *~
diff --git a/example/exp-template/README.md b/example/exp-template/README.md
new file mode 100644
index 0000000..8c30a29
--- /dev/null
+++ b/example/exp-template/README.md
@@ -0,0 +1,4 @@
+This folder is not example of mshadow code.
+It is example code introducing expression template, the trick behind mshadow.
+
+See: https://github.com/tqchen/mshadow/wiki/Expression-Template
diff --git a/example/exp-template/exp_lazy.cpp b/example/exp-template/exp_lazy.cpp
new file mode 100644
index 0000000..3d25fe1
--- /dev/null
+++ b/example/exp-template/exp_lazy.cpp
@@ -0,0 +1,39 @@
+#include <cstdio>
+// Example Lazy evaluation code
+struct Vec;
+// expression structure holds the expression
+struct BinaryAddExp{
+    const Vec& lhs;
+    const Vec& rhs;
+    BinaryAddExp(const Vec& lhs, const Vec& rhs):lhs(lhs),rhs(rhs){}
+};
+// no constructor and destructor to allocate and de-allocate memory, allocation done by user
+struct Vec {
+    int len;
+    float* dptr;
+    Vec (void){}
+    Vec (float *dptr, int len):len(len),dptr(dptr){}
+    // here is where evaluation happens
+    inline Vec& operator= (const BinaryAddExp& src){
+        for( int i=0; i< src.lhs.len; ++ i ){
+            dptr[i] = src.lhs.dptr[i] + src.rhs.dptr[i];
+        }
+        return *this;
+    }
+};
+// no evaluation happens here
+inline BinaryAddExp operator+ (const Vec& lhs, const Vec& rhs){
+    return BinaryAddExp(lhs, rhs);
+}
+
+const int n = 3; 
+int main( void ){
+    float sa[n]={1,2,3},sb[n]={2,3,4},sc[n]={3,4,5};
+    Vec A(sa,n), B(sb,n), C(sc,n);
+    // run expression
+    A = B + C;
+    for( int i = 0; i < n; ++ i ){
+        printf("%d:%f==%f+%f\n", i, A.dptr[i], B.dptr[i], C.dptr[i] );
+    }
+    return 0;
+}