aml.h 38.3 KB
Newer Older
Swann Perarnau's avatar
Swann Perarnau committed
1 2 3 4 5 6 7 8
/*******************************************************************************
 * Copyright 2019 UChicago Argonne, LLC.
 * (c.f. AUTHORS, LICENSE)
 *
 * This file is part of the AML project.
 * For more info, see https://xgitlab.cels.anl.gov/argo/aml
 *
 * SPDX-License-Identifier: BSD-3-Clause
9
 ******************************************************************************/
Swann Perarnau's avatar
Swann Perarnau committed
10

11 12 13 14 15 16 17
/**
 * \file aml.h
 *
 * \brief Main AML header file, contains all high level
 * abstractions declarations.
 **/

18 19 20
#ifndef AML_H
#define AML_H 1

21
#include <assert.h>
22
#include <errno.h>
23 24 25 26 27 28 29
#include <inttypes.h>
#include <numa.h>
#include <numaif.h>
#include <pthread.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdio.h>
30
#include <stdlib.h>
31 32
#include <string.h>
#include <strings.h>
33 34 35
#include <sys/mman.h>
#include <unistd.h>

36
#include "aml/utils/bitmap.h"
Nicolas Denoyelle's avatar
Nicolas Denoyelle committed
37
#include "aml/utils/error.h"
38
#include "aml/utils/inner-malloc.h"
39
#include "aml/utils/vector.h"
Nicolas Denoyelle's avatar
Nicolas Denoyelle committed
40
#include "aml/utils/version.h"
41

42
////////////////////////////////////////////////////////////////////////////////
43

44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
/**
 * @}
 * @defgroup aml "AML Library functions"
 * @brief Initialize/Finalize library.
 *
 * General functions of aml library.
 * Initialization of internal structures, cleanup of everything at the end.
 *
 * @see aml_error
 * @see aml_version
 * @{
 **/

////////////////////////////////////////////////////////////////////////////////

/**
60
 * Initializes the library.
61 62 63 64 65 66
 * @param argc: pointer to the main()'s argc argument; contents can get
 *        modified.
 * @param argv: pointer to the main()'s argv argument; contents can get
 *        modified.
 * @return 0 if successful; an error code otherwise.
 **/
67
int aml_init(int *argc, char **argv[]);
68 69

/**
70
 * Terminates the library.
71 72
 * @return 0 if successful; an error code otherwise.
 **/
73
int aml_finalize(void);
74

75
////////////////////////////////////////////////////////////////////////////////
76

Nicolas Denoyelle's avatar
Nicolas Denoyelle committed
77
/**
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
 * @}
 * @defgroup aml_area "AML Area"
 * @brief Area High-Level API
 *
 * AML areas represent places where data can belong.
 * In shared memory systems, locality is a major concern for performance.
 * Beeing able to query memory from specific places is of a major interest
 * two achieve this goal. AML Areas provide mmap / munmap low level functions
 * to query memory from specific places materialized as areas. Available area
 * implementations dictate the way such places can be arranged and with which
 * properties. It is important to notice that function provided through Area API
 * are low-level functions and are not optimized for performance as allocator
 * are.
 *
 * @image html area.png "Illustration of areas on a copmlex system." width=700cm
 *
 * @see aml_area_linux
 *
 * @{
Nicolas Denoyelle's avatar
Nicolas Denoyelle committed
97 98
 **/

99 100 101 102 103 104
////////////////////////////////////////////////////////////////////////////////

/**
 * aml_area_data is an opaque handle defined by each aml_area
 * implementation. This not supposed to be used by end users.
 **/
105 106
struct aml_area_data;

107 108 109 110 111 112 113 114
/**
 * Opaque handle to pass additional options to area mmap hook.
 * This is implementation specific and cannot be used as a
 * generic interface but rather for customizing area behaviour
 * on per mmap basis.
 **/
struct aml_area_mmap_options;

115 116 117 118 119 120
/**
 * aml_area_ops is a structure containing implementations
 * of an area operations.
 * Aware users may create or modify implementation by assembling
 * appropriate operations in such a structure.
 **/
121 122
struct aml_area_ops {
	/**
123
	 * Building block for coarse grain allocator of virtual memory.
124
	 *
125 126
	 * @param[in] data: Opaque handle to implementation specific data.
	 * @param[in] size: The minimum size of allocation.
127 128 129 130 131
	 *        Is greater than 0. Must not fail unless not enough
	 *        memory is available, or ptr argument does not point to a
	 *        suitable address.
	 *        In case of failure, aml_errno must be set to an appropriate
	 *        value.
132 133
	 * @param[in/out] opts: Opaque handle to pass additional options to area
	 *        mmap hook. Can be NULL and must work with NULL opts.
134
	 * @return a pointer to allocated memory object.
135
	 **/
136
	void* (*mmap)(const struct aml_area_data  *data,
137 138
		      size_t                       size,
		      struct aml_area_mmap_options *opts);
139 140

	/**
141 142
	 * Building block for unmapping of virtual memory mapped with mmap()
	 * of the same area.
143
	 *
144 145 146 147 148 149
	 * @param data: An opaque handle to implementation specific data.
	 * @param ptr: Pointer to data mapped in physical memory. Cannot be
	 *        NULL.
	 * @param size: The size of data. Cannot be 0.
	 * @return: AML_AREA_* error code.
	 * @see mmap()
150
	 **/
151
	int (*munmap)(const struct aml_area_data *data,
152 153 154 155
		      void                       *ptr,
		      size_t                      size);
};

156 157 158 159 160 161 162
/**
 * An AML area is an implementation of memory operations for several type
 * of devices through a consistent abstraction.
 * This abstraction is meant to be implemented for several kind of devices,
 * i.e the same function calls allocate different kinds of devices depending
 * on the area implementation provided.
 **/
163
struct aml_area {
164
	/** Basic memory operations implementation **/
165
	struct aml_area_ops *ops;
166
	/** Implementation specific data. Set to NULL at creation. **/
167 168
	struct aml_area_data *data;
};
Nicolas Denoyelle's avatar
Nicolas Denoyelle committed
169 170

/**
171
 * Low-level function for getting memory from an area.
172 173 174
 * @param[in] area: A valid area implementing access to target memory.
 * @param[in] size: The usable size of memory returned.
 * @param[in, out] opts: Opaque handle to pass additional options to area
175
 * @return virtual memory from this area with at least queried size bytes.
176 177
 * @return NULL on failure, with aml_errno set to the appropriate error
 * code.
Nicolas Denoyelle's avatar
Nicolas Denoyelle committed
178
 **/
179 180 181
void *aml_area_mmap(const struct aml_area        *area,
		    size_t                        size,
		    struct aml_area_mmap_options *opts);
Nicolas Denoyelle's avatar
Nicolas Denoyelle committed
182 183 184

/**
 * Release data provided with aml_area_mmap() and the same area.
185 186
 * @param area: A valid area implementing access to target memory.
 * @param ptr: A pointer to memory address provided with aml_area_mmap()
Nicolas Denoyelle's avatar
Nicolas Denoyelle committed
187
 *        by same area and size.
188 189 190
 * @param size: The size of memory region pointed by "ptr".
 * @return an AML error code on operation success.
 * @see aml_area_mmap()
Nicolas Denoyelle's avatar
Nicolas Denoyelle committed
191 192 193 194 195
 **/
int
aml_area_munmap(const struct aml_area *area,
		void                  *ptr,
		size_t                 size);
196

197
////////////////////////////////////////////////////////////////////////////////
198

199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557
/**
 * @}
 * @defgroup aml_layout "AML Layout"
 * @brief Low level description of data orrganization at the byte granularity.
 *
 * Layout describes how contiguous element of a flat memory address space are
 * organized into a multidimensional array of elements of a fixed size.
 * The abstraction provide functions to build layouts, access elements,
 * reshape a layout, or subset a layout.
 *
 * Layouts are characterized by:
 * * A pointer to the data it describes
 * * A set of dimensions on which data spans.
 * * A stride in between elements of a dimension.
 * * A pitch indicating the space between contiguous elements of a dimension.
 *
 * The figure below describes a 2D layout with a sub-layout
 * (obtained with aml_layout_slice()) operation. The sub-layout has a stride
 * of 1 element along the second dimension. The slice has an offset of 1 element
 * along the same dimension, and its pitch is the pitch of the original
 * layout. Calling aml_layout_deref() on this sublayout with appropriate
 * coordinates will return a pointer to elements noted (coor_x, coord_y).
 * @see aml_layout_slice()
 *
 * @image html layout.png "2D layout with a 2D slice." width=400cm
 *
 * Access to specific elements of a layout can be done with
 * the aml_layout_deref() function. Access to an element is always done
 * relatively to the dimensions order set by at creation time.
 * However, internally, the library will store dimensions from the last
 * dimension to the first dimension such that elements along the first dimension
 * are contiguous in memory. This order is defined called with the value
 * AML_LAYOUT_ORDER_FORTRAN. Therefore, AML provides access to elements
 * without the overhead of user order choice through function suffixed
 * with "native".
 * @see aml_layout_deref()
 * @see aml_layout_deref_native()
 * @see aml_layout_dims_native()
 * @see aml_layout_slice_native()
 *
 * The layout abstraction also provides a function to reshape data
 * with a different set of dimensions. A reshaped layout will access
 * the same data but with different coordinates as pictured in the
 * figure below.
 * @see aml_layout_reshape()
 *
 * @image html reshape.png "2D layout turned into a 3D layout." width=700cm
 *
 * @see aml_layout_dense
 * @see aml_layout_pad
 * @{
 **/

////////////////////////////////////////////////////////////////////////////////

struct aml_layout_ops;
struct aml_layout_data;

/** Structure definition of AML layouts **/
struct aml_layout {
	/** Layout functions implementation **/
	struct aml_layout_ops *ops;
	/** Implementation specific data of a layout**/
	struct aml_layout_data *data;
};

/** List of operators implemented by layouts. **/
struct aml_layout_ops {
	/**
	 * Layout must provide a way to access a specific element
	 * according to the provided dimensions.
	 * Coordinates bounds checking is done in the generic API.
	 * Coordinates provided by the user will match the order
	 * Of the dimensions provided by the user in the constructor.
	 * However, dimensions are always stored internally in the
	 * AML_LAYOUT_ORDER_FORTRAN order.
	 * @param data[in]: The non-NULL handle to layout internal data.
	 * @param coords[in]: The non-NULL coordinates on which to access data.
	 * Coordinates are checked to be valid in aml_layout_deref().
	 * @return A pointer to the dereferenced element on success.
	 * @return NULL on failure with aml_errno set to the error reason.
	 **/
	void *(*deref)(const struct aml_layout_data *data,
		       const size_t *coords);

	/**
	 * Function for derefencing elements of a layout inside the library.
	 * Layout assumes data is always stored in AML_LAYOUT_ORDER_FORTRAN
	 * order. Coordinates provided by the library will match the same
	 * order, i.e last dimension first.
	 * @param data[in]: The non-NULL handle to layout internal data.
	 * @param coords[in]: The non-NULL coordinates on which to access data.
	 * The first coordinate should be the last dimensions and so on to the
	 * last, coordinate, last dimension.
	 * @return A pointer to the dereferenced element on success.
	 * @return NULL on failure with aml_errno set to the error reason.
	 **/
	void *(*deref_native)(const struct aml_layout_data *data,
			      const size_t *coords);

	/**
	 * Get the order in which dimensions of the layout are
	 * supposed to be accessed by the user.
	 * @param data[in]: The non-NULL handle to layout internal data.
	 * @return Order value. It is a bitmask with order bit set (or not set).
	 * Output value can be further checked against order AML_LAYOUT_ORDER
	 * flags by using the macro AML_LAYOUT_ORDER() on output value.
	 * @see AML_LAYOUT_ORDER()
	 **/
	int (*order)(const struct aml_layout_data *data);

	/**
	 * Return the layout dimensions in the user order.
	 * @param data[in]: The non-NULL handle to layout internal data.
	 * @param dims[out]: The non-NULL array of dimensions to fill. It is
	 * supposed to be large enough to contain ndims() elements.
	 * @return AML_SUCCESS on success, else an AML error code.
	 **/
	int (*dims)(const struct aml_layout_data *data, size_t *dims);

	/**
	 * Return the layout dimensions in the order they are actually stored
	 * in the library.
	 * @param data[in]: The non-NULL handle to layout internal data.
	 * @param dims[out]: The non-NULL array of dimensions to fill. It is
	 * supposed to be large enough to contain ndims() elements.
	 * @return AML_SUCCESS on success, else an AML error code.
	 **/
	int (*dims_native)(const struct aml_layout_data *data,
			   size_t *dims);

	/**
	 * Return the number of dimensions in a layout.
	 * @param data[in]: The non-NULL handle to layout internal data.
	 * @return The number of dimensions in the layout.
	 **/
	size_t (*ndims)(const struct aml_layout_data *data);

	/**
	 * Return the size of layout elements.
	 * @param data[in]: The non-NULL handle to layout internal data.
	 * @return The size of elements stored with this layout.
	 **/
	size_t (*element_size)(const struct aml_layout_data *data);

	/**
	 * Reshape the layout with different dimensions.
	 * Layout dimensions are checked in aml_layout_reshape() to store
	 * the exact same number of elements.
	 * @param data[in]: The non-NULL handle to layout internal data.
	 * @param output[out]: A non NULL pointer to a layout where to allocate
	 * a new layout resulting from the reshape operation.
	 * @param ndims[in]: The number of dimensions of the new layout.
	 * @param dims[in]: The number of elements along each dimension of
	 * the new layout.
	 * @return AML_SUCCESS on success, else an AML error code (<0).
	 **/
	int (*reshape)(const struct aml_layout_data *data,
		       struct aml_layout **output,
		       const size_t ndims,
		       const size_t *dims);

	/**
	 * Return a layout that is a subset of another layout.
	 * Slice arguments compatibility with the original layout are
	 * checked in aml_layout_slice().
	 * @param data[in]: The non-NULL handle to layout internal data.
	 * @param output[out]: A non NULL pointer to a layout where to allocate
	 * a new layout resulting from the slice operation.
	 * @param dims[in]: The number of elements of the slice along each
	 * dimension .
	 * @param offsets[in]: The index of the first element of the slice
	 * in each dimension.
	 * @param strides[in]: The displacement (in number of elements) between
	 * elements of the slice.
	 * @return A newly allocated layout with the queried subset of the
	 * original layout on succes.
	 * @return NULL on error with aml_errno set to the failure reason.
	 **/
	int (*slice)(const struct aml_layout_data *data,
		     struct aml_layout **output,
		     const size_t *dims,
		     const size_t *offsets,
		     const size_t *strides);

	/**
	 * Return a layout that is a subset of another layout, assuming
	 * dimensions are stored with AML_LAYOUT_ORDER_FORTRAN.
	 * Slice arguments compatibility with the original layout are
	 * checked in aml_layout_slice().
	 * @param data[in]: The non-NULL handle to layout internal data.
	 * @param output[out]: A non NULL pointer to a layout where to allocate
	 * a new layout resulting from the slice operation.
	 * @param dims[in]: The number of elements of the slice along each
	 * dimension .
	 * @param offsets[in]: The index of the first element of the slice
	 * in each dimension.
	 * @param strides[in]: The displacement (in number of elements) between
	 * elements of the slice.
	 * @return A newly allocated layout with the queried subset of the
	 * original layout on succes.
	 * @return NULL on error with aml_errno set to the failure reason.
	 **/
	int (*slice_native)(const struct aml_layout_data *data,
			    struct aml_layout **output,
			    const size_t *dims,
			    const size_t *offsets,
			    const size_t *strides);
};

/**
 * Tag specifying user storage of dimensions inside a layout.
 * Layout order is the first bit in an integer bitmask.
 * @see AML_LAYOUT_ORDER()
 * This tag will store dimensions in the order provided by the user,
 * i.e elements of the last dimension will be contiguous in memory.
 **/
#define AML_LAYOUT_ORDER_C (0<<0)

/**
 * Tag specifying user storage of dimensions inside a layout.
 * Layout order is the first bit in an integer bitmask.
 * @see AML_LAYOUT_ORDER()
 * This tag will store dimensions in the reversed order provided
 * by the user, i.e elements of the first dimension will be contiguous
 * in memory. This storage is the actual storage used by the library
 * inside the structure.
 **/
#define AML_LAYOUT_ORDER_FORTRAN (1<<0)

/**
 * This is equivalent to AML_LAYOUT_ORDER_C.
 * @see AML_LAYOUT_ORDER_C
 **/
#define AML_LAYOUT_ORDER_COLUMN_MAJOR (0<<0)

/**
 * This is equivalent to AML_LAYOUT_ORDER_FORTRAN.
 * @see AML_LAYOUT_ORDER_FORTRAN
 **/
#define AML_LAYOUT_ORDER_ROW_MAJOR (1<<0)

/**
 * Get the order bit of an integer bitmask.
 * The value can be further checked for equality
 * with AML_LAYOUT_ORDER_* values.
 * @param x: An integer with the first bit set
 * to the order value.
 * @return An integer containing only the bit order.
 **/
#define AML_LAYOUT_ORDER(x) ((x) & (1<<0))

/**
 * Dereference an element of a layout by its coordinates.
 * @param layout[in]: An initialized layout.
 * @param coords[in]: The coordinates on which to access data.
 * @return A pointer to the dereferenced element on success.
 * @return NULL on failure with aml_errno set to the error reason:
 * * AML_EINVAL if coordinate are out of bound
 * * See specific implementation of layout for further information
 * on possible error codes.
 **/
void *aml_layout_deref(const struct aml_layout *layout,
		       const size_t *coords);

/**
 * Equivalent to aml_layout_deref() but with bound checking
 * on coordinates.
 * @see aml_layout_deref()
 **/
void *aml_layout_deref_safe(const struct aml_layout *layout,
			    const size_t *coords);

/**
 * Get the order in which dimensions of the layout are supposed to be
 * accessed by the user.
 * @param layout[in]: An initialized layout.
 * @return The order (>0) on success, an AML error (<0) on failure.
 * @return On success, a bitmask with order bit set (or not set).
 * Output value can be further checked against order AML_LAYOUT_ORDER
 * flags by using the macro AML_LAYOUT_ORDER() on output value.
 * @see AML_LAYOUT_ORDER()
 **/
int aml_layout_order(const struct aml_layout *layout);

/**
 * Return the layout dimensions in the user order.
 * @param layout[in]: An initialized layout.
 * @param dims[out]: The non-NULL array of dimensions to fill. It is
 * supposed to be large enough to contain ndims() elements.
 * @return AML_SUCCESS on success, else an AML error code.
 **/
int aml_layout_dims(const struct aml_layout *layout, size_t *dims);

/**
 * Return the number of dimensions in a layout.
 * @param layout[in]: An initialized layout.
 * @return The number of dimensions in the layout.
 **/
size_t aml_layout_ndims(const struct aml_layout *layout);

/**
 * @brief Return the size of layout elements.
 * @param layout[in]: An initialized layout.
 * @return The size of elements stored with this layout.
 **/
size_t aml_layout_element_size(const struct aml_layout *layout);

/**
 * @brief Reshape the layout with different dimensions.
 * This function checks that the number of elements of
 * the reshaped layout matches the number of elements
 * in the original layout. Additional constraint may apply
 * depending on the layout implementation.
 * @param layout[in]: An initialized layout.
 * @param reshaped_layout[out]: A newly allocated layout
 * with the queried shape on succes.
 * @param ndims[in]: The number of dimensions of the new layout.
 * @param dims[in]: The number of elements along each dimension of
 * the new layout.
 * @return AML_SUCCESS on success.
 * @return AML_EINVAL if reshape dimensions are not compatible
 * with original layout dimensions.
 * @return AML_ENOMEM if AML failed to allocate the new structure.
 * @return Another aml_error code. Refer to the layout
 * implementation of reshape function.
 **/
int aml_layout_reshape(const struct aml_layout *layout,
		       struct aml_layout **reshaped_layout,
		       const size_t ndims,
		       const size_t *dims);

/**
 * Return a layout that is a subset of another layout.
 * The number of elements to subset along each dimension
 * must be compatible with offsets and strides.
 * This function checks that the amount of elements along
 * each dimensions of the slice actually fits in the original
 * layout.
 * @param layout[in]: An initialized layout.
 * @param reshaped_layout[out]: a pointer where to store a
 * newly allocated layout with the queried subset of the
 * original layout on succes.
 * @param dims[in]: The number of elements of the slice along each
 * dimension .
 * @param offsets[in]: The index of the first element of the slice
 * in each dimension. If NULL, offset is set to 0.
 * @param strides[in]: The displacement (in number of elements) between
 * elements of the slice. If NULL, stride is set to 1.
 * @return AML_SUCCESS on success, else an AML error code (<0).
 **/
int aml_layout_slice(const struct aml_layout *layout,
		     struct aml_layout **reshaped_layout,
		     const size_t *dims,
		     const size_t *offsets,
		     const size_t *strides);

////////////////////////////////////////////////////////////////////////////////

558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707
/**
 * @}
 * @defgroup aml_tiling "AML Tiling"
 * @brief Tiling Data Structure High-Level API
 *
 * Tiling is a representation of the decomposition of data structures. It
 * identifies ways a layout can be split into layouts of smaller size. As such,
 * the main function of a tiling is to provide an index into subcomponents of a
 * layout. Implementations focus on the ability to provide sublayouts of
 * different sizes at the corners, and linearization of the index range.
 * @{
 **/

////////////////////////////////////////////////////////////////////////////////

/**
 * Tag specifying user storage of dimensions inside a layout.
 * Layout order is the first bit in an integer bitmask.
 * @see AML_TILING_ORDER()
 * This tag will store dimensions in the order provided by the user,
 * i.e elements of the last dimension will be contiguous in memory.
 **/
#define AML_TILING_ORDER_C (0<<0)

/**
 * Tag specifying user storage of dimensions inside a layout.
 * Layout order is the first bit in an integer bitmask.
 * @see AML_TILING_ORDER()
 * This tag will store dimensions in the reversed order provided
 * by the user, i.e elements of the first dimension will be contiguous
 * in memory. This storage is the actual storage used by the library
 * inside the structure.
 **/
#define AML_TILING_ORDER_FORTRAN (1<<0)

/**
 * This is equivalent to AML_TILING_ORDER_C.
 * @see AML_TILING_ORDER_C
 **/
#define AML_TILING_ORDER_COLUMN_MAJOR (0<<0)

/**
 * This is equivalent to AML_TILING_ORDER_FORTRAN.
 * @see AML_TILING_ORDER_FORTRAN
 **/
#define AML_TILING_ORDER_ROW_MAJOR (1<<0)

/**
 * Get the order bit of an integer bitmask.
 * The value can be further checked for equality
 * with AML_TILING_ORDER_* values.
 * @param x: An integer with the first bit set
 * to the order value.
 * @return An integer containing only the bit order.
 **/
#define AML_TILING_ORDER(x) ((x) & (1<<0))


/**
 * aml_tiling_data is an opaque handle defined by each aml_tiling
 * implementation. This not supposed to be used by end users.
 **/
struct aml_tiling_data;

/**
 * aml_tiling_ops is a structure containing a set of operation
 * over a tiling. These operations focus on:
 * - retrieving a tile
 * - getting information about the size of tiles and the tiling itself.
 **/
struct aml_tiling_ops {
	/** retrieve a tile as a layout **/
	struct aml_layout* (*index)(const struct aml_tiling_data *t,
				    const size_t *coords);
	/** retrieve a tile from linear index **/
	struct aml_layout* (*index_linear)(const struct aml_tiling_data *t,
					   const size_t uuid);
	int (*order)(const struct aml_tiling_data *t);
	int (*tile_dims)(const struct aml_tiling_data *t, size_t *dims);
	int (*dims)(const struct aml_tiling_data *t, size_t *dims);
	size_t (*ndims)(const struct aml_tiling_data *t);
	size_t (*ntiles)(const struct aml_tiling_data *t);
};

/**
 **/
struct aml_tiling {
	/** @see aml_tiling_ops **/
	struct aml_tiling_ops *ops;
	/** @see aml_tiling_data **/
	struct aml_tiling_data *data;
};

/**
 * Get the order in which dimensions of the tiling are supposed to be
 * accessed by the user.
 * @param tiling[in]: An initialized tiling.
 * @return The order (>0) on success, an AML error (<0) on failure.
 * @return On success, a bitmask with order bit set (or not set).
 * Output value can be further checked against order AML_tiling_ORDER
 * flags by using the macro AML_tiling_ORDER() on output value.
 * @see AML_tiling_ORDER()
 **/
int aml_tiling_order(const struct aml_tiling *tiling);

/**
 * Return the tiling dimensions in the user order.
 * @param tiling[in]: An initialized tiling.
 * @param dims[out]: The non-NULL array of dimensions to fill. It is
 * supposed to be large enough to contain ndims() elements.
 * @return AML_SUCCESS on success, else an AML error code.
 **/
int aml_tiling_dims(const struct aml_tiling *tiling, size_t *dims);

/**
 * Return the dimensions of a tile in the tiling, in the user order.
 * @param tiling[in]: An initialized tiling.
 * @param dims[out]: The non-NULL array of dimensions to fill. It is
 * supposed to be large enough to contain ndims() elements.
 * @return AML_SUCCESS on success, else an AML error code.
 **/
int aml_tiling_tile_dims(const struct aml_tiling *tiling, size_t *dims);

/**
 * Provide the number of dimensions in a tiling.
 * @param tiling: an initialized tiling structure.
 * @return the number of dimensions in this tiling
 **/
size_t aml_tiling_ndims(const struct aml_tiling *tiling);

/**
 * Return the tile at specified coordinates in the tiling
 * @param tiling: an initialized tiling
 * @param coords: the coordinates for the tile
 * @return the tile as a layout on success, NULL on error.
 **/
struct aml_layout *aml_tiling_index(const struct aml_tiling *t,
				    const size_t *coords);
/**
 * Return the tile at specified coordinates in the tiling, using a linear index
 * @param tiling: an initialized tiling
 * @param uuid: the 1D coordinate for the tile
 * @return the tile as a layout on success, NULL on error.
 **/
struct aml_layout *aml_tiling_index_linear(const struct aml_tiling *t,
					   const size_t uuid);

size_t aml_tiling_nbtiles(const struct aml_tiling *t);
////////////////////////////////////////////////////////////////////////////////

708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753
/**
 * @}
 * @defgroup aml_dma "AML DMA"
 * @brief Management of low-level memory movements.
 *
 * AML DMA (inspired by Direct Memory Access engines) is an abstraction over the
 * ability to move data between places. A DMAs presents an interface that allows
 * clients to create an asynchronous request to move data and to wait for this
 * request to complete. Depending on the exact operation it is configured to do,
 * the DMA might transform the data during the operation.
 *
 * Implementations are mostly responsible for providing access to various types
 * of execution engine for data movement itself.
 *
 * @image html dma.png width=600
 * @{
 **/

////////////////////////////////////////////////////////////////////////////////

/**
 * Internal macros used for tracking DMA request types.
 * Invalid request type.  Used for marking inactive requests in the vector.
 **/
#define AML_DMA_REQUEST_TYPE_INVALID -1

/**
 * The request is in the format (dest layout, src layout)
 **/
#define AML_DMA_REQUEST_TYPE_LAYOUT 0

/**
 * aml_dma is mainly used to asynchronously move data.
 * aml_dma_request is an opaque structure containing information
 * about ongoing request for data movement in a dma operation.
 * @see aml_dma_ops
 * @see aml_dma_async_copy()
 **/
struct aml_dma_request;

/**
 * Opaque handle implemented by each aml_dma implementations.
 * Should not be used by end-users.
 **/
struct aml_dma_data;

754 755 756 757
/**
 * Type of the function used to perform the DMA between two layouts.
 * @param dst: destination layout
 * @param src: source layout
758
 * @param arg: extra argument needed by the operator
759 760
 **/
typedef int (*aml_dma_operator)(struct aml_layout *dst,
761
				const struct aml_layout *src, void *arg);
762

763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781
/**
   aml_dma_ops is a structure containing operations for a specific
   * aml_dma implementation.
   * These operation are operation are detailed in the structure.
   * They are specific in:
   * - the type of aml_area source and destination,
   * - the progress engine performing the operation,
   * - the type of of source and destination data structures.
   *
   * Each different combination of these three points may require a different
   * set of dma operations.
   **/
struct aml_dma_ops {
	/**
	 * Initiate a data movement, from a source pointer to a destination
	 * pointer, and output a request handler for managing the transfer.
	 * @param dma: dma_implementation internal data.
	 * @param req[out]: the request handle to manage termination
	 *        of the movement.
782 783
	 * @param dest: layout describing the destination.
	 * @param src: layout describing the source.
784 785 786 787
	 * @return an AML error code.
	 **/
	int (*create_request)(struct aml_dma_data *dma,
			      struct aml_dma_request **req,
788
			      struct aml_layout *dest,
789
			      struct aml_layout *src,
790
			      aml_dma_operator op, void *op_arg);
791 792 793 794 795 796 797 798 799 800

	/**
	 * Destroy the request handle. If the data movement is still ongoing,
	 * then cancel it.
	 *
	 * @param dma: dma_implementation internal data.
	 * @param req: the request handle to manage termination of the movement.
	 * @return an AML error code.
	 **/
	int (*destroy_request)(struct aml_dma_data *dma,
Swann Perarnau's avatar
Swann Perarnau committed
801
			       struct aml_dma_request **req);
802 803 804 805 806 807 808 809 810 811

	/**
	 * Wait for termination of a data movement and destroy the request
	 * handle.
	 *
	 * @param dma: dma_implementation internal data.
	 * @param req: the request handle to manage termination of the movement.
	 * @return an AML error code.
	 **/
	int (*wait_request)(struct aml_dma_data *dma,
Swann Perarnau's avatar
Swann Perarnau committed
812
			    struct aml_dma_request **req);
813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831
};

/**
 * aml_dma is an abstraction for (asynchronously) moving data
 * from one area to another. The implementation of dma to use
 * is depends on the source and destination areas. The appropriate
 * dma choice is delegated to the user.
 * @see struct aml_area.
 **/
struct aml_dma {
	/** @see aml_dma_ops **/
	struct aml_dma_ops *ops;
	/** @see aml_dma_data **/
	struct aml_dma_data *data;
};

/**
 * Requests a synchronous data copy between two different buffers.
 * @param dma: an initialized DMA structure.
832 833
 * @param dest: layout describing the destination.
 * @param src: layout describing the source.
834
 * @param op: optional custom operator for this dma
835 836
 * @return 0 if successful; an error code otherwise.
 **/
837
int aml_dma_copy_custom(struct aml_dma *dma, struct aml_layout *dest,
838
		 struct aml_layout *src, aml_dma_operator op, void *op_arg);
839 840 841 842 843 844 845

/**
 * Requests a data copy between two different buffers.This is an asynchronous
 * version of aml_dma_copy().
 * @param dma: an initialized DMA structure.
 * @param req: an address where the pointer to the newly assigned DMA request
 *        will be stored.
846 847
 * @param dest: layout describing the destination.
 * @param src: layout describing the source.
848
 * @param op: optional custom operator for this dma
849 850
 * @return 0 if successful; an error code otherwise.
 **/
851
int aml_dma_async_copy_custom(struct aml_dma *dma, struct aml_dma_request **req,
852
		       struct aml_layout *dest,
853
		       struct aml_layout *src,
854
		       aml_dma_operator op, void *op_arg);
855

856
#define aml_dma_copy(dma, d, s) aml_dma_copy_custom(dma, d, s, NULL, NULL)
857
#define aml_dma_async_copy(dma, r, d, s) \
858
	aml_dma_async_copy_custom(dma, r, d, s, NULL, NULL)
859 860 861 862 863 864 865

/**
 * Waits for an asynchronous DMA request to complete.
 * @param dma: an initialized DMA structure.
 * @param req: a DMA request obtained using aml_dma_async_*() calls.
 * @return 0 if successful; an error code otherwise.
 **/
Swann Perarnau's avatar
Swann Perarnau committed
866
int aml_dma_wait(struct aml_dma *dma, struct aml_dma_request **req);
867 868 869 870 871 872 873

/**
 * Tears down an asynchronous DMA request before it completes.
 * @param dma: an initialized DMA structure.
 * @param req: a DMA request obtained using aml_dma_async_*() calls.
 * @return 0 if successful; an error code otherwise.
 **/
Swann Perarnau's avatar
Swann Perarnau committed
874
int aml_dma_cancel(struct aml_dma *dma, struct aml_dma_request **req);
875 876 877 878 879

/**
 * Generic helper to copy from one layout to another.
 * @param dst[out]: destination layout
 * @param src[in]: source layout
880
 * @param arg: unused
881 882
 */
int aml_copy_layout_generic(struct aml_layout *dst,
883
			    const struct aml_layout *src, void *arg);
884 885 886 887


////////////////////////////////////////////////////////////////////////////////

888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912
/**
 * @}
 * @defgroup aml_scratch "AML Scratchpad"
 * @brief Stage-in, Stage-out High Level Abstraction.
 *
 * Scratchpad in an abstraction fro moving data back and forth from
 * a data representation in an area to another data representation in another
 * areas. This is especially usefull from moving to user data representation
 * to an architecure optimized representation for heavy computational work,
 * then returning the to user representation.
 * Data movement is performed with two dma engines from one area and tiling to
 * another area and tiling.
 *
 * @image html scratch.png width=600
 * @see aml_dma
 * @{
 **/

////////////////////////////////////////////////////////////////////////////////

/**
 * Scratch is mainly used to asynchronously move data back and forth between
 * two areas. aml_scratch_request is an opaque structure containing information
 * about ongoing requests for data movement.
 **/
913
struct aml_scratch_request;
914 915 916 917 918

/**
 * Opaque handle implemented by each scratches implementation.
 * Should not be used by end users.
 **/
919 920
struct aml_scratch_data;

921 922 923 924
/**
 * Scratchpad request types.
 * Invalid request type.  Used for marking inactive requests in the vector.
 **/
925
#define AML_SCRATCH_REQUEST_TYPE_INVALID -1
926 927 928 929 930

/**
 * Scratchpad request types.
 * Push from the scratchpad to regular memory.
 **/
931
#define AML_SCRATCH_REQUEST_TYPE_PUSH 0
932 933 934 935 936

/**
 * Scratchpad request types.
 * Pull from regular memory to the scratchpad.
 **/
937
#define AML_SCRATCH_REQUEST_TYPE_PULL 1
938 939 940 941 942

/**
 * Scratchpad request types.
 * No-op/empty request
 **/
943
#define AML_SCRATCH_REQUEST_TYPE_NOOP 2
944

945 946 947 948 949 950 951 952 953
/**
 * aml_scratch_ops contain a scratch implementation specific operations.
 * These operations implementation may vary depending on the source and
 * destination of data, and thus scratch implementations use different
 * operations.
 * Aware users may create or modify implementation by assembling
 * appropriate operations in such a structure.
 * @see struct aml_scratch
 **/
954
struct aml_scratch_ops {
955 956 957
	/**
	 * \todo Doc
	 **/
958 959
	int (*create_request)(struct aml_scratch_data *scratch,
			      struct aml_scratch_request **req, int type,
960 961
			      struct aml_layout **dest, int *destid,
			      struct aml_layout *src, int srcid);
962 963 964
	/**
	 * \todo Doc
	 **/
965 966
	int (*destroy_request)(struct aml_scratch_data *scratch,
			       struct aml_scratch_request *req);
967 968 969
	/**
	 * \todo Doc
	 **/
970 971
	int (*wait_request)(struct aml_scratch_data *scratch,
			    struct aml_scratch_request *req);
972 973 974
	/**
	 * \todo Doc
	 **/
975
	int (*release)(struct aml_scratch_data *scratch, int scratchid);
976 977
};

978 979 980 981 982 983
/**
 * An aml_scratch is abstraction aimed toward temporary use of a data structures
 * in a different area than the one where data currently resides. Scratches in
 * AML take care of asynchornously allocating and moving the data back and forth
 * between areas.
 **/
984
struct aml_scratch {
985
	/** @see aml_scratch_ops **/
986
	struct aml_scratch_ops *ops;
987
	/** @see aml_scratch_data **/
988 989 990
	struct aml_scratch_data *data;
};

991
/**
992
 * Requests a synchronous pull from regular memory to the scratchpad.
993 994 995 996 997
 * @param scratch: an initialized scratchpad structure.
 * @param scratchptr: an argument of type void*; the scratchpad base pointer.
 * @param scratchid: an argument of type int*; gets filled with the scratch tile
 *        identifier where the data will be pulled into.
 * @param srcptr: an argument of type void*; the start address of the complete
998
 *             source user data structure.
999 1000 1001 1002
 * @param srcid: an argument of type int; the source tile identifier.
 * @see aml_scratch_baseptr()
 * @return 0 if successful; an error code otherwise.
 **/
1003 1004 1005
int aml_scratch_pull(struct aml_scratch *scratch,
		     struct aml_layout **dest, int *scratchid,
		     struct aml_layout *src, int srcid);
1006 1007 1008

/**
 * Requests a pull from regular memory to the scratchpad. This is an
1009
 * asynchronous version of aml_scratch_pull().
1010 1011 1012 1013 1014 1015 1016
 * @param scratch: an initialized scratchpad structure.
 * @param req: an address where the pointer to the newly assigned scratch
 *        request will be stored.
 * @param variadic arguments: see aml_scratch_pull().
 * @return 0 if successful; an error code otherwise.
 * @see aml_scratch_pull()
 **/
1017
int aml_scratch_async_pull(struct aml_scratch *scratch,
1018 1019 1020
			   struct aml_scratch_request **req,
			   struct aml_layout **scratch_layout, int *scratchid,
			   struct aml_layout *src_layout, int srcid);
1021
/**
1022
 * Requests a synchronous push from the scratchpad to regular memory.
1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033
 * @param scratch: an initialized scratchpad structure.
 * @param dstptr: an argument of type void*; the start address of the complete
 *        destination user data structure.
 * @param dstid: an argument of type int*; gets filled with the destination tile
 *        identifier where the data will be pushed into (and where it was
 *        pulled from in the first place).
 * @param scratchptr: an argument of type void*; the scratchpad base pointer.
 * @param scratchid: an argument of type int; the scratchpad tile identifier.
 * @return 0 if successful; an error code otherwise.
 * @see aml_scratch_baseptr()
 **/
1034 1035 1036
int aml_scratch_push(struct aml_scratch *scratch,
		     struct aml_layout **dest_layout, int *destid,
		     struct aml_layout *scratch_layout, int scratchid);
1037 1038

/**
1039 1040
 * Requests a push from the scratchpad to regular memory.  This is an
 * asynchronous version of aml_scratch_push().
1041 1042 1043
 * @param scratch: an initialized scratchpad structure.
 * @param req: an address where the pointer to the newly assigned scratch
 *        request will be stored.
1044
 * Variadic arguments: see aml_scratch_push().
1045 1046 1047
 * @return 0 if successful; an error code otherwise.
 * @see aml_scratch_push()
 **/
1048
int aml_scratch_async_push(struct aml_scratch *scratch,
1049 1050 1051
			   struct aml_scratch_request **req,
			   struct aml_layout **dest_layout, int *destid,
			   struct aml_layout *scratch_layout, int scratchid);
1052
/**
1053
 * Waits for an asynchronous scratch request to complete.
1054 1055 1056 1057
 * @param scratch: an initialized scratchpad structure.
 * @param req: a scratch request obtained using aml_scratch_async_*() calls.
 * @return 0 if successful; an error code otherwise.
 **/
1058 1059
int aml_scratch_wait(struct aml_scratch *scratch,
		     struct aml_scratch_request *req);
1060

1061
/**
1062
 * Tears down an asynchronous scratch request before it completes.
1063 1064 1065 1066
 * @param scratch: an initialized scratchpad structure.
 * @param req: a scratch request obtained using aml_scratch_async_*() calls.
 * @return 0 if successful; an error code otherwise.
 **/
1067 1068
int aml_scratch_cancel(struct aml_scratch *scratch,
		       struct aml_scratch_request *req);
1069
/**
1070
 * Provides the location of the scratchpad.
1071 1072 1073
 * @param scratch: an initialized scratchpad structure.
 * @return a base pointer to the scratchpad memory buffer.
 **/
1074
void *aml_scratch_baseptr(const struct aml_scratch *scratch);
1075

1076
/**
1077
 * Release a scratch tile for immediate reuse.
1078 1079 1080 1081
 * @param scratch: an initialized scratchpad structure.
 * @param scratchid: a scratchpad tile identifier.
 * @return 0 if successful; an error code otherwise.
 **/
1082 1083
int aml_scratch_release(struct aml_scratch *scratch, int scratchid);

1084 1085 1086 1087 1088 1089
////////////////////////////////////////////////////////////////////////////////

/**
 * @}
 **/

1090
#endif