|
Boost-Commit : |
Subject: [Boost-commit] svn:boost r82780 - sandbox/variadic_templates/sandbox/array_dyn
From: cppljevans_at_[hidden]
Date: 2013-02-07 19:11:53
Author: cppljevans
Date: 2013-02-07 19:11:52 EST (Thu, 07 Feb 2013)
New Revision: 82780
URL: http://svn.boost.org/trac/boost/changeset/82780
Log:
Enabled reduce_axis to handle dir_fwd.
Added
templated reduce_test
reduce_add_double
to reduce_axis.cpp
to better illustrate what's being done
Changes to other files--can't remember why.
Text files modified:
sandbox/variadic_templates/sandbox/array_dyn/array_dyn.hpp | 39 ++---
sandbox/variadic_templates/sandbox/array_dyn/box_domain.hpp | 2
sandbox/variadic_templates/sandbox/array_dyn/reduce_axis.cpp | 236 ++++++++++++---------------------------
sandbox/variadic_templates/sandbox/array_dyn/reduce_axis.hpp | 28 +++-
4 files changed, 113 insertions(+), 192 deletions(-)
Modified: sandbox/variadic_templates/sandbox/array_dyn/array_dyn.hpp
==============================================================================
--- sandbox/variadic_templates/sandbox/array_dyn/array_dyn.hpp (original)
+++ sandbox/variadic_templates/sandbox/array_dyn/array_dyn.hpp 2013-02-07 19:11:52 EST (Thu, 07 Feb 2013)
@@ -196,30 +196,24 @@
index_t const axis_size=my_array.size(axis_now);
bool const is_leaf=axis_now==my_axis_end;
index_t const axis_stride=my_array.stride(axis_now);
- if(0 == axis_size)
+ a_viz.visit_pre(is_leaf, axis_now);
+ for(index_t i=0; i<axis_size; ++i)
{
- a_viz.visit_empty(is_leaf, axis_now);
- }
- else
- {//0 < axis_size
- for(index_t i=0; i<axis_size; ++i)
+ a_viz.visit_pre(is_leaf, axis_now, i);
+ if(is_leaf)
{
- a_viz.visit_pre(is_leaf, axis_now, i);
- if(is_leaf)
- {
- a_viz.visit_node(my_array.my_data[offset]);
- }
- else
- {
- this->accept_off_ax
- ( a_viz
- , offset
- , axis_now+my_axis_dir
- );
- }
- offset+=axis_stride;
+ a_viz.visit_node(my_array.my_data[offset]);
}
- }//0 < axis_size
+ else
+ {
+ this->accept_off_ax
+ ( a_viz
+ , offset
+ , axis_now+my_axis_dir
+ );
+ }
+ offset+=axis_stride;
+ }
a_viz.visit_post(is_leaf, axis_now);
}
};
@@ -258,7 +252,7 @@
, my_indent(a_indent)
{}
- void visit_empty( bool is_leaf, index_t axis)
+ void visit_pre( bool is_leaf, index_t axis)
{
my_sout<<std::left<<std::setw(my_indent)<<"{";
}
@@ -267,7 +261,6 @@
{
if(index==0)
{
- my_sout<<std::left<<std::setw(my_indent)<<"{";
}
else
{
Modified: sandbox/variadic_templates/sandbox/array_dyn/box_domain.hpp
==============================================================================
--- sandbox/variadic_templates/sandbox/array_dyn/box_domain.hpp (original)
+++ sandbox/variadic_templates/sandbox/array_dyn/box_domain.hpp 2013-02-07 19:11:52 EST (Thu, 07 Feb 2013)
@@ -393,7 +393,7 @@
for( unsigned i=0; i<n && result; ++i,++index_iter)
{
index_t index_i=*index_iter;
- result = 0<=index_i && index_i<size(i);
+ result = index_i<size(i);
}
return result;
}
Modified: sandbox/variadic_templates/sandbox/array_dyn/reduce_axis.cpp
==============================================================================
--- sandbox/variadic_templates/sandbox/array_dyn/reduce_axis.cpp (original)
+++ sandbox/variadic_templates/sandbox/array_dyn/reduce_axis.cpp 2013-02-07 19:11:52 EST (Thu, 07 Feb 2013)
@@ -3,7 +3,7 @@
//
#include <boost/iostreams/utility/indent_scoped_ostreambuf.hpp>
#include <vector>
-#include <algorithm>//for_each
+#include "reducer_variance.hpp"
#include "reduce_axis.hpp"
template<typename Elem>
@@ -36,83 +36,69 @@
};
template
- < typename Value
+ < typename Reducer=reducer_variance
>
-struct reduce_add_one
-/**@brief
- * add values.
- */
+void reduce_test()
{
- Value&
- my_value
- ;
- reduce_add_one
- ( Value&a_value
- )
- : my_value
- ( a_value
- )
- {}
- Value
- operator()(Value operand)
- {
- my_value+=operand;
- return my_value;
- }
-};
- template
- < typename Value
- >
-struct reduce_var_one
-/**@brief
- * sum square of deviation from a mean.
- */
-{
- Value&
- my_value
- //result value
- ;
- Value const
- my_mean
- //the mean
- ;
- reduce_var_one
- ( Value&a_value//sum of square of devitation from mean
- , Value a_mean=0
- )
- : my_value
- ( a_value
- )
- , my_mean
- ( a_mean
- )
- {}
- Value
- operator()(Value operand)
+ std::cout<<"*************************\n";
+ for( unsigned i_dir=dir_fwd; i_dir<dir_rev+1; ++i_dir)
{
- Value
- deviation=operand-my_mean;
- my_value+=deviation*deviation;
- return my_value;
- }
-};
-struct reducer_variance
+ dirs const dir_op=dirs(i_dir);
+ std::cout<<"dir_op="<<dir_op<<"\n";
+ indent_scope is;
+ typedef double val_t;
+ typedef array_dyn<val_t> array_t;
+ typedef array_t::index_t index_t;
+ std::vector<index_t> sample_shape({2,3,4});
+ std::cout<<"sample_shape="<<sample_shape<<"\n";
+ array_t sample_data(dir_op,sample_shape);
+ unsigned const sample_size=sample_data.my_data.size();
+ std::cout<<"my_data.size()="<<sample_size<<"\n";
+ unsigned const value0=1000;
+ for( unsigned i=0; i<sample_size; ++i)
+ {
+ sample_data.my_data[i]=value0+i;
+ }
+ unsigned const rank=sample_data.rank();
+ std::cout<<"rank="<<rank<<"\n";
+ std::cout<<"size()="<<sample_data.size()<<"\n";
+ for(unsigned i=0;i<rank;++i)
+ {
+ std::cout<<"stride("<<i<<")="<<sample_data.stride(i)<<"\n";
+ std::cout<<" size("<<i<<")="<<sample_data.size(i)<<"\n";
+ }
+ #if 1
+ std::cout<<"sample_data=\n";
+ std::cout<<sample_data<<".\n";
+ #endif
+ std::cout<<":Reducer for each axis:\n";
+ {
+ indent_scope is;
+ for(unsigned oper_axis=0; oper_axis<rank; ++oper_axis)
+ {
+ std::cout<<":oper_axis="<<oper_axis<<"\n";
+ {
+ indent_scope is;
+ array_t
+ arr_reduce
+ ( reduce_axis
+ < Reducer const&
+ >
+ ( Reducer()
+ , sample_data
+ , oper_axis
+ )
+ );
+ std::cout<<":arr_reduce=\n";
+ std::cout<<arr_reduce<<".\n";
+ }
+ }//exit for(mean_axis...)
+ }
+ }//end for(i_dir...)
+}
+
+struct reduce_add_double
{
- unsigned const
- my_bessel_correction
- /**@brief
- * == 0 or 1, indicating whether to use
- * Bessel's correction:
- * http://en.wikipedia.org/wiki/Bessel%27s_correction
- * if == 0, don't correct.
- * if == 1, then do correct.
- */
- ;
- reducer_variance
- ( bool a_bessel_correction=false
- )
- : my_bessel_correction(a_bessel_correction?1:0)
- {}
template
< typename Iter
>
@@ -125,97 +111,23 @@
* return value == variance of data in sample, *beg ... *(end-1).
*/
{
- auto
- sample_size=beg.distance(end);
- assert(sample_size > my_bessel_correction);
- typedef
- typename Iter::value_type
- val_t
- ;
- val_t
- mean_v=0
- ;
- reduce_add_one<val_t>
- reduce_add_v(mean_v)
- ;
- std::for_each
- ( beg
- , end
- , reduce_add_v
- );
- mean_v=mean_v/sample_size
- //mean_v = mean of data in *beg ... *(end-1)
- ;
- val_t
- var_v;
- reduce_var_one<val_t>
- reduce_var_v(var_v,mean_v)
- ;
- std::for_each
- ( beg
- , end
- , reduce_var_v
- );
- var_v=var_v/(sample_size-my_bessel_correction)
- //var_v = variance of data in *beg ... *(end-1)
- ;
- return var_v;
- }
+ double sum=0.0;
+ std::cout<<sum;
+ for(; beg!=end; ++beg)
+ {
+ double val=*beg;
+ std::cout<<"+"<<val;
+ sum+=val;
+ }
+ std::cout<<"="<<sum<<"\n";
+ return sum;
+ };
+
};
-
+
int main(void)
{
::boost::iostreams::indent_scoped_ostreambuf<> indent_cout(std::cout);
- std::cout<<"*************************\n";
- dirs const dir_op=dir_rev;
- std::cout<<"dir_op="<<dir_op<<"\n";
- typedef double val_t;
- typedef array_dyn<val_t> array_t;
- typedef array_t::index_t index_t;
- std::vector<index_t> sample_shape({2,3,4});
- std::cout<<"sample_shape="<<sample_shape<<"\n";
- array_t sample_data(dir_op,sample_shape);
- unsigned const sample_size=sample_data.my_data.size();
- std::cout<<"my_data.size()="<<sample_size<<"\n";
- unsigned const value0=1000;
- for( unsigned i=0; i<sample_size; ++i)
- {
- sample_data.my_data[i]=value0+i;
- }
- unsigned const rank=sample_data.rank();
- std::cout<<"rank="<<rank<<"\n";
- std::cout<<"size()="<<sample_data.size()<<"\n";
- for(unsigned i=0;i<rank;++i)
- {
- std::cout<<"stride("<<i<<")="<<sample_data.stride(i)<<"\n";
- std::cout<<" size("<<i<<")="<<sample_data.size(i)<<"\n";
- }
- #if 1
- std::cout<<"sample_data=\n";
- std::cout<<sample_data<<".\n";
- #endif
- std::cout<<":reducer_variance for each axis:\n";
- {
- indent_scope is;
- for(unsigned oper_axis=0; oper_axis<rank; ++oper_axis)
- {
- std::cout<<":oper_axis="<<oper_axis<<"\n";
- {
- indent_scope is;
- array_t
- arr_reduce
- ( reduce_axis
- < reducer_variance const&
- >
- ( reducer_variance()
- , sample_data
- , oper_axis
- )
- );
- std::cout<<":arr_reduce=\n";
- std::cout<<arr_reduce<<".\n";
- }
- }//exit for(mean_axis...)
- }
+ reduce_test<reduce_add_double>();
return 0;
}
Modified: sandbox/variadic_templates/sandbox/array_dyn/reduce_axis.hpp
==============================================================================
--- sandbox/variadic_templates/sandbox/array_dyn/reduce_axis.hpp (original)
+++ sandbox/variadic_templates/sandbox/array_dyn/reduce_axis.hpp 2013-02-07 19:11:52 EST (Thu, 07 Feb 2013)
@@ -65,7 +65,6 @@
}
};
-#include <cassert>
#include "array_dyn.hpp"
template
@@ -92,23 +91,40 @@
* http://aplwiki.com/LearnApl/AplOperators#Reduce_and_scan
*/
{
- assert(a_source.my_dir == dir_rev);
unsigned const rank=a_source.rank();
- enum{outer,oper,inner};
+ enum
+ { outer //outer loop axis tag (larger strides).
+ , oper //oper axis tag.
+ , inner //inner loop axis tag (smaller strides).
+ };
+ //[following assumes a_source.my_dir==dir_rev.
+ unsigned max_stride_axis=0;//axis with maximum stride.
+ unsigned min_stride_axis=rank-1;//axis with minimum stride.
+ int greater_axis=-1;//axis increment to axis with greater stride.
+ //]preceding assumed a_source.my_dir==dir_rev.
+ if(a_source.my_dir != dir_rev)
+ {
+ //Adjust if above assumption about a_source.my_dir wrong.
+ std::swap(min_stride_axis,max_stride_axis);
+ greater_axis=1;
+ }
std::vector<loop> loops(inner+1);
- if(oper_axis==0)
+ if(oper_axis==max_stride_axis)
{
+ //Since oper_axis already at maximum stride,
+ //created a pseudo axis of size 1 and stride 1
+ //so that outer loop just executes once.
loops[outer].stride=1;
loops[outer].limit =1;
}
else
{
- loops[outer].stride=a_source.stride(oper_axis-1);
+ loops[outer].stride=a_source.stride(oper_axis+greater_axis);
loops[outer].limit =a_source.size ();
}
loops[oper ].stride=a_source.stride(oper_axis);
loops[oper ].limit =a_source.size (oper_axis)*loops[oper].stride;
- loops[inner].stride=a_source.stride(rank-1 );
+ loops[inner].stride=a_source.stride(min_stride_axis);
loops[inner].limit =a_source.stride(oper_axis);
auto
shape_target=a_source.shape()
Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk