Commit 59bc7f76 authored by Swann Perarnau's avatar Swann Perarnau
Browse files

Merge branch 'layout-destroy' into 'staging'

Generic layout_destroy

See merge request !144
parents d1d0d11b ed1f976f
Pipeline #10948 passed with stages
in 3 minutes and 27 seconds
......@@ -448,9 +448,9 @@ int main(int argc, char *argv[])
aml_tiling_resize_destroy(&ta);
aml_tiling_resize_destroy(&tb);
aml_tiling_resize_destroy(&tc);
aml_layout_dense_destroy(&la);
aml_layout_dense_destroy(&lb);
aml_layout_dense_destroy(&lc);
aml_layout_destroy(&la);
aml_layout_destroy(&lb);
aml_layout_destroy(&lc);
aml_area_munmap(area, a, memsize * sizeof(double));
aml_area_munmap(area, b, memsize * sizeof(double));
aml_area_munmap(area, c, memsize * sizeof(double));
......
......@@ -77,8 +77,8 @@ main(void)
return 1;
// cleanup
aml_layout_dense_destroy(&src_layout);
aml_layout_dense_destroy(&dst_layout);
aml_layout_destroy(&src_layout);
aml_layout_destroy(&dst_layout);
aml_dma_linux_par_destroy(&dma);
return 0;
......
......@@ -155,10 +155,10 @@ main(void)
//----------------------------- Cleanup
//-------------------------------------
aml_layout_dense_destroy(&a_layout);
aml_layout_dense_destroy(&b_layout);
aml_layout_dense_destroy(&a_continuous_layout);
aml_layout_dense_destroy(&a_continuous_layout);
aml_layout_destroy(&a_layout);
aml_layout_destroy(&b_layout);
aml_layout_destroy(&a_continuous_layout);
aml_layout_destroy(&a_continuous_layout);
aml_dma_linux_par_destroy(&dma);
return 0;
......
......@@ -70,8 +70,8 @@ int main(int argc, char **argv)
aml_layout_fprintf(stderr, "layout_f", layout_f);
print_layout(layout_f);
aml_layout_dense_destroy(&layout_c);
aml_layout_dense_destroy(&layout_f);
aml_layout_destroy(&layout_c);
aml_layout_destroy(&layout_f);
aml_area_munmap(area, array, sizeof(double) * x * y);
aml_finalize();
......
......@@ -132,8 +132,8 @@ int main(int argc, char **argv)
assert(!aml_layout_reshape(layout_c, &reshape_layout, 3, new_dims));
print_layout(reshape_layout);
aml_layout_dense_destroy(&layout_c);
aml_layout_dense_destroy(&reshape_layout);
aml_layout_destroy(&layout_c);
aml_layout_destroy(&reshape_layout);
aml_area_munmap(area, mat, sizeof(double) * x * y);
aml_finalize();
......
......@@ -123,10 +123,9 @@ int main(int argc, char **argv)
(size_t[]){i, 0, 0}));
fprintf(stderr, "\n");
aml_layout_dense_destroy(&lay_part);
aml_layout_dense_destroy(&layout_elements);
aml_layout_dense_destroy(&new_layout);
aml_layout_destroy(&lay_part);
aml_layout_destroy(&layout_elements);
aml_layout_destroy(&new_layout);
aml_area_munmap(area, particles, sizeof(struct particle) * size);
free(array_coords);
......
......@@ -107,9 +107,11 @@ destroy your layout when you're done using it.
.. code-block:: c
aml_layout_dense_destroy(&layout_c);
aml_layout_dense_destroy(&layout_f);
aml_layout_destroy(&layout_c);
aml_layout_destroy(&layout_f);
Note that destroy is one of many operations that are generic to the layout
type.
Generic operations on an AML layout
-----------------------------------
......
......@@ -69,7 +69,7 @@ int fill_tiling(struct aml_tiling *tiling)
}
}
aml_layout_dense_destroy(&ltile);
aml_layout_destroy(&ltile);
}
return AML_SUCCESS;
}
......@@ -146,8 +146,8 @@ int main(int argc, char **argv)
/* Destroy everything */
aml_tiling_resize_destroy(&tiling_c);
aml_tiling_resize_destroy(&tiling_f);
aml_layout_dense_destroy(&layout_c);
aml_layout_dense_destroy(&layout_f);
aml_layout_destroy(&layout_c);
aml_layout_destroy(&layout_f);
aml_area_munmap(area, mat, sizeof(double) * x * y);
aml_finalize();
......
......@@ -177,9 +177,9 @@ int main(int argc, char **argv)
aml_tiling_resize_destroy(&tiling_a);
aml_tiling_resize_destroy(&tiling_b);
aml_tiling_resize_destroy(&tiling_c);
aml_layout_dense_destroy(&layout_a);
aml_layout_dense_destroy(&layout_b);
aml_layout_dense_destroy(&layout_c);
aml_layout_destroy(&layout_a);
aml_layout_destroy(&layout_b);
aml_layout_destroy(&layout_c);
free(a);
free(b);
free(c);
......
......@@ -451,6 +451,11 @@ struct aml_layout_ops {
**/
int (*fprintf)(const struct aml_layout_data *data,
FILE *stream, const char *prefix);
/**
* Destroys the layout and frees all associated memory.
**/
void (*destroy)(struct aml_layout *);
};
/**
......@@ -617,6 +622,11 @@ int aml_layout_slice(const struct aml_layout *layout,
int aml_layout_fprintf(FILE *stream, const char *prefix,
const struct aml_layout *layout);
/**
* Destroy (free) a layout, irrespective of its type.
* @param[in,out] layout the layout to destroy. NULL on return.
**/
void aml_layout_destroy(struct aml_layout **layout);
////////////////////////////////////////////////////////////////////////////////
......
......@@ -80,14 +80,6 @@ int aml_layout_cuda_create(struct aml_layout **out,
const size_t *stride,
const size_t *pitch);
/**
* Destroy a layout obtained with aml_layout_cuda_create().
* @param[in, out] layout: A pointer to the layout to destroy.
* On exit, the pointer content is set to NULL.
* @return AML_SUCCESS or -AML_EINVAL if layout or *layout is NULL.
**/
int aml_layout_cuda_destroy(struct aml_layout **layout);
/** Always returns the pointer to device_ptr, whatever the coordinates. **/
void *aml_layout_cuda_deref(const struct aml_layout_data *data,
const size_t *coords);
......
......@@ -81,12 +81,6 @@ int aml_layout_dense_create(struct aml_layout **layout,
const size_t *stride,
const size_t *pitch);
/**
* Function to free a dense layout.
* @param[in,out] layout: The layout to deallocate. NULL on return.
**/
void aml_layout_dense_destroy(struct aml_layout **layout);
/**
* Deref operator for dense layout in AML_ORDER_COLUMN_MAJOR.
* Also used as the deref operator for this type of layout.
......
......@@ -45,11 +45,27 @@ struct aml_layout_pad {
void *neutral;
};
int aml_layout_pad_create(struct aml_layout **layout, const int order,
struct aml_layout *target, const size_t *dim,
void *neutral);
/**
* Creates a padded layout on top of a target layout, and takes ownership of it
* (destroy will allow destroy the target layout).
* @param layout a pointer to where to store a newly allocated layout.
* @param order the order in which dimensions are organized.
* @param target targeted layout.
* @param dims the number of elements along each dimension of the layout,
* including targeted layout and its pad
* @param neutral a pointer to a neutral element to fill the pad with (copied
* internally).
* @return -AML_ENOMEM if layout allocation failed
* @return -AML_EINVAL if target, dims, or neutral are NULL
* @return AML_SUCCESS if creation succeeded.
**/
int aml_layout_pad_create(struct aml_layout **layout,
const int order,
struct aml_layout *target,
const size_t *dims,
void *neutral);
void aml_layout_pad_destroy(struct aml_layout **layout);
void aml_layout_pad_destroy(struct aml_layout *layout);
/**
* Pre-existing operators for padded layout
......
......@@ -37,13 +37,26 @@ struct aml_layout_data_reshape {
size_t *target_dims;
};
/**
* Creates a reshaped layout on top of a target layout and takes ownership of it
* (destroy will allow destroy the target layout).
* @param layout a pointer to where to store the newly allocated layout
* @param target targeted layout.
* @param order the order in which dimensions are organized.
* @param ndims the number of dimensions of the reshaped layout
* @param dims the number of elements along each dimension of the reshaped
* layout.
* @return -AML_ENOMEM if layout allocation failed
* @return -AML_EINVAL if target or dims are NULL
* @return AML_SUCCESS if creation succeeded.
**/
int aml_layout_reshape_create(struct aml_layout **layout,
struct aml_layout *target,
const int order,
const size_t ndims,
const size_t *dims);
void aml_layout_reshape_destroy(struct aml_layout **l);
void aml_layout_reshape_destroy(struct aml_layout *l);
extern struct aml_layout_ops aml_layout_reshape_row_ops;
extern struct aml_layout_ops aml_layout_reshape_column_ops;
......
......@@ -69,15 +69,6 @@ int aml_layout_cuda_create(struct aml_layout **out,
return AML_SUCCESS;
}
int aml_layout_cuda_destroy(struct aml_layout **layout)
{
if (layout == NULL || *layout == NULL)
return -AML_EINVAL;
free(*layout);
*layout = NULL;
return AML_SUCCESS;
}
void *aml_layout_cuda_deref(const struct aml_layout_data *data,
const size_t *coords)
{
......@@ -167,4 +158,6 @@ struct aml_layout_ops aml_layout_cuda_ops = {
.reshape = NULL,
.slice = NULL,
.slice_native = NULL,
.fprintf = NULL,
.destroy = NULL,
};
......@@ -128,14 +128,6 @@ int aml_layout_dense_create(struct aml_layout **layout,
return AML_SUCCESS;
}
void aml_layout_dense_destroy(struct aml_layout **l)
{
if (l == NULL || *l == NULL)
return;
free(*l);
*l = NULL;
}
/*******************************************************************************
* COLUMN OPERATORS:
******************************************************************************/
......@@ -394,20 +386,20 @@ int aml_layout_column_fprintf(const struct aml_layout_data *data,
return AML_SUCCESS;
}
struct aml_layout_ops aml_layout_column_ops = {
aml_layout_column_deref,
aml_layout_column_deref,
aml_layout_dense_rawptr,
aml_layout_column_order,
aml_layout_column_dims,
aml_layout_column_dims,
aml_layout_dense_ndims,
aml_layout_dense_element_size,
aml_layout_column_reshape,
aml_layout_column_slice,
aml_layout_column_slice,
aml_layout_column_fprintf,
aml_layout_column_deref,
aml_layout_column_deref,
aml_layout_dense_rawptr,
aml_layout_column_order,
aml_layout_column_dims,
aml_layout_column_dims,
aml_layout_dense_ndims,
aml_layout_dense_element_size,
aml_layout_column_reshape,
aml_layout_column_slice,
aml_layout_column_slice,
aml_layout_column_fprintf,
NULL,
};
/*******************************************************************************
......@@ -595,17 +587,17 @@ int aml_layout_row_fprintf(const struct aml_layout_data *data,
}
struct aml_layout_ops aml_layout_row_ops = {
aml_layout_row_deref,
aml_layout_column_deref,
aml_layout_dense_rawptr,
aml_layout_row_order,
aml_layout_row_dims,
aml_layout_column_dims,
aml_layout_dense_ndims,
aml_layout_dense_element_size,
aml_layout_row_reshape,
aml_layout_row_slice,
aml_layout_row_slice_native,
aml_layout_row_fprintf,
aml_layout_row_deref,
aml_layout_column_deref,
aml_layout_dense_rawptr,
aml_layout_row_order,
aml_layout_row_dims,
aml_layout_column_dims,
aml_layout_dense_ndims,
aml_layout_dense_element_size,
aml_layout_row_reshape,
aml_layout_row_slice,
aml_layout_row_slice_native,
aml_layout_row_fprintf,
NULL,
};
......@@ -296,3 +296,16 @@ int aml_layout_fprintf(FILE *stream, const char *prefix,
return layout->ops->fprintf(layout->data, stream, p);
}
void aml_layout_destroy(struct aml_layout **layout)
{
if (layout == NULL || *layout == NULL)
return;
struct aml_layout *l = *layout;
if (l->ops->destroy != NULL)
l->ops->destroy(l);
else
free(l);
*layout = NULL;
}
......@@ -106,12 +106,13 @@ int aml_layout_pad_create(struct aml_layout **layout, const int order,
return AML_SUCCESS;
}
void aml_layout_pad_destroy(struct aml_layout **l)
void aml_layout_pad_destroy(struct aml_layout *l)
{
if (l == NULL || *l == NULL)
return;
free(*l);
*l = NULL;
assert(l != NULL);
struct aml_layout_pad *data = (struct aml_layout_pad *)l->data;
aml_layout_destroy(&data->target);
free(l);
}
/*******************************************************************************
......@@ -198,18 +199,19 @@ int aml_layout_pad_column_fprintf(const struct aml_layout_data *data,
}
struct aml_layout_ops aml_layout_pad_column_ops = {
aml_layout_pad_column_deref,
aml_layout_pad_column_deref,
aml_layout_pad_rawptr,
aml_layout_pad_column_order,
aml_layout_pad_column_dims,
aml_layout_pad_column_dims,
aml_layout_pad_ndims,
aml_layout_pad_element_size,
NULL,
NULL,
NULL,
aml_layout_pad_column_fprintf,
aml_layout_pad_column_deref,
aml_layout_pad_column_deref,
aml_layout_pad_rawptr,
aml_layout_pad_column_order,
aml_layout_pad_column_dims,
aml_layout_pad_column_dims,
aml_layout_pad_ndims,
aml_layout_pad_element_size,
NULL,
NULL,
NULL,
aml_layout_pad_column_fprintf,
aml_layout_pad_destroy,
};
/*******************************************************************************
......@@ -285,17 +287,17 @@ int aml_layout_pad_row_fprintf(const struct aml_layout_data *data,
}
struct aml_layout_ops aml_layout_pad_row_ops = {
aml_layout_pad_row_deref,
aml_layout_pad_column_deref,
aml_layout_pad_rawptr,
aml_layout_pad_row_order,
aml_layout_pad_row_dims,
aml_layout_pad_column_dims,
aml_layout_pad_ndims,
aml_layout_pad_element_size,
NULL,
NULL,
NULL,
aml_layout_pad_row_fprintf,
aml_layout_pad_row_deref,
aml_layout_pad_column_deref,
aml_layout_pad_rawptr,
aml_layout_pad_row_order,
aml_layout_pad_row_dims,
aml_layout_pad_column_dims,
aml_layout_pad_ndims,
aml_layout_pad_element_size,
NULL,
NULL,
NULL,
aml_layout_pad_row_fprintf,
aml_layout_pad_destroy,
};
......@@ -125,12 +125,14 @@ int aml_layout_reshape_create(struct aml_layout **layout,
return AML_SUCCESS;
}
void aml_layout_reshape_destroy(struct aml_layout **layout)
void aml_layout_reshape_destroy(struct aml_layout *l)
{
if (layout == NULL || *layout == NULL)
return;
free(*layout);
*layout = NULL;
assert(l != NULL);
struct aml_layout_data_reshape *data =
(struct aml_layout_data_reshape *)l->data;
aml_layout_destroy(&data->target);
free(l);
}
/*******************************************************************************
......@@ -228,18 +230,19 @@ int aml_layout_reshape_column_fprintf(const struct aml_layout_data *data,
}
struct aml_layout_ops aml_layout_reshape_column_ops = {
aml_layout_reshape_column_deref,
aml_layout_reshape_column_deref,
aml_layout_reshape_rawptr,
aml_layout_reshape_column_order,
aml_layout_reshape_column_dims,
aml_layout_reshape_column_dims,
aml_layout_reshape_ndims,
aml_layout_reshape_element_size,
NULL,
NULL,
NULL,
aml_layout_reshape_column_fprintf,
aml_layout_reshape_column_deref,
aml_layout_reshape_column_deref,
aml_layout_reshape_rawptr,
aml_layout_reshape_column_order,
aml_layout_reshape_column_dims,
aml_layout_reshape_column_dims,
aml_layout_reshape_ndims,
aml_layout_reshape_element_size,
NULL,
NULL,
NULL,
aml_layout_reshape_column_fprintf,
aml_layout_reshape_destroy,
};
/*******************************************************************************
......@@ -313,16 +316,17 @@ int aml_layout_reshape_row_fprintf(const struct aml_layout_data *data,
}
struct aml_layout_ops aml_layout_reshape_row_ops = {
aml_layout_reshape_row_deref,
aml_layout_reshape_column_deref,
aml_layout_reshape_rawptr,
aml_layout_reshape_row_order,
aml_layout_reshape_row_dims,
aml_layout_reshape_column_dims,
aml_layout_reshape_ndims,
aml_layout_reshape_element_size,
NULL,
NULL,
NULL,
aml_layout_reshape_row_fprintf,
aml_layout_reshape_row_deref,
aml_layout_reshape_column_deref,
aml_layout_reshape_rawptr,
aml_layout_reshape_row_order,
aml_layout_reshape_row_dims,
aml_layout_reshape_column_dims,
aml_layout_reshape_ndims,
aml_layout_reshape_element_size,
NULL,
NULL,
NULL,
aml_layout_reshape_row_fprintf,
aml_layout_reshape_destroy,
};
......@@ -165,7 +165,6 @@ aml_tiling_pad_column_index(const struct aml_tiling_data *t,
int order = aml_layout_order(d->layout);
if (AML_LAYOUT_ORDER(order) == AML_LAYOUT_ORDER_COLUMN_MAJOR) {
/* WARNING: OWNERSHIP!!! */
aml_layout_pad_create(&p_layout,
AML_LAYOUT_ORDER_COLUMN_MAJOR,
ret, d->tile_dims, d->neutral);
......@@ -174,7 +173,6 @@ aml_tiling_pad_column_index(const struct aml_tiling_data *t,
for (size_t i = 0; i < ndims; i++)
row_dims[i] = d->tile_dims[ndims - i - 1];
/* WARNING: OWNERSHIP!!! */
aml_layout_pad_create(&p_layout,
AML_LAYOUT_ORDER_ROW_MAJOR,
ret, row_dims, d->neutral);
......@@ -342,7 +340,6 @@ aml_tiling_pad_row_index(const struct aml_tiling_data *t, const size_t *coords)
int order = aml_layout_order(d->layout);
if (AML_LAYOUT_ORDER(order) == AML_LAYOUT_ORDER_COLUMN_MAJOR) {
/* WARNING: OWNERSHIP!!! */
aml_layout_pad_create(&p_layout,
AML_LAYOUT_ORDER_COLUMN_MAJOR,
ret, d->tile_dims, d->neutral);
......@@ -351,7 +348,6 @@ aml_tiling_pad_row_index(const struct aml_tiling_data *t, const size_t *coords)
for (size_t i = 0; i < ndims; i++)
row_dims[i] = d->tile_dims[ndims - i - 1];
/* WARNING: OWNERSHIP!!! */
aml_layout_pad_create(&p_layout,
AML_LAYOUT_ORDER_ROW_MAJOR,
ret, row_dims, d->neutral);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment