// closes the read archive void CReadWriter::Close(void) { // prevent the archive from being updated elsewhere mIsOpen = false; // flush the buffer if(mPartitionMembers > 0) WritePartition(); // ================= // update the header // ================= // update the number of reads in the archive fseek64(mOutStream, UPDATE_HEADER_OFFSET, SEEK_SET); fwrite((char*)&mNumReads, SIZEOF_UINT64, 1, mOutStream); // update the number of bases in the archive fwrite((char*)&mNumBases, SIZEOF_UINT64, 1, mOutStream); // close the file stream fclose(mOutStream); }
/************************************************************************* * Let the game begin **************************************************************************/ main(int argc, char *argv[]) { int i, nparts, options[10]; idxtype *part; float rubvec[MAXNCON], lbvec[MAXNCON]; GraphType graph; char filename[256]; int numflag = 0, wgtflag = 0, edgecut; timer TOTALTmr, METISTmr, IOTmr; if (argc != 3) { printf("Usage: %s <GraphFile> <Nparts>\n",argv[0]); exit(0); } strcpy(filename, argv[1]); nparts = atoi(argv[2]); if (nparts < 2) { printf("The number of partitions should be greater than 1!\n"); exit(0); } cleartimer(TOTALTmr); cleartimer(METISTmr); cleartimer(IOTmr); starttimer(TOTALTmr); starttimer(IOTmr); ReadGraph(&graph, filename, &wgtflag); if (graph.nvtxs <= 0) { printf("Empty graph. Nothing to do.\n"); exit(0); } stoptimer(IOTmr); printf("**********************************************************************\n"); printf("%s", METISTITLE); printf("Graph Information ---------------------------------------------------\n"); printf(" Name: %s, #Vertices: %d, #Edges: %d, #Parts: %d\n", filename, graph.nvtxs, graph.nedges/2, nparts); if (graph.ncon > 1) printf(" Balancing Constraints: %d\n", graph.ncon); printf("\nK-way Partitioning... -----------------------------------------------\n"); part = idxmalloc(graph.nvtxs, "main: part"); options[0] = 0; starttimer(METISTmr); if (graph.ncon == 1) { METIS_PartGraphKway(&graph.nvtxs, graph.xadj, graph.adjncy, graph.vwgt, graph.adjwgt, &wgtflag, &numflag, &nparts, options, &edgecut, part); } else { for (i=0; i<graph.ncon; i++) rubvec[i] = HORIZONTAL_IMBALANCE; METIS_mCPartGraphKway(&graph.nvtxs, &graph.ncon, graph.xadj, graph.adjncy, graph.vwgt, graph.adjwgt, &wgtflag, &numflag, &nparts, rubvec, options, &edgecut, part); } stoptimer(METISTmr); ComputePartitionBalance(&graph, nparts, part, lbvec); printf(" %d-way Edge-Cut: %7d, Balance: ", nparts, edgecut); for (i=0; i<graph.ncon; i++) printf("%5.2f ", lbvec[i]); printf("\n"); starttimer(IOTmr); WritePartition(filename, part, graph.nvtxs, nparts); stoptimer(IOTmr); stoptimer(TOTALTmr); printf("\nTiming Information --------------------------------------------------\n"); printf(" I/O: \t\t %7.3f\n", gettimer(IOTmr)); printf(" Partitioning: \t\t %7.3f (KMETIS time)\n", gettimer(METISTmr)); printf(" Total: \t\t %7.3f\n", gettimer(TOTALTmr)); printf("**********************************************************************\n"); GKfree(&graph.xadj, &graph.adjncy, &graph.vwgt, &graph.adjwgt, &part, LTERM); }
sparse_matrix* graph_partition(LIST *list , partition_t* partition_info) { graph_t* graph; params_t *params; idx_t options[METIS_NOPTIONS], status; idx_t objval; sparse_matrix* matrix = (sparse_matrix*)malloc(sizeof(sparse_matrix)); graph = ReadGraph(list); params = bmalloc(sizeof(*params), "Allocating memory for params"); METIS_SetDefaultOptions(options); METIS_init_params(params, graph->ncon); options[METIS_OPTION_OBJTYPE] = params->objtype; options[METIS_OPTION_CTYPE] = params->ctype; options[METIS_OPTION_IPTYPE] = params->iptype; options[METIS_OPTION_RTYPE] = params->rtype; options[METIS_OPTION_NO2HOP] = params->no2hop; options[METIS_OPTION_MINCONN] = params->minconn; options[METIS_OPTION_CONTIG] = params->contig; options[METIS_OPTION_SEED] = params->seed; options[METIS_OPTION_NITER] = params->niter; options[METIS_OPTION_NCUTS] = params->ncuts; options[METIS_OPTION_UFACTOR] = params->ufactor; options[METIS_OPTION_DBGLVL] = params->dbglvl; //print_input_data(graph,params); params->tpwgts = NULL; params->ubvec = NULL; partition_info->partion_table = imalloc(graph->nvtxs, "Allocate memory for part"); switch (params->ptype) { case METIS_PTYPE_RB: status = METIS_PartGraphRecursive(&graph->nvtxs, &graph->ncon, graph->xadj, graph->adjncy, graph->vwgt, graph->vsize, graph->adjwgt, ¶ms->nparts, params->tpwgts, params->ubvec, options, &objval, partition_info->partion_table); break; case METIS_PTYPE_KWAY: status = METIS_PartGraphKway(&graph->nvtxs, &graph->ncon, graph->xadj, graph->adjncy, graph->vwgt, graph->vsize, graph->adjwgt, ¶ms->nparts, params->tpwgts, params->ubvec, options, &objval, partition_info->partion_table); break; } if (status != METIS_OK) { fprintf(stderr,"\n***Metis returned with an error.***\n"); return NULL; } partition_info->size = graph->nvtxs; partition_info->noOfParts = params->nparts; WritePartition(params->filename, partition_info->partion_table, graph->nvtxs, params->nparts); printf("Objval: %d\n",objval); matrix->nz = -1; matrix->n = graph->nvtxs; matrix->p = graph->xadj; matrix->i = graph->adjncy; matrix->x = NULL; return matrix; }
// saves the read to the read archive void CReadWriter::SaveRead(const Mosaik::Read& mr) { // initialize unsigned char readNameLen = (unsigned char)mr.Name.Length(); unsigned short numMate1Bases = (unsigned short)mr.Mate1.Bases.Length(); unsigned short numMate2Bases = (unsigned short)mr.Mate2.Bases.Length(); // return if both mates are empty if((numMate1Bases == 0) && (numMate2Bases == 0)) return; mNumBases += numMate1Bases + numMate2Bases; bool isPairedEnd = false; if(numMate2Bases > 0) isPairedEnd = true; // calculate the entry size unsigned short entrySize = 2 * numMate1Bases + readNameLen + SIZEOF_SHORT + 2; if(isPairedEnd) entrySize += 2 * numMate2Bases + SIZEOF_SHORT; if(mIsSOLiD) { entrySize += SOLID_PREFIX_LENGTH; if(isPairedEnd) entrySize += SOLID_PREFIX_LENGTH; } // check the memory buffer if(mBufferPosition >= (mBufferLen - entrySize)) AdjustBuffer(); // ============================ // serialize data to our buffer // ============================ // no need to leave space for entry size unsigned int bufferOffset = mBufferPosition; // store the read type mBuffer[bufferOffset++] = (isPairedEnd ? 1 : 0); // store the read name mBuffer[bufferOffset++] = readNameLen; memcpy(mBuffer + bufferOffset, mr.Name.CData(), readNameLen); bufferOffset += readNameLen; // ============ // store mate 1 // ============ // sanity check if ( mr.Mate1.Bases.Length() != mr.Mate1.Qualities.Length() ) { printf("ERROR: The lengths of bases(%u) and qualities(%u) of Read (%s) didn't match.\n", mr.Mate1.Bases.Length(), mr.Mate1.Qualities.Length(), mr.Name.CData()); exit(1); } // store the read length memcpy(mBuffer + bufferOffset, (char*)&numMate1Bases, SIZEOF_SHORT); bufferOffset += SIZEOF_SHORT; // store the bases memcpy(mBuffer + bufferOffset, mr.Mate1.Bases.CData(), numMate1Bases); bufferOffset += numMate1Bases; if(mIsSOLiD) { memcpy(mBuffer + bufferOffset, mr.Mate1.SolidPrefixTransition, SOLID_PREFIX_LENGTH); bufferOffset += SOLID_PREFIX_LENGTH; } // store the qualities memcpy(mBuffer + bufferOffset, mr.Mate1.Qualities.CData(), numMate1Bases); bufferOffset += numMate1Bases; // ============ // store mate 2 // ============ if(isPairedEnd) { // sanity check if ( mr.Mate2.Bases.Length() != mr.Mate2.Qualities.Length() ) { printf("ERROR: The lengths of bases(%u) and qualities(%u) of Read (%s) didn't match.\n", mr.Mate2.Bases.Length(), mr.Mate2.Qualities.Length(), mr.Name.CData()); exit(1); } // store the read length memcpy(mBuffer + bufferOffset, (char*)&numMate2Bases, SIZEOF_SHORT); bufferOffset += SIZEOF_SHORT; // store the bases memcpy(mBuffer + bufferOffset, mr.Mate2.Bases.CData(), numMate2Bases); bufferOffset += numMate2Bases; if(mIsSOLiD) { memcpy(mBuffer + bufferOffset, mr.Mate2.SolidPrefixTransition, SOLID_PREFIX_LENGTH); bufferOffset += SOLID_PREFIX_LENGTH; } // store the qualities memcpy(mBuffer + bufferOffset, mr.Mate2.Qualities.CData(), numMate2Bases); bufferOffset += numMate2Bases; } // check the buffer if(bufferOffset >= mBufferLen) { cout << endl << "ERROR: Buffer overrun detected when saving read. Used " << bufferOffset << " bytes, but allocated " << mBufferLen << " bytes." << endl; exit(1); } // update the partition variables and buffer position mPartitionMembers++; mBufferPosition = bufferOffset; // flush the buffer if(mPartitionMembers >= mPartitionSize) WritePartition(); // increment the read counter mNumReads++; }
idx_t* gpmetis( int argc, char **argv ) /*************************************************************************/ /*! Let the game begin! */ /*************************************************************************/ //int main(int argc, char *argv[]) { idx_t i; char *curptr, *newptr; idx_t options[METIS_NOPTIONS]; graph_t *graph; idx_t *part; idx_t objval; params_t *params; int status=0; gk_optind = 0; //printf( "argc: %d\n", argc ); //printf( "gk_optind %d\n", gk_optind ); fflush( stdout ); for( i = 0; i < argc; i++ ) { //printf( "%s*\n", argv[ i ] ); } params = parse_cmdline(argc, argv); //printf( "gk_optind %d\n", gk_optind ); //fflush( stdout ); //return NULL; gk_startcputimer(params->iotimer); graph = ReadGraph(params); ReadTPwgts(params, graph->ncon); gk_stopcputimer(params->iotimer); /* Check if the graph is contiguous */ if (params->contig && !IsConnected(graph, 0)) { printf("***The input graph is not contiguous.\n" "***The specified -contig option will be ignored.\n"); params->contig = 0; } /* Get ubvec if supplied */ if (params->ubvecstr) { params->ubvec = rmalloc(graph->ncon, "main"); curptr = params->ubvecstr; for (i=0; i<graph->ncon; i++) { params->ubvec[i] = strtoreal(curptr, &newptr); if (curptr == newptr) errexit("Error parsing entry #%"PRIDX" of ubvec [%s] (possibly missing).\n", i, params->ubvecstr); curptr = newptr; } } /* Setup iptype */ if (params->iptype == -1) { if (params->ptype == METIS_PTYPE_RB) { if (graph->ncon == 1) params->iptype = METIS_IPTYPE_GROW; else params->iptype = METIS_IPTYPE_RANDOM; } } GPPrintInfo(params, graph); part = imalloc(graph->nvtxs, "main: part"); METIS_SetDefaultOptions(options); options[METIS_OPTION_OBJTYPE] = params->objtype; options[METIS_OPTION_CTYPE] = params->ctype; options[METIS_OPTION_IPTYPE] = params->iptype; options[METIS_OPTION_RTYPE] = params->rtype; options[METIS_OPTION_MINCONN] = params->minconn; options[METIS_OPTION_CONTIG] = params->contig; options[METIS_OPTION_SEED] = params->seed; options[METIS_OPTION_NITER] = params->niter; options[METIS_OPTION_NCUTS] = params->ncuts; options[METIS_OPTION_UFACTOR] = params->ufactor; options[METIS_OPTION_DBGLVL] = params->dbglvl; gk_malloc_init(); gk_startcputimer(params->parttimer); switch (params->ptype) { case METIS_PTYPE_RB: status = METIS_PartGraphRecursive(&graph->nvtxs, &graph->ncon, graph->xadj, graph->adjncy, graph->vwgt, graph->vsize, graph->adjwgt, ¶ms->nparts, params->tpwgts, params->ubvec, options, &objval, part); break; case METIS_PTYPE_KWAY: status = METIS_PartGraphKway(&graph->nvtxs, &graph->ncon, graph->xadj, graph->adjncy, graph->vwgt, graph->vsize, graph->adjwgt, ¶ms->nparts, params->tpwgts, params->ubvec, options, &objval, part); break; } gk_stopcputimer(params->parttimer); if (gk_GetCurMemoryUsed() != 0) printf("***It seems that Metis did not free all of its memory! Report this.\n"); params->maxmemory = gk_GetMaxMemoryUsed(); gk_malloc_cleanup(0); if (status != METIS_OK) { printf("\n***Metis returned with an error.\n"); } else { if (!params->nooutput) { /* Write the solution */ gk_startcputimer(params->iotimer); WritePartition(params->filename, part, graph->nvtxs, params->nparts); gk_stopcputimer(params->iotimer); } GPReportResults(params, graph, part, objval); } idx_t *r_part = ( idx_t* ) calloc( graph->nvtxs, sizeof( idx_t ) ); for( i = 0; i < graph->nvtxs; i++ ) { r_part[ i ] = part[ i ]; } FreeGraph(&graph); gk_free((void **)&part, LTERM); gk_free((void **)¶ms->filename, ¶ms->tpwgtsfile, ¶ms->tpwgts, ¶ms->ubvecstr, ¶ms->ubvec, ¶ms, LTERM); return r_part; }