hypre_ParMultiVector * hypre_ParMultiVectorCreate(MPI_Comm comm, HYPRE_Int global_size, HYPRE_Int *partitioning, HYPRE_Int num_vectors) { hypre_ParMultiVector *vector; HYPRE_Int num_procs, my_id; vector = hypre_CTAlloc(hypre_ParMultiVector, 1); hypre_MPI_Comm_rank(comm, &my_id); if (! partitioning) { hypre_MPI_Comm_size(comm, &num_procs); hypre_GeneratePartitioning(global_size, num_procs, &partitioning); } hypre_ParMultiVectorComm(vector) = comm; hypre_ParMultiVectorGlobalSize(vector) = global_size; hypre_ParMultiVectorPartitioning(vector) = partitioning; hypre_ParMultiVectorNumVectors(vector) = num_vectors; hypre_ParMultiVectorLocalVector(vector) = hypre_SeqMultivectorCreate((partitioning[my_id+1]-partitioning[my_id]), num_vectors); hypre_ParMultiVectorFirstIndex(vector) = partitioning[my_id]; /* we set these 2 defaults exactly as in par_vector.c, although it's questionable */ hypre_ParMultiVectorOwnsData(vector) = 1; hypre_ParMultiVectorOwnsPartitioning(vector) = 1; return vector; }
hypre_ParVector * hypre_ParVectorCreate( MPI_Comm comm, HYPRE_Int global_size, HYPRE_Int *partitioning) { hypre_ParVector *vector; HYPRE_Int num_procs, my_id; if (global_size < 0) { hypre_error_in_arg(2); return NULL; } vector = hypre_CTAlloc(hypre_ParVector, 1); hypre_MPI_Comm_rank(comm,&my_id); if (!partitioning) { hypre_MPI_Comm_size(comm,&num_procs); #ifdef HYPRE_NO_GLOBAL_PARTITION hypre_GenerateLocalPartitioning(global_size, num_procs, my_id, &partitioning); #else hypre_GeneratePartitioning(global_size, num_procs, &partitioning); #endif } hypre_ParVectorAssumedPartition(vector) = NULL; hypre_ParVectorComm(vector) = comm; hypre_ParVectorGlobalSize(vector) = global_size; #ifdef HYPRE_NO_GLOBAL_PARTITION hypre_ParVectorFirstIndex(vector) = partitioning[0]; hypre_ParVectorLastIndex(vector) = partitioning[1]-1; hypre_ParVectorPartitioning(vector) = partitioning; hypre_ParVectorLocalVector(vector) = hypre_SeqVectorCreate(partitioning[1]-partitioning[0]); #else hypre_ParVectorFirstIndex(vector) = partitioning[my_id]; hypre_ParVectorLastIndex(vector) = partitioning[my_id+1] -1; hypre_ParVectorPartitioning(vector) = partitioning; hypre_ParVectorLocalVector(vector) = hypre_SeqVectorCreate(partitioning[my_id+1]-partitioning[my_id]); #endif /* set defaults */ hypre_ParVectorOwnsData(vector) = 1; hypre_ParVectorOwnsPartitioning(vector) = 1; return vector; }
HYPRE_Int main( HYPRE_Int argc, char *argv[] ) { hypre_ParVector *vector1; hypre_ParVector *vector2; hypre_ParVector *tmp_vector; HYPRE_Int num_procs, my_id; HYPRE_Int global_size = 20; HYPRE_Int local_size; HYPRE_Int first_index; HYPRE_Int num_vectors, vecstride, idxstride; HYPRE_Int i, j; HYPRE_Int *partitioning; double prod; double *data, *data2; hypre_Vector *vector; hypre_Vector *local_vector; hypre_Vector *local_vector2; /* Initialize MPI */ hypre_MPI_Init(&argc, &argv); hypre_MPI_Comm_size(hypre_MPI_COMM_WORLD, &num_procs ); hypre_MPI_Comm_rank(hypre_MPI_COMM_WORLD, &my_id ); hypre_printf(" my_id: %d num_procs: %d\n", my_id, num_procs); partitioning = NULL; num_vectors = 3; vector1 = hypre_ParMultiVectorCreate ( hypre_MPI_COMM_WORLD, global_size, partitioning, num_vectors ); partitioning = hypre_ParVectorPartitioning(vector1); hypre_ParVectorInitialize(vector1); local_vector = hypre_ParVectorLocalVector(vector1); data = hypre_VectorData(local_vector); local_size = hypre_VectorSize(local_vector); vecstride = hypre_VectorVectorStride(local_vector); idxstride = hypre_VectorIndexStride(local_vector); first_index = partitioning[my_id]; hypre_printf("vecstride=%i idxstride=%i local_size=%i num_vectors=%i", vecstride, idxstride, local_size, num_vectors ); for (j=0; j<num_vectors; ++j ) for (i=0; i < local_size; i++) data[ j*vecstride + i*idxstride ] = first_index+i + 100*j; hypre_ParVectorPrint(vector1, "Vector"); local_vector2 = hypre_SeqMultiVectorCreate( global_size, num_vectors ); hypre_SeqVectorInitialize(local_vector2); data2 = hypre_VectorData(local_vector2); vecstride = hypre_VectorVectorStride(local_vector2); idxstride = hypre_VectorIndexStride(local_vector2); for (j=0; j<num_vectors; ++j ) for (i=0; i < global_size; i++) data2[ j*vecstride + i*idxstride ] = i + 100*j; /* partitioning = hypre_CTAlloc(HYPRE_Int,4); partitioning[0] = 0; partitioning[1] = 10; partitioning[2] = 10; partitioning[3] = 20; */ partitioning = hypre_CTAlloc(HYPRE_Int,1+num_procs); hypre_GeneratePartitioning( global_size, num_procs, &partitioning ); vector2 = hypre_VectorToParVector(hypre_MPI_COMM_WORLD,local_vector2,partitioning); hypre_ParVectorSetPartitioningOwner(vector2,0); hypre_ParVectorPrint(vector2, "Convert"); vector = hypre_ParVectorToVectorAll(vector2); /*----------------------------------------------------------- * Copy the vector into tmp_vector *-----------------------------------------------------------*/ /* Read doesn't work for multivectors yet... tmp_vector = hypre_ParVectorRead(hypre_MPI_COMM_WORLD, "Convert");*/ tmp_vector = hypre_ParMultiVectorCreate ( hypre_MPI_COMM_WORLD, global_size, partitioning, num_vectors ); hypre_ParVectorInitialize( tmp_vector ); hypre_ParVectorCopy( vector2, tmp_vector ); /* tmp_vector = hypre_ParVectorCreate(hypre_MPI_COMM_WORLD,global_size,partitioning); hypre_ParVectorSetPartitioningOwner(tmp_vector,0); hypre_ParVectorInitialize(tmp_vector); hypre_ParVectorCopy(vector1, tmp_vector); hypre_ParVectorPrint(tmp_vector,"Copy"); */ /*----------------------------------------------------------- * Scale tmp_vector *-----------------------------------------------------------*/ hypre_ParVectorScale(2.0, tmp_vector); hypre_ParVectorPrint(tmp_vector,"Scale"); /*----------------------------------------------------------- * Do an Axpy (2*vector - vector) = vector *-----------------------------------------------------------*/ hypre_ParVectorAxpy(-1.0, vector1, tmp_vector); hypre_ParVectorPrint(tmp_vector,"Axpy"); /*----------------------------------------------------------- * Do an inner product vector* tmp_vector *-----------------------------------------------------------*/ prod = hypre_ParVectorInnerProd(vector1, tmp_vector); hypre_printf (" prod: %8.2f \n", prod); /*----------------------------------------------------------- * Finalize things *-----------------------------------------------------------*/ hypre_ParVectorDestroy(vector1); hypre_ParVectorDestroy(vector2); hypre_ParVectorDestroy(tmp_vector); hypre_SeqVectorDestroy(local_vector2); if (vector) hypre_SeqVectorDestroy(vector); /* Finalize MPI */ hypre_MPI_Finalize(); return 0; }
HYPRE_ParCSRMatrix GenerateRotate7pt( MPI_Comm comm, HYPRE_Int nx, HYPRE_Int ny, HYPRE_Int P, HYPRE_Int Q, HYPRE_Int p, HYPRE_Int q, HYPRE_Real alpha, HYPRE_Real eps ) { hypre_ParCSRMatrix *A; hypre_CSRMatrix *diag; hypre_CSRMatrix *offd; HYPRE_Int *diag_i; HYPRE_Int *diag_j; HYPRE_Real *diag_data; HYPRE_Int *offd_i; HYPRE_Int *offd_j; HYPRE_Real *offd_data; HYPRE_Real *value; HYPRE_Real ac, bc, cc, s, c, pi, x; HYPRE_Int *global_part; HYPRE_Int ix, iy; HYPRE_Int cnt, o_cnt; HYPRE_Int local_num_rows; HYPRE_Int *col_map_offd; HYPRE_Int *work; HYPRE_Int row_index; HYPRE_Int i,j; HYPRE_Int nx_local, ny_local; HYPRE_Int nx_size, ny_size; HYPRE_Int num_cols_offd; HYPRE_Int grid_size; HYPRE_Int *nx_part; HYPRE_Int *ny_part; HYPRE_Int num_procs, my_id; HYPRE_Int P_busy, Q_busy; hypre_MPI_Comm_size(comm,&num_procs); hypre_MPI_Comm_rank(comm,&my_id); grid_size = nx*ny; value = hypre_CTAlloc(HYPRE_Real,4); pi = 4.0*atan(1.0); x = pi*alpha/180.0; s = sin(x); c = cos(x); ac = -(c*c + eps*s*s); bc = 2.0*(1.0 - eps)*s*c; cc = -(s*s + eps*c*c); value[0] = -2*(2*ac+bc+2*cc); value[1] = 2*ac+bc; value[2] = bc+2*cc; value[3] = -bc; hypre_GeneratePartitioning(nx,P,&nx_part); hypre_GeneratePartitioning(ny,Q,&ny_part); global_part = hypre_CTAlloc(HYPRE_Int,P*Q+1); global_part[0] = 0; cnt = 1; for (iy = 0; iy < Q; iy++) { ny_size = ny_part[iy+1]-ny_part[iy]; for (ix = 0; ix < P; ix++) { nx_size = nx_part[ix+1] - nx_part[ix]; global_part[cnt] = global_part[cnt-1]; global_part[cnt++] += nx_size*ny_size; } } nx_local = nx_part[p+1] - nx_part[p]; ny_local = ny_part[q+1] - ny_part[q]; my_id = q*P + p; num_procs = P*Q; local_num_rows = nx_local*ny_local; diag_i = hypre_CTAlloc(HYPRE_Int, local_num_rows+1); offd_i = hypre_CTAlloc(HYPRE_Int, local_num_rows+1); P_busy = hypre_min(nx,P); Q_busy = hypre_min(ny,Q); num_cols_offd = 0; if (p) num_cols_offd += ny_local; if (p < P_busy-1) num_cols_offd += ny_local; if (q) num_cols_offd += nx_local; if (q < Q_busy-1) num_cols_offd += nx_local; if (p && q) num_cols_offd++; if (p && q < Q_busy-1 ) num_cols_offd++; if (p < P_busy-1 && q ) num_cols_offd++; if (p < P_busy-1 && q < Q_busy-1 ) num_cols_offd++; if (!local_num_rows) num_cols_offd = 0; col_map_offd = hypre_CTAlloc(HYPRE_Int, num_cols_offd); cnt = 0; o_cnt = 0; diag_i[0] = 0; offd_i[0] = 0; for (iy = ny_part[q]; iy < ny_part[q+1]; iy++) { for (ix = nx_part[p]; ix < nx_part[p+1]; ix++) { cnt++; o_cnt++; diag_i[cnt] = diag_i[cnt-1]; offd_i[o_cnt] = offd_i[o_cnt-1]; diag_i[cnt]++; if (iy > ny_part[q]) { diag_i[cnt]++; if (ix > nx_part[p]) { diag_i[cnt]++; } else { if (ix) offd_i[o_cnt]++; } } else { if (iy) { offd_i[o_cnt]++; if (ix > nx_part[p]) { offd_i[o_cnt]++; } else if (ix) { offd_i[o_cnt]++; } } } if (ix > nx_part[p]) diag_i[cnt]++; else { if (ix) { offd_i[o_cnt]++; } } if (ix+1 < nx_part[p+1]) diag_i[cnt]++; else { if (ix+1 < nx) { offd_i[o_cnt]++; } } if (iy+1 < ny_part[q+1]) { diag_i[cnt]++; if (ix < nx_part[p+1]-1) { diag_i[cnt]++; } else { if (ix+1 < nx) offd_i[o_cnt]++; } } else { if (iy+1 < ny) { offd_i[o_cnt]++; if (ix < nx_part[p+1]-1) { offd_i[o_cnt]++; } else if (ix < nx-1) { offd_i[o_cnt]++; } } } } } diag_j = hypre_CTAlloc(HYPRE_Int, diag_i[local_num_rows]); diag_data = hypre_CTAlloc(HYPRE_Real, diag_i[local_num_rows]); if (num_procs > 1) { offd_j = hypre_CTAlloc(HYPRE_Int, offd_i[local_num_rows]); offd_data = hypre_CTAlloc(HYPRE_Real, offd_i[local_num_rows]); } row_index = 0; cnt = 0; o_cnt = 0; for (iy = ny_part[q]; iy < ny_part[q+1]; iy++) { for (ix = nx_part[p]; ix < nx_part[p+1]; ix++) { diag_j[cnt] = row_index; diag_data[cnt++] = value[0]; if (iy > ny_part[q]) { if (ix > nx_part[p]) { diag_j[cnt] = row_index-nx_local-1 ; diag_data[cnt++] = value[3]; } else { if (ix) { offd_j[o_cnt] = hypre_map2(ix-1,iy-1,p-1,q,P,Q, nx_part,ny_part,global_part); offd_data[o_cnt++] = value[3]; } } diag_j[cnt] = row_index-nx_local; diag_data[cnt++] = value[2]; } else { if (iy) { if (ix > nx_part[p]) { offd_j[o_cnt] = hypre_map2(ix-1,iy-1,p,q-1,P,Q, nx_part,ny_part,global_part); offd_data[o_cnt++] = value[3]; } else if (ix) { offd_j[o_cnt] = hypre_map2(ix-1,iy-1,p-1,q-1,P,Q, nx_part,ny_part,global_part); offd_data[o_cnt++] = value[3]; } offd_j[o_cnt] = hypre_map2(ix,iy-1,p,q-1,P,Q, nx_part,ny_part,global_part); offd_data[o_cnt++] = value[2]; } } if (ix > nx_part[p]) { diag_j[cnt] = row_index-1; diag_data[cnt++] = value[1]; } else { if (ix) { offd_j[o_cnt] = hypre_map2(ix-1,iy,p-1,q,P,Q, nx_part,ny_part,global_part); offd_data[o_cnt++] = value[1]; } } if (ix+1 < nx_part[p+1]) { diag_j[cnt] = row_index+1; diag_data[cnt++] = value[1]; } else { if (ix+1 < nx) { offd_j[o_cnt] = hypre_map2(ix+1,iy,p+1,q,P,Q, nx_part,ny_part,global_part); offd_data[o_cnt++] = value[1]; } } if (iy+1 < ny_part[q+1]) { diag_j[cnt] = row_index+nx_local; diag_data[cnt++] = value[2]; if (ix < nx_part[p+1]-1) { diag_j[cnt] = row_index+nx_local+1 ; diag_data[cnt++] = value[3]; } else { if (ix+1 < nx) { offd_j[o_cnt] = hypre_map2(ix+1,iy+1,p+1,q,P,Q, nx_part,ny_part,global_part); offd_data[o_cnt++] = value[3]; } } } else { if (iy+1 < ny) { offd_j[o_cnt] = hypre_map2(ix,iy+1,p,q+1,P,Q, nx_part,ny_part,global_part); offd_data[o_cnt++] = value[2]; if (ix < nx_part[p+1]-1) { offd_j[o_cnt] = hypre_map2(ix+1,iy+1,p,q+1,P,Q, nx_part,ny_part,global_part); offd_data[o_cnt++] = value[3]; } else if (ix < nx-1) { offd_j[o_cnt] = hypre_map2(ix+1,iy+1,p+1,q+1,P,Q, nx_part,ny_part,global_part); offd_data[o_cnt++] = value[3]; } } } row_index++; } } if (num_procs > 1) { work = hypre_CTAlloc(HYPRE_Int,o_cnt); for (i=0; i < o_cnt; i++) work[i] = offd_j[i]; qsort0(work, 0, o_cnt-1); col_map_offd[0] = work[0]; cnt = 0; for (i=0; i < o_cnt; i++) { if (work[i] > col_map_offd[cnt]) { cnt++; col_map_offd[cnt] = work[i]; } } for (i=0; i < o_cnt; i++) { for (j=0; j < num_cols_offd; j++) { if (offd_j[i] == col_map_offd[j]) { offd_j[i] = j; break; } } } hypre_TFree(work); } A = hypre_ParCSRMatrixCreate(comm, grid_size, grid_size, global_part, global_part, num_cols_offd, diag_i[local_num_rows], offd_i[local_num_rows]); hypre_ParCSRMatrixColMapOffd(A) = col_map_offd; diag = hypre_ParCSRMatrixDiag(A); hypre_CSRMatrixI(diag) = diag_i; hypre_CSRMatrixJ(diag) = diag_j; hypre_CSRMatrixData(diag) = diag_data; offd = hypre_ParCSRMatrixOffd(A); hypre_CSRMatrixI(offd) = offd_i; if (num_cols_offd) { hypre_CSRMatrixJ(offd) = offd_j; hypre_CSRMatrixData(offd) = offd_data; } hypre_TFree(nx_part); hypre_TFree(ny_part); hypre_TFree(value); return (HYPRE_ParCSRMatrix) A; }
hypre_ParCSRBooleanMatrix *hypre_ParCSRBooleanMatrixCreate( MPI_Comm comm, HYPRE_Int global_num_rows, HYPRE_Int global_num_cols, HYPRE_Int *row_starts, HYPRE_Int *col_starts, HYPRE_Int num_cols_offd, HYPRE_Int num_nonzeros_diag, HYPRE_Int num_nonzeros_offd) { hypre_ParCSRBooleanMatrix *matrix; HYPRE_Int num_procs, my_id; HYPRE_Int local_num_rows, local_num_cols; HYPRE_Int first_row_index, first_col_diag; matrix = hypre_CTAlloc(hypre_ParCSRBooleanMatrix, 1); hypre_MPI_Comm_rank(comm,&my_id); hypre_MPI_Comm_size(comm,&num_procs); if (!row_starts) { hypre_GeneratePartitioning(global_num_rows,num_procs,&row_starts); } if (!col_starts) { if (global_num_rows == global_num_cols) { col_starts = row_starts; } else { hypre_GeneratePartitioning(global_num_cols,num_procs,&col_starts); } } first_row_index = row_starts[my_id]; local_num_rows = row_starts[my_id+1]-first_row_index; first_col_diag = col_starts[my_id]; local_num_cols = col_starts[my_id+1]-first_col_diag; hypre_ParCSRBooleanMatrix_Get_Comm(matrix) = comm; hypre_ParCSRBooleanMatrix_Get_Diag(matrix) = hypre_CSRBooleanMatrixCreate(local_num_rows, local_num_cols, num_nonzeros_diag); hypre_ParCSRBooleanMatrix_Get_Offd(matrix) = hypre_CSRBooleanMatrixCreate(local_num_rows, num_cols_offd, num_nonzeros_offd); hypre_ParCSRBooleanMatrix_Get_GlobalNRows(matrix) = global_num_rows; hypre_ParCSRBooleanMatrix_Get_GlobalNCols(matrix) = global_num_cols; hypre_ParCSRBooleanMatrix_Get_StartRow(matrix) = first_row_index; hypre_ParCSRBooleanMatrix_Get_FirstColDiag(matrix) = first_col_diag; hypre_ParCSRBooleanMatrix_Get_ColMapOffd(matrix) = NULL; hypre_ParCSRBooleanMatrix_Get_RowStarts(matrix) = row_starts; hypre_ParCSRBooleanMatrix_Get_ColStarts(matrix) = col_starts; hypre_ParCSRBooleanMatrix_Get_CommPkg(matrix) = NULL; hypre_ParCSRBooleanMatrix_Get_OwnsData(matrix) = 1; hypre_ParCSRBooleanMatrix_Get_OwnsRowStarts(matrix) = 1; hypre_ParCSRBooleanMatrix_Get_OwnsColStarts(matrix) = 1; if (row_starts == col_starts) hypre_ParCSRBooleanMatrix_Get_OwnsColStarts(matrix) = 0; hypre_ParCSRBooleanMatrix_Get_Rowindices(matrix) = NULL; hypre_ParCSRBooleanMatrix_Get_Getrowactive(matrix) = 0; return matrix; }
HYPRE_ParCSRMatrix GenerateLaplacian9pt( MPI_Comm comm, HYPRE_Int nx, HYPRE_Int ny, HYPRE_Int P, HYPRE_Int Q, HYPRE_Int p, HYPRE_Int q, double *value ) { hypre_ParCSRMatrix *A; hypre_CSRMatrix *diag; hypre_CSRMatrix *offd; HYPRE_Int *diag_i; HYPRE_Int *diag_j; double *diag_data; HYPRE_Int *offd_i; HYPRE_Int *offd_j; double *offd_data; HYPRE_Int *global_part; HYPRE_Int ix, iy; HYPRE_Int cnt, o_cnt; HYPRE_Int local_num_rows; HYPRE_Int *col_map_offd; HYPRE_Int *work; HYPRE_Int row_index; HYPRE_Int i,j; HYPRE_Int nx_local, ny_local; HYPRE_Int nx_size, ny_size; HYPRE_Int num_cols_offd; HYPRE_Int grid_size; HYPRE_Int *nx_part; HYPRE_Int *ny_part; HYPRE_Int num_procs, my_id; HYPRE_Int P_busy, Q_busy; hypre_MPI_Comm_size(comm,&num_procs); hypre_MPI_Comm_rank(comm,&my_id); grid_size = nx*ny; hypre_GeneratePartitioning(nx,P,&nx_part); hypre_GeneratePartitioning(ny,Q,&ny_part); global_part = hypre_CTAlloc(HYPRE_Int,P*Q+1); global_part[0] = 0; cnt = 1; for (iy = 0; iy < Q; iy++) { ny_size = ny_part[iy+1]-ny_part[iy]; for (ix = 0; ix < P; ix++) { nx_size = nx_part[ix+1] - nx_part[ix]; global_part[cnt] = global_part[cnt-1]; global_part[cnt++] += nx_size*ny_size; } } nx_local = nx_part[p+1] - nx_part[p]; ny_local = ny_part[q+1] - ny_part[q]; my_id = q*P + p; num_procs = P*Q; local_num_rows = nx_local*ny_local; diag_i = hypre_CTAlloc(HYPRE_Int, local_num_rows+1); offd_i = hypre_CTAlloc(HYPRE_Int, local_num_rows+1); P_busy = hypre_min(nx,P); Q_busy = hypre_min(ny,Q); num_cols_offd = 0; if (p) num_cols_offd += ny_local; if (p < P_busy-1) num_cols_offd += ny_local; if (q) num_cols_offd += nx_local; if (q < Q_busy-1) num_cols_offd += nx_local; if (p && q) num_cols_offd++; if (p && q < Q_busy-1 ) num_cols_offd++; if (p < P_busy-1 && q ) num_cols_offd++; if (p < P_busy-1 && q < Q_busy-1 ) num_cols_offd++; if (!local_num_rows) num_cols_offd = 0; col_map_offd = hypre_CTAlloc(HYPRE_Int, num_cols_offd); cnt = 0; o_cnt = 0; diag_i[0] = 0; offd_i[0] = 0; for (iy = ny_part[q]; iy < ny_part[q+1]; iy++) { for (ix = nx_part[p]; ix < nx_part[p+1]; ix++) { cnt++; o_cnt++; diag_i[cnt] = diag_i[cnt-1]; offd_i[o_cnt] = offd_i[o_cnt-1]; diag_i[cnt]++; if (iy > ny_part[q]) { diag_i[cnt]++; if (ix > nx_part[p]) { diag_i[cnt]++; } else { if (ix) offd_i[o_cnt]++; } if (ix < nx_part[p+1]-1) { diag_i[cnt]++; } else { if (ix+1 < nx) offd_i[o_cnt]++; } } else { if (iy) { offd_i[o_cnt]++; if (ix > nx_part[p]) { offd_i[o_cnt]++; } else if (ix) { offd_i[o_cnt]++; } if (ix < nx_part[p+1]-1) { offd_i[o_cnt]++; } else if (ix < nx-1) { offd_i[o_cnt]++; } } } if (ix > nx_part[p]) diag_i[cnt]++; else { if (ix) { offd_i[o_cnt]++; } } if (ix+1 < nx_part[p+1]) diag_i[cnt]++; else { if (ix+1 < nx) { offd_i[o_cnt]++; } } if (iy+1 < ny_part[q+1]) { diag_i[cnt]++; if (ix > nx_part[p]) { diag_i[cnt]++; } else { if (ix) offd_i[o_cnt]++; } if (ix < nx_part[p+1]-1) { diag_i[cnt]++; } else { if (ix+1 < nx) offd_i[o_cnt]++; } } else { if (iy+1 < ny) { offd_i[o_cnt]++; if (ix > nx_part[p]) { offd_i[o_cnt]++; } else if (ix) { offd_i[o_cnt]++; } if (ix < nx_part[p+1]-1) { offd_i[o_cnt]++; } else if (ix < nx-1) { offd_i[o_cnt]++; } } } } } diag_j = hypre_CTAlloc(HYPRE_Int, diag_i[local_num_rows]); diag_data = hypre_CTAlloc(double, diag_i[local_num_rows]); if (num_procs > 1) { offd_j = hypre_CTAlloc(HYPRE_Int, offd_i[local_num_rows]); offd_data = hypre_CTAlloc(double, offd_i[local_num_rows]); }
HYPRE_ParCSRMatrix GenerateRotate7pt( MPI_Comm comm, HYPRE_Int nx, HYPRE_Int ny, HYPRE_Int P, HYPRE_Int Q, HYPRE_Int p, HYPRE_Int q, double alpha, double eps ) { hypre_ParCSRMatrix *A; hypre_CSRMatrix *diag; hypre_CSRMatrix *offd; HYPRE_Int *diag_i; HYPRE_Int *diag_j; double *diag_data; HYPRE_Int *offd_i; HYPRE_Int *offd_j; double *offd_data; double *value; double ac, bc, cc, s, c, pi, x; HYPRE_Int *global_part; HYPRE_Int ix, iy; HYPRE_Int cnt, o_cnt; HYPRE_Int local_num_rows; HYPRE_Int *col_map_offd; HYPRE_Int *work; HYPRE_Int row_index; HYPRE_Int i,j; HYPRE_Int nx_local, ny_local; HYPRE_Int nx_size, ny_size; HYPRE_Int num_cols_offd; HYPRE_Int grid_size; HYPRE_Int *nx_part; HYPRE_Int *ny_part; HYPRE_Int num_procs, my_id; HYPRE_Int P_busy, Q_busy; hypre_MPI_Comm_size(comm,&num_procs); hypre_MPI_Comm_rank(comm,&my_id); grid_size = nx*ny; value = hypre_CTAlloc(double,4); pi = 4.0*atan(1.0); x = pi*alpha/180.0; s = sin(x); c = cos(x); ac = -(c*c + eps*s*s); bc = 2.0*(1.0 - eps)*s*c; cc = -(s*s + eps*c*c); value[0] = -2*(2*ac+bc+2*cc); value[1] = 2*ac+bc; value[2] = bc+2*cc; value[3] = -bc; hypre_GeneratePartitioning(nx,P,&nx_part); hypre_GeneratePartitioning(ny,Q,&ny_part); global_part = hypre_CTAlloc(HYPRE_Int,P*Q+1); global_part[0] = 0; cnt = 1; for (iy = 0; iy < Q; iy++) { ny_size = ny_part[iy+1]-ny_part[iy]; for (ix = 0; ix < P; ix++) { nx_size = nx_part[ix+1] - nx_part[ix]; global_part[cnt] = global_part[cnt-1]; global_part[cnt++] += nx_size*ny_size; } } nx_local = nx_part[p+1] - nx_part[p]; ny_local = ny_part[q+1] - ny_part[q]; my_id = q*P + p; num_procs = P*Q; local_num_rows = nx_local*ny_local; diag_i = hypre_CTAlloc(HYPRE_Int, local_num_rows+1); offd_i = hypre_CTAlloc(HYPRE_Int, local_num_rows+1); P_busy = hypre_min(nx,P); Q_busy = hypre_min(ny,Q); num_cols_offd = 0; if (p) num_cols_offd += ny_local; if (p < P_busy-1) num_cols_offd += ny_local; if (q) num_cols_offd += nx_local; if (q < Q_busy-1) num_cols_offd += nx_local; if (p && q) num_cols_offd++; if (p && q < Q_busy-1 ) num_cols_offd++; if (p < P_busy-1 && q ) num_cols_offd++; if (p < P_busy-1 && q < Q_busy-1 ) num_cols_offd++; if (!local_num_rows) num_cols_offd = 0; col_map_offd = hypre_CTAlloc(HYPRE_Int, num_cols_offd); cnt = 0; o_cnt = 0; diag_i[0] = 0; offd_i[0] = 0; for (iy = ny_part[q]; iy < ny_part[q+1]; iy++) { for (ix = nx_part[p]; ix < nx_part[p+1]; ix++) { cnt++; o_cnt++; diag_i[cnt] = diag_i[cnt-1]; offd_i[o_cnt] = offd_i[o_cnt-1]; diag_i[cnt]++; if (iy > ny_part[q]) { diag_i[cnt]++; if (ix > nx_part[p]) { diag_i[cnt]++; } else { if (ix) offd_i[o_cnt]++; } } else { if (iy) { offd_i[o_cnt]++; if (ix > nx_part[p]) { offd_i[o_cnt]++; } else if (ix) { offd_i[o_cnt]++; } } } if (ix > nx_part[p]) diag_i[cnt]++; else { if (ix) { offd_i[o_cnt]++; } } if (ix+1 < nx_part[p+1]) diag_i[cnt]++; else { if (ix+1 < nx) { offd_i[o_cnt]++; } } if (iy+1 < ny_part[q+1]) { diag_i[cnt]++; if (ix < nx_part[p+1]-1) { diag_i[cnt]++; } else { if (ix+1 < nx) offd_i[o_cnt]++; } } else { if (iy+1 < ny) { offd_i[o_cnt]++; if (ix < nx_part[p+1]-1) { offd_i[o_cnt]++; } else if (ix < nx-1) { offd_i[o_cnt]++; } } } } } diag_j = hypre_CTAlloc(HYPRE_Int, diag_i[local_num_rows]); diag_data = hypre_CTAlloc(double, diag_i[local_num_rows]); if (num_procs > 1) { offd_j = hypre_CTAlloc(HYPRE_Int, offd_i[local_num_rows]); offd_data = hypre_CTAlloc(double, offd_i[local_num_rows]); }
hypre_ParCSRBlockMatrix * hypre_ParCSRBlockMatrixCreate( MPI_Comm comm, HYPRE_Int block_size, HYPRE_Int global_num_rows, HYPRE_Int global_num_cols, HYPRE_Int *row_starts, HYPRE_Int *col_starts, HYPRE_Int num_cols_offd, HYPRE_Int num_nonzeros_diag, HYPRE_Int num_nonzeros_offd ) { hypre_ParCSRBlockMatrix *matrix; HYPRE_Int num_procs, my_id; HYPRE_Int local_num_rows, local_num_cols; HYPRE_Int first_row_index, first_col_diag; matrix = hypre_CTAlloc(hypre_ParCSRBlockMatrix, 1); hypre_MPI_Comm_rank(comm,&my_id); hypre_MPI_Comm_size(comm,&num_procs); if (!row_starts) { #ifdef HYPRE_NO_GLOBAL_PARTITION hypre_GenerateLocalPartitioning(global_num_rows, num_procs, my_id, &row_starts); #else hypre_GeneratePartitioning(global_num_rows,num_procs,&row_starts); #endif } if (!col_starts) { if (global_num_rows == global_num_cols) { col_starts = row_starts; } else { #ifdef HYPRE_NO_GLOBAL_PARTITION hypre_GenerateLocalPartitioning(global_num_cols, num_procs, my_id, &col_starts); #else hypre_GeneratePartitioning(global_num_cols,num_procs,&col_starts); #endif } } #ifdef HYPRE_NO_GLOBAL_PARTITION /* row_starts[0] is start of local rows. row_starts[1] is start of next processor's rows */ first_row_index = row_starts[0]; local_num_rows = row_starts[1]-first_row_index ; first_col_diag = col_starts[0]; local_num_cols = col_starts[1]-first_col_diag; #else first_row_index = row_starts[my_id]; local_num_rows = row_starts[my_id+1]-first_row_index; first_col_diag = col_starts[my_id]; local_num_cols = col_starts[my_id+1]-first_col_diag; #endif hypre_ParCSRBlockMatrixComm(matrix) = comm; hypre_ParCSRBlockMatrixDiag(matrix) = hypre_CSRBlockMatrixCreate(block_size, local_num_rows,local_num_cols,num_nonzeros_diag); hypre_ParCSRBlockMatrixOffd(matrix) = hypre_CSRBlockMatrixCreate(block_size, local_num_rows, num_cols_offd, num_nonzeros_offd); hypre_ParCSRBlockMatrixBlockSize(matrix) = block_size; hypre_ParCSRBlockMatrixGlobalNumRows(matrix) = global_num_rows; hypre_ParCSRBlockMatrixGlobalNumCols(matrix) = global_num_cols; hypre_ParCSRBlockMatrixFirstRowIndex(matrix) = first_row_index; hypre_ParCSRBlockMatrixFirstColDiag(matrix) = first_col_diag; hypre_ParCSRBlockMatrixLastRowIndex(matrix) = first_row_index + local_num_rows - 1; hypre_ParCSRBlockMatrixLastColDiag(matrix) = first_col_diag + local_num_cols - 1; hypre_ParCSRBlockMatrixColMapOffd(matrix) = NULL; hypre_ParCSRBlockMatrixAssumedPartition(matrix) = NULL; /* When NO_GLOBAL_PARTITION is set we could make these null, instead of leaving the range. If that change is made, then when this create is called from functions like the matrix-matrix multiply, be careful not to generate a new partition */ hypre_ParCSRBlockMatrixRowStarts(matrix) = row_starts; hypre_ParCSRBlockMatrixColStarts(matrix) = col_starts; hypre_ParCSRBlockMatrixCommPkg(matrix) = NULL; hypre_ParCSRBlockMatrixCommPkgT(matrix) = NULL; /* set defaults */ hypre_ParCSRBlockMatrixOwnsData(matrix) = 1; hypre_ParCSRBlockMatrixOwnsRowStarts(matrix) = 1; hypre_ParCSRBlockMatrixOwnsColStarts(matrix) = 1; if (row_starts == col_starts) hypre_ParCSRBlockMatrixOwnsColStarts(matrix) = 0; return matrix; }
hypre_ParVector * hypre_ParVectorCreateFromBlock( MPI_Comm comm, HYPRE_Int p_global_size, HYPRE_Int *p_partitioning, HYPRE_Int block_size) { hypre_ParVector *vector; HYPRE_Int num_procs, my_id, i; HYPRE_Int global_size; HYPRE_Int *new_partitioning; /* need to create a new partitioning - son't want to write over what is passed in */ global_size = p_global_size*block_size; vector = hypre_CTAlloc(hypre_ParVector, 1); hypre_MPI_Comm_rank(comm,&my_id); hypre_MPI_Comm_size(comm,&num_procs); if (!p_partitioning) { #ifdef HYPRE_NO_GLOBAL_PARTITION hypre_GenerateLocalPartitioning(global_size, num_procs, my_id, &new_partitioning); #else hypre_GeneratePartitioning(global_size, num_procs, &new_partitioning); #endif } else /* adjust for block_size */ { #ifdef HYPRE_NO_GLOBAL_PARTITION new_partitioning = hypre_CTAlloc(HYPRE_Int, 2); for(i = 0; i < 2; i++) { new_partitioning[i] = p_partitioning[i]*block_size; } #else new_partitioning = hypre_CTAlloc(HYPRE_Int, num_procs + 1); for(i = 0; i < num_procs + 1; i++) { new_partitioning[i] = p_partitioning[i]*block_size; } #endif } hypre_ParVectorComm(vector) = comm; hypre_ParVectorGlobalSize(vector) = global_size; #ifdef HYPRE_NO_GLOBAL_PARTITION hypre_ParVectorFirstIndex(vector) = new_partitioning[0]; hypre_ParVectorLastIndex(vector) = new_partitioning[1]-1; hypre_ParVectorPartitioning(vector) = new_partitioning; hypre_ParVectorLocalVector(vector) = hypre_SeqVectorCreate(new_partitioning[1]-new_partitioning[0]); #else hypre_ParVectorFirstIndex(vector) = new_partitioning[my_id]; hypre_ParVectorLastIndex(vector) = new_partitioning[my_id+1] -1; hypre_ParVectorPartitioning(vector) = new_partitioning; hypre_ParVectorLocalVector(vector) = hypre_SeqVectorCreate(new_partitioning[my_id+1]-new_partitioning[my_id]); #endif /* set defaults */ hypre_ParVectorOwnsData(vector) = 1; hypre_ParVectorOwnsPartitioning(vector) = 1; return vector; }
HYPRE_ParCSRMatrix GenerateDifConv( MPI_Comm comm, HYPRE_Int nx, HYPRE_Int ny, HYPRE_Int nz, HYPRE_Int P, HYPRE_Int Q, HYPRE_Int R, HYPRE_Int p, HYPRE_Int q, HYPRE_Int r, double *value ) { hypre_ParCSRMatrix *A; hypre_CSRMatrix *diag; hypre_CSRMatrix *offd; HYPRE_Int *diag_i; HYPRE_Int *diag_j; double *diag_data; HYPRE_Int *offd_i; HYPRE_Int *offd_j; double *offd_data; HYPRE_Int *global_part; HYPRE_Int ix, iy, iz; HYPRE_Int cnt, o_cnt; HYPRE_Int local_num_rows; HYPRE_Int *col_map_offd; HYPRE_Int row_index; HYPRE_Int i,j; HYPRE_Int nx_local, ny_local, nz_local; HYPRE_Int nx_size, ny_size, nz_size; HYPRE_Int num_cols_offd; HYPRE_Int grid_size; HYPRE_Int *nx_part; HYPRE_Int *ny_part; HYPRE_Int *nz_part; HYPRE_Int num_procs, my_id; HYPRE_Int P_busy, Q_busy, R_busy; hypre_MPI_Comm_size(comm,&num_procs); hypre_MPI_Comm_rank(comm,&my_id); grid_size = nx*ny*nz; hypre_GeneratePartitioning(nx,P,&nx_part); hypre_GeneratePartitioning(ny,Q,&ny_part); hypre_GeneratePartitioning(nz,R,&nz_part); global_part = hypre_CTAlloc(HYPRE_Int,P*Q*R+1); global_part[0] = 0; cnt = 1; for (iz = 0; iz < R; iz++) { nz_size = nz_part[iz+1]-nz_part[iz]; for (iy = 0; iy < Q; iy++) { ny_size = ny_part[iy+1]-ny_part[iy]; for (ix = 0; ix < P; ix++) { nx_size = nx_part[ix+1] - nx_part[ix]; global_part[cnt] = global_part[cnt-1]; global_part[cnt++] += nx_size*ny_size*nz_size; } } } nx_local = nx_part[p+1] - nx_part[p]; ny_local = ny_part[q+1] - ny_part[q]; nz_local = nz_part[r+1] - nz_part[r]; my_id = r*(P*Q) + q*P + p; num_procs = P*Q*R; local_num_rows = nx_local*ny_local*nz_local; diag_i = hypre_CTAlloc(HYPRE_Int, local_num_rows+1); offd_i = hypre_CTAlloc(HYPRE_Int, local_num_rows+1); P_busy = hypre_min(nx,P); Q_busy = hypre_min(ny,Q); R_busy = hypre_min(nz,R); num_cols_offd = 0; if (p) num_cols_offd += ny_local*nz_local; if (p < P_busy-1) num_cols_offd += ny_local*nz_local; if (q) num_cols_offd += nx_local*nz_local; if (q < Q_busy-1) num_cols_offd += nx_local*nz_local; if (r) num_cols_offd += nx_local*ny_local; if (r < R_busy-1) num_cols_offd += nx_local*ny_local; if (!local_num_rows) num_cols_offd = 0; col_map_offd = hypre_CTAlloc(HYPRE_Int, num_cols_offd); cnt = 1; o_cnt = 1; diag_i[0] = 0; offd_i[0] = 0; for (iz = nz_part[r]; iz < nz_part[r+1]; iz++) { for (iy = ny_part[q]; iy < ny_part[q+1]; iy++) { for (ix = nx_part[p]; ix < nx_part[p+1]; ix++) { diag_i[cnt] = diag_i[cnt-1]; offd_i[o_cnt] = offd_i[o_cnt-1]; diag_i[cnt]++; if (iz > nz_part[r]) diag_i[cnt]++; else { if (iz) { offd_i[o_cnt]++; } } if (iy > ny_part[q]) diag_i[cnt]++; else { if (iy) { offd_i[o_cnt]++; } } if (ix > nx_part[p]) diag_i[cnt]++; else { if (ix) { offd_i[o_cnt]++; } } if (ix+1 < nx_part[p+1]) diag_i[cnt]++; else { if (ix+1 < nx) { offd_i[o_cnt]++; } } if (iy+1 < ny_part[q+1]) diag_i[cnt]++; else { if (iy+1 < ny) { offd_i[o_cnt]++; } } if (iz+1 < nz_part[r+1]) diag_i[cnt]++; else { if (iz+1 < nz) { offd_i[o_cnt]++; } } cnt++; o_cnt++; } } } diag_j = hypre_CTAlloc(HYPRE_Int, diag_i[local_num_rows]); diag_data = hypre_CTAlloc(double, diag_i[local_num_rows]); if (num_procs > 1) { offd_j = hypre_CTAlloc(HYPRE_Int, offd_i[local_num_rows]); offd_data = hypre_CTAlloc(double, offd_i[local_num_rows]); }
hypre_CSRMatrix * hypre_GenerateDifConv( HYPRE_Int nx, HYPRE_Int ny, HYPRE_Int nz, HYPRE_Int P, HYPRE_Int Q, HYPRE_Int R, HYPRE_Real *value ) { hypre_CSRMatrix *A; HYPRE_Int *A_i; HYPRE_Int *A_j; HYPRE_Real *A_data; HYPRE_Int *global_part; HYPRE_Int ix, iy, iz; HYPRE_Int p, q, r; HYPRE_Int cnt; HYPRE_Int num_rows; HYPRE_Int row_index; HYPRE_Int nx_size, ny_size, nz_size; HYPRE_Int *nx_part; HYPRE_Int *ny_part; HYPRE_Int *nz_part; num_rows = nx*ny*nz; hypre_GeneratePartitioning(nx,P,&nx_part); hypre_GeneratePartitioning(ny,Q,&ny_part); hypre_GeneratePartitioning(nz,R,&nz_part); global_part = hypre_CTAlloc(HYPRE_Int,P*Q*R+1); global_part[0] = 0; cnt = 1; for (iz = 0; iz < R; iz++) { nz_size = nz_part[iz+1]-nz_part[iz]; for (iy = 0; iy < Q; iy++) { ny_size = ny_part[iy+1]-ny_part[iy]; for (ix = 0; ix < P; ix++) { nx_size = nx_part[ix+1] - nx_part[ix]; global_part[cnt] = global_part[cnt-1]; global_part[cnt++] += nx_size*ny_size*nz_size; } } } A_i = hypre_CTAlloc(HYPRE_Int, num_rows+1); cnt = 1; A_i[0] = 0; for (r = 0; r < R; r++) { for (q = 0; q < Q; q++) { for (p = 0; p < P; p++) { for (iz = nz_part[r]; iz < nz_part[r+1]; iz++) { for (iy = ny_part[q]; iy < ny_part[q+1]; iy++) { for (ix = nx_part[p]; ix < nx_part[p+1]; ix++) { A_i[cnt] = A_i[cnt-1]; A_i[cnt]++; if (iz > nz_part[r]) A_i[cnt]++; else { if (iz) { A_i[cnt]++; } } if (iy > ny_part[q]) A_i[cnt]++; else { if (iy) { A_i[cnt]++; } } if (ix > nx_part[p]) A_i[cnt]++; else { if (ix) { A_i[cnt]++; } } if (ix+1 < nx_part[p+1]) A_i[cnt]++; else { if (ix+1 < nx) { A_i[cnt]++; } } if (iy+1 < ny_part[q+1]) A_i[cnt]++; else { if (iy+1 < ny) { A_i[cnt]++; } } if (iz+1 < nz_part[r+1]) A_i[cnt]++; else { if (iz+1 < nz) { A_i[cnt]++; } } cnt++; } } } } } } A_j = hypre_CTAlloc(HYPRE_Int, A_i[num_rows]); A_data = hypre_CTAlloc(HYPRE_Real, A_i[num_rows]); row_index = 0; cnt = 0; for (r = 0; r < R; r++) { for (q = 0; q < Q; q++) { ny_size = ny_part[q+1]-ny_part[q]; for (p = 0; p < P; p++) { nx_size = nx_part[p+1] - nx_part[p]; for (iz = nz_part[r]; iz < nz_part[r+1]; iz++) { for (iy = ny_part[q]; iy < ny_part[q+1]; iy++) { for (ix = nx_part[p]; ix < nx_part[p+1]; ix++) { A_j[cnt] = row_index; A_data[cnt++] = value[0]; if (iz > nz_part[r]) { A_j[cnt] = row_index-nx_size*ny_size; A_data[cnt++] = value[3]; } else { if (iz) { A_j[cnt] = map(ix,iy,iz-1,p,q,r-1,P,Q,R, nx_part,ny_part,nz_part,global_part); A_data[cnt++] = value[3]; } } if (iy > ny_part[q]) { A_j[cnt] = row_index-nx_size; A_data[cnt++] = value[2]; } else { if (iy) { A_j[cnt] = map(ix,iy-1,iz,p,q-1,r,P,Q,R, nx_part,ny_part,nz_part,global_part); A_data[cnt++] = value[2]; } } if (ix > nx_part[p]) { A_j[cnt] = row_index-1; A_data[cnt++] = value[1]; } else { if (ix) { A_j[cnt] = map(ix-1,iy,iz,p-1,q,r,P,Q,R, nx_part,ny_part,nz_part,global_part); A_data[cnt++] = value[1]; } } if (ix+1 < nx_part[p+1]) { A_j[cnt] = row_index+1; A_data[cnt++] = value[4]; } else { if (ix+1 < nx) { A_j[cnt] = map(ix+1,iy,iz,p+1,q,r,P,Q,R, nx_part,ny_part,nz_part,global_part); A_data[cnt++] = value[4]; } } if (iy+1 < ny_part[q+1]) { A_j[cnt] = row_index+nx_size; A_data[cnt++] = value[5]; } else { if (iy+1 < ny) { A_j[cnt] = map(ix,iy+1,iz,p,q+1,r,P,Q,R, nx_part,ny_part,nz_part,global_part); A_data[cnt++] = value[5]; } } if (iz+1 < nz_part[r+1]) { A_j[cnt] = row_index+nx_size*ny_size; A_data[cnt++] = value[6]; } else { if (iz+1 < nz) { A_j[cnt] = map(ix,iy,iz+1,p,q,r+1,P,Q,R, nx_part,ny_part,nz_part,global_part); A_data[cnt++] = value[6]; } } row_index++; } } } } } } A = hypre_CSRMatrixCreate(num_rows, num_rows, A_i[num_rows]); hypre_CSRMatrixI(A) = A_i; hypre_CSRMatrixJ(A) = A_j; hypre_CSRMatrixData(A) = A_data; hypre_TFree(nx_part); hypre_TFree(ny_part); hypre_TFree(nz_part); hypre_TFree(global_part); return A; }
hypre_CSRMatrix * hypre_GenerateLaplacian9pt( HYPRE_Int nx, HYPRE_Int ny, HYPRE_Int P, HYPRE_Int Q, double *value ) { hypre_CSRMatrix *A; HYPRE_Int *A_i; HYPRE_Int *A_j; double *A_data; HYPRE_Int *global_part; HYPRE_Int ix, iy; HYPRE_Int p, q; HYPRE_Int cnt; HYPRE_Int num_rows; HYPRE_Int row_index; HYPRE_Int nx_size, ny_size; HYPRE_Int *nx_part; HYPRE_Int *ny_part; num_rows = nx*ny; hypre_GeneratePartitioning(nx,P,&nx_part); hypre_GeneratePartitioning(ny,Q,&ny_part); global_part = hypre_CTAlloc(HYPRE_Int,P*Q+1); global_part[0] = 0; cnt = 1; for (iy = 0; iy < Q; iy++) { ny_size = ny_part[iy+1]-ny_part[iy]; for (ix = 0; ix < P; ix++) { nx_size = nx_part[ix+1] - nx_part[ix]; global_part[cnt] = global_part[cnt-1]; global_part[cnt++] += nx_size*ny_size; } } A_i = hypre_CTAlloc(HYPRE_Int,num_rows+1); cnt = 0; A_i[0] = 0; for (q = 0; q < Q; q++) { for (p=0; p < P; p++) { for (iy = ny_part[q]; iy < ny_part[q+1]; iy++) { for (ix = nx_part[p]; ix < nx_part[p+1]; ix++) { cnt++; A_i[cnt] = A_i[cnt-1]; A_i[cnt]++; if (iy > ny_part[q]) { A_i[cnt]++; if (ix > nx_part[p]) { A_i[cnt]++; } else { if (ix) A_i[cnt]++; } if (ix < nx_part[p+1]-1) { A_i[cnt]++; } else { if (ix+1 < nx) A_i[cnt]++; } } else { if (iy) { A_i[cnt]++; if (ix > nx_part[p]) { A_i[cnt]++; } else if (ix) { A_i[cnt]++; } if (ix < nx_part[p+1]-1) { A_i[cnt]++; } else if (ix < nx-1) { A_i[cnt]++; } } } if (ix > nx_part[p]) A_i[cnt]++; else { if (ix) { A_i[cnt]++; } } if (ix+1 < nx_part[p+1]) A_i[cnt]++; else { if (ix+1 < nx) { A_i[cnt]++; } } if (iy+1 < ny_part[q+1]) { A_i[cnt]++; if (ix > nx_part[p]) { A_i[cnt]++; } else { if (ix) A_i[cnt]++; } if (ix < nx_part[p+1]-1) { A_i[cnt]++; } else { if (ix+1 < nx) A_i[cnt]++; } } else { if (iy+1 < ny) { A_i[cnt]++; if (ix > nx_part[p]) { A_i[cnt]++; } else if (ix) { A_i[cnt]++; } if (ix < nx_part[p+1]-1) { A_i[cnt]++; } else if (ix < nx-1) { A_i[cnt]++; } } } } } } } A_j = hypre_CTAlloc(HYPRE_Int, A_i[num_rows]); A_data = hypre_CTAlloc(double, A_i[num_rows]); row_index = 0; cnt = 0; for (q=0; q < Q; q++) { for (p=0; p < P; p++) { for (iy = ny_part[q]; iy < ny_part[q+1]; iy++) { for (ix = nx_part[p]; ix < nx_part[p+1]; ix++) { nx_size = nx_part[p+1]-nx_part[p]; A_j[cnt] = row_index; A_data[cnt++] = value[0]; if (iy > ny_part[q]) { if (ix > nx_part[p]) { A_j[cnt] = row_index-nx_size-1 ; A_data[cnt++] = value[1]; } else { if (ix) { A_j[cnt] = map2(ix-1,iy-1,p-1,q,P,Q, nx_part,ny_part,global_part); A_data[cnt++] = value[1]; } } A_j[cnt] = row_index-nx_size; A_data[cnt++] = value[1]; if (ix < nx_part[p+1]-1) { A_j[cnt] = row_index-nx_size+1 ; A_data[cnt++] = value[1]; } else { if (ix+1 < nx) { A_j[cnt] = map2(ix+1,iy-1,p+1,q,P,Q, nx_part,ny_part,global_part); A_data[cnt++] = value[1]; } } } else { if (iy) { if (ix > nx_part[p]) { A_j[cnt] = map2(ix-1,iy-1,p,q-1,P,Q, nx_part,ny_part,global_part); A_data[cnt++] = value[1]; } else if (ix) { A_j[cnt] = map2(ix-1,iy-1,p-1,q-1,P,Q, nx_part,ny_part,global_part); A_data[cnt++] = value[1]; } A_j[cnt] = map2(ix,iy-1,p,q-1,P,Q, nx_part,ny_part,global_part); A_data[cnt++] = value[1]; if (ix < nx_part[p+1]-1) { A_j[cnt] = map2(ix+1,iy-1,p,q-1,P,Q, nx_part,ny_part,global_part); A_data[cnt++] = value[1]; } else if (ix+1 < nx) { A_j[cnt] = map2(ix+1,iy-1,p+1,q-1,P,Q, nx_part,ny_part,global_part); A_data[cnt++] = value[1]; } } } if (ix > nx_part[p]) { A_j[cnt] = row_index-1; A_data[cnt++] = value[1]; } else { if (ix) { A_j[cnt] = map2(ix-1,iy,p-1,q,P,Q, nx_part,ny_part,global_part); A_data[cnt++] = value[1]; } } if (ix+1 < nx_part[p+1]) { A_j[cnt] = row_index+1; A_data[cnt++] = value[1]; } else { if (ix+1 < nx) { A_j[cnt] = map2(ix+1,iy,p+1,q,P,Q, nx_part,ny_part,global_part); A_data[cnt++] = value[1]; } } if (iy+1 < ny_part[q+1]) { if (ix > nx_part[p]) { A_j[cnt] = row_index+nx_size-1 ; A_data[cnt++] = value[1]; } else { if (ix) { A_j[cnt] = map2(ix-1,iy+1,p-1,q,P,Q, nx_part,ny_part,global_part); A_data[cnt++] = value[1]; } } A_j[cnt] = row_index+nx_size; A_data[cnt++] = value[1]; if (ix < nx_part[p+1]-1) { A_j[cnt] = row_index+nx_size+1 ; A_data[cnt++] = value[1]; } else { if (ix+1 < nx) { A_j[cnt] = map2(ix+1,iy+1,p+1,q,P,Q, nx_part,ny_part,global_part); A_data[cnt++] = value[1]; } } } else { if (iy+1 < ny) { if (ix > nx_part[p]) { A_j[cnt] = map2(ix-1,iy+1,p,q+1,P,Q, nx_part,ny_part,global_part); A_data[cnt++] = value[1]; } else if (ix) { A_j[cnt] = map2(ix-1,iy+1,p-1,q+1,P,Q, nx_part,ny_part,global_part); A_data[cnt++] = value[1]; } A_j[cnt] = map2(ix,iy+1,p,q+1,P,Q, nx_part,ny_part,global_part); A_data[cnt++] = value[1]; if (ix < nx_part[p+1]-1) { A_j[cnt] = map2(ix+1,iy+1,p,q+1,P,Q, nx_part,ny_part,global_part); A_data[cnt++] = value[1]; } else if (ix < nx-1) { A_j[cnt] = map2(ix+1,iy+1,p+1,q+1,P,Q, nx_part,ny_part,global_part); A_data[cnt++] = value[1]; } } } row_index++; } } } } A = hypre_CSRMatrixCreate(num_rows, num_rows, A_i[num_rows]); hypre_CSRMatrixI(A) = A_i; hypre_CSRMatrixJ(A) = A_j; hypre_CSRMatrixData(A) = A_data; hypre_TFree(nx_part); hypre_TFree(ny_part); hypre_TFree(global_part); return A; }
HYPRE_ParCSRMatrix GenerateSysLaplacianVCoef( MPI_Comm comm, HYPRE_Int nx, HYPRE_Int ny, HYPRE_Int nz, HYPRE_Int P, HYPRE_Int Q, HYPRE_Int R, HYPRE_Int p, HYPRE_Int q, HYPRE_Int r, HYPRE_Int num_fun, HYPRE_Real *mtrx, HYPRE_Real *value ) { hypre_ParCSRMatrix *A; hypre_CSRMatrix *diag; hypre_CSRMatrix *offd; HYPRE_Int *diag_i; HYPRE_Int *diag_j; HYPRE_Real *diag_data; HYPRE_Int *offd_i; HYPRE_Int *offd_j; HYPRE_Real *offd_data; HYPRE_Int *global_part; HYPRE_Int ix, iy, iz; HYPRE_Int cnt, o_cnt; HYPRE_Int local_num_rows; HYPRE_Int *col_map_offd; HYPRE_Int row_index, row, col; HYPRE_Int index, diag_index; HYPRE_Int i,j; HYPRE_Int nx_local, ny_local, nz_local; HYPRE_Int nx_size, ny_size, nz_size; HYPRE_Int num_cols_offd; HYPRE_Int grid_size; HYPRE_Int local_grid_size; HYPRE_Int first_j, j_ind; HYPRE_Int num_coeffs, num_offd_coeffs; HYPRE_Int *nx_part; HYPRE_Int *ny_part; HYPRE_Int *nz_part; HYPRE_Int num_procs, my_id; HYPRE_Int P_busy, Q_busy, R_busy; HYPRE_Real val; /* for indexing in values */ HYPRE_Int sz = num_fun*num_fun; hypre_MPI_Comm_size(comm,&num_procs); hypre_MPI_Comm_rank(comm,&my_id); grid_size = nx*ny*nz; hypre_GeneratePartitioning(nx,P,&nx_part); hypre_GeneratePartitioning(ny,Q,&ny_part); hypre_GeneratePartitioning(nz,R,&nz_part); global_part = hypre_CTAlloc(HYPRE_Int,P*Q*R+1); global_part[0] = 0; cnt = 1; for (iz = 0; iz < R; iz++) { nz_size = nz_part[iz+1]-nz_part[iz]; for (iy = 0; iy < Q; iy++) { ny_size = ny_part[iy+1]-ny_part[iy]; for (ix = 0; ix < P; ix++) { nx_size = nx_part[ix+1] - nx_part[ix]; global_part[cnt] = global_part[cnt-1]; global_part[cnt++] += nx_size*ny_size*nz_size; } } } nx_local = nx_part[p+1] - nx_part[p]; ny_local = ny_part[q+1] - ny_part[q]; nz_local = nz_part[r+1] - nz_part[r]; my_id = r*(P*Q) + q*P + p; num_procs = P*Q*R; local_grid_size = nx_local*ny_local*nz_local; local_num_rows = num_fun*local_grid_size; diag_i = hypre_CTAlloc(HYPRE_Int, local_num_rows+1); offd_i = hypre_CTAlloc(HYPRE_Int, local_num_rows+1); P_busy = hypre_min(nx,P); Q_busy = hypre_min(ny,Q); R_busy = hypre_min(nz,R); num_cols_offd = 0; if (p) num_cols_offd += ny_local*nz_local; if (p < P_busy-1) num_cols_offd += ny_local*nz_local; if (q) num_cols_offd += nx_local*nz_local; if (q < Q_busy-1) num_cols_offd += nx_local*nz_local; if (r) num_cols_offd += nx_local*ny_local; if (r < R_busy-1) num_cols_offd += nx_local*ny_local; num_cols_offd *= num_fun; if (!local_num_rows) num_cols_offd = 0; col_map_offd = hypre_CTAlloc(HYPRE_Int, num_cols_offd); cnt = 1; diag_i[0] = 0; offd_i[0] = 0; for (iz = nz_part[r]; iz < nz_part[r+1]; iz++) { for (iy = ny_part[q]; iy < ny_part[q+1]; iy++) { for (ix = nx_part[p]; ix < nx_part[p+1]; ix++) { diag_i[cnt] = diag_i[cnt-1]; offd_i[cnt] = offd_i[cnt-1]; diag_i[cnt] += num_fun; if (iz > nz_part[r]) diag_i[cnt] += num_fun; else { if (iz) { offd_i[cnt] += num_fun; } } if (iy > ny_part[q]) diag_i[cnt] += num_fun; else { if (iy) { offd_i[cnt] += num_fun; } } if (ix > nx_part[p]) diag_i[cnt] += num_fun; else { if (ix) { offd_i[cnt] += num_fun; } } if (ix+1 < nx_part[p+1]) diag_i[cnt] += num_fun; else { if (ix+1 < nx) { offd_i[cnt] += num_fun; } } if (iy+1 < ny_part[q+1]) diag_i[cnt] += num_fun; else { if (iy+1 < ny) { offd_i[cnt] += num_fun; } } if (iz+1 < nz_part[r+1]) diag_i[cnt] += num_fun; else { if (iz+1 < nz) { offd_i[cnt] += num_fun; } } num_coeffs = diag_i[cnt]-diag_i[cnt-1]; num_offd_coeffs = offd_i[cnt]-offd_i[cnt-1]; cnt++; for (i=1; i < num_fun; i++) { diag_i[cnt] = diag_i[cnt-1]+num_coeffs; offd_i[cnt] = offd_i[cnt-1]+num_offd_coeffs; cnt++; } } } } diag_j = hypre_CTAlloc(HYPRE_Int, diag_i[local_num_rows]); diag_data = hypre_CTAlloc(HYPRE_Real, diag_i[local_num_rows]); if (num_procs > 1) { offd_j = hypre_CTAlloc(HYPRE_Int, offd_i[local_num_rows]); offd_data = hypre_CTAlloc(HYPRE_Real, offd_i[local_num_rows]); } row_index = 0; for (iz = nz_part[r]; iz < nz_part[r+1]; iz++) { for (iy = ny_part[q]; iy < ny_part[q+1]; iy++) { for (ix = nx_part[p]; ix < nx_part[p+1]; ix++) { cnt = diag_i[row_index];; o_cnt = offd_i[row_index];; num_coeffs = diag_i[row_index+1]-diag_i[row_index]; num_offd_coeffs = offd_i[row_index+1]-offd_i[row_index]; first_j = row_index; for (i=0; i < num_fun; i++) { for (j=0; j < num_fun; j++) { j_ind = cnt+i*num_coeffs+j; diag_j[j_ind] = first_j+j; diag_data[j_ind] = value[0*sz + i*num_fun+j]*mtrx[i*num_fun+j]; } } cnt += num_fun; if (iz > nz_part[r]) { first_j = row_index-nx_local*ny_local*num_fun; for (i=0; i < num_fun; i++) { for (j=0; j < num_fun; j++) { j_ind = cnt+i*num_coeffs+j; diag_j[j_ind] = first_j+j; diag_data[j_ind] = value[3*sz + i*num_fun+j]*mtrx[i*num_fun+j]; } } cnt += num_fun; } else { if (iz) { first_j = num_fun*hypre_map(ix,iy,iz-1,p,q,r-1,P,Q,R, nx_part,ny_part,nz_part,global_part); for (i=0; i < num_fun; i++) { for (j=0; j < num_fun; j++) { j_ind = o_cnt+i*num_offd_coeffs+j; offd_j[j_ind] = first_j+j; offd_data[j_ind] = value[3*sz + i*num_fun+j]*mtrx[i*num_fun+j]; } } o_cnt += num_fun; } } if (iy > ny_part[q]) { first_j = row_index-nx_local*num_fun; for (i=0; i < num_fun; i++) { for (j=0; j < num_fun; j++) { j_ind = cnt+i*num_coeffs+j; diag_j[j_ind] = first_j+j; diag_data[j_ind] = value[2*sz + i*num_fun+j]*mtrx[i*num_fun+j]; } } cnt += num_fun; } else { if (iy) { first_j = num_fun*hypre_map(ix,iy-1,iz,p,q-1,r,P,Q,R, nx_part,ny_part,nz_part,global_part); for (i=0; i < num_fun; i++) { for (j=0; j < num_fun; j++) { j_ind = o_cnt+i*num_offd_coeffs+j; offd_j[j_ind] = first_j+j; offd_data[j_ind] = value[2*sz + i*num_fun+j]*mtrx[i*num_fun+j]; } } o_cnt += num_fun; } } if (ix > nx_part[p]) { first_j = row_index-num_fun; for (i=0; i < num_fun; i++) { for (j=0; j < num_fun; j++) { j_ind = cnt+i*num_coeffs+j; diag_j[j_ind] = first_j+j; diag_data[j_ind] = value[1*sz + i*num_fun+j]*mtrx[i*num_fun+j]; } } cnt += num_fun; } else { if (ix) { first_j = num_fun*hypre_map(ix-1,iy,iz,p-1,q,r,P,Q,R, nx_part,ny_part,nz_part,global_part); for (i=0; i < num_fun; i++) { for (j=0; j < num_fun; j++) { j_ind = o_cnt+i*num_offd_coeffs+j; offd_j[j_ind] = first_j+j; offd_data[j_ind] = value[1*sz + i*num_fun+j]*mtrx[i*num_fun+j]; } } o_cnt += num_fun; } } if (ix+1 < nx_part[p+1]) { first_j = row_index+num_fun; for (i=0; i < num_fun; i++) { for (j=0; j < num_fun; j++) { j_ind = cnt+i*num_coeffs+j; diag_j[j_ind] = first_j+j; diag_data[j_ind] = value[1*sz + i*num_fun+j]*mtrx[i*num_fun+j]; } } cnt += num_fun; } else { if (ix+1 < nx) { first_j = num_fun*hypre_map(ix+1,iy,iz,p+1,q,r,P,Q,R, nx_part,ny_part,nz_part,global_part); for (i=0; i < num_fun; i++) { for (j=0; j < num_fun; j++) { j_ind = o_cnt+i*num_offd_coeffs+j; offd_j[j_ind] = first_j+j; offd_data[j_ind] = value[1*sz + i*num_fun+j]*mtrx[i*num_fun+j]; } } o_cnt += num_fun; } } if (iy+1 < ny_part[q+1]) { first_j = row_index+nx_local*num_fun; for (i=0; i < num_fun; i++) { for (j=0; j < num_fun; j++) { j_ind = cnt+i*num_coeffs+j; diag_j[j_ind] = first_j+j; diag_data[j_ind] = value[2*sz + i*num_fun+j]*mtrx[i*num_fun+j]; } } cnt += num_fun; } else { if (iy+1 < ny) { first_j = num_fun*hypre_map(ix,iy+1,iz,p,q+1,r,P,Q,R, nx_part,ny_part,nz_part,global_part); for (i=0; i < num_fun; i++) { for (j=0; j < num_fun; j++) { j_ind = o_cnt+i*num_offd_coeffs+j; offd_j[j_ind] = first_j+j; offd_data[j_ind] = value[2*sz + i*num_fun+j]*mtrx[i*num_fun+j]; } } o_cnt += num_fun; } } if (iz+1 < nz_part[r+1]) { first_j = row_index+nx_local*ny_local*num_fun; for (i=0; i < num_fun; i++) { for (j=0; j < num_fun; j++) { j_ind = cnt+i*num_coeffs+j; diag_j[j_ind] = first_j+j; diag_data[j_ind] = value[3*sz + i*num_fun+j]*mtrx[i*num_fun+j]; } } cnt += num_fun; } else { if (iz+1 < nz) { first_j = num_fun*hypre_map(ix,iy,iz+1,p,q,r+1,P,Q,R, nx_part,ny_part,nz_part,global_part); for (i=0; i < num_fun; i++) { for (j=0; j < num_fun; j++) { j_ind = o_cnt+i*num_offd_coeffs+j; offd_j[j_ind] = first_j+j; offd_data[j_ind] = value[3*sz + i*num_fun+j]*mtrx[i*num_fun+j]; } } o_cnt += num_fun; } } row_index += num_fun; } } } if (num_procs > 1) { cnt = 0; for (i=0; i < local_num_rows; i+=num_fun) { for (j=offd_i[i]; j < offd_i[i+1]; j++) { col_map_offd[cnt++] = offd_j[j]; } } hypre_qsort0(col_map_offd, 0, num_cols_offd-1); for (i=0; i < num_fun*num_cols_offd; i++) for (j=hypre_min(0,abs(i-num_fun)); j < num_cols_offd; j++) if (offd_j[i] == col_map_offd[j]) { offd_j[i] = j; break; } } for (i=0; i < num_procs+1; i++) global_part[i] *= num_fun; for (j=1; j< num_fun; j++) { for (i=0; i<local_grid_size; i++) { row = i*num_fun+j; diag_index = diag_i[row]; index = diag_index+j; val = diag_data[diag_index]; col = diag_j[diag_index]; diag_data[diag_index] = diag_data[index]; diag_j[diag_index] = diag_j[index]; diag_data[index] = val; diag_j[index] = col; } } #ifdef HYPRE_NO_GLOBAL_PARTITION /* ideally we would use less storage earlier in this function, but this is fine for testing */ { HYPRE_Int tmp1, tmp2; tmp1 = global_part[my_id]; tmp2 = global_part[my_id + 1]; hypre_TFree(global_part); global_part = hypre_CTAlloc(HYPRE_Int, 2); global_part[0] = tmp1; global_part[1] = tmp2; } #endif A = hypre_ParCSRMatrixCreate(comm, num_fun*grid_size, num_fun*grid_size, global_part, global_part, num_cols_offd, diag_i[local_num_rows], offd_i[local_num_rows]); hypre_ParCSRMatrixColMapOffd(A) = col_map_offd; diag = hypre_ParCSRMatrixDiag(A); hypre_CSRMatrixI(diag) = diag_i; hypre_CSRMatrixJ(diag) = diag_j; hypre_CSRMatrixData(diag) = diag_data; offd = hypre_ParCSRMatrixOffd(A); hypre_CSRMatrixI(offd) = offd_i; if (num_cols_offd) { hypre_CSRMatrixJ(offd) = offd_j; hypre_CSRMatrixData(offd) = offd_data; } hypre_TFree(nx_part); hypre_TFree(ny_part); hypre_TFree(nz_part); return (HYPRE_ParCSRMatrix) A; }
HYPRE_ParCSRMatrix GenerateLaplacian( MPI_Comm comm, HYPRE_Int nx, HYPRE_Int ny, HYPRE_Int nz, HYPRE_Int P, HYPRE_Int Q, HYPRE_Int R, HYPRE_Int p, HYPRE_Int q, HYPRE_Int r, HYPRE_Real *value ) { hypre_ParCSRMatrix *A; hypre_CSRMatrix *diag; hypre_CSRMatrix *offd; HYPRE_Int *diag_i; HYPRE_Int *diag_j; HYPRE_Real *diag_data; HYPRE_Int *offd_i; HYPRE_Int *offd_j = NULL; HYPRE_Real *offd_data = NULL; HYPRE_Int *global_part; HYPRE_Int ix, iy, iz; HYPRE_Int cnt, o_cnt; HYPRE_Int local_num_rows; HYPRE_Int *col_map_offd; HYPRE_Int row_index; HYPRE_Int i,j; HYPRE_Int nx_local, ny_local, nz_local; HYPRE_Int nx_size, ny_size, nz_size; HYPRE_Int num_cols_offd; HYPRE_Int grid_size; HYPRE_Int *nx_part; HYPRE_Int *ny_part; HYPRE_Int *nz_part; HYPRE_Int num_procs, my_id; HYPRE_Int P_busy, Q_busy, R_busy; hypre_MPI_Comm_size(comm,&num_procs); hypre_MPI_Comm_rank(comm,&my_id); grid_size = nx*ny*nz; hypre_GeneratePartitioning(nx,P,&nx_part); hypre_GeneratePartitioning(ny,Q,&ny_part); hypre_GeneratePartitioning(nz,R,&nz_part); global_part = hypre_CTAlloc(HYPRE_Int,P*Q*R+1); global_part[0] = 0; cnt = 1; for (iz = 0; iz < R; iz++) { nz_size = nz_part[iz+1]-nz_part[iz]; for (iy = 0; iy < Q; iy++) { ny_size = ny_part[iy+1]-ny_part[iy]; for (ix = 0; ix < P; ix++) { nx_size = nx_part[ix+1] - nx_part[ix]; global_part[cnt] = global_part[cnt-1]; global_part[cnt++] += nx_size*ny_size*nz_size; } } } nx_local = nx_part[p+1] - nx_part[p]; ny_local = ny_part[q+1] - ny_part[q]; nz_local = nz_part[r+1] - nz_part[r]; my_id = r*(P*Q) + q*P + p; num_procs = P*Q*R; local_num_rows = nx_local*ny_local*nz_local; diag_i = hypre_CTAlloc(HYPRE_Int, local_num_rows+1); offd_i = hypre_CTAlloc(HYPRE_Int, local_num_rows+1); P_busy = hypre_min(nx,P); Q_busy = hypre_min(ny,Q); R_busy = hypre_min(nz,R); num_cols_offd = 0; if (p) num_cols_offd += ny_local*nz_local; if (p < P_busy-1) num_cols_offd += ny_local*nz_local; if (q) num_cols_offd += nx_local*nz_local; if (q < Q_busy-1) num_cols_offd += nx_local*nz_local; if (r) num_cols_offd += nx_local*ny_local; if (r < R_busy-1) num_cols_offd += nx_local*ny_local; if (!local_num_rows) num_cols_offd = 0; col_map_offd = hypre_CTAlloc(HYPRE_Int, num_cols_offd); cnt = 1; o_cnt = 1; diag_i[0] = 0; offd_i[0] = 0; for (iz = nz_part[r]; iz < nz_part[r+1]; iz++) { for (iy = ny_part[q]; iy < ny_part[q+1]; iy++) { for (ix = nx_part[p]; ix < nx_part[p+1]; ix++) { diag_i[cnt] = diag_i[cnt-1]; offd_i[o_cnt] = offd_i[o_cnt-1]; diag_i[cnt]++; if (iz > nz_part[r]) diag_i[cnt]++; else { if (iz) { offd_i[o_cnt]++; } } if (iy > ny_part[q]) diag_i[cnt]++; else { if (iy) { offd_i[o_cnt]++; } } if (ix > nx_part[p]) diag_i[cnt]++; else { if (ix) { offd_i[o_cnt]++; } } if (ix+1 < nx_part[p+1]) diag_i[cnt]++; else { if (ix+1 < nx) { offd_i[o_cnt]++; } } if (iy+1 < ny_part[q+1]) diag_i[cnt]++; else { if (iy+1 < ny) { offd_i[o_cnt]++; } } if (iz+1 < nz_part[r+1]) diag_i[cnt]++; else { if (iz+1 < nz) { offd_i[o_cnt]++; } } cnt++; o_cnt++; } } } diag_j = hypre_CTAlloc(HYPRE_Int, diag_i[local_num_rows]); diag_data = hypre_CTAlloc(HYPRE_Real, diag_i[local_num_rows]); if (num_procs > 1) { offd_j = hypre_CTAlloc(HYPRE_Int, offd_i[local_num_rows]); offd_data = hypre_CTAlloc(HYPRE_Real, offd_i[local_num_rows]); } row_index = 0; cnt = 0; o_cnt = 0; for (iz = nz_part[r]; iz < nz_part[r+1]; iz++) { for (iy = ny_part[q]; iy < ny_part[q+1]; iy++) { for (ix = nx_part[p]; ix < nx_part[p+1]; ix++) { diag_j[cnt] = row_index; diag_data[cnt++] = value[0]; if (iz > nz_part[r]) { diag_j[cnt] = row_index-nx_local*ny_local; diag_data[cnt++] = value[3]; } else { if (iz) { offd_j[o_cnt] = hypre_map(ix,iy,iz-1,p,q,r-1,P,Q,R, nx_part,ny_part,nz_part,global_part); offd_data[o_cnt++] = value[3]; } } if (iy > ny_part[q]) { diag_j[cnt] = row_index-nx_local; diag_data[cnt++] = value[2]; } else { if (iy) { offd_j[o_cnt] = hypre_map(ix,iy-1,iz,p,q-1,r,P,Q,R, nx_part,ny_part,nz_part,global_part); offd_data[o_cnt++] = value[2]; } } if (ix > nx_part[p]) { diag_j[cnt] = row_index-1; diag_data[cnt++] = value[1]; } else { if (ix) { offd_j[o_cnt] = hypre_map(ix-1,iy,iz,p-1,q,r,P,Q,R, nx_part,ny_part,nz_part,global_part); offd_data[o_cnt++] = value[1]; } } if (ix+1 < nx_part[p+1]) { diag_j[cnt] = row_index+1; diag_data[cnt++] = value[1]; } else { if (ix+1 < nx) { offd_j[o_cnt] = hypre_map(ix+1,iy,iz,p+1,q,r,P,Q,R, nx_part,ny_part,nz_part,global_part); offd_data[o_cnt++] = value[1]; } } if (iy+1 < ny_part[q+1]) { diag_j[cnt] = row_index+nx_local; diag_data[cnt++] = value[2]; } else { if (iy+1 < ny) { offd_j[o_cnt] = hypre_map(ix,iy+1,iz,p,q+1,r,P,Q,R, nx_part,ny_part,nz_part,global_part); offd_data[o_cnt++] = value[2]; } } if (iz+1 < nz_part[r+1]) { diag_j[cnt] = row_index+nx_local*ny_local; diag_data[cnt++] = value[3]; } else { if (iz+1 < nz) { offd_j[o_cnt] = hypre_map(ix,iy,iz+1,p,q,r+1,P,Q,R, nx_part,ny_part,nz_part,global_part); offd_data[o_cnt++] = value[3]; } } row_index++; } } } if (num_procs > 1) { for (i=0; i < num_cols_offd; i++) col_map_offd[i] = offd_j[i]; hypre_qsort0(col_map_offd, 0, num_cols_offd-1); for (i=0; i < num_cols_offd; i++) for (j=0; j < num_cols_offd; j++) if (offd_j[i] == col_map_offd[j]) { offd_j[i] = j; break; } } #ifdef HYPRE_NO_GLOBAL_PARTITION /* ideally we would use less storage earlier in this function, but this is fine for testing */ { HYPRE_Int tmp1, tmp2; tmp1 = global_part[my_id]; tmp2 = global_part[my_id + 1]; hypre_TFree(global_part); global_part = hypre_CTAlloc(HYPRE_Int, 2); global_part[0] = tmp1; global_part[1] = tmp2; } #endif A = hypre_ParCSRMatrixCreate(comm, grid_size, grid_size, global_part, global_part, num_cols_offd, diag_i[local_num_rows], offd_i[local_num_rows]); hypre_ParCSRMatrixColMapOffd(A) = col_map_offd; diag = hypre_ParCSRMatrixDiag(A); hypre_CSRMatrixI(diag) = diag_i; hypre_CSRMatrixJ(diag) = diag_j; hypre_CSRMatrixData(diag) = diag_data; offd = hypre_ParCSRMatrixOffd(A); hypre_CSRMatrixI(offd) = offd_i; if (num_cols_offd) { hypre_CSRMatrixJ(offd) = offd_j; hypre_CSRMatrixData(offd) = offd_data; } hypre_TFree(nx_part); hypre_TFree(ny_part); hypre_TFree(nz_part); return (HYPRE_ParCSRMatrix) A; }
float * GenerateCoordinates( MPI_Comm comm, HYPRE_Int nx, HYPRE_Int ny, HYPRE_Int nz, HYPRE_Int P, HYPRE_Int Q, HYPRE_Int R, HYPRE_Int p, HYPRE_Int q, HYPRE_Int r, HYPRE_Int coorddim) { HYPRE_Int ix, iy, iz; HYPRE_Int cnt; HYPRE_Int nx_local, ny_local, nz_local; HYPRE_Int local_num_rows; HYPRE_Int *nx_part; HYPRE_Int *ny_part; HYPRE_Int *nz_part; float *coord=NULL; if (coorddim<1 || coorddim>3) { return NULL; } hypre_GeneratePartitioning(nx,P,&nx_part); hypre_GeneratePartitioning(ny,Q,&ny_part); hypre_GeneratePartitioning(nz,R,&nz_part); nx_local = nx_part[p+1] - nx_part[p]; ny_local = ny_part[q+1] - ny_part[q]; nz_local = nz_part[r+1] - nz_part[r]; local_num_rows = nx_local*ny_local*nz_local; coord = hypre_CTAlloc(float, coorddim*local_num_rows); cnt = 0; for (iz = nz_part[r]; iz < nz_part[r+1]; iz++) { for (iy = ny_part[q]; iy < ny_part[q+1]; iy++) { for (ix = nx_part[p]; ix < nx_part[p+1]; ix++) { /* set coordinates BM Oct 17, 2006 */ if (coord) { if (nx>1) coord[cnt++] = ix; if (ny>1) coord[cnt++] = iy; if (nz>1) coord[cnt++] = iz; } } } } hypre_TFree(nx_part); hypre_TFree(ny_part); hypre_TFree(nz_part); return coord; }