DataVectorAlt.h
Go to the documentation of this file.
/*****************************************************************************
*
* Copyright (c) 2003-2017 by The University of Queensland
* http://www.uq.edu.au
*
* Primary Business: Queensland, Australia
* Licensed under the Apache License, version 2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
* Development until 2012 by Earth Systems Science Computational Center (ESSCC)
* Development 2012-2013 by School of Earth Sciences
* Development from 2014 by Centre for Geoscience Computing (GeoComp)
*
*****************************************************************************/
#ifndef __ESCRIPT_DATAVECTORALT_H__
#define __ESCRIPT_DATAVECTORALT_H__
#include "DataTypes.h"
#include "system_dep.h"
#include "Assert.h"
#include "DataException.h"
#include "WrappedArray.h"
#include <sstream>
namespace escript
{
namespace DataTypes
{
template <class T>
class ESCRIPT_DLL_API DataVectorAlt {
public:
//
// The type of the elements stored in the vector.
typedef T ElementType;
//
// Various types exported to clients of this class.
typedef const ElementType * const_pointer;
typedef ElementType value_type;
typedef DataTypes::vec_size_type size_type;
typedef ElementType & reference;
typedef const ElementType & const_reference;
DataVectorAlt();
DataVectorAlt(const DataVectorAlt<T>& other);
DataVectorAlt(const size_type size,
const value_type val=0.0,
const size_type blockSize=1);
~DataVectorAlt();
void
resize(const size_type newSize,
const value_type newVal=0.0,
const size_type newBlockSize=1);
void
copyFromArray(const WrappedArray& value, size_type copies);
// Please make sure that any implementation changes here are reflected in the specialised
// version in the .cpp file
void
copyFromArrayToOffset(const WrappedArray& value, size_type offset, size_type copies);
inline
size_type
size() const;
DataVectorAlt&
operator=(const DataVectorAlt<T>& other);
bool
operator==(const DataVectorAlt<T>& other) const;
bool
operator!=(const DataVectorAlt<T>& other) const;
inline
reference
operator[](const size_type i);
inline
const_reference
operator[](const size_type i) const;
// for compatibility with std::vector
ElementType* data();
const ElementType* data() const;
protected:
private:
size_type m_size;
size_type m_dim;
size_type m_N;
ElementType* m_array_data;
};
template <class T>
inline
typename DataVectorAlt<T>::ElementType* DataVectorAlt<T>::data()
{
return m_array_data;
}
template <class T>
inline
const typename DataVectorAlt<T>::ElementType* DataVectorAlt<T>::data() const
{
return m_array_data;
}
template <class T>
inline
typename DataVectorAlt<T>::size_type
DataVectorAlt<T>::size() const
{
return m_size;
}
template <class T>
inline
typename DataVectorAlt<T>::reference
DataVectorAlt<T>::operator[](const DataVectorAlt::size_type i)
{
ESYS_ASSERT(i<size(), "DataVectorAlt: invalid index specified, " << i << " of " << size());
return m_array_data[i];
}
template <class T>
inline
typename DataVectorAlt<T>::const_reference
DataVectorAlt<T>::operator[](const DataVectorAlt::size_type i) const
{
ESYS_ASSERT(i<size(), "DataVectorAlt: invalid index specified. " << i << " of " << size());
return m_array_data[i];
}
template <class T>
DataTypes::DataVectorAlt<T>::DataVectorAlt() :
m_size(0),
m_dim(0),
m_N(0),
m_array_data(0)
{
}
template <class T>
DataTypes::DataVectorAlt<T>::DataVectorAlt(const DataVectorAlt& other) :
m_size(other.m_size),
m_dim(other.m_dim),
m_N(other.m_N),
m_array_data(0)
{
m_array_data=reinterpret_cast<T*>(malloc(sizeof(T)*m_size));
int i;
#pragma omp parallel for private(i) schedule(static)
for (i=0; i<m_size; i++) {
m_array_data[i] = other.m_array_data[i];
}
}
template <class T>
DataTypes::DataVectorAlt<T>::DataVectorAlt(const DataVectorAlt<T>::size_type size,
const DataVectorAlt<T>::value_type val,
const DataVectorAlt<T>::size_type blockSize) :
m_size(size),
m_dim(blockSize),
m_array_data(0)
{
resize(size, val, blockSize);
}
template <class T>
DataTypes::DataVectorAlt<T>::~DataVectorAlt()
{
// clear data members
m_size = -1;
m_dim = -1;
m_N = -1;
if (m_array_data!=0)
{
free(m_array_data);
}
m_array_data=0;
}
template <class T>
void
DataVectorAlt<T>::resize(const DataVectorAlt<T>::size_type newSize,
const DataVectorAlt<T>::value_type newValue,
const DataVectorAlt<T>::size_type newBlockSize)
{
// The < 1 is to catch both ==0 and negatives
if ( newBlockSize < 1) {
std::ostringstream oss;
oss << "DataVectorAlt: invalid blockSize specified (" << newBlockSize << ')';
throw DataException(oss.str());
}
if ( newSize < 0 ) {
std::ostringstream oss;
oss << "DataVectorAlt: invalid new size specified (" << newSize << ')';
throw DataException(oss.str());
}
if ( (newSize % newBlockSize) != 0) {
std::ostringstream oss;
oss << "DataVectorAlt: newSize is not a multiple of blockSize: (" << newSize << ", " << newBlockSize<< ')';
throw DataException(oss.str());
}
m_size = newSize;
m_dim = newBlockSize;
m_N = newSize / newBlockSize;
if (m_array_data!=0)
{
free(m_array_data);
}
m_array_data=reinterpret_cast<T*>(malloc(sizeof(T)*m_size));
int i;
#pragma omp parallel for private(i) schedule(static)
for (i=0; i<m_size; i++) {
m_array_data[i] = newValue;
}
}
template <class T>
DataVectorAlt<T>&
DataVectorAlt<T>::operator=(const DataVectorAlt& other)
{
assert(m_size >= 0);
m_size = other.m_size;
m_dim = other.m_dim;
m_N = other.m_N;
if (m_array_data!=0)
{
free(m_array_data);
}
m_array_data=reinterpret_cast<T*>(malloc(sizeof(T)*m_size));
int i;
#pragma omp parallel for private(i) schedule(static)
for (i=0; i<m_size; i++) {
m_array_data[i] = other.m_array_data[i];
}
return *this;
}
template <class T>
bool
DataVectorAlt<T>::operator==(const DataVectorAlt& other) const
{
assert(m_size >= 0);
if (m_size!=other.m_size) {
return false;
}
if (m_dim!=other.m_dim) {
return false;
}
if (m_N!=other.m_N) {
return false;
}
for (int i=0; i<m_size; i++) {
if (m_array_data[i] != other.m_array_data[i]) {
return false;
}
}
return true;
}
template <class T>
bool
DataVectorAlt<T>::operator!=(const DataVectorAlt& other) const
{
return !(*this==other);
}
template <class T>
void
DataVectorAlt<T>::copyFromArrayToOffset(const WrappedArray& value, size_type offset, size_type copies)
{
const DataTypes::ShapeType& tempShape=value.getShape();
size_type len=DataTypes::noValues(tempShape);
if (offset+len*copies>size())
{
std::ostringstream ss;
ss << "Error - not enough room for that DataPoint at that offset. (";
ss << "offset=" << offset << " + " << " len=" << len << " >= " << size();
throw DataException(ss.str());
}
size_type si=0,sj=0,sk=0,sl=0;
switch (value.getRank())
{
case 0:
for (size_type z=0;z<copies;++z)
{
m_array_data[offset+z]=value.getElt();
}
break;
case 1:
for (size_type z=0;z<copies;++z)
{
for (size_t i=0;i<tempShape[0];++i)
{
m_array_data[offset+i]=value.getElt(i);
}
offset+=len;
}
break;
case 2:
si=tempShape[0];
sj=tempShape[1];
for (size_type z=0;z<copies;++z)
{
for (size_type i=0;i<si;i++)
{
for (size_type j=0;j<sj;j++)
{
m_array_data[offset+DataTypes::getRelIndex(tempShape,i,j)]=value.getElt(i,j);
}
}
offset+=len;
}
break;
case 3:
si=tempShape[0];
sj=tempShape[1];
sk=tempShape[2];
for (size_type z=0;z<copies;++z)
{
for (size_type i=0;i<si;i++)
{
for (size_type j=0;j<sj;j++)
{
for (size_type k=0;k<sk;k++)
{
m_array_data[offset+DataTypes::getRelIndex(tempShape,i,j,k)]=value.getElt(i,j,k);
}
}
}
offset+=len;
}
break;
case 4:
si=tempShape[0];
sj=tempShape[1];
sk=tempShape[2];
sl=tempShape[3];
for (size_type z=0;z<copies;++z)
{
for (size_type i=0;i<si;i++)
{
for (size_type j=0;j<sj;j++)
{
for (size_type k=0;k<sk;k++)
{
for (size_type l=0;l<sl;l++)
{
m_array_data[offset+DataTypes::getRelIndex(tempShape,i,j,k,l)]=value.getElt(i,j,k,l);
}
}
}
}
offset+=len;
}
break;
default:
std::ostringstream oss;
oss << "Error - unknown rank. Rank=" << value.getRank();
throw DataException(oss.str());
}
}
template <class T>
void
DataVectorAlt<T>::copyFromArray(const WrappedArray& value, size_type copies)
{
DataTypes::ShapeType tempShape=value.getShape();
DataVectorAlt<T>::size_type nelements=DataTypes::noValues(tempShape)*copies;
if (m_array_data!=0)
{
free(m_array_data);
}
m_array_data=reinterpret_cast<T*>(malloc(sizeof(T)*nelements));
m_size=nelements; // total amount of elements
m_dim=m_size; // elements per sample
m_N=1; // number of samples
copyFromArrayToOffset(value,0,copies);
}
} // end of namespace
} // end of namespace
#endif // __ESCRIPT_DATAVECTORALT_H__
escript::DataTypes::DataVectorAlt::m_array_data
ElementType * m_array_data
Definition: DataVectorAlt.h:194
escript::WrappedArray::getShape
const DataTypes::ShapeType & getShape() const
Definition: WrappedArray.h:82
escript::DataTypes::DataVectorAlt::DataVectorAlt
DataVectorAlt()
Default constructor for DataVectorAlt.
Definition: DataVectorAlt.h:240
WrappedArray.h
escript::DataTypes::DataVectorAlt::reference
ElementType & reference
Definition: DataVectorAlt.h:50
escript::DataTypes::DataVectorAlt::size_type
DataTypes::vec_size_type size_type
Definition: DataVectorAlt.h:49
escript::DataTypes::DataVectorAlt::m_dim
size_type m_dim
Definition: DataVectorAlt.h:191
escript
Definition: AbstractContinuousDomain.cpp:22
escript::WrappedArray::getElt
DataTypes::real_t getElt() const
Definition: WrappedArray.h:88
escript::DataTypes::getRelIndex
vec_size_type getRelIndex(const DataTypes::ShapeType &shape, vec_size_type i)
Compute the offset (in 1D vector) of a given subscript with a shape.
Definition: DataTypes.h:233
escript::DataTypes::ShapeType
std::vector< int > ShapeType
The shape of a single datapoint.
Definition: DataTypes.h:42
escript::WrappedArray::getRank
unsigned int getRank() const
Definition: WrappedArray.h:76
system_dep.h
escript::DataTypes::DataVectorAlt::size
size_type size() const
Return the number of elements in this DataVectorAlt.
Definition: DataVectorAlt.h:214
DataException.h
DataTypes
escript::DataTypes::DataVectorAlt::value_type
ElementType value_type
Definition: DataVectorAlt.h:48
escript::DataTypes::DataVectorAlt::m_size
size_type m_size
Definition: DataVectorAlt.h:190
escript::DataTypes::DataVectorAlt::data
ElementType * data()
Definition: DataVectorAlt.h:199
escript::DataTypes::DataVectorAlt
Definition: DataVectorAlt.h:36
DataTypes.h
Assert.h
escript::DataTypes::DataVectorAlt::ElementType
T ElementType
Definition: DataVectorAlt.h:42
escript::DataTypes::noValues
int noValues(const ShapeType &shape)
Calculate the number of values in a datapoint with the given shape.
Definition: DataTypes.cpp:90
escript::DataException
Definition: DataException.h:26
ESCRIPT_DLL_API
#define ESCRIPT_DLL_API
Definition: escriptcore/src/system_dep.h:29
escript::DataTypes::DataVectorAlt::operator[]
reference operator[](const size_type i)
Return a reference to the element at position i in this DataVectorAlt. Will throw an exception if an ...
Definition: DataVectorAlt.h:222
escript::DataTypes::DataVectorAlt::m_N
size_type m_N
Definition: DataVectorAlt.h:192
escript::DataTypes::DataVectorAlt::const_reference
const ElementType & const_reference
Definition: DataVectorAlt.h:51
ESYS_ASSERT
#define ESYS_ASSERT(a, b)
EsysAssert is a MACRO that will throw an exception if the boolean condition specified is false...
Definition: Assert.h:78
escript::WrappedArray
Definition: WrappedArray.h:31
escript::DataTypes::vec_size_type
long vec_size_type
Definition: DataTypes.h:47
escript::DataTypes::DataVectorAlt::const_pointer
const ElementType * const_pointer
Definition: DataVectorAlt.h:47