[0b990d] | 1 | //
|
---|
| 2 | // util.cc
|
---|
| 3 | //
|
---|
| 4 | // Copyright (C) 1997 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 | #ifdef __GNUC__
|
---|
| 29 | #pragma implementation
|
---|
| 30 | #endif
|
---|
| 31 |
|
---|
| 32 | #include <chemistry/qc/mbpt/util.h>
|
---|
| 33 |
|
---|
| 34 | using namespace sc;
|
---|
| 35 |
|
---|
| 36 | BiggestContribs::BiggestContribs(int nindex, int maxcontrib)
|
---|
| 37 | {
|
---|
| 38 | maxcontrib_ = maxcontrib;
|
---|
| 39 | nindex_ = nindex;
|
---|
| 40 |
|
---|
| 41 | vals_ = new double[maxcontrib_];
|
---|
| 42 |
|
---|
| 43 | indices_ = new int*[maxcontrib_];
|
---|
| 44 | for (int i=0; i<maxcontrib_; i++) indices_[i] = new int[nindex_];
|
---|
| 45 |
|
---|
| 46 | ncontrib_ = 0;
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | BiggestContribs::~BiggestContribs()
|
---|
| 50 | {
|
---|
| 51 | delete[] vals_;
|
---|
| 52 | for (int i=0; i<maxcontrib_; i++) delete[] indices_[i];
|
---|
| 53 | delete[] indices_;
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | void
|
---|
| 57 | BiggestContribs::insert(double val, int i0, int i1)
|
---|
| 58 | {
|
---|
| 59 | int i[2];
|
---|
| 60 | i[0] = i0;
|
---|
| 61 | i[1] = i1;
|
---|
| 62 | insert(val,i);
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | void
|
---|
| 66 | BiggestContribs::insert(double val, int i0, int i1, int i2, int i3)
|
---|
| 67 | {
|
---|
| 68 | int i[4];
|
---|
| 69 | i[0] = i0;
|
---|
| 70 | i[1] = i1;
|
---|
| 71 | i[2] = i2;
|
---|
| 72 | i[3] = i3;
|
---|
| 73 | insert(val,i);
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | void
|
---|
| 77 | BiggestContribs::insert(double val, int i0, int i1, int i2, int i3, int i4)
|
---|
| 78 | {
|
---|
| 79 | int i[5];
|
---|
| 80 | i[0] = i0;
|
---|
| 81 | i[1] = i1;
|
---|
| 82 | i[2] = i2;
|
---|
| 83 | i[3] = i3;
|
---|
| 84 | i[4] = i4;
|
---|
| 85 | insert(val,i);
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | void
|
---|
| 89 | BiggestContribs::insert(double val, const int *ii)
|
---|
| 90 | {
|
---|
| 91 | if (maxcontrib_ == 0) return;
|
---|
| 92 | if (ncontrib_ == 0) {
|
---|
| 93 | vals_[0] = val;
|
---|
| 94 | memcpy(indices_[0], ii, nindex_*sizeof(int));
|
---|
| 95 | ncontrib_ = 1;
|
---|
| 96 | return;
|
---|
| 97 | }
|
---|
| 98 | int i;
|
---|
| 99 | double fabsval = (val>=0.0?val:-val);
|
---|
| 100 | int contrib = 0;
|
---|
| 101 | for (i=ncontrib_-1; i>=0; i--) {
|
---|
| 102 | double fabstmp = (vals_[i]>=0.0?vals_[i]:-vals_[i]);
|
---|
| 103 | if (fabsval > fabstmp) {
|
---|
| 104 | contrib = 1;
|
---|
| 105 | if (i < maxcontrib_-1) {
|
---|
| 106 | vals_[i+1] = vals_[i];
|
---|
| 107 | memcpy(indices_[i+1], indices_[i], nindex_*sizeof(int));
|
---|
| 108 | }
|
---|
| 109 | vals_[i] = val;
|
---|
| 110 | memcpy(indices_[i], ii, nindex_*sizeof(int));
|
---|
| 111 | }
|
---|
| 112 | else {
|
---|
| 113 | break;
|
---|
| 114 | }
|
---|
| 115 | }
|
---|
| 116 | if (ncontrib_ < maxcontrib_) {
|
---|
| 117 | if (!contrib) {
|
---|
| 118 | vals_[ncontrib_] = val;
|
---|
| 119 | memcpy(indices_[ncontrib_], ii, nindex_*sizeof(int));
|
---|
| 120 | }
|
---|
| 121 | ncontrib_++;
|
---|
| 122 | }
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 | void
|
---|
| 126 | BiggestContribs::combine(const Ref<MessageGrp> &grp)
|
---|
| 127 | {
|
---|
| 128 | int i;
|
---|
| 129 | int n = grp->n();
|
---|
| 130 | int me = grp->me();
|
---|
| 131 | // allocate array to store the number of contribs from each node
|
---|
| 132 | int *ncontrib_each = new int[n];
|
---|
| 133 | memset(ncontrib_each, 0, n*sizeof(int));
|
---|
| 134 | ncontrib_each[me] = ncontrib_;
|
---|
| 135 | grp->sum(ncontrib_each,n);
|
---|
| 136 |
|
---|
| 137 | // compute the total number of contribs and my offset for contrib data
|
---|
| 138 | int ncontrib_all = 0;
|
---|
| 139 | int contrib_offset = 0;
|
---|
| 140 | for (i=0; i<n; i++) {
|
---|
| 141 | ncontrib_all += ncontrib_each[i];
|
---|
| 142 | if (i<me) contrib_offset += ncontrib_each[i];
|
---|
| 143 | }
|
---|
| 144 |
|
---|
| 145 | // allocate storage for contrib data from all nodes
|
---|
| 146 | double *vals_all = new double[ncontrib_all];
|
---|
| 147 | int **indices_all = new int*[ncontrib_all];
|
---|
| 148 | indices_all[0] = new int[ncontrib_all*nindex_];
|
---|
| 149 | for (i=1; i<ncontrib_all; i++) indices_all[i] = &indices_all[0][nindex_*i];
|
---|
| 150 | memset(vals_all, 0, sizeof(double)*ncontrib_all);
|
---|
| 151 | memset(indices_all[0], 0, sizeof(int)*ncontrib_all*nindex_);
|
---|
| 152 |
|
---|
| 153 | // send contrib data from all nodes to 0
|
---|
| 154 | for (i=0; i<ncontrib_; i++) {
|
---|
| 155 | vals_all[contrib_offset+i] = vals_[i];
|
---|
| 156 | memcpy(indices_all[contrib_offset+i], indices_[i], sizeof(int)*nindex_);
|
---|
| 157 | }
|
---|
| 158 | grp->sum(vals_all, ncontrib_all);
|
---|
| 159 | grp->sum(indices_all[0], ncontrib_all*nindex_);
|
---|
| 160 |
|
---|
| 161 | ncontrib_ = 0;
|
---|
| 162 | for (i=0; i<ncontrib_all; i++) {
|
---|
| 163 | insert(vals_all[i], indices_all[i]);
|
---|
| 164 | }
|
---|
| 165 |
|
---|
| 166 | delete[] ncontrib_each;
|
---|
| 167 | delete[] vals_all;
|
---|
| 168 | delete[] indices_all[0];
|
---|
| 169 | delete[] indices_all;
|
---|
| 170 | }
|
---|
| 171 |
|
---|
| 172 | /////////////////////////////////////////////////////////////////////////////
|
---|
| 173 |
|
---|
| 174 | // Local Variables:
|
---|
| 175 | // mode: c++
|
---|
| 176 | // c-file-style: "CLJ"
|
---|
| 177 | // End:
|
---|