#include "cs_defs.h"
#include <assert.h>
#include <string.h>
#include <map>
#include "bft_error.h"
#include "bft_mem.h"
#include "cs_base_accel.h"
Functions | |
int | cs_get_device_id (void) |
Return currently associated device id. More... | |
void * | cs_malloc_hd (cs_alloc_mode_t mode, size_t ni, size_t size, const char *var_name, const char *file_name, int line_num) |
Allocate memory on host and device for ni elements of size bytes. More... | |
void * | cs_realloc_hd (void *ptr, cs_alloc_mode_t mode, size_t ni, size_t size, const char *var_name, const char *file_name, int line_num) |
Reallocate memory on host and device for ni elements of size bytes. More... | |
void | cs_free_hd (void *ptr, const char *var_name, const char *file_name, int line_num) |
Free memory on host and device for a given host pointer. More... | |
void | cs_free (void *ptr, const char *var_name, const char *file_name, int line_num) |
Free memory on host and device for a given pointer. More... | |
void * | cs_get_device_ptr (void *ptr) |
Return matching device pointer for a given pointer. More... | |
const void * | cs_get_device_ptr_const (const void *ptr) |
Return matching device pointer for a given constant pointer. More... | |
const void * | cs_get_device_ptr_const_pf (const void *ptr) |
Return matching device pointer for a given constant pointer, prefetching if applicable. More... | |
cs_alloc_mode_t | cs_check_device_ptr (const void *ptr) |
Check if a pointer is associated with a device. More... | |
void * | cs_associate_device_ptr (void *host_ptr, size_t ni, size_t size) |
Associate device memory with a given host memory pointer. More... | |
void | cs_dissassociate_device_ptr (void *host_ptr) |
Detach device memory from a given host memory pointer. More... | |
void | cs_set_alloc_mode (void **host_ptr, cs_alloc_mode_t mode) |
Set allocation mode for an already allocated pointer. More... | |
void | cs_sync_h2d (const void *ptr) |
Synchronize data from host to device. More... | |
void | cs_sync_h2d_future (const void *ptr) |
Initiate synchronization of data from host to device for future access. More... | |
void | cs_sync_d2h (void *ptr) |
Synchronize data from device to host. More... | |
void | cs_prefetch_h2d (void *ptr, size_t size) |
Prefetch data from host to device. More... | |
void | cs_prefetch_d2h (void *ptr, size_t size) |
Prefetch data from device to host. More... | |
void | cs_copy_h2d (void *dest, const void *src, size_t size) |
Copy data from host to device. More... | |
void | cs_copy_d2h (void *dest, const void *src, size_t size) |
Copy data from device to host. More... | |
void | cs_copy_d2d (void *dest, const void *src, size_t size) |
Copy data from device to device. More... | |
int | cs_get_n_allocations_hd (void) |
Return number of host-device allocations. More... | |
size_t | cs_get_allocation_hd_size (void *host_ptr) |
Check if a given host pointer is allocated with associated with cs_alloc_hd or cs_realloc_hd. More... | |
void* cs_associate_device_ptr | ( | void * | host_ptr, |
size_t | ni, | ||
size_t | size | ||
) |
Associate device memory with a given host memory pointer.
If the host memory is already associated with the device, the existing device pointer is returned. Otherwise, a new device allocation is called and returned.
[in] | host_ptr | host pointer |
[in] | ni | number of elements |
[in] | size | element size |
cs_alloc_mode_t cs_check_device_ptr | ( | const void * | ptr | ) |
Check if a pointer is associated with a device.
If separate pointers are used on the host and device, the host pointer should be used with this function.
void cs_copy_d2d | ( | void * | dest, |
const void * | src, | ||
size_t | size | ||
) |
Copy data from device to device.
This function should be usable on subsets of arrays allocated on the host and device.
[out] | dest | pointer to destination data on host |
[in,out] | src | pointer to source data on device |
[in] | size | number of bytes to prefetch |
void cs_copy_d2h | ( | void * | dest, |
const void * | src, | ||
size_t | size | ||
) |
Copy data from device to host.
This function should be usable on subsets of arrays allocated on the host and device.
[out] | dest | pointer to destination data on host |
[in,out] | src | pointer to source data on device |
[in] | size | number of bytes to prefetch |
void cs_copy_h2d | ( | void * | dest, |
const void * | src, | ||
size_t | size | ||
) |
Copy data from host to device.
This function should be usable on subsets of arrays allocated on the host and device.
[out] | dest | pointer to destination data on device |
[in,out] | src | pointer to source data on host |
[in] | size | number of bytes to prefetch |
void cs_dissassociate_device_ptr | ( | void * | host_ptr | ) |
Detach device memory from a given host memory pointer.
If the host memory is shared with the device (i.e. using CS_ALLOC_SHARED), device memory stays shared.
[in] | host_ptr | host pointer |
void cs_free | ( | void * | ptr, |
const char * | var_name, | ||
const char * | file_name, | ||
int | line_num | ||
) |
Free memory on host and device for a given pointer.
Compared to , this function also allows freeing memory allocated through BFT_MEM_MALLOC / bft_mem_malloc.
[in] | ptr | pointer to free |
[in] | var_name | allocated variable name string |
[in] | file_name | name of calling source file |
[in] | line_num | line number in calling source file |
void cs_free_hd | ( | void * | ptr, |
const char * | var_name, | ||
const char * | file_name, | ||
int | line_num | ||
) |
Free memory on host and device for a given host pointer.
If separate pointers are used on the host and device, the host pointer should be used with this function.
[in] | ptr | pointer to free |
[in] | var_name | allocated variable name string |
[in] | file_name | name of calling source file |
[in] | line_num | line number in calling source file |
size_t cs_get_allocation_hd_size | ( | void * | host_ptr | ) |
Check if a given host pointer is allocated with associated with cs_alloc_hd or cs_realloc_hd.
int cs_get_device_id | ( | void | ) |
Return currently associated device id.
void* cs_get_device_ptr | ( | void * | ptr | ) |
Return matching device pointer for a given pointer.
If separate pointers are used on the host and device, the host pointer should be used with this function.
If memory is not allocated on device yet at the call site, it will be allocated automatically by this function.
[in] | ptr | pointer |
const void* cs_get_device_ptr_const | ( | const void * | ptr | ) |
Return matching device pointer for a given constant pointer.
If separate pointers are used on the host and device, the host pointer should be used with this function.
If memory is not allocated on device yet at the call site, it will be allocated automatically by this function.
[in] | ptr | pointer |
const void* cs_get_device_ptr_const_pf | ( | const void * | ptr | ) |
Return matching device pointer for a given constant pointer, prefetching if applicable.
If separate pointers are used on the host and device, the host pointer should be used with this function. In this case, it is assumed that the host and device values have already been synchronized, unless memory is not allocated on device yet at the call site, in which case it will be allocated automatically by this function.
[in] | ptr | pointer |
int cs_get_n_allocations_hd | ( | void | ) |
Return number of host-device allocations.
void* cs_malloc_hd | ( | cs_alloc_mode_t | mode, |
size_t | ni, | ||
size_t | size, | ||
const char * | var_name, | ||
const char * | file_name, | ||
int | line_num | ||
) |
Allocate memory on host and device for ni elements of size bytes.
This function calls the appropriate allocation function based on the requested mode, and allows introspection of the allocated memory.
If separate pointers are used on the host and device, the host pointer is returned.
[in] | mode | allocation mode |
[in] | ni | number of elements |
[in] | size | element size |
[in] | var_name | allocated variable name string |
[in] | file_name | name of calling source file |
[in] | line_num | line number in calling source file |
void cs_prefetch_d2h | ( | void * | ptr, |
size_t | size | ||
) |
Prefetch data from device to host.
This function should only be used on arrays using shared host and device memory, shuch as those allocated using CS_ALLOC_HOST_DEVICE_SHARED. It should be usable on a subset of such an array.
[in,out] | ptr | pointer to data to prefetch |
[in] | size | number of bytes to prefetch |
void cs_prefetch_h2d | ( | void * | ptr, |
size_t | size | ||
) |
Prefetch data from host to device.
This function should only be used on arrays using shared host and device memory, shuch as those allocated using CS_ALLOC_HOST_DEVICE_SHARED. It should be usable on a subset of such an array.
[in,out] | ptr | pointer to data to prefetch |
[in] | size | number of bytes to prefetch |
void* cs_realloc_hd | ( | void * | ptr, |
cs_alloc_mode_t | mode, | ||
size_t | ni, | ||
size_t | size, | ||
const char * | var_name, | ||
const char * | file_name, | ||
int | line_num | ||
) |
Reallocate memory on host and device for ni elements of size bytes.
This function calls the appropriate reallocation function based on the requested mode, and allows introspection of the allocated memory.
If separate pointers are used on the host and device, the host pointer should be used with this function.
If the allocation parameters are unchanged, no actual reallocation occurs.
[in] | ptr | pointer to previously allocated memory |
[in] | mode | allocation mode |
[in] | ni | number of elements |
[in] | size | element size |
[in] | var_name | allocated variable name string |
[in] | file_name | name of calling source file |
[in] | line_num | line number in calling source file |
void cs_set_alloc_mode | ( | void ** | host_ptr, |
cs_alloc_mode_t | mode | ||
) |
Set allocation mode for an already allocated pointer.
If the allocation mode is different from the previous one, the associated memory will be reallocated with the desired mode, and the previous allocation freed.
[in,out] | host_ptr | pointer to host pointer to modify |
[in] | mode | desired allocation mode |
void cs_sync_d2h | ( | void * | ptr | ) |
Synchronize data from device to host.
If separate allocations are used on the host and device (mode == CS_ALLOC_HOST_DEVICE), the host pointer should be passed to this function.
Depending on the allocaton type, this can imply a copy, data prefetch, or a no-op.
This function assumes the provided pointer was allocated using CS_MALLOC_HD or CS_REALLOC_HD, as it uses the associated mapping to determine associated metadata.
[in,out] | ptr | pointer to values to copy or prefetch |
void cs_sync_h2d | ( | const void * | ptr | ) |
Synchronize data from host to device.
If separate pointers are used on the host and device, the host pointer should be used with this function.
Depending on the allocation type, this can imply a copy, data prefetch, or a no-op.
This function assumes the provided pointer was allocated using CS_MALLOC_HD or CS_REALLOC_HD, as it uses the associated mapping to determine associated metadata.
[in,out] | ptr | host pointer to values to copy or prefetch |
void cs_sync_h2d_future | ( | const void * | ptr | ) |
Initiate synchronization of data from host to device for future access.
If separate pointers are used on the host and device, the host pointer should be used with this function. In this case, synchronization is done are started (asynchronously if the allocation mode supports it).
In other cases, synchronization will be delayed until actual use. the host pointer should be used with this function.
Depending on the allocation type, this can imply a copy, data prefetch, or a no-op.
This function assumes the provided pointer was allocated using CS_MALLOC_HD or CS_REALLOC_HD, as it uses the associated mapping to determine associated metadata.
[in,out] | ptr | host pointer to values to copy or prefetch |