Memkind 例程1 调用memkind_malloc()分配内存

版权声明: https://blog.csdn.net/qccz123456/article/details/81905635

例程1 调用memkind_malloc()分配内存
Key:
void *memkind_malloc(memkind_t kind, size_t size);   // 用于分配内存,完成后得到相应的指针,和malloc作用相同
int memkind_posix_memalign(memkind_t kind, void **memptr, size_t alignment, size_t size);   // 采用内存对齐的方式分配内存

memkind_t 指定的是可以分配哪些memory,在下文中详细说明了可调用的参数,但是调用是否成功,需依据具体硬件而定:http://memkind.github.io/memkind/man_pages/memkind.html

MEMKIND_DEFAULT  Default allocation using standard memory and default page size.
MEMKIND_HUGETLB  Allocate from standard memory using huge pages. Note: This kind requires huge pages configuration described in SYSTEM CONFIGURATION section.
MEMKIND_GBTLB (DEPRECATED)  Allocate from standard memory using 1GB chunks backed by huge pages. Note: This kind requires huge pages configuration described in SYSTEM CONFIGURATION section.
MEMKIND_INTERLEAVE  Allocate pages interleaved across all NUMA nodes with transparent huge pages disabled.
MEMKIND_HBW  Allocate from the closest high bandwidth memory NUMA node at time of allocation. If there is not enough high bandwidth memory to satisfy the request errno is set to ENOMEM and the allocated pointer is set to NULL.
MEMKIND_HBW_ALL  Same as MEMKIND_HBW except decision regarding closest NUMA node is postponed until the time of first write.
MEMKIND_HBW_HUGETLB  Same as MEMKIND_HBW except the allocation is backed by huge pages. Note: This kind requires huge pages configuration described in SYSTEM CONFIGURATION section.
MEMKIND_HBW_ALL_HUGETLB  Combination of MEMKIND_HBW_ALL and MEMKIND_HBW_HUGETLB properties. Note: This kind requires huge pages configuration described in SYSTEM CONFIGURATION section.
MEMKIND_HBW_PREFERRED  Same as MEMKIND_HBW except that if there is not enough high bandwidth memory to satisfy the request, the allocation will fall back on standard memory.
MEMKIND_HBW_PREFERRED_HUGETLB  Same as MEMKIND_HBW_PREFERRED except the allocation is backed by huge pages. Note: This kind requires huge pages configuration described in SYSTEM CONFIGURATION section.
MEMKIND_HBW_GBTLB (DEPRECATED)  Same as MEMKIND_HBW except the allocation is backed by 1GB chunks of huge pages. Note that size can take on any value, but full gigabyte pages will allocated for each request, so remainder of the last page will be wasted. This kind requires huge pages configuration described in SYSTEM CONFIGURATION section.
MEMKIND_HBW_PREFERRED_GBTLB (DEPRECATED)  Same as MEMKIND_HBW_GBTLB except that if there is not enough high bandwidth memory to satisfy the request, the allocation will fall back on standard memory. Note: This kind requires huge pages configuration described in SYSTEM CONFIGURATION section.
MEMKIND_HBW_INTERLEAVE  Same as MEMKIND_HBW except that the pages that support the allocation are interleaved across all high bandwidth nodes and transparent huge pages are disabled.
MEMKIND_REGULAR  Allocate from regular memory using the default page size. Regular means general purpose memory from the NUMA nodes containing CPUs.

例程如下:

#include <stdlib.h>
#include <stdio.h>
#include <errno.h>

#include <memkind.h>

int main(int argc, char **argv)
{
    const size_t size = 512;
    char *default_str = NULL;
    char *hugetlb_str = NULL;
    char *hbw_str = NULL;
    char *hbw_hugetlb_str = NULL;
    char *hbw_preferred_str = NULL;
    char *hbw_preferred_hugetlb_str = NULL;
    char *hbw_interleave_str = NULL;

    default_str = (char *)memkind_malloc(MEMKIND_DEFAULT, size);
    if (default_str == NULL) {
        perror("memkind_malloc()");
        fprintf(stderr, "Unable to allocate default string\n");
        return errno ? -errno : 1;
    }
    hugetlb_str = (char *)memkind_malloc(MEMKIND_HUGETLB, size);
    if (hugetlb_str == NULL) {
        perror("memkind_malloc()");
        fprintf(stderr, "Unable to allocate hugetlb string\n");
        return errno ? -errno : 1;
    }
    hbw_str = (char *)memkind_malloc(MEMKIND_HBW, size);
    if (hbw_str == NULL) {
        perror("memkind_malloc()");
        fprintf(stderr, "Unable to allocate hbw string\n");
        return errno ? -errno : 1;
    }
    hbw_hugetlb_str = (char *)memkind_malloc(MEMKIND_HBW_HUGETLB, size);
    if (hbw_hugetlb_str == NULL) {
        perror("memkind_malloc()");
        fprintf(stderr, "Unable to allocate hbw_hugetlb string\n");
        return errno ? -errno : 1;
    }
    hbw_preferred_str = (char *)memkind_malloc(MEMKIND_HBW_PREFERRED, size);
    if (hbw_preferred_str == NULL) {
        perror("memkind_malloc()");
        fprintf(stderr, "Unable to allocate hbw_preferred string\n");
        return errno ? -errno : 1;
    }
    hbw_preferred_hugetlb_str = (char *)memkind_malloc(MEMKIND_HBW_PREFERRED_HUGETLB, size);
    if (hbw_preferred_hugetlb_str == NULL) {
        perror("memkind_malloc()");
        fprintf(stderr, "Unable to allocate hbw_preferred_hugetlb string\n");
        return errno ? -errno : 1;
    }
    hbw_interleave_str = (char *)memkind_malloc(MEMKIND_HBW_INTERLEAVE, size);
    if (hbw_interleave_str == NULL) {
        perror("memkind_malloc()");
        fprintf(stderr,"Unable to allocate hbw_interleave string\n");
        return errno ? -errno : 1;
    }

    sprintf(default_str, "Hello world from standard memory\n");
    sprintf(hugetlb_str, "Hello world from standard memory with 2 MB pages\n");
    sprintf(hbw_str, "Hello world from high bandwidth memory\n");
    sprintf(hbw_hugetlb_str, "Hello world from high bandwidth 2 MB paged memory\n");
    sprintf(hbw_preferred_str, "Hello world from high bandwidth memory if sufficient resources exist\n");
    sprintf(hbw_preferred_hugetlb_str, "Hello world from high bandwidth 2 MB paged memory if sufficient resources exist\n");
    sprintf(hbw_interleave_str, "Hello world from high bandwidth interleaved memory\n");

    fprintf(stdout, "%s", default_str);
    fprintf(stdout, "%s", hugetlb_str);
    fprintf(stdout, "%s", hbw_str);
    fprintf(stdout, "%s", hbw_hugetlb_str);
    fprintf(stdout, "%s", hbw_preferred_str);
    fprintf(stdout, "%s", hbw_preferred_hugetlb_str);
    fprintf(stdout, "%s", hbw_interleave_str);

    memkind_free(MEMKIND_HBW_INTERLEAVE, hbw_interleave_str);
    memkind_free(MEMKIND_HBW_PREFERRED_HUGETLB, hbw_preferred_hugetlb_str);
    memkind_free(MEMKIND_HBW_PREFERRED, hbw_preferred_str);
    memkind_free(MEMKIND_HBW_HUGETLB, hbw_hugetlb_str);
    memkind_free(MEMKIND_HBW, hbw_str);
    memkind_free(MEMKIND_HUGETLB, hugetlb_str);
    memkind_free(MEMKIND_DEFAULT, default_str);

    return 0;
}

运行结果如下:

$ ./hello_memkind
memkind_malloc(): Success
Unable to allocate hbw string

猜你喜欢

转载自blog.csdn.net/qccz123456/article/details/81905635