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 6 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
106 changes: 105 additions & 1 deletion src/IJ_mv/HYPRE_IJVector.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
*--------------------------------------------------------------------------*/
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -318,6 +383,45 @@ 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 )
{
#if defined(HYPRE_USING_GPU)
HYPRE_ExecutionPolicy exec = hypre_GetExecPolicy1( hypre_IJVectorMemoryLocation(vector) );

if (exec == HYPRE_EXEC_DEVICE)
{
return ( hypre_IJVectorSetConstantValuesParDevice(vec, value) );
}
else
#endif
{
return ( hypre_IJVectorSetConstantValuesPar(vec, value) );
}
}
else
{
hypre_error_in_arg(1);
}

return hypre_error_flag;
}

/*--------------------------------------------------------------------------
* HYPRE_IJVectorAddToValues
*--------------------------------------------------------------------------*/
Expand Down
33 changes: 31 additions & 2 deletions src/IJ_mv/HYPRE_IJ_mv.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down 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_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.
**/
Expand Down Expand Up @@ -534,6 +557,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.
Expand Down
157 changes: 88 additions & 69 deletions src/IJ_mv/IJVector_parcsr.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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)
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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
Expand Down
Loading