Actual source code: ex98.c

  1: static char help[] = "Tests MatMPIAIJSetPreallocationCSR()\n\n";

  3: /*
  4:   Include "petscmat.h" so that we can use matrices.  Note that this file
  5:   automatically includes:
  6:      petscsys.h - base PETSc routines
  7:      petscvec.h - vectors
  8:      petscis.h  - index sets
  9: */
 10: #include <petscmat.h>

 12: int main(int argc, char **args)
 13: {
 14:   Mat         A;
 15:   PetscInt   *ia, *ja;
 16:   PetscMPIInt rank, size;

 18:   PetscFunctionBeginUser;
 19:   PetscCall(PetscInitialize(&argc, &args, NULL, help));
 20:   PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD, &size));
 21:   PetscCheck(size == 4, PETSC_COMM_WORLD, PETSC_ERR_WRONG_MPI_SIZE, "Must run with 4 processors");
 22:   PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank));

 24:   PetscCall(PetscMalloc1(5, &ia));
 25:   PetscCall(PetscMalloc1(16, &ja));
 26:   if (rank == 0) {
 27:     ja[0] = 1;
 28:     ja[1] = 4;
 29:     ja[2] = 0;
 30:     ja[3] = 2;
 31:     ja[4] = 5;
 32:     ja[5] = 1;
 33:     ja[6] = 3;
 34:     ja[7] = 6;
 35:     ja[8] = 2;
 36:     ja[9] = 7;
 37:     ia[0] = 0;
 38:     ia[1] = 2;
 39:     ia[2] = 5;
 40:     ia[3] = 8;
 41:     ia[4] = 10;
 42:   } else if (rank == 1) {
 43:     ja[0]  = 0;
 44:     ja[1]  = 5;
 45:     ja[2]  = 8;
 46:     ja[3]  = 1;
 47:     ja[4]  = 4;
 48:     ja[5]  = 6;
 49:     ja[6]  = 9;
 50:     ja[7]  = 2;
 51:     ja[8]  = 5;
 52:     ja[9]  = 7;
 53:     ja[10] = 10;
 54:     ja[11] = 3;
 55:     ja[12] = 6;
 56:     ja[13] = 11;
 57:     ia[0]  = 0;
 58:     ia[1]  = 3;
 59:     ia[2]  = 7;
 60:     ia[3]  = 11;
 61:     ia[4]  = 14;
 62:   } else if (rank == 2) {
 63:     ja[0]  = 4;
 64:     ja[1]  = 9;
 65:     ja[2]  = 12;
 66:     ja[3]  = 5;
 67:     ja[4]  = 8;
 68:     ja[5]  = 10;
 69:     ja[6]  = 13;
 70:     ja[7]  = 6;
 71:     ja[8]  = 9;
 72:     ja[9]  = 11;
 73:     ja[10] = 14;
 74:     ja[11] = 7;
 75:     ja[12] = 10;
 76:     ja[13] = 15;
 77:     ia[0]  = 0;
 78:     ia[1]  = 3;
 79:     ia[2]  = 7;
 80:     ia[3]  = 11;
 81:     ia[4]  = 14;
 82:   } else {
 83:     ja[0] = 8;
 84:     ja[1] = 13;
 85:     ja[2] = 9;
 86:     ja[3] = 12;
 87:     ja[4] = 14;
 88:     ja[5] = 10;
 89:     ja[6] = 13;
 90:     ja[7] = 15;
 91:     ja[8] = 11;
 92:     ja[9] = 14;
 93:     ia[0] = 0;
 94:     ia[1] = 2;
 95:     ia[2] = 5;
 96:     ia[3] = 8;
 97:     ia[4] = 10;
 98:   }

100:   PetscCall(MatCreate(PETSC_COMM_WORLD, &A));
101:   PetscCall(MatSetSizes(A, 4, 4, 16, 16));
102:   PetscCall(MatSetType(A, MATMPIAIJ));
103:   PetscCall(MatMPIAIJSetPreallocationCSR(A, ia, ja, NULL));
104:   PetscCall(PetscFree(ia));
105:   PetscCall(PetscFree(ja));
106:   PetscCall(MatView(A, PETSC_VIEWER_STDOUT_WORLD));
107:   PetscCall(MatDestroy(&A));
108:   PetscCall(PetscFinalize());
109:   return 0;
110: }

112: /*TEST

114:    test:
115:       nsize: 4

117: TEST*/