RTOpPack: Extra C/C++ Code for Vector Reduction/Transformation Operators  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
RTOp_apply_op_mpi.c
1 /*
2 // @HEADER
3 // ***********************************************************************
4 //
5 // Moocho: Multi-functional Object-Oriented arCHitecture for Optimization
6 // Copyright (2003) Sandia Corporation
7 //
8 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
9 // license for use of this work by or on behalf of the U.S. Government.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 //
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of the Corporation nor the names of the
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact Roscoe A. Bartlett (rabartl@sandia.gov)
39 //
40 // ***********************************************************************
41 // @HEADER
42 */
43 
44 #include "RTOp_apply_op_mpi.h"
45 #include "RTOp_parallel_helpers.h"
46 #include "RTOpToMPI.h"
47 
48 #include <stdlib.h>
49 
50 int RTOp_apply_op_mpi(
51  MPI_Comm comm
52  ,RTOp_index_type global_dim_in, RTOp_index_type local_sub_dim_in, RTOp_index_type local_offset_in
53  ,const int num_cols
54  ,const int num_vecs, const RTOp_value_type* l_vec_ptrs[], const ptrdiff_t l_vec_strides[], const ptrdiff_t l_vec_leading_dim[]
55  ,const int num_targ_vecs, RTOp_value_type* l_targ_vec_ptrs[], const ptrdiff_t l_targ_vec_strides[], const ptrdiff_t l_targ_vec_leading_dim[]
56  ,const RTOp_index_type first_ele_in, const RTOp_index_type sub_dim_in, const RTOp_index_type global_offset_in
57  ,const struct RTOp_RTOp* op
58  ,RTOp_ReductTarget reduct_objs[]
59  )
60 {
61  int err = 0;
62  struct RTOp_SubVector *local_vecs = NULL;
63  struct RTOp_MutableSubVector *local_targ_vecs = NULL;
64  RTOp_index_type overlap_first_local_ele = 0;
65  RTOp_index_type overalap_local_sub_dim = 0;
66  RTOp_index_type overlap_global_offset = 0;
67  int k;
68  int kc;
69  /* Validate the input */
70 #ifdef RTOp_DEBUG
71  assert( num_vecs || num_targ_vecs );
72  if(num_vecs)
73  assert( l_vec_ptrs != NULL );
74  if(num_targ_vecs)
75  assert( l_targ_vec_ptrs != NULL );
76  assert( 0 <= sub_dim_in && sub_dim_in <= global_dim_in );
77 #endif
78  /* Pre-initialize the local sub-vectors */
79  if(num_vecs) {
80  local_vecs = malloc( sizeof(struct RTOp_SubVector) * num_vecs * num_cols );
81  for( kc = 0; kc < num_cols; ++kc ) {
82  for( k = 0; k < num_vecs; ++k )
83  RTOp_sub_vector_null(&local_vecs[kc*num_cols+k]);
84  }
85  }
86  if(num_targ_vecs) {
87  local_targ_vecs = malloc( sizeof(struct RTOp_MutableSubVector) * num_targ_vecs );
88  for( kc = 0; kc < num_cols; ++kc ) {
89  for( k = 0; k < num_targ_vecs; ++k )
90  RTOp_mutable_sub_vector_null(&local_targ_vecs[kc*num_cols+k]);
91  }
92  }
93  /* Get the overlap in the current process with the input logical sub-vector */
94  /* from (first_ele_in,sub_dim_in,global_offset_in) */
95  RTOp_parallel_calc_overlap(
96  global_dim_in, local_sub_dim_in, local_offset_in, first_ele_in, sub_dim_in, global_offset_in
97  ,&overlap_first_local_ele, &overalap_local_sub_dim, &overlap_global_offset
98  );
99  if( overlap_first_local_ele != 0 ) {
100  /* Sub-vector structs for the local elements that are to participate in the */
101  /* reduction/transforamtion operation. */
102  for( kc = 0; kc < num_cols; ++kc ) {
103  for(k = 0; k < num_vecs; ++k) {
105  overlap_global_offset /* global_offset */
106  ,overalap_local_sub_dim /* sub_dim */
107  ,l_vec_ptrs[k]+(overlap_first_local_ele-1)*l_vec_strides[k]
108  + ( num_cols > 1 ? kc*l_vec_leading_dim[k] : 0 ) /* values */
109  ,l_vec_strides[k] /* values_stride */
110  ,&local_vecs[kc*num_cols+k]
111  );
112  }
113  for(k = 0; k < num_targ_vecs; ++k) {
115  overlap_global_offset /* global_offset */
116  ,overalap_local_sub_dim /* sub_dim */
117  ,l_targ_vec_ptrs[k]+(overlap_first_local_ele-1)*l_targ_vec_strides[k]
118  + ( num_cols > 1 ? kc*l_targ_vec_leading_dim[k] : 0 ) /* values */
119  ,l_targ_vec_strides[k] /* values_stride */
120  ,&local_targ_vecs[kc*num_cols+k]
121  );
122  }
123  }
124  }
125  /* */
126  /* Apply the reduction operation over the sub-vectors in */
127  /* this process then collect the reductions over */
128  /* all the processes and return the result */
129  /* to all the processes (including this one of course). */
130  /* If all of the sub-svectors are empty then this will */
131  /* just call the reduction operation with NULL sub-vectors */
132  /* */
133  err = RTOp_MPI_apply_op(
134  comm, op, -1 /* MPI_Allreduce(...) */
135  ,num_cols
136  ,num_vecs, num_vecs && overlap_first_local_ele ? &local_vecs[0] : NULL
137  ,num_targ_vecs, num_targ_vecs && overlap_first_local_ele ? &local_targ_vecs[0] : NULL
138  ,reduct_objs
139  );
140 
141  if(local_vecs) free(local_vecs);
142  if(local_targ_vecs) free(local_targ_vecs);
143 
144  /* Deallocate memory */
145 
146  return err;
147 }
void RTOp_mutable_sub_vector(RTOp_index_type global_offset, RTOp_index_type sub_dim, RTOp_value_type values[], ptrdiff_t values_stride, struct RTOp_MutableSubVector *sub_vec)
Definition: RTOp.c:77
void RTOp_sub_vector(RTOp_index_type global_offset, RTOp_index_type sub_dim, const RTOp_value_type values[], ptrdiff_t values_stride, struct RTOp_SubVector *sub_vec)
Definition: RTOp.c:52
void RTOp_mutable_sub_vector_null(struct RTOp_MutableSubVector *sub_vec)
Definition: RTOp.c:95
int RTOp_MPI_apply_op(MPI_Comm comm, const struct RTOp_RTOp *op, int root_rank, const int num_cols, const int num_vecs, const struct RTOp_SubVector sub_vecs[], const int num_targ_vecs, const struct RTOp_MutableSubVector sub_targ_vecs[], RTOp_ReductTarget reduct_objs[])
Definition: RTOpToMPI.c:144
void RTOp_sub_vector_null(struct RTOp_SubVector *sub_vec)
Definition: RTOp.c:69