/** * \ingroup ijv * \brief sort an ijv on the i entries , * and then on the j entries * * not yet working!!! */ void ijvSortij(ijvType *ijv_orig) { int *perm; int e; ijvType *ijv_temp; perm = CountingSort(ijv_orig->i, ijv_orig->nnz, ijv_orig->n); ijv_temp = makeIJV(ijv_orig->nnz); ijv_temp->n = ijv_orig->n; for (e=0; e < ijv_orig->nnz; e++){ ijv_temp->i[perm[e]] = ijv_orig->i[e]; ijv_temp->j[perm[e]] = ijv_orig->j[e]; ijv_temp->v[perm[e]] = ijv_orig->v[e]; } cFree(perm); perm = CountingSort(ijv_temp->j, ijv_temp->nnz, ijv_temp->n); for (e=0; e < ijv_temp->nnz; e++){ ijv_orig->i[perm[e]] = ijv_temp->i[e]; ijv_orig->j[perm[e]] = ijv_temp->j[e]; ijv_orig->v[perm[e]] = ijv_temp->v[e]; } cFree(perm); }
/** * \ingroup ijv * */ void freeIJV(ijvType *ijv) { int i; if (ijv->i) cFree(ijv->i); if (ijv->j) cFree(ijv->j); if (ijv->v) cFree(ijv->v); cFree(ijv); }
/** * \ingroup ijv * \brief sort an ijv on the j entries * * */ void ijvSortj(ijvType *ijv) { int *perm; ijvType *ijv_temp; int e; ijv_temp = makeIJV(ijv->nnz); ijv_temp->n = ijv->n; perm = CountingSort(ijv->j, ijv->nnz, ijv->n); for (e=0; e < ijv->nnz; e++){ ijv_temp->i[perm[e]] = ijv->i[e]; ijv_temp->j[perm[e]] = ijv->j[e]; ijv_temp->v[perm[e]] = ijv->v[e]; } if (perm) cFree(perm); for (e=0; e < ijv->nnz; e++){ ijv->i[e] = ijv_temp->i[e]; ijv->j[e] = ijv_temp->j[e]; ijv->v[e] = ijv_temp->v[e]; } freeIJV(ijv_temp); }
void cMutexDestroyPort(MutexT *mt) { if(mt && mt->mutex) { pthread_mutex_destroy((pthread_mutex_t*)mt->mutex); pthread_mutexattr_destroy(&g_mutex_attr); cFree((void*)mt->mutex); mt->mutex = NULL; } }
void cBufFree(CBufferT *mem) { if(!mem) return; if(mem->buffer) { cFree(mem->buffer); mem->buffer = NULL; } mem->size = 0; mem->usedLen = 0; }
/** * \ingroup ijv * \brief removed duplicate edges in an IJV * sorts its input, and returns input sorted * with j major */ ijvType *compressIJV(ijvType *ijvIn) { char *newEntry; int x; ijvType *ijvOut; int prevI, prevJ; int count; ijvSorti(ijvIn); ijvSortj(ijvIn); newEntry = (char *) cCalloc(ijvIn->nnz, sizeof(char),"compressIJV"); /* note all set to 0 */ prevI = -1; prevJ = -1; count = 0; for (x = 0; x < ijvIn->nnz; x++) { /* skip until get a new one */ while ((x < ijvIn->nnz) && ((ijvIn->i[x] == prevI) && (ijvIn->j[x] == prevJ))) x++; if (x < ijvIn->nnz) { count++; newEntry[x] = 1; prevI = ijvIn->i[x]; prevJ = ijvIn->j[x]; } } ijvOut = makeIJV(count); ijvOut->n = ijvIn->n; int ind; ind = 0; for (x = 0; x < ijvIn->nnz; x++) if (newEntry[x]) { ijvOut->i[ind] = ijvIn->i[x]; ijvOut->j[ind] = ijvIn->j[x]; ijvOut->v[ind] = ijvIn->v[x]; ind++; } cFree(newEntry); return ijvOut; }
int * CountingSort(int * A, int n, int k){ int *perm, *C; int i; C = cCalloc(k, sizeof(int),""); perm = cCalloc(n, sizeof(int),""); /* for (i = 0; i< k; i++){ C[i] = 0; } */ /* for (i = 0; i< n; i++){ C[A[i]] = C[A[i]] + 1; } */ int limit; limit = 3*(n/3); register int a1, a2, a3; for (i = 0; i< limit; i+= 3){ a1 = A[i]; a2 = A[i+1]; a3 = A[i+2]; C[a1]++; C[a2]++; C[a3]++; } if( i < n ) { switch( n - i ) { case 2 : C[A[i++]]++; case 1 : C[A[i++]]++; } } for (i = 1; i< k; i++){ C[i] = C[i] + C[i-1]; } register int b1; for (i = n-1; i>-1; i--){ b1 = A[i]; perm[i] = C[b1] - 1; C[b1]--; } if (C) cFree(C); return perm; }
void freeIjvBlockDiagNotPart(ijvBlockDiag *bd) { int i; if (bd) { if (bd->blocks) for (i = 0; i < bd->part->numParts; i++) freeIJV(bd->blocks[i]); cFree(bd); } }
bool cIOCopy(const char *aDstFile, const char *aSrcFile) { Int32 srcFd = -1; Int32 dstFd = -1; Int32 fileSize = -1; BYTE *buf = NULL; bool isOK = false; srcFd = cIOOpen(aSrcFile, MADA_OF_READ); if(srcFd <= 0) { goto done; } fileSize = cIOSize(srcFd); if(fileSize < 0) { goto done; } buf = cMalloc(fileSize); if(NULL == buf) { goto done; } if(cIORead(srcFd, buf, fileSize) != fileSize) { goto done; } dstFd = cIOOpen(aDstFile, MADA_OF_CREATE | MADA_OF_WRITE | MADA_OF_TRUNC); if(dstFd <= 0) { goto done; } if(cIOWrite(dstFd, buf, fileSize) != fileSize) { goto done; } isOK = true; done: if(NULL != buf) { cFree(buf); } if(srcFd > 0) { cIOClose(srcFd); } if(dstFd > 0) { cIOClose(dstFd); } return isOK; }
void freePart(partType *p) { if (p != NULL) { if (p->compVec) cFree(p->compVec); if (p->compMap) cFree(p->compMap); if (p->partSizes) cFree(p->partSizes); if (p->partMap) cFree(p->partMap); if (p->partMapBlock) cFree(p->partMapBlock); cFree(p); } }
bool cBufExpand(CBufferT *mem, int size) { char* tmp; if(!mem) return false; if((int)mem->size >= size) return true; tmp = mem->buffer; mem->buffer = (char*)cMalloc(size); if(!mem->buffer) { mem->buffer = tmp; return false; } cMemset(mem->buffer, 0, size); if(tmp != NULL) { cMemcpy(mem->buffer, tmp, mem->size); cFree(tmp); } mem->size = size; return true; }
/** * \relatesalso myGraph * */ void freeGraph(myGraph *G) { int i; for (i = 0; i < G->n; i++) { G->nbrs[i] = NULL; G->wts[i] = NULL; } if (G->nbrs) cFree(G->nbrs); if (G->wts) cFree(G->wts); if (G->back) cFree(G->back); if (G->deg) cFree(G->deg); if (G->nnz > 0) { cFree(G->backBlock); cFree(G->wtsBlock); cFree(G->nbrsBlock); } cFree(G); }
int * CountingSortOld(int * A, int n, int k){ int *perm, *C; int i; C = cCalloc(k, sizeof(int),""); perm = cCalloc(n, sizeof(int),""); /* not needed as calloc makes 0 for (i = 0; i< k; i++){ C[i] = 0; } */ /* replace with below for (i = 0; i< n; i++){ C[A[i]] = C[A[i]] + 1; } */ for (i = 0; i< n; i++){ C[A[i]]++; } for (i = 1; i< k; i++){ C[i] = C[i] + C[i-1]; } for (i = n-1; i>-1; i--){ perm[i] = C[A[i]] - 1; C[A[i]] = C[A[i]] -1; } if (C) cFree(C); return perm; }
ijvType *st_part(ijvType *ijv, int *opts) { ijvType *ijvOut; int *comp; ijvBlockDiag *bd; struct tms t1 ; struct tms t2 ; if (!(opts[stOptNumTree] == stOptValTreeYes) && (opts[stOptNumTime2])) { wtime_bridges = XTIMER(); } /* debug stuff */ if (opts[stOptNumDebug]) { fprintf(stderr,"debug, n: %d, nnz: %d\n",ijv->n,ijv->nnz); } /* if it is a tree, return original */ if (ijv->nnz == ((ijv->n)-1)) { return copyIJV(ijv); } /* DAS: 10/30/04: alter the balance) */ int saven; saven = ijv->n; ijv->n = (int) (ijv->n * metisBalance); if (opts[stOptNumTime]) { printf("metis start\n"); wtime_metis = XTIMER(); } comp = (int *) cCalloc(ijv->n, sizeof(int),"comp in mex_metis"); if (opts[stOptNumTree] == stOptValTreeYes) st_metis(ijv, 2, comp, opts[stOptNumMetis]); else st_metis(ijv, opts[stOptNumParts], comp, opts[stOptNumMetis]); (void) times (&t2) ; if (opts[stOptNumTime]) { wtime_metis = XTIMER() - wtime_metis; printf("metis time: %10.3f seconds\n",wtime_metis); opts[stOptNumTime] = 0; } ijv->n = saven; /* modifies comp */ bd = st_rePart(ijv, comp); int numParts; numParts = bd->part->numParts; /* for each part, recursively compute a tree */ ijvType **trees; trees = (ijvType **) cCalloc(numParts, sizeof(ijvType *), "trees in st_part"); int i; int *treeOpts; if (opts[stOptNumTree] == stOptValTreeYes) treeOpts = opts; else { treeOpts = copyOpts(opts); treeOpts[stOptNumTree] = stOptValTreeYes; } for (i = 0; i < numParts; i++) { if (bd->part->partSizes[i] > 0) { trees[i] = st_part(bd->blocks[i],treeOpts); } else { trees[i] = NULL; } } if (opts[stOptNumTree] != stOptValTreeYes) cFree(treeOpts); ijvType *meta, *metaSparse; meta = st_metaGraph(ijv, bd->part->compVec); int numTrees; numTrees = numParts; /* handling the meta graph */ ijvType *metaSparse1, *metaSparse2; metaSparse1 = NULL; metaSparse2 = NULL; if (opts[stOptNumTree] == stOptValTreeYes) { metaSparse = st_part(meta,opts); freeIJV(meta); } else { if (opts[stOptNumSpars] == stOptValSparsNo) metaSparse = meta; else { if (opts[stOptNumSpars] == stOptValSparsPrim) { if (opts[stOptNumDebug] || opts[stOptNumData]) { fprintf(stderr,"multi-Prim on meta, n: %d, nnz: %d\n",meta->n,meta->nnz); } myGraph *G; G = ijv2graph(meta); metaSparse1 = multi_Prim (G, opts[stOptNumSparsParam]); freeGraph(G); if (opts[stOptNumDebug] || opts[stOptNumData]) { fprintf(stderr,"Multi-Prim output: metaSparse1, n: %d, nnz: %d\n",metaSparse1->n,metaSparse1->nnz); } } if (opts[stOptNumSparsMinDeg] || opts[stOptNumSparsMinFracDeg] || opts[stOptNumSparsMinFracWt]) { if (opts[stOptNumDebug] || opts[stOptNumData]) { fprintf(stderr,"Min Degrees on meta, n: %d, nnz: %d\n",meta->n,meta->nnz); } myGraph *G; G = ijv2graph(meta); metaSparse2 = vertexWise_Sampling(G,opts[stOptNumSparsMinDeg], sparsMinFracDeg, sparsMinFracWt); freeGraph(G); if (opts[stOptNumDebug] || opts[stOptNumData]) { fprintf(stderr,"Min Degrees output: metaSparse2, n: %d, nnz: %d\n",metaSparse2->n,metaSparse2->nnz); } } if (!metaSparse2) metaSparse = metaSparse1; else if (!metaSparse1) { metaSparse = compressIJV(metaSparse2); freeIJV(metaSparse2); if (opts[stOptNumDebug] || opts[stOptNumData]) { fprintf(stderr,"Min Degrees output: metaSparse, n: %d, nnz: %d\n",metaSparse->n,metaSparse->nnz); } } else { /* both sparse1 and sparse2 exist */ ijvType *both[2]; both[0] = metaSparse1; both[1] = metaSparse2; ijvType *metaSparse12; metaSparse12 = mergeIJVs(meta->n, both, 2); freeIJV(metaSparse1); freeIJV(metaSparse2); metaSparse = compressIJV(metaSparse12); freeIJV(metaSparse12); if (opts[stOptNumDebug] || opts[stOptNumData]) { fprintf(stderr,"Combined sparsifier: metaSparse, n: %d, nnz: %d\n",metaSparse->n,metaSparse->nnz); } } } } ijvType **ijvArray; if (!(opts[stOptNumTree] == stOptValTreeYes) && (opts[stOptNumTime2])) { wtime_offdiags = XTIMER(); } ijvArray = st_offDiags(ijv, bd->part, metaSparse); if (!(opts[stOptNumTree] == stOptValTreeYes) && (opts[stOptNumTime2])) { wtime_offdiags = XTIMER() - wtime_offdiags; printf("offdiags time: %10.3f seconds\n",wtime_offdiags); } if (!(opts[stOptNumTree] == stOptValTreeYes) && (opts[stOptNumTime2])) { wtime_bridges = XTIMER(); } ijvType *ijvBridges; ijvBridges = buildBridges(ijv->n, bd->part, ijvArray, metaSparse, trees); if (!(opts[stOptNumTree] == stOptValTreeYes) && (opts[stOptNumTime2])) { wtime_bridges = XTIMER() - wtime_bridges; printf("bridges time: %10.3f seconds\n",wtime_bridges); } if (!ijvBridges) { fprintf(stderr,"error in st_partTree\n"); printIJV(ijv); fprintf(stderr,"\n COMP: \n"); printIntVec(bd->part->compVec,ijv->n,"compnew"); printIntVec(comp,ijv->n,"compOrig"); for (i = 0; i < numParts; i++) { fprintf(stderr,"block %d\n",i); printIJV(bd->blocks[i]); fprintf(stderr,"\ntree %d\n",i); printIJV(trees[i]); } return NULL; } cFree(comp); if (!(opts[stOptNumTree] == stOptValTreeYes) && (opts[stOptNumTime2])) { wtime_merge = XTIMER(); } /* now, merge the trees with the bridges, first, allocating space for the new array of pointers, and then performing the merge before we merge, we need to re-map the vertex labels in the trees */ ijvType **treesAndBridges; treesAndBridges = (ijvType **) cCalloc(numTrees+1, sizeof(ijvType *), "treesAndBridges in mex_partTreeB"); /* issue is that some trees may be null, so we don't put those on this list */ int ind; int numGoodTrees; ind = 0; for (i = 0; i < numTrees; i++) { if (bd->part->partSizes[i] > 0) { remapIJV(trees[i], bd->part, i); treesAndBridges[ind++] = trees[i]; } } numGoodTrees = ind; treesAndBridges[numGoodTrees] = ijvBridges; ijvOut = mergeIJVs(ijv->n, treesAndBridges, numGoodTrees+1); /* ijvSorti(ijvOut); */ ijvSortij(ijvOut); if (!(opts[stOptNumTree] == stOptValTreeYes) && (opts[stOptNumTime2])) { wtime_merge = XTIMER() - wtime_merge; printf("merge time: %10.3f seconds\n",wtime_merge); } freeIjvBlockDiagAndPart(bd); for (i = 0; i < numTrees; i++) { if (trees[i]) freeIJV(trees[i]); } cFree(trees); for (i = 0; i < metaSparse->nnz; i++) freeIJV(ijvArray[i]); cFree(ijvArray); freeIJV(metaSparse); freeIJV(ijvBridges); cFree(treesAndBridges); if (!(opts[stOptNumTree] == stOptValTreeYes) && (opts[stOptNumTime2])) { wtime_total = XTIMER() - wtime_total; printf("total time: %10.3f seconds\n",wtime_total); } return ijvOut; }
ijvType *st_partTree(ijvType *ijv, int *opts) { ijvType *ijvOut; int *comp; ijvBlockDiag *bd; /* if it is a tree, return original */ if (ijv->nnz == ((ijv->n)-1)) { return copyIJV(ijv); } comp = (int *) cCalloc(ijv->n, sizeof(int),"comp in mex_metis"); st_metis(ijv, 2, comp); /* modifies comp */ bd = st_rePart(ijv, comp); int numParts; numParts = bd->part->numParts; /* for each part, recursively compute a tree */ ijvType **trees; trees = (ijvType **) cCalloc(numParts, sizeof(ijvType *), "trees in st_partTree"); int i; for (i = 0; i < numParts; i++) { trees[i] = st_partTree(bd->blocks[i]); } ijvType *meta, *metaTree; meta = st_metaGraph(ijv, bd->part->compVec); int numTrees; numTrees = numParts; metaTree = st_partTree(meta); freeIJV(meta); ijvType **ijvArray; ijvArray = st_offDiags(ijv, bd->part, metaTree); ijvType *ijvBridges; ijvBridges = buildBridges(ijv->n, bd->part, ijvArray, metaTree, trees); if (!ijvBridges) { fprintf(stderr,"error in st_partTree\n"); printIJV(ijv); fprintf(stderr,"\n COMP: \n"); printIntVec(bd->part->compVec,ijv->n,"compnew"); printIntVec(comp,ijv->n,"compOrig"); for (i = 0; i < numParts; i++) { fprintf(stderr,"block %d\n",i); printIJV(bd->blocks[i]); fprintf(stderr,"\ntree %d\n",i); printIJV(trees[i]); } return NULL; } cFree(comp); /* now, merge the trees with the bridges, first, allocating space for the new array of pointers, and then performing the merge before we merge, we need to re-map the vertex labels in the trees */ ijvType **treesAndBridges; treesAndBridges = (ijvType **) cCalloc(numTrees+1, sizeof(ijvType *), "treesAndBridges in mex_partTreeB"); for (i = 0; i < numTrees; i++) { remapIJV(trees[i], bd->part, i); treesAndBridges[i] = trees[i]; } treesAndBridges[numTrees] = ijvBridges; ijvOut = mergeIJVs(ijv->n, treesAndBridges, numTrees+1); /* ijvSorti(ijvOut); */ ijvSortij(ijvOut); freeIjvBlockDiagAndPart(bd); for (i = 0; i < numTrees; i++) { freeIJV(trees[i]); } cFree(trees); for (i = 0; i < metaTree->nnz; i++) freeIJV(ijvArray[i]); cFree(ijvArray); freeIJV(metaTree); freeIJV(ijvBridges); cFree(treesAndBridges); return ijvOut; }