Cyclops Tensor Framework
parallel arithmetic on multidimensional arrays
checkpoint.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 
12 int checkpoint(int n,
13  World & dw,
14  int qtf=NS){
15 
16  Matrix<> A(n, n, qtf, dw);
17  Matrix<> A2(n, n, qtf, dw);
18  Matrix<> A3(n, n, qtf, dw);
19  Matrix<> A4(n, n, qtf, dw);
20  Matrix<> A5(n, n, qtf, dw);
21 
22  srand48(13*dw.rank);
23  A.fill_random(0.0,1.0);
24  A.print();
25  A["ii"] = 0.0;
26  A2["ij"] = A["ij"];
27  A3["ij"] = 2.*A["ij"];
28 
29  MPI_File file;
30  MPI_File_open(dw.comm, "CTF_checkpoint_test_file.bin", MPI_MODE_WRONLY | MPI_MODE_CREATE, MPI_INFO_NULL, &file);
31  A2.write_dense_to_file(file);
32  A3.write_dense_to_file(file,n*n*sizeof(double));
33  MPI_File_close(&file);
34 
35  MPI_File_open(dw.comm, "CTF_checkpoint_test_file.bin", MPI_MODE_RDONLY | MPI_MODE_DELETE_ON_CLOSE, MPI_INFO_NULL, &file);
36  A4.read_dense_from_file(file);
37 
38  A4.print();
39  A["ij"] -= A4["ij"];
40  int pass = A.norm2() <= 1.e-9*n;
41 
42  A5.read_dense_from_file(file,n*n*sizeof(double));
43  MPI_File_close(&file);
44  A5["ij"] -= 2.*A4["ij"];
45  pass = pass & (A5.norm2() <= 1.e-9*n);
46 
47  if (dw.rank == 0){
48  if (!pass){
49  printf("{ checkpointing using dense data representation with qtf=%d } failed\n",qtf);
50  } else {
51  printf("{ checkpointing using dense data representation with qtf=%d } passed\n",qtf);
52  }
53  }
54  return pass;
55 
56 }
57 
58 
59 #ifndef TEST_SUITE
60 char* getCmdOption(char ** begin,
61  char ** end,
62  const std::string & option){
63  char ** itr = std::find(begin, end, option);
64  if (itr != end && ++itr != end){
65  return *itr;
66  }
67  return 0;
68 }
69 
70 
71 int main(int argc, char ** argv){
72  int rank, np, n, qtf;
73  int const in_num = argc;
74  char ** input_str = argv;
75 
76  MPI_Init(&argc, &argv);
77  MPI_Comm_rank(MPI_COMM_WORLD, &rank);
78  MPI_Comm_size(MPI_COMM_WORLD, &np);
79 
80  if (getCmdOption(input_str, input_str+in_num, "-n")){
81  n = atoi(getCmdOption(input_str, input_str+in_num, "-n"));
82  if (n < 0) n = 7;
83  } else n = 7;
84 
85  if (getCmdOption(input_str, input_str+in_num, "-qtf")){
86  qtf = atoi(getCmdOption(input_str, input_str+in_num, "-qtf"));
87  if (qtf < 0) qtf = NS;
88  } else qtf = NS;
89 
90 
91 
92  {
93  World dw(MPI_COMM_WORLD, argc, argv);
94  if (rank == 0){
95  printf("Checking checkpoint calculation n = %d, p = %d, qtf = %d:\n",n,np,qtf);
96  }
97  int pass = checkpoint(n,dw,qtf);
98  assert(pass);
99  }
100 
101  MPI_Finalize();
102  return 0;
103 }
109 #endif
Matrix class which encapsulates a 2D tensor.
Definition: matrix.h:18
def rank(self)
Definition: core.pyx:312
int checkpoint(int n, World &dw, int qtf=NS)
Definition: checkpoint.cxx:12
Definition: common.h:37
an instance of the CTF library (world) on a MPI communicator
Definition: world.h:19
void write_dense_to_file(MPI_File &file, int64_t offset=0)
write all tensor data to binary file in element order, unpacking from sparse or symmetric formats ...
void read_dense_from_file(MPI_File &file, int64_t offset=0)
read all tensor data from binary file in element order, which should be stored as nonsymmetric and de...
string
Definition: core.pyx:456
dtype norm2()
computes the frobenius norm of the tensor (needs sqrt()!)
Definition: tensor.h:811
void fill_random(dtype rmin, dtype rmax)
fills local unique tensor elements to random values in the range [min,max] works only for dtype in {f...
Definition: tensor.cxx:928
int rank
rank of local processor
Definition: world.h:24
int main(int argc, char **argv)
Definition: checkpoint.cxx:71
void print(FILE *fp, dtype cutoff) const
prints tensor data to file using process 0 (modify print(...) overload in set.h if you would like a d...
Definition: tensor.cxx:407
char * getCmdOption(char **begin, char **end, const std::string &option)
Definition: checkpoint.cxx:60
Definition: apsp.cxx:17
MPI_Comm comm
set of processors making up this world
Definition: world.h:22
def np(self)
Definition: core.pyx:315