Integer util_gnxtval_(Integer *val) { if(*val > 0) { if(!initialized) GA_Error("nxtval: not yet initialized", 0L); return (Integer) NGA_Read_inc(g_T, &subscript, 1); } else if(*val==0) { int n = 1; initialized=1; /* create task array */ GA_Mask_sync(0, 1); g_T = NGA_Create(C_LONG, 1, &n,"Atomic Task", NULL); /* Initialize the task array */ if(GA_Nodeid()==0) { int lo=0, hi=0; NGA_Put (g_T, &lo, &hi, &initval, &hi); initval=0; } GA_Sync(); return 0; } else if (*val < 0) { GA_Destroy(g_T); initialized=0; initval=0; return 0;} GA_Error("nxtval: invalid value passed", 0L); return -1; }
void verify(int g_a, int g_b, int g_c, int *lo, int *hi, int *ld, int N) { double rchk, alpha=1.0, beta=0.0; int g_chk, me=GA_Nodeid(); g_chk = GA_Duplicate(g_a, "array Check"); if(!g_chk) GA_Error("duplicate failed",NDIMS); GA_Sync(); GA_Dgemm('n', 'n', N, N, N, 1.0, g_a, g_b, 0.0, g_chk); GA_Sync(); alpha=1.0, beta=-1.0; GA_Add(&alpha, g_c, &beta, g_chk, g_chk); rchk = GA_Ddot(g_chk, g_chk); if (me==0) { printf("Normed difference in matrices: %12.4e\n", rchk); if(rchk < -TOLERANCE || rchk > TOLERANCE) GA_Error("Matrix multiply verify failed",0); else printf("Matrix Mutiply OK\n"); } GA_Destroy(g_chk); }
/** * Block Topology (of Force Matrix): * Say for example: If there are 4 block and 100 atoms, the size of * the force matrix is 100x100 and each block size is 50x50. * * ----------- * | | | * | 0,0 | 0,1 | * ----------- * | 1,0 | 1,1 | * | | | * ----------- */ int SetupBlocks(AppCtx *user) { int i,j,k=0; int n; int zero = 0; int x_space, g_space; if (user->natoms % user->BlockSize) { GA_Error("Number of atoms should be a multiple of block size. Choose a different block size.", 0L); } n = user->natoms / user->BlockSize; user->nBlocks = n*n; if (user->nBlocks > MAX_BLOCKS) GA_Error("Number of blocks is greater that MAX_BLOCKS: Solution is either to increase the defined MAX_BLOCKS or increase your block size",0L); if (user->nBlocks < user->nproc) GA_Error("Number of blocks should be greater than or equal to the number of processors",0L); for (i=0;i<n;i++) for (j=0;j<n;j++,k++) { user->btopo[k].x = i; user->btopo[k].y = j; } /* Create task array */ n = 1; user->atomicTask = NGA_Create(C_INT, 1, &n, "Atomic Task", NULL); if (!user->atomicTask) GA_Error("NGA_Create failed for Atomic Task",0); if (user->me == 0) NGA_Put(user->atomicTask, &zero, &zero, &user->nproc, &zero); /* space for x values from two processors */ x_space = 2 * user->BlockSize * user->ndim; /* space for ALL gradient value */ g_space = user->natoms * user->ndim; if (MA_push_stack(C_DBL, x_space + g_space+3, "GA LJ bufs", &user->memHandle)) MA_get_pointer(user->memHandle, &user->x1); else GA_Error("ma_alloc_get failed",x_space + g_space); user->x2 = user->x1 + x_space/2 + 1; user->grad = user->x2 + x_space/2 + 1; GA_Sync(); return 0; }
int nw_inp_from_string(Integer rtdb, const char *input) { char filename[30]; FILE *file; #if defined(USE_FCD) || defined(CRAY_T3E) || defined(WIN32) _fcd fstring; #else char fstring[255]; #endif int status; const char base[] = "temp"; const char ending[] = ".nw"; int number ; // This is bad, not 100% sure to be unique, since could be subgroup if (GA_Pgroup_get_world() != GA_Pgroup_get_default()) { number = (int) util_sgroup_mygroup_() ; } else { number = 0 ; } sprintf(filename, "%s%d%s", base,number,ending); if (GA_Nodeid() == 0) { if (!(file = fopen(filename,"w"))) { GA_Error("nw_inp_from_string: failed to open temp.nw\n",0); } if (fwrite(input, 1, strlen(input), file) != strlen(input)) { GA_Error("nw_inp_from_string: failed to write to temp.nw\n",0); } if (fwrite("\n", 1, 1, file) != 1) { GA_Error("nw_inp_from_string: failed to write to temp.nw\n",0); } (void) fclose(file); } #if defined(CRAY_T3E) fstring = _cptofcd(filename, strlen(filename)); status = nw_inp_from_file_(&rtdb, fstring); #elif defined(WIN32) fstring.string = filename; fstring.len = strlen(filename); status = nw_inp_from_file_(&rtdb, fstring); #elif defined(USE_FCD) #error Do something about _fcd #else status = nw_inp_from_file_(&rtdb, filename, strlen(filename)); #endif if (GA_Nodeid() == 0) (void) unlink(filename); return status; }
main(int argc, char **argv) { int rank, nprocs, i, j; int g_A, g_B; int dims[MAX_DIM], val=4, ndim, re; MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD, &rank); MPI_Comm_size(MPI_COMM_WORLD, &nprocs); MA_init(C_INT, 1000, 1000); GA_Initialize(); for(i=1; i<=MAX_DIM; i++) { ndim=i; dims[i]=SIZE; // for(j=0; j<ndim; j++) g_A = NGA_Create(C_INT, ndim, dims, "array_A", NULL); g_B = NGA_Create(C_INT, ndim, dims, "array_B", NULL); if(!g_A) GA_Error("GA Error: no global array exists \n", ndim); if(!g_B) GA_Error("GA Error: no global array exists \n", ndim); } GA_Sync(); GA_Fill(g_A, &val); re=GA_Solve(g_A, g_B); if(re==0) printf("Cholesky Fact is Successful \n"); else if (re >0) printf("Cholesky Fact couldn't be completed \n"); else printf("An Error occured\n"); if(rank == 0) GA_PRINT_MSG(); GA_Destroy(g_A); GA_Destroy(g_B); GA_Terminate(); MPI_Finalize(); }
validate_transpose(int g_A, int g_B, int* lo, int* hi, int ld) { int i, j; int local_A[SIZE][SIZE], local_B[SIZE][SIZE]; // int **local_A=NULL, **local_B=NULL; /* local_A=(int**)malloc(SIZE*sizeof(int*)); for(i=0; i<SIZE; i++) local_value[i]=(int*)malloc(SIZE*sizeof(int)); local_B=(int**)malloc(SIZE*sizeof(int*)); for(i=0; i<SIZE; i++) local_value[i]=(int*)malloc(SIZE*sizeof(int)); */ NGA_Get(g_A, lo, hi, local_A, &ld); NGA_Get(g_B, lo, hi, local_B, &ld); for(i=1; i<SIZE; i++) { for(j=1; j<SIZE; j++) { if(local_B[j][i]!=local_A[i][j]) GA_Error("ERROR : in passing values", DIM); } } }
void FATR ga_antisymmetrize_(Integer *g_a) { DoublePrecision alpha = 0.5; int i, me = GA_Nodeid(); extern void * FATR ga_malloc(Integer nelem, int type, char *name); extern void FATR ga_free(void *ptr); void FATR gai_subtr(int *lo, int *hi, void *a, void *b, DoublePrecision alpha, int type, Integer nelem, int ndim); int alo[GA_MAX_DIM], ahi[GA_MAX_DIM], lda[GA_MAX_DIM]; int blo[GA_MAX_DIM], bhi[GA_MAX_DIM], ldb[GA_MAX_DIM]; int ndim, dims[GA_MAX_DIM], type; Integer nelem=1; Logical have_data; void *a_ptr, *b_ptr; GA_Sync(); NGA_Inquire((int)(*g_a), &type, &ndim, dims); if (dims[0] != dims[1]) GA_Error("ga_sym: can only sym square matrix", 0L); /* Find the local distribution */ NGA_Distribution((int)(*g_a), me, alo, ahi); have_data = ahi[0]>=0; for(i=1; i<ndim; i++) have_data = have_data && ahi[i]>=0; if(have_data) { NGA_Access((int)(*g_a), alo, ahi, &a_ptr, lda); for(i=0; i<ndim; i++) nelem *= ahi[i]-alo[i] +1; b_ptr = (void *) ga_malloc(nelem, MT_C_DBL, "v"); for(i=2; i<ndim; i++) {bhi[i]=ahi[i]; blo[i]=alo[i]; } /* switch rows and cols */ blo[1]=alo[0]; bhi[1]=ahi[0]; blo[0]=alo[1]; bhi[0]=ahi[1]; for (i=0; i < ndim-1; i++) ldb[i] = bhi[i+1] - blo[i+1] + 1; NGA_Get((int)(*g_a), blo, bhi, b_ptr, ldb); } GA_Sync(); if(have_data) { gai_subtr(alo, ahi, a_ptr, b_ptr, alpha, type, nelem, ndim); NGA_Release_update((int)(*g_a), alo, ahi); ga_free(b_ptr); } GA_Sync(); }
/* * Return the no. of bytes that n ints=Integers occupy */ Integer FATR util_mitob_(Integer *n) { if (*n < 0) GA_Error("util_MITOB_: negative argument",*n); return (Integer) (*n * sizeof(Integer)); }
/* * Check to see if inversion is correct. Start by copying g_a into local * buffer a, and g_b into local buffer b. */ void verify(int g_a, int g_b) { int i, type, ndim, dims[MAXDIM], lo[MAXDIM], hi[MAXDIM], ld[MAXDIM]; int a[MAXPROC*TOTALELEMS],b[MAXPROC*TOTALELEMS]; /* Get dimensions of GA */ NGA_Inquire(g_a, &type, &ndim, dims); lo[0] = 0; hi[0] = dims[0]-1; /* ### copy the block of data described by the arrays "lo" and "hi" from * ### the global array "g_a" into the local array "a". Copy the same block * ### of data from "g_b" into the local array "b". Use the array of strides * ### "ld" to describe the physical layout of "a" and "b". */ NGA_Get(g_a,lo,hi,a,ld); NGA_Get(g_b,lo,hi,b,ld); for(i=0; i<dims[0]; i++) if (a[i] != b[dims[0]-i-1]) { printf("Mismatch: a[%d]=%d is not equal to b[%d]=%d\n", i, a[i], dims[0]-i-1, b[dims[0]-i-1]); GA_Error("verify failed",0); } printf(" Transpose OK\n"); }
int main(int argc, char **argv) { int heap=300000, stack=300000; int me, nprocs, i; double start, end; /* Initialize Message Passing library */ MPI_Init(&argc, &argv); /* Initialize GA */ GA_Initialize(); /* Initialize Memory Allocator (MA) */ if(! MA_init(C_DBL, stack, heap) ) GA_Error("MA_init failed",stack+heap); me = GA_Nodeid(); nprocs = GA_Nnodes(); if(me==0) { printf("\nUsing %d processes\n\n", nprocs); fflush(stdout); } start = MPI_Wtime(); for(i =0; i< 1000; i++) { TRANSPOSE1D(); } end = MPI_Wtime(); if(me==0) printf(" Time=%2.5e secs\n\n",end-start); if(me==0)printf("\nTerminating ..\n"); GA_Terminate(); MPI_Finalize(); }
void FATR util_fadvise_noreuse_(const char *fort_fname, int flen){ char buf[1024]; if (!fortchar_to_string(fort_fname, flen, buf, sizeof(buf))) GA_Error("util_fadvise: fortchar_to_string failed for fname",0); (void) FATR util_fadvise(buf, POSIX_FADV_NOREUSE); }
int main(int argc, char **argv) { int status, me; int max_arrays = 10; double max_sz = 1e8, max_disk = 1e10, max_mem = 1e6; int stack = 120000, heap = 3200000; int numfiles, numioprocs; int total, nproc; MP_INIT(argc,argv); GA_Initialize(); me = GA_Nodeid(); nproc = GA_Nnodes(); total = pow(SIZE,NDIM)*sizeof(double); if (!GA_Uses_ma()) { if (GA_Nodeid() == 0) { printf("GA is not using MA\n"); } stack = 100000; heap = (int)(2.2*(float)(total)); } if (MA_init(MT_F_DBL, stack, heap) ) { if (DRA_Init(max_arrays, max_sz, max_disk, max_mem) != 0) GA_Error("DRA_Init failed: ",0); if (USER_CONFIG == 0) { numfiles = -1; numioprocs = -1; } else if (USER_CONFIG == 1) { numfiles = 1; numioprocs = GA_Cluster_nnodes(); } else if (USER_CONFIG == 2) { numfiles = GA_Cluster_nnodes(); numioprocs = GA_Cluster_nnodes(); } else { numfiles = 1; numioprocs = 1; } if (me==0) { printf("Disk resident arrays configured as:\n"); printf(" Number of files: %d\n",numfiles); printf(" Number of I/O processors: %d\n",numioprocs); } DRA_Set_default_config(numfiles,numioprocs); if (me == 0) printf("\n"); if (me == 0) printf("TESTING PERFORMANCE OF DISK ARRAYS\n"); if (me == 0) printf("\n"); test_io_dbl(); status = DRA_Terminate(); GA_Terminate(); } else { printf("MA_init failed\n"); } if(me == 0) printf("all done ...\n"); MP_FINALIZE(); return 0; }
static void Error(char *string, int integer) { /* (void) fflush(stdout); (void) fprintf(stderr,"\n\nError was called.\n"); (void) fprintf(stderr,string); (void) fprintf(stderr," %d (%#x).\n",integer,integer); exit(1); */ GA_Error(string, (long) integer); }
void FATR UTIL_FILE_UNLINK(_fcd input) { int lin = _fcdlen(input); #else void util_file_unlink_(const char *input, int lin) { #endif char in[255]; if (!fortchar_to_string(input, lin, in, sizeof(in))) GA_Error("util_file_unlink: fortchar_to_string failed for in",0); util_file_unlink(in); }
int util_mic_get_device_() { int count; if (!offload_master_()) { fprintf(stdout, "%02d: need to be offload master\n", GA_Nodeid()); GA_Error("util_mic_get_device error", 0L); } count = util_cgetppn()/util_mic_get_num_devices_(); if (count > 0) { count=util_my_smp_index()/util_nwc_ranks_per_device_(); } return count; }
void util_file_unlink(const char *filename) /* Delete the file. If the file does not exist, quietly return. If the file exists and the unlink fails then abort. */ { /* if (access(filename, F_OK) == 0) { if (unlink(filename)) { fprintf(stderr,"util_file_unlink: failed unlinking %s\n", filename); GA_Error("util_file_unlink",0); } } */ if (EAF_Delete(filename) != 0) GA_Error("util_file_unlink",0); }
int main(int argc, char **argv) { int proc, nprocs; int M, N, K; /* */ int blockX_len, blockY_len; int heap=3000000, stack=3000000; if (argc == 6) { M = atoi(argv[1]); N = atoi(argv[2]); K = atoi(argv[3]); blockX_len = atoi(argv[4]); blockY_len = atoi(argv[5]); } else { printf("Please enter ./a.out <M> <N> <K> <BLOCK-X-LEN> <BLOCK-Y-LEN>"); exit(-1); } MPI_Init(&argc, &argv); GA_Initialize(); if(! MA_init(C_DBL, stack, heap) ) GA_Error("MA_init failed",stack+heap); proc = GA_Nodeid(); nprocs = GA_Nnodes(); if(proc == 0) { printf("Using %d processes\n", nprocs); fflush(stdout); } matrix_multiply(M, N, K, blockX_len, blockY_len); if(proc == 0) printf("\nTerminating ..\n"); GA_Terminate(); MPI_Finalize(); return 0; }
void lu(double *A, int matrix_size) { double *a=NULL, *a_verify=NULL; int info; a = (double*)malloc(matrix_size*matrix_size*sizeof(double)); a_verify = (double*)malloc(matrix_size*matrix_size*sizeof(double)); if(a == NULL || a_verify == NULL) { GA_Error("lu(): malloc failed", matrix_size*matrix_size*sizeof(double)); } copy_array(A, a, matrix_size); copy_array(A, a_verify, matrix_size); #if 0 printf("\nDoing LU Factorization\n\n"); lu_basic(a, matrix_size); printf("LU = \n"); print_array(a, matrix_size); printf("\nDoing LAPACK's LU Factorization\n\n"); #endif info = lu_lapack(a_verify, matrix_size); #if DEBUG printf("LU = "); print_array(a_verify, matrix_size); #endif if(info!=0) { printf("\nError: dgetrf() of lapack is NOT successful (INFO=%d)\n\n", info); printf("NOTE:\n INFO=0: successful exit\n INFO<0: if INFO = -i, the i-th argument had an illegal value\n INFO>0: if INFO = i, U(i,i) is exactly zero. The factorization\n has been completed, but the factor U is exactly singular, and\n division by zero will occur if it is used to solve a system of\n equations.\n"); exit(0); } free(a_verify); free(a); }
int main(int argc, char **args) { PetscErrorCode ierr; ierr = PetscInitialize(&argc, &args, (char *) 0, ""); CHKERRQ(ierr); ierr = PetscPrintf(PETSC_COMM_WORLD, "Start %s\n", __FILE__); CHKERRQ(ierr); GA_Initialize(); ierr = MA_init(MT_DBL, 1000, 1000 ); if( !ierr ) GA_Error("\n\n\nga allocaltion failed\n\n",ierr); size_t len = 1024; char name[1024]; PetscGetHostName(name,len); PetscPrintf(PETSC_COMM_SELF, "Hello from %s\n", name); ierr = testCreate3D(); CHKERRQ(ierr); GA_Terminate(); ierr = PetscPrintf(PETSC_COMM_WORLD, "End %s\n", __FILE__); CHKERRQ(ierr); ierr = PetscFinalize(); CHKERRQ(ierr); }
int main(int argc, char **argv) { int heap=300000, stack=300000; int me, nprocs; /* Step1: Initialize Message Passing library */ #ifdef MPI MPI_Init(&argc, &argv); /* initialize MPI */ #else PBEGIN_(argc, argv); /* initialize TCGMSG */ #endif /* Step2: Initialize GA */ /* ### intialize the GA library */ GA_Initialize(); /* Step3: Initialize Memory Allocator (MA) */ if(! MA_init(C_DBL, stack, heap) ) GA_Error("MA_init failed",stack+heap); /* ### assign the local processor ID to the int variable "me" * ### and the total number of processors to the int variable * ### "nprocs" */ me = GA_Nodeid(); nprocs = GA_Nnodes(); if(me==0) { printf("\nUsing %d processes\n\n", nprocs); fflush(stdout); } TRANSPOSE1D(); if(me==0)printf("\nTerminating ..\n"); /* ### terminate the GA library */ GA_Terminate(); #ifdef MPI MPI_Finalize(); #else PEND_(); #endif }
/*\ fill n-dimensional array section with value \*/ void fill_patch(double *ptr, int dim[], int ld[], int ndim, double val) { int i, j, stride=1; switch (ndim){ case 0: GA_Error("fill_patch: error",ndim); case 1: for(i=0;i <dim[0];i++)ptr[i]=val; break; case 2: for(i=0; i< dim[0]; i++){ double *arr = ptr + i*ld[0]; for(j=0; j< dim[1]; j++)arr[j]=val; } break; default: for(i=0; i<ndim-1; i++)stride *=ld[i]; for(i=0; i<dim[0]; i++){ double *arr = ptr + stride*i; fill_patch(arr, dim+1, ld+1, ndim-1, val); } } }
int main(int argc, char **argv) { Integer heap=9000000, stack=9000000; int me, nproc; DoublePrecision time; MP_INIT(argc,argv); GA_INIT(argc,argv); /* initialize GA */ nproc = GA_Nnodes(); me = GA_Nodeid(); if(me==0) printf("Using %d processes\n\n",nproc); if (me==0) printf ("Matrix size is %d X %d\n",N,N); #ifdef USE_REGULAR if (me == 0) printf("\nUsing regular data distribution\n\n"); #endif #ifdef USE_SIMPLE_CYCLIC if (me == 0) printf("\nUsing simple block-cyclic data distribution\n\n"); #endif #ifdef USE_SCALAPACK if (me == 0) printf("\nUsing ScaLAPACK data distribution\n\n"); #endif #ifdef USE_TILED if (me == 0) printf("\nUsing tiled data distribution\n\n"); #endif if(!MA_init((Integer)MT_F_DBL, stack/nproc, heap/nproc)) GA_Error("MA_init failed bytes= %d",stack+heap); #ifdef PERMUTE { int i, *list = (int*)malloc(nproc*sizeof(int)); if(!list)GA_Error("malloc failed",nproc); for(i=0; i<nproc;i++)list[i]=nproc-1-i; GA_Register_proclist(list, nproc); free(list); } #endif if(GA_Uses_fapi())GA_Error("Program runs with C API only",1); time = MP_TIMER(); do_work(); /* printf("%d: Total Time = %lf\n", me, MP_TIMER()-time); printf("%d: GEMM Total Time = %lf\n", me, gTime); */ if(me==0)printf("\nSuccess\n\n"); GA_Terminate(); MP_FINALIZE(); return 0; }
void test_io_dbl() { int n, ndim = NDIM; double err, tt0, tt1, mbytes; int g_a, g_b, d_a; int i, itmp, j, req, loop; int glo[MAXDIM],ghi[MAXDIM]; dra_size_t dlo[MAXDIM],dhi[MAXDIM]; dra_size_t ddims[MAXDIM],reqdims[MAXDIM]; dra_size_t m; int index[MAXDIM], dims[MAXDIM]; int me, nproc, isize; double *ptr; double plus, minus; int ld[MAXDIM], chunk[MAXDIM]; char filename[80]; FILE *fd; n = SIZE; m = ((dra_size_t)NFACTOR)*((dra_size_t)SIZE); loop = 1; for (i=0; i<ndim; i++) loop *= NFACTOR; req = -1; nproc = GA_Nnodes(); me = GA_Nodeid(); if (me == 0) { printf("Creating temporary global arrays %d",n); for (i=1; i<ndim; i++) { printf(" x %d",n); } printf("\n"); } if (me == 0) fflush(stdout); GA_Sync(); for (i=0; i<ndim; i++) { dims[i] = n; chunk[i] = 1; } g_a = NGA_Create(MT_DBL, ndim, dims, "a", chunk); if (!g_a) GA_Error("NGA_Create failed: a", 0); g_b = NGA_Create(MT_DBL, ndim, dims, "b", chunk); if (!g_b) GA_Error("NGA_Create failed: b", 0); if (me == 0) printf("done\n"); if (me == 0) fflush(stdout); /* initialize g_a, g_b with random values ... use ga_access to avoid allocating local buffers for ga_put */ GA_Sync(); NGA_Distribution(g_a, me, glo, ghi); NGA_Access(g_a, glo, ghi, &ptr, ld); isize = 1; for (i=0; i<ndim; i++) isize *= (ghi[i]-glo[i]+1); fill_random(ptr, isize); GA_Sync(); GA_Zero(g_b); /*.......................................................................*/ if (me == 0) { printf("Creating Disk array %ld",m); for (i=1; i<ndim; i++) { printf(" x %ld",m); } printf("\n"); } if (me == 0) fflush(stdout); for (i=0; i<ndim; i++) { ddims[i] = m; reqdims[i] = (dra_size_t)n; } GA_Sync(); strcpy(filename,FNAME); if (! (fd = fopen(filename, "w"))) { strcpy(filename,FNAME_ALT); if (! (fd = fopen(filename, "w"))) { GA_Error("open failed",0); } } fclose(fd); if (NDRA_Create(MT_DBL, ndim, ddims, "A", filename, DRA_RW, reqdims, &d_a) != 0) { GA_Error("NDRA_Create failed(d_a): ",0); } if (me == 0) printf("testing write\n"); fflush(stdout); tt1 = 0.0; for (i=0; i<loop; i++) { itmp=i; for (j=0; j<ndim; j++) { index[j] = itmp%NFACTOR; itmp = (itmp - index[j])/NFACTOR; } for (j=0; j<ndim; j++) { glo[j] = 0; ghi[j] = SIZE - 1; dlo[j] = ((dra_size_t)index[j])*((dra_size_t)SIZE); dhi[j] = (((dra_size_t)index[j])+(dra_size_t)1) * ((dra_size_t)SIZE) - (dra_size_t)1; } tt0 = MP_TIMER(); if (NDRA_Write_section(FALSE, g_a, glo, ghi, d_a, dlo, dhi, &req) != 0) { GA_Error("ndra_write_section failed:",0); } if (DRA_Wait(req) != 0) { GA_Error("DRA_Wait failed(d_a): ",req); } tt1 += (MP_TIMER() - tt0); } GA_Dgop(&tt1,1,"+"); tt1 = tt1/((double)nproc); mbytes = 1.e-6 * (double)(pow(m,ndim)*sizeof(double)); if (me == 0) { printf("%11.2f MB time = %11.2f rate = %11.3f MB/s\n", mbytes,tt1,mbytes/tt1); } if (DRA_Close(d_a) != 0) { GA_Error("DRA_Close failed(d_a): ",d_a); } if (me == 0) printf("\n"); if (me == 0) printf("disk array closed\n"); if (me == 0) fflush(stdout); /*..........................................................*/ if (me == 0) printf("\n"); if (me == 0) printf("opening disk array\n"); if (DRA_Open(filename, DRA_R, &d_a) != 0) { GA_Error("DRA_Open failed",0); } if (me == 0) printf("testing read\n"); /* printf("testing read on proc %d\n",me); */ if (me == 0) fflush(stdout); tt1 = 0.0; for (i=0; i<loop; i++) { itmp=i; for (j=0; j<ndim; j++) { index[j] = itmp%NFACTOR; itmp = (itmp - index[j])/NFACTOR; } for (j=0; j<ndim; j++) { glo[j] = 0; ghi[j] = SIZE - 1; dlo[j] = ((dra_size_t)index[j])*((dra_size_t)SIZE); dhi[j] = (((dra_size_t)index[j])+(dra_size_t)1) * ((dra_size_t)SIZE) - (dra_size_t)1; } tt0 = MP_TIMER(); if (NDRA_Read_section(FALSE, g_b, glo, ghi, d_a, dlo, dhi, &req) != 0) { GA_Error("ndra_read_section failed:",0); } if (DRA_Wait(req) != 0) { GA_Error("DRA_Wait failed(d_a): ",req); } tt1 += (MP_TIMER() - tt0); plus = 1.0; minus = -1.0; GA_Add(&plus, g_a, &minus, g_b, g_b); err = GA_Ddot(g_b, g_b); if (err != 0) { if (me == 0) { printf("BTW, we have error = %f on loop value %d\n", err,i); } GA_Error(" bye",0); } } GA_Dgop(&tt1,1,"+"); tt1 = tt1/((double)nproc); if (me == 0) { printf("%11.2f MB time = %11.2f rate = %11.3f MB/s\n", mbytes,tt1,mbytes/tt1); } if (DRA_Delete(d_a) != 0) GA_Error("DRA_Delete failed",0); /*.......................................................................*/ GA_Destroy(g_a); GA_Destroy(g_b); }
int main(int argc, char **argv) { int rank, nprocs; int g_A; int *local_A=NULL, *local_B=NULL, *output_A=NULL; int dims[DIM]={SIZE,SIZE}, dims2[DIM], lo[DIM]={SIZE-SIZE,SIZE-SIZE}, hi[DIM]={SIZE-1,SIZE-1}, ld=SIZE; int value=SIZE; #if defined(USE_ELEMENTAL) // initialize Elemental (which will initialize MPI) ElInitialize( &argc, &argv ); ElMPICommRank( MPI_COMM_WORLD, &rank ); ElMPICommSize( MPI_COMM_WORLD, &nprocs ); // instantiate el::global array ElGlobalArraysConstruct_i( &eliga ); // initialize global arrays ElGlobalArraysInitialize_i( eliga ); #else MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD, &rank); MPI_Comm_size(MPI_COMM_WORLD, &nprocs); MA_init(C_INT, 1000, 1000); GA_Initialize(); #endif local_A=(int*)malloc(SIZE*SIZE*sizeof(int)); output_A=(int*)malloc(SIZE*SIZE*sizeof(int)); memset (output_A, 0, SIZE*SIZE*sizeof(int)); for(int j=0; j<SIZE; j++) for(int i=0; i<SIZE; i++) local_A[i+j*ld]=(i + j); //for(int i=0; i<SIZE; i++) local_A[i+j*ld]=(rand()%10); local_B=(int*)malloc(SIZE*SIZE*sizeof(int)); memset (local_B, 0, SIZE*SIZE*sizeof(int)); // nb handle #if defined(USE_ELEMENTAL) typedef ElInt ga_nbhdl_t; #endif ga_nbhdl_t nbnb; #if defined(USE_ELEMENTAL) ElGlobalArraysCreate_i( eliga, DIM, dims, "array_A", NULL, &g_A ); ElGlobalArraysFill_i( eliga, g_A, &value ); #else g_A = NGA_Create(C_INT, DIM, dims, "array_A", NULL); GA_Fill(g_A, &value); #endif if (rank == 0) printf ("Initial global array:\n"); #if defined(USE_ELEMENTAL) ElGlobalArraysPrint_i( eliga, g_A ); #else GA_Print(g_A); #endif for (int i = 0; i < NITERS; i++) { // acc data #if defined(USE_ELEMENTAL) ElGlobalArraysNBAccumulate_i( eliga, g_A, lo, hi, local_A, &ld, &value, &nbnb ); #else NGA_NbAcc(g_A, lo, hi, local_A, &ld, &value, &nbnb); #endif // updated output MPI_Reduce (local_A, output_A, SIZE*SIZE, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD); #if defined(USE_ELEMENTAL) ElGlobalArraysNBWait_i( eliga, &nbnb ); #else NGA_NbWait (&nbnb); #endif // get if (rank == 0) printf ("Get in iter #%d\n", i); #if defined(USE_ELEMENTAL) ElGlobalArraysSync_i( eliga ); ElGlobalArraysGet_i( eliga, g_A, lo, hi, local_B, &ld ); ElGlobalArraysPrint_i( eliga, g_A ); #else GA_Sync(); NGA_Get(g_A, lo, hi, local_B, &ld); GA_Print(g_A); #endif } // end of iters if(rank==0) { printf(" Alpha (multiplier): %d\n", value); printf(" Original local buffer (before accumulation): \n"); for(int i=0; i<SIZE; i++) { for(int j=0; j<SIZE; j++) printf("%d ", local_A[i*ld+j]); printf("\n"); } printf("\n"); printf(" Get returns: \n"); for(int i=0; i<SIZE; i++) { for(int j=0; j<SIZE; j++) printf("%d ", local_B[i*ld + j]); printf("\n"); } printf("\n"); for(int i=0; i<SIZE; i++) { for(int j=0; j<SIZE; j++) { if(local_B[i*ld+j]!=(value + (NITERS * value * (output_A[i*ld+j])))) GA_Error("ERROR", -99); } } } #if defined(USE_ELEMENTAL) ElGlobalArraysDestroy_i( eliga, g_A ); #else GA_Destroy(g_A); #endif if(rank == 0) printf ("OK. Test passed\n"); free (local_A); free (local_B); free (output_A); #if defined(USE_ELEMENTAL) ElGlobalArraysTerminate_i( eliga ); // call el::global arrays destructor ElGlobalArraysDestruct_i( eliga ); ElFinalize(); #else GA_Terminate(); MPI_Finalize(); #endif }
void test(int data_type) { int me=GA_Nodeid(); int nproc = GA_Nnodes(); int g_a, g_b, g_c; int ndim = 2; int dims[2]={N,N}; int lo[2]={0,0}; int hi[2]={N-1,N-1}; int block_size[2]={NB,NB-1}; int proc_grid[2]; int i,j,l,k,m,n, ld; double alpha_dbl = 1.0, beta_dbl = 0.0; double dzero = 0.0; double ddiff; float alpha_flt = 1.0, beta_flt = 0.0; float fzero = 0.0; float fdiff; float ftmp; double dtmp; SingleComplex ctmp; DoubleComplex ztmp; DoubleComplex alpha_dcpl = {1.0, 0.0} , beta_dcpl = {0.0, 0.0}; DoubleComplex zzero = {0.0,0.0}; DoubleComplex zdiff; SingleComplex alpha_scpl = {1.0, 0.0} , beta_scpl = {0.0, 0.0}; SingleComplex czero = {0.0,0.0}; SingleComplex cdiff; void *alpha=NULL, *beta=NULL; void *abuf=NULL, *bbuf=NULL, *cbuf=NULL, *c_ptr=NULL; switch (data_type) { case C_FLOAT: alpha = (void *)&alpha_flt; beta = (void *)&beta_flt; abuf = (void*)malloc(N*N*sizeof(float)); bbuf = (void*)malloc(N*N*sizeof(float)); cbuf = (void*)malloc(N*N*sizeof(float)); if(me==0) printf("Single Precision: Testing GA_Sgemm,NGA_Matmul_patch for %d-Dimension", ndim); break; case C_DBL: alpha = (void *)&alpha_dbl; beta = (void *)&beta_dbl; abuf = (void*)malloc(N*N*sizeof(double)); bbuf = (void*)malloc(N*N*sizeof(double)); cbuf = (void*)malloc(N*N*sizeof(double)); if(me==0) printf("Double Precision: Testing GA_Dgemm,NGA_Matmul_patch for %d-Dimension", ndim); break; case C_DCPL: alpha = (void *)&alpha_dcpl; beta = (void *)&beta_dcpl; abuf = (void*)malloc(N*N*sizeof(DoubleComplex)); bbuf = (void*)malloc(N*N*sizeof(DoubleComplex)); cbuf = (void*)malloc(N*N*sizeof(DoubleComplex)); if(me==0) printf("Double Complex: Testing GA_Zgemm,NGA_Matmul_patch for %d-Dimension", ndim); break; case C_SCPL: alpha = (void *)&alpha_scpl; beta = (void *)&beta_scpl; abuf = (void*)malloc(N*N*sizeof(SingleComplex)); bbuf = (void*)malloc(N*N*sizeof(SingleComplex)); cbuf = (void*)malloc(N*N*sizeof(SingleComplex)); if(me==0) printf("Single Complex: Testing GA_Cgemm,NGA_Matmul_patch for %d-Dimension", ndim); break; default: GA_Error("wrong data type", data_type); } if (me==0) printf("\nCreate A, B, C\n"); #ifdef USE_REGULAR g_a = NGA_Create(data_type, ndim, dims, "array A", NULL); #endif #ifdef USE_SIMPLE_CYCLIC g_a = NGA_Create_handle(); NGA_Set_data(g_a,ndim,dims,data_type); NGA_Set_array_name(g_a,"array A"); NGA_Set_block_cyclic(g_a,block_size); if (!GA_Allocate(g_a)) { GA_Error("Failed: create: g_a",40); } #endif #ifdef USE_SCALAPACK g_a = NGA_Create_handle(); NGA_Set_data(g_a,ndim,dims,data_type); NGA_Set_array_name(g_a,"array A"); grid_factor(nproc,&i,&j); proc_grid[0] = i; proc_grid[1] = j; NGA_Set_block_cyclic_proc_grid(g_a,block_size,proc_grid); if (!GA_Allocate(g_a)) { GA_Error("Failed: create: g_a",40); } #endif #ifdef USE_TILED g_a = NGA_Create_handle(); NGA_Set_data(g_a,ndim,dims,data_type); NGA_Set_array_name(g_a,"array A"); grid_factor(nproc,&i,&j); proc_grid[0] = i; proc_grid[1] = j; NGA_Set_tiled_proc_grid(g_a,block_size,proc_grid); if (!GA_Allocate(g_a)) { GA_Error("Failed: create: g_a",40); } #endif g_b = GA_Duplicate(g_a, "array B"); g_c = GA_Duplicate(g_a, "array C"); if(!g_a || !g_b || !g_c) GA_Error("Create failed: a, b or c",1); ld = N; if (me==0) printf("\nInitialize A\n"); /* Set up matrix A */ if (me == 0) { for (i=0; i<N; i++) { for (j=0; j<N; j++) { switch (data_type) { case C_FLOAT: ((float*)abuf)[i*N+j] = (float)(i*N+j); break; case C_DBL: ((double*)abuf)[i*N+j] = (double)(i*N+j); break; case C_DCPL: ((DoubleComplex*)abuf)[i*N+j].real = (double)(i*N+j); ((DoubleComplex*)abuf)[i*N+j].imag = 1.0; break; case C_SCPL: ((SingleComplex*)abuf)[i*N+j].real = (float)(i*N+j); ((SingleComplex*)abuf)[i*N+j].imag = 1.0; break; default: GA_Error("wrong data type", data_type); } } } NGA_Put(g_a,lo,hi,abuf,&ld); } GA_Sync(); if (me==0) printf("\nInitialize B\n"); /* Set up matrix B */ if (me == 0) { for (i=0; i<N; i++) { for (j=0; j<N; j++) { switch (data_type) { case C_FLOAT: ((float*)bbuf)[i*N+j] = (float)(j*N+i); break; case C_DBL: ((double*)bbuf)[i*N+j] = (double)(j*N+i); break; case C_DCPL: ((DoubleComplex*)bbuf)[i*N+j].real = (double)(j*N+i); ((DoubleComplex*)bbuf)[i*N+j].imag = 1.0; break; case C_SCPL: ((SingleComplex*)bbuf)[i*N+j].real = (float)(j*N+i); ((SingleComplex*)bbuf)[i*N+j].imag = 1.0; break; default: GA_Error("wrong data type", data_type); } } } NGA_Put(g_b,lo,hi,bbuf,&ld); } GA_Sync(); if (me==0) printf("\nPerform matrix multiply\n"); switch (data_type) { case C_FLOAT: NGA_Matmul_patch('N','N',&alpha_flt,&beta_flt,g_a,lo,hi, g_b,lo,hi,g_c,lo,hi); break; case C_DBL: NGA_Matmul_patch('N','N',&alpha_dbl,&beta_dbl,g_a,lo,hi, g_b,lo,hi,g_c,lo,hi); break; case C_SCPL: NGA_Matmul_patch('N','N',&alpha_scpl,&beta_scpl,g_a,lo,hi, g_b,lo,hi,g_c,lo,hi); break; case C_DCPL: NGA_Matmul_patch('N','N',&alpha_dcpl,&beta_dcpl,g_a,lo,hi, g_b,lo,hi,g_c,lo,hi); break; default: GA_Error("wrong data type", data_type); } GA_Sync(); #if 0 if (me==0) printf("\nCheck answer\n"); /* GA_Print(g_a); if (me == 0) printf("\n\n\n\n"); GA_Print(g_b); if (me == 0) printf("\n\n\n\n"); GA_Print(g_c); */ /* Check answer */ NGA_Get(g_a,lo,hi,abuf,&ld); NGA_Get(g_b,lo,hi,bbuf,&ld); for (i=0; i<N; i++) { for (j=0; j<N; j++) { switch (data_type) { case C_FLOAT: ((float*)cbuf)[i*N+j] = fzero; break; case C_DBL: ((double*)cbuf)[i*N+j] = dzero; break; case C_DCPL: ((DoubleComplex*)cbuf)[i*N+j] = zzero; break; case C_SCPL: ((SingleComplex*)cbuf)[i*N+j] = czero; break; default: GA_Error("wrong data type", data_type); } for (k=0; k<N; k++) { switch (data_type) { case C_FLOAT: ((float*)cbuf)[i*N+j] += ((float*)abuf)[i*N+k] *((float*)bbuf)[k*N+j]; break; case C_DBL: ((double*)cbuf)[i*N+j] += ((double*)abuf)[i*N+k] *((double*)bbuf)[k*N+j]; break; case C_DCPL: ((DoubleComplex*)cbuf)[i*N+j].real += (((DoubleComplex*)abuf)[i*N+k].real *((DoubleComplex*)bbuf)[k*N+j].real -(((DoubleComplex*)abuf)[i*N+k].imag *((DoubleComplex*)bbuf)[k*N+j].imag)); ((DoubleComplex*)cbuf)[i*N+j].imag += (((DoubleComplex*)abuf)[i*N+k].real *((DoubleComplex*)bbuf)[k*N+j].imag +(((DoubleComplex*)abuf)[i*N+k].imag *((DoubleComplex*)bbuf)[k*N+j].real)); break; case C_SCPL: ((SingleComplex*)cbuf)[i*N+j].real += (((SingleComplex*)abuf)[i*N+k].real *((SingleComplex*)bbuf)[k*N+j].real -(((SingleComplex*)abuf)[i*N+k].imag *((SingleComplex*)bbuf)[k*N+j].imag)); ((SingleComplex*)cbuf)[i*N+j].imag += (((SingleComplex*)abuf)[i*N+k].real *((SingleComplex*)bbuf)[k*N+j].imag +(((SingleComplex*)abuf)[i*N+k].imag *((SingleComplex*)bbuf)[k*N+j].real)); break; default: GA_Error("wrong data type", data_type); } } } } GA_Sync(); if (me == 0) { NGA_Get(g_c,lo,hi,abuf,&ld); for (i=0; i<N; i++) { for (j=0; j<N; j++) { switch (data_type) { case C_FLOAT: fdiff = ((float*)abuf)[i*N+j]-((float*)cbuf)[i*N+j]; if (((float*)abuf)[i*N+j] != 0.0) { fdiff /= ((float*)abuf)[i*N+j]; } if (fabs(fdiff) > TOLERANCE) { printf("p[%d] [%d,%d] Actual: %f Expected: %f\n",me,i,j, ((float*)abuf)[i*N+j],((float*)cbuf)[i*N+j]); } break; case C_DBL: ddiff = ((double*)abuf)[i*N+j]-((double*)cbuf)[i*N+j]; if (((double*)abuf)[i*N+j] != 0.0) { ddiff /= ((double*)abuf)[i*N+j]; } if (fabs(ddiff) > TOLERANCE) { printf("p[%d] [%d,%d] Actual: %f Expected: %f\n",me,i,j, ((double*)abuf)[i*N+j],((double*)cbuf)[i*N+j]); } break; case C_DCPL: zdiff.real = ((DoubleComplex*)abuf)[i*N+j].real -((DoubleComplex*)cbuf)[i*N+j].real; zdiff.imag = ((DoubleComplex*)abuf)[i*N+j].imag -((DoubleComplex*)cbuf)[i*N+j].imag; if (((DoubleComplex*)abuf)[i*N+j].real != 0.0 || ((DoubleComplex*)abuf)[i*N+j].imag != 0.0) { ztmp = ((DoubleComplex*)abuf)[i*N+j]; ddiff = sqrt((zdiff.real*zdiff.real+zdiff.imag*zdiff.imag) /(ztmp.real*ztmp.real+ztmp.imag*ztmp.imag)); } else { ddiff = sqrt(zdiff.real*zdiff.real+zdiff.imag*zdiff.imag); } if (fabs(ddiff) > TOLERANCE) { printf("p[%d] [%d,%d] Actual: (%f,%f) Expected: (%f,%f)\n",me,i,j, ((DoubleComplex*)abuf)[i*N+j].real, ((DoubleComplex*)abuf)[i*N+j].imag, ((DoubleComplex*)cbuf)[i*N+j].real, ((DoubleComplex*)cbuf)[i*N+j].imag); } break; case C_SCPL: cdiff.real = ((SingleComplex*)abuf)[i*N+j].real -((SingleComplex*)cbuf)[i*N+j].real; cdiff.imag = ((SingleComplex*)abuf)[i*N+j].imag -((SingleComplex*)cbuf)[i*N+j].imag; if (((SingleComplex*)abuf)[i*N+j].real != 0.0 || ((SingleComplex*)abuf)[i*N+j].imag != 0.0) { ctmp = ((SingleComplex*)abuf)[i*N+j]; fdiff = sqrt((cdiff.real*cdiff.real+cdiff.imag*cdiff.imag) /(ctmp.real*ctmp.real+ctmp.imag*ctmp.imag)); } else { fdiff = sqrt(cdiff.real*cdiff.real+cdiff.imag*cdiff.imag); } if (fabs(fdiff) > TOLERANCE) { printf("p[%d] [%d,%d] Actual: (%f,%f) Expected: (%f,%f)\n",me,i,j, ((SingleComplex*)abuf)[i*N+j].real, ((SingleComplex*)abuf)[i*N+j].imag, ((SingleComplex*)cbuf)[i*N+j].real, ((SingleComplex*)cbuf)[i*N+j].imag); } break; default: GA_Error("wrong data type", data_type); } } } } GA_Sync(); /* copy cbuf back to g_a */ if (me == 0) { NGA_Put(g_a,lo,hi,cbuf,&ld); } GA_Sync(); /* Get norm of g_a */ switch (data_type) { case C_FLOAT: ftmp = GA_Fdot(g_a,g_a); break; case C_DBL: dtmp = GA_Ddot(g_a,g_a); break; case C_DCPL: ztmp = GA_Zdot(g_a,g_a); break; case C_SCPL: ctmp = GA_Cdot(g_a,g_a); break; default: GA_Error("wrong data type", data_type); } /* subtract C from A and put the results in B */ beta_flt = -1.0; beta_dbl = -1.0; beta_scpl.real = -1.0; beta_dcpl.real = -1.0; GA_Zero(g_b); GA_Add(alpha,g_a,beta,g_c,g_b); /* evaluate the norm of the difference between the two matrices */ switch (data_type) { case C_FLOAT: fdiff = GA_Fdot(g_b, g_b); if (ftmp != 0.0) { fdiff /= ftmp; } if(fabs(fdiff) > TOLERANCE) { printf("\nabs(result) = %f > %f\n", fabsf(fdiff), TOLERANCE); GA_Error("GA_Sgemm Failed", 1); } else if (me == 0) { printf("\nGA_Sgemm OK\n\n"); } break; case C_DBL: ddiff = GA_Ddot(g_b, g_b); if (dtmp != 0.0) { ddiff /= dtmp; } if(fabs(ddiff) > TOLERANCE) { printf("\nabs(result) = %f > %f\n", fabsf(ddiff), TOLERANCE); GA_Error("GA_Dgemm Failed", 1); } else if (me == 0) { printf("\nGA_Dgemm OK\n\n"); } break; case C_DCPL: zdiff = GA_Zdot(g_b, g_b); if (ztmp.real != 0.0 || ztmp.imag != 0.0) { ddiff = sqrt((zdiff.real*zdiff.real+zdiff.imag*zdiff.imag) /(ztmp.real*ztmp.real+ztmp.imag*ztmp.imag)); } else { ddiff = sqrt(zdiff.real*zdiff.real+zdiff.imag*zdiff.imag); } if(fabs(ddiff) > TOLERANCE) { printf("\nabs(result) = %f > %f\n", fabsf(zdiff.real), TOLERANCE); GA_Error("GA_Zgemm Failed", 1); } else if (me == 0) { printf("\nGA_Zgemm OK\n\n"); } break; case C_SCPL: cdiff = GA_Cdot(g_b, g_b); if (ctmp.real != 0.0 || ctmp.imag != 0.0) { fdiff = sqrt((cdiff.real*cdiff.real+cdiff.imag*cdiff.imag) /(ctmp.real*ctmp.real+ctmp.imag*ctmp.imag)); } else { fdiff = sqrt(cdiff.real*cdiff.real+cdiff.imag*cdiff.imag); } if(fabs(fdiff) > TOLERANCE) { printf("\nabs(result) = %f > %f\n", fabsf(cdiff.real), TOLERANCE); GA_Error("GA_Cgemm Failed", 1); } else if (me == 0) { printf("\nGA_Cgemm OK\n\n"); } break; default: GA_Error("wrong data type", data_type); } #endif free(abuf); free(bbuf); free(cbuf); switch (data_type) { case C_FLOAT: abuf = (void*)malloc(N*N*sizeof(float)/4); bbuf = (void*)malloc(N*N*sizeof(float)/4); cbuf = (void*)malloc(N*N*sizeof(float)/4); break; case C_DBL: abuf = (void*)malloc(N*N*sizeof(double)/4); bbuf = (void*)malloc(N*N*sizeof(double)/4); cbuf = (void*)malloc(N*N*sizeof(double)/4); break; case C_DCPL: abuf = (void*)malloc(N*N*sizeof(DoubleComplex)/4); bbuf = (void*)malloc(N*N*sizeof(DoubleComplex)/4); cbuf = (void*)malloc(N*N*sizeof(DoubleComplex)/4); break; case C_SCPL: abuf = (void*)malloc(N*N*sizeof(SingleComplex)/4); bbuf = (void*)malloc(N*N*sizeof(SingleComplex)/4); cbuf = (void*)malloc(N*N*sizeof(SingleComplex)/4); break; default: GA_Error("wrong data type", data_type); } /* Test multiply on a fraction of matrix. Start by reinitializing * A and B */ GA_Zero(g_a); GA_Zero(g_b); GA_Zero(g_c); if (me==0) printf("\nTest patch multiply\n"); lo[0] = N/4; lo[1] = N/4; hi[0] = 3*N/4-1; hi[1] = 3*N/4-1; ld = N/2; /* Set up matrix A */ if (me==0) printf("\nInitialize A\n"); if (me == 0) { for (i=N/4; i<3*N/4; i++) { for (j=N/4; j<3*N/4; j++) { switch (data_type) { case C_FLOAT: ((float*)abuf)[(i-N/4)*N/2+(j-N/4)] = (float)(i*N+j); break; case C_DBL: ((double*)abuf)[(i-N/4)*N/2+(j-N/4)] = (double)(i*N+j); break; case C_DCPL: ((DoubleComplex*)abuf)[(i-N/4)*N/2+(j-N/4)].real = (double)(i*N+j); ((DoubleComplex*)abuf)[(i-N/4)*N/2+(j-N/4)].imag = 1.0; break; case C_SCPL: ((SingleComplex*)abuf)[(i-N/4)*N/2+(j-N/4)].real = (float)(i*N+j); ((SingleComplex*)abuf)[(i-N/4)*N/2+(j-N/4)].imag = 1.0; break; default: GA_Error("wrong data type", data_type); } } } NGA_Put(g_a,lo,hi,abuf,&ld); } GA_Sync(); if (me==0) printf("\nInitialize B\n"); /* Set up matrix B */ if (me == 0) { for (i=N/4; i<3*N/4; i++) { for (j=N/4; j<3*N/4; j++) { switch (data_type) { case C_FLOAT: ((float*)bbuf)[(i-N/4)*N/2+(j-N/4)] = (float)(j*N+i); break; case C_DBL: ((double*)bbuf)[(i-N/4)*N/2+(j-N/4)] = (double)(j*N+i); break; case C_DCPL: ((DoubleComplex*)bbuf)[(i-N/4)*N/2+(j-N/4)].real = (double)(j*N+i); ((DoubleComplex*)bbuf)[(i-N/4)*N/2+(j-N/4)].imag = 1.0; break; case C_SCPL: ((SingleComplex*)bbuf)[(i-N/4)*N/2+(j-N/4)].real = (float)(j*N+i); ((SingleComplex*)bbuf)[(i-N/4)*N/2+(j-N/4)].imag = 1.0; break; default: GA_Error("wrong data type", data_type); } } } NGA_Put(g_b,lo,hi,bbuf,&ld); } GA_Sync(); beta_flt = 0.0; beta_dbl = 0.0; beta_scpl.real = 0.0; beta_dcpl.real = 0.0; if (me==0) printf("\nPerform matrix multiply on sub-blocks\n"); switch (data_type) { case C_FLOAT: NGA_Matmul_patch('N','N',&alpha_flt,&beta_flt,g_a,lo,hi, g_b,lo,hi,g_c,lo,hi); break; case C_DBL: NGA_Matmul_patch('N','N',&alpha_dbl,&beta_dbl,g_a,lo,hi, g_b,lo,hi,g_c,lo,hi); break; case C_SCPL: NGA_Matmul_patch('N','N',&alpha_scpl,&beta_scpl,g_a,lo,hi, g_b,lo,hi,g_c,lo,hi); break; case C_DCPL: NGA_Matmul_patch('N','N',&alpha_dcpl,&beta_dcpl,g_a,lo,hi, g_b,lo,hi,g_c,lo,hi); break; default: GA_Error("wrong data type", data_type); } GA_Sync(); #if 0 if (0) { /* if (data_type != C_SCPL && data_type != C_DCPL) { */ if (me==0) printf("\nCheck answer\n"); /* Multiply buffers by hand */ if (me == 0) { for (i=0; i<N/2; i++) { for (j=0; j<N/2; j++) { switch (data_type) { case C_FLOAT: ((float*)cbuf)[i*N/2+j] = fzero; break; case C_DBL: ((double*)cbuf)[i*N/2+j] = dzero; break; case C_DCPL: ((DoubleComplex*)cbuf)[i*N/2+j] = zzero; break; case C_SCPL: ((SingleComplex*)cbuf)[i*N/2+j] = czero; break; default: GA_Error("wrong data type", data_type); } for (k=0; k<N/2; k++) { switch (data_type) { case C_FLOAT: ((float*)cbuf)[i*N/2+j] += ((float*)abuf)[i*N/2+k] *((float*)bbuf)[k*N/2+j]; break; case C_DBL: ((double*)cbuf)[i*N/2+j] += ((double*)abuf)[i*N/2+k] *((double*)bbuf)[k*N/2+j]; break; case C_DCPL: ((DoubleComplex*)cbuf)[i*N/2+j].real += (((DoubleComplex*)abuf)[i*N/2+k].real *((DoubleComplex*)bbuf)[k*N/2+j].real -(((DoubleComplex*)abuf)[i*N/2+k].imag *((DoubleComplex*)bbuf)[k*N/2+j].imag)); ((DoubleComplex*)cbuf)[i*N/2+j].imag += (((DoubleComplex*)abuf)[i*N/2+k].real *((DoubleComplex*)bbuf)[k*N/2+j].imag +(((DoubleComplex*)abuf)[i*N/2+k].imag *((DoubleComplex*)bbuf)[k*N/2+j].real)); break; case C_SCPL: ((SingleComplex*)cbuf)[i*N/2+j].real += (((SingleComplex*)abuf)[i*N/2+k].real *((SingleComplex*)bbuf)[k*N/2+j].real -(((SingleComplex*)abuf)[i*N/2+k].imag *((SingleComplex*)bbuf)[k*N/2+j].imag)); ((SingleComplex*)cbuf)[i*N/2+j].imag += (((SingleComplex*)abuf)[i*N/2+k].real *((SingleComplex*)bbuf)[k*N/2+j].imag +(((SingleComplex*)abuf)[i*N/2+k].imag *((SingleComplex*)bbuf)[k*N/2+j].real)); break; default: GA_Error("wrong data type", data_type); } } } } NGA_Put(g_a,lo,hi,cbuf,&ld); } if (me == 0) printf("\n\n\n\n"); /* Get norm of g_a */ switch (data_type) { case C_FLOAT: ftmp = NGA_Fdot_patch(g_a,'N',lo,hi,g_a,'N',lo,hi); break; case C_DBL: dtmp = NGA_Ddot_patch(g_a,'N',lo,hi,g_a,'N',lo,hi); break; case C_DCPL: ztmp = NGA_Zdot_patch(g_a,'N',lo,hi,g_a,'N',lo,hi); break; case C_SCPL: ctmp = NGA_Cdot_patch(g_a,'N',lo,hi,g_a,'N',lo,hi); break; default: GA_Error("wrong data type", data_type); } /* subtract C from A and put the results in B */ beta_flt = -1.0; beta_dbl = -1.0; beta_scpl.real = -1.0; beta_dcpl.real = -1.0; NGA_Zero_patch(g_b,lo,hi); NGA_Add_patch(alpha,g_a,lo,hi,beta,g_c,lo,hi,g_b,lo,hi); /* evaluate the norm of the difference between the two matrices */ switch (data_type) { case C_FLOAT: fdiff = NGA_Fdot_patch(g_b,'N',lo,hi,g_b,'N',lo,hi); if (ftmp != 0.0) { fdiff /= ftmp; } if(fabs(fdiff) > TOLERANCE) { printf("\nabs(result) = %f > %f\n", fabsf(fdiff), TOLERANCE); GA_Error("GA_Sgemm Failed", 1); } else if (me == 0) { printf("\nGA_Sgemm OK\n\n"); } break; case C_DBL: ddiff = NGA_Ddot_patch(g_b,'N',lo,hi,g_b,'N',lo,hi); if (dtmp != 0.0) { ddiff /= dtmp; } if(fabs(ddiff) > TOLERANCE) { printf("\nabs(result) = %f > %f\n", fabsf(ddiff), TOLERANCE); GA_Error("GA_Dgemm Failed", 1); } else if (me == 0) { printf("\nGA_Dgemm OK\n\n"); } break; case C_DCPL: zdiff = NGA_Zdot_patch(g_b,'N',lo,hi,g_b,'N',lo,hi); if (ztmp.real != 0.0 || ztmp.imag != 0.0) { ddiff = sqrt((zdiff.real*zdiff.real+zdiff.imag*zdiff.imag) /(ztmp.real*ztmp.real+ztmp.imag*ztmp.imag)); } else { ddiff = sqrt(zdiff.real*zdiff.real+zdiff.imag*zdiff.imag); } if(fabs(ddiff) > TOLERANCE) { printf("\nabs(result) = %f > %f\n", fabsf(zdiff.real), TOLERANCE); GA_Error("GA_Zgemm Failed", 1); } else if (me == 0) { printf("\nGA_Zgemm OK\n\n"); } break; case C_SCPL: cdiff = NGA_Cdot_patch(g_b,'N',lo,hi,g_b,'N',lo,hi); if (ctmp.real != 0.0 || ctmp.imag != 0.0) { fdiff = sqrt((cdiff.real*cdiff.real+cdiff.imag*cdiff.imag) /(ctmp.real*ctmp.real+ctmp.imag*ctmp.imag)); } else { fdiff = sqrt(cdiff.real*cdiff.real+cdiff.imag*cdiff.imag); } if(fabs(fdiff) > TOLERANCE) { printf("\nabs(result) = %f > %f\n", fabsf(cdiff.real), TOLERANCE); GA_Error("GA_Cgemm Failed", 1); } else if (me == 0) { printf("\nGA_Cgemm OK\n\n"); } break; default: GA_Error("wrong data type", data_type); } } #endif free(abuf); free(bbuf); free(cbuf); GA_Destroy(g_a); GA_Destroy(g_b); GA_Destroy(g_c); }
void do_work() { int ONE=1 ; /* useful constants */ int g_a, g_b; int n=N, type=MT_F_DBL; int me=GA_Nodeid(), nproc=GA_Nnodes(); int i, row; int dims[2]={N,N}; int lo[2], hi[2], ld; /* Note: on all current platforms DoublePrecision == double */ double buf[N], err, alpha, beta; if(me==0)printf("Creating matrix A\n"); g_a = NGA_Create(type, 2, dims, "A", NULL); if(!g_a) GA_Error("create failed: A",n); if(me==0)printf("OK\n"); if(me==0)printf("Creating matrix B\n"); /* create matrix B so that it has dims and distribution of A*/ g_b = GA_Duplicate(g_a, "B"); if(! g_b) GA_Error("duplicate failed",n); if(me==0)printf("OK\n"); GA_Zero(g_a); /* zero the matrix */ if(me==0)printf("Initializing matrix A\n"); /* fill in matrix A with random values in range 0.. 1 */ lo[1]=0; hi[1]=n-1; for(row=me; row<n; row+= nproc){ /* each process works on a different row in MIMD style */ lo[0]=hi[0]=row; for(i=0; i<n; i++) buf[i]=sin((double)i + 0.1*(row+1)); NGA_Put(g_a, lo, hi, buf, &n); } if(me==0)printf("Symmetrizing matrix A\n"); GA_Symmetrize(g_a); /* symmetrize the matrix A = 0.5*(A+A') */ /* check if A is symmetric */ if(me==0)printf("Checking if matrix A is symmetric\n"); GA_Transpose(g_a, g_b); /* B=A' */ alpha=1.; beta=-1.; GA_Add(&alpha, g_a, &beta, g_b, g_b); /* B= A - B */ err= GA_Ddot(g_b, g_b); if(me==0)printf("Error=%f\n",(double)err); if(me==0)printf("\nChecking atomic accumulate \n"); GA_Zero(g_a); /* zero the matrix */ for(i=0; i<n; i++) buf[i]=(double)i; /* everybody accumulates to the same location/row */ alpha = 1.0; row = n/2; lo[0]=hi[0]=row; lo[1]=0; hi[1]=n-1; ld = hi[1]-lo[1]+1; NGA_Acc(g_a, lo, hi, buf, &ld, &alpha ); GA_Sync(); if(me==0){ /* node 0 is checking the result */ NGA_Get(g_a, lo, hi, buf,&ld); for(i=0; i<n; i++) if(buf[i] != (double)nproc*i) GA_Error("failed: column=",i); printf("OK\n\n"); } GA_Destroy(g_a); GA_Destroy(g_b); }
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; }
PetscErrorCode vizGA2DA() { PetscErrorCode ierr; int rank; MPI_Comm_rank(PETSC_COMM_WORLD,&rank); int d1 = 40, d2 = 50; DA da; Vec vec; const PetscInt *lx, *ly, *lz; PetscInt m,n,p; DALocalInfo info; ierr = DACreate2d(PETSC_COMM_WORLD,DA_NONPERIODIC,DA_STENCIL_STAR, d1,d2,PETSC_DECIDE,PETSC_DECIDE,1,1,0,0, &da); CHKERRQ(ierr); ierr = DACreateGlobalVector(da, &vec); CHKERRQ(ierr); ierr = DAGetOwnershipRanges(da, &lx, &ly, &lz); CHKERRQ(ierr); ierr = DAGetLocalInfo(da,&info); CHKERRQ(ierr); ierr = DAGetInfo(da,0,0,0,0,&m,&n,&p,0,0,0,0); CHKERRQ(ierr); /**/ ierr = DAView(da, PETSC_VIEWER_STDOUT_WORLD); CHKERRQ(ierr); for (int i = 0; i < m; ++i) { PetscPrintf(PETSC_COMM_WORLD,"%d\tlx: %d\n",i,lx[i]); } for (int i = 0; i < n; ++i) { PetscPrintf(PETSC_COMM_WORLD,"%d\tly: %d\n",i,ly[i]); } /**/ int ga = GA_Create_handle(); int ndim = 2; int dims[2] = {d2,d1}; GA_Set_data(ga,2,dims,MT_DBL); int *map; PetscMalloc( sizeof(int)*(m+n), &map); map[0] = 0; for( int i = 1; i < n; i++ ) { map[i] = ly[i-1] + map[i-1]; } map[n] = 0; for( int i = n+1; i < m+n; i++ ) { map[i] = lx[i-n-1] + map[i-1]; } /* correct ordering, but nodeid's dont line up with mpi rank for petsc's da * DA: +---+---+ GA: +---+---+ * +-2-+-3-+ +-1-+-3-+ * +---+---+ +---+---+ * +-0-+-1-+ +-0-+-2-+ * +---+---+ +---+---+ int *map; PetscMalloc( sizeof(int)*(m+n), &map); map[0] = 0; for( int i = 1; i < m; i++ ) { map[i] = lx[i] + map[i-1]; } map[m] = 0; for( int i = m+1; i < m+n; i++ ) { map[i] = ly[i-m] + map[i-1]; } */ int block[2] = {n,m}; GA_Set_irreg_distr(ga,map,block); ierr = GA_Allocate( ga ); if( !ierr ) GA_Error("\n\n\nga allocaltion failed\n\n",ierr); if( !ga ) GA_Error("\n\n\n ga null \n\n",ierr); if( rank != GA_Nodeid() ) GA_Error("MPI rank does not match GA_Nodeid()",1); GA_Print_distribution(ga); int lo[2], hi[2]; NGA_Distribution(ga,rank,lo,hi); if( lo[1] != info.xs || hi[1] != info.xs+info.xm-1 || lo[0] != info.ys || hi[0] != info.ys+info.ym-1 ) { PetscSynchronizedPrintf(PETSC_COMM_SELF,"[%d] lo:(%2d,%2d) hi:(%2d,%2d) \t DA: (%2d,%2d), (%2d, %2d)\n", rank, lo[1], lo[0], hi[1], hi[0], info.xs, info.ys, info.xs+info.xm-1, info.ys+info.ym-1); } PetscBarrier(0); PetscSynchronizedFlush(PETSC_COMM_WORLD); AO ao; DAGetAO(da,&ao); if( rank == 0 ) { int *idx, len = d1*d2; PetscReal *val; PetscMalloc(sizeof(PetscReal)*len, &val); PetscMalloc(sizeof(int)*len, &idx); for (int j = 0; j < d2; ++j) { for (int i = 0; i < d1; ++i) { idx[i + d1*j] = i + d1*j; val[i + d1*j] = i + d1*j; } } AOApplicationToPetsc(ao,len,idx); VecSetValues(vec,len,idx,val,INSERT_VALUES); int a[2], b[2],ld[1]={0}; double c = 0; for (int j = 0; j < d2; ++j) { for (int i = 0; i < d1; ++i) { a[0] = j; a[1] = i; // printf("%5.0f ",c); NGA_Put(ga,a,a,&c,ld); c++; } } } // GA_Print(ga); VecAssemblyBegin(vec); VecAssemblyEnd(vec); int ld; double *ptr; NGA_Access(ga,lo,hi,&ptr,&ld); PetscReal **d; int c=0; ierr = DAVecGetArray(da,vec,&d); CHKERRQ(ierr); for (int j = info.ys; j < info.ys+info.ym; ++j) { for (int i = info.xs; i < info.xs+info.xm; ++i) { if( d[j][i] != ptr[(i-info.xs)+ld*(j-info.ys)] ) GA_Error("DA array is not equal to GA array",1); // printf("%d (%d,%d):\t%3.0f\t%3.0f\n", c, i, j, d[j][i], ptr[(i-info.xs)+ld*(j-info.ys)]); c++; } } ierr = DAVecRestoreArray(da,vec,&d); CHKERRQ(ierr); c=0; PetscReal *v; int start, end; VecGetOwnershipRange(vec, &start, &end); VecGetArray( vec, &v ); for( int i = start; i < end; i++) { // printf("%d:\t%3.0f\t%3.0f\t%s\n", start, v[i-start], ptr[i-start], (v[i-start]-ptr[i-start]==0?"":"NO") ); } VecRestoreArray( vec, &v ); NGA_Release_update(ga,lo,hi); Vec gada; VecCreateMPIWithArray(((PetscObject)da)->comm,da->Nlocal,PETSC_DETERMINE,ptr,&gada); VecView(gada,PETSC_VIEWER_STDOUT_SELF); GA_Destroy(ga); ierr = VecDestroy(vec); CHKERRQ(ierr); ierr = DADestroy(da); CHKERRQ(ierr); PetscFunctionReturn(0); }
int main(int argc, char**argv) { int nprocs, me; int i,j; MPI_Init(&argc,&argv); MPI_Comm_size(MPI_COMM_WORLD,&nprocs); MPI_Comm_rank(MPI_COMM_WORLD,&me); GA_Initialize(); const int heap=3000000, stack=300000; if(! MA_init(C_INT,stack,heap) ) GA_Error((char *) "MA_init failed",stack+heap /*error code*/); int Nx=97; int Ny=97; int Nz = 97; Nx+=3; Ny+=3; Nz+=3; int data[Nx*Ny*Nz]; int num_splines = 32; int g_a,dims[4]={Nx,Ny,Nz,num_splines},chunk[4]={-1,-1,-1,num_splines}; int width[4] = {3, 3, 3, 0}; int type=C_INT; //g_a=NGA_Create(type,4,dims,"Coefs",chunk); g_a=NGA_Create_ghosts(type, 4, dims, width, "Coefs", chunk); int lo[4],hi[4],ld[3]; //double value=9.0; GA_Fill(g_a,&value); GA_Print_distribution(g_a); fflush(stdout); if(me==0) { for (i=0; i<num_splines; i++) { int x, y, z; for (x=0; x<Nx; x++) for (y=0; y<Ny; y++) for (z=0; z<Nz; z++) { j=x*(Ny*Nz)+y*Nz+z; data[j] = (x*100*100+y*100+z)*100+i;} lo[0]=lo[1]=lo[2]=0; hi[0]=Nx-1;hi[1]=Ny-1;hi[2]=Nz-1; lo[3]=hi[3]=i%num_splines; ld[0]=Ny;ld[1]=Nz;ld[2]=1; NGA_Put(g_a,lo,hi,data,ld); } } GA_Update_ghosts(g_a); GA_Sync(); printf("done\n"),fflush(stdout); ga_coefs_t *ga_coefs = malloc(sizeof(ga_coefs_t)); ga_coefs->Mx = Nx; ga_coefs->My = Ny; ga_coefs->Mz = Nz; ga_coefs->nsplines = num_splines; ga_coefs->g_a=g_a; int *coefs1 = (int*)malloc((size_t)1*sizeof(int)*4*4*4*num_splines); int ix,iy,iz; Nx-=3; Ny-=3; Nz-=3; ga_coefs->sumt=ga_coefs->amount=0; NGA_Distribution(g_a,me,lo,hi); GA_Print_distribution(g_a); int low[16][4],high[16][4]; for(i=0;i<nprocs;i++) NGA_Distribution(g_a,i,low[i],high[i]); srand ( time(NULL) ); int k=GA_Nodeid(); printf("%d: low[k]=%d high[k]=%d\n", GA_Nodeid(), low[k][2], high[k][2]); int unequal=0; for(i=0;i<1000;i++) { ix=rand_index(low[k][0],high[k][0]); if(ix+3>=dims[0]) ix=low[k][0]; iy=rand_index(low[k][1],high[k][1]); if(iy+3>=dims[1]) iy=low[k][1]; iz=rand_index(low[k][2],high[k][2]); if(iz+3>=dims[2]) iz=low[k][2]; coefs_ga_get_3d(ga_coefs,coefs1,ix,iy,iz); long get_sum=mini_cube_sum(coefs1, ga_coefs->nsplines); long ghost_sum=coefs_ghost_access_3d(ga_coefs->g_a, ix, iy, iz, ga_coefs->nsplines); if(get_sum!=ghost_sum) { printf("ixyz=\t%d\t%d\t%d\t", ix, iy, iz); printf("get_sum=%ld ghost_sum=%ld\n", get_sum, ghost_sum); unequal++; } } printf("unequal count=%d\n", unequal); free(coefs1); GA_Terminate(); MPI_Finalize(); return 0; }
void do_work() { int ZERO=0; /* useful constants */ int g_a, g_b; int n=N, ndim=2,type=MT_F_DBL,dims[2]={N,N},coord[2]; int me=GA_Nodeid(), nproc=GA_Nnodes(); int row, i, j; int lo[2], hi[2]; /* Note: on all current platforms DoublePrecision = double */ DoublePrecision buf[N], *max_row=NULL; MPI_Comm WORLD_COMM; MPI_Comm ROW_COMM; int ilo,ihi, jlo,jhi, ld, prow, pcol; int root=0, grp_me=-1; WORLD_COMM = GA_MPI_Comm_pgroup_default(); if(me==0)printf("Creating matrix A\n"); dims[0]=n; dims[1]=n; g_a = NGA_Create(type, ndim, dims, "A", NULL); if(!g_a) GA_Error("create failed: A",n); if(me==0)printf("OK\n"); if(me==0)printf("Creating matrix B\n"); dims[0]=n; g_b = NGA_Create(type, 1, dims, "B", NULL); if(!g_b) GA_Error("create failed: B",n); if(me==0)printf("OK\n"); GA_Zero(g_a); /* zero the matrix */ if(me==0)printf("Initializing matrix A\n"); /* fill in matrix A with values: A(i,j) = (i+j) */ for(row=me; row<n; row+= nproc){ /** * simple load balancing: * each process works on a different row in MIMD style */ for(i=0; i<n; i++) buf[i]=(DoublePrecision)(i+row+1); lo[0]=hi[0]=row; lo[1]=ZERO; hi[1]=n-1; NGA_Put(g_a, lo, hi, buf, &n); } /* GA_print(&g_a);*/ NGA_Distribution(g_a, me, lo, hi); ilo=lo[0]; ihi=hi[0]; jlo=lo[1]; jhi=hi[1]; GA_Sync(); if(ihi-ilo+1 >0){ max_row=(DoublePrecision*)malloc(sizeof(DoublePrecision)*(ihi-ilo+1)); if (!max_row) GA_Error("malloc 3 failed",(ihi-ilo+1)); for (i=0; i<(ihi-ilo+1); i++) { max_row[i] = 0.0; } } NGA_Proc_topology(g_a, me, coord); /* block coordinates */ prow = coord[0]; pcol = coord[1]; if(me==0)printf("Splitting comm according to distribution of A\n"); /* GA on SP1 requires synchronization before & after message-passing !!*/ GA_Sync(); if(me==0)printf("Computing max row elements\n"); /* create communicator for processes that 'own' A[:,jlo:jhi] */ MPI_Barrier(WORLD_COMM); if(pcol < 0 || prow <0) MPI_Comm_split(WORLD_COMM,MPI_UNDEFINED,MPI_UNDEFINED, &ROW_COMM); else MPI_Comm_split(WORLD_COMM, (int)pcol, (int)prow, &ROW_COMM); if(ROW_COMM != MPI_COMM_NULL){ double *ptr; MPI_Comm_rank(ROW_COMM, &grp_me); /* each process computes max elements in the block it 'owns' */ lo[0]=ilo; hi[0]=ihi; lo[1]=jlo; hi[1]=jhi; NGA_Access(g_a, lo, hi, &ptr, &ld); for(i=0; i<ihi-ilo+1; i++){ for(j=0; j<jhi-jlo+1; j++) if(max_row[i] < ptr[i*ld + j]){ max_row[i] = ptr[i*ld + j]; } } MPI_Reduce(max_row, buf, ihi-ilo+1, MPI_DOUBLE, MPI_MAX, root, ROW_COMM); }else fprintf(stderr,"process %d not participating\n",me); GA_Sync(); /* processes with rank=root in ROW_COMM put results into g_b */ ld = 1; if(grp_me == root) { lo[0]=ilo; hi[0]=ihi; NGA_Put(g_b, lo, hi, buf, &ld); } GA_Sync(); if(me==0)printf("Checking the result\n"); if(me==0){ lo[0]=ZERO; hi[0]=n-1; NGA_Get(g_b, lo, hi, buf, &n); for(i=0; i< n; i++)if(buf[i] != (double)n+i){ fprintf(stderr,"error:%d max=%f should be:%d\n",i,buf[i],n+i); GA_Error("terminating...",1); } } if(me==0)printf("OK\n"); GA_Destroy(g_a); GA_Destroy(g_b); }