Cyclops Tensor Framework
parallel arithmetic on multidimensional arrays
test/scalar.cxx
Go to the documentation of this file.
1 /*Copyright (c) 2011, Edgar Solomonik, all rights reserved.*/
9 #include <ctf.hpp>
10 using namespace CTF;
11 
12 int scalar(CTF::World &dw){
13  int rank, num_pes, pass;
14  int64_t np, * indices;
15  double val, * pairs;
16 
17  MPI_Comm_rank(MPI_COMM_WORLD, &rank);
18  MPI_Comm_size(MPI_COMM_WORLD, &num_pes);
19 
20  pass = 1;
21 
22  CTF::Scalar<> A(dw);
23 
24  A.get_local_data(&np,&indices,&pairs);
25  pass -=!(np<=1);
26 
27  if (np>0){
28  pass -=!(indices[0] == 0);
29  assert(pass);
30  pass -=!(std::abs(pairs[0]) < 1.E-9);
31  assert(pass);
32  pairs[0] = 4.2;
33  }
34  A.write(np,indices,pairs);
35  free(indices);
36  delete [] pairs;
37  //A = 4.2;
38  A.get_local_data(&np,&indices,&pairs);
39  pass -= !(np<=1);
40  assert(pass);
41 
42  if (np>0){
43  pass -=(indices[0] != 0);
44  assert(pass);
45  pass -=!(pairs[0]-4.2 < 1.E-9);
46  assert(pass);
47  }
48  free(indices);
49  delete [] pairs;
50  val = A;
51  pass -=!(val-4.2 < 1.E-9);
52  assert(pass);
53 
54  CTF::Scalar<> B(4.3, dw);
55  pass -=!((double)B-4.3 < 1.E-9);
56  assert(pass);
57 
58  B=A;
59  pass -=!((double)B-4.2 < 1.E-9);
60  assert(pass);
61 
62  int n = 7;
63 #if 0 //does not make sense to set AS matrix to scalar
64  CTF::Matrix<> C(n,n,AS,dw);
65 
66  //FIXME: this is nonsense! should result in zero tensor
67  C["ij"]=A[""];
68 
69 
70  val = C["ij"];
71 
72 /* if (C.sym == AS){
73  pass-= !( fabs(C.reduce(CTF::OP_SUM)-n*(n-1)*2.1)<1.E-10);
74  printf("C sum is %lf, abs sum is %lf, C[\"ij\"]=%lf expectd %lf\n",
75  C.reduce(CTF::OP_SUM), C.reduce(CTF::OP_SUMABS), val, n*(n-1)*4.2);
76  } else {
77  printf("C sum is %lf, abs sum is %lf, C[\"ij\"]=%lf expectd %lf\n",
78  C.reduce(CTF::OP_SUM), C.reduce(CTF::OP_SUMABS), val, n*n*4.2);
79  }*/
80  printf("%lf, %lf\n",C.reduce(CTF::OP_SUMABS),n*(n-1)*4.2);
81  pass-= !( fabs(C.reduce(CTF::OP_SUMABS)-n*(n-1)*4.2)<1.E-8);
82  assert(pass);
83 
84  printf("NOW\n");
85 
86  C["ij"]=13.1;
87 
88  printf("NOT NOW\n");
89  C.print();
90 
91  printf("%lf, %lf\n",C.reduce(CTF::OP_SUMABS),n*(n-1)*13.1);
92  pass-= !( fabs(C.reduce(CTF::OP_SUMABS)-n*(n-1)*13.1)<1.E-10);
93  assert(pass);
94 #endif
95  int sizeN4[4] = {n,0,n,n};
96  int shapeN4[4] = {NS,NS,SY,NS};
97  CTF::Matrix<> E(n,n,NS,dw);
98  CTF::Tensor<> D(4, sizeN4, shapeN4, dw);
99 
100  E["ij"]=13.1;
101 
102  int sizeN2[2] = {n,n};
103  CTF::Tensor<> E2(2,sizeN2,shapeN4,dw);
104 
105  E2=13.1;
106  E["ij"]-=E2["ij"];
107  assert(E.norm2() <1.E-6);
108 
109  E["ij"]=13.1;
110  E["ii"]=D["klij"]*E["ki"];
111 
112  pass-= !( fabs(E.reduce(CTF::OP_SUMABS)-0)>1.E-10);
113  assert(pass);
114 
115  E["ij"]=D["klij"]*E["ki"];
116 
117 
118  pass-= !( fabs(E.reduce(CTF::OP_SUMABS)-0)<1.E-10);
119  assert(pass);
120 
121  if (rank == 0){
122  MPI_Reduce(MPI_IN_PLACE, &pass, 1, MPI_INT, MPI_MIN, 0, MPI_COMM_WORLD);
123  if (pass < 1){
124  printf("{ scalar tests } failed\n");
125  } else {
126  printf("{ scalar tests } passed\n");
127  }
128  } else
129  MPI_Reduce(&pass, MPI_IN_PLACE, 1, MPI_INT, MPI_MIN, 0, MPI_COMM_WORLD);
130  if (pass < 0) pass = 0;
131  return pass;
132 
133 }
134 
135 
136 #ifndef TEST_SUITE
137 char* getCmdOption(char ** begin,
138  char ** end,
139  const std::string & option){
140  char ** itr = std::find(begin, end, option);
141  if (itr != end && ++itr != end){
142  return *itr;
143  }
144  return 0;
145 }
146 
147 
148 int main(int argc, char ** argv){
149  int rank, np;
150  MPI_Init(&argc, &argv);
151  MPI_Comm_rank(MPI_COMM_WORLD, &rank);
152  MPI_Comm_size(MPI_COMM_WORLD, &np);
153 
154 
155  {
156  CTF::World dw(MPI_COMM_WORLD, argc, argv);
157  int pass = scalar(dw);
158  assert(pass>0);
159  }
160 
161  MPI_Finalize();
162  return 0;
163 }
164 #endif
165 
Matrix class which encapsulates a 2D tensor.
Definition: matrix.h:18
def rank(self)
Definition: core.pyx:312
char * getCmdOption(char **begin, char **end, const std::string &option)
dtype reduce(OP op)
performs a reduction on the tensor
Definition: tensor.cxx:731
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
int scalar(CTF::World &dw)
Definition: test/scalar.cxx:12
Scalar class which encapsulates a 0D tensor.
Definition: scalar.h:13
int main(int argc, char **argv)
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
def abs(initA)
Definition: core.pyx:5440
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
Definition: common.h:37
Definition: common.h:37
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
def np(self)
Definition: core.pyx:315