| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
#ifdef HAVE_CONFIG_H |
| 8 |
# include "config.h" |
| 9 |
#endif |
| 10 |
|
| 11 |
#include "ugni_device.h" |
| 12 |
#include "ugni_iface.h" |
| 13 |
#include "ugni_md.h" |
| 14 |
#include <uct/api/v2/uct_v2.h> |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
UCS_CONFIG_DEFINE_ARRAY(ugni_alloc_methods, sizeof(uct_alloc_method_t), |
| 19 |
UCS_CONFIG_TYPE_ENUM(uct_alloc_method_names)); |
| 20 |
|
| 21 |
pthread_mutex_t uct_ugni_global_lock = PTHREAD_MUTEX_INITIALIZER; |
| 22 |
|
| 23 |
|
| 24 |
static ucs_status_t |
| 25 |
uct_ugni_query_md_resources(uct_component_h component, |
| 26 |
uct_md_resource_desc_t **resources_p, |
| 27 |
unsigned *num_resources_p) |
| 28 |
{ |
| 29 |
if (getenv("PMI_GNI_PTAG") == NULL) { |
| 30 |
return uct_md_query_empty_md_resource(resources_p, num_resources_p); |
| 31 |
} |
| 32 |
|
| 33 |
return uct_md_query_single_md_resource(component, resources_p, |
| 34 |
num_resources_p); |
| 35 |
} |
| 36 |
|
| 37 |
static ucs_status_t uct_ugni_md_query(uct_md_h md, uct_md_attr_v2_t *md_attr) |
| 38 |
{ |
| 39 |
uct_md_base_md_query(md_attr); |
| 40 |
md_attr->rkey_packed_size = 3 * sizeof(uint64_t); |
| 41 |
md_attr->flags = UCT_MD_FLAG_REG | UCT_MD_FLAG_NEED_MEMH | |
| 42 |
UCT_MD_FLAG_NEED_RKEY; |
| 43 |
md_attr->reg_mem_types = UCS_BIT(UCS_MEMORY_TYPE_HOST); |
| 44 |
md_attr->cache_mem_types = UCS_BIT(UCS_MEMORY_TYPE_HOST); |
| 45 |
md_attr->access_mem_types = UCS_BIT(UCS_MEMORY_TYPE_HOST); |
| 46 |
md_attr->reg_cost = ucs_linear_func_make(1000.0e-9, 0.007e-9); |
| 47 |
return UCS_OK; |
| 48 |
} |
| 49 |
|
| 50 |
static ucs_status_t uct_ugni_mem_reg(uct_md_h md, void *address, size_t length, |
| 51 |
const uct_md_mem_reg_params_t *params, |
| 52 |
uct_mem_h *memh_p) |
| 53 |
{ |
| 54 |
ucs_status_t status; |
| 55 |
gni_return_t ugni_rc; |
| 56 |
uct_ugni_md_t *ugni_md = ucs_derived_of(md, uct_ugni_md_t); |
| 57 |
gni_mem_handle_t * mem_hndl = NULL; |
| 58 |
|
| 59 |
mem_hndl = ucs_malloc(sizeof(gni_mem_handle_t), "gni_mem_handle_t"); |
| 60 |
if (NULL == mem_hndl) { |
| 61 |
ucs_error("Failed to allocate memory for gni_mem_handle_t"); |
| 62 |
status = UCS_ERR_NO_MEMORY; |
| 63 |
goto mem_err; |
| 64 |
} |
| 65 |
|
| 66 |
uct_ugni_cdm_lock(&ugni_md->cdm); |
| 67 |
ugni_rc = GNI_MemRegister(ugni_md->cdm.nic_handle, (uint64_t)address, |
| 68 |
length, NULL, |
| 69 |
GNI_MEM_READWRITE | GNI_MEM_RELAXED_PI_ORDERING, |
| 70 |
-1, mem_hndl); |
| 71 |
uct_ugni_cdm_unlock(&ugni_md->cdm); |
| 72 |
if (GNI_RC_SUCCESS != ugni_rc) { |
| 73 |
ucs_error("GNI_MemRegister failed (addr %p, size %zu), Error status: %s %d", |
| 74 |
address, length, gni_err_str[ugni_rc], ugni_rc); |
| 75 |
status = UCS_ERR_IO_ERROR; |
| 76 |
goto mem_err; |
| 77 |
} |
| 78 |
|
| 79 |
ucs_debug("Memory registration address %p, len %lu, keys [%"PRIx64" %"PRIx64"]", |
| 80 |
address, length, mem_hndl->qword1, mem_hndl->qword2); |
| 81 |
*memh_p = mem_hndl; |
| 82 |
return UCS_OK; |
| 83 |
|
| 84 |
mem_err: |
| 85 |
ucs_free(mem_hndl); |
| 86 |
return status; |
| 87 |
} |
| 88 |
|
| 89 |
static ucs_status_t uct_ugni_mem_dereg(uct_md_h md, |
| 90 |
const uct_md_mem_dereg_params_t *params) |
| 91 |
{ |
| 92 |
uct_ugni_md_t *ugni_md = ucs_derived_of(md, uct_ugni_md_t); |
| 93 |
gni_mem_handle_t *mem_hndl; |
| 94 |
gni_return_t ugni_rc; |
| 95 |
ucs_status_t status = UCS_OK; |
| 96 |
|
| 97 |
UCT_MD_MEM_DEREG_CHECK_PARAMS(params, 0); |
| 98 |
|
| 99 |
uct_ugni_cdm_lock(&ugni_md->cdm); |
| 100 |
mem_hndl = (gni_mem_handle_t *)params->memh; |
| 101 |
ugni_rc = GNI_MemDeregister(ugni_md->cdm.nic_handle, mem_hndl); |
| 102 |
uct_ugni_cdm_unlock(&ugni_md->cdm); |
| 103 |
if (GNI_RC_SUCCESS != ugni_rc) { |
| 104 |
ucs_error("GNI_MemDeregister failed, Error status: %s %d", |
| 105 |
gni_err_str[ugni_rc], ugni_rc); |
| 106 |
status = UCS_ERR_IO_ERROR; |
| 107 |
} |
| 108 |
ucs_free(mem_hndl); |
| 109 |
|
| 110 |
return status; |
| 111 |
} |
| 112 |
|
| 113 |
static ucs_status_t |
| 114 |
uct_ugni_mkey_pack(uct_md_h md, uct_mem_h memh, void *address, size_t length, |
| 115 |
const uct_md_mkey_pack_params_t *params, |
| 116 |
void *mkey_buffer) |
| 117 |
{ |
| 118 |
gni_mem_handle_t *mem_hndl = memh; |
| 119 |
uint64_t *ptr = mkey_buffer; |
| 120 |
|
| 121 |
ptr[0] = UCT_UGNI_RKEY_MAGIC; |
| 122 |
ptr[1] = mem_hndl->qword1; |
| 123 |
ptr[2] = mem_hndl->qword2; |
| 124 |
ucs_debug("Packed [ %"PRIx64" %"PRIx64" %"PRIx64"]", ptr[0], ptr[1], ptr[2]); |
| 125 |
return UCS_OK; |
| 126 |
} |
| 127 |
|
| 128 |
static ucs_status_t uct_ugni_rkey_release(uct_component_t *component, |
| 129 |
uct_rkey_t rkey, void *handle) |
| 130 |
{ |
| 131 |
ucs_assert(NULL == handle); |
| 132 |
ucs_free((void *)rkey); |
| 133 |
return UCS_OK; |
| 134 |
} |
| 135 |
|
| 136 |
static ucs_status_t uct_ugni_rkey_unpack(uct_component_t *component, |
| 137 |
const void *rkey_buffer, |
| 138 |
const uct_rkey_unpack_params_t *params, |
| 139 |
uct_rkey_t *rkey_p, void **handle_p) |
| 140 |
{ |
| 141 |
const uint64_t *ptr = rkey_buffer; |
| 142 |
gni_mem_handle_t *mem_hndl = NULL; |
| 143 |
uint64_t magic = 0; |
| 144 |
|
| 145 |
ucs_debug("Unpacking [ %"PRIx64" %"PRIx64" %"PRIx64"]", ptr[0], ptr[1], ptr[2]); |
| 146 |
magic = ptr[0]; |
| 147 |
if (magic != UCT_UGNI_RKEY_MAGIC) { |
| 148 |
ucs_error("Failed to identify key. Expected %llx but received %"PRIx64"", |
| 149 |
UCT_UGNI_RKEY_MAGIC, magic); |
| 150 |
return UCS_ERR_UNSUPPORTED; |
| 151 |
} |
| 152 |
|
| 153 |
mem_hndl = ucs_malloc(sizeof(gni_mem_handle_t), "gni_mem_handle_t"); |
| 154 |
if (NULL == mem_hndl) { |
| 155 |
ucs_error("Failed to allocate memory for gni_mem_handle_t"); |
| 156 |
return UCS_ERR_NO_MEMORY; |
| 157 |
} |
| 158 |
|
| 159 |
mem_hndl->qword1 = ptr[1]; |
| 160 |
mem_hndl->qword2 = ptr[2]; |
| 161 |
*rkey_p = (uintptr_t)mem_hndl; |
| 162 |
*handle_p = NULL; |
| 163 |
return UCS_OK; |
| 164 |
} |
| 165 |
|
| 166 |
static void uct_ugni_md_close(uct_md_h md) |
| 167 |
{ |
| 168 |
uct_ugni_md_t *ugni_md = ucs_derived_of(md, uct_ugni_md_t); |
| 169 |
|
| 170 |
pthread_mutex_lock(&uct_ugni_global_lock); |
| 171 |
ugni_md->ref_count--; |
| 172 |
if (!ugni_md->ref_count) { |
| 173 |
ucs_debug("Tearing down MD CDM"); |
| 174 |
uct_ugni_destroy_cdm(&ugni_md->cdm); |
| 175 |
} |
| 176 |
pthread_mutex_unlock(&uct_ugni_global_lock); |
| 177 |
} |
| 178 |
|
| 179 |
static ucs_status_t |
| 180 |
uct_ugni_md_open(uct_component_h component,const char *md_name, |
| 181 |
const uct_md_config_t *md_config, uct_md_h *md_p) |
| 182 |
{ |
| 183 |
ucs_status_t status = UCS_OK; |
| 184 |
static uct_md_ops_t md_ops; |
| 185 |
static uct_ugni_md_t md; |
| 186 |
|
| 187 |
pthread_mutex_lock(&uct_ugni_global_lock); |
| 188 |
md_ops.close = uct_ugni_md_close; |
| 189 |
md_ops.query = uct_ugni_md_query; |
| 190 |
md_ops.mem_alloc = (void*)ucs_empty_function; |
| 191 |
md_ops.mem_free = (void*)ucs_empty_function; |
| 192 |
md_ops.mem_reg = uct_ugni_mem_reg; |
| 193 |
md_ops.mem_dereg = uct_ugni_mem_dereg; |
| 194 |
md_ops.mem_attach = (uct_md_mem_attach_func_t)ucs_empty_function_return_unsupported; |
| 195 |
md_ops.mkey_pack = uct_ugni_mkey_pack; |
| 196 |
md_ops.detect_memory_type = (uct_md_detect_memory_type_func_t)ucs_empty_function_return_unsupported; |
| 197 |
|
| 198 |
md.super.ops = &md_ops; |
| 199 |
md.super.component = &uct_ugni_component; |
| 200 |
md.ref_count = 0; |
| 201 |
|
| 202 |
*md_p = &md.super; |
| 203 |
|
| 204 |
if (!md.ref_count) { |
| 205 |
status = init_device_list(); |
| 206 |
if (UCS_OK != status) { |
| 207 |
ucs_error("Failed to init device list, Error status: %d", status); |
| 208 |
goto error; |
| 209 |
} |
| 210 |
status = uct_ugni_create_md_cdm(&md.cdm); |
| 211 |
if (UCS_OK != status) { |
| 212 |
ucs_error("Failed to UGNI NIC, Error status: %d", status); |
| 213 |
goto error; |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
md.ref_count++; |
| 218 |
|
| 219 |
error: |
| 220 |
pthread_mutex_unlock(&uct_ugni_global_lock); |
| 221 |
return status; |
| 222 |
} |
| 223 |
|
| 224 |
uct_component_t uct_ugni_component = { |
| 225 |
.query_md_resources = uct_ugni_query_md_resources, |
| 226 |
.md_open = uct_ugni_md_open, |
| 227 |
.cm_open = (uct_component_cm_open_func_t)ucs_empty_function_return_unsupported, |
| 228 |
.rkey_unpack = uct_ugni_rkey_unpack, |
| 229 |
.rkey_ptr = (uct_component_rkey_ptr_func_t)ucs_empty_function_return_unsupported, |
| 230 |
.rkey_release = uct_ugni_rkey_release, |
| 231 |
.rkey_compare = uct_base_rkey_compare, |
| 232 |
.name = UCT_UGNI_MD_NAME, |
| 233 |
.md_config = { |
| 234 |
.name = "UGNI memory domain", |
| 235 |
.prefix = "UGNI_", |
| 236 |
.table = uct_md_config_table, |
| 237 |
.size = sizeof(uct_md_config_t), |
| 238 |
}, |
| 239 |
.cm_config = UCS_CONFIG_EMPTY_GLOBAL_LIST_ENTRY, |
| 240 |
.tl_list = UCT_COMPONENT_TL_LIST_INITIALIZER(&uct_ugni_component), |
| 241 |
.flags = 0, |
| 242 |
.md_vfs_init = (uct_component_md_vfs_init_func_t)ucs_empty_function |
| 243 |
}; |
| 244 |
UCT_COMPONENT_REGISTER(&uct_ugni_component); |