-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathreadwrite_test.cxx
192 lines (148 loc) · 3.79 KB
/
readwrite_test.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*Copyright (c) 2011, Edgar Solomonik, all rights reserved.*/
/** \addtogroup tests
* @{
* \defgroup readwrite_test readwrite_test
* @{
* \brief Tests how writes to diagonals are handled for various tensors
*/
#include <ctf.hpp>
using namespace CTF;
int readwrite_test(int n,
World &dw){
int rank, i, num_pes, pass;
double sum;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &num_pes);
int shape_NS4[] = {NS,NS,NS,NS};
int shape_SY4[] = {SY,NS,SY,NS};
int shape_SH4[] = {SH,NS,SH,NS};
int shape_AS4[] = {SH,NS,SH,NS};
int sizeN4[] = {n,n,n,n};
//* Creates distributed tensors initialized with zeros
Tensor<> A_NS(4, sizeN4, shape_NS4, dw);
Tensor<> A_SY(4, sizeN4, shape_SY4, dw);
Tensor<> A_SH(4, sizeN4, shape_SH4, dw);
Tensor<> A_AS(4, sizeN4, shape_AS4, dw);
std::vector<int64_t> indices;
std::vector<double> vals;
if (rank == 0){
for (i=0; i<n; i++){
// main diagonal
indices.push_back(i+i*n+i*n*n+i*n*n*n);
vals.push_back((double)(i+1));
}
}
pass = 1;
A_NS[indices]+=vals;
sum = A_NS.reduce(CTF::OP_SUM);
if (fabs(sum-n*(n+1.)/2.)>1.E-10){
pass = 0;
#ifndef TEST_SUITE
if (rank == 0){
printf("Nonsymmetric diagonal write failed!\n");
}
#endif
}
A_SY[indices]+=vals;
sum = A_SY.reduce(CTF::OP_SUM);
if (fabs(sum-n*(n+1.)/2.)>1.E-10){
pass = 0;
#ifndef TEST_SUITE
if (rank == 0){
printf("Symmetric diagonal write failed!, err - %lf\n",sum-n*(n+1.)/2.);
}
#endif
}
A_AS[indices]+=vals;
sum = A_AS.reduce(CTF::OP_SUM);
if (sum != 0.0){
pass = 0;
#ifndef TEST_SUITE
if (rank == 0){
printf("Asymmetric diagonal write failed!\n");
}
#endif
}
A_SH[indices]+=vals;
sum = A_SH.reduce(CTF::OP_SUM);
if (sum != 0.0){
pass = 0;
#ifndef TEST_SUITE
if (rank == 0){
printf("Symmetric-hollow diagonal write failed!\n");
}
#endif
}
for (i=0; i<(int)vals.size(); i++){
vals[i] = sqrt(vals[i]);
}
A_SY[indices]=vals;
sum = 0.0;
A_NS["ijkl"]=0.0;
A_NS["ijkl"]=A_SY["ijkl"];
sum += A_NS["ijkl"]*A_NS["ijkl"];
if (fabs(sum-n*(n+1.)/2.)>1.E-10){
pass = 0;
#ifndef TEST_SUITE
if (rank == 0){
printf("Nonsymmetric self contraction failed!, err = %lf\n",sum-n*(n+1.)/2.);
}
#endif
}
Tensor<> B_SY(4, sizeN4, shape_SY4, dw);
/* B_SY["ijkl"]=A_SY["ijkl"];
sum = A_SY["ijkl"]*B_SY["ijkl"];*/
sum = A_SY["ijkl"]*A_SY["ijkl"];
if (fabs(sum-n*(n+1.)/2.)>1.E-10){
pass = 0;
#ifndef TEST_SUITE
if (rank == 0){
printf("Symmetric self contraction failed!, err = %lf\n",sum-n*(n+1.)/2.);
}
#endif
}
if (rank == 0){
MPI_Reduce(MPI_IN_PLACE, &pass, 1, MPI_INT, MPI_MIN, 0, MPI_COMM_WORLD);
if (pass){
printf("{ diagonal write test } passed\n");
} else {
printf("{ diagonal write test } failed\n");
}
} else
MPI_Reduce(&pass, MPI_IN_PLACE, 1, MPI_INT, MPI_MIN, 0, MPI_COMM_WORLD);
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, n;
int 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, "-n")){
n = atoi(getCmdOption(input_str, input_str+in_num, "-n"));
if (n < 0) n = 7;
} else n = 7;
{
World dw(MPI_COMM_WORLD, argc, argv);
if (rank == 0) printf("Testing reading and writing functions in CTF\n");
readwrite_test(n, dw);
}
MPI_Finalize();
return 0;
}
/**
* @}
* @}
*/
#endif