Cyclops Tensor Framework
parallel arithmetic on multidimensional arrays
repack.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 repack(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 shapeS4[] = {NS,NS,SY,NS};
27  int sizeN4[] = {n,n,n,n};
28 
29  //* Creates distributed tensors initialized with zeros
30  Tensor<> An(4, sizeN4, shapeN4, dw);
31  Tensor<> As(4, sizeN4, shapeS4, dw);
32 
33  As.get_local_data(&np, &indices, &pairs);
34  for (i=0; i<np; i++ ) pairs[i] = drand48()-.5; //(1.E-3)*sin(indices[i]);
35  As.write(np, indices, pairs);
36  An.write(np, indices, pairs);
37 
38  Tensor<> Anr(An, shapeS4);
39 
40  Anr["ijkl"] -= As["ijkl"];
41 
42  double norm = Anr.norm2();
43 
44  if (norm < 1.E-6)
45  pass = 1;
46  else
47  pass = 0;
48 
49  if (!pass)
50  printf("{ NS -> SY repack } failed \n");
51  else {
52  Tensor<> Anur(As, shapeN4);
53  Tensor<> Asur(As, shapeN4);
54  Asur["ijkl"] = 0.0;
55  Asur.write(np, indices, pairs);
56  Anur["ijkl"] -= Asur["ijkl"];
57 
58  norm = Anur.norm2();
59 
60  if (norm < 1.E-6){
61  pass = 1;
62  if (rank == 0)
63  printf("{ NS -> SY -> NS repack } passed \n");
64  } else {
65  pass = 0;
66  if (rank == 0)
67  printf("{ SY -> NS repack } failed \n");
68  }
69 
70  }
71  delete [] pairs;
72  free(indices);
73  return pass;
74 }
75 
76 
77 #ifndef TEST_SUITE
78 char* getCmdOption(char ** begin,
79  char ** end,
80  const std::string & option){
81  char ** itr = std::find(begin, end, option);
82  if (itr != end && ++itr != end){
83  return *itr;
84  }
85  return 0;
86 }
87 
88 
89 int main(int argc, char ** argv){
90  int rank, np, n;
91  int in_num = argc;
92  char ** input_str = argv;
93 
94  MPI_Init(&argc, &argv);
95  MPI_Comm_rank(MPI_COMM_WORLD, &rank);
96  MPI_Comm_size(MPI_COMM_WORLD, &np);
97 
98  if (getCmdOption(input_str, input_str+in_num, "-n")){
99  n = atoi(getCmdOption(input_str, input_str+in_num, "-n"));
100  if (n < 0) n = 7;
101  } else n = 7;
102 
103 
104  {
105  World dw(argc, argv);
106  repack(n, dw);
107  }
108 
109  MPI_Finalize();
110  return 0;
111 }
117 #endif
def rank(self)
Definition: core.pyx:312
int main(int argc, char **argv)
Definition: repack.cxx:89
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)
Definition: repack.cxx:78
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 repack(int n, World &dw)
Definition: repack.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