/[escript]/trunk/escript/src/DataAlgorithm.h
ViewVC logotype

Diff of /trunk/escript/src/DataAlgorithm.h

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 122 by jgs, Thu Jun 9 05:38:05 2005 UTC revision 147 by jgs, Fri Aug 12 01:45:47 2005 UTC
# Line 47  class DataAlgorithmAdapter { Line 47  class DataAlgorithmAdapter {
47        m_currentValue(initialValue)        m_currentValue(initialValue)
48      {      {
49      }      }
50        DataAlgorithmAdapter(const DataAlgorithmAdapter& other):
51          m_initialValue(other.m_initialValue),
52          m_currentValue(other.m_initialValue),
53          operation(other.operation)
54        {
55        }
56      inline void operator()(double value)      inline void operator()(double value)
57      {      {
58        m_currentValue=operation(m_currentValue,value);        m_currentValue=operation(m_currentValue,value);
# Line 151  struct Trace : public std::binary_functi Line 157  struct Trace : public std::binary_functi
157    
158     Calls DataArrayView::reductionOp     Calls DataArrayView::reductionOp
159  */  */
160  template <class UnaryFunction>  template <class BinaryFunction>
161  inline  inline
162  double  double
163  algorithm(DataExpanded& data,  algorithm(DataExpanded& data,
164            UnaryFunction operation)            BinaryFunction operation,
165          double initial_value)
166  {  {
167    int i,j;    int i,j;
168    int numDPPSample=data.getNumDPPSample();    int numDPPSample=data.getNumDPPSample();
# Line 166  algorithm(DataExpanded& data, Line 173  algorithm(DataExpanded& data,
173    // calculate the reduction operation value for each data point    // calculate the reduction operation value for each data point
174    // storing the result for each data-point in successive entries    // storing the result for each data-point in successive entries
175    // in resultVector    // in resultVector
176    //    #pragma omp parallel for private(i,j) schedule(static)
   // this loop cannot be prallelised as "operation" is an instance of DataAlgorithmAdapter  
   // which maintains state between calls which would be corrupted by parallel execution  
177    for (i=0;i<numSamples;i++) {    for (i=0;i<numSamples;i++) {
178      for (j=0;j<numDPPSample;j++) {      for (j=0;j<numDPPSample;j++) {
179        resultVector[j*numSamples+i]=dataView.reductionOp(data.getPointOffset(i,j),operation);        resultVector[j*numSamples+i]=dataView.reductionOp(data.getPointOffset(i,j),operation,initial_value);
180      }      }
181    }    }
182    // now calculate the reduction operation value across the results    // now calculate the reduction operation value across the results
183    // for each data-point    // for each data-point
184    //    double current_value=initial_value;
   // this loop cannot be prallelised as "operation" is an instance of DataAlgorithmAdapter  
   // which maintains state between calls which would be corrupted by parallel execution  
   operation.resetResult();  
185    for (int l=0;l<resultVectorLength;l++) {    for (int l=0;l<resultVectorLength;l++) {
186      operation(resultVector[l]);      current_value=operation(current_value,resultVector[l]);
187    }    }
188    return operation.getResult();    return current_value;
189  }  }
190    
191  template <class UnaryFunction>  template <class BinaryFunction>
192  inline  inline
193  double  double
194  algorithm(DataTagged& data,  algorithm(DataTagged& data,
195            UnaryFunction operation)            BinaryFunction operation,
196          double initial_value)
197  {  {
198    const DataTagged::DataMapType& lookup=data.getTagLookup();    const DataTagged::DataMapType& lookup=data.getTagLookup();
199    DataTagged::DataMapType::const_iterator i;    DataTagged::DataMapType::const_iterator i;
# Line 200  algorithm(DataTagged& data, Line 203  algorithm(DataTagged& data,
203    int resultVectorLength;    int resultVectorLength;
204    // perform the operation on each tagged value    // perform the operation on each tagged value
205    for (i=lookup.begin();i!=lookupEnd;i++) {    for (i=lookup.begin();i!=lookupEnd;i++) {
206      resultVector.push_back(dataView.reductionOp(i->second,operation));      resultVector.push_back(dataView.reductionOp(i->second,operation,initial_value));
207    }    }
208    // perform the operation on the default value    // perform the operation on the default value
209    resultVector.push_back(data.getDefaultValue().reductionOp(operation));    resultVector.push_back(data.getDefaultValue().reductionOp(operation,initial_value));
210    // now calculate the reduction operation value across the results    // now calculate the reduction operation value across the results
211    // for each tagged value    // for each tagged value
   //  
   // this loop cannot be prallelised as "operation" is an instance of DataAlgorithmAdapter  
   // which maintains state between calls which would be corrupted by parallel execution  
212    resultVectorLength=resultVector.size();    resultVectorLength=resultVector.size();
213    operation.resetResult();    double current_value=initial_value;
214    for (int l=0;l<resultVectorLength;l++) {    for (int l=0;l<resultVectorLength;l++) {
215      operation(resultVector[l]);      current_value=operation(current_value,resultVector[l]);
216    }    }
217    return operation.getResult();    return current_value;
218  }  }
219    
220  template <class UnaryFunction>  template <class BinaryFunction>
221  inline  inline
222  double  double
223  algorithm(DataConstant& data,  algorithm(DataConstant& data,
224            UnaryFunction operation)            BinaryFunction operation,
225          double initial_value)
226  {  {
227    return data.getPointDataView().reductionOp(operation);    return data.getPointDataView().reductionOp(operation,initial_value);
228  }  }
229    
230  /**  /**
# Line 237  algorithm(DataConstant& data, Line 238  algorithm(DataConstant& data,
238    
239     Calls DataArrayView::reductionOp     Calls DataArrayView::reductionOp
240  */  */
241  template <class UnaryFunction>  template <class BinaryFunction>
242  inline  inline
243  void  void
244  dp_algorithm(DataExpanded& data,  dp_algorithm(DataExpanded& data,
245               DataExpanded& result,               DataExpanded& result,
246               UnaryFunction operation)               BinaryFunction operation,
247             double initial_value)
248  {  {
249    int i,j;    int i,j;
250    int numSamples=data.getNumSamples();    int numSamples=data.getNumSamples();
# Line 251  dp_algorithm(DataExpanded& data, Line 253  dp_algorithm(DataExpanded& data,
253    DataArrayView resultView=result.getPointDataView();    DataArrayView resultView=result.getPointDataView();
254    // perform the operation on each data-point and assign    // perform the operation on each data-point and assign
255    // this to the corresponding element in result    // this to the corresponding element in result
256    //    #pragma omp parallel for private(i,j) schedule(static)
   // this loop cannot be prallelised as "operation" is an instance of DataAlgorithmAdapter  
   // which maintains state between calls which would be corrupted by parallel execution  
257    for (i=0;i<numSamples;i++) {    for (i=0;i<numSamples;i++) {
258      for (j=0;j<numDPPSample;j++) {      for (j=0;j<numDPPSample;j++) {
259        resultView.getData(result.getPointOffset(i,j)) =        resultView.getData(result.getPointOffset(i,j)) =
260          dataView.reductionOp(data.getPointOffset(i,j),operation);          dataView.reductionOp(data.getPointOffset(i,j),operation,initial_value);
261      }      }
262    }    }
263  }  }
264    
265  template <class UnaryFunction>  template <class BinaryFunction>
266  inline  inline
267  void  void
268  dp_algorithm(DataTagged& data,  dp_algorithm(DataTagged& data,
269               DataTagged& result,               DataTagged& result,
270               UnaryFunction operation)               BinaryFunction operation,
271             double initial_value)
272  {  {
273    const DataTagged::DataMapType& lookup=data.getTagLookup();    const DataTagged::DataMapType& lookup=data.getTagLookup();
274    DataTagged::DataMapType::const_iterator i;    DataTagged::DataMapType::const_iterator i;
# Line 276  dp_algorithm(DataTagged& data, Line 277  dp_algorithm(DataTagged& data,
277    DataArrayView resultView=result.getPointDataView();    DataArrayView resultView=result.getPointDataView();
278    // perform the operation on each tagged data value    // perform the operation on each tagged data value
279    // and assign this to the corresponding element in result    // and assign this to the corresponding element in result
   //  
   // this loop cannot be prallelised as "operation" is an instance of DataAlgorithmAdapter  
   // which maintains state between calls which would be corrupted by parallel execution  
280    for (i=lookup.begin();i!=lookupEnd;i++) {    for (i=lookup.begin();i!=lookupEnd;i++) {
281      resultView.getData(i->second) =      resultView.getData(i->second) =
282        dataView.reductionOp(i->second,operation);        dataView.reductionOp(i->second,operation,initial_value);
283    }    }
284    // perform the operation on the default data value    // perform the operation on the default data value
285    // and assign this to the default element in result    // and assign this to the default element in result
286    resultView.getData(0) =    resultView.getData(0) =
287      data.getDefaultValue().reductionOp(operation);      data.getDefaultValue().reductionOp(operation,initial_value);
288  }  }
289    
290  template <class UnaryFunction>  template <class BinaryFunction>
291  inline  inline
292  void  void
293  dp_algorithm(DataConstant& data,  dp_algorithm(DataConstant& data,
294               DataConstant& result,               DataConstant& result,
295               UnaryFunction operation)               BinaryFunction operation,
296             double initial_value)
297  {  {
298    // perform the operation on the data value    // perform the operation on the data value
299    // and assign this to the element in result    // and assign this to the element in result
300    result.getPointDataView().getData(0) =    result.getPointDataView().getData(0) =
301      data.getPointDataView().reductionOp(operation);      data.getPointDataView().reductionOp(operation,initial_value);
302  }  }
303    
304  } // end of namespace  } // end of namespace

Legend:
Removed from v.122  
changed lines
  Added in v.147

  ViewVC Help
Powered by ViewVC 1.1.26