Cyclops Tensor Framework
parallel arithmetic on multidimensional arrays
btwn_central_kernels.cxx
Go to the documentation of this file.
1 
2 #include "btwn_central.h"
3 using namespace CTF;
4 
5 
7 void mfunc(mpath a, mpath & b){
8  if (a.w<b.w){ b=a; }
9  else if (b.w==a.w){ b.m+=a.m; }
10 }
11 
12 DEVICE HOST
13 mpath addw(int w, mpath p){ p.w=p.w+w; return p; }
14 
17  //return new Bivar_Function<int,mpath,mpath>(addw);
18 }
19 
20 DEVICE HOST
21 void cfunc(cpath a, cpath & b){
22  if (a.w>b.w){ b.c=a.c; b.w=a.w; }
23  else if (b.w == a.w){ b.c+=a.c; }
24 }
25 
27 cpath subw(int w, cpath p){
28  return cpath(p.w-w , p.m, p.c*p.m);
29 }
30 
33  //return new Bivar_Function<int,mpath,mpath>(addw);
34 }
35 
36 
37 void mpath_red(mpath const * a,
38  mpath * b,
39  int n){
40  #pragma omp parallel for
41  for (int i=0; i<n; i++){
42  if (a[i].w < b[i].w){
43  b[i].w = a[i].w;
44  b[i].m = a[i].m;
45  } else if (a[i].w == b[i].w) b[i].m += a[i].m;
46  }
47 }
48 
49 //(min, +) tropical semiring for mpath structure
51  //struct for mpath with w=mpath weight, h=#hops
52  MPI_Op ompath;
53 
54  MPI_Op_create(
55  [](void * a, void * b, int * n, MPI_Datatype*){
56  mpath_red((mpath*)a, (mpath*)b, *n);
57 /* for (int i=0; i<*n; i++){
58  if (((mpath*)a)[i].w < ((mpath*)b)[i].w){
59  ((mpath*)b)[i] = ((mpath*)a)[i];
60  } else if (((mpath*)a)[i].w == ((mpath*)b)[i].w){
61  ((mpath*)b)[i].m += ((mpath*)a)[i].m;
62  }
63  }*/
64  },
65  1, &ompath);
66 
67  //tropical semiring with hops carried by winner of min
68  Semiring<mpath> p(mpath(INT_MAX/2,0),
69  [](mpath a, mpath b){
70  if (a.w<b.w){ return a; }
71  else if (b.w<a.w){ return b; }
72  else { return mpath(a.w, a.m+b.m); }
73  },
74  ompath,
75  mpath(0,1),
76  [](mpath a, mpath b){ return mpath(a.w+b.w, a.m*b.m); });
77 
78  return p;
79 }
80 
81 
82 void cpath_red(cpath const * a,
83  cpath * b,
84  int n){
85  #pragma omp parallel for
86  for (int i=0; i<n; i++){
87  if (a[i].w > b[i].w){
88  b[i].w = a[i].w;
89  b[i].m = a[i].m;
90  b[i].c = a[i].c;
91  } else if (a[i].w == b[i].w){
92  b[i].c += a[i].c;
93  }
94  }
95 }
96 
97 
98 // min Monoid for cpath structure
100  //struct for cpath with w=cpath weight, h=#hops
101  MPI_Op ocpath;
102 
103  MPI_Op_create(
104  [](void * a, void * b, int * n, MPI_Datatype*){
105  cpath_red((cpath*)a, (cpath*)b, *n);
106 /* for (int i=0; i<*n; i++){
107  if (((cpath*)a)[i].w > ((cpath*)b)[i].w){
108  ((cpath*)b)[i] = ((cpath*)a)[i];
109  } else if (((cpath*)a)[i].w == ((cpath*)b)[i].w){
110  ((cpath*)b)[i].m += ((cpath*)a)[i].m;
111  ((cpath*)b)[i].c += ((cpath*)a)[i].c;
112  }
113  }*/
114  },
115  1, &ocpath);
116 
117  Monoid<cpath> cp(cpath(-INT_MAX/2,1,0.),
118  [](cpath a, cpath b){
119  if (a.w>b.w){ return a; }
120  else if (b.w>a.w){ return b; }
121  else { return cpath(a.w, a.m, a.c+b.c); }
122  }, ocpath);
123 
124 // Bivar_Kernel<cpath,cpath,cpath,cfunc, cnfunc> tmp;
125 // Kernel k = tmp.generate_kernel<cfunc>();
126 
127 // Monoid<cpath> cp(cpath(-INT_MAX/2,1,0.), ker);
128 // Kernel<cpath, cpath, cpath, cfunc> ker;
129 
130  return cp;
131 }
132 
133 
134 
DEVICE HOST void cfunc(cpath a, cpath &b)
#define DEVICE
Definition: btwn_central.h:10
Semiring is a Monoid with an addition multiplicaton function addition must have an identity and be as...
Definition: semiring.h:359
custom bivariate function on two tensors: e.g. C["ij"] = f(A["ik"],B["kj"])
Definition: functions.h:137
Semiring< mpath > get_mpath_semiring()
double c
Definition: btwn_central.h:31
int m
Definition: btwn_central.h:19
float m
Definition: btwn_central.h:32
void mpath_red(mpath const *a, mpath *b, int n)
void cpath_red(cpath const *a, cpath *b, int n)
Bivar_Function< int, cpath, cpath > * get_Brandes_kernel()
DEVICE HOST void mfunc(mpath a, mpath &b)
Definition: apsp.cxx:17
A Monoid is a Set equipped with a binary addition operator &#39;+&#39; or a custom function addition must hav...
Definition: monoid.h:69
Bivar_Function< int, mpath, mpath > * get_Bellman_kernel()
int w
Definition: btwn_central.h:18
DEVICE HOST cpath subw(int w, cpath p)
DEVICE HOST mpath addw(int w, mpath p)
int w
Definition: btwn_central.h:33
Monoid< cpath > get_cpath_monoid()
#define HOST
Definition: btwn_central.h:11