Skip to content

Commit

Permalink
Merge pull request apache#17 from winstywang/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
tqchen committed Dec 16, 2014
2 parents f2eb4c2 + c874225 commit f966f11
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 6 deletions.
2 changes: 1 addition & 1 deletion example/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
export CC = gcc
export CXX = g++
export NVCC =nvcc
export CFLAGS = -Wall -O3 -msse3 -Wno-unknown-pragmas -funroll-loops -I../
export CFLAGS = -Wall -O3 -msse3 -Wno-unknown-pragmas -funroll-loops -I../ -I/opt/intel/mkl/include -I/usr/local/cuda/include/ -L/opt/intel/mkl/lib/intel64 -L/opt/intel/lib/intel64 -L/usr/local/cuda/lib64
export LDFLAGS= -lm -lcudart -lcublas -lmkl_core -lmkl_intel_lp64 -lmkl_intel_thread -liomp5 -lpthread
export NVCCFLAGS = -O3 --use_fast_math -ccbin $(CXX)

Expand Down
16 changes: 16 additions & 0 deletions example/basic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,22 @@ int main( void ){
}
printf("\n");
}

// create a tensor without explictly allocating spaces.
Tensor<cpu, 2> mat3 = NewTensor<cpu>(Shape2(2, 5), 0.0f);
Tensor<cpu, 2> mat4 = NewTensor<cpu>(Shape2(2, 5), 1.0f);
// transpose, and then add mat4.
mat3 = mat.T() + mat4;

// index the shape using size(), this is more natural for MATLAB/numpy user.
printf("%u X %u matrix\n", mat3.size(0), mat3.size(1) );
// print out matrix
for( index_t i = 0; i < mat3.size(0); i ++ ){
for( index_t j = 0; j < mat3.size(1); j ++ ){
printf("%.2f ", mat3[i][j]);
}
printf("\n");
}
// shutdown tensor enigne after usage
ShutdownTensorEngine();
return 0;
Expand Down
8 changes: 4 additions & 4 deletions mshadow/tensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ namespace mshadow {
* \return the size
*/
MSHADOW_XINLINE index_t size(index_t i) const {
return shape_[dimension - 1 - i];
return shape[dimension - 1 - i];
}
/*!
* \brief flatten the tensor to 2 dimension, collapse the higher dimensions together
Expand Down Expand Up @@ -303,7 +303,7 @@ namespace mshadow {
(dptr) + begin, s);
}
MSHADOW_XINLINE index_t size(index_t i) const {
return shape_[0];
return shape[0];
}
MSHADOW_XINLINE real_t &operator[](index_t idx) { return dptr[ idx ]; }
MSHADOW_XINLINE const real_t &operator[](index_t idx)const { return dptr[ idx ]; }
Expand Down Expand Up @@ -470,12 +470,12 @@ namespace mshadow{

// execution implementation of expression evaluations
#include "tensor_expr_engine-inl.hpp"
// extension of expressions
#include "tensor_expr_ext.h"
// cpu implementation of functions
#include "tensor_cpu-inl.hpp"
// gpu implementation of functions
#include "tensor_gpu-inl.hpp"
// extension of expressions
#include "tensor_expr_ext.h"
// io
#include "tensor_io.h"
// container
Expand Down
2 changes: 1 addition & 1 deletion mshadow/tensor_expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ namespace mshadow{

/*! \brief represent a transpose expression of a container */
template<typename EType>
struct TransposeExp: public Exp< TransposeExp<EType>, type::kComplex >{
struct TransposeExp: public Exp< TransposeExp<EType>, type::kMapper >{
public:
/*! \brief expression to be transposed */
const EType &exp;
Expand Down
30 changes: 30 additions & 0 deletions mshadow/tensor_expr_engine-inl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,17 @@ namespace mshadow{
Plan<SubType> src_;
};

template<typename EType>
class Plan< TransposeExp<EType> >{
public:
Plan( const Plan<EType> &src ):src_(src){}
MSHADOW_XINLINE real_t Eval( index_t y, index_t x ) const{
return src_.Eval( x, y );
}
private:
Plan<EType> src_;
};

// allow UnaryMap see the plan
template<typename OP, typename TA, typename TB, int etype>
inline Plan< BinaryMapExp<OP,TA,TB,etype> > MakePlan( const BinaryMapExp<OP,TA,TB,etype> &e );
Expand All @@ -125,6 +136,11 @@ namespace mshadow{
return Plan<T>( e.self() );
}

template<typename T>
inline Plan<TransposeExp<T> > MakePlan( const TransposeExp<T> &e ){
return Plan<TransposeExp<T> >( MakePlan(e.exp) );
}

template<typename T, typename SrcExp, int dim>
inline Plan< T > MakePlan( const MakeTensorExp<T,SrcExp,dim> &e ){
return Plan< T >( e.real_self() );
Expand Down Expand Up @@ -159,6 +175,11 @@ namespace mshadow{
const static int kDim = 0;
const static int kDevMask = 0xffff;
};
template<typename E>
struct ExpInfo<TransposeExp< E > >{
const static int kDim = ExpInfo<E>::kDim;
const static int kDevMask = ExpInfo<E>::kDevMask;
};
template<typename Device, int dim>
struct ExpInfo< Tensor<Device,dim> >{
const static int kDim = dim;
Expand Down Expand Up @@ -224,6 +245,15 @@ namespace mshadow{
return shape;
}
};
template<int dim, typename E>
struct ShapeCheck<dim, TransposeExp< E > >{
inline static Shape<dim> Check( const TransposeExp< E > &e ){
// swap the lowest two dimensions
Shape<dim> s = ShapeCheck<dim,E>::Check( e.exp );
std::swap(s[0], s[1]);
return s;
}
};
template<int dim,typename Device>
struct ShapeCheck<dim,Tensor<Device,dim> >{
inline static Shape<dim> Check( const Tensor<Device,dim> &t ){
Expand Down

0 comments on commit f966f11

Please sign in to comment.