MPQC  3.0.0-alpha
messaget.h
1 //
2 // messaget.h
3 //
4 // Copyright (C) 1996 Limit Point Systems, Inc.
5 //
6 // Author: Curtis Janssen <cljanss@limitpt.com>
7 // Maintainer: LPS
8 //
9 // This file is part of the SC Toolkit.
10 //
11 // The SC Toolkit is free software; you can redistribute it and/or modify
12 // it under the terms of the GNU Library General Public License as published by
13 // the Free Software Foundation; either version 2, or (at your option)
14 // any later version.
15 //
16 // The SC Toolkit is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 // GNU Library General Public License for more details.
20 //
21 // You should have received a copy of the GNU Library General Public License
22 // along with the SC Toolkit; see the file COPYING.LIB. If not, write to
23 // the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
24 //
25 // The U.S. Government is granted a limited license as per AL 91-7.
26 //
27 
28 #ifndef _util_group_messaget_h
29 #define _util_group_messaget_h
30 
31 #include <util/group/message.h>
32 
33 namespace sc {
34 
35 template <class T>
36 void
37 GrpSumReduce<T>::reduce(T*target, T*data, int nelement)
38 {
39  for (int i=0; i<nelement; i++) {
40  target[i] += data[i];
41  }
42 }
43 
44 template <class T>
45 void
46 GrpMinReduce<T>::reduce(T*target, T*data, int nelement)
47 {
48  for (int i=0; i<nelement; i++) {
49  if (target[i] > data[i]) target[i] = data[i];
50  }
51 }
52 
53 template <class T>
54 void
55 GrpMaxReduce<T>::reduce(T*target, T*data, int nelement)
56 {
57  for (int i=0; i<nelement; i++) {
58  if (target[i] < data[i]) target[i] = data[i];
59  }
60 }
61 
62 template <class T, class BinaryPredicate>
63 void
64 GrpCompareReduce<T, BinaryPredicate>::reduce(T*target, T*data, int nelement)
65 {
66  for (int i=0; i<nelement; i++) {
67  if (Op(target[i], data[i])) target[i] = data[i];
68  }
69 }
70 
71 template <class T>
72 void
73 GrpArithmeticAndReduce<T>::reduce(T*target, T*data, int nelement)
74 {
75  for (int i=0; i<nelement; i++) {
76  target[i] = target[i] & data[i];
77  }
78 }
79 
80 template <class T>
81 void
82 GrpArithmeticOrReduce<T>::reduce(T*target, T*data, int nelement)
83 {
84  for (int i=0; i<nelement; i++) {
85  target[i] = target[i] | data[i];
86  }
87 }
88 
89 template <class T>
90 void
91 GrpArithmeticXOrReduce<T>::reduce(T*target, T*data, int nelement)
92 {
93  for (int i=0; i<nelement; i++) {
94  target[i] = target[i] ^ data[i];
95  }
96 }
97 
98 template <class T>
99 void
100 GrpProductReduce<T>::reduce(T*target, T*data, int nelement)
101 {
102  for (int i=0; i<nelement; i++) {
103  target[i] *= data[i];
104  }
105 }
106 
107 template <class T>
108 void
109 GrpFunctionReduce<T>::reduce(T*target, T*data, int nelement)
110 {
111  (*func_)(target,data,nelement);
112 }
113 
114 template <typename T>
115 void
116 MessageGrp::reduce(T* data, int n, GrpReduce<T>& red,
117  T* scratch, int target)
118 {
119  int tgop_max = gop_max_/sizeof(T);
120  if (tgop_max == 0) tgop_max = gop_max_?1:n;
121 
122  int passed_scratch;
123  if (!scratch) {
124  scratch = new T[n>tgop_max?tgop_max:n];
125  passed_scratch = 0;
126  }
127  else passed_scratch = 1;
128 
129  Ref<GlobalMsgIter> i(topology_->global_msg_iter(this,
130  (target== -1?0:target)));
131  for (i->backwards(); !i->done(); i->next()) {
132  for (int idat=0; idat<n; idat+=tgop_max) {
133  int ndat = (idat+tgop_max>n)?(n-idat):tgop_max;
134  if (i->send()) {
135  raw_send(i->sendto(), &data[idat], ndat*sizeof(T));
136  }
137  if (i->recv()) {
138  raw_recv(i->recvfrom(), scratch, ndat*sizeof(T));
139  red.reduce(&data[idat], scratch, ndat);
140  }
141  }
142  if (n > tgop_max) sync();
143  }
144 
145  if (target == -1) {
146  raw_bcast(data, n*sizeof(T), 0);
147  }
148 
149  if (!passed_scratch) delete[] scratch;
150 }
151 
152 }
153 
154 #endif
155 
157 
158 // Local Variables:
159 // mode: c++
160 // c-file-style: "CLJ"
161 // End:
sc::Ref
A template class that maintains references counts.
Definition: ref.h:361
sc::GrpReduce
Definition: message.h:43
sc::MessageGrp::n
int n()
Returns the number of processors.
Definition: message.h:192
sc::MessageGrp::sync
virtual void sync()
Synchronize all of the processors.
sc
Contains all MPQC code up to version 3.
Definition: mpqcin.h:14
sc::MessageGrp::reduce
void reduce(T *, int n, GrpReduce< T > &, T *scratch=0, int target=-1)
T must be a POD (plain old data) type so that it can be copied bytewise.
Definition: messaget.h:116

Generated at Sun Jan 26 2020 23:24:01 for MPQC 3.0.0-alpha using the documentation package Doxygen 1.8.16.