diff --git a/src/IJ_mv/F90_HYPRE_IJMatrix.c b/src/IJ_mv/F90_HYPRE_IJMatrix.c index b287fbc69..051284ae5 100644 --- a/src/IJ_mv/F90_HYPRE_IJMatrix.c +++ b/src/IJ_mv/F90_HYPRE_IJMatrix.c @@ -70,6 +70,38 @@ hypre_F90_IFACE(hypre_ijmatrixinitialize, HYPRE_IJMATRIXINITIALIZE) hypre_F90_PassObj (HYPRE_IJMatrix, matrix) ) ); } +/*-------------------------------------------------------------------------- + * HYPRE_IJMatrixInitialize_v2 + *--------------------------------------------------------------------------*/ + +void +hypre_F90_IFACE(hypre_ijmatrixinitialize_v2, HYPRE_IJMATRIXINITIALIZE_V2) +( hypre_F90_Obj *matrix, + hypre_F90_Int *memory_location, + hypre_F90_Int *ierr ) +{ + *ierr = (hypre_F90_Int) + ( HYPRE_IJMatrixInitialize_v2( + hypre_F90_PassObj (HYPRE_IJMatrix, matrix), + hypre_F90_PassObj (HYPRE_MemoryLocation, memory_location) ) ); +} + +/*-------------------------------------------------------------------------- + * HYPRE_IJMatrixMigrate + *--------------------------------------------------------------------------*/ + +void +hypre_F90_IFACE(hypre_ijmatrixmigrate, HYPRE_IJMATRIXMIGRATE) +( hypre_F90_Obj *matrix, + hypre_F90_Int *memory_location, + hypre_F90_Int *ierr ) +{ + *ierr = (hypre_F90_Int) + ( HYPRE_IJMatrixMigrate( + hypre_F90_PassObj (HYPRE_IJMatrix, matrix), + hypre_F90_PassObj (HYPRE_MemoryLocation, memory_location) ) ); +} + /*-------------------------------------------------------------------------- * HYPRE_IJMatrixSetValues *--------------------------------------------------------------------------*/ diff --git a/src/IJ_mv/F90_HYPRE_IJVector.c b/src/IJ_mv/F90_HYPRE_IJVector.c index e1f04fe3e..856b9ca49 100644 --- a/src/IJ_mv/F90_HYPRE_IJVector.c +++ b/src/IJ_mv/F90_HYPRE_IJVector.c @@ -66,6 +66,38 @@ hypre_F90_IFACE(hypre_ijvectorinitialize, HYPRE_IJVECTORINITIALIZE) hypre_F90_PassObj (HYPRE_IJVector, vector) ) ); } +/*-------------------------------------------------------------------------- + * HYPRE_IJVectorInitialize_v2 + *--------------------------------------------------------------------------*/ + +void +hypre_F90_IFACE(hypre_ijvectorinitialize_v2, HYPRE_IJVECTORINITIALIZE_V2) +( hypre_F90_Obj *vector, + hypre_F90_Int *memory_location, + hypre_F90_Int *ierr ) +{ + *ierr = (hypre_F90_Int) + ( HYPRE_IJVectorInitialize_v2( + hypre_F90_PassObj (HYPRE_IJVector, vector), + hypre_F90_PassObj (HYPRE_MemoryLocation, memory_location) ) ); +} + +/*-------------------------------------------------------------------------- + * HYPRE_IJVectorMigrate + *--------------------------------------------------------------------------*/ + +void +hypre_F90_IFACE(hypre_ijvectormigrate, HYPRE_IJVECTORMIGRATE) +( hypre_F90_Obj *vector, + hypre_F90_Int *memory_location, + hypre_F90_Int *ierr ) +{ + *ierr = (hypre_F90_Int) + ( HYPRE_IJVectorMigrate( + hypre_F90_PassObj (HYPRE_IJVector, vector), + hypre_F90_PassObj (HYPRE_MemoryLocation, memory_location) ) ); +} + /*-------------------------------------------------------------------------- * HYPRE_IJVectorSetValues *--------------------------------------------------------------------------*/ diff --git a/src/IJ_mv/HYPRE_IJMatrix.c b/src/IJ_mv/HYPRE_IJMatrix.c index 041fc9b8b..7e7587731 100644 --- a/src/IJ_mv/HYPRE_IJMatrix.c +++ b/src/IJ_mv/HYPRE_IJMatrix.c @@ -1548,3 +1548,34 @@ HYPRE_IJMatrixAdd( HYPRE_Complex alpha, return hypre_error_flag; } + +/*-------------------------------------------------------------------------- + * HYPRE_IJMatrixMigrate + * + * Migrates an IJMatrix to the specified memory location + *--------------------------------------------------------------------------*/ + +HYPRE_Int +HYPRE_IJMatrixMigrate(HYPRE_IJMatrix matrix, + HYPRE_MemoryLocation memory_location) +{ + hypre_IJMatrix *ijmatrix = (hypre_IJMatrix *) matrix; + + if (!ijmatrix) + { + hypre_error_in_arg(1); + return hypre_error_flag; + } + + /* Only implemented for ParCSR matrices */ + if (hypre_IJMatrixObjectType(ijmatrix) == HYPRE_PARCSR) + { + hypre_IJMatrixMigrateParCSR(ijmatrix, memory_location); + } + else + { + hypre_error_in_arg(1); + } + + return hypre_error_flag; +} diff --git a/src/IJ_mv/HYPRE_IJVector.c b/src/IJ_mv/HYPRE_IJVector.c index c65cac9fe..a909596a9 100644 --- a/src/IJ_mv/HYPRE_IJVector.c +++ b/src/IJ_mv/HYPRE_IJVector.c @@ -182,6 +182,66 @@ HYPRE_IJVectorDestroy( HYPRE_IJVector vector ) return hypre_error_flag; } +/*-------------------------------------------------------------------------- + * HYPRE_IJVectorInitializeShell + *--------------------------------------------------------------------------*/ + +HYPRE_Int +HYPRE_IJVectorInitializeShell(HYPRE_IJVector vector) +{ + hypre_IJVector *vec = (hypre_IJVector *) vector; + + if (!vec) + { + hypre_error_in_arg(1); + return hypre_error_flag; + } + + if ( hypre_IJVectorObjectType(vec) == HYPRE_PARCSR ) + { + if (!hypre_IJVectorObject(vec)) + { + hypre_IJVectorCreatePar(vec, hypre_IJVectorPartitioning(vec)); + } + + hypre_IJVectorInitializeParShell(vec); + } + else + { + hypre_error_in_arg(1); + } + + return hypre_error_flag; +} + +/*-------------------------------------------------------------------------- + * HYPRE_IJVectorSetData + *--------------------------------------------------------------------------*/ + +HYPRE_Int +HYPRE_IJVectorSetData(HYPRE_IJVector vector, + HYPRE_Complex *data) +{ + hypre_IJVector *vec = (hypre_IJVector *) vector; + + if (!vec) + { + hypre_error_in_arg(1); + return hypre_error_flag; + } + + if ( hypre_IJVectorObjectType(vec) == HYPRE_PARCSR ) + { + hypre_IJVectorSetParData(vec, data); + } + else + { + hypre_error_in_arg(1); + } + + return hypre_error_flag; +} + /*-------------------------------------------------------------------------- * HYPRE_IJVectorInitialize *--------------------------------------------------------------------------*/ @@ -214,8 +274,13 @@ HYPRE_IJVectorInitialize( HYPRE_IJVector vector ) return hypre_error_flag; } +/*-------------------------------------------------------------------------- + * HYPRE_IJVectorInitialize_v2 + *--------------------------------------------------------------------------*/ + HYPRE_Int -HYPRE_IJVectorInitialize_v2( HYPRE_IJVector vector, HYPRE_MemoryLocation memory_location ) +HYPRE_IJVectorInitialize_v2( HYPRE_IJVector vector, + HYPRE_MemoryLocation memory_location ) { hypre_IJVector *vec = (hypre_IJVector *) vector; @@ -318,6 +383,34 @@ HYPRE_IJVectorSetValues( HYPRE_IJVector vector, return hypre_error_flag; } +/*-------------------------------------------------------------------------- + * HYPRE_IJVectorSetConstantValues + *--------------------------------------------------------------------------*/ + +HYPRE_Int +HYPRE_IJVectorSetConstantValues( HYPRE_IJVector vector, + HYPRE_Complex value ) +{ + hypre_IJVector *vec = (hypre_IJVector *) vector; + + if (!vec) + { + hypre_error_in_arg(1); + return hypre_error_flag; + } + + if ( hypre_IJVectorObjectType(vec) == HYPRE_PARCSR ) + { + return ( hypre_IJVectorSetConstantValuesPar(vec, value) ); + } + else + { + hypre_error_in_arg(1); + } + + return hypre_error_flag; +} + /*-------------------------------------------------------------------------- * HYPRE_IJVectorAddToValues *--------------------------------------------------------------------------*/ @@ -853,3 +946,34 @@ HYPRE_IJVectorInnerProd( HYPRE_IJVector x, return hypre_error_flag; } + +/*-------------------------------------------------------------------------- + * HYPRE_IJVectorMigrate + * + * Migrates an IJVector to the specified memory location + *--------------------------------------------------------------------------*/ + +HYPRE_Int +HYPRE_IJVectorMigrate(HYPRE_IJVector vector, + HYPRE_MemoryLocation memory_location) +{ + hypre_IJVector *ijvector = (hypre_IJVector *) vector; + + if (!ijvector) + { + hypre_error_in_arg(1); + return hypre_error_flag; + } + + /* Only implemented for ParVector */ + if (hypre_IJVectorObjectType(ijvector) == HYPRE_PARCSR) + { + hypre_IJVectorMigrateParCSR(ijvector, memory_location); + } + else + { + hypre_error_in_arg(1); + } + + return hypre_error_flag; +} diff --git a/src/IJ_mv/HYPRE_IJ_mv.h b/src/IJ_mv/HYPRE_IJ_mv.h index f6cda553f..6b339575f 100644 --- a/src/IJ_mv/HYPRE_IJ_mv.h +++ b/src/IJ_mv/HYPRE_IJ_mv.h @@ -130,7 +130,7 @@ HYPRE_Int HYPRE_IJMatrixSetValues(HYPRE_IJMatrix matrix, const HYPRE_Complex *values); /** - * Sets all matrix coefficients of an already assembled matrix to + * Sets all matrix coefficients of an already assembled matrix to * \e value **/ HYPRE_Int HYPRE_IJMatrixSetConstantValues(HYPRE_IJMatrix matrix, @@ -430,6 +430,12 @@ HYPRE_Int HYPRE_IJMatrixReadBinary(const char *filename, HYPRE_Int type, HYPRE_IJMatrix *matrix_ptr); +/** + * Migrate the matrix to a given memory location. + **/ +HYPRE_Int HYPRE_IJMatrixMigrate(HYPRE_IJMatrix matrix, + HYPRE_MemoryLocation memory_location); + /**@}*/ /*-------------------------------------------------------------------------- @@ -475,7 +481,30 @@ HYPRE_Int HYPRE_IJVectorCreate(MPI_Comm comm, HYPRE_Int HYPRE_IJVectorDestroy(HYPRE_IJVector vector); /** - * Prepare a vector object for setting coefficient values. This + * This function should be called before `HYPRE_IJVectorSetData` + * if users intend to reuse an existing data pointer, thereby avoiding + * unnecessary memory copies. It configures the vector to accept external + * data without allocating new storage. + **/ +HYPRE_Int HYPRE_IJVectorInitializeShell(HYPRE_IJVector vector); + +/** + * This function sets the internal data pointer of the vector to an external + * array, allowing direct control over the vector's data storage without + * transferring ownership. Users are responsible for managing the memory + * of the `data` array, which must remain valid for the vector's lifetime. + * + * Users should call `HYPRE_IJVectorInitializeShell` before this function + * to prepare the vector for external data. The memory location of the `data` + * array is expected to be on the host when hypre is configured without GPU + * support. If hypre is configured with GPU support, it is assumed that `data` + * resides in device memory. + **/ +HYPRE_Int HYPRE_IJVectorSetData(HYPRE_IJVector vector, + HYPRE_Complex *data); + +/** + * Prepare a vector object for setting coefficient values. This * routine will also re-initialize an already assembled vector, * allowing users to modify coefficient values. **/ @@ -534,6 +563,12 @@ HYPRE_Int HYPRE_IJVectorSetValues(HYPRE_IJVector vector, const HYPRE_BigInt *indices, const HYPRE_Complex *values); +/** + * Sets all vector coefficients to \e value + **/ +HYPRE_Int HYPRE_IJVectorSetConstantValues(HYPRE_IJVector vector, + HYPRE_Complex value); + /** * Adds to values in vector. Usage details are analogous to * \ref HYPRE_IJVectorSetValues. @@ -653,6 +688,12 @@ HYPRE_Int HYPRE_IJVectorInnerProd(HYPRE_IJVector x, HYPRE_IJVector y, HYPRE_Real *prod); +/** + * Migrate the vector to a given memory location. + **/ +HYPRE_Int HYPRE_IJVectorMigrate(HYPRE_IJVector vector, + HYPRE_MemoryLocation memory_location); + /**@}*/ /**@}*/ diff --git a/src/IJ_mv/IJMatrix_parcsr.c b/src/IJ_mv/IJMatrix_parcsr.c index 8ba824ba3..dc7ce6240 100644 --- a/src/IJ_mv/IJMatrix_parcsr.c +++ b/src/IJ_mv/IJMatrix_parcsr.c @@ -4287,3 +4287,19 @@ hypre_IJMatrixAddToValuesOMPParCSR( hypre_IJMatrix *matrix, return hypre_error_flag; } + +/*-------------------------------------------------------------------------- + * hypre_IJMatrixMigrateParCSR + *--------------------------------------------------------------------------*/ + +HYPRE_Int +hypre_IJMatrixMigrateParCSR(hypre_IJMatrix *matrix, + HYPRE_MemoryLocation memory_location) +{ + hypre_ParCSRMatrix *par_matrix = (hypre_ParCSRMatrix *) hypre_IJMatrixObject(matrix); + + /* Migrate the ParCSR matrix */ + hypre_ParCSRMatrixMigrate(par_matrix, memory_location); + + return hypre_error_flag; +} diff --git a/src/IJ_mv/IJVector_parcsr.c b/src/IJ_mv/IJVector_parcsr.c index 377db219f..13c4ff8c7 100644 --- a/src/IJ_mv/IJVector_parcsr.c +++ b/src/IJ_mv/IJVector_parcsr.c @@ -67,18 +67,16 @@ hypre_IJVectorDestroyPar(hypre_IJVector *vector) * initializes ParVector of IJVectorPar * *****************************************************************************/ -HYPRE_Int -hypre_IJVectorInitializePar(hypre_IJVector *vector) -{ - return hypre_IJVectorInitializePar_v2(vector, hypre_IJVectorMemoryLocation(vector)); -} + +/*-------------------------------------------------------------------------- + * hypre_IJVectorInitializeParShell + *--------------------------------------------------------------------------*/ HYPRE_Int -hypre_IJVectorInitializePar_v2(hypre_IJVector *vector, HYPRE_MemoryLocation memory_location) +hypre_IJVectorInitializeParShell(hypre_IJVector *vector) { MPI_Comm comm = hypre_IJVectorComm(vector); hypre_ParVector *par_vector = (hypre_ParVector*) hypre_IJVectorObject(vector); - hypre_AuxParVector *aux_vector = (hypre_AuxParVector*) hypre_IJVectorTranslator(vector); HYPRE_Int print_level = hypre_IJVectorPrintLevel(vector); HYPRE_Int num_vectors = hypre_IJVectorNumComponents(vector); @@ -87,9 +85,6 @@ hypre_IJVectorInitializePar_v2(hypre_IJVector *vector, HYPRE_MemoryLocation memo HYPRE_Int my_id; - HYPRE_MemoryLocation memory_location_aux = - hypre_GetExecPolicy1(memory_location) == HYPRE_EXEC_HOST ? HYPRE_MEMORY_HOST : HYPRE_MEMORY_DEVICE; - hypre_MPI_Comm_rank(comm, &my_id); if (!partitioning) @@ -106,14 +101,60 @@ hypre_IJVectorInitializePar_v2(hypre_IJVector *vector, HYPRE_MemoryLocation memo hypre_VectorNumVectors(local_vector) = num_vectors; hypre_VectorSize(local_vector) = (HYPRE_Int)(partitioning[1] - partitioning[0]); - hypre_ParVectorInitialize_v2(par_vector, memory_location); + return hypre_error_flag; +} + +/*-------------------------------------------------------------------------- + * hypre_IJVectorSetParData + *--------------------------------------------------------------------------*/ + +HYPRE_Int +hypre_IJVectorSetParData(hypre_IJVector *vector, + HYPRE_Complex *data) +{ + hypre_ParVector *par_vector = (hypre_ParVector*) hypre_IJVectorObject(vector); + hypre_ParVectorSetData(par_vector, data); + + return hypre_error_flag; +} + +/*-------------------------------------------------------------------------- + * hypre_IJVectorInitializePar + *--------------------------------------------------------------------------*/ + +HYPRE_Int +hypre_IJVectorInitializePar(hypre_IJVector *vector) +{ + return hypre_IJVectorInitializePar_v2(vector, hypre_IJVectorMemoryLocation(vector)); +} + +/*-------------------------------------------------------------------------- + * hypre_IJVectorInitializePar_v2 + *--------------------------------------------------------------------------*/ + +HYPRE_Int +hypre_IJVectorInitializePar_v2(hypre_IJVector *vector, + HYPRE_MemoryLocation memory_location) +{ + hypre_ParVector *par_vector; + hypre_AuxParVector *aux_vector; + + /* Set up the basic structure and metadata for the vector */ + hypre_IJVectorInitializeParShell(vector); + par_vector = (hypre_ParVector*) hypre_IJVectorObject(vector); + aux_vector = (hypre_AuxParVector*) hypre_IJVectorTranslator(vector); + + /* Check if auxiliary vectors needs to be created */ if (!aux_vector) { hypre_AuxParVectorCreate(&aux_vector); hypre_IJVectorTranslator(vector) = aux_vector; } - hypre_AuxParVectorInitialize_v2(aux_vector, memory_location_aux); + + /* Memory allocations */ + hypre_ParVectorInitialize_v2(par_vector, memory_location); + hypre_AuxParVectorInitialize_v2(aux_vector, memory_location); return hypre_error_flag; } @@ -207,63 +248,7 @@ hypre_IJVectorDistributePar(hypre_IJVector *vector, HYPRE_Int hypre_IJVectorZeroValuesPar(hypre_IJVector *vector) { - HYPRE_Int my_id; - HYPRE_BigInt vec_start, vec_stop; - - hypre_ParVector *par_vector = (hypre_ParVector*) hypre_IJVectorObject(vector); - MPI_Comm comm = hypre_IJVectorComm(vector); - HYPRE_BigInt *partitioning; - hypre_Vector *local_vector; - HYPRE_Int print_level = hypre_IJVectorPrintLevel(vector); - - hypre_MPI_Comm_rank(comm, &my_id); - - /* If par_vector == NULL or partitioning == NULL or local_vector == NULL - let user know of catastrophe and exit */ - - if (!par_vector) - { - if (print_level) - { - hypre_printf("par_vector == NULL -- "); - hypre_printf("hypre_IJVectorZeroValuesPar\n"); - hypre_printf("**** Vector storage is either unallocated or orphaned ****\n"); - } - hypre_error_in_arg(1); - return hypre_error_flag; - } - partitioning = hypre_ParVectorPartitioning(par_vector); - local_vector = hypre_ParVectorLocalVector(par_vector); - if (!local_vector) - { - if (print_level) - { - hypre_printf("local_vector == NULL -- "); - hypre_printf("hypre_IJVectorZeroValuesPar\n"); - hypre_printf("**** Vector local data is either unallocated or orphaned ****\n"); - } - hypre_error_in_arg(1); - return hypre_error_flag; - } - - vec_start = partitioning[0]; - vec_stop = partitioning[1]; - - if (vec_start > vec_stop) - { - if (print_level) - { - hypre_printf("vec_start > vec_stop -- "); - hypre_printf("hypre_IJVectorZeroValuesPar\n"); - hypre_printf("**** This vector partitioning should not occur ****\n"); - } - hypre_error_in_arg(1); - return hypre_error_flag; - } - - hypre_assert(hypre_VectorSize(local_vector) == (HYPRE_Int)(vec_stop - vec_start)); - - hypre_SeqVectorSetConstantValues(local_vector, 0.0); + hypre_IJVectorSetConstantValuesPar(vector, 0.0); return hypre_error_flag; } @@ -424,6 +409,40 @@ hypre_IJVectorSetValuesPar(hypre_IJVector *vector, return hypre_error_flag; } +/****************************************************************************** + * + * hypre_IJVectorSetConstantValuesPar + * + * sets all vector coefficients to a given value + * + *****************************************************************************/ + +HYPRE_Int +hypre_IJVectorSetConstantValuesPar(hypre_IJVector *vector, + HYPRE_Complex value) +{ + HYPRE_Int print_level = hypre_IJVectorPrintLevel(vector); + hypre_ParVector *par_vector = (hypre_ParVector*) hypre_IJVectorObject(vector); + + /* Sanity check for par_vector */ + if (!par_vector) + { + if (print_level) + { + hypre_printf("par_vector == NULL -- "); + hypre_printf("hypre_IJVectorSetValuesPar\n"); + hypre_printf("**** Vector storage is either unallocated or orphaned ****\n"); + } + hypre_error_in_arg(1); + return hypre_error_flag; + } + + /* Set vector coefficients to "value" */ + hypre_ParVectorSetConstantValues(par_vector, value); + + return hypre_error_flag; +} + /****************************************************************************** * * hypre_IJVectorAddToValuesPar @@ -1216,3 +1235,19 @@ hypre_IJVectorAssembleOffProcValsPar( hypre_IJVector *vector, return hypre_error_flag; } + +/*-------------------------------------------------------------------------- + * hypre_IJVectorMigrateParCSR + *--------------------------------------------------------------------------*/ + +HYPRE_Int +hypre_IJVectorMigrateParCSR(hypre_IJVector *vector, + HYPRE_MemoryLocation memory_location) +{ + hypre_ParVector *par_vector = (hypre_ParVector *) hypre_IJVectorObject(vector); + + /* Migrate the ParVector */ + hypre_ParVectorMigrate(par_vector, memory_location); + + return hypre_error_flag; +} diff --git a/src/IJ_mv/_hypre_IJ_mv.h b/src/IJ_mv/_hypre_IJ_mv.h index 29991146b..0cb4090a8 100644 --- a/src/IJ_mv/_hypre_IJ_mv.h +++ b/src/IJ_mv/_hypre_IJ_mv.h @@ -478,6 +478,7 @@ HYPRE_Int hypre_IJMatrixInitializeParCSR_v2(hypre_IJMatrix *matrix, HYPRE_MemoryLocation memory_location); HYPRE_Int hypre_IJMatrixSetConstantValuesParCSRDevice( hypre_IJMatrix *matrix, HYPRE_Complex value ); +HYPRE_Int hypre_IJMatrixMigrateParCSR(hypre_IJMatrix *matrix, HYPRE_MemoryLocation memory_location); /* IJMatrix_petsc.c */ HYPRE_Int hypre_IJMatrixSetLocalSizePETSc ( hypre_IJMatrix *matrix, HYPRE_Int local_m, @@ -512,6 +513,8 @@ HYPRE_Int hypre_IJVectorReadBinary ( MPI_Comm comm, const char *filename, HYPRE_ /* IJVector_parcsr.c */ HYPRE_Int hypre_IJVectorCreatePar ( hypre_IJVector *vector, HYPRE_BigInt *IJpartitioning ); HYPRE_Int hypre_IJVectorDestroyPar ( hypre_IJVector *vector ); +HYPRE_Int hypre_IJVectorInitializeParShell (hypre_IJVector *vector ); +HYPRE_Int hypre_IJVectorSetParData( hypre_IJVector *vector, HYPRE_Complex *data ); HYPRE_Int hypre_IJVectorInitializePar ( hypre_IJVector *vector ); HYPRE_Int hypre_IJVectorInitializePar_v2(hypre_IJVector *vector, HYPRE_MemoryLocation memory_location); @@ -522,6 +525,7 @@ HYPRE_Int hypre_IJVectorZeroValuesPar ( hypre_IJVector *vector ); HYPRE_Int hypre_IJVectorSetComponentPar ( hypre_IJVector *vector, HYPRE_Int component); HYPRE_Int hypre_IJVectorSetValuesPar ( hypre_IJVector *vector, HYPRE_Int num_values, const HYPRE_BigInt *indices, const HYPRE_Complex *values ); +HYPRE_Int hypre_IJVectorSetConstantValuesPar ( hypre_IJVector *vector, HYPRE_Complex value ); HYPRE_Int hypre_IJVectorAddToValuesPar ( hypre_IJVector *vector, HYPRE_Int num_values, const HYPRE_BigInt *indices, const HYPRE_Complex *values ); HYPRE_Int hypre_IJVectorAssemblePar ( hypre_IJVector *vector ); @@ -533,9 +537,9 @@ HYPRE_Int hypre_IJVectorAssembleOffProcValsPar ( hypre_IJVector *vector, HYPRE_Int hypre_IJVectorSetAddValuesParDevice(hypre_IJVector *vector, HYPRE_Int num_values, const HYPRE_BigInt *indices, const HYPRE_Complex *values, const char *action); HYPRE_Int hypre_IJVectorAssembleParDevice(hypre_IJVector *vector); - HYPRE_Int hypre_IJVectorUpdateValuesDevice( hypre_IJVector *vector, HYPRE_Int num_values, const HYPRE_BigInt *indices, const HYPRE_Complex *values, HYPRE_Int action); +HYPRE_Int hypre_IJVectorMigrateParCSR(hypre_IJVector *vector, HYPRE_MemoryLocation memory_location); /* HYPRE_IJMatrix.c */ HYPRE_Int HYPRE_IJMatrixCreate ( MPI_Comm comm, HYPRE_BigInt ilower, HYPRE_BigInt iupper, @@ -587,6 +591,7 @@ HYPRE_Int HYPRE_IJVectorInitialize ( HYPRE_IJVector vector ); HYPRE_Int HYPRE_IJVectorSetPrintLevel ( HYPRE_IJVector vector, HYPRE_Int print_level ); HYPRE_Int HYPRE_IJVectorSetValues ( HYPRE_IJVector vector, HYPRE_Int nvalues, const HYPRE_BigInt *indices, const HYPRE_Complex *values ); +HYPRE_Int HYPRE_IJVectorSetConstantValues ( HYPRE_IJVector vector, HYPRE_Complex value ); HYPRE_Int HYPRE_IJVectorAddToValues ( HYPRE_IJVector vector, HYPRE_Int nvalues, const HYPRE_BigInt *indices, const HYPRE_Complex *values ); HYPRE_Int HYPRE_IJVectorAssemble ( HYPRE_IJVector vector ); diff --git a/src/IJ_mv/protos.h b/src/IJ_mv/protos.h index d1baa59d8..b1f5fbd8b 100644 --- a/src/IJ_mv/protos.h +++ b/src/IJ_mv/protos.h @@ -108,6 +108,7 @@ HYPRE_Int hypre_IJMatrixInitializeParCSR_v2(hypre_IJMatrix *matrix, HYPRE_MemoryLocation memory_location); HYPRE_Int hypre_IJMatrixSetConstantValuesParCSRDevice( hypre_IJMatrix *matrix, HYPRE_Complex value ); +HYPRE_Int hypre_IJMatrixMigrateParCSR(hypre_IJMatrix *matrix, HYPRE_MemoryLocation memory_location); /* IJMatrix_petsc.c */ HYPRE_Int hypre_IJMatrixSetLocalSizePETSc ( hypre_IJMatrix *matrix, HYPRE_Int local_m, @@ -142,6 +143,8 @@ HYPRE_Int hypre_IJVectorReadBinary ( MPI_Comm comm, const char *filename, HYPRE_ /* IJVector_parcsr.c */ HYPRE_Int hypre_IJVectorCreatePar ( hypre_IJVector *vector, HYPRE_BigInt *IJpartitioning ); HYPRE_Int hypre_IJVectorDestroyPar ( hypre_IJVector *vector ); +HYPRE_Int hypre_IJVectorInitializeParShell (hypre_IJVector *vector ); +HYPRE_Int hypre_IJVectorSetParData( hypre_IJVector *vector, HYPRE_Complex *data ); HYPRE_Int hypre_IJVectorInitializePar ( hypre_IJVector *vector ); HYPRE_Int hypre_IJVectorInitializePar_v2(hypre_IJVector *vector, HYPRE_MemoryLocation memory_location); @@ -152,6 +155,7 @@ HYPRE_Int hypre_IJVectorZeroValuesPar ( hypre_IJVector *vector ); HYPRE_Int hypre_IJVectorSetComponentPar ( hypre_IJVector *vector, HYPRE_Int component); HYPRE_Int hypre_IJVectorSetValuesPar ( hypre_IJVector *vector, HYPRE_Int num_values, const HYPRE_BigInt *indices, const HYPRE_Complex *values ); +HYPRE_Int hypre_IJVectorSetConstantValuesPar ( hypre_IJVector *vector, HYPRE_Complex value ); HYPRE_Int hypre_IJVectorAddToValuesPar ( hypre_IJVector *vector, HYPRE_Int num_values, const HYPRE_BigInt *indices, const HYPRE_Complex *values ); HYPRE_Int hypre_IJVectorAssemblePar ( hypre_IJVector *vector ); @@ -163,9 +167,9 @@ HYPRE_Int hypre_IJVectorAssembleOffProcValsPar ( hypre_IJVector *vector, HYPRE_Int hypre_IJVectorSetAddValuesParDevice(hypre_IJVector *vector, HYPRE_Int num_values, const HYPRE_BigInt *indices, const HYPRE_Complex *values, const char *action); HYPRE_Int hypre_IJVectorAssembleParDevice(hypre_IJVector *vector); - HYPRE_Int hypre_IJVectorUpdateValuesDevice( hypre_IJVector *vector, HYPRE_Int num_values, const HYPRE_BigInt *indices, const HYPRE_Complex *values, HYPRE_Int action); +HYPRE_Int hypre_IJVectorMigrateParCSR(hypre_IJVector *vector, HYPRE_MemoryLocation memory_location); /* HYPRE_IJMatrix.c */ HYPRE_Int HYPRE_IJMatrixCreate ( MPI_Comm comm, HYPRE_BigInt ilower, HYPRE_BigInt iupper, @@ -217,6 +221,7 @@ HYPRE_Int HYPRE_IJVectorInitialize ( HYPRE_IJVector vector ); HYPRE_Int HYPRE_IJVectorSetPrintLevel ( HYPRE_IJVector vector, HYPRE_Int print_level ); HYPRE_Int HYPRE_IJVectorSetValues ( HYPRE_IJVector vector, HYPRE_Int nvalues, const HYPRE_BigInt *indices, const HYPRE_Complex *values ); +HYPRE_Int HYPRE_IJVectorSetConstantValues ( HYPRE_IJVector vector, HYPRE_Complex value ); HYPRE_Int HYPRE_IJVectorAddToValues ( HYPRE_IJVector vector, HYPRE_Int nvalues, const HYPRE_BigInt *indices, const HYPRE_Complex *values ); HYPRE_Int HYPRE_IJVectorAssemble ( HYPRE_IJVector vector ); diff --git a/src/config/HYPREConfig.cmake.in b/src/config/HYPREConfig.cmake.in index c223575d3..6d9abd904 100644 --- a/src/config/HYPREConfig.cmake.in +++ b/src/config/HYPREConfig.cmake.in @@ -102,6 +102,7 @@ if(HYPRE_ENABLE_CUDA OR HYPRE_ENABLE_HIP OR HYPRE_ENABLE_SYCL) set(REQUIRED_GPU_COMPONENTS) if(HYPRE_ENABLE_CUDA) + list(APPEND REQUIRED_GPU_COMPONENTS cudart) if(HYPRE_ENABLE_CUSPARSE) list(APPEND REQUIRED_GPU_COMPONENTS cusparse) endif() diff --git a/src/config/cmake/HYPRE_SetupCUDAToolkit.cmake b/src/config/cmake/HYPRE_SetupCUDAToolkit.cmake index 3b08d8327..8f47bb9eb 100644 --- a/src/config/cmake/HYPRE_SetupCUDAToolkit.cmake +++ b/src/config/cmake/HYPRE_SetupCUDAToolkit.cmake @@ -206,6 +206,7 @@ function(find_and_add_cuda_library LIB_NAME HYPRE_ENABLE_VAR) endfunction() # Handle CUDA libraries +list(APPEND CUDA_LIBS CUDA::cudart) # Add cudart first since other CUDA libraries may depend on it find_and_add_cuda_library(cusparse HYPRE_ENABLE_CUSPARSE) find_and_add_cuda_library(curand HYPRE_ENABLE_CURAND) find_and_add_cuda_library(cublas HYPRE_ENABLE_CUBLAS) diff --git a/src/parcsr_mv/_hypre_parcsr_mv.h b/src/parcsr_mv/_hypre_parcsr_mv.h index 9af29fbce..b920dc4f0 100644 --- a/src/parcsr_mv/_hypre_parcsr_mv.h +++ b/src/parcsr_mv/_hypre_parcsr_mv.h @@ -1258,6 +1258,8 @@ hypre_ParVector *hypre_ParVectorCreate ( MPI_Comm comm, HYPRE_BigInt global_size hypre_ParVector *hypre_ParMultiVectorCreate ( MPI_Comm comm, HYPRE_BigInt global_size, HYPRE_BigInt *partitioning, HYPRE_Int num_vectors ); HYPRE_Int hypre_ParVectorDestroy ( hypre_ParVector *vector ); +HYPRE_Int hypre_ParVectorInitializeShell( hypre_ParVector *vector ); +HYPRE_Int hypre_ParVectorSetData( hypre_ParVector *vector, HYPRE_Complex *data ); HYPRE_Int hypre_ParVectorInitialize ( hypre_ParVector *vector ); HYPRE_Int hypre_ParVectorInitialize_v2( hypre_ParVector *vector, HYPRE_MemoryLocation memory_location ); diff --git a/src/parcsr_mv/par_csr_bool_matop.c b/src/parcsr_mv/par_csr_bool_matop.c index a88cb955e..87026046d 100644 --- a/src/parcsr_mv/par_csr_bool_matop.c +++ b/src/parcsr_mv/par_csr_bool_matop.c @@ -43,7 +43,7 @@ hypre_ParBooleanMatmul( hypre_ParCSRBooleanMatrix *A, HYPRE_Int num_cols_offd_B = hypre_CSRBooleanMatrix_Get_NCols(B_offd); hypre_ParCSRBooleanMatrix *C; - HYPRE_BigInt *col_map_offd_C; + HYPRE_BigInt *col_map_offd_C = NULL; HYPRE_Int *map_B_to_C = NULL; hypre_CSRBooleanMatrix *C_diag; diff --git a/src/parcsr_mv/par_csr_matop.c b/src/parcsr_mv/par_csr_matop.c index 85402ac01..c28d57a8c 100644 --- a/src/parcsr_mv/par_csr_matop.c +++ b/src/parcsr_mv/par_csr_matop.c @@ -3487,7 +3487,7 @@ hypre_ParTMatmul( hypre_ParCSRMatrix *A, hypre_ParCSRMatrix *C; HYPRE_BigInt *col_map_offd_C = NULL; - HYPRE_Int *map_B_to_C; + HYPRE_Int *map_B_to_C = NULL; hypre_CSRMatrix *C_diag = NULL; hypre_CSRMatrix *C_tmp_diag = NULL; diff --git a/src/parcsr_mv/par_vector.c b/src/parcsr_mv/par_vector.c index 738201b6e..32b1907c0 100644 --- a/src/parcsr_mv/par_vector.c +++ b/src/parcsr_mv/par_vector.c @@ -39,6 +39,7 @@ hypre_ParVectorCreate( MPI_Comm comm, return NULL; } vector = hypre_CTAlloc(hypre_ParVector, 1, HYPRE_MEMORY_HOST); + hypre_MPI_Comm_rank(comm, &my_id); if (!partitioning_in) @@ -112,22 +113,53 @@ hypre_ParVectorDestroy( hypre_ParVector *vector ) } /*-------------------------------------------------------------------------- - * hypre_ParVectorInitialize_v2 - * - * Initialize a hypre_ParVector at a given memory location + * hypre_ParVectorInitializeShell *--------------------------------------------------------------------------*/ HYPRE_Int -hypre_ParVectorInitialize_v2( hypre_ParVector *vector, HYPRE_MemoryLocation memory_location ) +hypre_ParVectorInitializeShell(hypre_ParVector *vector) { if (!vector) { hypre_error_in_arg(1); return hypre_error_flag; } - hypre_SeqVectorInitialize_v2(hypre_ParVectorLocalVector(vector), memory_location); - hypre_ParVectorActualLocalSize(vector) = hypre_VectorSize(hypre_ParVectorLocalVector(vector)); + hypre_Vector *local_vector = hypre_ParVectorLocalVector(vector); + + hypre_SeqVectorInitializeShell(local_vector); + hypre_ParVectorActualLocalSize(vector) = hypre_VectorSize(local_vector); + + return hypre_error_flag; +} + +/*-------------------------------------------------------------------------- + * hypre_ParVectorSetData + *--------------------------------------------------------------------------*/ + +HYPRE_Int +hypre_ParVectorSetData(hypre_ParVector *vector, + HYPRE_Complex *data) +{ + hypre_SeqVectorSetData(hypre_ParVectorLocalVector(vector), data); + + return hypre_error_flag; +} + +/*-------------------------------------------------------------------------- + * hypre_ParVectorInitialize_v2 + * + * Initialize a hypre_ParVector at a given memory location + *--------------------------------------------------------------------------*/ + +HYPRE_Int +hypre_ParVectorInitialize_v2( hypre_ParVector *vector, + HYPRE_MemoryLocation memory_location ) +{ + hypre_Vector *local_vector = hypre_ParVectorLocalVector(vector); + + hypre_ParVectorInitializeShell(vector); + hypre_SeqVectorInitialize_v2(local_vector, memory_location); return hypre_error_flag; } diff --git a/src/parcsr_mv/protos.h b/src/parcsr_mv/protos.h index 9d601d008..127497564 100644 --- a/src/parcsr_mv/protos.h +++ b/src/parcsr_mv/protos.h @@ -597,6 +597,8 @@ hypre_ParVector *hypre_ParVectorCreate ( MPI_Comm comm, HYPRE_BigInt global_size hypre_ParVector *hypre_ParMultiVectorCreate ( MPI_Comm comm, HYPRE_BigInt global_size, HYPRE_BigInt *partitioning, HYPRE_Int num_vectors ); HYPRE_Int hypre_ParVectorDestroy ( hypre_ParVector *vector ); +HYPRE_Int hypre_ParVectorInitializeShell( hypre_ParVector *vector ); +HYPRE_Int hypre_ParVectorSetData( hypre_ParVector *vector, HYPRE_Complex *data ); HYPRE_Int hypre_ParVectorInitialize ( hypre_ParVector *vector ); HYPRE_Int hypre_ParVectorInitialize_v2( hypre_ParVector *vector, HYPRE_MemoryLocation memory_location ); diff --git a/src/seq_mv/protos.h b/src/seq_mv/protos.h index 6a3799444..f645aa0c2 100644 --- a/src/seq_mv/protos.h +++ b/src/seq_mv/protos.h @@ -260,6 +260,8 @@ HYPRE_Int hypre_MultiblockMatrixSetSubmatrix ( hypre_MultiblockMatrix *matrix, H hypre_Vector *hypre_SeqVectorCreate ( HYPRE_Int size ); hypre_Vector *hypre_SeqMultiVectorCreate ( HYPRE_Int size, HYPRE_Int num_vectors ); HYPRE_Int hypre_SeqVectorDestroy ( hypre_Vector *vector ); +HYPRE_Int hypre_SeqVectorInitializeShell( hypre_Vector *vector ); +HYPRE_Int hypre_SeqVectorSetData( hypre_Vector *vector, HYPRE_Complex *data ); HYPRE_Int hypre_SeqVectorInitialize_v2( hypre_Vector *vector, HYPRE_MemoryLocation memory_location ); HYPRE_Int hypre_SeqVectorInitialize ( hypre_Vector *vector ); diff --git a/src/seq_mv/seq_mv.h b/src/seq_mv/seq_mv.h index 0a9ab039a..5c1fe7c73 100644 --- a/src/seq_mv/seq_mv.h +++ b/src/seq_mv/seq_mv.h @@ -539,6 +539,8 @@ HYPRE_Int hypre_MultiblockMatrixSetSubmatrix ( hypre_MultiblockMatrix *matrix, H hypre_Vector *hypre_SeqVectorCreate ( HYPRE_Int size ); hypre_Vector *hypre_SeqMultiVectorCreate ( HYPRE_Int size, HYPRE_Int num_vectors ); HYPRE_Int hypre_SeqVectorDestroy ( hypre_Vector *vector ); +HYPRE_Int hypre_SeqVectorInitializeShell( hypre_Vector *vector ); +HYPRE_Int hypre_SeqVectorSetData( hypre_Vector *vector, HYPRE_Complex *data ); HYPRE_Int hypre_SeqVectorInitialize_v2( hypre_Vector *vector, HYPRE_MemoryLocation memory_location ); HYPRE_Int hypre_SeqVectorInitialize ( hypre_Vector *vector ); diff --git a/src/seq_mv/vector.c b/src/seq_mv/vector.c index 1da7c9379..d74b37f2a 100644 --- a/src/seq_mv/vector.c +++ b/src/seq_mv/vector.c @@ -74,29 +74,16 @@ hypre_SeqVectorDestroy( hypre_Vector *vector ) } /*-------------------------------------------------------------------------- - * hypre_SeqVectorInitialize_v2 - * - * Initialize a vector at a given memory location + * hypre_SeqVectorInitializeShell *--------------------------------------------------------------------------*/ HYPRE_Int -hypre_SeqVectorInitialize_v2( hypre_Vector *vector, HYPRE_MemoryLocation memory_location ) +hypre_SeqVectorInitializeShell( hypre_Vector *vector ) { HYPRE_Int size = hypre_VectorSize(vector); HYPRE_Int num_vectors = hypre_VectorNumVectors(vector); HYPRE_Int multivec_storage_method = hypre_VectorMultiVecStorageMethod(vector); - hypre_VectorMemoryLocation(vector) = memory_location; - - /* Caveat: for pre-existing data, the memory location must be guaranteed - * to be consistent with `memory_location' - * Otherwise, mismatches will exist and problems will be encountered - * when being used, and freed */ - if (!hypre_VectorData(vector)) - { - hypre_VectorData(vector) = hypre_CTAlloc(HYPRE_Complex, num_vectors * size, memory_location); - } - if (multivec_storage_method == 0) { hypre_VectorVectorStride(vector) = size; @@ -116,6 +103,60 @@ hypre_SeqVectorInitialize_v2( hypre_Vector *vector, HYPRE_MemoryLocation memory_ return hypre_error_flag; } +/*-------------------------------------------------------------------------- + * hypre_SeqVectorSetData + *--------------------------------------------------------------------------*/ + +HYPRE_Int +hypre_SeqVectorSetData( hypre_Vector *vector, + HYPRE_Complex *data ) +{ + /* Free data array if already present */ + if (hypre_VectorData(vector) && hypre_VectorOwnsData(vector)) + { + hypre_TFree(hypre_VectorData(vector), hypre_VectorMemoryLocation(vector)); + } + + /* Set data pointer passed via input */ + hypre_VectorData(vector) = data; + + /* Remove data pointer ownership */ + hypre_VectorOwnsData(vector) = 0; + + return hypre_error_flag; +} + +/*-------------------------------------------------------------------------- + * hypre_SeqVectorInitialize_v2 + * + * Initialize a vector at a given memory location + *--------------------------------------------------------------------------*/ + +HYPRE_Int +hypre_SeqVectorInitialize_v2( hypre_Vector *vector, + HYPRE_MemoryLocation memory_location ) +{ + HYPRE_Int size = hypre_VectorSize(vector); + HYPRE_Int num_vectors = hypre_VectorNumVectors(vector); + + /* Set up the basic structure and metadata for the local vector */ + hypre_SeqVectorInitializeShell(vector); + + /* Set memory location */ + hypre_VectorMemoryLocation(vector) = memory_location; + + /* Caveat: for pre-existing data, the memory location must be guaranteed + * to be consistent with `memory_location' + * Otherwise, mismatches will exist and problems will be encountered + * when being used, and freed */ + if (!hypre_VectorData(vector)) + { + hypre_VectorData(vector) = hypre_CTAlloc(HYPRE_Complex, num_vectors * size, memory_location); + } + + return hypre_error_flag; +} + /*-------------------------------------------------------------------------- * hypre_SeqVectorInitialize *--------------------------------------------------------------------------*/ @@ -132,7 +173,7 @@ hypre_SeqVectorInitialize( hypre_Vector *vector ) HYPRE_Int hypre_SeqVectorSetDataOwner( hypre_Vector *vector, - HYPRE_Int owns_data ) + HYPRE_Int owns_data ) { hypre_VectorOwnsData(vector) = owns_data;