Path: blob/devel/elmergrid/src/metis-5.1.0/GKlib/test/gkgraph.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: gkgraph.c 11408 2012-01-25 15:05:58Z karypis $ \endverbatim7*/89#include <GKlib.h>1011/*************************************************************************/12/*! Data structures for the code */13/*************************************************************************/14typedef struct {15int type;16int niter;17float eps;18float lamda;1920char *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_TYPE 431#define CMD_HELP 10323334/*************************************************************************/35/*! Local variables */36/*************************************************************************/37static struct gk_option long_options[] = {38{"type", 1, 0, CMD_TYPE},39{"niter", 1, 0, CMD_NITER},40{"lamda", 1, 0, CMD_LAMDA},41{"eps", 1, 0, CMD_EPS},42{"help", 0, 0, CMD_HELP},43{0, 0, 0, 0}44};454647/*-------------------------------------------------------------------*/48/* Mini help */49/*-------------------------------------------------------------------*/50static char helpstr[][100] = {51" ",52"Usage: gkgraph [options] <graph-file> [<out-file>]",53" ",54" Required parameters",55" graph-file",56" The name of the file storing the graph. The file is in ",57" Metis' graph format.",58" ",59" Optional parameters",60" -niter=int",61" Specifies the maximum number of iterations. [default: 100]",62" ",63" -lamda=float",64" Specifies the follow-the-adjacent-links probability. [default: 0.80]",65" ",66" -eps=float",67" Specifies the error tollerance. [default: 1e-10]",68" ",69" -help",70" Prints this message.",71""72};7374static char shorthelpstr[][100] = {75" ",76" Usage: gkgraph [options] <graph-file> [<out-file>]",77" use 'gkgraph -help' for a summary of the options.",78""79};80818283/*************************************************************************/84/*! Function prototypes */85/*************************************************************************/86double compute_compactness(params_t *params, gk_graph_t *graph, int32_t *perm);87void reorder_centroid(params_t *params, gk_graph_t *graph, int32_t *perm);88void print_init_info(params_t *params, gk_graph_t *graph);89void print_final_info(params_t *params);90params_t *parse_cmdline(int argc, char *argv[]);919293/*************************************************************************/94/*! the entry point */95/**************************************************************************/96int main(int argc, char *argv[])97{98ssize_t i, j, v;99params_t *params;100gk_graph_t *graph, *pgraph;101int32_t *perm;102103/* get command-line options */104params = parse_cmdline(argc, argv);105106/* read the data */107graph = gk_graph_Read(params->infile, GK_GRAPH_FMT_METIS, 0, 0, 0);108109/* display some basic stats */110print_init_info(params, graph);111112113/* determine the initial compactness of the graph */114printf("Initial compactness: %le\n", compute_compactness(params, graph, NULL));115116/* compute the BFS ordering and re-order the graph */117//for (i=0; i<params->niter; i++) {118for (i=0; i<1; i++) {119v = RandomInRange(graph->nvtxs);120gk_graph_ComputeBFSOrdering(graph, v, &perm, NULL);121printf("BFS from %8d. Compactness: %le\n",122(int) v, compute_compactness(params, graph, perm));123124pgraph = gk_graph_Reorder(graph, perm, NULL);125gk_graph_Write(pgraph, "bfs.metis", GK_GRAPH_FMT_METIS);126gk_graph_Free(&pgraph);127128gk_graph_ComputeBestFOrdering(graph, v, params->type, &perm, NULL);129printf("BestF from %8d. Compactness: %le\n",130(int) v, compute_compactness(params, graph, perm));131132pgraph = gk_graph_Reorder(graph, perm, NULL);133gk_graph_Write(pgraph, "bestf.metis", GK_GRAPH_FMT_METIS);134gk_graph_Free(&pgraph);135136#ifdef XXX137for (j=0; j<params->niter; j++) {138reorder_centroid(params, graph, perm);139printf("\tAfter centroid; Compactness: %le\n",140compute_compactness(params, graph, perm));141}142143pgraph = gk_graph_Reorder(graph, perm, NULL);144gk_graph_Write(pgraph, "centroid.metis", GK_GRAPH_FMT_METIS);145gk_graph_Free(&pgraph);146#endif147gk_free((void **)&perm, LTERM);148}149150gk_graph_Free(&graph);151//gk_graph_Free(&pgraph);152153print_final_info(params);154}155156157158159/*************************************************************************/160/*! This function computes the compactness of the graph's adjacency list */161/*************************************************************************/162double compute_compactness(params_t *params, gk_graph_t *graph, int32_t *perm)163{164int i, v, u, nvtxs;165ssize_t j, *xadj;166int32_t *adjncy;167double compactness=0.0;168int *freq;169170nvtxs = graph->nvtxs;171xadj = graph->xadj;172adjncy = graph->adjncy;173174freq = gk_ismalloc(nvtxs, 0, "compute_compactness: freq");175176for (i=0; i<nvtxs; i++) {177v = (perm == NULL ? i : perm[i]);178for (j=xadj[i]; j<xadj[i+1]; j++) {179u = (perm == NULL ? adjncy[j] : perm[adjncy[j]]);180compactness += fabs(v-u);181freq[gk_abs(v-u)]++;182}183}184185/*186for (i=0; i<nvtxs; i++) {187if (freq[i] > 0)188printf("%7d %6d\n", i, freq[i]);189}190*/191printf("\tnsmall: %d\n", freq[1]+freq[2]+freq[3]);192193return compactness/xadj[nvtxs];194}195196197/*************************************************************************/198/*! This function uses a centroid-based approach to refine the ordering */199/*************************************************************************/200void reorder_centroid(params_t *params, gk_graph_t *graph, int32_t *perm)201{202int i, v, u, nvtxs;203ssize_t j, *xadj;204int32_t *adjncy;205gk_fkv_t *cand;206double displacement;207208nvtxs = graph->nvtxs;209xadj = graph->xadj;210adjncy = graph->adjncy;211212cand = gk_fkvmalloc(nvtxs, "reorder_centroid: cand");213214for (i=0; i<nvtxs; i++) {215v = perm[i];216displacement = 0.0;217218for (j=xadj[i]; j<xadj[i+1]; j++) {219u = perm[adjncy[j]];220displacement += u-v;221//displacement += sign(u-v, sqrt(fabs(u-v)));222}223224cand[i].val = i;225cand[i].key = v + displacement*params->lamda/(xadj[i+1]-xadj[i]);226}227228/* sort them based on the target position in increasing order */229gk_fkvsorti(nvtxs, cand);230231232/* derive the permutation from the ordered list */233gk_i32set(nvtxs, -1, perm);234for (i=0; i<nvtxs; i++) {235if (perm[cand[i].val] != -1)236errexit("Resetting perm[%d] = %d\n", cand[i].val, perm[cand[i].val]);237perm[cand[i].val] = i;238}239240gk_free((void **)&cand, LTERM);241}242243244245246247248249250/*************************************************************************/251/*! This function prints run parameters */252/*************************************************************************/253void print_init_info(params_t *params, gk_graph_t *graph)254{255printf("*******************************************************************************\n");256printf(" gkgraph\n\n");257printf("Graph Information ----------------------------------------------------------\n");258printf(" input file=%s, [%d, %zd]\n",259params->infile, graph->nvtxs, graph->xadj[graph->nvtxs]);260261printf("\n");262printf("Options --------------------------------------------------------------------\n");263printf(" type=%d, niter=%d, lamda=%f, eps=%e\n",264params->type, params->niter, params->lamda, params->eps);265266printf("\n");267printf("Working... -----------------------------------------------------------------\n");268}269270271/*************************************************************************/272/*! This function prints final statistics */273/*************************************************************************/274void print_final_info(params_t *params)275{276printf("\n");277printf("Memory Usage Information -----------------------------------------------------\n");278printf(" Maximum memory used: %10zd bytes\n", (ssize_t) gk_GetMaxMemoryUsed());279printf(" Current memory used: %10zd bytes\n", (ssize_t) gk_GetCurMemoryUsed());280printf("********************************************************************************\n");281}282283284/*************************************************************************/285/*! This is the entry point of the command-line argument parser */286/*************************************************************************/287params_t *parse_cmdline(int argc, char *argv[])288{289int i;290int c, option_index;291params_t *params;292293params = (params_t *)gk_malloc(sizeof(params_t), "parse_cmdline: params");294295/* initialize the params data structure */296params->type = 1;297params->niter = 1;298params->eps = 1e-10;299params->lamda = 0.20;300params->infile = NULL;301302303/* Parse the command line arguments */304while ((c = gk_getopt_long_only(argc, argv, "", long_options, &option_index)) != -1) {305switch (c) {306case CMD_TYPE:307if (gk_optarg) params->type = atoi(gk_optarg);308break;309case CMD_NITER:310if (gk_optarg) params->niter = atoi(gk_optarg);311break;312case CMD_EPS:313if (gk_optarg) params->eps = atof(gk_optarg);314break;315case CMD_LAMDA:316if (gk_optarg) params->lamda = atof(gk_optarg);317break;318319case CMD_HELP:320for (i=0; strlen(helpstr[i]) > 0; i++)321printf("%s\n", helpstr[i]);322exit(0);323break;324case '?':325default:326printf("Illegal command-line option(s)\nUse %s -help for a summary of the options.\n", argv[0]);327exit(0);328}329}330331if (argc-gk_optind != 1) {332printf("Unrecognized parameters.");333for (i=0; strlen(shorthelpstr[i]) > 0; i++)334printf("%s\n", shorthelpstr[i]);335exit(0);336}337338params->infile = gk_strdup(argv[gk_optind++]);339340if (argc-gk_optind > 0)341params->outfile = gk_strdup(argv[gk_optind++]);342else343params->outfile = gk_strdup("gkgraph.out");344345if (!gk_fexists(params->infile))346errexit("input file %s does not exist.\n", params->infile);347348return params;349}350351352353