MPQC 3.0.0-alpha
Loading...
Searching...
No Matches
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
33namespace sc {
34
35template <class T>
36void
37GrpSumReduce<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
44template <class T>
45void
46GrpMinReduce<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
53template <class T>
54void
55GrpMaxReduce<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
62template <class T, class BinaryPredicate>
63void
64GrpCompareReduce<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
71template <class T>
72void
73GrpArithmeticAndReduce<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
80template <class T>
81void
82GrpArithmeticOrReduce<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
89template <class T>
90void
91GrpArithmeticXOrReduce<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
98template <class T>
99void
100GrpProductReduce<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
107template <class T>
108void
109GrpFunctionReduce<T>::reduce(T*target, T*data, int nelement)
110{
111 (*func_)(target,data,nelement);
112}
113
114template <typename T>
115void
116MessageGrp::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:
Definition message.h:43
virtual void sync()
Synchronize all of the processors.
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
int n()
Returns the number of processors.
Definition message.h:192
A template class that maintains references counts.
Definition ref.h:361
Contains all MPQC code up to version 3.
Definition mpqcin.h:14

Generated at Wed Sep 25 2024 02:45:30 for MPQC 3.0.0-alpha using the documentation package Doxygen 1.12.0.