RTOp_ROp_dot_prod.c

00001 /*
00002 // @HEADER
00003 // ***********************************************************************
00004 // 
00005 // Moocho: Multi-functional Object-Oriented arCHitecture for Optimization
00006 //                  Copyright (2003) Sandia Corporation
00007 // 
00008 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00009 // license for use of this work by or on behalf of the U.S. Government.
00010 // 
00011 // This library is free software; you can redistribute it and/or modify
00012 // it under the terms of the GNU Lesser General Public License as
00013 // published by the Free Software Foundation; either version 2.1 of the
00014 // License, or (at your option) any later version.
00015 //  
00016 // This library is distributed in the hope that it will be useful, but
00017 // WITHOUT ANY WARRANTY; without even the implied warranty of
00018 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00019 // Lesser General Public License for more details.
00020 //  
00021 // You should have received a copy of the GNU Lesser General Public
00022 // License along with this library; if not, write to the Free Software
00023 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00024 // USA
00025 // Questions? Contact Roscoe A. Bartlett (rabartl@sandia.gov) 
00026 // 
00027 // ***********************************************************************
00028 // @HEADER
00029 */
00030 
00031 #include "RTOp_ROp_dot_prod.h"
00032 #include "RTOp_obj_null_vtbl.h"
00033 #include "RTOp_obj_value_vtbl.h"
00034 #include "RTOp_reduct_sum_value.h"
00035 
00036 /* Implementation functions */
00037 
00038 static int RTOp_ROp_dot_prod_apply_op(
00039   const struct RTOp_RTOp_vtbl_t* vtbl, const void* obj_data
00040   , const int num_vecs, const struct RTOp_SubVector vecs[]
00041   , const int num_targ_vecs, const struct RTOp_MutableSubVector targ_vecs[]
00042   , RTOp_ReductTarget targ_obj )
00043 {
00044   /* */
00045   /* Declare local variables */
00046   /* */
00047 
00048   /* v0 */
00049   RTOp_index_type        v0_sub_dim;
00050   const RTOp_value_type  *v0_val;
00051   ptrdiff_t              v0_val_s;
00052 
00053   /* v1 */
00054   RTOp_index_type        v1_sub_dim;
00055   const RTOp_value_type  *v1_val;
00056   ptrdiff_t              v1_val_s;
00057 
00058   /* dot_prod */
00059   RTOp_value_type        dot_prod       = 0.0;
00060 
00061   /* Some temporary values */
00062   register RTOp_index_type k;
00063   RTOp_index_type sub_dim;
00064 
00065   /* */
00066   /* Validate the input */
00067   /* */
00068   if( num_vecs != 2 )
00069     return RTOp_ERR_INVALID_NUM_VECS;
00070   if( num_targ_vecs != 0 )
00071     return RTOp_ERR_INVALID_NUM_TARG_VECS;
00072   if( vecs[0].sub_dim != vecs[1].sub_dim )
00073     return RTOp_ERR_INCOMPATIBLE_VECS;
00074 
00075   /* */
00076   /* Get pointers to data */
00077   /* */
00078 
00079   /* v0 */
00080   v0_sub_dim     = vecs[0].sub_dim;
00081   v0_val         = vecs[0].values;
00082   v0_val_s       = vecs[0].values_stride;
00083 
00084   /* v1 */
00085   v1_sub_dim     = vecs[1].sub_dim;
00086   v1_val         = vecs[1].values;
00087   v1_val_s       = vecs[1].values_stride;
00088 
00089   sub_dim = v0_sub_dim;
00090 
00091   /* */
00092   /* Perform the dot product */
00093   /* */
00094 
00095   for( k = 0; k < sub_dim; ++k, v0_val += v0_val_s, v1_val += v1_val_s )
00096     dot_prod += (*v0_val) * (*v1_val);
00097 
00098   /* */
00099   /* Add this to the result */
00100   /* */
00101   *((RTOp_value_type*)targ_obj) += dot_prod;
00102 
00103   return 0; /* success? */
00104 }
00105 
00106 /* Virtual function table pointer */
00107 const struct RTOp_RTOp_vtbl_t RTOp_ROp_dot_prod_vtbl =
00108 {
00109   &RTOp_obj_null_vtbl   /* use null type for instance data */
00110   ,&RTOp_obj_value_vtbl /* use simple scalar type for target object */
00111   ,"ROp_dot_prod"
00112   ,NULL
00113   ,RTOp_ROp_dot_prod_apply_op
00114   ,RTOp_reduct_sum_value
00115   ,RTOp_get_reduct_sum_value_op
00116 };
00117 
00118 /* Class specific functions */
00119 
00120 int RTOp_ROp_dot_prod_construct( struct RTOp_RTOp* op )
00121 {
00122   op->obj_data = NULL;
00123   op->vtbl     = &RTOp_ROp_dot_prod_vtbl;
00124   return 0;
00125 }
00126 
00127 int RTOp_ROp_dot_prod_destroy( struct RTOp_RTOp* op )
00128 {
00129   op->obj_data = NULL;
00130   op->vtbl     = NULL;
00131   return 0;
00132 }
00133 
00134 RTOp_value_type RTOp_ROp_dot_prod_val(RTOp_ReductTarget targ_obj)
00135 {
00136   return *((RTOp_value_type*)targ_obj);
00137 }

Generated on Tue Jul 13 09:28:39 2010 for RTOpPack: Extra C/C++ Code for Vector Reduction/Transformation Operators by  doxygen 1.4.7