Actual source code: ex52.c

  1: static char help[] = "Tests various routines in MatMPIBAIJ format.\n";

  3: #include <petscmat.h>

  5: int main(int argc, char **args)
  6: {
  7:   Mat         A;
  8:   PetscInt    m = 2, bs = 1, M, row, col, start, end;
  9:   PetscMPIInt rank, size;
 10:   PetscScalar data = 100;
 11:   PetscBool   flg;

 13:   PetscFunctionBeginUser;
 14:   PetscCall(PetscInitialize(&argc, &args, NULL, help));
 15:   PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank));
 16:   PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD, &size));

 18:   /* Test MatSetValues() and MatGetValues() */
 19:   PetscCall(PetscOptionsGetInt(NULL, NULL, "-mat_block_size", &bs, NULL));
 20:   PetscCall(PetscOptionsGetInt(NULL, NULL, "-mat_size", &m, NULL));

 22:   M = m * bs * size;
 23:   PetscCall(MatCreateBAIJ(PETSC_COMM_WORLD, bs, PETSC_DECIDE, PETSC_DECIDE, M, M, PETSC_DECIDE, NULL, PETSC_DECIDE, NULL, &A));

 25:   PetscCall(MatGetOwnershipRange(A, &start, &end));
 26:   PetscCall(PetscOptionsHasName(NULL, NULL, "-column_oriented", &flg));
 27:   if (flg == PETSC_TRUE) PetscCall(MatSetOption(A, MAT_ROW_ORIENTED, PETSC_FALSE));

 29:   /* inproc assembly */
 30:   for (row = start; row < end; row++) {
 31:     for (col = start; col < end; col++, data += 1) PetscCall(MatSetValues(A, 1, &row, 1, &col, &data, INSERT_VALUES));
 32:   }
 33:   PetscCall(MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY));
 34:   PetscCall(MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY));

 36:   /* offproc assembly */
 37:   data = 5.0;
 38:   row  = (M + start - 1) % M;
 39:   for (col = 0; col < M; col++) PetscCall(MatSetValues(A, 1, &row, 1, &col, &data, ADD_VALUES));
 40:   PetscCall(MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY));
 41:   PetscCall(MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY));

 43:   /* Test MatSetValuesBlocked() */
 44:   PetscCall(PetscOptionsHasName(NULL, NULL, "-test_setvaluesblocked", &flg));
 45:   if (flg == PETSC_TRUE) {
 46:     PetscScalar *bval;
 47:     PetscInt     cols[2];

 49:     row /= bs;
 50:     col     = start / bs;
 51:     cols[0] = col;
 52:     cols[1] = col + 1;
 53:     PetscCall(PetscMalloc1(2 * bs * bs, &bval));
 54:     for (PetscInt i = 0; i < 2 * bs * bs; i++) {
 55:       bval[i] = (PetscScalar)(i + 1);
 56:     }
 57:     PetscCall(MatSetValuesBlocked(A, 1, &row, 2, cols, bval, INSERT_VALUES));
 58:     PetscCall(MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY));
 59:     PetscCall(MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY));
 60:     PetscCall(PetscFree(bval));
 61:   }

 63:   PetscCall(MatView(A, PETSC_VIEWER_STDOUT_WORLD));
 64:   PetscCall(MatDestroy(&A));
 65:   PetscCall(PetscFinalize());
 66:   return 0;
 67: }

 69: /*TEST

 71:    test:
 72:       suffix: 1
 73:       nsize: 3
 74:       args: -mat_block_size 2 -test_setvaluesblocked
 75:       diff_args: -j

 77:    test:
 78:       suffix: 2
 79:       nsize: 3
 80:       args: -mat_block_size 2 -test_setvaluesblocked -column_oriented
 81:       diff_args: -j

 83:    test:
 84:       suffix: 3
 85:       nsize: 3
 86:       args: -mat_block_size 1 -test_setvaluesblocked
 87:       diff_args: -j

 89:    test:
 90:       suffix: 4
 91:       nsize: 3
 92:       args: -mat_block_size 1 -test_setvaluesblocked -column_oriented
 93:       diff_args: -j

 95: TEST*/