/** * Based on previously calculated offsets, set indices for a buses and branches. * It is up to the individual bus and branch implementations to store these * values. */ void setIndices(void) { // Construct lists of indices that need to be collected int **bus_index = new int*[p_nBuses]; int **branch_index = new int*[p_nBranches]; int *bus_index_buf = new int[p_nBuses]; int *branch_index_buf = new int[p_nBranches]; int *i_bus_value_buf = new int[p_nBuses]; int *i_branch_value_buf = new int[p_nBranches]; int i, j; // Get offsets for all buses and branches; for (i=0; i<p_nBuses; i++) { bus_index_buf[i] = p_network->getGlobalBusIndex(i); bus_index[i] = &bus_index_buf[i]; } for (i=0; i<p_nBranches; i++) { branch_index_buf[i] = p_network->getGlobalBranchIndex(i); branch_index[i] = &branch_index_buf[i]; } NGA_Gather(g_bus_offsets, i_bus_value_buf, bus_index, p_nBuses); NGA_Gather(g_branch_offsets, i_branch_value_buf, branch_index, p_nBranches); // Offsets are now available. Set indices in all network components int offset, nrows, ncols, idx; for (i=0; i<p_nBuses; i++) { nrows = p_network->getBus(i)->vectorNumElements(); if (nrows > 0) { offset = i_bus_value_buf[i]; for (j=0; j<nrows; j++) { idx = offset+j; p_network->getBus(i)->vectorSetElementIndex(j,idx); } } } for (i=0; i<p_nBranches; i++) { nrows = p_network->getBranch(i)->vectorNumElements(); if (nrows > 0) { offset = i_branch_value_buf[i]; for (j=0; j<nrows; j++) { idx = offset+j; p_network->getBranch(i)->vectorSetElementIndex(j,idx); } } } delete [] bus_index; delete [] branch_index; delete [] bus_index_buf; delete [] branch_index_buf; delete [] i_bus_value_buf; delete [] i_branch_value_buf; // Global arrays are no longer needed so we can get rid of them GA_Destroy(g_bus_offsets); GA_Destroy(g_branch_offsets); }
main(int argc, char **argv) { int rank, nprocs; int g_A, dims[D]={SIZE,SIZE}, *local_A=NULL, *local_G=NULL, **sub_array=NULL, **s_array=NULL; int i, j, value=5; MPI_Init(&argc, &argv); GA_Initialize(); MA_init(C_INT, 1000, 1000); MPI_Comm_rank(MPI_COMM_WORLD, &rank); MPI_Comm_size(MPI_COMM_WORLD, &nprocs); s_array=(int**)malloc(N*sizeof(int*)); for(i=0; i<N; i++) { s_array[i]=(int*)malloc(D*sizeof(int)); for(j=0; j<D; j++) s_array[i][j]=rand()%10; } sub_array=(int**)malloc(N*sizeof(int*)); for(i=0; i<N; i++) { sub_array[i]=(int*)malloc(D*sizeof(int)); for(j=0; j<D; j++) sub_array[i][j]=rand()%10; } for(i=0; i<N; i++) local_A=(int*)malloc(N*sizeof(int)); for(i=0; i<N; i++) local_G=(int*)malloc(N*sizeof(int)); g_A=NGA_Create(C_INT, D, dims, "array_A", NULL); GA_Fill(g_A, &value); GA_Sync(); NGA_Scatter(g_A, local_A, s_array, N); NGA_Gather(g_A, local_G, s_array, N); GA_Sync(); GA_Print(g_A); if(rank==0) { for(i=0; i<N; i++) if(local_G[i]!=local_A[i]) printf("GA Error: \n"); } GA_Sync(); if(rank==0) GA_PRINT_MSG(); GA_Terminate(); MPI_Finalize(); return 0; }
main(int argc, char **argv) { int rank, nprocs; int g_A, dims[D]={5,10}, local_A[N], local_G[N], **sub_array=NULL, **s_array=NULL; int i, j, value=5; MPI_Init(&argc, &argv); GA_Initialize(); MA_init(C_INT, 1000, 1000); MPI_Comm_rank(MPI_COMM_WORLD, &rank); MPI_Comm_size(MPI_COMM_WORLD, &nprocs); s_array=(int**)malloc(N*sizeof(int*)); for(i=0; i<N; i++) { s_array[i]=(int*)malloc(D*sizeof(int)); for(j=0; j<D; j++) s_array[i][j]=rand()%5; } sub_array=(int**)malloc(N*sizeof(int*)); for(i=0; i<N; i++) { sub_array[i]=(int*)malloc(D*sizeof(int)); for(j=0; j<D; j++) sub_array[i][j]=rand()%5; } for(i=0; i<N; i++) //local_A=(int*)malloc(N*sizeof(int)); /* * depends on the value of array ..we can generate the location values in randon * we can also use the if-condition */ // PRINTing all the genrated array for reference for(i=0; i<N; i++) { for(j=0; j<D; j++)printf("%d ",s_array[i][j]); printf("\n"); } printf("\n"); for(i=0; i<N; i++) { for(j=0; j<D; j++)printf("%d ",sub_array[i][j]); printf("\n"); } printf("\n"); for(i=0; i<N; i++)printf("%d \n",local_A[i]=rand()%5+1); // PRINT done - now creating array g_A=NGA_Create(C_INT, D, dims, "array_A", NULL); GA_Fill(g_A, &value); GA_Sync(); NGA_Scatter(g_A, local_A, s_array, N); NGA_Gather(g_A, local_G, s_array, N); GA_Sync(); GA_Print(g_A); for(i=0; i<N; i++)printf("%d \n",local_G[i]); printf("\n"); if(rank==0) { for(i=0; i<N; i++) if(local_G[i]!=local_A[i]) printf("GA Error: \n"); } GA_Sync(); if(rank==0) GA_PRINT_MSG(); GA_Terminate(); MPI_Finalize(); return 0; }
int main( int argc, char **argv ) { int g_a, g_b, i, j, size, size_me; int icnt, idx, jdx, ld; int n=N, type=MT_C_INT, one; int *values, *ptr; int **indices; int dims[2]={N,N}; int lo[2], hi[2]; int heap=3000000, stack=2000000; int me, nproc; int datatype, elements; double *prealloc_mem; MP_INIT(argc,argv); #if 1 GA_INIT(argc,argv); /* initialize GA */ me=GA_Nodeid(); nproc=GA_Nnodes(); if(me==0) { if(GA_Uses_fapi())GA_Error("Program runs with C array API only",1); printf("\nUsing %ld processes\n",(long)nproc); fflush(stdout); } heap /= nproc; stack /= nproc; if(! MA_init(MT_F_DBL, stack, heap)) GA_Error("MA_init failed",stack+heap); /* initialize memory allocator*/ /* Create a regular matrix. */ if(me==0)printf("\nCreating matrix A of size %d x %d\n",N,N); g_a = NGA_Create(type, 2, dims, "A", NULL); if(!g_a) GA_Error("create failed: A",n); /* Fill matrix using scatter routines */ size = N*N; if (size%nproc == 0) { size_me = size/nproc; } else { i = size - size%nproc; size_me = i/nproc; if (me < size%nproc) size_me++; } /* Check that sizes are all okay */ i = size_me; GA_Igop(&i,1,"+"); if (i != size) { GA_Error("Sizes don't add up correctly: ",i); } else if (me==0) { printf("\nSizes add up correctly\n"); } /* Allocate index and value arrays */ indices = (int**)malloc(size_me*sizeof(int*)); values = (int*)malloc(size_me*sizeof(int)); icnt = me; for (i=0; i<size_me; i++) { values[i] = icnt; idx = icnt%N; jdx = (icnt-idx)/N; if (idx >= N || idx < 0) { printf("p[%d] Bogus index i: %d\n",me,idx); } if (jdx >= N || jdx < 0) { printf("p[%d] Bogus index j: %d\n",me,jdx); } indices[i] = (int*)malloc(2*sizeof(int)); (indices[i])[0] = idx; (indices[i])[1] = jdx; icnt += nproc; } /* Scatter values into g_a */ NGA_Scatter(g_a, values, indices, size_me); GA_Sync(); /* Check to see if contents of g_a are correct */ NGA_Distribution( g_a, me, lo, hi ); NGA_Access(g_a, lo, hi, &ptr, &ld); for (i=lo[0]; i<hi[0]; i++) { idx = i-lo[0]; for (j=lo[1]; j<hi[1]; j++) { jdx = j-lo[1]; if (ptr[idx*ld+jdx] != j*N+i) { printf("p[%d] (Scatter) expected: %d actual: %d\n",me,j*N+i,ptr[idx*ld+jdx]); } } } if (me==0) printf("\nCompleted test of NGA_Scatter\n"); for (i=0; i<size_me; i++) { values[i] = 0; } GA_Sync(); NGA_Gather(g_a, values, indices, size_me); icnt = me; for (i=0; i<size_me; i++) { if (icnt != values[i]) { printf("p[%d] (Gather) expected: %d actual: %d\n",me,icnt,values[i]); } icnt += nproc; } if (me==0) printf("\nCompleted test of NGA_Gather\n"); GA_Sync(); /* Scatter-accumulate values back into GA*/ one = 1; NGA_Scatter_acc(g_a, values, indices, size_me, &one); GA_Sync(); /* Check to see if contents of g_a are correct */ for (i=lo[0]; i<hi[0]; i++) { idx = i-lo[0]; for (j=lo[1]; j<hi[1]; j++) { jdx = j-lo[1]; if (ptr[idx*ld+jdx] != 2*(j*N+i)) { printf("p[%d] (Scatter_acc) expected: %d actual: %d\n",me,2*(j*N+i),ptr[idx*ld+jdx]); } } } if (me==0) printf("\nCompleted test of NGA_Scatter_acc\n"); NGA_Release(g_a, lo, hi); /* Test fixed buffer size */ NGA_Alloc_gatscat_buf(size_me); /* Scatter-accumulate values back into GA*/ GA_Sync(); NGA_Scatter_acc(g_a, values, indices, size_me, &one); GA_Sync(); /* Check to see if contents of g_a are correct */ for (i=lo[0]; i<hi[0]; i++) { idx = i-lo[0]; for (j=lo[1]; j<hi[1]; j++) { jdx = j-lo[1]; if (ptr[idx*ld+jdx] != 3*(j*N+i)) { printf("p[%d] (Scatter_acc) expected: %d actual: %d\n",me,3*(j*N+i),ptr[idx*ld+jdx]); } } } if (me==0) printf("\nCompleted test of NGA_Scatter_acc using fixed buffers\n"); NGA_Release(g_a, lo, hi); NGA_Free_gatscat_buf(); GA_Destroy(g_a); if(me==0)printf("\nSuccess\n"); GA_Terminate(); #endif MP_FINALIZE(); return 0; }