Cyclops Tensor Framework
parallel arithmetic on multidimensional arrays
reduce_bcast.cxx
Go to the documentation of this file.
1 /*Copyright (c) 2011, Edgar Solomonik, all rights reserved.*/
2 
10 #include <ctf.hpp>
11 
12 using namespace CTF;
13 
14 int reduce_bcast(int n,
15  World & dw){
16  int pass;
17 
18  Matrix<> A(n,n,dw);
19  Matrix<> B(n,1,dw);
20  Matrix<> C(n,n,dw);
21  Matrix<> C2(n,n,dw);
22  Vector<> d(n,dw);
23 
24  srand48(13*dw.rank);
25 
26  A.fill_random(0.,1.);
27  B.fill_random(0.,1.);
28  C.fill_random(0.,1.);
29  C2["ij"] = C["ij"];
30  d.fill_random(0.,1.);
31 
32  C["ij"] += B["ik"];
33 
34  d["i"] = B["ij"];
35 
36  C2["ij"] += d["i"];
37 
38  C["ij"] -= C2["ij"];
39 
40  pass = true;
41  if (C.norm2() > 1.E-6){
42  pass = false;
43  if (dw.rank == 0)
44  printf("{ (A[\"ij\"]+=B[\"ik\"] with square B } failed \n");
45  return pass;
46  }
47 
48  C["ij"] = C2["ij"];
49 
50  C["ij"] += B["ik"];
51 
52  d["i"] = B["ik"];
53 
54  C2["ij"] += d["i"];
55 
56  C["ij"] -= C2["ij"];
57 
58  if (C.norm2() > 1.E-6)
59  pass = false;
60 
61  if (pass){
62  if (dw.rank == 0)
63  printf("{ (A[\"ij\"]+=B[\"ik\"] } passed \n");
64  } else {
65  if (dw.rank == 0)
66  printf("{ (A[\"ij\"]+=B[\"ik\"] with column vector B } failed \n");
67  }
68  return pass;
69 }
70 
71 
72 #ifndef TEST_SUITE
73 char* getCmdOption(char ** begin,
74  char ** end,
75  const std::string & option){
76  char ** itr = std::find(begin, end, option);
77  if (itr != end && ++itr != end){
78  return *itr;
79  }
80  return 0;
81 }
82 
83 
84 int main(int argc, char ** argv){
85  int rank, np, n;
86  int in_num = argc;
87  char ** input_str = argv;
88 
89  MPI_Init(&argc, &argv);
90  MPI_Comm_rank(MPI_COMM_WORLD, &rank);
91  MPI_Comm_size(MPI_COMM_WORLD, &np);
92 
93  if (getCmdOption(input_str, input_str+in_num, "-n")){
94  n = atoi(getCmdOption(input_str, input_str+in_num, "-n"));
95  if (n < 0) n = 7;
96  } else n = 7;
97 
98 
99  {
100  World dw(argc, argv);
101  reduce_bcast(n, dw);
102  }
103 
104  MPI_Finalize();
105  return 0;
106 }
112 #endif
char * getCmdOption(char **begin, char **end, const std::string &option)
Matrix class which encapsulates a 2D tensor.
Definition: matrix.h:18
def rank(self)
Definition: core.pyx:312
Vector class which encapsulates a 1D tensor.
Definition: vector.h:14
int main(int argc, char **argv)
an instance of the CTF library (world) on a MPI communicator
Definition: world.h:19
string
Definition: core.pyx:456
dtype norm2()
computes the frobenius norm of the tensor (needs sqrt()!)
Definition: tensor.h:811
void fill_random(dtype rmin, dtype rmax)
fills local unique tensor elements to random values in the range [min,max] works only for dtype in {f...
Definition: tensor.cxx:928
int rank
rank of local processor
Definition: world.h:24
Definition: apsp.cxx:17
int reduce_bcast(int n, World &dw)
def np(self)
Definition: core.pyx:315