Cyclops Tensor Framework
parallel arithmetic on multidimensional arrays
weigh_4D.cxx
Go to the documentation of this file.
1 /*Copyright (c) 2011, Edgar Solomonik, all rights reserved.*/
2 
10 #include <ctf.hpp>
11 using namespace CTF;
12 
13 double divide(double a, double b){
14  return a/b;
15 }
16 
17 int weigh_4D(int const n,
18  int const sym,
19  World &dw){
20  int rank, i, num_pes;
21  int64_t np, np_A;
22  double * pairs, * post_pairs_C, * pairs_A;
23  int64_t * indices, * indices_A;
24 
25  MPI_Comm_rank(MPI_COMM_WORLD, &rank);
26  MPI_Comm_size(MPI_COMM_WORLD, &num_pes);
27 
28  int shapeN4[] = {sym,NS,sym,NS};
29  int sizeN4[] = {n,n,n,n};
30 
31  //* Creates distributed tensors initialized with zeros
32  Tensor<> A(4, sizeN4, shapeN4, dw);
33  Tensor<> B(4, sizeN4, shapeN4, dw);
34  Tensor<> C(4, sizeN4, shapeN4, dw);
35 
36  srand48(13*rank);
37  A.get_local_data(&np_A, &indices_A, &pairs_A);
38  for (i=0; i<np_A; i++ ) pairs_A[i] = drand48()-.5; //(1.E-3)*sin(indices[i]);
39  A.write(np_A, indices_A, pairs_A);
40  B.get_local_data(&np, &indices, &pairs);
41  for (i=0; i<np; i++ ) pairs[i] = drand48()-.5; //(1.E-3)*sin(indices[i]);
42  B.write(np, indices, pairs);
43  delete [] pairs;
44  free(indices);
45  C.get_local_data(&np, &indices, &pairs);
46  for (i=0; i<np; i++ ) pairs[i] = drand48()-.5; //(1.E-3)*sin(indices[i]);
47  C.write(np, indices, pairs);
48  delete [] pairs;
49  free(indices);
50 
51  C["ijkl"] = A["ijkl"]*B["klij"];
52 
53  CTF::Function<> fctr(&divide);
54 
55  C.contract(1.0, C, "ijkl", B, "klij", 0.0, "ijkl", fctr);
56 
57  post_pairs_C = (double*)malloc(np_A*sizeof(double));
58  C.read(np_A, indices_A, post_pairs_C);
59 
60  int pass = 1;
61  for (i=0; i<np_A; i++){
62  if (fabs(pairs_A[i]) > 1.E-10 &&
63  fabs((double)post_pairs_C[i]-(double)pairs_A[i])/(double)pairs_A[i]>1.E-10){
64  pass = 0;
65  }
66  }
67  if (rank == 0){
68  MPI_Reduce(MPI_IN_PLACE, &pass, 1, MPI_INT, MPI_MIN, 0, MPI_COMM_WORLD);
69  if (pass){
70  printf("{ C[\"ijkl\"] = A[\"ijkl\"]*B[\"ijkl\"] } passed\n");
71  } else {
72  printf("{ C[\"ijkl\"] = A[\"ijkl\"]*B[\"ijkl\"] } failed\n");
73  }
74  } else
75  MPI_Reduce(&pass, MPI_IN_PLACE, 1, MPI_INT, MPI_MIN, 0, MPI_COMM_WORLD);
76 
77  free(indices_A);
78  delete [] pairs_A;
79  free(post_pairs_C);
80  return pass;
81 }
82 
83 
84 #ifndef TEST_SUITE
85 
86 char* getCmdOption(char ** begin,
87  char ** end,
88  const std::string & option){
89  char ** itr = std::find(begin, end, option);
90  if (itr != end && ++itr != end){
91  return *itr;
92  }
93  return 0;
94 }
95 
96 
97 int main(int argc, char ** argv){
98  int rank, np, n;
99  int const in_num = argc;
100  char ** input_str = argv;
101 
102  MPI_Init(&argc, &argv);
103  MPI_Comm_rank(MPI_COMM_WORLD, &rank);
104  MPI_Comm_size(MPI_COMM_WORLD, &np);
105 
106  if (getCmdOption(input_str, input_str+in_num, "-n")){
107  n = atoi(getCmdOption(input_str, input_str+in_num, "-n"));
108  if (n < 0) n = 7;
109  } else n = 7;
110 
111 
112  {
113  World dw(MPI_COMM_WORLD, argc, argv);
114 
115  if (rank == 0){
116  printf("Computing C_ijkl = A_ijkl*B_kilj\n");
117  printf("Non-symmetric: NS = NS*NS weigh:\n");
118  }
119  weigh_4D(n, NS, dw);
120  if (rank == 0){
121  printf("Symmetric: SY = SY*SY weigh:\n");
122  }
123  weigh_4D(n, SY, dw);
124  if (rank == 0){
125  printf("(Anti-)Skew-symmetric: AS = AS*AS weigh:\n");
126  }
127  weigh_4D(n, AS, dw);
128  }
129 
130 
131  MPI_Finalize();
132  return 0;
133 }
134 
140 #endif
void contract(dtype alpha, CTF_int::tensor &A, char const *idx_A, CTF_int::tensor &B, char const *idx_B, dtype beta, char const *idx_C)
contracts C[idx_C] = beta*C[idx_C] + alpha*A[idx_A]*B[idx_B]
double divide(double a, double b)
Definition: weigh_4D.cxx:13
int weigh_4D(int const n, int const sym, World &dw)
Definition: weigh_4D.cxx:17
def rank(self)
Definition: core.pyx:312
Definition: common.h:37
int main(int argc, char **argv)
Definition: weigh_4D.cxx:97
an instance of the CTF library (world) on a MPI communicator
Definition: world.h:19
string
Definition: core.pyx:456
void get_local_data(int64_t *npair, int64_t **global_idx, dtype **data, bool nonzeros_only=false, bool unpack_sym=false) const
Gives the global indices and values associated with the local data.
Definition: tensor.cxx:159
Definition: apsp.cxx:17
an instance of a tensor within a CTF world
Definition: tensor.h:74
void read(int64_t npair, Pair< dtype > *pairs)
Gives the values associated with any set of indices.
Definition: tensor.cxx:246
Definition: common.h:37
Definition: common.h:37
char * getCmdOption(char **begin, char **end, const std::string &option)
Definition: weigh_4D.cxx:86
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
def np(self)
Definition: core.pyx:315