Cyclops Tensor Framework
parallel arithmetic on multidimensional arrays
multi_tsr_sym.cxx
Go to the documentation of this file.
1 /*Copyright (c) 2011, Edgar Solomonik, all rights reserved.*/
9 #include <ctf.hpp>
10 
11 using namespace CTF;
12 
13 int multi_tsr_sym(int m,
14  int n,
15  World & dw){
16  int rank, i, num_pes;
17  int64_t np;
18  double * pairs;
19  int64_t * indices;
20 
21  MPI_Comm_rank(MPI_COMM_WORLD, &rank);
22  MPI_Comm_size(MPI_COMM_WORLD, &num_pes);
23 
24 #ifndef TEST_SUITE
25  if (rank == 0)
26  printf("m = %d, n = %d, p = %d\n",
27  m,n,num_pes);
28 #endif
29 
30  //* Creates distributed tensors initialized with zeros
31  Matrix<> A(n, m, NS, dw);
32  Matrix<> C_NS(n, n, NS, dw);
33  Matrix<> C_SY(n, n, SY, dw);
34  Matrix<> diff(n, n, NS, dw);
35 
36  srand48(13*rank);
37  //* Writes noise to local data based on global index
38  A.get_local_data(&np, &indices, &pairs);
39  for (i=0; i<np; i++ ) pairs[i] = drand48()-.5; //(1.E-3)*sin(indices[i]);
40  A.write(np, indices, pairs);
41  delete [] pairs;
42  free(indices);
43 
44  C_NS["ij"] = A["ik"]*A["jk"];
45  C_SY["ij"] = A["ik"]*A["jk"];
46 
47  diff["ij"] = C_SY["ij"] - C_NS["ij"];
48 
49  double err = diff.norm2();
50  if (rank == 0){
51  if (err < 1.E-6)
52  printf("{ A[\"ik\"]*A[\"jk\"] = C_NS[\"ij\"] = C_AS[\"ij\"] } passed.\n");
53  else
54  printf("{ A[\"ik\"]*A[\"jk\"] = C_NS[\"ij\"] = C_AS[\"ij\"] } failed!\n");
55  }
56 
57  return err < 1.E-6;
58 }
59 
60 
61 #ifndef TEST_SUITE
62 char* getCmdOption(char ** begin,
63  char ** end,
64  const std::string & option){
65  char ** itr = std::find(begin, end, option);
66  if (itr != end && ++itr != end){
67  return *itr;
68  }
69  return 0;
70 }
71 
72 int main(int argc, char ** argv){
73  int rank, np, n, m, pass;
74  int in_num = argc;
75  char ** input_str = argv;
76 
77  MPI_Init(&argc, &argv);
78  MPI_Comm_rank(MPI_COMM_WORLD, &rank);
79  MPI_Comm_size(MPI_COMM_WORLD, &np);
80 
81  if (getCmdOption(input_str, input_str+in_num, "-n")){
82  n = atoi(getCmdOption(input_str, input_str+in_num, "-n"));
83  if (n < 0) n = 7;
84  } else n = 7;
85  if (getCmdOption(input_str, input_str+in_num, "-m")){
86  m = atoi(getCmdOption(input_str, input_str+in_num, "-m"));
87  if (m < 0) m = 7;
88  } else m = 7;
89 
90  {
91  World dw(MPI_COMM_WORLD, argc, argv);
92 
93  pass =multi_tsr_sym(m, n, dw);
94  assert(pass);
95  }
96 
97  MPI_Finalize();
98  return 0;
99 }
106 #endif
Matrix class which encapsulates a 2D tensor.
Definition: matrix.h:18
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
string
Definition: core.pyx:456
dtype norm2()
computes the frobenius norm of the tensor (needs sqrt()!)
Definition: tensor.h:811
char * getCmdOption(char **begin, char **end, const std::string &option)
int main(int argc, char **argv)
int multi_tsr_sym(int m, int n, World &dw)
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
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
def np(self)
Definition: core.pyx:315