Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ElmerCSC
GitHub Repository: ElmerCSC/elmerfem
Path: blob/devel/elmergrid/src/metis-5.1.0/GKlib/test/rw.c
3206 views
1
/*!
2
\file
3
\brief A simple frequent itemset discovery program to test GKlib's routines
4
5
\date 6/12/2008
6
\author George
7
\version \verbatim $Id: rw.c 11387 2012-01-21 23:36:23Z karypis $ \endverbatim
8
*/
9
10
#include <GKlib.h>
11
12
/*************************************************************************/
13
/*! Data structures for the code */
14
/*************************************************************************/
15
typedef struct {
16
int niter;
17
int ntvs;
18
int ppr;
19
float eps;
20
float lamda;
21
char *infile;
22
char *outfile;
23
} params_t;
24
25
/*************************************************************************/
26
/*! Constants */
27
/*************************************************************************/
28
#define CMD_NITER 1
29
#define CMD_EPS 2
30
#define CMD_LAMDA 3
31
#define CMD_PPR 4
32
#define CMD_NTVS 5
33
#define CMD_HELP 10
34
35
36
/*************************************************************************/
37
/*! Local variables */
38
/*************************************************************************/
39
static struct gk_option long_options[] = {
40
{"niter", 1, 0, CMD_NITER},
41
{"lamda", 1, 0, CMD_LAMDA},
42
{"eps", 1, 0, CMD_EPS},
43
{"ppr", 1, 0, CMD_PPR},
44
{"ntvs", 1, 0, CMD_NTVS},
45
{"help", 0, 0, CMD_HELP},
46
{0, 0, 0, 0}
47
};
48
49
50
/*-------------------------------------------------------------------*/
51
/* Mini help */
52
/*-------------------------------------------------------------------*/
53
static char helpstr[][100] = {
54
" ",
55
"Usage: rw [options] <graph-file> <out-file>",
56
" ",
57
" Required parameters",
58
" graph-file",
59
" The name of the file storing the transactions. The file is in ",
60
" Metis' graph format.",
61
" ",
62
" Optional parameters",
63
" -niter=int",
64
" Specifies the maximum number of iterations. [default: 100]",
65
" ",
66
" -lamda=float",
67
" Specifies the follow-the-adjacent-links probability. [default: 0.80]",
68
" ",
69
" -eps=float",
70
" Specifies the error tollerance. [default: 1e-10]",
71
" ",
72
" -ppr=int",
73
" Specifies the source of the personalized PR. [default: -1]",
74
" ",
75
" -ntvs=int",
76
" Specifies the number of test-vectors to compute. [default: -1]",
77
" ",
78
" -help",
79
" Prints this message.",
80
""
81
};
82
83
static char shorthelpstr[][100] = {
84
" ",
85
" Usage: rw [options] <graph-file> <out-file>",
86
" use 'rw -help' for a summary of the options.",
87
""
88
};
89
90
91
92
/*************************************************************************/
93
/*! Function prototypes */
94
/*************************************************************************/
95
void print_init_info(params_t *params, gk_csr_t *mat);
96
void print_final_info(params_t *params);
97
params_t *parse_cmdline(int argc, char *argv[]);
98
99
100
/*************************************************************************/
101
/*! the entry point */
102
/**************************************************************************/
103
int main(int argc, char *argv[])
104
{
105
ssize_t i, j, niter;
106
params_t *params;
107
gk_csr_t *mat;
108
FILE *fpout;
109
110
/* get command-line options */
111
params = parse_cmdline(argc, argv);
112
113
/* read the data */
114
mat = gk_csr_Read(params->infile, GK_CSR_FMT_METIS, 1, 1);
115
116
/* display some basic stats */
117
print_init_info(params, mat);
118
119
120
121
if (params->ntvs != -1) {
122
/* compute the pr for different randomly generated restart-distribution vectors */
123
float **prs;
124
125
prs = gk_fAllocMatrix(params->ntvs, mat->nrows, 0.0, "main: prs");
126
127
/* generate the random restart vectors */
128
for (j=0; j<params->ntvs; j++) {
129
for (i=0; i<mat->nrows; i++)
130
prs[j][i] = RandomInRange(931);
131
gk_fscale(mat->nrows, 1.0/gk_fsum(mat->nrows, prs[j], 1), prs[j], 1);
132
133
niter = gk_rw_PageRank(mat, params->lamda, params->eps, params->niter, prs[j]);
134
printf("tvs#: %zd; niters: %zd\n", j, niter);
135
}
136
137
/* output the computed pr scores */
138
fpout = gk_fopen(params->outfile, "w", "main: outfile");
139
for (i=0; i<mat->nrows; i++) {
140
for (j=0; j<params->ntvs; j++)
141
fprintf(fpout, "%.4e ", prs[j][i]);
142
fprintf(fpout, "\n");
143
}
144
gk_fclose(fpout);
145
146
gk_fFreeMatrix(&prs, params->ntvs, mat->nrows);
147
}
148
else if (params->ppr != -1) {
149
/* compute the personalized pr from the specified vertex */
150
float *pr;
151
152
pr = gk_fsmalloc(mat->nrows, 0.0, "main: pr");
153
154
pr[params->ppr-1] = 1.0;
155
156
niter = gk_rw_PageRank(mat, params->lamda, params->eps, params->niter, pr);
157
printf("ppr: %d; niters: %zd\n", params->ppr, niter);
158
159
/* output the computed pr scores */
160
fpout = gk_fopen(params->outfile, "w", "main: outfile");
161
for (i=0; i<mat->nrows; i++)
162
fprintf(fpout, "%.4e\n", pr[i]);
163
gk_fclose(fpout);
164
165
gk_free((void **)&pr, LTERM);
166
}
167
else {
168
/* compute the standard pr */
169
int jmax;
170
float diff, maxdiff;
171
float *pr;
172
173
pr = gk_fsmalloc(mat->nrows, 1.0/mat->nrows, "main: pr");
174
175
niter = gk_rw_PageRank(mat, params->lamda, params->eps, params->niter, pr);
176
printf("pr; niters: %zd\n", niter);
177
178
/* output the computed pr scores */
179
fpout = gk_fopen(params->outfile, "w", "main: outfile");
180
for (i=0; i<mat->nrows; i++) {
181
for (jmax=i, maxdiff=0.0, j=mat->rowptr[i]; j<mat->rowptr[i+1]; j++) {
182
if ((diff = fabs(pr[i]-pr[mat->rowind[j]])) > maxdiff) {
183
maxdiff = diff;
184
jmax = mat->rowind[j];
185
}
186
}
187
fprintf(fpout, "%.4e %10zd %.4e %10d\n", pr[i],
188
mat->rowptr[i+1]-mat->rowptr[i], maxdiff, jmax+1);
189
}
190
gk_fclose(fpout);
191
192
gk_free((void **)&pr, LTERM);
193
}
194
195
gk_csr_Free(&mat);
196
197
/* display some final stats */
198
print_final_info(params);
199
}
200
201
202
203
/*************************************************************************/
204
/*! This function prints run parameters */
205
/*************************************************************************/
206
void print_init_info(params_t *params, gk_csr_t *mat)
207
{
208
printf("*******************************************************************************\n");
209
printf(" fis\n\n");
210
printf("Matrix Information ---------------------------------------------------------\n");
211
printf(" input file=%s, [%d, %d, %zd]\n",
212
params->infile, mat->nrows, mat->ncols, mat->rowptr[mat->nrows]);
213
214
printf("\n");
215
printf("Options --------------------------------------------------------------------\n");
216
printf(" niter=%d, ntvs=%d, ppr=%d, lamda=%f, eps=%e\n",
217
params->niter, params->ntvs, params->ppr, params->lamda, params->eps);
218
219
printf("\n");
220
printf("Performing random walks... ----------------------------------------------\n");
221
}
222
223
224
/*************************************************************************/
225
/*! This function prints final statistics */
226
/*************************************************************************/
227
void print_final_info(params_t *params)
228
{
229
printf("\n");
230
printf("Memory Usage Information -----------------------------------------------------\n");
231
printf(" Maximum memory used: %10zd bytes\n", (ssize_t) gk_GetMaxMemoryUsed());
232
printf(" Current memory used: %10zd bytes\n", (ssize_t) gk_GetCurMemoryUsed());
233
printf("********************************************************************************\n");
234
}
235
236
237
/*************************************************************************/
238
/*! This is the entry point of the command-line argument parser */
239
/*************************************************************************/
240
params_t *parse_cmdline(int argc, char *argv[])
241
{
242
int i;
243
int c, option_index;
244
params_t *params;
245
246
params = (params_t *)gk_malloc(sizeof(params_t), "parse_cmdline: params");
247
248
/* initialize the params data structure */
249
params->niter = 100;
250
params->ppr = -1;
251
params->ntvs = -1;
252
params->eps = 1e-10;
253
params->lamda = 0.80;
254
params->infile = NULL;
255
params->outfile = NULL;
256
257
258
/* Parse the command line arguments */
259
while ((c = gk_getopt_long_only(argc, argv, "", long_options, &option_index)) != -1) {
260
switch (c) {
261
case CMD_NITER:
262
if (gk_optarg) params->niter = atoi(gk_optarg);
263
break;
264
case CMD_NTVS:
265
if (gk_optarg) params->ntvs = atoi(gk_optarg);
266
break;
267
case CMD_PPR:
268
if (gk_optarg) params->ppr = atoi(gk_optarg);
269
break;
270
case CMD_EPS:
271
if (gk_optarg) params->eps = atof(gk_optarg);
272
break;
273
case CMD_LAMDA:
274
if (gk_optarg) params->lamda = atof(gk_optarg);
275
break;
276
277
case CMD_HELP:
278
for (i=0; strlen(helpstr[i]) > 0; i++)
279
printf("%s\n", helpstr[i]);
280
exit(0);
281
break;
282
case '?':
283
default:
284
printf("Illegal command-line option(s)\nUse %s -help for a summary of the options.\n", argv[0]);
285
exit(0);
286
}
287
}
288
289
if (argc-gk_optind != 2) {
290
printf("Unrecognized parameters.");
291
for (i=0; strlen(shorthelpstr[i]) > 0; i++)
292
printf("%s\n", shorthelpstr[i]);
293
exit(0);
294
}
295
296
params->infile = gk_strdup(argv[gk_optind++]);
297
params->outfile = gk_strdup(argv[gk_optind++]);
298
299
if (!gk_fexists(params->infile))
300
errexit("input file %s does not exist.\n", params->infile);
301
302
if (params->ppr != -1 && params->ntvs != -1)
303
errexit("Only one of the -ppr and -ntvs options can be specified.\n");
304
305
return params;
306
}
307
308
309