/[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 148 by jgs, Tue Aug 23 01:24:31 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();
169    int numSamples=data.getNumSamples();    int numSamples=data.getNumSamples();
170    int resultVectorLength=numDPPSample*numSamples;    double global_current_value=initial_value;
171    std::vector<double> resultVector(resultVectorLength);    double local_current_value;
172    DataArrayView dataView=data.getPointDataView();    DataArrayView dataView=data.getPointDataView();
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    // reducing the result for each data-point into the current_value variables
175    // in resultVector    #pragma omp parallel private(local_current_value)
176    //    {
177    // this loop cannot be prallelised as "operation" is an instance of DataAlgorithmAdapter        local_current_value=initial_value;
178    // which maintains state between calls which would be corrupted by parallel execution        #pragma omp for private(i,j) schedule(static)
179    for (i=0;i<numSamples;i++) {        for (i=0;i<numSamples;i++) {
180      for (j=0;j<numDPPSample;j++) {          for (j=0;j<numDPPSample;j++) {
181        resultVector[j*numSamples+i]=dataView.reductionOp(data.getPointOffset(i,j),operation);            local_current_value=operation(local_current_value,dataView.reductionOp(data.getPointOffset(i,j),operation,initial_value));
182      }          }
183    }        }
184    // now calculate the reduction operation value across the results        #pragma omp critical
185    // for each data-point        global_current_value=operation(global_current_value,local_current_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();  
   for (int l=0;l<resultVectorLength;l++) {  
     operation(resultVector[l]);  
186    }    }
187    return operation.getResult();    return global_current_value;
188  }  }
189    
190  template <class UnaryFunction>  template <class BinaryFunction>
191  inline  inline
192  double  double
193  algorithm(DataTagged& data,  algorithm(DataTagged& data,
194            UnaryFunction operation)            BinaryFunction operation,
195          double initial_value)
196  {  {
197    const DataTagged::DataMapType& lookup=data.getTagLookup();    const DataTagged::DataMapType& lookup=data.getTagLookup();
198    DataTagged::DataMapType::const_iterator i;    DataTagged::DataMapType::const_iterator i;
199    DataTagged::DataMapType::const_iterator lookupEnd=lookup.end();    DataTagged::DataMapType::const_iterator lookupEnd=lookup.end();
200    DataArrayView& dataView=data.getPointDataView();    DataArrayView& dataView=data.getPointDataView();
201    std::vector<double> resultVector;    double current_value=initial_value;
   int resultVectorLength;  
202    // perform the operation on each tagged value    // perform the operation on each tagged value
203    for (i=lookup.begin();i!=lookupEnd;i++) {    for (i=lookup.begin();i!=lookupEnd;i++) {
204      resultVector.push_back(dataView.reductionOp(i->second,operation));      current_value=operation(current_value,dataView.reductionOp(i->second,operation,initial_value));
205    }    }
206    // perform the operation on the default value    // perform the operation on the default value
207    resultVector.push_back(data.getDefaultValue().reductionOp(operation));    current_value=operation(current_value,data.getDefaultValue().reductionOp(operation,initial_value));
208    // now calculate the reduction operation value across the results    return current_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  
   resultVectorLength=resultVector.size();  
   operation.resetResult();  
   for (int l=0;l<resultVectorLength;l++) {  
     operation(resultVector[l]);  
   }  
   return operation.getResult();  
209  }  }
210    
211  template <class UnaryFunction>  template <class BinaryFunction>
212  inline  inline
213  double  double
214  algorithm(DataConstant& data,  algorithm(DataConstant& data,
215            UnaryFunction operation)            BinaryFunction operation,
216          double initial_value)
217  {  {
218    return data.getPointDataView().reductionOp(operation);    return data.getPointDataView().reductionOp(operation,initial_value);
219  }  }
220    
221  /**  /**
# Line 237  algorithm(DataConstant& data, Line 229  algorithm(DataConstant& data,
229    
230     Calls DataArrayView::reductionOp     Calls DataArrayView::reductionOp
231  */  */
232  template <class UnaryFunction>  template <class BinaryFunction>
233  inline  inline
234  void  void
235  dp_algorithm(DataExpanded& data,  dp_algorithm(DataExpanded& data,
236               DataExpanded& result,               DataExpanded& result,
237               UnaryFunction operation)               BinaryFunction operation,
238             double initial_value)
239  {  {
240    int i,j;    int i,j;
241    int numSamples=data.getNumSamples();    int numSamples=data.getNumSamples();
# Line 251  dp_algorithm(DataExpanded& data, Line 244  dp_algorithm(DataExpanded& data,
244    DataArrayView resultView=result.getPointDataView();    DataArrayView resultView=result.getPointDataView();
245    // perform the operation on each data-point and assign    // perform the operation on each data-point and assign
246    // this to the corresponding element in result    // this to the corresponding element in result
247    //    #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  
248    for (i=0;i<numSamples;i++) {    for (i=0;i<numSamples;i++) {
249      for (j=0;j<numDPPSample;j++) {      for (j=0;j<numDPPSample;j++) {
250        resultView.getData(result.getPointOffset(i,j)) =        resultView.getData(result.getPointOffset(i,j)) =
251          dataView.reductionOp(data.getPointOffset(i,j),operation);          dataView.reductionOp(data.getPointOffset(i,j),operation,initial_value);
252      }      }
253    }    }
254  }  }
255    
256  template <class UnaryFunction>  template <class BinaryFunction>
257  inline  inline
258  void  void
259  dp_algorithm(DataTagged& data,  dp_algorithm(DataTagged& data,
260               DataTagged& result,               DataTagged& result,
261               UnaryFunction operation)               BinaryFunction operation,
262             double initial_value)
263  {  {
264    const DataTagged::DataMapType& lookup=data.getTagLookup();    const DataTagged::DataMapType& lookup=data.getTagLookup();
265    DataTagged::DataMapType::const_iterator i;    DataTagged::DataMapType::const_iterator i;
# Line 276  dp_algorithm(DataTagged& data, Line 268  dp_algorithm(DataTagged& data,
268    DataArrayView resultView=result.getPointDataView();    DataArrayView resultView=result.getPointDataView();
269    // perform the operation on each tagged data value    // perform the operation on each tagged data value
270    // 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  
271    for (i=lookup.begin();i!=lookupEnd;i++) {    for (i=lookup.begin();i!=lookupEnd;i++) {
272      resultView.getData(i->second) =      resultView.getData(i->second) =
273        dataView.reductionOp(i->second,operation);        dataView.reductionOp(i->second,operation,initial_value);
274    }    }
275    // perform the operation on the default data value    // perform the operation on the default data value
276    // and assign this to the default element in result    // and assign this to the default element in result
277    resultView.getData(0) =    resultView.getData(0) =
278      data.getDefaultValue().reductionOp(operation);      data.getDefaultValue().reductionOp(operation,initial_value);
279  }  }
280    
281  template <class UnaryFunction>  template <class BinaryFunction>
282  inline  inline
283  void  void
284  dp_algorithm(DataConstant& data,  dp_algorithm(DataConstant& data,
285               DataConstant& result,               DataConstant& result,
286               UnaryFunction operation)               BinaryFunction operation,
287             double initial_value)
288  {  {
289    // perform the operation on the data value    // perform the operation on the data value
290    // and assign this to the element in result    // and assign this to the element in result
291    result.getPointDataView().getData(0) =    result.getPointDataView().getData(0) =
292      data.getPointDataView().reductionOp(operation);      data.getPointDataView().reductionOp(operation,initial_value);
293  }  }
294    
295  } // end of namespace  } // end of namespace

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

  ViewVC Help
Powered by ViewVC 1.1.26