graph kruskal(graph G, float (*pesoArco )(void *)) { int nNodes = graphCountNodes(G); graph GF = graphInit(nNodes, GRAPH_IS_NOT_ORIENTED); // e gli orientati?? uf_handler uf = uf_init(graphGetMaxNodes(G)); archInfo arco; coda allArcs=graphGetAllArchs(G); heap archHeap=heapInit(nNodes, pesoArco, HEAP_GET_MIN); while ((arco=codaGet(allArcs))!=NULL) { heapInsert(archHeap, arco); } int from, to; while ((arco= heapExtract(archHeap))!=NULL) { from=arco->fromNode; to= arco->toNode; if (uf_find(uf,from , to)) { uf_unionFind(uf, from, to); graphAddNode(GF, from, arco->fromInfo); graphAddNode(GF, to, arco->toInfo); graphAddArch(GF, from, to, arco->archInfo); } } return GF; }
/* Issue the drawing action. */ int ex(nodeType *p){ int rte, rtm; graphInit(); exNode(p, 0, 0, &rte, &rtm); graphFinish(); return 0; }
coda topological_sort_SelfTest(graph Gin){ graph G=graphInit(9, GRAPH_IS_ORIENTED); for (int i=1; i<10; graphAddNode(G, i, NULL), i++); //graphAddArch(G, 0, 1, NULL); graphAddArch(G, 1, 2, NULL); graphAddArch(G, 2, 8, NULL); graphAddArch(G, 3, 2, NULL); graphAddArch(G, 3, 7, NULL); graphAddArch(G, 7, 8, NULL); graphAddArch(G, 4, 5, NULL); coda C=topological_sort(G); coda_iterator I=codaIteratorInit(C, NULL, FORWARD_ITERATION); int x; while ((x=coda_NextNum(I))!=CODA_ITERATION_END) printf(">%d\n",(int)x); codaIteratorFree(I); puts("\n ---------\n"); return NULL; }
int graphLoad(GraphStruct * graph, FILE * gdata) { int numGlobalVertices, numGlobalEdges, numParts; int i, j, nnbors; /* Get the number of vertices */ getNextLine(gdata, line, bufsize); sscanf(line, "%d", &numGlobalVertices); /* Get the number of edges */ getNextLine(gdata, line, bufsize); sscanf(line, "%d", &numGlobalEdges); /* Get the number of partitions */ getNextLine(gdata, line, bufsize); sscanf(line, "%d", &numParts); /* Allocate arrays to read in entire graph */ graphInit(graph, numGlobalVertices, numGlobalEdges << 1, numParts); char * token; //TODO partition placement??? // //read current partition mapping to physical cores in pairs of <partNO, coreID> for (i = 0; i < numParts; i++) { getNextLine(gdata, line, bufsize); // token = strtok(line, " "); // part = atoi(token); // token = strtok(NULL, " "); // memcpy(graph->partPlacement + part * 5, token, 5); } // graph->partPlacement[5 * numParts] = '\0'; //each line is in the form of vtx_id part_id vtx_wgt vtx_size num_nbors nbor_id edge_wgt nbor_id edge_wgt for (i = 0; i < numGlobalVertices; i++) { getNextLine(gdata, line, bufsize); token = strtok(line, " "); graph->vertexGIDs[i] = atoi(token); token = strtok(NULL, " "); graph->partVector[i] = atoi(token); token = strtok(NULL, " "); graph->vertexWgts[i] = (float) atof(token); token = strtok(NULL, " "); graph->vertexSize[i] = atoi(token); token = strtok(NULL, " "); nnbors = atoi(token); graph->nborIndex[i + 1] = graph->nborIndex[i] + nnbors; for (j = graph->nborIndex[i]; j<graph->nborIndex[i + 1]; j++) { token = strtok(NULL, " "); graph->nborGIDs[j] = atoi(token); token = strtok(NULL, " "); graph->edgeWgts[j] = (float) atof(token); } } return 0; }
//https://github.com/KHEngineering/SmoothPathPlanner planner::planner(QWidget *parent) : QMainWindow(parent), ui(new Ui::planner) { ui->setupUi(this); graphInit(); resolution=0.2; //map resolution interval=1; plan=new path; populateMap(); }
void startupInit() { usbdataEnable(); gdispSetBacklight(50); gdispSetContrast(50); geventListenerInit(&gl); gwinAttachListener(&gl); // Setup UI objects and show splash guiCreate(); gwinProgressbarSetResolution(ghProgbarLoad, 10); gwinProgressbarStart(ghProgbarLoad, 300); graphInit(); // Setup INA219 I2C Current Sensor ina219Init(); sduObjectInit(USB_SERIAL_DRIVER); sduStart(USB_SERIAL_DRIVER, &serusbcfg); /* * Activates the USB driver and then the USB bus pull-up on D+. * Note, a delay is inserted in order to not have to disconnect the cable * after a reset. */ usbDisconnectBus(serusbcfg.usbp); chThdSleepMilliseconds(1000); usbStart(serusbcfg.usbp, &usbcfg); usbConnectBus(serusbcfg.usbp); setStartTime(); chThdCreateStatic(ledThread, sizeof(ledThread), LOWPRIO, ThreadLED, NULL); chThdCreateStatic(inaThread, sizeof(inaThread), HIGHPRIO, ThreadINA, NULL); chThdCreateStatic(gfxvalThread, sizeof(gfxvalThread), NORMALPRIO, ThreadGFXVal, NULL); chThdCreateStatic(timeThread, sizeof(timeThread), NORMALPRIO, ThreadTIME, NULL); chThdCreateStatic(gfxEventThread, sizeof(gfxEventThread), NORMALPRIO, ThreadGFXEvent, NULL); chThdSleepMilliseconds(2000); chThdCreateStatic(graphThread, sizeof(graphThread), NORMALPRIO+5, ThreadGRAPH, NULL); // Destroy splay screen and show main page gwinDestroy(ghContainerPageSplash); guiShowPage(1); }
void graphRecv(MPI_Comm comm, GraphStruct * local_graph){ int num_procs; MPI_Comm_size(comm, &num_procs); int send_count[4]; int ack = 0, ack_tag = 5, count_tag = 10, id_tag = 15; MPI_Recv(send_count, 4, MPI_INT, 0, count_tag, comm, MPI_STATUS_IGNORE); MPI_Send(&ack, 1, MPI_INT, 0, ack_tag, comm); if (send_count[0] > 0) { graphInit(local_graph, send_count[0], send_count[1], send_count[2]); MPI_Recv(local_graph->vertexGIDs, send_count[0], MPI_INT, 0, id_tag, comm, MPI_STATUS_IGNORE); MPI_Recv(local_graph->nborIndex, send_count[0] + 1, MPI_INT, 0, id_tag + 1, comm, MPI_STATUS_IGNORE); MPI_Recv(local_graph->partVector, send_count[0], MPI_INT, 0, id_tag + 2, comm, MPI_STATUS_IGNORE); MPI_Recv(local_graph->vertexSize, send_count[0], MPI_INT, 0, id_tag + 3, comm, MPI_STATUS_IGNORE); MPI_Recv(local_graph->vertexWgts, send_count[0], MPI_FLOAT, 0, id_tag + 4, comm, MPI_STATUS_IGNORE); if (send_count[1] > 0) { MPI_Recv(local_graph->nborGIDs, send_count[1], MPI_INT, 0, id_tag + 5, comm, MPI_STATUS_IGNORE); MPI_Recv(local_graph->nborProcs, send_count[1], MPI_INT, 0, id_tag + 6, comm, MPI_STATUS_IGNORE); MPI_Recv(local_graph->edgeWgts, send_count[1], MPI_FLOAT, 0, id_tag + 7, comm, MPI_STATUS_IGNORE); } MPI_Recv(local_graph->initialPartNo2CoreID, local_graph->numParts, MPI_INT, 0, id_tag + 9, comm, MPI_STATUS_IGNORE); } MPI_Recv(&ack, 1, MPI_INT, 0, 0, comm, MPI_STATUS_IGNORE); if (ack < 0) { int my_rank; MPI_Comm_rank(comm, &my_rank); printf("oops! Rank %d receive subgraph from rank 0 failed!\n", my_rank); } }
int main(void) { int i, j; graphInit(V); graphSet(0, 2); graphSet(1, 4); graphSet(2, 5); graphSet(3, 6); graphSet(0, 4); graphSet(6, 0); graphSet(1, 3); // Showing matrix for (i = 0; i < V; ++i) { for (j = 0; j < V; ++j) { printf("%i ", graphGet(i, j)); } printf("\n"); } return 0; }
void MinimumWeightedMatch( int array[SIZE][SIZE], int match[SIZE], int n ){ int i, j; int mini; for ( i = 1 ; i <= n ; i++ ){ mini = INT_MAX; for ( j = 1 ; j <= n ; j++ ) if ( mini > array[i][j] ) mini = array[i][j]; for ( j = 1 ; j <= n ; j++ ) array[i][j] -= mini; } /*printMatrix(array, n); printf("For row...\n\n");*/ if ( ExactMatch(array, match, n) ) return; for ( j = 1 ; j <= n ; j++ ){ mini = INT_MAX; for ( i = 1 ; i <= n ; i++ ) if ( mini > array[i][j] ) mini = array[i][j]; for ( i = 1 ; i <= n ; i++ ) array[i][j] -= mini; } /*printMatrix(array, n); printf("For column...\n\n");*/ if ( ExactMatch(array, match, n) ) return; while ( 1 ){ graphInit( 2 * n + 1 ); mini = INT_MAX; for ( i = 1 ; i <= n ; i++ ) for ( j = 1 ; j <= n ; j++ ) if ( array[i][j] == 0 ) adj[i][degree[i]++] = n + j; if ( n == HopcroftKarp(n, n) ){ /*printf("Matching about to be found...\n");*/ for ( i = 1 ; i <= n ; i++ ){ match[i] = pair[i] - n; /*printf("HKarp matches row %d to col %d\n", i, pair[i] - n);*/ } return; } for ( i = 1 ; i <= n ; i++ ) for ( j = 1 ; j <= n ; j++ ) if ( !T[i] && T[j+n] && (mini > array[i][j]) ) mini = array[i][j]; for ( i = 1 ; i <= n ; i++ ){ if ( T[i] ){ /*printf(" r%d", i);*/ for ( j = 1 ; j <= n ; j++ ) array[i][j] += mini; } } for ( i = 1 ; i <= n ; i++ ){ if ( !T[i + n] ){ /*printf(" c%d", i);*/ for ( j = 1 ; j <= n ; j++ ) array[j][i] += mini; } } /*puts("");*/ for ( i = 1 ; i <= n ; i++ ) for ( j = 1 ; j <= n ; j++ ) array[i][j] -= mini; /*printMatrix(array, n); getchar();*/ } }
int main ( int argc, char * argv[]) { FILE * fileptr; FILE * stream; Wgraph wgrfdat; Strat * straptr; double clktime[2]; double runtime; struct timespec tp; Gnum partnbr; Gnum partval; Gnum vertnum; Gnum comploadsum; Gnum comploadavg; Gnum comploadmax; Gnum comploadmin; int i; char c; const Gnum * restrict vlbltax; errorProg (argv[0]); if (argc != 5) { errorPrint ("main: usage is \"%s <nparts> <input source graph file> <output mapping file> <strategy>\"\n", argv[0]); exit (1); } fprintf (stderr, "pid %d\n", getpid ()); printf ("Waiting for key press...\n"); scanf ("%c", &c); if (graphInit (&wgrfdat.s) != 0) { /* Initialize source graph */ errorPrint ("main: cannot initialize graph"); return (1); } if ((partnbr = atoi (argv[1])) < 1) { errorPrint ("main: invalid number of parts (\"%s\")", argv[1]); return (1); } fileptr = NULL; if ((fileptr = fopen (argv[2], "r")) == NULL) { errorPrint ("main: cannot open graph file"); return (1); } if (graphLoad (&wgrfdat.s, fileptr, -1, 0) != 0) { errorPrint ("main: cannot load graph file"); return (1); } if (fileptr != NULL) fclose (fileptr); wgraphInit (&wgrfdat, &wgrfdat.s, partnbr); /* wgraphZero (&wgrfdat); TODO to clean? */ if ((straptr = stratInit (&wgraphpartststratab, argv[4])) == NULL) { errorPrint ("main: cannot initialize strategy"); return (1); } /* Init clock. */ clktime[0] = clktime[1] = runtime = 0; /* Start clock. */ clock_gettime (CLOCK_REALTIME, &tp); /* Elapsed time */ clktime[0] = ((double) tp.tv_sec + (double) tp.tv_nsec * 1.0e-9L); wgraphPartSt (&wgrfdat, straptr); /* Stop clock. */ clock_gettime (CLOCK_REALTIME, &tp); /* Elapsed time */ clktime[1] = ((double) tp.tv_sec + (double) tp.tv_nsec * 1.0e-9L); runtime += (clktime[1] - clktime[0]); comploadsum = comploadavg = comploadmax = 0; comploadmin = GNUMMAX; for (i = 0; i < partnbr; i ++) { comploadsum += wgrfdat.compload[i]; if (wgrfdat.compload[i] > comploadmax) comploadmax = wgrfdat.compload[i]; if (wgrfdat.compload[i] < comploadmin) comploadmin = wgrfdat.compload[i]; } comploadavg = comploadsum / wgrfdat.partnbr; for (partval = 0; partval < wgrfdat.partnbr; partval ++) /* for each part */ printf("\033[0;33mcompload[%d] %d %d\033[0m\n", partval, wgrfdat.compload[partval], wgrfdat.compsize[partval]); printf ("\033[0;32mCompLoad(max/avg)=%g,\t(min/avg)=%g\n", ((double) comploadmax / (double) comploadavg), ((double) comploadmin / (double) comploadavg)); printf ("\033[0;32mFronLoad=%ld\n", (long) wgrfdat.fronload); printf ("\033[0;32mTime=%f\n", runtime); stream = NULL; if ((stream = fopen (argv[3], "w")) == NULL) { errorPrint ("main: cannot open graph file"); return (1); } vlbltax = (wgrfdat.s.vlbltax != NULL) ? (wgrfdat.s.vlbltax) : NULL; if (fprintf (stream, "%ld\n", (long) wgrfdat.s.vertnbr) == EOF) { errorPrint ("main: bad output (1)"); return (1); } for (vertnum = wgrfdat.s.baseval; vertnum < wgrfdat.s.vertnnd; vertnum ++) { if (fprintf (stream, "%ld\t%ld\n", (long) ((vlbltax != NULL) ? vlbltax[vertnum] : vertnum), (long) wgrfdat.parttax[vertnum]) == EOF) { errorPrint ("main: bad output (2)"); return (1); } } if (stream != NULL) fclose (stream); wgrfdat.parttax = NULL; /* Do not free parttax as grouped with frontab */ wgraphExit (&wgrfdat); stratExit (straptr); exit (0); }
//distribute the graph according to the partition number (rank-i responsible for partition i); void graphDistribute(MPI_Comm comm, GraphStruct global_graph, GraphStruct * local_graph){ int num_procs; MPI_Comm_size(comm, &num_procs); int i, j, proc_id, vtx_lid, nnbors, nbor_index, nbor_proc; GraphStruct *sub_graphs = (GraphStruct *) malloc(sizeof(GraphStruct) * num_procs); for (i = 0; i < num_procs; i++) { sub_graphs[i].numVertices = 0; sub_graphs[i].numNbors = 0; } int num_parts_per_proc; if(global_graph.numParts > num_procs){ num_parts_per_proc = global_graph.numParts / num_procs + global_graph.numParts % num_procs; //TODO }else{ num_parts_per_proc = 1; } int *global_part_vector = malloc(sizeof(int) * global_graph.numVertices); for (i = 0; i < global_graph.numVertices; i++) { proc_id = global_graph.partVector[i] / num_parts_per_proc; sub_graphs[proc_id].numVertices++; sub_graphs[proc_id].numNbors += (global_graph.nborIndex[i + 1] - global_graph.nborIndex[i]); global_part_vector[global_graph.vertexGIDs[i]] = global_graph.partVector[i]; } for (i = 0; i < num_procs; i++) { graphInit(&sub_graphs[i], sub_graphs[i].numVertices, sub_graphs[i].numNbors, global_graph.numParts); sub_graphs[i].numVertices = 0; sub_graphs[i].numNbors = 0; memcpy(sub_graphs[i].initialPartNo2CoreID, global_graph.initialPartNo2CoreID, global_graph.numParts * sizeof(int)); } for (i = 0; i < global_graph.numVertices; i++) { proc_id = global_graph.partVector[i] / num_parts_per_proc; vtx_lid = sub_graphs[proc_id].numVertices ++; sub_graphs[proc_id].vertexGIDs[vtx_lid] = global_graph.vertexGIDs[i]; sub_graphs[proc_id].vertexSize[vtx_lid] = global_graph.vertexSize[i]; sub_graphs[proc_id].vertexWgts[vtx_lid] = global_graph.vertexWgts[i]; sub_graphs[proc_id].partVector[vtx_lid] = global_graph.partVector[i]; nnbors = global_graph.nborIndex[i + 1] - global_graph.nborIndex[i]; sub_graphs[proc_id].nborIndex[vtx_lid + 1] = sub_graphs[proc_id].nborIndex[vtx_lid] + nnbors; for (j = global_graph.nborIndex[i]; j < global_graph.nborIndex[i + 1]; j++) { nbor_index = sub_graphs[proc_id].numNbors ++; nbor_proc = global_part_vector[global_graph.nborGIDs[j]] / num_parts_per_proc; sub_graphs[proc_id].nborProcs[nbor_index] = nbor_proc; sub_graphs[proc_id].nborGIDs[nbor_index] = global_graph.nborGIDs[j]; sub_graphs[proc_id].edgeWgts[nbor_index] = global_graph.edgeWgts[j]; } } free(global_part_vector); *local_graph = sub_graphs[0]; for (i = 1; i < num_procs; i++) { graphSend(comm, sub_graphs[i], i); graphDeinit(&sub_graphs[i]); } free(sub_graphs); int ack = 0; for (i = 1; i < num_procs; i++) { MPI_Send(&ack, 1, MPI_INT, i, 0, comm); } }