Cyclops Tensor Framework parallel arithmetic on multidimensional arrays
sy_times_ns.cxx
Go to the documentation of this file.
2
10 #include <ctf.hpp>
11
12 using namespace CTF;
13
14 int sy_times_ns(int n,
15  World & dw){
16  int rank, i, num_pes, pass;
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
25  int shapeN4[] = {NS,NS,NS,NS};
26  int sizeN4[] = {n,n,n,n};
27
28  //* Creates distributed tensors initialized with zeros
29  Tensor<> B(4, sizeN4, shapeN4, dw);
30
31  Matrix<> A(n, n, SY, dw);
32  Matrix<> An(n, n, NS, dw);
33  Matrix<> C(n, n, SY, dw, "C");
34  Matrix<> Cn(n, n, NS, dw, "Cn");
35
36  srand48(13*rank);
37
38
39  A.get_local_data(&np, &indices, &pairs);
40  for (i=0; i<np; i++ ) pairs[i] = drand48()-.5; //(1.E-3)*sin(indices[i]);
41 // A.write(np, indices, pairs);
42  delete [] pairs;
43  free(indices);
44  B.get_local_data(&np, &indices, &pairs);
45  for (i=0; i<np; i++ ) pairs[i] = drand48()-.5; //(1.E-3)*sin(indices[i]);
46 // B.write(np, indices, pairs);
47  delete [] pairs;
48  free(indices);
49  C.get_local_data(&np, &indices, &pairs);
50  for (i=0; i<np; i++ ) pairs[i] = drand48()-.5; //(1.E-3)*sin(indices[i]);
51  C.write(np, indices, pairs);
52  delete [] pairs;
53  free(indices);
54
55  An["ij"] = A["ij"];
56  Cn["ij"] = C["ij"];
57
58  C["ij"] += A["ij"]*B["ijkl"];
59  Cn["ij"] += An["ij"]*B["ijkl"];
60  Cn["ji"] += An["ij"]*B["ijkl"];
61
62
63  Cn["ij"] -= C["ij"];
64
65  double norm = Cn.norm2();
66
67  if (norm < 1.E-10){
68  pass = 1;
69  if (rank == 0)
70  printf("{ C[\"(ij)\"]=A[\"(ij)\"]*B[\"ijkl\"] } passed \n");
71  } else {
72  pass = 0;
73  if (rank == 0)
74  printf("{ C[\"(ij)\"]=A[\"(ij)\"]*B[\"ijkl\"] } failed \n");
75  }
76  return pass;
77 }
78
79
80 #ifndef TEST_SUITE
81 char* getCmdOption(char ** begin,
82  char ** end,
83  const std::string & option){
84  char ** itr = std::find(begin, end, option);
85  if (itr != end && ++itr != end){
86  return *itr;
87  }
88  return 0;
89 }
90
91
92 int main(int argc, char ** argv){
93  int rank, np, n;
94  int in_num = argc;
95  char ** input_str = argv;
96
97  MPI_Init(&argc, &argv);
98  MPI_Comm_rank(MPI_COMM_WORLD, &rank);
99  MPI_Comm_size(MPI_COMM_WORLD, &np);
100
101  if (getCmdOption(input_str, input_str+in_num, "-n")){
102  n = atoi(getCmdOption(input_str, input_str+in_num, "-n"));
103  if (n < 0) n = 7;
104  } else n = 7;
105
106
107  {
108  World dw(argc, argv);
109  sy_times_ns(n, dw);
110  }
111
112  MPI_Finalize();
113  return 0;
114 }
120 #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
int main(int argc, char **argv)
Definition: sy_times_ns.cxx:92
char * getCmdOption(char **begin, char **end, const std::string &option)
Definition: sy_times_ns.cxx:81
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
Definition: common.h:37
int sy_times_ns(int n, World &dw)
Definition: sy_times_ns.cxx:14
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