<commit_before>/**
* Copyright (C) Mellanox Technologies Ltd. 2001-2014.  ALL RIGHTS RESERVED.
* Copyright (C) UT-Battelle, LLC. 2014. ALL RIGHTS RESERVED.
* $COPYRIGHT$
* $HEADER$
*/

#define _GNU_SOURCE /* for CPU_ZERO/CPU_SET in sched.h */
#include "ib_device.h"

#include <uct/tl/context.h>
#include <ucs/debug/memtrack.h>
#include <ucs/debug/log.h>
#include <ucs/async/async.h>
#include <ucs/sys/compiler.h>
#include <ucs/sys/sys.h>
#include <sys/poll.h>
#include <sched.h>


#define UCT_IB_PD_PREFIX         "ib"
#define UCT_IB_MEM_ACCESS_FLAGS  (IBV_ACCESS_LOCAL_WRITE | \
                                  IBV_ACCESS_REMOTE_WRITE | \
                                  IBV_ACCESS_REMOTE_READ | \
                                  IBV_ACCESS_REMOTE_ATOMIC)

#if ENABLE_STATS
static ucs_stats_class_t uct_ib_device_stats_class = {
    .name           = "",
    .num_counters   = UCT_IB_DEVICE_STAT_LAST,
    .counter_names = {
        [UCT_IB_DEVICE_STAT_MEM_ALLOC]   = "mem_alloc",
        [UCT_IB_DEVICE_STAT_MEM_REG]     = "mem_reg",
        [UCT_IB_DEVICE_STAT_ASYNC_EVENT] = "async_event"
    }
};
#endif

static void uct_ib_device_get_affinity(const char *dev_name, cpu_set_t *cpu_mask)
{
    char *p, buf[CPU_SETSIZE];
    ssize_t nread;
    uint32_t word;
    int base, k;

    CPU_ZERO(cpu_mask);
    nread = ucs_read_file(buf, sizeof(buf), 1,
                          "/sys/class/infiniband/%s/device/local_cpus",
                          dev_name);
    if (nread >= 0) {
        base = 0;
        do {
            p = strrchr(buf, ',');
            if (p == NULL) {
                p = buf;
            } else if (*p == ',') {
                *(p++) = 0;
            }

            word = strtoul(p, 0, 16);
            for (k = 0; word; ++k, word >>= 1) {
                if (word & 1) {
                    CPU_SET(base + k, cpu_mask);
                }
            }
            base += 32;
        } while ((base < CPU_SETSIZE) && (p != buf));
    } else {
        /* If affinity file is not present, treat all CPUs as local */
        for (k = 0; k < CPU_SETSIZE; ++k) {
            CPU_SET(k, cpu_mask);
        }
    }
}

static void uct_ib_pd_close(uct_pd_h pd)
{
    uct_ib_device_t *dev = ucs_derived_of(pd, uct_ib_device_t);

    ucs_debug("closing ib device %s", uct_ib_device_name(dev));
    ucs_async_unset_event_handler(dev->ibv_context->async_fd);
    UCS_STATS_NODE_FREE(dev->stats);
    ibv_dealloc_pd(dev->pd);
    ibv_close_device(dev->ibv_context);
    ucs_free(dev);
}

static ucs_status_t uct_ib_pd_query(uct_pd_h pd, uct_pd_attr_t *pd_attr)
{
    uct_ib_device_t *dev = ucs_derived_of(pd, uct_ib_device_t);

    pd_attr->cap.max_alloc = ULONG_MAX; /* TODO query device */
    pd_attr->cap.max_reg   = ULONG_MAX; /* TODO query device */
    pd_attr->cap.flags     = UCT_PD_FLAG_REG;
    pd_attr->local_cpus    = dev->local_cpus;
    return UCS_OK;
}

static ucs_status_t uct_ib_dereg_mr(struct ibv_mr *mr)
{
    int ret;

    ret = ibv_dereg_mr(mr);
    if (ret != 0) {
        ucs_error("ibv_dereg_mr() failed: %m");
        return UCS_ERR_IO_ERROR;
    }

    return UCS_OK;
}

static ucs_status_t uct_ib_mem_alloc(uct_pd_h pd, size_t *length_p, void **address_p,
                                     uct_mem_h *memh_p UCS_MEMTRACK_ARG)
{
    uct_ib_device_t *dev = ucs_derived_of(pd, uct_ib_device_t);
    struct ibv_exp_reg_mr_in in = {
        dev->pd,
        NULL,
        ucs_memtrack_adjust_alloc_size(*length_p),
        UCT_IB_MEM_ACCESS_FLAGS | IBV_EXP_ACCESS_ALLOCATE_MR,
        0
    };
    struct ibv_mr *mr;<commit_msg>IB: Enable using contig pages, was off by mistake.<commit_after>/**
* Copyright (C) Mellanox Technologies Ltd. 2001-2014.  ALL RIGHTS RESERVED.
* Copyright (C) UT-Battelle, LLC. 2014. ALL RIGHTS RESERVED.
* $COPYRIGHT$
* $HEADER$
*/

#define _GNU_SOURCE /* for CPU_ZERO/CPU_SET in sched.h */
#include "ib_device.h"

#include <uct/tl/context.h>
#include <ucs/debug/memtrack.h>
#include <ucs/debug/log.h>
#include <ucs/async/async.h>
#include <ucs/sys/compiler.h>
#include <ucs/sys/sys.h>
#include <sys/poll.h>
#include <sched.h>


#define UCT_IB_PD_PREFIX         "ib"
#define UCT_IB_MEM_ACCESS_FLAGS  (IBV_ACCESS_LOCAL_WRITE | \
                                  IBV_ACCESS_REMOTE_WRITE | \
                                  IBV_ACCESS_REMOTE_READ | \
                                  IBV_ACCESS_REMOTE_ATOMIC)

#if ENABLE_STATS
static ucs_stats_class_t uct_ib_device_stats_class = {
    .name           = "",
    .num_counters   = UCT_IB_DEVICE_STAT_LAST,
    .counter_names = {
        [UCT_IB_DEVICE_STAT_MEM_ALLOC]   = "mem_alloc",
        [UCT_IB_DEVICE_STAT_MEM_REG]     = "mem_reg",
        [UCT_IB_DEVICE_STAT_ASYNC_EVENT] = "async_event"
    }
};
#endif

static void uct_ib_device_get_affinity(const char *dev_name, cpu_set_t *cpu_mask)
{
    char *p, buf[CPU_SETSIZE];
    ssize_t nread;
    uint32_t word;
    int base, k;

    CPU_ZERO(cpu_mask);
    nread = ucs_read_file(buf, sizeof(buf), 1,
                          "/sys/class/infiniband/%s/device/local_cpus",
                          dev_name);
    if (nread >= 0) {
        base = 0;
        do {
            p = strrchr(buf, ',');
            if (p == NULL) {
                p = buf;
            } else if (*p == ',') {
                *(p++) = 0;
            }

            word = strtoul(p, 0, 16);
            for (k = 0; word; ++k, word >>= 1) {
                if (word & 1) {
                    CPU_SET(base + k, cpu_mask);
                }
            }
            base += 32;
        } while ((base < CPU_SETSIZE) && (p != buf));
    } else {
        /* If affinity file is not present, treat all CPUs as local */
        for (k = 0; k < CPU_SETSIZE; ++k) {
            CPU_SET(k, cpu_mask);
        }
    }
}

static void uct_ib_pd_close(uct_pd_h pd)
{
    uct_ib_device_t *dev = ucs_derived_of(pd, uct_ib_device_t);

    ucs_debug("closing ib device %s", uct_ib_device_name(dev));
    ucs_async_unset_event_handler(dev->ibv_context->async_fd);
    UCS_STATS_NODE_FREE(dev->stats);
    ibv_dealloc_pd(dev->pd);
    ibv_close_device(dev->ibv_context);
    ucs_free(dev);
}

static ucs_status_t uct_ib_pd_query(uct_pd_h pd, uct_pd_attr_t *pd_attr)
{
    uct_ib_device_t *dev = ucs_derived_of(pd, uct_ib_device_t);

    pd_attr->cap.max_alloc = ULONG_MAX; /* TODO query device */
    pd_attr->cap.max_reg   = ULONG_MAX; /* TODO query device */
    pd_attr->cap.flags     = UCT_PD_FLAG_REG;

    if (IBV_EXP_HAVE_CONTIG_PAGES(&dev->dev_attr)) {
        pd_attr->cap.flags |= UCT_PD_FLAG_ALLOC;
    }

    pd_attr->local_cpus    = dev->local_cpus;
    return UCS_OK;
}

static ucs_status_t uct_ib_dereg_mr(struct ibv_mr *mr)
{
    int ret;

    ret = ibv_dereg_mr(mr);
    if (ret != 0) {
        ucs_error("ibv_dereg_mr() failed: %m");
        return UCS_ERR_IO_ERROR;
    }

    return UCS_OK;
}

static ucs_status_t uct_ib_mem_alloc(uct_pd_h pd, size_t *length_p, void **address_p,
                                     uct_mem_h *memh_p UCS_MEMTRACK_ARG)
{
    uct_ib_device_t *dev = ucs_derived_of(pd, uct_ib_device_t);
    struct ibv_exp_reg_mr_in in = {
        dev->pd,
        NULL,
        ucs_memtrack_adjust_alloc_size(*length_p),
        UCT_IB_MEM_ACCESS_FLAGS | IBV_EXP_ACCESS_ALLOCATE_MR,
        0
    };
    struct ibv_mr *mr;
{'fraction_non_alphanumeric': 0.11833995343103684, 'fraction_numerical': 0.00794411724421312, 'mean_word_length': 2.2395740905057675, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1852020', 'n_tokens_mistral': 2946, 'n_tokens_neox': 2809, 'n_words': 501}