Cyclops Tensor Framework
parallel arithmetic on multidimensional arrays
checkpoint_sparse.cxx
Go to the documentation of this file.
1 /*Copyright (c) 2016, Edgar Solomonik, all rights reserved.*/
9 #include <ctf.hpp>
10 using namespace CTF;
11 
13  World & dw,
14  int qtf=NS){
15  int lens_u[] = {n,n,n};
16 
17  Tensor<> u(3, true, lens_u);
18  u.fill_sp_random(0., 1., .1);
19  u.write_sparse_to_file("checkpoint_sparse_tensor.txt");
20  Tensor<> v(3, true, lens_u);
21 
22  v.read_sparse_from_file("checkpoint_sparse_tensor.txt");
23  srand48(13*dw.rank);
24  v["ijk"] -= u["ijk"];
25 
26  bool pass = v.norm2() < 1.e-7*n*n*.1*n;
27 
28  MPI_Info info;
29  MPI_File_delete("checkpoint_sparse_tensor.txt", info);
30 
31  if (dw.rank == 0){
32  if (!pass){
33  printf("{ sparse_checkpointing using dense data representation with qtf=%d } failed\n",qtf);
34  } else {
35  printf("{ sparse_checkpointing using dense data representation with qtf=%d } passed\n",qtf);
36  }
37  }
38  return pass;
39 
40 }
41 
42 
43 #ifndef TEST_SUITE
44 char* getCmdOption(char ** begin,
45  char ** end,
46  const std::string & option){
47  char ** itr = std::find(begin, end, option);
48  if (itr != end && ++itr != end){
49  return *itr;
50  }
51  return 0;
52 }
53 
54 
55 int main(int argc, char ** argv){
56  int rank, np, n, qtf;
57  int const in_num = argc;
58  char ** input_str = argv;
59 
60  MPI_Init(&argc, &argv);
61  MPI_Comm_rank(MPI_COMM_WORLD, &rank);
62  MPI_Comm_size(MPI_COMM_WORLD, &np);
63 
64  if (getCmdOption(input_str, input_str+in_num, "-n")){
65  n = atoi(getCmdOption(input_str, input_str+in_num, "-n"));
66  if (n < 0) n = 7;
67  } else n = 7;
68 
69  if (getCmdOption(input_str, input_str+in_num, "-qtf")){
70  qtf = atoi(getCmdOption(input_str, input_str+in_num, "-qtf"));
71  if (qtf < 0) qtf = NS;
72  } else qtf = NS;
73 
74 
75 
76  {
77  World dw(MPI_COMM_WORLD, argc, argv);
78  if (rank == 0){
79  printf("Checking sparse_checkpoint calculation n = %d, p = %d, qtf = %d:\n",n,np,qtf);
80  }
81  int pass = sparse_checkpoint(n,dw,qtf);
82  assert(pass);
83  }
84 
85  MPI_Finalize();
86  return 0;
87 }
93 #endif
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
string
Definition: core.pyx:456
dtype norm2()
computes the frobenius norm of the tensor (needs sqrt()!)
Definition: tensor.h:811
void read_sparse_from_file(const char *fpath, bool with_vals=true)
read sparse tensor from file, entries of tensor must be stored one per line, as i_1 ...
int rank
rank of local processor
Definition: world.h:24
int main(int argc, char **argv)
void fill_sp_random(dtype rmin, dtype rmax, double frac_sp)
generate roughly frac_sp*dense_tensor_size nonzeros between rmin and rmax, works only for dtype in {f...
Definition: tensor.cxx:969
int sparse_checkpoint(int n, World &dw, int qtf=NS)
void write_sparse_to_file(const char *fpath, bool with_vals=true)
write sparse tensor to file, entries of tensor will be stored one per line, as i_1 ...
Definition: apsp.cxx:17
an instance of a tensor within a CTF world
Definition: tensor.h:74
def np(self)
Definition: core.pyx:315