1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
|
/*
* Copyright (c) 2020 Intel Corporation. All rights reserved.
* Copyright (c) 2021 Amazon.com, Inc. or its affiliates.
*
* This software is available to you under the BSD license below:
*
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the following
* conditions are met:
*
* - Redistributions of source code must retain the above
* copyright notice, this list of conditions and the following
* disclaimer.
*
* - Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
*/
#if HAVE_CONFIG_H
#include <config.h>
#endif
#include <inttypes.h>
#include <stdbool.h>
#include "hmem.h"
static bool hmem_initialized = false;
struct ft_hmem_ops {
int (*init)(void);
int (*cleanup)(void);
int (*alloc)(uint64_t device, void **buf, size_t size);
int (*alloc_host)(void **buf, size_t size);
int (*free)(void *buf);
int (*free_host)(void *buf);
int (*mem_set)(uint64_t device, void *buf, int value, size_t size);
int (*copy_to_hmem)(uint64_t device, void *dst, const void *src,
size_t size);
int (*copy_from_hmem)(uint64_t device, void *dst, const void *src,
size_t size);
int (*get_dmabuf_fd)(void *buf, size_t len,
int *fd, uint64_t *offset);
};
static struct ft_hmem_ops hmem_ops[] = {
[FI_HMEM_SYSTEM] = {
.init = ft_host_init,
.cleanup = ft_host_cleanup,
.alloc = ft_host_alloc,
.alloc_host = ft_default_alloc_host,
.free = ft_host_free,
.free_host = ft_default_free_host,
.mem_set = ft_host_memset,
.copy_to_hmem = ft_host_memcpy,
.copy_from_hmem = ft_host_memcpy,
.get_dmabuf_fd = ft_hmem_no_get_dmabuf_fd,
},
[FI_HMEM_CUDA] = {
.init = ft_cuda_init,
.cleanup = ft_cuda_cleanup,
.alloc = ft_cuda_alloc,
.alloc_host = ft_cuda_alloc_host,
.free = ft_cuda_free,
.free_host = ft_cuda_free_host,
.mem_set = ft_cuda_memset,
.copy_to_hmem = ft_cuda_copy_to_hmem,
.copy_from_hmem = ft_cuda_copy_from_hmem,
.get_dmabuf_fd = ft_cuda_get_dmabuf_fd,
},
[FI_HMEM_ROCR] = {
.init = ft_rocr_init,
.cleanup = ft_rocr_cleanup,
.alloc = ft_rocr_alloc,
.alloc_host = ft_default_alloc_host,
.free = ft_rocr_free,
.free_host = ft_default_free_host,
.mem_set = ft_rocr_memset,
.copy_to_hmem = ft_rocr_memcpy,
.copy_from_hmem = ft_rocr_memcpy,
.get_dmabuf_fd = ft_hmem_no_get_dmabuf_fd,
},
[FI_HMEM_ZE] = {
.init = ft_ze_init,
.cleanup = ft_ze_cleanup,
.alloc = ft_ze_alloc,
.alloc_host = ft_ze_alloc_host,
.free = ft_ze_free,
.free_host = ft_ze_free,
.mem_set = ft_ze_memset,
.copy_to_hmem = ft_ze_copy,
.copy_from_hmem = ft_ze_copy,
.get_dmabuf_fd = ft_hmem_no_get_dmabuf_fd,
},
[FI_HMEM_NEURON] = {
.init = ft_neuron_init,
.cleanup = ft_neuron_cleanup,
.alloc = ft_neuron_alloc,
.alloc_host = ft_default_alloc_host,
.free = ft_neuron_free,
.free_host = ft_default_free_host,
.mem_set = ft_neuron_memset,
.copy_to_hmem = ft_neuron_memcpy_to_hmem,
.copy_from_hmem = ft_neuron_memcpy_from_hmem,
.get_dmabuf_fd = ft_hmem_no_get_dmabuf_fd,
},
};
int ft_hmem_init(enum fi_hmem_iface iface)
{
int ret;
ret = hmem_ops[iface].init();
if (ret == FI_SUCCESS)
hmem_initialized = true;
return ret;
}
int ft_hmem_cleanup(enum fi_hmem_iface iface)
{
int ret = FI_SUCCESS;
if (hmem_initialized) {
ret = hmem_ops[iface].cleanup();
if (ret == FI_SUCCESS)
hmem_initialized = false;
}
return ret;
}
int ft_hmem_alloc(enum fi_hmem_iface iface, uint64_t device, void **buf,
size_t size)
{
return hmem_ops[iface].alloc(device, buf, size);
}
int ft_default_alloc_host(void **buf, size_t size)
{
*buf = malloc(size);
return (*buf == NULL) ? -FI_ENOMEM : 0;
}
int ft_default_free_host(void *buf)
{
free(buf);
return 0;
}
int ft_hmem_alloc_host(enum fi_hmem_iface iface, void **buf,
size_t size)
{
return hmem_ops[iface].alloc_host(buf, size);
}
int ft_hmem_free(enum fi_hmem_iface iface, void *buf)
{
return hmem_ops[iface].free(buf);
}
int ft_hmem_free_host(enum fi_hmem_iface iface, void *buf)
{
return hmem_ops[iface].free_host(buf);
}
int ft_hmem_memset(enum fi_hmem_iface iface, uint64_t device, void *buf,
int value, size_t size)
{
return hmem_ops[iface].mem_set(device, buf, value, size);
}
int ft_hmem_copy_to(enum fi_hmem_iface iface, uint64_t device, void *dst,
const void *src, size_t size)
{
return hmem_ops[iface].copy_to_hmem(device, dst, src, size);
}
int ft_hmem_copy_from(enum fi_hmem_iface iface, uint64_t device, void *dst,
const void *src, size_t size)
{
return hmem_ops[iface].copy_from_hmem(device, dst, src, size);
}
int ft_hmem_get_dmabuf_fd(enum fi_hmem_iface iface,
void *buf, size_t len,
int *fd, uint64_t *offset)
{
return hmem_ops[iface].get_dmabuf_fd(buf, len, fd, offset);
}
int ft_hmem_no_get_dmabuf_fd(void *buf, size_t len,
int *fd, uint64_t *offset)
{
return -FI_ENOSYS;
}
|