/[escript]/branches/arrexp_2137_win/escript/src/DataAbstract.cpp
ViewVC logotype

Contents of /branches/arrexp_2137_win/escript/src/DataAbstract.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2212 - (show annotations)
Wed Jan 14 00:15:00 2009 UTC (11 years, 5 months ago) by jfenwick
File size: 6215 byte(s)
Executive summary:

This commit adds copy on write checks to operations involving shared data. 

Changes:

new #defines:
~~~~~~~~~~~~~
Data.cpp has ASSIGNMENT_MEANS_DEEPCOPY (defaults to undefined).
Defining this will put the data = operator back to making deep copies instead
of sharing data (now the default.)

Data:
~~~~~
. Added exclusiveWrite method to copy the underlying data if it is shared.
. Some operators which took python objects now call the c++ versions intead of duplicating code.

DataAbstract and offspring:
~~~~~~~~~~~~~~~~~~~~~~~~~~~
. Added method to determine whether the data is currently shared.
. Added getVectorRO to children of DataReady.
. Added getTagRO.

. Operations which modify values in place (or return modifiable pointers) now use
a macro to check for sharing. In the case where a modification attempt is detected, it throws an exception. In the future, I will enable this only for debugging.

. This shold not really have been required but the compiler was not choosing the use the const version as I would have liked. Besides, this makes things explict.

. Moved (and de-inlined) getVector in DataConstant (It was virtual in a parent class).

Unit tests:
~~~~~~~~~~~
Added both python and c++ unit tests to check known cases of sharing and "inplace"
modification operations.

General:
~~~~~~~~
Removed some commented out code.

1
2 /*******************************************************
3 *
4 * Copyright (c) 2003-2008 by University of Queensland
5 * Earth Systems Science Computational Center (ESSCC)
6 * http://www.uq.edu.au/esscc
7 *
8 * Primary Business: Queensland, Australia
9 * Licensed under the Open Software License version 3.0
10 * http://www.opensource.org/licenses/osl-3.0.php
11 *
12 *******************************************************/
13
14
15 #include "DataAbstract.h"
16 #include "DataException.h"
17 #include "DataLazy.h"
18
19 using namespace std;
20
21 namespace escript {
22
23 DataAbstract_ptr DataAbstract::getPtr()
24 {
25 if (_internal_weak_this.expired())
26 {
27 return DataAbstract_ptr(this);
28 }
29 else
30 {
31 return shared_from_this();
32 }
33 }
34
35 const_DataAbstract_ptr DataAbstract::getPtr() const
36 {
37 if (_internal_weak_this.expired())
38 {
39 return const_DataAbstract_ptr(this);
40 }
41 else
42 {
43 return shared_from_this();
44 }
45 }
46
47
48 // Warning - this method uses .use_count() which the boost doco labels inefficient.
49 // If this method needs to be called in debug contexts, we may need to do some
50 // timing experiments to determine how inefficient and possibly switch over to
51 // invasive pointers which can answer these questions faster
52 bool DataAbstract::checkNoSharing() const
53 {
54 if (_internal_weak_this.expired()) // there is no shared_ptr for this object yet
55 {
56 return true;
57 }
58 if (shared_from_this().use_count()==2) // shared_from_this will increase the ref count
59 { // which is the reason .unique is no use.
60 return true;
61 }
62 return false;
63 }
64
65 bool
66 DataAbstract::isLazy() const
67 {
68 return (dynamic_cast<const DataLazy*>(this)!=0);
69 }
70
71
72
73 DataAbstract::DataAbstract(const FunctionSpace& what, const ShapeType& shape, bool isDataEmpty):
74 m_noSamples(what.getNumSamples()),
75 m_noDataPointsPerSample(what.getNumDPPSample()),
76 m_functionSpace(what),
77 m_shape(shape),
78 m_novalues(DataTypes::noValues(shape)),
79 m_rank(DataTypes::getRank(shape))
80
81 {
82 m_isempty=isDataEmpty;
83 if (m_rank>ESCRIPT_MAX_DATA_RANK)
84 {
85 ostringstream os;
86 os << "Error - Attempt to create a rank " << m_rank
87 << " object. The maximum rank is " << ESCRIPT_MAX_DATA_RANK << ".";
88 throw DataException(os.str());
89 }
90 }
91
92 DataAbstract::~DataAbstract()
93 {
94 }
95
96
97 void
98 DataAbstract::operandCheck(const DataAbstract& right) const
99 {
100 if ((right.getNumDPPSample()!=getNumDPPSample()) ||
101 (right.getNumSamples()!=getNumSamples()) ||
102 (right.getFunctionSpace()!=getFunctionSpace())) {
103 stringstream temp;
104 temp << "Error - Right hand argument sample shape or function space "
105 << "incompatible with left." << endl
106 << "LHS: (" << getNumSamples() << ","
107 << getNumDPPSample() << ") " << getFunctionSpace().toString()
108 << endl
109 << "RHS: (" << right.getNumSamples() << ","
110 << right.getNumDPPSample() << ") "
111 << right.getFunctionSpace().toString();
112 throw DataException(temp.str());
113 }
114
115 //
116 // Check the shape of the point data, a rank of 0(scalar) is okay
117 if (!((right.getRank()==0) || (getRank()==0) ||
118 (right.getShape()==getShape())))
119 {
120 stringstream temp;
121 temp << "Error - Right hand argument point data shape: "
122 << DataTypes::shapeToString(right.getShape())
123 << " doesn't match left: "
124 << DataTypes::shapeToString(getShape());
125 throw DataException(temp.str());
126 }
127 }
128
129 void
130 DataAbstract::dump(const std::string fileName) const
131 {
132 throw DataException("Error - DataAbstract::dump: not implemented.");
133 }
134
135
136
137 DataAbstract::ValueType::value_type*
138 DataAbstract::getSampleDataByTag(int tag)
139 {
140 throw DataException("Error - DataAbstract::getSampleDataByTag: Data type does not have tag values.");
141 }
142
143
144 void
145 DataAbstract::setTaggedValue(int tagKey,
146 const DataTypes::ShapeType& pointshape,
147 const DataTypes::ValueType& value,
148 int dataOffset)
149 {
150 throw DataException("Error - DataAbstract::setTaggedValue: Data type does not have tag values.");
151 }
152
153
154 int
155 DataAbstract::getTagNumber(int dpno)
156 {
157 throw DataException("Error - DataAbstract::getTagNumber: Data type cannot be accessed by tag values.");
158 return (0);
159 }
160
161 void
162 DataAbstract::copyToDataPoint(const int sampleNo, const int dataPointNo, const double value)
163 {
164 throw DataException("Error - DataAbstract::copying data from double value to a single data point is not supported.");
165 }
166
167
168 void
169 DataAbstract::copyToDataPoint(const int sampleNo, const int dataPointNo, const WrappedArray& value)
170 {
171 throw DataException("Error - DataAbstract::copying data from WrappedArray objects to a single data point is not supported.");
172 }
173
174
175 void
176 DataAbstract::symmetric(DataAbstract* ev)
177 {
178 throw DataException("Error - DataAbstract::symmetric is not supported.");
179 }
180
181 void
182 DataAbstract::nonsymmetric(DataAbstract* ev)
183 {
184 throw DataException("Error - DataAbstract::nonsymmetric is not supported.");
185 }
186
187 void
188 DataAbstract::trace(DataAbstract* ev, int axis_offset)
189 {
190 throw DataException("Error - DataAbstract::trace is not supported.");
191 }
192
193 void
194 DataAbstract::swapaxes(DataAbstract* ev, int axis0, int axis1)
195 {
196 throw DataException("Error - DataAbstract::component swapaxes is not supported.");
197 }
198 void
199 DataAbstract::transpose(DataAbstract* ev, int axis_offset)
200 {
201 throw DataException("Error - DataAbstract::transpose is not supported.");
202 }
203
204 void
205 DataAbstract::eigenvalues(DataAbstract* ev)
206 {
207 throw DataException("Error - DataAbstract::eigenvalues is not supported.");
208 }
209 void
210 DataAbstract::eigenvalues_and_eigenvectors(DataAbstract* ev,DataAbstract* V,const double tol)
211 {
212 throw DataException("Error - DataAbstract::eigenvalues_and_eigenvectors is not supported.");
213
214 }
215 void
216 DataAbstract::setToZero()
217 {
218 throw DataException("Error - DataAbstract:: cannot set values to zero.");
219 }
220
221 void
222 DataAbstract::reorderByReferenceIDs(int *reference_ids)
223 {
224 throw DataException("Error - DataAbstract:: cannot reorder by reference ids.");
225 }
226
227
228 // DataTypes::ValueType&
229 // DataAbstract::getVector()
230 // {
231 // throw DataException("Error - DataAbstract:: does not have a DataVector.");
232 // }
233 //
234 // const DataTypes::ValueType&
235 // DataAbstract::getVector() const
236 // {
237 // throw DataException("Error - DataAbstract:: does not have a DataVector.");
238 // }
239
240
241
242
243 } // end of namespace

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

  ViewVC Help
Powered by ViewVC 1.1.26