/*
 * This file is part of the StarPU Handbook.
 * Copyright (C) 2009--2011  Universit@'e de Bordeaux
 * Copyright (C) 2010, 2011, 2012, 2013, 2015, 2016  CNRS
 * Copyright (C) 2011, 2012 INRIA
 * See the file version.doxy for copying conditions.
 */

/*! \defgroup API_Standard_Memory_Library Standard Memory Library

\def starpu_data_malloc_pinned_if_possible
\ingroup API_Standard_Memory_Library
\deprecated
Equivalent to starpu_malloc(). This macro is provided to avoid breaking old codes.

\def starpu_data_free_pinned_if_possible
\ingroup API_Standard_Memory_Library
\deprecated
Equivalent to starpu_free(). This macro is provided to avoid breaking old codes.

\def STARPU_MALLOC_PINNED
\ingroup API_Standard_Memory_Library
Value passed to the function starpu_malloc_flags() to indicate the memory allocation should be pinned.

\def STARPU_MALLOC_COUNT
\ingroup API_Standard_Memory_Library
Value passed to the function starpu_malloc_flags() to indicate
the memory allocation should be in the limit defined by the
environment variables \ref STARPU_LIMIT_CUDA_devid_MEM,
\ref STARPU_LIMIT_CUDA_MEM, \ref STARPU_LIMIT_OPENCL_devid_MEM,
\ref STARPU_LIMIT_OPENCL_MEM and \ref STARPU_LIMIT_CPU_MEM (see
Section \ref HowToLimitMemoryPerNode).
If no memory is available, it tries to reclaim memory from StarPU.
Memory allocated this way needs to be freed by calling the function
starpu_free_flags() with the same flag.

\def STARPU_MALLOC_NORECLAIM
\ingroup API_Standard_Memory_Library
Value passed to the function starpu_malloc_flags() along STARPU_MALLOC_COUNT
to indicate that while the memory allocation should be kept in the limits
defined for STARPU_MALLOC_COUNT, no reclaiming should be performed by
starpu_malloc_flags itself, thus potentially overflowing the
memory node a bit. StarPU will reclaim memory after next task termination,
according to the STARPU_MINIMUM_AVAILABLE_MEM and STARPU_TARGET_AVAILABLE_MEM
environment variables. If STARPU_MEMORY_WAIT is set, no overflowing will happen,
starpu_malloc_flags() will wait for other eviction mechanisms to release enough memory.

\def STARPU_MALLOC_SIMULATION_FOLDED
\ingroup API_Standard_Memory_Library
Value passed to the function starpu_malloc_flags() to indicate that when
StarPU is using simgrid, the allocation can be "folded", i.e. a memory area is
allocated, but its content is actually a replicate of the same memory area, to
avoid having to actually allocate that much memory . This thus allows to have a
memory area that does not actually consumes memory, to which one can read from
and write to normally, but get bogus values.

\fn int starpu_malloc_flags(void **A, size_t dim, int flags)
\ingroup API_Standard_Memory_Library
Performs a memory allocation based on the constraints defined
by the given flag.

\fn void starpu_malloc_set_align(size_t align)
\ingroup API_Standard_Memory_Library
This function sets an alignment constraints for starpu_malloc()
allocations. align must be a power of two. This is for instance called
automatically by the OpenCL driver to specify its own alignment
constraints.

\fn int starpu_malloc(void **A, size_t dim)
\ingroup API_Standard_Memory_Library
This function allocates data of the given size \p dim in main memory, and
returns the pointer to the allocated data through \p A.
It will also try to pin it in CUDA or OpenCL, so that data transfers
from this buffer can be asynchronous, and thus permit data transfer
and computation overlapping. The allocated buffer must be freed thanks
to the starpu_free() function.

\fn int starpu_free(void *A)
\ingroup API_Standard_Memory_Library
This function frees memory which has previously been allocated
with starpu_malloc().

\fn int starpu_free_flags(void *A, size_t dim, int flags)
\ingroup API_Standard_Memory_Library
This function frees memory by specifying its size. The given
flags should be consistent with the ones given to starpu_malloc_flags()
when allocating the memory.

\fn int starpu_memory_pin(void *addr, size_t size)
\ingroup API_Standard_Memory_Library
This function pins the given memory area, so that CPU-GPU transfers can be done
asynchronously with DMAs. The memory must be unpinned with
starpu_memory_unpin() before being freed. Returns 0 on success, -1 on error.

\fn int starpu_memory_unpin(void *addr, size_t size)
\ingroup API_Standard_Memory_Library
This function unpins the given memory area previously pinned with
starpu_memory_pin(). Returns 0 on success, -1 on error.

\fn ssize_t starpu_memory_get_total(unsigned node)
\ingroup API_Standard_Memory_Library
If a memory limit is defined on the given node (see Section
\ref HowToLimitMemoryPerNode), return the amount of total memory
on the node. Otherwise return -1.

\fn ssize_t starpu_memory_get_total_all_nodes()
\ingroup API_Standard_Memory_Library
return the amount of total memory on all memory nodes for whose a memory limit
is defined (see Section \ref HowToLimitMemoryPerNode).

\fn ssize_t starpu_memory_get_available(unsigned node)
\ingroup API_Standard_Memory_Library
If a memory limit is defined on the given node (see Section
\ref HowToLimitMemoryPerNode), return the amount of available memory
on the node. Otherwise return -1.

\fn ssize_t starpu_memory_get_available_all_nodes()
\ingroup API_Standard_Memory_Library
return the amount of available memory on all memory nodes for whose a memory limit
is defined (see Section \ref HowToLimitMemoryPerNode).

\fn int starpu_memory_allocate(unsigned node, size_t size, int flags)
\ingroup API_Standard_Memory_Library
If a memory limit is defined on the given node (see Section
\ref HowToLimitMemoryPerNode), try to allocate some of it. This does not actually
allocate memory, but only accounts for it. This can be useful when the
application allocates data another way, but want StarPU to be aware of the
allocation size e.g. for memory reclaiming.
By default, the function returns -ENOMEM if there is not enough room on
the given node. \p flags can be either STARPU_MEMORY_WAIT or
STARPU_MEMORY_OVERFLOW to change this.

\fn void starpu_memory_deallocate(unsigned node, size_t size)
\ingroup API_Standard_Memory_Library
If a memory limit is defined on the given node (see Section
\ref HowToLimitMemoryPerNode), free some of it. This does not actually free memory,
but only accounts for it, like starpu_memory_allocate(). The amount does not
have to be exactly the same as what was passed to starpu_memory_allocate(),
only the eventual amount needs to be the same, i.e. one call to
starpu_memory_allocate() can be followed by several calls to
starpu_memory_deallocate() to declare the deallocation piece by piece.

\fn void starpu_memory_wait_available(unsigned node, size_t size)
\ingroup API_Standard_Memory_Library
If a memory limit is defined on the given
node (see Section \ref HowToLimitMemoryPerNode), this will wait for \p size
bytes to become available on \p node. Of course, since another thread may be
allocating memory concurrently, this does not necessarily mean that this amount
will be actually available, just that it was reached. To atomically wait for
some amount of memory and reserve it, starpu_memory_allocate() should be used
with the STARPU_MEMORY_WAIT flag.

\def STARPU_MEMORY_WAIT
\ingroup API_Standard_Memory_Library
Value passed to starpu_memory_allocate() to specify that the function should
wait for the requested amount of memory to become available, and atomically
allocate it.

\def STARPU_MEMORY_OVERFLOW
\ingroup API_Standard_Memory_Library
Value passed to starpu_memory_allocate() to specify that the function should
allocate the amount of memory, even if that means overflowing the total size of
the memory node.


*/
