Cyclops Tensor Framework
parallel arithmetic on multidimensional arrays
force_integration.cxx
Go to the documentation of this file.
1 /*Copyright (c) 2011, Edgar Solomonik, all rights reserved.*/
2 
10 #include <ctf.hpp>
11 #include "moldynamics.h"
12 using namespace CTF;
13 
15  World & dw){
16 
19 
20  Vector<particle> P(n, dw, sP);
21  Matrix<force> F (n, n, AS, dw, gF);
22  Matrix<force> F2(n, n, AS, dw, gF);
23 
24  particle * loc_parts;
25  int64_t nloc;
26  int64_t * inds;
27  P.get_local_data(&nloc, &inds, &loc_parts);
28 
29  srand48(dw.rank);
30 
31  for (int64_t i=0; i<nloc; i++){
32  loc_parts[i].dx = drand48();
33  loc_parts[i].dy = drand48();
34  loc_parts[i].coeff = .001*drand48();
35  loc_parts[i].id = 777;
36  }
37  P.write(nloc, inds, loc_parts);
38 
39  force * loc_frcs;
40  int64_t * finds;
41  int64_t nf;
42  F.get_local_data(&nf, &finds, &loc_frcs);
43  for (int64_t i=0; i<nf; i++){
44  loc_frcs[i].fx = drand48();
45  loc_frcs[i].fy = drand48();
46  }
47  F.write(nf, finds, loc_frcs);
48  delete [] loc_frcs;
49  free(finds);
50 
52  p.dx += f.fx*p.coeff;
53  p.dy += f.fy*p.coeff;
54  });
55  F2["ij"] += F["ij"];
56  F2["ij"] += F["ij"];
57 
58  uacc(F2["ij"],P["i"]);
59 
60  particle * loc_parts_new = new particle[nloc];
61  P.read(nloc, inds, loc_parts_new);
62 
63  //check that something changed
64  int pass = 1;
65  if (pass){
66  for (int64_t i=0; i<nloc; i++){
67  if (fabs(loc_parts[i].dx - loc_parts_new[i].dx)<1.E-6 &&
68  fabs(loc_parts[i].dy - loc_parts_new[i].dy)<1.E-6) pass = 0;
69  }
70  }
71  MPI_Allreduce(MPI_IN_PLACE, &pass, 1, MPI_INT, MPI_MIN, MPI_COMM_WORLD);
72  if (!pass && dw.rank == 0){
73  printf("Test incorrect: application of uacc did not modify some value.\n");
74  }
75 
76  //FIXME = must invert tensors over groups via addinv() rather than - or -=, since these use the inverse of the multiplicative id
77  F.addinv();
78  uacc(F["ij"],P["i"]);
79  uacc(F["ij"],P["i"]);
80 
81  P.read(nloc, inds, loc_parts_new);
82  free(inds);
83 
84  if (pass){
85  for (int64_t i=0; i<nloc; i++){
86  if (fabs(loc_parts[i].dx - loc_parts_new[i].dx)>1.E-6 ||
87  fabs(loc_parts[i].dy - loc_parts_new[i].dy)>1.E-6) pass = 0;
88  }
89  }
90  delete [] loc_parts;
91  delete [] loc_parts_new;
92  MPI_Allreduce(MPI_IN_PLACE, &pass, 1, MPI_INT, MPI_MIN, MPI_COMM_WORLD);
93 
94  if (dw.rank == 0){
95  if (pass){
96  printf("{ P[\"i\"] = uacc(F[\"ij\"]) } passed\n");
97  } else {
98  printf("{ P[\"i\"] = uacc(F[\"ij\"]) } failed\n");
99  }
100  }
101 
102 
103  return pass;
104 }
105 
106 
107 #ifndef TEST_SUITE
108 
109 char* getCmdOption(char ** begin,
110  char ** end,
111  const std::string & option){
112  char ** itr = std::find(begin, end, option);
113  if (itr != end && ++itr != end){
114  return *itr;
115  }
116  return 0;
117 }
118 
119 
120 int main(int argc, char ** argv){
121  int rank, np, n;
122  int const in_num = argc;
123  char ** input_str = argv;
124 
125  MPI_Init(&argc, &argv);
126  MPI_Comm_rank(MPI_COMM_WORLD, &rank);
127  MPI_Comm_size(MPI_COMM_WORLD, &np);
128 
129  if (getCmdOption(input_str, input_str+in_num, "-n")){
130  n = atoi(getCmdOption(input_str, input_str+in_num, "-n"));
131  if (n < 0) n = 5;
132  } else n = 5;
133 
134 
135  {
136  World dw(MPI_COMM_WORLD, argc, argv);
137 
138  if (rank == 0){
139  printf("Computing force_integration A_ijkl = f(A_ijkl)\n");
140  }
141  force_integration(n, dw);
142  }
143 
144 
145  MPI_Finalize();
146  return 0;
147 }
148 
154 #endif
Set class defined by a datatype and a min/max function (if it is partially ordered i...
Definition: set.h:280
Matrix class which encapsulates a 2D tensor.
Definition: matrix.h:18
def rank(self)
Definition: core.pyx:312
double dy
Definition: moldynamics.h:38
double fy
Definition: moldynamics.h:7
Vector class which encapsulates a 1D tensor.
Definition: vector.h:14
int main(int argc, char **argv)
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 force_integration(int n, World &dw)
double coeff
Definition: moldynamics.h:39
Group is a Monoid with operator &#39;-&#39; defined special case (parent) of a ring.
Definition: group.h:16
char * getCmdOption(char **begin, char **end, const std::string &option)
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
double dx
Definition: moldynamics.h:37
double fx
Definition: moldynamics.h:6
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