Skip to content

Commit

Permalink
major update move constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaffaljidhmah2 committed Mar 22, 2019
1 parent 363e205 commit 72eb8ca
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 29 deletions.
8 changes: 4 additions & 4 deletions include/variable.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ namespace dlframework{
Tensor();
Tensor(float);
Tensor(const std::initializer_list<unsigned> & init_shape);
Tensor(const Tensor & rhs); //change to shadow copy !
Tensor(const Tensor & rhs);
Tensor(Tensor && rhs);
virtual ~Tensor();

void copy(const Tensor & rhs);

Tensor & operator=(const std::initializer_list<float> & array);
Tensor & operator=(const Tensor & rhs); //shadow copy
Tensor & operator=(const Tensor & rhs);
Tensor & operator=(Tensor && rhs);
float & operator()(const std::initializer_list<unsigned> & indices);
Tensor & operator+=(const Tensor & b);
Tensor operator+(const Tensor& b) const;
Expand Down
18 changes: 11 additions & 7 deletions src/functional.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@ Tensor matmul(const Tensor & M, const Tensor & x)
//assert M.shape[1] == x.shape[0]

Tensor res({m,k});
for (int i=0;i<m;++i)
for (unsigned i=0;i<m;++i)
{
for (int j=0;j<k;++j)
for (unsigned j=0;j<k;++j)
{
float & current = res.p[i*k+j];
for (int l=0;l<n;++l)
current=0.0;
for (unsigned l=0;l<n;++l)
{
current += M.p[i*n+l] * x.p[l*k+j];
}
}
}
return res;
Expand All @@ -31,10 +34,11 @@ Tensor matmul(const Tensor & M, const Tensor & x)
//assert M.shape[1] == x.shape[0]

Tensor res({m});
for (int i=0;i<m;++i)
for (unsigned i=0;i<m;++i)
{
float & current = res.p[i];
for (int l=0;l<n;++l)
current=0;
for (unsigned l=0;l<n;++l)
current += M.p[i*n+l] * x.p[l];
}
return res;
Expand All @@ -55,7 +59,7 @@ Tensor sub(const Tensor & a, const Tensor & b)
Tensor max(const Tensor & a, const Tensor & b)
{
//assert a.shape==b.shape
Tensor res;res.copy(a);
Tensor res(a);
for (int i=0;i<a.length;++i)
if (res.p[i]<b.p[i]) res.p[i]=b.p[i];
return res;
Expand All @@ -64,7 +68,7 @@ Tensor max(const Tensor & a, const Tensor & b)
Tensor min(const Tensor & a, const Tensor & b)
{
//assert a.shape==b.shape
Tensor res;res.copy(a);
Tensor res(a);
for (int i=0;i<a.length;++i)
if (res.p[i]>b.p[i]) res.p[i]=b.p[i];
return res;
Expand Down
51 changes: 34 additions & 17 deletions src/tensor.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "variable.h"
#include "operator.h"
#include <iostream> //debug

namespace dlframework{

Tensor::Tensor(float x){
Expand Down Expand Up @@ -35,22 +35,23 @@ Tensor::Tensor()
dim=0;length=0;p=nullptr;shape[0]=1;
}

Tensor::Tensor(const Tensor & rhs) //shadow copy
Tensor::Tensor(const Tensor & rhs)
{
dim=rhs.dim;
length=rhs.length;
for (unsigned i=0;i<=dim;++i) shape[i]=rhs.shape[i];
p=rhs.p;
p=new float[length];
for (unsigned i=0;i<length;++i) p[i]=rhs.p[i];
}

void Tensor::copy(const Tensor & rhs)

Tensor::Tensor(Tensor && rhs)
{
dim=rhs.dim;
length=rhs.length;
for (unsigned i=0;i<=dim;++i) shape[i]=rhs.shape[i];
if (p!=nullptr) delete[] p;
p=new float[length];
for (unsigned i=0;i<length;++i) p[i]=rhs.p[i];
p=rhs.p;
rhs.p=nullptr;
}

std::ostream& operator<<(std::ostream & o, const Tensor & rhs)
Expand All @@ -77,16 +78,34 @@ std::ostream& operator<<(std::ostream & o, const Tensor & rhs)

Tensor & Tensor::operator=(const Tensor & rhs)
{
dim=rhs.dim;
length=rhs.length;
for (unsigned i=0;i<=dim;++i) shape[i]=rhs.shape[i];
p=rhs.p;
if (this != &rhs)
{
if (p!=nullptr) delete[] p;
dim=rhs.dim;
length=rhs.length;
for (unsigned i=0;i<=dim;++i) shape[i]=rhs.shape[i];
p=new float[length];
for (unsigned i=0;i<length;++i) p[i]=rhs.p[i];
}
return *this;
}

Tensor & Tensor::operator=(Tensor && rhs)
{
if (this != &rhs)
{
if (p!=nullptr) delete[] p;
dim=rhs.dim;
length=rhs.length;
for (unsigned i=0;i<=dim;++i) shape[i]=rhs.shape[i];
p=rhs.p;
rhs.p=nullptr;
}
return *this;
}

Tensor::~Tensor(){
if (p!=nullptr) delete[] p;
std::cout<<"DEBUG INFO: DELETE Tensor with "<<length<<"DATA"<<std::endl;
if (p!=nullptr) delete[] p;
}

float& Tensor::operator()(const std::initializer_list<unsigned> & indices){
Expand Down Expand Up @@ -115,8 +134,7 @@ Tensor & Tensor::operator+=(const Tensor & b)

Tensor Tensor::operator+(const Tensor& b) const
{
Tensor x;
x.copy(*this);
Tensor x(*this);
x+=b;
return x;
}
Expand All @@ -133,8 +151,7 @@ Tensor & Tensor::operator-=(const Tensor & b)

Tensor Tensor::operator-(const Tensor& b) const
{
Tensor x;
x.copy(*this);
Tensor x(*this);
x-=b;
return x;
}
Expand Down
21 changes: 20 additions & 1 deletion src/test.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
#include "variable.h"
#include "operator.h"
#include "functional.h"
#include <iostream>

using namespace std;
using namespace dlframework;

int main()
{
// // Tensors
// // Tensors

//
// Tensor x,y(1.4),z({2,3}),w({2,3,4});
// cout<<x<<endl;
// cout<<y<<endl;
Expand Down Expand Up @@ -54,7 +57,23 @@ int main()
// Tensor tx({2,3});Variable vx(tx); cout<<vx<<endl;
// tx={2,3,7}; cout<<vx<<endl;

// // Functionals

// Tensor x({2,3}),y({2,3});x={1,2,2,1,2,2};y={2,-1,0,4,4,1};
// cout<<x<<endl;cout<<y<<endl;
// cout<<functional::add(x,y)<<endl;
// cout<<functional::sub(x,y)<<endl;
// cout<<functional::sub(y,x)<<endl;
// cout<<functional::add(functional::add(x,y),y)<<endl;

// cout<<functional::max(x,y)<<endl;
// cout<<functional::min(x,y)<<endl;

Tensor x({2,2}),y({2,2});x={1,2,2,1};y={1,0,0,1};
cout<<x<<endl;cout<<y<<endl;
cout<<functional::matmul(x,y)<<endl;
// 1 2 1 0 = 1 2
// 2 1 0 1 = 2 1



Expand Down

0 comments on commit 72eb8ca

Please sign in to comment.