Cyclops Tensor Framework
parallel arithmetic on multidimensional arrays
bench_contraction.cxx
Go to the documentation of this file.
1 
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <string>
13 #include <math.h>
14 #include <assert.h>
15 #include <algorithm>
16 #include <ctf.hpp>
17 #include "../src/shared/util.h"
18 
20  int niter,
21  char const * iA,
22  char const * iB,
23  char const * iC,
24  CTF_World &dw){
25 
26  int rank, i, num_pes;
27 
28  MPI_Comm_rank(MPI_COMM_WORLD, &rank);
29  MPI_Comm_size(MPI_COMM_WORLD, &num_pes);
30 
31  int order_A, order_B, order_C;
32  order_A = strlen(iA);
33  order_B = strlen(iB);
34  order_C = strlen(iC);
35 
36  int NS_A[order_A];
37  int NS_B[order_B];
38  int NS_C[order_C];
39  int n_A[order_A];
40  int n_B[order_B];
41  int n_C[order_C];
42 
43  for (i=0; i<order_A; i++){
44  n_A[i] = n;
45  NS_A[i] = NS;
46  }
47  for (i=0; i<order_B; i++){
48  n_B[i] = n;
49  NS_B[i] = NS;
50  }
51  for (i=0; i<order_C; i++){
52  n_C[i] = n;
53  NS_C[i] = NS;
54  }
55 
56 
57  //* Creates distributed tensors initialized with zeros
58  CTF_Tensor A(order_A, n_A, NS_A, dw, "A", 1);
59  CTF_Tensor B(order_B, n_B, NS_B, dw, "B", 1);
60  CTF_Tensor C(order_C, n_C, NS_C, dw, "C", 1);
61 
62  double st_time = MPI_Wtime();
63 
64  for (i=0; i<niter; i++){
65  C[iC] += A[iA]*B[iB];
66  }
67 
68  double end_time = MPI_Wtime();
69 
70  if (rank == 0)
71  printf("Performed %d iterations of C[\"%s\"] += A[\"%s\"]*B[\"%s\"] in %lf sec/iter\n",
72  niter, iC, iA, iB, (end_time-st_time)/niter);
73 
74  return 1;
75 }
76 
77 char* getCmdOption(char ** begin,
78  char ** end,
79  const std::string & option){
80  char ** itr = std::find(begin, end, option);
81  if (itr != end && ++itr != end){
82  return *itr;
83  }
84  return 0;
85 }
86 
87 
88 int main(int argc, char ** argv){
89  int rank, np, niter, n;
90  int const in_num = argc;
91  char ** input_str = argv;
92  char const * A;
93  char const * B;
94  char const * C;
95 
96  MPI_Init(&argc, &argv);
97  MPI_Comm_rank(MPI_COMM_WORLD, &rank);
98  MPI_Comm_size(MPI_COMM_WORLD, &np);
99 
100  if (getCmdOption(input_str, input_str+in_num, "-n")){
101  n = atoi(getCmdOption(input_str, input_str+in_num, "-n"));
102  if (n < 0) n = 4;
103  } else n = 4;
104 
105  if (getCmdOption(input_str, input_str+in_num, "-niter")){
106  niter = atoi(getCmdOption(input_str, input_str+in_num, "-niter"));
107  if (niter < 0) niter = 3;
108  } else niter = 3;
109 
110  if (getCmdOption(input_str, input_str+in_num, "-A")){
111  A = getCmdOption(input_str, input_str+in_num, "-A");
112  } else A = "ik";
113  if (getCmdOption(input_str, input_str+in_num, "-B")){
114  B = getCmdOption(input_str, input_str+in_num, "-B");
115  } else B = "kj";
116  if (getCmdOption(input_str, input_str+in_num, "-C")){
117  C = getCmdOption(input_str, input_str+in_num, "-C");
118  } else C = "ij";
119 
120 
121 
122  {
123  CTF_World dw(argc, argv);
124  int pass = bench_contraction(n, niter, A, B, C, dw);
125  assert(pass);
126  }
127 
128 
129  MPI_Finalize();
130  return 0;
131 }
char * getCmdOption(char **begin, char **end, const std::string &option)
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 main(int argc, char **argv)
an instance of a tensor within a CTF world
Definition: tensor.h:74
int bench_contraction(int n, int niter, char const *iA, char const *iB, char const *iC, CTF_World &dw)
def np(self)
Definition: core.pyx:315