void SelectSortTest() { int arr[] = {9,1,5,8,3,7,4,6,2}; int length = sizeof(arr)/sizeof(arr[0]); printf("Befor sort:\n"); PrintArr(arr, length); SelectSort(arr, length); printf("After sort:\n"); PrintArr(arr, length); }
int main() { int nArr[10] = {42,1,3,2,16,9,10,14,8,17}; PrintArr(nArr, 10); QuickSort(nArr, 0,9); PrintArr(nArr, 10); system("pause"); return 0; }
int main() { int nArr[10] = {4,1,3,2,16,9,10,14,8,7}; PrintArr(nArr, 10); HeapSort(nArr, 10); PrintArr(nArr, 10); system("pause"); return 0; }
void main(void) { int rows = 4, cols = 3, m = 3, n = 2, t = 4; int A1[MAX_SIZE][MAX_SIZE] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, 11, 12} }; int B1[MAX_SIZE][MAX_SIZE]; int A2[MAX_SIZE][MAX_SIZE] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, 11, 12} }; int B2[MAX_SIZE][MAX_SIZE] = { {0, 1, 2}, {3, 4, 5}, {6, 7, 8}, {9, 10, 11} }; int C2[MAX_SIZE][MAX_SIZE]; int A3[MAX_SIZE][MAX_SIZE] = {{1, 2}, {4, 5}, {3, 6} }; int B3[MAX_SIZE][MAX_SIZE] = {{0, 1, 1, 2}, {4, 0, -1, 3} }; int C3[MAX_SIZE][MAX_SIZE]; /* Test Array Transposition */ printf("\nPress any key to test Array Transpose (按任一鍵執行矩陣轉置) "); getch(); ArrTrans(A1, B1, rows, cols); printf("\n矩陣A =>\n"); PrintArr(A1, rows, cols); printf("矩陣A 的轉置 矩陣B=>\n"); PrintArr(B1, cols, rows); /* Test Array Addition */ printf("\nPress any key to test Array Addition (按任一鍵執行矩陣相加) "); getch(); ArrAdd(A2, B2, C2, rows, cols); printf("\n矩陣A =>\n"); PrintArr(A2, rows, cols); printf("加矩陣B =>\n"); PrintArr(B2, rows, cols); printf("等於矩陣C =>\n"); PrintArr(C2, rows, cols); /* Test Array Multiplication */ printf("\nPress any key to test Array Multiplication (按任一鍵執行矩陣相乘) "); getch(); ArrMul(A3, B3, C3, m, n, t); printf("\n矩陣A =>\n"); PrintArr(A3, m, n); printf("乘矩陣B =>\n"); PrintArr(B3, n, t); printf("等於矩陣C =>\n"); PrintArr(C3, m, t); }
int main() { int nArr[10]; srand(time(NULL)); for (int i = 0; i < 10; i++) { nArr[i] = rand()%100; } PrintArr(nArr, 10); RandomArr(nArr, 10); PrintArr(nArr, 10); system("pause"); return 0; }
void Heapsort( ElementType A[ ], int N ) { int i; PrintArr ( A, N ) ; printf ( "Bulid Heap\n" ); /* 1*/ for( i = N / 2; i >= 0; i-- ) { /* BuildHeap */ printf ( "PercDown %d\n", A[i] ) ; /* 2*/ PercDown( A, i, N ); PrintArr ( A, N ) ; } PrintArr ( A, N ) ; printf ( "Sort Operation\n" ) ; /* 3*/ for( i = N - 1; i > 0; i-- ) { /* 4*/ Swap( &A[ 0 ], &A[ i ] ); /* DeleteMax */ printf ( "After swap( %d, %d )\n", A[ 0 ], A[ i ] ) ; PrintArr ( A, N ) ; printf ( "PercDown %d\n", A[0] ) ; /* 5*/ PercDown( A, 0, i ); PrintArr ( A, N ) ; } }
static int Zoltan_PHG_Redistribute_Hypergraph( ZZ *zz, PHGPartParams *hgp, /* Input: parameters; used only for UseFixedVtx */ HGraph *ohg, /* Input: Local part of distributed hypergraph */ int firstproc, /* Input: rank (in ocomm) of the first proc of the ncomm*/ int *v2Col, /* Input: Vertex to processor Column Mapping */ int *n2Row, /* Input: Net to processor Row Mapping */ PHGComm *ncomm, /* Input: communicators of new distribution */ HGraph *nhg, /* Output: Newly redistributed hypergraph */ int **vmap, /* Output: allocated with the size nhg->nVtx and vertex map from nhg to ohg's local vertex number*/ int **vdest /* Output: allocated with the size nhg->nVtx and stores dest proc in ocomm */ ) { char * yo = "Zoltan_PHG_Redistribute_Hypergraph"; PHGComm *ocomm = ohg->comm; int ierr=ZOLTAN_OK; int i, v, n, nPins, nsend, elemsz, nVtx, nEdge; int msg_tag = 9999; int *proclist=NULL, *sendbuf=NULL; int *vno=NULL, *nno=NULL, *dist_x=NULL, *dist_y=NULL, *vsn=NULL, *nsn=NULL, *pins=NULL, *cnt=NULL; ZOLTAN_COMM_OBJ *plan; Zoltan_HG_HGraph_Init (nhg); nhg->comm = ncomm; nhg->dist_x = (int *) ZOLTAN_CALLOC(ncomm->nProc_x+1, sizeof(int)); nhg->dist_y = (int *) ZOLTAN_CALLOC(ncomm->nProc_y+1, sizeof(int)); dist_x = (int *) ZOLTAN_CALLOC(ncomm->nProc_x+1, sizeof(int)); dist_y = (int *) ZOLTAN_CALLOC(ncomm->nProc_y+1, sizeof(int)); vsn = (int *) ZOLTAN_CALLOC(ncomm->nProc_x+1, sizeof(int)); nsn = (int *) ZOLTAN_CALLOC(ncomm->nProc_y+1, sizeof(int)); vno = (int *) ZOLTAN_MALLOC(ohg->nVtx * sizeof(int)); nno = (int *) ZOLTAN_MALLOC(ohg->nEdge * sizeof(int)); if (!nhg->dist_x || !nhg->dist_y || !dist_x || !dist_y || !vsn || !nsn || (ohg->nVtx && !vno) || (ohg->nEdge && !nno) ) { uprintf(ocomm, " new comm nProcx=%d nProcy=%d nvtx=%d nedge=%d", ncomm->nProc_x, ncomm->nProc_y, ohg->nVtx, ohg->nEdge); MEMORY_ERROR; } for (v = 0; v < ohg->nVtx; ++v) ++dist_x[v2Col[v]]; for (n = 0; n < ohg->nEdge; ++n) ++dist_y[n2Row[n]]; /* UVCUVC: CHECK ASSUMPTION This code assumes that the objects in the receive buffer of Zoltan_Comm_Do function are 1- in the increasing processor order, 2- order of the items send by a processor is preserved. */ /* compute prefix sum to find new vertex start numbers; for each processor */ MPI_Scan(dist_x, vsn, ncomm->nProc_x, MPI_INT, MPI_SUM, ocomm->row_comm); /* All reduce to compute how many each processor will have */ MPI_Allreduce(dist_x, &(nhg->dist_x[1]), ncomm->nProc_x, MPI_INT, MPI_SUM, ocomm->row_comm); nhg->dist_x[0] = 0; for (i=1; i<=ncomm->nProc_x; ++i) nhg->dist_x[i] += nhg->dist_x[i-1]; MPI_Scan(dist_y, nsn, ncomm->nProc_y, MPI_INT, MPI_SUM, ocomm->col_comm); MPI_Allreduce(dist_y, &(nhg->dist_y[1]), ncomm->nProc_y, MPI_INT, MPI_SUM, ocomm->col_comm); nhg->dist_y[0] = 0; for (i=1; i<=ncomm->nProc_y; ++i) nhg->dist_y[i] += nhg->dist_y[i-1]; #ifdef _DEBUG1 PrintArr(ocomm, "vsn", vsn, ncomm->nProc_x); PrintArr(ocomm, "nsn", nsn, ncomm->nProc_y); #endif /* find mapping of current LOCAL vertex no (in my node) to "new" vertex no LOCAL to dest node*/ for (v = ohg->nVtx-1; v>=0; --v) vno[v] = --vsn[v2Col[v]]; for (n = ohg->nEdge-1; n>=0; --n) nno[n] = --nsn[n2Row[n]]; nsend = MAX(MAX(ohg->nPins, ohg->nVtx), ohg->nEdge); elemsz = MAX(MAX(2, ohg->VtxWeightDim), ohg->EdgeWeightDim); elemsz = (sizeof(float)>sizeof(int)) ? sizeof(float)*elemsz : sizeof(int)*elemsz; proclist = (int *) ZOLTAN_MALLOC(nsend * sizeof(int)); sendbuf = (int *) ZOLTAN_MALLOC(nsend * elemsz); /* first communicate pins */ nPins = 0; for (v = 0; v < ohg->nVtx; ++v) { for (i = ohg->vindex[v]; i < ohg->vindex[v+1]; ++i) { #ifdef _DEBUG1 if ((n2Row[ohg->vedge[i]] * ncomm->nProc_x + v2Col[v])<0 || (n2Row[ohg->vedge[i]] * ncomm->nProc_x + v2Col[v])>=ocomm->nProc) errexit("vertex %d vedge[%d]=%d n2Row=%d #Proc_x=%d v2Col=%d", i, ohg->vedge[i], n2Row[ohg->vedge[i]], ncomm->nProc_x , v2Col[v]); #endif proclist[nPins] = firstproc + n2Row[ohg->vedge[i]] * ncomm->nProc_x + v2Col[v]; sendbuf[2*nPins] = vno[v]; sendbuf[2*nPins+1]= nno[ohg->vedge[i]]; ++nPins; } } #ifdef _DEBUG1 if (nPins!=ohg->nPins) { uprintf(ocomm, "sanity check failed nPins(%d)!=hg->nPins(%d)\n", nPins, ohg->nPins); errexit("terminating"); } #endif --msg_tag; ierr |= Zoltan_Comm_Create(&plan, ohg->nPins, proclist, ocomm->Communicator, msg_tag, &nPins); #ifdef _DEBUG1 if (ncomm->myProc==-1 && nPins>1) { /* this processor is not in new comm but receiving data?*/ uprintf(ocomm, "Something wrong; why I'm receiving data nPins=%d\n", nPins); errexit("terminating"); } #endif if (nPins && (pins = (int *) ZOLTAN_MALLOC(nPins * 2 * sizeof(int)))==NULL) MEMORY_ERROR; --msg_tag; Zoltan_Comm_Do(plan, msg_tag, (char *) sendbuf, 2*sizeof(int), (char *) pins); Zoltan_Comm_Destroy(&plan); /* now communicate vertex map */ nsend = 0; if (!ocomm->myProc_y) { /* only first row sends to the first row of ncomm */ for (v = 0; v < ohg->nVtx; ++v) { proclist[nsend] = firstproc+v2Col[v]; sendbuf[nsend++] = ohg->vmap[v]; } } --msg_tag; ierr |= Zoltan_Comm_Create(&plan, nsend, proclist, ocomm->Communicator, msg_tag, &nVtx); #ifdef _DEBUG1 if (ncomm->myProc==-1 && nVtx>1) { /* this processor is not in new comm but receiving data?*/ uprintf(ocomm, "Something wrong; why I'm receiving data nVtx=%d\n", nVtx); errexit("terminating"); } #endif /* those are only needed in the first row of ncomm */ *vmap = *vdest = NULL; if (!ncomm->myProc_y && nVtx && (!(*vmap = (int *) ZOLTAN_MALLOC(nVtx * sizeof(int))) || !(*vdest = (int *) ZOLTAN_MALLOC(nVtx * sizeof(int))))) MEMORY_ERROR; --msg_tag; Zoltan_Comm_Do(plan, msg_tag, (char *) sendbuf, sizeof(int), (char *) *vmap); if (!ocomm->myProc_y) { /* only first row sends to the first row of ncomm */ for (v = 0; v < ohg->nVtx; ++v) sendbuf[v] = ocomm->myProc; } --msg_tag; Zoltan_Comm_Do(plan, msg_tag, (char *) sendbuf, sizeof(int), (char *) *vdest); if (ncomm->myProc!=-1) { /* I'm in the new comm */ /* ncomm's first row now bcast to other rows */ MPI_Bcast(&nVtx, 1, MPI_INT, 0, ncomm->col_comm); #ifdef _DEBUG1 if (nVtx!=(nhg->dist_x[ncomm->myProc_x+1] - nhg->dist_x[ncomm->myProc_x])) errexit("nVtx(%d)!= nhg->dist_x[ncomm->myProc_x+1] - nhg->dist_x[ncomm->myProc_x](%d)", nVtx, nhg->dist_x[ncomm->myProc_x+1] - nhg->dist_x[ncomm->myProc_x]); #endif if (nVtx && (nhg->vmap = (int *) ZOLTAN_MALLOC(nVtx * sizeof(int)))==NULL) MEMORY_ERROR; for (i=0; i<nVtx; ++i) nhg->vmap[i] = i; } /* now communicate vertex weights */ if (ohg->VtxWeightDim) { if (nVtx) nhg->vwgt = (float*) ZOLTAN_MALLOC(nVtx*ohg->VtxWeightDim*sizeof(float)); --msg_tag; Zoltan_Comm_Do(plan, msg_tag, (char *) ohg->vwgt, ohg->VtxWeightDim*sizeof(float), (char *) nhg->vwgt); if (ncomm->myProc!=-1) /* ncomm's first row now bcast to other rows */ MPI_Bcast(nhg->vwgt, nVtx*ohg->VtxWeightDim, MPI_FLOAT, 0, ncomm->col_comm); } /* communicate fixed vertices, if any */ if (hgp->UseFixedVtx) { if (nVtx) nhg->fixed_part = (int *) ZOLTAN_MALLOC(nVtx*sizeof(int)); --msg_tag; Zoltan_Comm_Do(plan, msg_tag, (char *) ohg->fixed_part, sizeof(int), (char *) nhg->fixed_part); if (ncomm->myProc!=-1) /* ncomm's first row now bcast to other rows */ MPI_Bcast(nhg->fixed_part, nVtx, MPI_INT, 0, ncomm->col_comm); } /* communicate pref parts, if any */ if (hgp->UsePrefPart) { if (nVtx) nhg->pref_part = (int *) ZOLTAN_MALLOC(nVtx*sizeof(int)); --msg_tag; Zoltan_Comm_Do(plan, msg_tag, (char *) ohg->pref_part, sizeof(int), (char *) nhg->pref_part); if (ncomm->myProc!=-1) /* ncomm's first row now bcast to other rows */ MPI_Bcast(nhg->pref_part, nVtx, MPI_INT, 0, ncomm->col_comm); } /* this comm plan is no longer needed. */ Zoltan_Comm_Destroy(&plan); if (ohg->EdgeWeightDim) { /* now communicate edge weights */ nsend = 0; if (!ocomm->myProc_x) /* only first column sends to first column of ncomm */ for (n = 0; n < ohg->nEdge; ++n) proclist[nsend++] = firstproc + n2Row[n]*ncomm->nProc_x; --msg_tag; ierr |= Zoltan_Comm_Create(&plan, nsend, proclist, ocomm->Communicator, msg_tag, &nEdge); #ifdef _DEBUG1 if (ncomm->myProc==-1 && nEdge>1) { /* this processor is not in new comm but receiving data?*/ uprintf(ocomm, "Something wrong; why I'm receiving data nEdge=%d\n", nEdge); errexit("terminating"); } #endif if (ncomm->myProc!=-1) { /* if we're in the new comm */ /* ncomm's first column now bcast to other columns */ MPI_Bcast(&nEdge, 1, MPI_INT, 0, ncomm->row_comm); #ifdef _DEBUG1 if (nEdge != (nhg->dist_y[ncomm->myProc_y+1] - nhg->dist_y[ncomm->myProc_y])) errexit("nEdge(%d)!=nhg->dist_y[ncomm->myProc_y+1] - nhg->dist_y[ncomm->myProc_y](%d)", nEdge, nhg->dist_y[ncomm->myProc_y+1] - nhg->dist_y[ncomm->myProc_y]); #endif } if (nEdge) nhg->ewgt = (float*) ZOLTAN_MALLOC(nEdge*ohg->EdgeWeightDim*sizeof(float)); --msg_tag; Zoltan_Comm_Do(plan, msg_tag, (char *) ohg->ewgt, ohg->EdgeWeightDim*sizeof(float), (char *) nhg->ewgt); if (ncomm->myProc!=-1) { /* if we're in the new comm */ /* ncomm's first column now bcast to other columns */ if (nEdge) MPI_Bcast(nhg->ewgt, nEdge*ohg->EdgeWeightDim, MPI_FLOAT, 0, ncomm->row_comm); } Zoltan_Comm_Destroy(&plan); } else nEdge = (ncomm->myProc==-1) ? 0 : nhg->dist_y[ncomm->myProc_y+1] - nhg->dist_y[ncomm->myProc_y]; if (ncomm->myProc==-1) { #ifdef _DEBUG1 if (nPins || nVtx || nEdge) errexit("I should not have any data: hey nPins=%d nVtx=%d nEdge=%d\n", nPins, nVtx, nEdge); #endif nhg->nEdge = nhg->nVtx = nhg->nPins = 0; } else { nhg->nEdge = nhg->dist_y[ncomm->myProc_y+1] - nhg->dist_y[ncomm->myProc_y]; nhg->nVtx = nhg->dist_x[ncomm->myProc_x+1] - nhg->dist_x[ncomm->myProc_x]; nhg->nPins = nPins; /* Unpack the pins received. */ cnt = (int *) ZOLTAN_CALLOC(nhg->nVtx + 1, sizeof(int)); nhg->vindex = (int *) ZOLTAN_CALLOC(nhg->nVtx + 1, sizeof(int)); nhg->vedge = (int *) ZOLTAN_MALLOC(nhg->nPins * sizeof(int)); if (!cnt || !nhg->vindex || (nPins && !nhg->vedge)) MEMORY_ERROR; /* Count the number of pins per vertex */ for (i = 0; i < nPins; ++i) ++cnt[pins[2*i]]; /* Compute prefix sum to represent hindex correctly. */ for (i = 0; i < nhg->nVtx; ++i) { nhg->vindex[i+1] = nhg->vindex[i] + cnt[i]; cnt[i] = nhg->vindex[i]; } for (i = 0; i < nPins; ++i) nhg->vedge[cnt[pins[2*i]]++] = pins[2*i+1]; nhg->info = ohg->info; nhg->VtxWeightDim = ohg->VtxWeightDim; nhg->EdgeWeightDim = ohg->EdgeWeightDim; ierr = Zoltan_HG_Create_Mirror(zz, nhg); if (ierr != ZOLTAN_OK && ierr != ZOLTAN_WARN) MEMORY_ERROR; } End: Zoltan_Multifree(__FILE__, __LINE__, 10, &proclist, &sendbuf, &pins, &cnt, &vno, &nno, &dist_x, &dist_y, &vsn, &nsn ); return ierr; }