Cyclops Tensor Framework
parallel arithmetic on multidimensional arrays
speye.cxx
Go to the documentation of this file.
1 
8 #include <ctf.hpp>
9 using namespace CTF;
10 
11 int speye(int n,
12  int order,
13  World & dw){
14 
15  int shape[order];
16  int size[order];
17  char idx_rep[order+1];
18  idx_rep[order]='\0';
19  char idx_chg[order+1];
20  idx_chg[order]='\0';
21  for (int i=0; i<order; i++){
22  if (i!=order-1)
23  shape[i] = NS;
24  else
25  shape[i] = NS;
26  size[i] = n;
27  idx_rep[i] = 'i';
28  idx_chg[i] = 'i'+i;
29  }
30 
31  // Create distributed sparse matrix
32  Tensor<> A(order, true, size, shape, dw);
33 
34  A[idx_rep] = 1.0;
35 
36 /* if (order == 3){
37  int ns[] = {n,n,n};
38  int sy[] = {SY,SY,NS};
39  Tensor<> AA(3, ns, sy, dw);
40  AA.fill_random(0.0,1.0);
41  A["ijk"] += AA["ijk"];
42  AA["ijk"] += A["ijk"];
43  AA["ijk"] += A["ijk"];
44  }*/
45 
46  /*if (dw.rank == 0)
47  printf("PRINTING\n");
48  A.print();*/
49 
50  double sum1 = A[idx_chg];
51  double sum2 = A[idx_rep];
52 
53  int pass = (fabs(sum1-n)<1.E-9) & (fabs(sum2-n)<1.E-9);
54  MPI_Allreduce(MPI_IN_PLACE, &pass, 1, MPI_INT, MPI_MIN, MPI_COMM_WORLD);
55  if (dw.rank == 0){
56  if (pass)
57  printf("{ A is sparse; A[\"iii...\"]=1; sum(A) = range of i } passed \n");
58  else
59  printf("{ A is sparse; A[\"iii...\"]=1; sum(A) = range of i } failed \n");
60  }
61  return pass;
62 }
63 
64 
65 #ifndef TEST_SUITE
66 char* getCmdOption(char ** begin,
67  char ** end,
68  const std::string & option){
69  char ** itr = std::find(begin, end, option);
70  if (itr != end && ++itr != end){
71  return *itr;
72  }
73  return 0;
74 }
75 
76 
77 int main(int argc, char ** argv){
78  int rank, np, n, pass, order;
79  int const in_num = argc;
80  char ** input_str = argv;
81 
82  MPI_Init(&argc, &argv);
83  MPI_Comm_rank(MPI_COMM_WORLD, &rank);
84  MPI_Comm_size(MPI_COMM_WORLD, &np);
85 
86  if (getCmdOption(input_str, input_str+in_num, "-n")){
87  n = atoi(getCmdOption(input_str, input_str+in_num, "-n"));
88  if (n < 0) n = 7;
89  } else n = 7;
90 
91  if (getCmdOption(input_str, input_str+in_num, "-order")){
92  order = atoi(getCmdOption(input_str, input_str+in_num, "-order"));
93  if (order < 0) order = 3;
94  } else order = 3;
95 
96  {
97  World dw(argc, argv);
98 
99  if (rank == 0){
100  printf("Computing sum of I where I is an identity tensor of order %d and dimension %d stored sparse\n", order, n);
101  }
102  pass = speye(n, order, dw);
103  assert(pass);
104  }
105 
106  MPI_Finalize();
107  return 0;
108 }
114 #endif
char * getCmdOption(char **begin, char **end, const std::string &option)
Definition: speye.cxx:66
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
int rank
rank of local processor
Definition: world.h:24
int main(int argc, char **argv)
Definition: speye.cxx:77
int speye(int n, int order, World &dw)
Definition: speye.cxx:11
Definition: apsp.cxx:17
an instance of a tensor within a CTF world
Definition: tensor.h:74
def np(self)
Definition: core.pyx:315