accu.template
vdop.cpp
Go to the documentation of this file.
1 #include "vdop.h"
2 #include <cassert> // assert()
3 #include <cmath>
4 #include <iostream>
5 #include <vector>
6 using namespace std;
7 
8 
9 void vddiv(vector<double> & x, vector<double> const& y,
10  vector<double> const& z)
11 {
12  assert( x.size()==y.size() && y.size()==z.size() );
13  size_t n = x.size();
14 
15 #pragma omp parallel for
16  for (size_t k = 0; k < n; ++k)
17  {
18  x[k] = y[k] / z[k];
19  }
20  return;
21 }
22 
23 //******************************************************************************
24 
25 void vdaxpy(std::vector<double> & x, std::vector<double> const& y,
26  double alpha, std::vector<double> const& z )
27 {
28  assert( x.size()==y.size() && y.size()==z.size() );
29  size_t n = x.size();
30 
31 #pragma omp parallel for
32  for (size_t k = 0; k < n; ++k)
33  {
34  x[k] = y[k] + alpha * z[k];
35  }
36  return;
37 }
38 //******************************************************************************
39 
40 double dscapr(std::vector<double> const& x, std::vector<double> const& y)
41 {
42  assert( x.size()==y.size());
43  size_t n = x.size();
44 
45  double s = 0.0;
46 //#pragma omp parallel for reduction(+:s)
47  for (size_t k = 0; k < n; ++k)
48  {
49  s += x[k] * y[k];
50  }
51 
52  return s;
53 }
54 
55 //******************************************************************************
56 //void DebugVector(vector<double> const &v)
57 //{
58  //cout << "\nVector (nnode = " << v.size() << ")\n";
59  //for (size_t j = 0; j < v.size(); ++j)
60  //{
61  //cout.setf(ios::right, ios::adjustfield);
62  //cout << v[j] << " ";
63  //}
64  //cout << endl;
65 
66  //return;
67 //}
68 //******************************************************************************
69 bool CompareVectors(std::vector<double> const& x, int const n, double const y[], double const eps)
70 {
71  bool bn = (static_cast<int>(x.size())==n);
72  if (!bn)
73  {
74  cout << "######### Error: " << "number of elements" << endl;
75  }
76  //bool bv = equal(x.cbegin(),x.cend(),y);
77  bool bv = equal(x.cbegin(),x.cend(),y,
78  [eps](double a, double b) -> bool
79  { return std::abs(a-b)<eps*(1.0+0.5*(std::abs(a)+ std::abs(a))); }
80  );
81  if (!bv)
82  {
83  assert(static_cast<int>(x.size())==n);
84  cout << "######### Error: " << "values" << endl;
85  }
86  return bn && bv;
87 }
88 
89 //******************************************************************************
90 double par_scalar(vector<double> const &x, vector<double> const &y, MPI_Comm const& icomm)
91 {
92  const double s = dscapr(x,y);
93  double sg;
94  MPI_Allreduce(&s,&sg,1,MPI_DOUBLE,MPI_SUM,icomm);
95 
96  return(sg);
97 }
98 
99 //******************************************************************************
100 void ExchangeAll(vector<double> const &xin, vector<double> &yout, MPI_Comm const &icomm)
101 {
102  int myrank, numprocs,ierr(-1);
103  MPI_Comm_rank(icomm, &myrank); // my MPI-rank
104  MPI_Comm_size(icomm, &numprocs);
105  int const N=xin.size();
106  int const sendcount = N/numprocs; // equal sized junks
107  assert(sendcount*numprocs==N); // really all junk sized?
108  assert(xin.size()==yout.size());
109 
110  auto sendbuf = xin.data();
111  auto recvbuf = yout.data();
112  ierr = MPI_Alltoall(sendbuf, sendcount, MPI_DOUBLE,
113  recvbuf, sendcount, MPI_DOUBLE, icomm);
114  assert(0==ierr);
115 
116  return;
117 }
118 
119 //******************************************************************************
120 void ExchangeAllInPlace(vector<double> &xin, MPI_Comm const &icomm)
121 {
122  int myrank, numprocs,ierr(-1);
123  MPI_Comm_rank(icomm, &myrank); // my MPI-rank
124  MPI_Comm_size(icomm, &numprocs);
125  int const N=xin.size();
126  int const sendcount = N/numprocs; // equal sized junks
127  assert(sendcount*numprocs==N); // really all junk sized?
128 
129  auto sendbuf = xin.data();
130  ierr = MPI_Alltoall(MPI_IN_PLACE, sendcount, MPI_DOUBLE,
131  sendbuf, sendcount, MPI_DOUBLE, icomm);
132  assert(0==ierr);
133 
134  return;
135 }
CompareVectors
bool CompareVectors(std::vector< double > const &x, int const n, double const y[], double const eps)
Compares an STL vector with POD vector.
Definition: vdop.cpp:69
par_scalar
double par_scalar(vector< double > const &x, vector< double > const &y, MPI_Comm const &icomm)
Definition: vdop.cpp:90
vdaxpy
void vdaxpy(std::vector< double > &x, std::vector< double > const &y, double alpha, std::vector< double > const &z)
Element-wise daxpy operation x(k) = y(k) + alpha*z(k).
Definition: vdop.cpp:25
dscapr
double dscapr(std::vector< double > const &x, std::vector< double > const &y)
Calculates the Euclidean inner product of two vectors.
Definition: vdop.cpp:40
vddiv
void vddiv(vector< double > &x, vector< double > const &y, vector< double > const &z)
Definition: vdop.cpp:9
vdop.h
ExchangeAllInPlace
void ExchangeAllInPlace(vector< double > &xin, MPI_Comm const &icomm)
Definition: vdop.cpp:120
ExchangeAll
void ExchangeAll(vector< double > const &xin, vector< double > &yout, MPI_Comm const &icomm)
Definition: vdop.cpp:100