Newer
Older
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*
This is a test of the l*(l - 1)-metric partitioning done by Mondriaan + PaToH.
The matrix
test_lambdalambda1.mtx
1 1 0 0 0 0 0
1 1 0 0 0 0 0
0 1 1 1 1 1 1
partitioned into 4 parts (ONEDIMCOL) with imbalance 1/7, has optimal partitionings
0 0 1 2 2 3 3 (volumes 3 and 12 for (l - 1) and l*(l - 1)) and
0 1 1 2 2 3 3 (volumes 4 and 10).
test_lambdalambda2.mtx
1 0 0 0 1 0 1 0 0 1
0 0 1 0 1 0 1 0 0 1
1 0 0 1 1 1 0 0 0 1
0 1 1 0 0 1 1 1 1 1
0 1 0 0 1 0 0 1 0 1
0 0 0 1 1 1 0 0 0 0
1 0 0 0 1 0 1 0 0 0
0 1 0 0 1 0 1 0 1 1
1 0 1 0 1 1 0 0 1 0
1 0 1 0 1 0 0 0 1 1
1 0 0 1 1 0 0 0 0 1
0 0 0 0 0 1 1 0 0 0
1 0 1 0 1 0 1 1 1 1
1 0 1 1 0 0 0 1 1 0
0 1 1 0 1 0 0 1 0 1
1 0 0 1 0 1 1 0 1 1
1 0 0 1 0 1 1 0 0 0
1 1 0 0 0 1 0 0 1 1
0 1 1 0 0 0 0 0 0 1
1 0 0 1 1 0 0 0 1 1
l - 1 : 43 (154) -- 4 2 1 4 0 3 3 2 1 0
l*(l - 1): 150 (44) -- 1 3 2 4 0 4 3 2 1 0
test_lambdalambda3.mtx
0 0 1 1 1 1 1 0
1 0 1 1 1 0 1 0
1 0 1 1 0 1 1 1
1 0 1 1 1 0 0 1
0 0 0 0 0 1 0 1
0 1 0 1 0 0 1 1
1 1 0 0 0 0 0 0
1 1 0 0 0 0 0 1
l - 1 : 13 (44) -- 3 3 2 1 2 0 1 0
l*(l - 1): 42 (14) -- 3 0 3 2 2 1 1 0
Mondriaan should find these optima.
*/
#include "Mondriaan.h"
struct opts Options;
#define TRYMAT 2
void ReadMatrix(struct sparsematrix *A) {
long i;
#if TRYMAT==0
FILE *file = fopen("test_lambdalambda1.mtx", "r");
Options.P = 4;
Options.eps = 1.0/6.9;
#elif TRYMAT==1
FILE *file = fopen("test_lambdalambda2.mtx", "r");
Options.P = 5;
Options.eps = 0.1;
#elif TRYMAT==2
FILE *file = fopen("test_lambdalambda3.mtx", "r");
Options.P = 4;
Options.eps = 0.1;
#endif
/* Read file from disk. */
if (file == NULL || !MMReadSparseMatrix(file, A)) {
printf("Error\n");
exit(1);
}
fclose(file);
/* Set weights. */
A->MMTypeCode[0] = 'W';
A->ColWeights = (long *)malloc(A->n*sizeof(long));
if (A->ColWeights == NULL) {
printf("Error\n");
exit(1);
}
A->NrColWeights = A->n;
for (i = 0; i < A->n; i++) A->ColWeights[i] = 1;
/* Create processor array. */
A->NrProcs = Options.P;
A->Pstart = (long *)malloc((A->NrProcs + 1)*sizeof(long));
if (A->Pstart == NULL) {
printf("Error\n");
exit(1);
}
A->Pstart[0] = 0;
for (i = 1; i <= A->NrProcs; i++) A->Pstart[i] = A->NrNzElts;
}
void CheckVol(long *_Vol1, long *_Vol2, const long *Partition, const struct sparsematrix *A) {
long *PartMark;
long LargestPart, Vol1, Vol2;
long i;
double epsilon;
PartMark = (long *)malloc(A->NrProcs*sizeof(long));
if (PartMark == NULL) {
printf("Error\n");
exit(1);
}
/* Check imbalance. */
for (i = 0; i < A->NrProcs; i++) PartMark[i] = 0;
for (i = 0; i < A->n; i++) PartMark[Partition[i]]++;
LargestPart = 0;
for (i = 0; i < A->NrProcs; i++) LargestPart = (LargestPart < PartMark[i] ? PartMark[i] : LargestPart);
epsilon = (double)(A->NrProcs*LargestPart - A->n)/(double)A->n;
if (epsilon > Options.eps) {
printf("Error\n");
exit(1);
}
/* Calculate volumes. */
Vol1 = 0;
Vol2 = 0;
for (i = 0; i < A->m; i++) {
Vol1 += A->RowLambda[i] - 1;
Vol2 += A->RowLambda[i]*(A->RowLambda[i] - 1);
}
*_Vol1 = Vol1;
*_Vol2 = Vol2;
/* Free memory. */
free(PartMark);
}
int main(int argc, char **argv) {
struct sparsematrix A;
long ComVol = -1;
long *Partition;
long Vol11 = -1, Vol12 = -1, Vol21 = -1, Vol22 = -1;
long i;
printf("Test LambdaLambdaMinusOneMetric: ");
#ifndef USE_PATOH
printf("Untested (no PaToH)\n");
exit(0);
#endif
/* Initialise relevant options */
if (!SetDefaultOptions(&Options)) {
printf("Error\n");
exit(1);
}
Options.P = -1; /* set by ReadMatrix() */
Options.eps = -1.0;
Options.Seed = 12345;
Options.SplitStrategy = OneDimCol;
Options.Partitioner = PartPaToH;
Options.SymmetricMatrix_UseSingleEntry = SingleEntNo;
Options.SquareMatrix_DistributeVectorsEqual = EqVecNo;
Options.Metric = MetricLambda;
if (!ApplyOptions(&Options)) {
printf("Error\n");
exit(1);
}
/* Try the (l - 1)-metric. */
Options.Metric = MetricLambda;
ReadMatrix(&A);
/* Allocate partitioning. */
Partition = (long *)malloc(A.n*sizeof(long));
if (Partition == NULL) {
printf("Error\n");
exit(1);
}
for (i = 0; i < A.n; i++) Partition[i] = 0;
if (!DistributeMatrixMondriaan(&A, Options.P, Options.eps, &Options, NULL)) {
printf("Error\n");
exit(1);
}
ComVol = DistributeVec(&A, Partition, ROW, &Options);
if (ComVol < 0) {
printf("Error\n");
exit(1);
}
CheckVol(&Vol11, &Vol12, Partition, &A);
/* Free memory. */
MMDeleteSparseMatrix(&A);
/* Try the l*(l - 1)-metric. */
Options.Metric = MetricLambdaLambdaMinusOne;
ReadMatrix(&A);
for (i = 0; i < A.n; i++) Partition[i] = 0;
if (!DistributeMatrixMondriaan(&A, Options.P, Options.eps, &Options, NULL)) {
printf("Error\n");
exit(1);
}
ComVol = DistributeVec(&A, Partition, ROW, &Options);
if (ComVol < 0) {
printf("Error\n");
exit(1);
}
CheckVol(&Vol21, &Vol22, Partition, &A);
/*
printf("Volumes | (l - 1) | l*(l - 1) |\n");
printf("----------+-----------+-----------+\n");
printf("(l - 1) | % 9ld | % 9ld |\n", Vol11, Vol12);
printf("l*(l - 1) | % 9ld | % 9ld |\n", Vol21, Vol22);
printf("----------+-----------+-----------+\n");
*/
/* Free memory. */
free(Partition);
MMDeleteSparseMatrix(&A);
if (Vol11 >= Vol21 || Vol22 >= Vol12 || Vol11 < 0 || Vol12 < 0 || Vol21 < 0 || Vol22 < 0) {
printf("Error\n");
exit(1);
}
printf("OK\n");
exit(0);
}