Cyclops Tensor Framework
parallel arithmetic on multidimensional arrays
fast_diagram.cxx
Go to the documentation of this file.
1 /*Copyright (c) 2011, Edgar Solomonik, all rights reserved.*/
2 
3 #include <ctf.hpp>
4 
5 using namespace CTF;
6 
7 int fast_diagram(int const n,
8  World &ctf){
9  int rank, i, num_pes;
10 
11  MPI_Comm_rank(MPI_COMM_WORLD, &rank);
12  MPI_Comm_size(MPI_COMM_WORLD, &num_pes);
13 
14  int len3[] = {n,n,n};
15  int len4[] = {n,n,n,n};
16  //int len5[] = {n,n,n,n,n};
17  int NNN[] = {NS,NS,NS};
18  int NNNN[] = {NS,NS,NS,NS};
19  int ANNN[] = {AS,NS,NS,NS};
20  int SNNN[] = {SH,NS,NS,NS};
21  //int AANNN[] = {AS,AS,NS,NS,NS};
22 
23  Tensor<> T(4, len4, SNNN, ctf);
24  Tensor<> V(4, len4, SNNN, ctf);
25 
26  Tensor<> W(4, len4, SNNN, ctf);
27  Tensor<> W_ans(4, len4, SNNN, ctf);
28 
29  Tensor<> Z_AS(4, len4, ANNN, ctf);
30  Tensor<> Z_SH(4, len4, SNNN, ctf);
31  Tensor<> Z_NS(4, len4, NNNN, ctf);
32  Tensor<> Z_D(3, len3, NNN, ctf);
33 
34 
35  Tensor<> Ts(3, len3, NNN, ctf);
36  Tensor<> Zs(3, len3, NNN, ctf);
37 
38  {
39  int64_t * indices;
40  double * values;
41  int64_t size;
42  srand48(173*rank);
43 
44  T.read_local(&size, &indices, &values);
45  for (i=0; i<size; i++){
46  values[i] = drand48();
47  }
48  T.write(size, indices, values);
49  free(indices);
50  free(values);
51  V.read_local(&size, &indices, &values);
52  for (i=0; i<size; i++){
53  values[i] = drand48();
54  }
55  V.write(size, indices, values);
56  free(indices);
57  free(values);
58  }
59  Z_NS["afin"] = T["aeim"]*V["efmn"];
60 // Z_NS.print(stdout);
61  W_ans["abij"] = Z_NS["afin"]*T["fbnj"];
62 // W_ans.print(stdout);
63 
64 // Z_AS.print(stdout);
65  Z_AS["afin"] = T["aeim"]*V["efmn"];
66  Z_SH["afin"] = T["aeim"]*V["efmn"];
67  Z_D["ain"] = T["aeim"]*V["eamn"];
68  W["abij"] = Z_AS["afin"]*T["fbnj"];
69  W["abij"] += Z_SH["afin"]*T["fbnj"];
70  W["abij"] += Z_D["ain"]*T["abnj"];
71 // W["abij"] -= Z_SY["aain"]*T["fbnj"];
72 // W.print(stdout);
73  //Z_AS["ebmj"] = V["efmn"]*T["fbnj"];
74 // W["abij"] = T["aeim"]*Z_AS["ebmj"];
75 // W["abij"] -= W_ans["abij"];
76 // W.print(stdout);
77 // Ts["eim"] = T["feim"];
78 // Zs["ain"] = Ts["eim"]*V["eamn"];
79 // W.print(stdout);
80 // W["abij"] -= Zs["ain"]*Ts["bnj"];
81 // W["abij"] = W["abij"];
82 
83  double nrm = sqrt((double)((W["abij"]-W_ans["abij"])*(W["abij"]-W_ans["abij"])));
84 
85  int pass = (nrm <=1.E-10);
86 
87  if (rank == 0){
88  MPI_Reduce(MPI_IN_PLACE, &pass, 1, MPI_INT, MPI_MIN, 0, MPI_COMM_WORLD);
89  if (pass) printf("{ C[\"(ij)ab\"] = A[\"(ik)al\"]*B[\"(kj)lb\"] } passed\n");
90  else printf("{ C[\"(ij)ab\"] = A[\"(ik)al\"]*B[\"(kj)lb\"] } failed\n");
91  } else
92  MPI_Reduce(&pass, MPI_IN_PLACE, 1, MPI_INT, MPI_MIN, 0, MPI_COMM_WORLD);
93  return pass;
94 }
95 
96 #ifndef TEST_SUITE
97 char* getCmdOption(char ** begin,
98  char ** end,
99  const std::string & option){
100  char ** itr = std::find(begin, end, option);
101  if (itr != end && ++itr != end){
102  return *itr;
103  }
104  return 0;
105 }
106 
107 
108 int main(int argc, char ** argv){
109  int rank, np, n;
110  int const in_num = argc;
111  char ** input_str = argv;
112 
113  MPI_Init(&argc, &argv);
114  MPI_Comm_rank(MPI_COMM_WORLD, &rank);
115  MPI_Comm_size(MPI_COMM_WORLD, &np);
116 
117  if (getCmdOption(input_str, input_str+in_num, "-n")){
118  n = atoi(getCmdOption(input_str, input_str+in_num, "-n"));
119  if (n < 0) n = 5;
120  } else n = 5;
121 
122  {
123  World dw(MPI_COMM_WORLD, argc, argv);
124  if (rank == 0){
125  printf("Computing W^(ab)_ij=sum_(efmn)T^(ae)_im*V^(ef)_mn*T^(fb)_nj\n");
126  }
127  int pass = fast_diagram(n, dw);
128  assert(pass);
129  }
130 
131  MPI_Finalize();
132  return 0;
133 }
134 #endif
135 
int fast_diagram(int const n, World &ctf)
Definition: fast_diagram.cxx:7
char * getCmdOption(char **begin, char **end, const std::string &option)
def rank(self)
Definition: core.pyx:312
Definition: common.h:37
an instance of the CTF library (world) on a MPI communicator
Definition: world.h:19
void read_local(int64_t *npair, int64_t **global_idx, dtype **data, bool unpack_sym=false) const
Using get_local_data(), which returns an array that must be freed with delete [], is more efficient...
Definition: tensor.cxx:182
string
Definition: core.pyx:456
Definition: apsp.cxx:17
int main(int argc, char **argv)
an instance of a tensor within a CTF world
Definition: tensor.h:74
Definition: common.h:37
void write(int64_t npair, int64_t const *global_idx, dtype const *data)
writes in values associated with any set of indices The sparse data is defined in coordinate format...
Definition: tensor.cxx:264
Definition: common.h:37
def np(self)
Definition: core.pyx:315