Skip to content

Commit

Permalink
feature partition-pack:introduce pack callback and partitioning_t
Browse files Browse the repository at this point in the history
  • Loading branch information
hannesbrandt committed Apr 11, 2024
1 parent 92bc141 commit 3ea48f9
Show file tree
Hide file tree
Showing 12 changed files with 175 additions and 12 deletions.
3 changes: 3 additions & 0 deletions src/fclaw2d_convenience.c
Original file line number Diff line number Diff line change
Expand Up @@ -719,6 +719,9 @@ fclaw2d_domain_partition (fclaw2d_domain_t * domain, int weight_exponent)
newd->partition_unchanged_first = (int) uf;
newd->partition_unchanged_length = (int) ul;
newd->partition_unchanged_old_first = (int) uof;
/* the new domain takes ownership of the partition_context */
newd->partition_context = domain->partition_context;
domain->partition_context = NULL;

fclaw2d_domain_copy_parameters (newd, domain);
return newd;
Expand Down
3 changes: 3 additions & 0 deletions src/fclaw2d_to_3d.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#define fclaw2d_patch_callback_t fclaw3d_patch_callback_t
#define fclaw2d_patch_relation_t fclaw3d_patch_relation_t
#define fclaw2d_match_callback_t fclaw3d_match_callback_t
#define fclaw2d_pack_callback_t fclaw3d_pack_callback_t
#define fclaw2d_domain_partition_t fclaw3d_domain_partition_t
#define fclaw2d_transfer_callback_t fclaw3d_transfer_callback_t
#define fclaw2d_domain_exchange_t fclaw3d_domain_exchange_t
#define fclaw2d_domain_indirect fclaw3d_domain_indirect
Expand Down Expand Up @@ -207,6 +209,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
/* translations for dimension independent wrapper functions/types */
#define fclaw_domain_wrap_2d fclaw_domain_wrap_3d
#define fclaw2d_patch_wrap_cb fclaw3d_patch_wrap_cb
#define fclaw2d_pack_wrap_cb fclaw3d_pack_wrap_cb
#define fclaw2d_transfer_wrap_cb fclaw3d_transfer_wrap_cb
#define fclaw2d_match_wrap_cb fclaw3d_match_wrap_cb
#define fclaw2d_intersect_wrap_cb fclaw3d_intersect_wrap_cb
Expand Down
12 changes: 12 additions & 0 deletions src/fclaw2d_wrap.c
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,18 @@ void fclaw2d_patch_wrap_cb(fclaw2d_domain_t * domain_2d,
wrap->pcb(domain, patch, blockno, patchno, wrap->user);
}

void
fclaw2d_pack_wrap_cb (fclaw2d_domain_t * domain_2d,
fclaw2d_patch_t * patch_2d,
int blockno, int patchno, void *pack_data_here,
void *user)
{
fclaw_pack_wrap_user_t *wrap = (fclaw_pack_wrap_user_t *) user;
fclaw_domain_t *domain = get_domain (domain_2d);
fclaw_patch_t *patch = get_patch (patch_2d);
wrap->pcb (domain, patch, blockno, patchno, pack_data_here, wrap->user);
}

void
fclaw2d_transfer_wrap_cb(fclaw2d_domain_t * old_domain_2d,
fclaw2d_patch_t * old_patch_2d,
Expand Down
10 changes: 10 additions & 0 deletions src/fclaw2d_wrap.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,16 @@ void fclaw2d_patch_wrap_cb(fclaw2d_domain_t * domain,
fclaw2d_patch_t * patch,
int blockno, int patchno, void *user);

/**
* @brief Wraps a pack callback for 2d domains.
* This is used by passing this callback to a function that takes a @ref fclaw2d_pack_callback_t.
* The user pointer should be a pointer to a @ref fclaw_pack_wrap_user_t.
*/
void fclaw2d_pack_wrap_cb (fclaw2d_domain_t * domain,
fclaw2d_patch_t * patch,
int blockno, int patchno, void *pack_data_here,
void *user);

/**
* @brief Wraps a transfer callback for 2d domains.
* This is used by passing this callback to a function that takes a @ref fclaw2d_transfer_callback_t.
Expand Down
10 changes: 10 additions & 0 deletions src/fclaw3d_wrap.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,16 @@ void fclaw3d_patch_wrap_cb(fclaw3d_domain_t * domain,
fclaw3d_patch_t * patch,
int blockno, int patchno, void *user);

/**
* @brief Wraps a pack callback for 3d domains.
* This is used by passing this callback to a function that takes a @ref fclaw3d_pack_callback_t.
* The user pointer should be a pointer to a @ref fclaw_pack_wrap_user_t.
*/
void fclaw3d_pack_wrap_cb (fclaw3d_domain_t * domain,
fclaw3d_patch_t * patch,
int blockno, int patchno, void *pack_data_here,
void *user);

/**
* @brief Wraps a transfer callback for 2d domains.
* This is used by passing this callback to a function that takes a @ref fclaw2d_transfer_callback_t.
Expand Down
3 changes: 2 additions & 1 deletion src/fclaw_partition.c
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,8 @@ void fclaw_partition_domain(fclaw_global_t* glob,
void ** patch_data = NULL;

fclaw_domain_allocate_before_partition (*domain, data_size,
&patch_data);
&patch_data, NULL, NULL, NULL,
NULL);

/* For all (patch i) { pack its numerical data into patch_data[i] }
Does all the data in every patch need to be copied? */
Expand Down
23 changes: 23 additions & 0 deletions src/fclaw_wrap.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,29 @@ typedef struct fclaw_patch_wrap_user
void* user;
} fclaw_patch_wrap_user_t;

/**
* @brief Callback wrapper struct for pack callbacks.
*
* This allows callbacks with dimension independent types to be
* called from dimension dependent code.
*
* This type should be passed in as the user pointer alongside
* @ref fclaw2d_pack_wrap_cb or @ref fclaw3d_pack_wrap_cb
* to the function that takes a dimensioned callback.
*
* @ref fclaw2d_pack_wrap_cb or @ref fclaw3d_pack_wrap_cb
* will then call the dimension independent callback specified in the struct,
* passing the user pointer specified in the struct.
*
*/
typedef struct fclaw_pack_wrap_user
{
/** Dimension independent transfer callback to call */
fclaw_pack_callback_t pcb;
/** User pointer to pass to dimension independent callback */
void *user;
} fclaw_pack_wrap_user_t;

/**
* @brief Callback wrapper struct for transfer callbacks.
*
Expand Down
33 changes: 29 additions & 4 deletions src/forestclaw.c
Original file line number Diff line number Diff line change
Expand Up @@ -777,15 +777,40 @@ fclaw_domain_iterate_adapted(fclaw_domain_t *old_domain, fclaw_domain_t *new_dom
}
}

void fclaw_domain_allocate_before_partition(fclaw_domain_t *domain, size_t data_size, void ***patch_data)
{
void
fclaw_domain_allocate_before_partition (fclaw_domain_t * domain,
size_t data_size,
void ***patch_data,
fclaw_pack_callback_t patch_pack,
void *user_pack,
fclaw_pack_callback_t patch_unpack,
void *user_unpack)
{
fclaw_pack_wrap_user_t patch_pack_wrap_user, patch_unpack_wrap_user;
patch_pack_wrap_user.pcb = patch_pack;
patch_pack_wrap_user.user = user_pack;
patch_unpack_wrap_user.pcb = patch_unpack;
patch_unpack_wrap_user.user = user_unpack;

if(domain->refine_dim == 2)
{
fclaw2d_domain_allocate_before_partition(domain->d2,data_size,patch_data);
fclaw2d_domain_allocate_before_partition(domain->d2,
data_size,
patch_data,
fclaw2d_pack_wrap_cb,
&patch_pack_wrap_user,
fclaw2d_pack_wrap_cb,
&patch_unpack_wrap_user);
}
else if (domain->refine_dim == 3)
{
fclaw3d_domain_allocate_before_partition(domain->d3,data_size,patch_data);
fclaw3d_domain_allocate_before_partition(domain->d3,
data_size,
patch_data,
fclaw3d_pack_wrap_cb,
&patch_pack_wrap_user,
fclaw3d_pack_wrap_cb,
&patch_unpack_wrap_user);
}
else
{
Expand Down
11 changes: 10 additions & 1 deletion src/forestclaw.h
Original file line number Diff line number Diff line change
Expand Up @@ -921,6 +921,11 @@ void fclaw_domain_iterate_adapted (fclaw_domain_t * old_domain,
/* ---------------------------------------------------------------------- */
///@{

typedef void (*fclaw_pack_callback_t) (fclaw_domain_t * domain,
fclaw_patch_t * patch, int blockno,
int patchno, void *pack_data_here,
void *user);

/** Allocate data buffer for parallel transfer of all patches.
* \param [in,out] domain The memory lives inside this domain.
* \param [in] data_size Number of bytes per patch to transfer.
Expand All @@ -933,7 +938,11 @@ void fclaw_domain_iterate_adapted (fclaw_domain_t * old_domain,
*/
void fclaw_domain_allocate_before_partition (fclaw_domain_t * domain,
size_t data_size,
void ***patch_data);
void ***patch_data,
fclaw_pack_callback_t patch_pack,
void *user_pack,
fclaw_pack_callback_t patch_unpack,
void *user_unpack);

/** Reallocate data buffer to reflect patch data after partition.
* \param [in,out] domain The memory lives inside this domain.
Expand Down
25 changes: 23 additions & 2 deletions src/forestclaw2d.c
Original file line number Diff line number Diff line change
Expand Up @@ -1548,8 +1548,24 @@ fclaw2d_domain_assign_for_partition (fclaw2d_domain_t * domain,
void
fclaw2d_domain_allocate_before_partition (fclaw2d_domain_t * domain,
size_t data_size,
void ***patch_data)
{
void ***patch_data,
fclaw2d_pack_callback_t patch_pack,
void *user_pack,
fclaw2d_pack_callback_t
patch_unpack, void *user_unpack)
{
fclaw2d_domain_partition_t *p;

/* store the packing callbacks in the domains partition context */
FCLAW_ASSERT (domain->partition_context == NULL);
p = FCLAW_ALLOC (fclaw2d_domain_partition_t, 1);
p->data_size = data_size;
p->patch_pack = patch_pack;
p->user_pack = user_pack;
p->patch_unpack = patch_unpack;
p->user_unpack = user_unpack;
domain->partition_context = p;

p4est_wrap_t *wrap = (p4est_wrap_t *) domain->pp;

FCLAW_ASSERT (*patch_data == NULL);
Expand Down Expand Up @@ -1709,6 +1725,11 @@ fclaw2d_domain_free_after_partition (fclaw2d_domain_t * domain,
FCLAW_FREE (*patch_data);
*patch_data = NULL;

/* free partition_context allocated in allocate_before_partition */
FCLAW_ASSERT (domain->partition_context != NULL);
FCLAW_FREE (domain->partition_context);
domain->partition_context = NULL;

p4est_reset_data (wrap->p4est, 0, NULL, wrap->p4est->user_pointer);
}

Expand Down
27 changes: 25 additions & 2 deletions src/forestclaw2d.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ typedef struct fclaw2d_domain_persist
}
fclaw2d_domain_persist_t;

typedef struct fclaw2d_domain_partition fclaw2d_domain_partition_t;

/**
* @brief The domain structure is a collection of blocks
*
Expand Down Expand Up @@ -197,6 +199,7 @@ struct fclaw2d_domain
int partition_unchanged_first; /**< local index of first unchanged patch */
int partition_unchanged_length; /**< number of unchanged quadrants */
int partition_unchanged_old_first; /**< local index wrt. previous partition */
fclaw2d_domain_partition_t *partition_context; /**< info necessary to partition patch data */

int num_blocks; /**< Total number of blocks. */
fclaw2d_block_t *blocks; /**< allocated storage */
Expand Down Expand Up @@ -729,10 +732,25 @@ void fclaw2d_domain_iterate_adapted (fclaw2d_domain_t * old_domain,

///@}
/* ---------------------------------------------------------------------- */
/// @name Parititon
/// @name Partition
/* ---------------------------------------------------------------------- */
///@{

typedef void (*fclaw2d_pack_callback_t) (fclaw2d_domain_t * domain,
fclaw2d_patch_t * patch, int blockno,
int patchno, void *pack_data_here,
void *user);

typedef struct fclaw2d_domain_partition
{
size_t data_size; /**< The number of bytes per patch to send */
fclaw2d_pack_callback_t patch_pack;
void *user_pack;
fclaw2d_pack_callback_t patch_unpack;
void *user_unpack;
}
fclaw2d_domain_partition_t;

/** Allocate data buffer for parallel transfer of all patches.
* \param [in,out] domain The memory lives inside this domain.
* \param [in] data_size Number of bytes per patch to transfer.
Expand All @@ -745,7 +763,12 @@ void fclaw2d_domain_iterate_adapted (fclaw2d_domain_t * old_domain,
*/
void fclaw2d_domain_allocate_before_partition (fclaw2d_domain_t * domain,
size_t data_size,
void ***patch_data);
void ***patch_data,
fclaw2d_pack_callback_t
patch_pack, void *user_pack,
fclaw2d_pack_callback_t
patch_unpack,
void *user_unpack);

/** Reallocate data buffer to reflect patch data after partition.
* \param [in,out] domain The memory lives inside this domain.
Expand Down
27 changes: 25 additions & 2 deletions src/forestclaw3d.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,8 @@ typedef struct fclaw3d_domain_persist
}
fclaw3d_domain_persist_t;

typedef struct fclaw3d_domain_partition fclaw3d_domain_partition_t;

/**
* @brief The domain structure is a collection of blocks
*
Expand Down Expand Up @@ -200,6 +202,7 @@ struct fclaw3d_domain
int partition_unchanged_first; /**< local index of first unchanged patch */
int partition_unchanged_length; /**< number of unchanged quadrants */
int partition_unchanged_old_first; /**< local index wrt. previous partition */
fclaw3d_domain_partition_t *partition_context; /**< info necessary to partition patch data */

int num_blocks; /**< Total number of blocks. */
fclaw3d_block_t *blocks; /**< allocated storage */
Expand Down Expand Up @@ -852,10 +855,25 @@ void fclaw3d_domain_iterate_adapted (fclaw3d_domain_t * old_domain,

///@}
/* ---------------------------------------------------------------------- */
/// @name Parititon
/// @name Partition
/* ---------------------------------------------------------------------- */
///@{

typedef void (*fclaw3d_pack_callback_t) (fclaw3d_domain_t * domain,
fclaw3d_patch_t * patch, int blockno,
int patchno, void *pack_data_here,
void *user);

typedef struct fclaw3d_domain_partition
{
size_t data_size; /**< The number of bytes per patch to send */
fclaw3d_pack_callback_t patch_pack;
void *user_pack;
fclaw3d_pack_callback_t patch_unpack;
void *user_unpack;
}
fclaw3d_domain_partition_t;

/** Allocate data buffer for parallel transfer of all patches.
* \param [in,out] domain The memory lives inside this domain.
* \param [in] data_size Number of bytes per patch to transfer.
Expand All @@ -868,7 +886,12 @@ void fclaw3d_domain_iterate_adapted (fclaw3d_domain_t * old_domain,
*/
void fclaw3d_domain_allocate_before_partition (fclaw3d_domain_t * domain,
size_t data_size,
void ***patch_data);
void ***patch_data,
fclaw3d_pack_callback_t
patch_pack, void *user_pack,
fclaw3d_pack_callback_t
patch_unpack,
void *user_unpack);

/** Reallocate data buffer to reflect patch data after partition.
* \param [in,out] domain The memory lives inside this domain.
Expand Down

0 comments on commit 3ea48f9

Please sign in to comment.