1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright (c) 2021 NVIDIA Corporation & Affiliates
14 #include <rte_common.h>
15 #include <rte_malloc.h>
16 #include <rte_memory.h>
18 #include <rte_ether.h>
19 #include <rte_ethdev.h>
20 #include <rte_mempool.h>
23 #include <rte_gpudev.h>
31 usage(const char *prog_name)
33 printf("%s [EAL options] --\n",
38 args_parse(int argc, char **argv)
44 static struct option lgopts[] = {
45 { "help", 0, 0, ARG_HELP},
51 while ((opt = getopt_long(argc, argvopt, "",
52 lgopts, &opt_idx)) != EOF) {
59 rte_exit(EXIT_FAILURE, "Invalid option: %s\n", argv[optind]);
66 alloc_gpu_memory(uint16_t gpu_id)
70 size_t buf_bytes = 1024;
73 printf("\n=======> TEST: Allocate GPU memory\n");
75 /* Alloc memory on GPU 0 */
76 ptr_1 = rte_gpu_mem_alloc(gpu_id, buf_bytes);
78 fprintf(stderr, "rte_gpu_mem_alloc GPU memory returned error\n");
81 printf("GPU memory allocated at 0x%p %zdB\n", ptr_1, buf_bytes);
83 ptr_2 = rte_gpu_mem_alloc(gpu_id, buf_bytes);
85 fprintf(stderr, "rte_gpu_mem_alloc GPU memory returned error\n");
88 printf("GPU memory allocated at 0x%p %zdB\n", ptr_2, buf_bytes);
90 ret = rte_gpu_mem_free(gpu_id, (uint8_t *)(ptr_1)+0x700);
92 printf("GPU memory 0x%p + 0x700 NOT freed because of memory address not recognized by driver\n", ptr_1);
94 fprintf(stderr, "rte_gpu_mem_free erroneously freed GPU memory 0x%p + 0x700\n", ptr_1);
98 ret = rte_gpu_mem_free(gpu_id, ptr_2);
100 fprintf(stderr, "rte_gpu_mem_free returned error %d\n", ret);
103 printf("GPU memory 0x%p freed\n", ptr_2);
105 ret = rte_gpu_mem_free(gpu_id, ptr_1);
107 fprintf(stderr, "rte_gpu_mem_free returned error %d\n", ret);
110 printf("GPU memory 0x%p freed\n", ptr_1);
116 register_cpu_memory(uint16_t gpu_id)
119 size_t buf_bytes = 1024;
122 printf("\n=======> TEST: Register CPU memory\n");
124 /* Alloc memory on CPU visible from GPU 0 */
125 ptr = rte_zmalloc(NULL, buf_bytes, 0);
127 fprintf(stderr, "Failed to allocate CPU memory.\n");
131 ret = rte_gpu_mem_register(gpu_id, buf_bytes, ptr);
133 fprintf(stderr, "rte_gpu_mem_register CPU memory returned error %d\n", ret);
136 printf("CPU memory registered at 0x%p %zdB\n", ptr, buf_bytes);
138 ret = rte_gpu_mem_unregister(gpu_id, (uint8_t *)(ptr)+0x700);
140 printf("CPU memory 0x%p + 0x700 NOT unregistered because of memory address not recognized by driver\n", ptr);
142 fprintf(stderr, "rte_gpu_mem_free erroneously freed GPU memory 0x%p + 0x700\n", ptr);
145 printf("CPU memory 0x%p unregistered\n", ptr);
147 ret = rte_gpu_mem_unregister(gpu_id, ptr);
149 fprintf(stderr, "rte_gpu_mem_unregister returned error %d\n", ret);
152 printf("CPU memory 0x%p unregistered\n", ptr);
158 main(int argc, char **argv)
163 struct rte_gpu_info ginfo;
166 ret = rte_eal_init(argc, argv);
168 rte_exit(EXIT_FAILURE, "EAL init failed\n");
172 args_parse(argc, argv);
176 nb_gpus = rte_gpu_count_avail();
177 printf("\n\nDPDK found %d GPUs:\n", nb_gpus);
178 RTE_GPU_FOREACH(gpu_id)
180 if (rte_gpu_info_get(gpu_id, &ginfo))
181 rte_exit(EXIT_FAILURE, "rte_gpu_info_get error - bye\n");
183 printf("\tGPU ID %d\n\t\tparent ID %d GPU Bus ID %s NUMA node %d Tot memory %.02f MB, Tot processors %d\n",
188 (((float)ginfo.total_memory)/(float)1024)/(float)1024,
189 ginfo.processor_count
195 fprintf(stderr, "Need at least one GPU on the system to run the example\n");
204 alloc_gpu_memory(gpu_id);
205 register_cpu_memory(gpu_id);
207 /* clean up the EAL */