Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New vector APIs for hypre_IJVector #1179

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 61 additions & 1 deletion src/IJ_mv/HYPRE_IJVector.c
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,61 @@ 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 )
{
hypre_IJVectorInitializeParShell(vec);
}
else
{
hypre_error_in_arg(1);
}

return hypre_error_flag;
}

/*--------------------------------------------------------------------------
* HYPRE_IJVectorInitializeData
*--------------------------------------------------------------------------*/

HYPRE_Int
HYPRE_IJVectorInitializeData(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_IJVectorInitializeParData(vec, data);
}
else
{
hypre_error_in_arg(1);
}

return hypre_error_flag;
}

/*--------------------------------------------------------------------------
* HYPRE_IJVectorInitialize
*--------------------------------------------------------------------------*/
Expand Down Expand Up @@ -214,8 +269,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;

Expand Down
25 changes: 24 additions & 1 deletion src/IJ_mv/HYPRE_IJ_mv.h
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,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_IJVectorInitializeData`
* 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_IJVectorInitializeData(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.
**/
Expand Down
65 changes: 53 additions & 12 deletions src/IJ_mv/IJVector_parcsr.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,13 @@ 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);
Expand All @@ -87,9 +86,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)
Expand All @@ -106,14 +102,59 @@ 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);

if (!aux_vector)
{
hypre_AuxParVectorCreate(&aux_vector);
hypre_IJVectorTranslator(vector) = aux_vector;
}
hypre_AuxParVectorInitialize_v2(aux_vector, memory_location_aux);

return hypre_error_flag;
}

/*--------------------------------------------------------------------------
* hypre_IJVectorInitializeParData
*--------------------------------------------------------------------------*/

HYPRE_Int
hypre_IJVectorInitializeParData(hypre_IJVector *vector,
HYPRE_Complex *data)
{
hypre_ParVector *par_vector = (hypre_ParVector*) hypre_IJVectorObject(vector);

hypre_ParVectorInitializeData(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);

/* Memory allocations */
hypre_ParVectorInitialize_v2(par_vector, memory_location);
hypre_AuxParVectorInitialize_v2(aux_vector, memory_location);

return hypre_error_flag;
}
Expand Down
2 changes: 2 additions & 0 deletions src/IJ_mv/_hypre_IJ_mv.h
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,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_IJVectorInitializeParData( 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);
Expand Down
2 changes: 2 additions & 0 deletions src/IJ_mv/protos.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,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_IJVectorInitializeParData( 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);
Expand Down
2 changes: 2 additions & 0 deletions src/parcsr_mv/_hypre_parcsr_mv.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_ParVectorInitializeData( 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 );
Expand Down
44 changes: 38 additions & 6 deletions src/parcsr_mv/par_vector.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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_ParVectorInitializeData
*--------------------------------------------------------------------------*/

HYPRE_Int
hypre_ParVectorInitializeData(hypre_ParVector *vector,
HYPRE_Complex *data)
{
hypre_SeqVectorInitializeData(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;
}
Expand Down
2 changes: 2 additions & 0 deletions src/parcsr_mv/protos.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_ParVectorInitializeData( 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 );
Expand Down
2 changes: 2 additions & 0 deletions src/seq_mv/protos.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_SeqVectorInitializeData( 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 );
Expand Down
2 changes: 2 additions & 0 deletions src/seq_mv/seq_mv.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_SeqVectorInitializeData( 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 );
Expand Down
Loading