Cyclops Tensor Framework
parallel arithmetic on multidimensional arrays
sptensor_sum.cxx
Go to the documentation of this file.
1 
8 #include <ctf.hpp>
9 using namespace CTF;
10 
11 int sptensor_sum(int n,
12  World & dw){
13 
14  int shapeN4[] = {NS,NS,NS,NS};
15  int sizeN4[] = {n,n,n,n};
16 
17  // Creates distributed sparse tensors initialized with zeros
18  Tensor<> A(4, true, sizeN4, shapeN4, dw);
19  Tensor<> B(4, true, sizeN4, shapeN4, dw);
20 
21  if (dw.rank == dw.np/2){
22  int64_t keys_A[4] = {1,2,4,8};
23  double vals_A[4] = {3.2,42.,1.4,-.8};
24 
25  A.write(4, keys_A, vals_A);
26 
27  int64_t keys_B[4] = {2,3};
28  double vals_B[4] = {24.,7.2};
29 
30  B.write(2, keys_B, vals_B);
31  } else {
32  A.write(0, NULL, NULL);
33  B.write(0, NULL, NULL);
34  }
35 
36  //A.print();
37  //B.print();
38 
39  B["abij"] += A["abij"];
40 
41  //B.print();
42 
43  int64_t * new_keys_B;
44  double * new_vals_B;
45  int64_t nloc;
46  B.get_local_data(&nloc, &new_keys_B, &new_vals_B, true);
47  int pass = 1;
48  for (int i=0; i<nloc; i++){
49  switch (new_keys_B[i]){
50  case 1:
51  if (fabs(3.2-new_vals_B[i]) > 1.E-9) pass = 0;
52  break;
53  case 2:
54  if (fabs(66.-new_vals_B[i]) > 1.E-9) pass = 0;
55  break;
56  case 3:
57  if (fabs(7.2-new_vals_B[i]) > 1.E-9) pass = 0;
58  break;
59  case 4:
60  if (fabs(1.4-new_vals_B[i]) > 1.E-9) pass = 0;
61  break;
62  case 8:
63  if (fabs(-.8-new_vals_B[i]) > 1.E-9) pass = 0;
64  break;
65  default:
66  pass = 0;
67  break;
68  }
69  }
70  free(new_keys_B);
71  delete [] new_vals_B;
72 
73  MPI_Allreduce(MPI_IN_PLACE, &pass, 1, MPI_INT, MPI_MIN, MPI_COMM_WORLD);
74  if (dw.rank == 0){
75  if (pass)
76  printf("{ B[\"abij\"] += A[\"abij\"] with sparse, A, B } passed \n");
77  else
78  printf("{ B[\"abij\"] += A[\"abij\"] with sparse, A, B } failed\n");
79  }
80  return pass;
81 }
82 
83 
84 #ifndef TEST_SUITE
85 char* getCmdOption(char ** begin,
86  char ** end,
87  const std::string & option){
88  char ** itr = std::find(begin, end, option);
89  if (itr != end && ++itr != end){
90  return *itr;
91  }
92  return 0;
93 }
94 
95 
96 int main(int argc, char ** argv){
97  int rank, np, n, pass;
98  int const in_num = argc;
99  char ** input_str = argv;
100 
101  MPI_Init(&argc, &argv);
102  MPI_Comm_rank(MPI_COMM_WORLD, &rank);
103  MPI_Comm_size(MPI_COMM_WORLD, &np);
104 
105  if (getCmdOption(input_str, input_str+in_num, "-n")){
106  n = atoi(getCmdOption(input_str, input_str+in_num, "-n"));
107  if (n < 0) n = 7;
108  } else n = 7;
109 
110 
111  {
112  World dw(argc, argv);
113 
114  if (rank == 0){
115  printf("Computing B+=A with B, A sparse\n");
116  }
117  pass = sptensor_sum(n, dw);
118  assert(pass);
119  }
120 
121  MPI_Finalize();
122  return 0;
123 }
129 #endif
def rank(self)
Definition: core.pyx:312
Definition: common.h:37
char * getCmdOption(char **begin, char **end, const std::string &option)
an instance of the CTF library (world) on a MPI communicator
Definition: world.h:19
string
Definition: core.pyx:456
int sptensor_sum(int n, World &dw)
int rank
rank of local processor
Definition: world.h:24
int main(int argc, char **argv)
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
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
int np
number of processors
Definition: world.h:26
def np(self)
Definition: core.pyx:315