00001
00010 #include <GKlib.h>
00011
00012
00014
00015 typedef struct {
00016 int niter;
00017 int ntvs;
00018 int ppr;
00019 float eps;
00020 float lamda;
00021 char *infile;
00022 char *outfile;
00023 } params_t;
00024
00025
00027
00028 #define CMD_NITER 1
00029 #define CMD_EPS 2
00030 #define CMD_LAMDA 3
00031 #define CMD_PPR 4
00032 #define CMD_NTVS 5
00033 #define CMD_HELP 10
00034
00035
00036
00038
00039 static struct gk_option long_options[] = {
00040 {"niter", 1, 0, CMD_NITER},
00041 {"lamda", 1, 0, CMD_LAMDA},
00042 {"eps", 1, 0, CMD_EPS},
00043 {"ppr", 1, 0, CMD_PPR},
00044 {"ntvs", 1, 0, CMD_NTVS},
00045 {"help", 0, 0, CMD_HELP},
00046 {0, 0, 0, 0}
00047 };
00048
00049
00050
00051
00052
00053 static char helpstr[][100] = {
00054 " ",
00055 "Usage: rw [options] <graph-file> <out-file>",
00056 " ",
00057 " Required parameters",
00058 " graph-file",
00059 " The name of the file storing the transactions. The file is in ",
00060 " Metis' graph format.",
00061 " ",
00062 " Optional parameters",
00063 " -niter=int",
00064 " Specifies the maximum number of iterations. [default: 100]",
00065 " ",
00066 " -lamda=float",
00067 " Specifies the follow-the-adjacent-links probability. [default: 0.80]",
00068 " ",
00069 " -eps=float",
00070 " Specifies the error tollerance. [default: 1e-10]",
00071 " ",
00072 " -ppr=int",
00073 " Specifies the source of the personalized PR. [default: -1]",
00074 " ",
00075 " -ntvs=int",
00076 " Specifies the number of test-vectors to compute. [default: -1]",
00077 " ",
00078 " -help",
00079 " Prints this message.",
00080 ""
00081 };
00082
00083 static char shorthelpstr[][100] = {
00084 " ",
00085 " Usage: rw [options] <graph-file> <out-file>",
00086 " use 'rw -help' for a summary of the options.",
00087 ""
00088 };
00089
00090
00091
00092
00094
00095 void print_init_info(params_t *params, gk_csr_t *mat);
00096 void print_final_info(params_t *params);
00097 params_t *parse_cmdline(int argc, char *argv[]);
00098
00099
00100
00102
00103 int main(int argc, char *argv[])
00104 {
00105 ssize_t i, j, niter;
00106 params_t *params;
00107 gk_csr_t *mat;
00108 FILE *fpout;
00109
00110
00111 params = parse_cmdline(argc, argv);
00112
00113
00114 mat = gk_csr_Read(params->infile, GK_CSR_FMT_METIS, 1, 1);
00115
00116
00117 print_init_info(params, mat);
00118
00119
00120
00121 if (params->ntvs != -1) {
00122
00123 float **prs;
00124
00125 prs = gk_fAllocMatrix(params->ntvs, mat->nrows, 0.0, "main: prs");
00126
00127
00128 for (j=0; j<params->ntvs; j++) {
00129 for (i=0; i<mat->nrows; i++)
00130 prs[j][i] = RandomInRange(931);
00131 gk_fscale(mat->nrows, 1.0/gk_fsum(mat->nrows, prs[j], 1), prs[j], 1);
00132
00133 niter = gk_rw_PageRank(mat, params->lamda, params->eps, params->niter, prs[j]);
00134 printf("tvs#: %zd; niters: %zd\n", j, niter);
00135 }
00136
00137
00138 fpout = gk_fopen(params->outfile, "w", "main: outfile");
00139 for (i=0; i<mat->nrows; i++) {
00140 for (j=0; j<params->ntvs; j++)
00141 fprintf(fpout, "%.4e ", prs[j][i]);
00142 fprintf(fpout, "\n");
00143 }
00144 gk_fclose(fpout);
00145
00146 gk_fFreeMatrix(&prs, params->ntvs, mat->nrows);
00147 }
00148 else if (params->ppr != -1) {
00149
00150 float *pr;
00151
00152 pr = gk_fsmalloc(mat->nrows, 0.0, "main: pr");
00153
00154 pr[params->ppr-1] = 1.0;
00155
00156 niter = gk_rw_PageRank(mat, params->lamda, params->eps, params->niter, pr);
00157 printf("ppr: %d; niters: %zd\n", params->ppr, niter);
00158
00159
00160 fpout = gk_fopen(params->outfile, "w", "main: outfile");
00161 for (i=0; i<mat->nrows; i++)
00162 fprintf(fpout, "%.4e\n", pr[i]);
00163 gk_fclose(fpout);
00164
00165 gk_free((void **)&pr, LTERM);
00166 }
00167 else {
00168
00169 int jmax;
00170 float diff, maxdiff;
00171 float *pr;
00172
00173 pr = gk_fsmalloc(mat->nrows, 1.0/mat->nrows, "main: pr");
00174
00175 niter = gk_rw_PageRank(mat, params->lamda, params->eps, params->niter, pr);
00176 printf("pr; niters: %zd\n", niter);
00177
00178
00179 fpout = gk_fopen(params->outfile, "w", "main: outfile");
00180 for (i=0; i<mat->nrows; i++) {
00181 for (jmax=i, maxdiff=0.0, j=mat->rowptr[i]; j<mat->rowptr[i+1]; j++) {
00182 if ((diff = fabs(pr[i]-pr[mat->rowind[j]])) > maxdiff) {
00183 maxdiff = diff;
00184 jmax = mat->rowind[j];
00185 }
00186 }
00187 fprintf(fpout, "%.4e %10zd %.4e %10d\n", pr[i],
00188 mat->rowptr[i+1]-mat->rowptr[i], maxdiff, jmax+1);
00189 }
00190 gk_fclose(fpout);
00191
00192 gk_free((void **)&pr, LTERM);
00193 }
00194
00195 gk_csr_Free(&mat);
00196
00197
00198 print_final_info(params);
00199 }
00200
00201
00202
00203
00205
00206 void print_init_info(params_t *params, gk_csr_t *mat)
00207 {
00208 printf("*******************************************************************************\n");
00209 printf(" fis\n\n");
00210 printf("Matrix Information ---------------------------------------------------------\n");
00211 printf(" input file=%s, [%d, %d, %zd]\n",
00212 params->infile, mat->nrows, mat->ncols, mat->rowptr[mat->nrows]);
00213
00214 printf("\n");
00215 printf("Options --------------------------------------------------------------------\n");
00216 printf(" niter=%d, ntvs=%d, ppr=%d, lamda=%f, eps=%e\n",
00217 params->niter, params->ntvs, params->ppr, params->lamda, params->eps);
00218
00219 printf("\n");
00220 printf("Performing random walks... ----------------------------------------------\n");
00221 }
00222
00223
00224
00226
00227 void print_final_info(params_t *params)
00228 {
00229 printf("\n");
00230 printf("Memory Usage Information -----------------------------------------------------\n");
00231 printf(" Maximum memory used: %10zd bytes\n", (ssize_t) gk_GetMaxMemoryUsed());
00232 printf(" Current memory used: %10zd bytes\n", (ssize_t) gk_GetCurMemoryUsed());
00233 printf("********************************************************************************\n");
00234 }
00235
00236
00237
00239
00240 params_t *parse_cmdline(int argc, char *argv[])
00241 {
00242 int i;
00243 int c, option_index;
00244 params_t *params;
00245
00246 params = (params_t *)gk_malloc(sizeof(params_t), "parse_cmdline: params");
00247
00248
00249 params->niter = 100;
00250 params->ppr = -1;
00251 params->ntvs = -1;
00252 params->eps = 1e-10;
00253 params->lamda = 0.80;
00254 params->infile = NULL;
00255 params->outfile = NULL;
00256
00257
00258
00259 while ((c = gk_getopt_long_only(argc, argv, "", long_options, &option_index)) != -1) {
00260 switch (c) {
00261 case CMD_NITER:
00262 if (gk_optarg) params->niter = atoi(gk_optarg);
00263 break;
00264 case CMD_NTVS:
00265 if (gk_optarg) params->ntvs = atoi(gk_optarg);
00266 break;
00267 case CMD_PPR:
00268 if (gk_optarg) params->ppr = atoi(gk_optarg);
00269 break;
00270 case CMD_EPS:
00271 if (gk_optarg) params->eps = atof(gk_optarg);
00272 break;
00273 case CMD_LAMDA:
00274 if (gk_optarg) params->lamda = atof(gk_optarg);
00275 break;
00276
00277 case CMD_HELP:
00278 for (i=0; strlen(helpstr[i]) > 0; i++)
00279 printf("%s\n", helpstr[i]);
00280 exit(0);
00281 break;
00282 case '?':
00283 default:
00284 printf("Illegal command-line option(s)\nUse %s -help for a summary of the options.\n", argv[0]);
00285 exit(0);
00286 }
00287 }
00288
00289 if (argc-gk_optind != 2) {
00290 printf("Unrecognized parameters.");
00291 for (i=0; strlen(shorthelpstr[i]) > 0; i++)
00292 printf("%s\n", shorthelpstr[i]);
00293 exit(0);
00294 }
00295
00296 params->infile = gk_strdup(argv[gk_optind++]);
00297 params->outfile = gk_strdup(argv[gk_optind++]);
00298
00299 if (!gk_fexists(params->infile))
00300 errexit("input file %s does not exist.\n", params->infile);
00301
00302 if (params->ppr != -1 && params->ntvs != -1)
00303 errexit("Only one of the -ppr and -ntvs options can be specified.\n");
00304
00305 return params;
00306 }
00307