Actual source code: densehdf5.c


  2: /* TODO change to
  3: #include <../src/mat/impls/dense/seq/dense.h>
  4: */
  5: #include <../src/mat/impls/dense/mpi/mpidense.h>
  6: #include <petsc/private/isimpl.h>
  7: #include <petsc/private/vecimpl.h>
  8: #include <petsc/private/viewerhdf5impl.h>
  9: #include <petsclayouthdf5.h>

 11: #if defined(PETSC_HAVE_HDF5)
 12: PetscErrorCode MatLoad_Dense_HDF5(Mat mat, PetscViewer viewer)
 13: {
 14:   PetscViewer_HDF5 *hdf5;
 15:   hid_t             scalartype; /* scalar type (H5T_NATIVE_FLOAT or H5T_NATIVE_DOUBLE) */
 16:   PetscLayout       vmap;
 17:   PetscViewerFormat format;
 18:   PetscScalar      *a        = NULL;
 19:   const char       *mat_name = NULL;
 20:   MPI_Comm          comm;
 21:   PetscMPIInt       rank, size;

 23:   PetscFunctionBegin;
 24:   PetscCall(PetscViewerGetFormat(viewer, &format));
 25:   switch (format) {
 26:   case PETSC_VIEWER_HDF5_PETSC:
 27:   case PETSC_VIEWER_DEFAULT:
 28:   case PETSC_VIEWER_NATIVE:
 29:   case PETSC_VIEWER_HDF5_MAT:
 30:     break;
 31:   default:
 32:     SETERRQ(PetscObjectComm((PetscObject)mat), PETSC_ERR_SUP, "PetscViewerFormat %s not supported for HDF5 input.", PetscViewerFormats[format]);
 33:   }
 34:   hdf5 = (PetscViewer_HDF5 *)viewer->data;
 35:   /* we store dense matrix columns as blocks, like MATLAB save(filename,variables,'-v7.3') does */
 36:   hdf5->horizontal = PETSC_TRUE;

 38:   PetscCheck(((PetscObject)mat)->name, PetscObjectComm((PetscObject)mat), PETSC_ERR_SUP, "Mat name must be set with PetscObjectSetName() before MatLoad()");
 39:   #if defined(PETSC_USE_REAL_SINGLE)
 40:   scalartype = H5T_NATIVE_FLOAT;
 41:   #elif defined(PETSC_USE_REAL___FLOAT128)
 42:     #error "HDF5 output with 128 bit floats not supported."
 43:   #elif defined(PETSC_USE_REAL___FP16)
 44:     #error "HDF5 output with 16 bit floats not supported."
 45:   #else
 46:   scalartype = H5T_NATIVE_DOUBLE;
 47:   #endif

 49:   PetscCall(PetscObjectGetComm((PetscObject)mat, &comm));
 50:   PetscCallMPI(MPI_Comm_rank(comm, &rank));
 51:   PetscCallMPI(MPI_Comm_size(comm, &size));
 52:   PetscCall(PetscObjectGetName((PetscObject)mat, &mat_name));

 54:   /* Convert user-defined rmap and cmap to the dataset layout */
 55:   PetscCall(PetscLayoutCreate(PetscObjectComm((PetscObject)mat), &vmap));
 56:   if (mat->rmap->n >= 0 && mat->cmap->N < 0) {
 57:     /* We need to know mat->cmap->N if user specifies custom mat->rmap->n, otherwise the latter would get ignored below */
 58:     PetscCall(PetscViewerHDF5ReadSizes(viewer, mat_name, &mat->cmap->N, NULL));
 59:   }
 60:   vmap->bs = mat->cmap->N;
 61:   vmap->n  = (mat->rmap->n < 0 || mat->cmap->N < 0) ? -1 : mat->rmap->n * mat->cmap->N;
 62:   vmap->N  = (mat->rmap->N < 0 || mat->cmap->N < 0) ? -1 : mat->rmap->N * mat->cmap->N;

 64:   /* Read the dataset and setup its layout */
 65:   /* Note: PetscViewerHDF5ReadSizes_Private takes into account that the dataset is transposed for MATLAB MAT files */
 66:   PetscCall(PetscViewerHDF5Load(viewer, mat_name, vmap, scalartype, (void **)&a));

 68:   /* Convert the dataset layout back to rmap and cmap */
 69:   mat->cmap->N = vmap->bs;
 70:   mat->rmap->n = vmap->n / mat->cmap->N;
 71:   mat->rmap->N = vmap->N / mat->cmap->N;
 72:   PetscCall(PetscLayoutSetUp(mat->rmap));
 73:   PetscCall(PetscLayoutSetUp(mat->cmap));
 74:   PetscCall(PetscLayoutDestroy(&vmap));

 76:   /* TODO adding PetscCopyMode flag to MatSeqDenseSetPreallocation would make this code cleaner and simpler */
 77:   {
 78:     PetscBool     flg;
 79:     Mat_SeqDense *impl;
 80:     PetscCall(PetscObjectTypeCompare((PetscObject)mat, MATSEQDENSE, &flg));
 81:     if (flg) {
 82:       impl = (Mat_SeqDense *)mat->data;
 83:       PetscCall(MatSeqDenseSetPreallocation(mat, a));
 84:     } else {
 85:       Mat_MPIDense *implm = (Mat_MPIDense *)mat->data;
 86:       PetscCall(MatMPIDenseSetPreallocation(mat, a));
 87:       impl = (Mat_SeqDense *)implm->A->data;
 88:     }
 89:     impl->user_alloc = PETSC_FALSE;
 90:   }

 92:   PetscCall(MatAssemblyBegin(mat, MAT_FINAL_ASSEMBLY));
 93:   PetscCall(MatAssemblyEnd(mat, MAT_FINAL_ASSEMBLY));
 94:   PetscFunctionReturn(PETSC_SUCCESS);
 95: }
 96: #endif