Cyclops Tensor Framework
parallel arithmetic on multidimensional arrays
spmv.cxx
Go to the documentation of this file.
1 
8 #include <ctf.hpp>
9 using namespace CTF;
10 
11 int spmv(int n,
12  World & dw){
13 
14  Matrix<> spA(n, n, SP, dw);
15  Matrix<> dnA(n, n, dw);
16  Vector<> b(n, dw);
17  Vector<> c1(n, dw);
18  Vector<> c2(n, dw);
19 
20  srand48(dw.rank);
21  b.fill_random(0.0,1.0);
22  c1.fill_random(0.0,1.0);
23  dnA.fill_random(0.0,1.0);
24 
25  spA["ij"] += dnA["ij"];
26  spA.sparsify(.5);
27  dnA["ij"] = 0.0;
28  dnA["ij"] += spA["ij"];
29 
30  c2["i"] = c1["i"];
31 
32  c1["i"] += dnA["ij"]*b["j"];
33 
34  c2["i"] += .5*spA["ij"]*b["j"];
35  c2["i"] += .5*b["j"]*spA["ij"];
36 
37 
38  bool pass = c2.norm2() >= 1E-6;
39 
40  c2["i"] -= c1["i"];
41 
42  if (pass) pass = c2.norm2() <= 1.E-6;
43 
44  if (dw.rank == 0){
45  if (pass)
46  printf("{ c[\"i\"] += A[\"ij\"]*b[\"j\"] with sparse, A } passed \n");
47  else
48  printf("{ c[\"i\"] += A[\"ij\"]*b[\"j\"] with sparse, A } failed \n");
49  }
50  return pass;
51 }
52 
53 
54 #ifndef TEST_SUITE
55 char* getCmdOption(char ** begin,
56  char ** end,
57  const std::string & option){
58  char ** itr = std::find(begin, end, option);
59  if (itr != end && ++itr != end){
60  return *itr;
61  }
62  return 0;
63 }
64 
65 
66 int main(int argc, char ** argv){
67  int rank, np, n, pass;
68  int const in_num = argc;
69  char ** input_str = argv;
70 
71  MPI_Init(&argc, &argv);
72  MPI_Comm_rank(MPI_COMM_WORLD, &rank);
73  MPI_Comm_size(MPI_COMM_WORLD, &np);
74 
75  if (getCmdOption(input_str, input_str+in_num, "-n")){
76  n = atoi(getCmdOption(input_str, input_str+in_num, "-n"));
77  if (n < 0) n = 7;
78  } else n = 7;
79 
80 
81  {
82  World dw(argc, argv);
83 
84  if (rank == 0){
85  printf("Multiplying %d-by-%d sparse matrix by vector\n",n,n);
86  }
87  pass = spmv(n, dw);
88  assert(pass);
89  }
90 
91  MPI_Finalize();
92  return 0;
93 }
99 #endif
Matrix class which encapsulates a 2D tensor.
Definition: matrix.h:18
def rank(self)
Definition: core.pyx:312
int spmv(int n, World &dw)
Definition: spmv.cxx:11
Definition: common.h:37
Vector class which encapsulates a 1D tensor.
Definition: vector.h:14
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 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 main(int argc, char **argv)
Definition: spmv.cxx:66
int rank
rank of local processor
Definition: world.h:24
char * getCmdOption(char **begin, char **end, const std::string &option)
Definition: spmv.cxx:55
void sparsify()
reduce tensor to sparse format, storing only nonzero data, or data above a specified threshold...
Definition: tensor.cxx:449
Definition: apsp.cxx:17
def np(self)
Definition: core.pyx:315