test/rw.c
Go to the documentation of this file.
1 
10 #include <GKlib.h>
11 
12 /*************************************************************************/
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 /*************************************************************************/
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 /*************************************************************************/
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 /*************************************************************************/
94 /*************************************************************************/
96 void print_final_info(params_t *params);
97 params_t *parse_cmdline(int argc, char *argv[]);
98 
99 
100 /*************************************************************************/
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 /*************************************************************************/
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 /*************************************************************************/
226 /*************************************************************************/
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 /*************************************************************************/
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 
The structure that stores the information about the command-line options.
Definition: gk_getopt.h:28
Definition: fis.c:15
FILE * gk_fopen(char *, char *, const char *)
Definition: GKlib/io.c:24
void errexit(char *f_str,...)
Definition: error.c:54
float eps
Definition: gkgraph.c:18
int gk_optind
Index in ARGV of the next element to be scanned.
Definition: getopt.c:68
static char helpstr[][100]
Definition: test/rw.c:53
int32_t * rowind
Definition: gk_struct.h:75
#define GK_CSR_FMT_METIS
Definition: gk_defs.h:63
void print_final_info(params_t *params)
Definition: test/rw.c:227
size_t gk_GetMaxMemoryUsed()
Definition: memory.c:246
#define CMD_HELP
Definition: test/rw.c:33
int gk_fexists(char *fname)
Definition: fs.c:21
Scalar Scalar * c
Definition: benchVecAdd.cpp:17
void print_init_info(params_t *params, gk_csr_t *mat)
Definition: test/rw.c:206
#define CMD_NTVS
Definition: test/rw.c:32
#define CMD_PPR
Definition: test/rw.c:31
int ntvs
Definition: test/rw.c:17
Real fabs(const Real &a)
static struct gk_option long_options[]
Definition: test/rw.c:39
static const SmartProjectionParams params
float lamda
Definition: gkgraph.c:19
static char shorthelpstr[][100]
Definition: test/rw.c:83
size_t gk_GetCurMemoryUsed()
Definition: memory.c:233
int niter
Definition: gkgraph.c:17
Array< double, 1, 3 > e(1./3., 0.5, 2.)
params_t * parse_cmdline(int argc, char *argv[])
Definition: test/rw.c:240
char * outfile
Definition: gkgraph.c:22
#define NULL
Definition: ccolamd.c:609
char * gk_strdup(char *orgstr)
Duplicates a string.
Definition: string.c:372
char * gk_optarg
For communication arguments to the caller.
Definition: getopt.c:56
void gk_csr_Free(gk_csr_t **mat)
Definition: csr.c:48
void * gk_malloc(size_t nbytes, char *msg)
Definition: memory.c:140
void gk_free(void **ptr1,...)
Definition: memory.c:202
ssize_t * rowptr
Definition: gk_struct.h:74
#define RandomInRange(u)
Definition: gk_macros.h:24
int gk_rw_PageRank(gk_csr_t *mat, float lamda, float eps, int max_niter, float *pr)
Definition: rw.c:29
int ppr
Definition: test/rw.c:18
#define CMD_LAMDA
Definition: test/rw.c:30
int32_t nrows
Definition: gk_struct.h:73
int32_t ncols
Definition: gk_struct.h:73
char * infile
Definition: gkgraph.c:21
#define CMD_EPS
Definition: test/rw.c:29
int main(int argc, char *argv[])
Definition: test/rw.c:103
void gk_fclose(FILE *)
Definition: GKlib/io.c:44
int gk_getopt_long_only(int argc, char **argv, char *options, struct gk_option *long_options, int *opt_index)
Parse command-line arguments with only long options.
Definition: getopt.c:850
std::ptrdiff_t j
#define LTERM
Definition: gk_defs.h:14
gk_csr_t * gk_csr_Read(char *filename, int format, int readvals, int numbering)
Definition: csr.c:349
#define CMD_NITER
Definition: test/rw.c:28


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:35:35