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:22 UTC
[incubator-mxnet] 02/28: more example
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 b16a17aa8fcbe70929f0e37b4354c6cf092c6732
Author: tqchen <ti...@gmail.com>
AuthorDate: Thu Apr 10 12:41:49 2014 -0700
more example
---
example/exp-template/Makefile | 3 +-
example/exp-template/exp_lazy.cpp | 5 +--
example/exp-template/exp_template.cpp | 64 +++++++++++++++++++++++++++++++++++
3 files changed, 69 insertions(+), 3 deletions(-)
diff --git a/example/exp-template/Makefile b/example/exp-template/Makefile
index 1e516ba..68d6168 100644
--- a/example/exp-template/Makefile
+++ b/example/exp-template/Makefile
@@ -3,13 +3,14 @@ export CC = gcc
export CXX = g++
export CFLAGS = -Wall -O3
# specify tensor path
-BIN = exp_lazy
+BIN = exp_lazy exp_template
.PHONY: clean all
all: $(BIN)
exp_lazy: exp_lazy.cpp
+exp_template: exp_template.cpp
$(BIN) :
$(CXX) $(CFLAGS) -o $@ $(filter %.cpp %.o %.c, $^)
diff --git a/example/exp-template/exp_lazy.cpp b/example/exp-template/exp_lazy.cpp
index 3d25fe1..91f49b4 100644
--- a/example/exp-template/exp_lazy.cpp
+++ b/example/exp-template/exp_lazy.cpp
@@ -1,5 +1,6 @@
-#include <cstdio>
// Example Lazy evaluation code
+// for simplicity, we use struct and make all members public
+#include <cstdio>
struct Vec;
// expression structure holds the expression
struct BinaryAddExp{
@@ -15,7 +16,7 @@ struct Vec {
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 ){
+ for( int i = 0; i < len; ++i ){
dptr[i] = src.lhs.dptr[i] + src.rhs.dptr[i];
}
return *this;
diff --git a/example/exp-template/exp_template.cpp b/example/exp-template/exp_template.cpp
new file mode 100644
index 0000000..d9ec462
--- /dev/null
+++ b/example/exp-template/exp_template.cpp
@@ -0,0 +1,64 @@
+// Example code, expression template, and more length equations
+// for simplicity, we use struct and make all members public
+
+#include <cstdio>
+
+// this is expression, all expressions must inheritate it, and put their type in subtype
+template<typename SubType>
+struct Exp{
+ // returns const reference of the actual type of this expression
+ inline const SubType& self(void) const{
+ return *static_cast<const SubType*>(this);
+ }
+};
+
+// binary add expression
+// note how it is inheritates from Exp
+// and put its own type into the template argument
+template<typename TLhs, typename TRhs>
+struct BinaryAddExp: public Exp< BinaryAddExp<TLhs,TRhs> >{
+ const TLhs& lhs;
+ const TRhs& rhs;
+ BinaryAddExp(const TLhs& lhs, const TRhs& rhs):lhs(lhs),rhs(rhs){}
+ // evaluation function, evaluate this expression at position i
+ inline float Eval( int i ) const{
+ return lhs.Eval(i) + rhs.Eval(i);
+ }
+};
+// no constructor and destructor to allocate and de-allocate memory, allocation done by user
+struct Vec: public Exp<Vec>{
+ int len;
+ float* dptr;
+ Vec (void){}
+ Vec (float *dptr, int len):len(len),dptr(dptr){}
+ // here is where evaluation happens
+ template<typename EType>
+ inline Vec& operator= (const Exp<EType>& src_){
+ const EType &src = src_.self();
+ for( int i=0; i < len; ++i ){
+ dptr[i] = src.Eval(i);
+ }
+ return *this;
+ }
+ // evaluation function, evaluate this expression at position i
+ inline float Eval( int i ) const{
+ return dptr[i];
+ }
+};
+// template add, works for any expressions
+template<typename TLhs, typename TRhs>
+inline BinaryAddExp<TLhs,TRhs> operator+ (const Exp<TLhs>& lhs, const Exp<TRhs>& rhs){
+ return BinaryAddExp<TLhs,TRhs>(lhs.self(), rhs.self());
+}
+
+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, this expression is longer:)
+ A = B + C + C;
+ for( int i = 0; i < n; ++ i ){
+ printf("%d:%f==%f+%f+%f\n", i, A.dptr[i], B.dptr[i], C.dptr[i], C.dptr[i] );
+ }
+ return 0;
+}