11 #ifndef RTOPPACK_SPMD_APPLY_OP_DEF_HPP
12 #define RTOPPACK_SPMD_APPLY_OP_DEF_HPP
30 template<
class Scalar>
36 *out <<
"subDim="<<v.
subDim()<<
"\n";
39 for(
int i = 0; i < v.
subDim(); ++i )
57 template<
class PrimitiveScalar>
65 + num_values *
sizeof(PrimitiveScalar)
71 template<
class Scalar>
81 using Teuchos::arrayView;
87 prim_value_type_size = PVTST::fromCountToDirectBytes(1),
88 index_type_size = ITST::fromCountToDirectBytes(1);
92 num_indexes_off = num_values_off + index_type_size,
93 num_chars_off = num_indexes_off + index_type_size,
94 values_off = num_chars_off + index_type_size,
95 indexes_off = values_off + num_values * prim_value_type_size,
96 chars_off = indexes_off + num_indexes * index_type_size;
97 ITST::serialize(1, &num_values, index_type_size, &reduct_obj_ext[num_values_off]);
98 ITST::serialize(1, &num_indexes, index_type_size, &reduct_obj_ext[num_indexes_off]);
99 ITST::serialize(1, &num_chars, index_type_size, &reduct_obj_ext[num_chars_off]);
102 arrayView(PVTST::convertFromCharPtr(&reduct_obj_ext[values_off]), num_values),
103 arrayView(ITST::convertFromCharPtr(&reduct_obj_ext[indexes_off]), num_indexes),
104 arrayView(CTST::convertFromCharPtr(&reduct_obj_ext[chars_off]), num_chars)
110 template<
class Scalar>
116 const char reduct_obj_ext[],
120 using Teuchos::arrayView;
126 prim_value_type_size = PVTST::fromCountToDirectBytes(1),
127 index_type_size = ITST::fromCountToDirectBytes(1);
131 num_indexes_off = num_values_off + index_type_size,
132 num_chars_off = num_indexes_off + index_type_size,
133 values_off = num_chars_off + index_type_size,
134 indexes_off = values_off + num_values_in * prim_value_type_size,
135 chars_off = indexes_off + num_indexes_in * index_type_size;
137 Ordinal num_values = -1, num_indexes = -1, num_chars = -1;
138 ITST::deserialize(index_type_size, &reduct_obj_ext[num_values_off], 1, &num_values);
139 ITST::deserialize(index_type_size, &reduct_obj_ext[num_indexes_off], 1, &num_indexes);
140 ITST::deserialize(index_type_size, &reduct_obj_ext[num_chars_off], 1, &num_chars);
143 num_values==num_values_in && num_indexes==num_indexes_in
144 && num_chars==num_chars_in ),
147 <<
", num_values="<<num_values<<
", num_values_in="<<num_values_in
148 <<
", num_indexes="<<num_indexes<<
", num_indexes_in="<<num_indexes_in
149 <<
", num_chars="<<num_chars<<
", num_chars_in="<<num_chars_in
153 arrayView(PVTST::convertFromCharPtr(&reduct_obj_ext[values_off]), num_values_in),
154 arrayView(ITST::convertFromCharPtr(&reduct_obj_ext[indexes_off]), num_indexes_in),
155 arrayView(CTST::convertFromCharPtr(&reduct_obj_ext[chars_off]), num_chars_in),
156 Teuchos::ptr(reduct_obj)
170 template<
class Scalar>
174 :op_(op.assert_not_null())
176 using Teuchos::outArg;
178 op_->get_reduct_type_num_entries(
185 template<
class Scalar>
189 return reduct_obj_ext_size_ * count;
193 template<
class Scalar>
210 for(
Ordinal i = 0; i < count; ++i, offset += reduct_obj_ext_size_ ) {
212 *op_,num_values_,num_indexes_,num_chars_
213 ,*reduct_objs[i],&charBuffer[offset]
219 template<
class Scalar>
223 return op_->reduct_obj_create();
226 template<
class Scalar>
229 ,
const char charBuffer[]
243 for(
Ordinal i = 0; i < count; ++i, offset += reduct_obj_ext_size_ ) {
245 *op_,num_values_,num_indexes_,num_chars_
246 ,&charBuffer[offset],reduct_objs[i]
257 template<
class Scalar>
265 template<
class Scalar>
272 for(
Ordinal i = 0; i < count; ++i )
273 op_->reduce_reduct_objs( *inBuffer[i], Teuchos::ptr(inoutBuffer[i]) );
280 template<
class Scalar>
290 using Teuchos::reduceAll;
292 Workspace<Teuchos::RCP<ReductTarget> >
293 i_i_reduct_objs( wss, num_cols );
294 Workspace<ReductTarget*>
295 _i_i_reduct_objs( wss, num_cols );
296 for(
int kc = 0; kc < num_cols; ++kc ) {
298 _i_i_reduct_objs[kc] = &*i_i_reduct_objs[kc];
301 serializer(Teuchos::rcpFromRef(op));
303 reductOp(Teuchos::rcpFromRef(op));
305 *comm, serializer, reductOp,
306 num_cols, &i_reduct_objs[0], &_i_i_reduct_objs[0]);
307 for(
int kc = 0; kc < num_cols; ++kc ) {
313 template<
class Scalar>
319 const int num_targ_vecs,
326 comm,op,1,num_vecs,sub_vecs,num_targ_vecs,targ_sub_vecs
327 ,reduct_obj ? reduct_objs : NULL
333 template<
class Scalar>
338 const int num_multi_vecs,
340 const int num_targ_multi_vecs,
349 Workspace<ConstSubVectorView<Scalar> > c_sub_vecs(wss,num_multi_vecs*num_cols);
351 for( off = 0, j = 0; j < num_cols; ++j ) {
352 for( k = 0; k < num_multi_vecs; ++k ) {
356 arcp(&mv(0,j), 0, mv.
subDim(),
false), 1);
365 Workspace<SubVectorView<Scalar> > c_targ_sub_vecs(wss,num_targ_multi_vecs*num_cols);
366 if(targ_sub_multi_vecs) {
367 for( off = 0, j = 0; j < num_cols; ++j ) {
368 for( k = 0; k < num_targ_multi_vecs; ++k ) {
371 if (mv.
subDim()) { mv_j = arcp(&mv(0,j), 0, mv.
subDim(),
false); }
378 ,num_multi_vecs, num_multi_vecs && sub_multi_vecs ? &c_sub_vecs[0] : NULL
379 ,num_targ_multi_vecs, num_targ_multi_vecs && targ_sub_multi_vecs ? &c_targ_sub_vecs[0] : NULL
385 template<
class Scalar>
392 const int num_targ_vecs,
397 using Teuchos::arrayView;
401 *out <<
"\nEntering RTOpPack::SPMD_apply_op(...) ...\n";
403 <<
"\ncomm = " << (comm?comm->
description():
"NULL")
405 <<
"\nnum_cols = " << num_cols
406 <<
"\nnum_vecs = " << num_vecs
407 <<
"\nnum_targ_vecs = " << num_targ_vecs
409 if( num_vecs && sub_vecs ) {
410 *out <<
"\nInput vectors:\n";
412 for(
int kc = 0; kc < num_cols; ++kc ) {
413 for(
int k = 0; k < num_vecs; ++k ) {
414 *out <<
"\nvecs["<<kc<<
","<<k<<
"] =\n";
415 print(sub_vecs[kc*num_vecs+k],*out);
419 if( num_targ_vecs && sub_targ_vecs ) {
420 *out <<
"\nInput/output vectors *before* transforamtion:\n";
422 for(
int kc = 0; kc < num_cols; ++kc ) {
423 for(
int k = 0; k < num_targ_vecs; ++k ) {
424 *out <<
"\nvecs["<<kc<<
","<<k<<
"] =\n";
425 print(sub_targ_vecs[kc*num_targ_vecs+k],*out);
430 *out <<
"\nInput/output reduction objects *before* reduction:\n";
432 for(
int kc = 0; kc < num_cols; ++kc ) {
434 <<
"\nreduct_objs["<<kc<<
"] =\n"
441 if( reduct_objs == NULL && sub_vecs == NULL && sub_targ_vecs == NULL ) {
446 const int localSubDim =
448 ? ( sub_vecs ? sub_vecs[0].
subDim() : 0 )
449 : ( sub_targ_vecs ? sub_targ_vecs[0].subDim() : 0 )
452 if( comm==NULL || reduct_objs == NULL ) {
453 if( ( sub_vecs || sub_targ_vecs ) && localSubDim ) {
454 for(
int kc = 0; kc < num_cols; ++kc ) {
456 arrayView(sub_vecs+kc*num_vecs, num_vecs),
457 arrayView(sub_targ_vecs+kc*num_targ_vecs, num_targ_vecs),
466 ( ( num_vecs && !sub_vecs) || ( num_targ_vecs && !sub_targ_vecs) ) && !( !sub_vecs && !sub_targ_vecs )
468 ,
"SPMD_apply_op(...): Error, invalid arguments num_vecs = " << num_vecs
469 <<
", sub_vecs = " << sub_vecs <<
", num_targ_vecs = " << num_targ_vecs
470 <<
", sub_targ_vecs = " << sub_targ_vecs
479 Workspace<Teuchos::RCP<ReductTarget> >
480 i_reduct_objs( wss, num_cols );
481 for(
int kc = 0; kc < num_cols; ++kc ) {
483 if( ( sub_vecs || sub_targ_vecs ) && localSubDim ) {
485 arrayView(sub_vecs+kc*num_vecs, num_vecs),
486 arrayView(sub_targ_vecs+kc*num_targ_vecs, num_targ_vecs),
487 i_reduct_objs[kc].ptr()
493 *out <<
"\nIntermediate reduction objects in this process before global reduction:\n";
495 for(
int kc = 0; kc < num_cols; ++kc ) {
497 <<
"\ni_reduct_objs["<<kc<<
"] =\n"
505 Workspace<const ReductTarget*>
506 _i_reduct_objs( wss, num_cols );
507 for(
int kc = 0; kc < num_cols; ++kc ) {
508 _i_reduct_objs[kc] = &*i_reduct_objs[kc];
512 *out <<
"\nPerforming global reduction ...\n";
519 if( num_targ_vecs && sub_targ_vecs ) {
520 *out <<
"\nInput/output vectors *after* transforamtion:\n";
522 for(
int kc = 0; kc < num_cols; ++kc ) {
523 for(
int k = 0; k < num_targ_vecs; ++k ) {
524 *out <<
"\nvecs["<<kc<<
","<<k<<
"] =\n";
525 print(sub_targ_vecs[kc*num_targ_vecs+k],*out);
530 *out <<
"\nInput/output reduction objects *after* reduction:\n";
532 for(
int kc = 0; kc < num_cols; ++kc ) {
534 <<
"\nreduct_objs["<<kc<<
"] =\n"
538 *out <<
"\nLeaving RTOpPack::SPMD_apply_op(...) ...\n";
549 #define RTOPPACK_SPMD_APPLY_OP_INSTANT_SCALAR(SCALAR) \
551 template int serializedSize<SCALAR >( \
557 template void serialize<SCALAR >( \
558 const RTOpT<SCALAR > &op, \
559 Ordinal num_values, \
560 Ordinal num_indexes, \
562 const ReductTarget &reduct_obj, \
563 char reduct_obj_ext[] \
566 template void deserialize<SCALAR >( \
567 const RTOpT<SCALAR > &op, \
569 int num_indexes_in, \
571 const char reduct_obj_ext[], \
572 ReductTarget *reduct_obj \
575 template class ReductTargetSerializer<SCALAR >; \
577 template class ReductTargetReductionOp<SCALAR >; \
579 template void SPMD_all_reduce<SCALAR >( \
580 const Teuchos::Comm<index_type> *comm, \
581 const RTOpT<SCALAR > &op, \
582 const int num_cols, \
583 const ReductTarget*const i_reduct_objs[], \
584 ReductTarget*const reduct_objs[] \
587 template void SPMD_apply_op<SCALAR >( \
588 const Teuchos::Comm<index_type> *comm, \
589 const RTOpT<SCALAR > &op, \
590 const int num_vecs, \
591 const RTOpPack::ConstSubVectorView<SCALAR > sub_vecs[], \
592 const int num_targ_vecs, \
593 const RTOpPack::SubVectorView<SCALAR > targ_sub_vecs[], \
594 ReductTarget *reduct_obj \
597 template void SPMD_apply_op<SCALAR >( \
598 const Teuchos::Comm<index_type> *comm, \
599 const RTOpT<SCALAR > &op, \
600 const int num_cols, \
601 const int num_multi_vecs, \
602 const RTOpPack::ConstSubMultiVectorView<SCALAR > sub_multi_vecs[], \
603 const int num_targ_multi_vecs, \
604 const RTOpPack::SubMultiVectorView<SCALAR > targ_sub_multi_vecs[], \
605 RTOpPack::ReductTarget*const reduct_objs[] \
608 template void SPMD_apply_op<SCALAR >( \
609 const Teuchos::Comm<index_type> *comm, \
610 const RTOpT<SCALAR > &op, \
611 const int num_cols, \
612 const int num_vecs, \
613 const ConstSubVectorView<SCALAR > sub_vecs[], \
614 const int num_targ_vecs, \
615 const SubVectorView<SCALAR > sub_targ_vecs[], \
616 ReductTarget*const reduct_objs[] \
620 #endif // RTOPPACK_SPMD_APPLY_OP_DEF_HPP
std::string op_name() const
Return the name (as a null-terminated C-style string) of the operator.
int serializedSize(int num_values, int num_indexes, int num_chars)
Return the size in bytes of an external representation of a ReductTarget object.
void SPMD_all_reduce(const Teuchos::Comm< index_type > *comm, const RTOpT< Scalar > &op, const int num_cols, const ReductTarget *const i_reduct_objs[], ReductTarget *const reduct_objs[])
Reduce a set of reduction objects.
Teuchos::RCP< ReductTarget > createObj() const
Class for a changeable sub-vector.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
void serialize(const index_type count, const ReductTarget *const reduct_objs[], const index_type bytes, char charBuffer[]) const
basic_OSTab< CharT, Traits > & incrTab(const int tabs=1)
Teuchos_Ordinal index_type
ReductionOp subclass for ReductTarget objects.
void serialize(const RTOpT< Scalar > &op, Ordinal num_values, Ordinal num_indexes, Ordinal num_chars, const ReductTarget &reduct_obj, char reduct_obj_ext[])
Serialize a ReductTarget object.
Class for a non-changeable sub-vector.
void deserialize(const RTOpT< Scalar > &op, int num_values, int num_indexes, int num_chars, const char reduct_obj_ext[], ReductTarget *reduct_obj)
Deserialize a ReductTarget object.
Serializer subclass for ReductTarget objects.
Ordinal globalOffset() const
Templated interface to vector reduction/transformation operators {abstract}.
index_type getBufferSize(const index_type count) const
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Class for a changeable sub-vector.
void extract_reduct_obj_state(const ReductTarget &reduct_obj, const ArrayView< primitive_value_type > &value_data, const ArrayView< index_type > &index_data, const ArrayView< char_type > &char_data) const
Extract the state of an already created reduction object.
void deserialize(const index_type bytes, const char charBuffer[], const index_type count, ReductTarget *const reduct_objs[]) const
void load_reduct_obj_state(const ArrayView< const primitive_value_type > &value_data, const ArrayView< const index_type > &index_data, const ArrayView< const char_type > &char_data, const Ptr< ReductTarget > &reduct_obj) const
Load the state of an already created reduction object given arrays of primitive objects.
virtual std::string description() const
PrimitiveTypeTraits< Scalar, Scalar >::primitiveType primitive_value_type
Abstract base class for all reduction objects.
void print(const ConstSubVectorView< Scalar > &v, Teuchos::FancyOStream &out_arg)
Ordinal globalOffset() const
void reduce_reduct_objs(const ReductTarget &in_reduct_obj, const Ptr< ReductTarget > &inout_reduct_obj) const
Reduce intermediate reduction target objects.
Teuchos::RCP< ReductTarget > reduct_obj_create() const
Creates a new reduction target object initialized and ready to be used in a reduction.
bool nonnull(const boost::shared_ptr< T > &p)
Class for a non-changeable sub-multi-vector (submatrix).
void initialize(Ordinal globalOffset_in, Ordinal subDim_in, Ordinal colOffset_in, Ordinal numSubCols_in, const ArrayRCP< const Scalar > &values_in, Ordinal leadingDim_in)
Teuchos::RCP< const RTOpT< Scalar > > op_
ReductTargetReductionOp()
void apply_op(const ArrayView< const ConstSubVectorView< Scalar > > &sub_vecs, const ArrayView< const SubVectorView< Scalar > > &targ_sub_vecs, const Ptr< ReductTarget > &reduct_obj) const
Apply the reduction/transformation operator to a set of sub-vectors.
void SPMD_apply_op(const Teuchos::Comm< index_type > *comm, const RTOpT< Scalar > &op, const int num_vecs, const ConstSubVectorView< Scalar > sub_vecs[], const int num_targ_vecs, const SubVectorView< Scalar > targ_sub_vecs[], ReductTarget *reduct_obj)
Apply an RTOp in SMPD mode to a set of vectors with contiguous storage per process.
#define TEUCHOS_TEST_FOR_EXCEPT(throw_exception_test)
TEUCHOSCORE_LIB_DLL_EXPORT Teuchos::RCP< WorkspaceStore > get_default_workspace_store()
void reduce(const Ordinal count, const ReductTarget *const inBuffer[], ReductTarget *const inoutBuffer[]) const
RCP< FancyOStream > & spmdApplyOpDumpOut()