forked from cyclops-community/ctf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscan.cxx
152 lines (120 loc) · 3.17 KB
/
scan.cxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/** \addtogroup examples
* @{
* \defgroup scan scan
* @{
* \brief scan iterative method using gemv and spmv
*/
#include <ctf.hpp>
#include <float.h>
using namespace CTF;
template <typename dtype>
void rec_scan(Tensor<dtype> & V){
if (V.order == 1){
Matrix<dtype> W(2, V.lens[0], V.lens[0], *V.wrld, *V.sr);
dtype mulid = ((dtype*)V.sr->mulid())[0];
W["ij"], [=](dtype & a){ a=mulid; };
int ssym[] = {SH, NS};
int nsym[] = {NS, NS};
Tensor<dtype> W1(W, ssym);
Tensor<dtype> W2(W1, nsym);
V["i"] = W2["ji"]*V["j"];
} else {
Tensor<dtype> V2(V.order-1, V.lens, *V.wrld, *V.sr);
char str[V.order];
for (int i=0; i<V.order; i++){ str[i] = 'a'+i; }
V2[str+1] += V[str];
rec_scan(V2);
Matrix<dtype> W(2, V.lens[V.order-1], V.lens[V.order-1], *V.wrld, *V.sr);
dtype mulid = ((dtype*)V.sr->mulid())[0];
W["ij"], [=](dtype & a){ a=mulid; };
int hsym[] = {SH, NS};
int nsym[] = {NS, NS};
Tensor<dtype> W1(W, hsym);
Tensor<dtype> W2(W1, nsym);
char str2[V.order];
memcpy(str2+1, str+1, V.order-1);
str2[0] = 'a'+V.order;
char strW[2] = {str2[0],'a'};
V[str] = W2[strW]*V[str2];
V[str] += V2[str+1];
}
}
template<typename dtype>
void scan(Vector<dtype> & v, int logn){
int64_t np;
int64_t * inds;
double * data;
int lens[logn];
std::fill(lens, lens+logn, 2);
// represent vector to scan as 2-by-...-by-2 tensor
Tensor<dtype> V(logn, lens, *v.wrld, *v.sr);
v.get_local_data(&np, &inds, &data);
V.write(np, inds, data);
free(inds);
delete [] data;
rec_scan(V);
// put the data from the tensor back into the vector
V.get_local_data(&np, &inds, &data);
v.write(np, inds, data);
free(inds);
delete [] data;
}
int scan_test(int logn,
World & dw){
Vector<> v(1<<logn, dw);
srand48(dw.rank*27);
v.fill_random(0.0, 1.0);
double start_data[1<<logn];
v.read_all(start_data);
scan(v, logn);
double data[1<<logn];
v.read_all(data);
int pass = 1;
for (int i=1; i<1<<logn; i++){
if (std::abs(data[i] - start_data[i-1] - data[i-1]) >= 1.E-9*(1<<logn)) pass = 0;
}
if (dw.rank == 0){
if (pass)
printf("{ scan via tensor contractions } passed \n");
else
printf("{ scan via tensor contractions } failed \n");
}
return pass;
}
#ifndef TEST_SUITE
char* getCmdOption(char ** begin,
char ** end,
const std::string & option){
char ** itr = std::find(begin, end, option);
if (itr != end && ++itr != end){
return *itr;
}
return 0;
}
int main(int argc, char ** argv){
int rank, np, logn, pass;
int const in_num = argc;
char ** input_str = argv;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &np);
if (getCmdOption(input_str, input_str+in_num, "-logn")){
logn = atoi(getCmdOption(input_str, input_str+in_num, "-logn"));
if (logn < 0) logn = 4;
} else logn = 4;
{
World dw(argc, argv);
if (rank == 0){
printf("Running scan on dimension %d vector\n",1<<logn);
}
pass = scan_test(logn, dw);
assert(pass);
}
MPI_Finalize();
return 0;
}
/**
* @}
* @}
*/
#endif