Path: blob/devel/elmergrid/src/metis-5.1.0/GKlib/test/rw.c
3206 views
/*!1\file2\brief A simple frequent itemset discovery program to test GKlib's routines34\date 6/12/20085\author George6\version \verbatim $Id: rw.c 11387 2012-01-21 23:36:23Z karypis $ \endverbatim7*/89#include <GKlib.h>1011/*************************************************************************/12/*! Data structures for the code */13/*************************************************************************/14typedef struct {15int niter;16int ntvs;17int ppr;18float eps;19float lamda;20char *infile;21char *outfile;22} params_t;2324/*************************************************************************/25/*! Constants */26/*************************************************************************/27#define CMD_NITER 128#define CMD_EPS 229#define CMD_LAMDA 330#define CMD_PPR 431#define CMD_NTVS 532#define CMD_HELP 10333435/*************************************************************************/36/*! Local variables */37/*************************************************************************/38static struct gk_option long_options[] = {39{"niter", 1, 0, CMD_NITER},40{"lamda", 1, 0, CMD_LAMDA},41{"eps", 1, 0, CMD_EPS},42{"ppr", 1, 0, CMD_PPR},43{"ntvs", 1, 0, CMD_NTVS},44{"help", 0, 0, CMD_HELP},45{0, 0, 0, 0}46};474849/*-------------------------------------------------------------------*/50/* Mini help */51/*-------------------------------------------------------------------*/52static char helpstr[][100] = {53" ",54"Usage: rw [options] <graph-file> <out-file>",55" ",56" Required parameters",57" graph-file",58" The name of the file storing the transactions. The file is in ",59" Metis' graph format.",60" ",61" Optional parameters",62" -niter=int",63" Specifies the maximum number of iterations. [default: 100]",64" ",65" -lamda=float",66" Specifies the follow-the-adjacent-links probability. [default: 0.80]",67" ",68" -eps=float",69" Specifies the error tollerance. [default: 1e-10]",70" ",71" -ppr=int",72" Specifies the source of the personalized PR. [default: -1]",73" ",74" -ntvs=int",75" Specifies the number of test-vectors to compute. [default: -1]",76" ",77" -help",78" Prints this message.",79""80};8182static char shorthelpstr[][100] = {83" ",84" Usage: rw [options] <graph-file> <out-file>",85" use 'rw -help' for a summary of the options.",86""87};88899091/*************************************************************************/92/*! Function prototypes */93/*************************************************************************/94void print_init_info(params_t *params, gk_csr_t *mat);95void print_final_info(params_t *params);96params_t *parse_cmdline(int argc, char *argv[]);979899/*************************************************************************/100/*! the entry point */101/**************************************************************************/102int main(int argc, char *argv[])103{104ssize_t i, j, niter;105params_t *params;106gk_csr_t *mat;107FILE *fpout;108109/* get command-line options */110params = parse_cmdline(argc, argv);111112/* read the data */113mat = gk_csr_Read(params->infile, GK_CSR_FMT_METIS, 1, 1);114115/* display some basic stats */116print_init_info(params, mat);117118119120if (params->ntvs != -1) {121/* compute the pr for different randomly generated restart-distribution vectors */122float **prs;123124prs = gk_fAllocMatrix(params->ntvs, mat->nrows, 0.0, "main: prs");125126/* generate the random restart vectors */127for (j=0; j<params->ntvs; j++) {128for (i=0; i<mat->nrows; i++)129prs[j][i] = RandomInRange(931);130gk_fscale(mat->nrows, 1.0/gk_fsum(mat->nrows, prs[j], 1), prs[j], 1);131132niter = gk_rw_PageRank(mat, params->lamda, params->eps, params->niter, prs[j]);133printf("tvs#: %zd; niters: %zd\n", j, niter);134}135136/* output the computed pr scores */137fpout = gk_fopen(params->outfile, "w", "main: outfile");138for (i=0; i<mat->nrows; i++) {139for (j=0; j<params->ntvs; j++)140fprintf(fpout, "%.4e ", prs[j][i]);141fprintf(fpout, "\n");142}143gk_fclose(fpout);144145gk_fFreeMatrix(&prs, params->ntvs, mat->nrows);146}147else if (params->ppr != -1) {148/* compute the personalized pr from the specified vertex */149float *pr;150151pr = gk_fsmalloc(mat->nrows, 0.0, "main: pr");152153pr[params->ppr-1] = 1.0;154155niter = gk_rw_PageRank(mat, params->lamda, params->eps, params->niter, pr);156printf("ppr: %d; niters: %zd\n", params->ppr, niter);157158/* output the computed pr scores */159fpout = gk_fopen(params->outfile, "w", "main: outfile");160for (i=0; i<mat->nrows; i++)161fprintf(fpout, "%.4e\n", pr[i]);162gk_fclose(fpout);163164gk_free((void **)&pr, LTERM);165}166else {167/* compute the standard pr */168int jmax;169float diff, maxdiff;170float *pr;171172pr = gk_fsmalloc(mat->nrows, 1.0/mat->nrows, "main: pr");173174niter = gk_rw_PageRank(mat, params->lamda, params->eps, params->niter, pr);175printf("pr; niters: %zd\n", niter);176177/* output the computed pr scores */178fpout = gk_fopen(params->outfile, "w", "main: outfile");179for (i=0; i<mat->nrows; i++) {180for (jmax=i, maxdiff=0.0, j=mat->rowptr[i]; j<mat->rowptr[i+1]; j++) {181if ((diff = fabs(pr[i]-pr[mat->rowind[j]])) > maxdiff) {182maxdiff = diff;183jmax = mat->rowind[j];184}185}186fprintf(fpout, "%.4e %10zd %.4e %10d\n", pr[i],187mat->rowptr[i+1]-mat->rowptr[i], maxdiff, jmax+1);188}189gk_fclose(fpout);190191gk_free((void **)&pr, LTERM);192}193194gk_csr_Free(&mat);195196/* display some final stats */197print_final_info(params);198}199200201202/*************************************************************************/203/*! This function prints run parameters */204/*************************************************************************/205void print_init_info(params_t *params, gk_csr_t *mat)206{207printf("*******************************************************************************\n");208printf(" fis\n\n");209printf("Matrix Information ---------------------------------------------------------\n");210printf(" input file=%s, [%d, %d, %zd]\n",211params->infile, mat->nrows, mat->ncols, mat->rowptr[mat->nrows]);212213printf("\n");214printf("Options --------------------------------------------------------------------\n");215printf(" niter=%d, ntvs=%d, ppr=%d, lamda=%f, eps=%e\n",216params->niter, params->ntvs, params->ppr, params->lamda, params->eps);217218printf("\n");219printf("Performing random walks... ----------------------------------------------\n");220}221222223/*************************************************************************/224/*! This function prints final statistics */225/*************************************************************************/226void print_final_info(params_t *params)227{228printf("\n");229printf("Memory Usage Information -----------------------------------------------------\n");230printf(" Maximum memory used: %10zd bytes\n", (ssize_t) gk_GetMaxMemoryUsed());231printf(" Current memory used: %10zd bytes\n", (ssize_t) gk_GetCurMemoryUsed());232printf("********************************************************************************\n");233}234235236/*************************************************************************/237/*! This is the entry point of the command-line argument parser */238/*************************************************************************/239params_t *parse_cmdline(int argc, char *argv[])240{241int i;242int c, option_index;243params_t *params;244245params = (params_t *)gk_malloc(sizeof(params_t), "parse_cmdline: params");246247/* initialize the params data structure */248params->niter = 100;249params->ppr = -1;250params->ntvs = -1;251params->eps = 1e-10;252params->lamda = 0.80;253params->infile = NULL;254params->outfile = NULL;255256257/* Parse the command line arguments */258while ((c = gk_getopt_long_only(argc, argv, "", long_options, &option_index)) != -1) {259switch (c) {260case CMD_NITER:261if (gk_optarg) params->niter = atoi(gk_optarg);262break;263case CMD_NTVS:264if (gk_optarg) params->ntvs = atoi(gk_optarg);265break;266case CMD_PPR:267if (gk_optarg) params->ppr = atoi(gk_optarg);268break;269case CMD_EPS:270if (gk_optarg) params->eps = atof(gk_optarg);271break;272case CMD_LAMDA:273if (gk_optarg) params->lamda = atof(gk_optarg);274break;275276case CMD_HELP:277for (i=0; strlen(helpstr[i]) > 0; i++)278printf("%s\n", helpstr[i]);279exit(0);280break;281case '?':282default:283printf("Illegal command-line option(s)\nUse %s -help for a summary of the options.\n", argv[0]);284exit(0);285}286}287288if (argc-gk_optind != 2) {289printf("Unrecognized parameters.");290for (i=0; strlen(shorthelpstr[i]) > 0; i++)291printf("%s\n", shorthelpstr[i]);292exit(0);293}294295params->infile = gk_strdup(argv[gk_optind++]);296params->outfile = gk_strdup(argv[gk_optind++]);297298if (!gk_fexists(params->infile))299errexit("input file %s does not exist.\n", params->infile);300301if (params->ppr != -1 && params->ntvs != -1)302errexit("Only one of the -ppr and -ntvs options can be specified.\n");303304return params;305}306307308309