/[escript]/branches/schroedinger/escript/src/Data.h
ViewVC logotype

Diff of /branches/schroedinger/escript/src/Data.h

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

revision 1333 by matt, Tue Oct 23 03:28:51 2007 UTC revision 1334 by matt, Thu Oct 25 05:08:54 2007 UTC
# Line 1201  class Data { Line 1201  class Data {
1201    ESCRIPT_DLL_API    ESCRIPT_DLL_API
1202    inline    inline
1203    void    void
1204    unaryOp(UnaryFunction operation);    unaryOp2(UnaryFunction operation);
1205    
1206    /**    /**
1207       \brief       \brief
# Line 1624  Data::binaryOp(const Data& right, Line 1624  Data::binaryOp(const Data& right,
1624    
1625  /**  /**
1626    \brief    \brief
   Perform the given unary operation on other and return the result.  
   Given operation is performed on each element of each data point, thus  
   argument object is a rank n Data object, and returned object is a rank n  
   Data object.  
   Calls Data::unaryOp.  
 */  
 template <class UnaryFunction>  
 inline  
 Data  
 unaryOp(const Data& other,  
         UnaryFunction operation)  
 {  
   Data result;  
   result.copy(other);  
   result.unaryOp(operation);  
   return result;  
 }  
   
 /**  
   \brief  
   Perform the given unary operation on this.  
   Given operation is performed on each element of each data point.  
   Calls escript::unaryOp.  
 */  
 template <class UnaryFunction>  
 inline  
 void  
 Data::unaryOp(UnaryFunction operation)  
 {  
   if (isExpanded()) {  
     DataExpanded* leftC=dynamic_cast<DataExpanded*>(m_data.get());  
     EsysAssert((leftC!=0), "Programming error - casting to DataExpanded.");  
     escript::unaryOp(*leftC,operation);  
   } else if (isTagged()) {  
     DataTagged* leftC=dynamic_cast<DataTagged*>(m_data.get());  
     EsysAssert((leftC!=0), "Programming error - casting to DataTagged.");  
     escript::unaryOp(*leftC,operation);  
   } else if (isConstant()) {  
     DataConstant* leftC=dynamic_cast<DataConstant*>(m_data.get());  
     EsysAssert((leftC!=0), "Programming error - casting to DataConstant.");  
     escript::unaryOp(*leftC,operation);  
   }  
 }  
   
 /**  
   \brief  
1627    Perform the given Data object reduction algorithm on this and return the result.    Perform the given Data object reduction algorithm on this and return the result.
1628    Given operation combines each element of each data point, thus argument    Given operation combines each element of each data point, thus argument
1629    object (*this) is a rank n Data object, and returned object is a scalar.    object (*this) is a rank n Data object, and returned object is a scalar.
# Line 2560  C_TensorBinaryOperation(Data const &arg_ Line 2514  C_TensorBinaryOperation(Data const &arg_
2514    }    }
2515    
2516    return res;    return res;
2517    }
2518    
2519    template <typename UnaryFunction>
2520    Data
2521    C_TensorUnaryOperation(Data const &arg_0,
2522                           UnaryFunction operation)
2523    {
2524      // Interpolate if necessary and find an appropriate function space
2525      Data arg_0_Z = Data(arg_0);
2526    
2527      // Get rank and shape of inputs
2528      int rank0 = arg_0_Z.getDataPointRank();
2529      DataArrayView::ShapeType shape0 = arg_0_Z.getDataPointShape();
2530      int size0 = arg_0_Z.getDataPointSize();
2531    
2532      // Declare output Data object
2533      Data res;
2534    
2535      if (arg_0_Z.isConstant()) {
2536        res = Data(0.0, shape0, arg_0_Z.getFunctionSpace());      // DataConstant output
2537        double *ptr_0 = &((arg_0_Z.getPointDataView().getData())[0]);
2538        double *ptr_2 = &((res.getPointDataView().getData())[0]);
2539        tensor_unary_operation(size0, ptr_0, ptr_2, operation);
2540      }
2541      else if (arg_0_Z.isTagged()) {
2542    
2543        // Borrow DataTagged input from Data object
2544        DataTagged* tmp_0=dynamic_cast<DataTagged*>(arg_0_Z.borrowData());
2545    
2546        // Prepare a DataTagged output 2
2547        res = Data(0.0, shape0, arg_0_Z.getFunctionSpace());   // DataTagged output
2548        res.tag();
2549        DataTagged* tmp_2=dynamic_cast<DataTagged*>(res.borrowData());
2550    
2551        // Get the views
2552        DataArrayView view_0 = tmp_0->getDefaultValue();
2553        DataArrayView view_2 = tmp_2->getDefaultValue();
2554        // Get the pointers to the actual data
2555        double *ptr_0 = &((view_0.getData())[0]);
2556        double *ptr_2 = &((view_2.getData())[0]);
2557        // Compute a result for the default
2558        tensor_unary_operation(size0, ptr_0, ptr_2, operation);
2559        // Compute a result for each tag
2560        const DataTagged::DataMapType& lookup_0=tmp_0->getTagLookup();
2561        DataTagged::DataMapType::const_iterator i; // i->first is a tag, i->second is an offset into memory
2562        for (i=lookup_0.begin();i!=lookup_0.end();i++) {
2563          tmp_2->addTaggedValue(i->first,tmp_2->getDefaultValue());
2564          DataArrayView view_0 = tmp_0->getDataPointByTag(i->first);
2565          DataArrayView view_2 = tmp_2->getDataPointByTag(i->first);
2566          double *ptr_0 = &view_0.getData(0);
2567          double *ptr_2 = &view_2.getData(0);
2568          tensor_unary_operation(size0, ptr_0, ptr_2, operation);
2569        }
2570    
2571      }
2572      else if (arg_0_Z.isExpanded()) {
2573    
2574        res = Data(0.0, shape0, arg_0_Z.getFunctionSpace(),true); // DataExpanded output
2575        DataExpanded* tmp_0=dynamic_cast<DataExpanded*>(arg_0_Z.borrowData());
2576        DataExpanded* tmp_2=dynamic_cast<DataExpanded*>(res.borrowData());
2577    
2578        int sampleNo_0,dataPointNo_0;
2579        int numSamples_0 = arg_0_Z.getNumSamples();
2580        int numDataPointsPerSample_0 = arg_0_Z.getNumDataPointsPerSample();
2581        #pragma omp parallel for private(sampleNo_0,dataPointNo_0) schedule(static)
2582        for (sampleNo_0 = 0; sampleNo_0 < numSamples_0; sampleNo_0++) {
2583          for (dataPointNo_0 = 0; dataPointNo_0 < numDataPointsPerSample_0; dataPointNo_0++) {
2584            int offset_0 = tmp_0->getPointOffset(sampleNo_0,dataPointNo_0);
2585            int offset_2 = tmp_2->getPointOffset(sampleNo_0,dataPointNo_0);
2586            double *ptr_0 = &((arg_0_Z.getPointDataView().getData())[offset_0]);
2587            double *ptr_2 = &((res.getPointDataView().getData())[offset_2]);
2588            tensor_unary_operation(size0, ptr_0, ptr_2, operation);
2589          }
2590        }
2591    
2592      }
2593      else {
2594        throw DataException("Error - C_TensorUnaryOperation: unknown combination of inputs");
2595      }
2596    
2597      return res;
2598  }  }
2599    
2600  }  }

Legend:
Removed from v.1333  
changed lines
  Added in v.1334

  ViewVC Help
Powered by ViewVC 1.1.26