Skip to content

Commit

Permalink
Adding move constructor and move assignment operators to Matrix and V…
Browse files Browse the repository at this point in the history
…ector classes. This results in 5% to 10% speedup in computations that involve heavy usage of these classes, and dont use static allocation to avoid malloc calls. Requieres C++11 compatible compiler (tested on gcc5.4 with -std=c++11 flag).

Enable during compilation with the -DUSE_CXX11 compiler flag.
  • Loading branch information
jaabell committed May 2, 2017
1 parent 461cadb commit 3b5dc70
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 10 deletions.
53 changes: 45 additions & 8 deletions DEVELOPER/core/Matrix.cpp
Expand Up @@ -172,19 +172,30 @@ Matrix::Matrix(const Matrix &other)
data = new (nothrow) double[dataSize];
// data = (double *)malloc(dataSize*sizeof(double));
if (data == 0) {
opserr << "WARNING:Matrix::Matrix(Matrix &): ";
opserr << "Ran out of memory on init of size " << dataSize << endln;
numRows = 0; numCols =0; dataSize = 0;
opserr << "WARNING:Matrix::Matrix(Matrix &): ";
opserr << "Ran out of memory on init of size " << dataSize << endln;
numRows = 0; numCols =0; dataSize = 0;
} else {
// copy the data
double *dataPtr = data;
double *otherDataPtr = other.data;
for (int i=0; i<dataSize; i++)
*dataPtr++ = *otherDataPtr++;
// copy the data
double *dataPtr = data;
double *otherDataPtr = other.data;
for (int i=0; i<dataSize; i++)
*dataPtr++ = *otherDataPtr++;
}
}
}

// Move ctor
#ifdef USE_CXX11
Matrix::Matrix(Matrix &&other)
:numRows(other.numRows), numCols(other.numCols), dataSize(other.dataSize), data(other.data), fromFree(0)
{
other.numRows = 0;
other.numCols = 0;
other.dataSize = 0;
other.data = 0;
}
#endif

//
// DESTRUCTOR
Expand Down Expand Up @@ -1198,6 +1209,32 @@ Matrix::operator=(const Matrix &other)
}


// Move assignment
//
#ifdef USE_CXX11
Matrix &
Matrix::operator=( Matrix &&other)
{
// first check we are not trying other = other
if (this == &other)
return *this;


if (this->data != 0)
delete [] this->data;

data = other.data;
this->dataSize = other.numCols*other.numRows;
this->numCols = other.numCols;
this->numRows = other.numRows;
other.data = 0;
other.dataSize = 0;
other.numCols = 0;
other.numRows = 0;

return *this;
}
#endif


// virtual Matrix &operator+=(double fact);
Expand Down
7 changes: 7 additions & 0 deletions DEVELOPER/core/Matrix.h
Expand Up @@ -53,6 +53,9 @@ class Matrix
Matrix(int nrows, int ncols);
Matrix(double *data, int nrows, int ncols);
Matrix(const Matrix &M);
#ifdef USE_CXX11
Matrix( Matrix &&M);
#endif
~Matrix();

// utility methods
Expand Down Expand Up @@ -82,6 +85,10 @@ class Matrix
Matrix operator()(const ID &rows, const ID & cols) const;

Matrix &operator=(const Matrix &M);

#ifdef USE_CXX11
Matrix &operator=(Matrix &&M);
#endif

// matrix operations which will preserve the derived type and
// which can be implemented efficiently without many constructor calls.
Expand Down
37 changes: 37 additions & 0 deletions DEVELOPER/core/Vector.cpp
Expand Up @@ -121,6 +121,22 @@ Vector::Vector(const Vector &other)
}



// Vector(const Vector&):
// Move constructor
#ifdef USE_CXX11
Vector::Vector(Vector &&other)
: sz(other.sz),theData(other.theData),fromFree(0)
{
//opserr << "move ctor!\n";
other.theData = 0;
other.sz = 0;
}
#endif




// ~Vector():
// destructor, deletes the [] data

Expand Down Expand Up @@ -741,6 +757,27 @@ Vector::operator=(const Vector &V)
return *this;
}

// Move assignment operator.
#ifdef USE_CXX11
Vector &
Vector::operator=(Vector &&V)
{
// first check we are not trying v = v
if (this != &V) {
// opserr << "move assign!\n";
if (this->theData != 0) delete [] this->theData;
theData = V.theData;
this->sz = V.sz;
V.theData = 0;
V.sz = 0;
}
return *this;
}
#endif





// Vector &operator+=(double fact):
// The += operator adds fact to each element of the vector, data[i] = data[i]+fact.
Expand Down
10 changes: 8 additions & 2 deletions DEVELOPER/core/Vector.h
Expand Up @@ -46,7 +46,11 @@ class Vector
// constructors and destructor
Vector();
Vector(int);
Vector(const Vector &);
Vector(const Vector &);
#ifdef USE_CXX11
Vector(Vector &&);
#endif

Vector(double *data, int size);
~Vector();

Expand All @@ -72,7 +76,9 @@ class Vector
double &operator[](int x);
Vector operator()(const ID &rows) const;
Vector &operator=(const Vector &V);

#ifdef USE_CXX11
Vector &operator=(Vector &&V);
#endif
Vector &operator+=(double fact);
Vector &operator-=(double fact);
Vector &operator*=(double fact);
Expand Down

0 comments on commit 3b5dc70

Please sign in to comment.