NVIDIA DOCA SDK Data Center on a Chip Framework Documentation
gpunetio_simple_send_main.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2023-2024 NVIDIA CORPORATION AND AFFILIATES. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without modification, are permitted
5  * provided that the following conditions are met:
6  * * Redistributions of source code must retain the above copyright notice, this list of
7  * conditions and the following disclaimer.
8  * * Redistributions in binary form must reproduce the above copyright notice, this list of
9  * conditions and the following disclaimer in the documentation and/or other materials
10  * provided with the distribution.
11  * * Neither the name of the NVIDIA CORPORATION nor the names of its contributors may be used
12  * to endorse or promote products derived from this software without specific prior written
13  * permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
17  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
19  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
20  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
21  * STRICT LIABILITY, OR TOR (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
22  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23  *
24  */
25 
26 #include <doca_argp.h>
27 #include <doca_log.h>
28 
29 #include "gpunetio_common.h"
30 
31 DOCA_LOG_REGISTER(GPU_SEND_WAIT_TIME::MAIN);
32 
33 /*
34  * Get GPU PCIe address input.
35  *
36  * @param [in]: Command line parameter
37  * @config [in]: Application configuration structure
38  * @return: DOCA_SUCCESS on success and DOCA_ERROR_INVALID_VALUE otherwise
39  */
40 static doca_error_t gpu_pci_address_callback(void *param, void *config)
41 {
42  struct sample_simple_send_cfg *sample_cfg = (struct sample_simple_send_cfg *)config;
43  char *pci_address = (char *)param;
44  size_t len;
45 
46  len = strnlen(pci_address, MAX_PCI_ADDRESS_LEN);
47  if (len >= MAX_PCI_ADDRESS_LEN) {
48  DOCA_LOG_ERR("PCI address too long. Max %d", MAX_PCI_ADDRESS_LEN - 1);
50  }
51 
52  strncpy(sample_cfg->gpu_pcie_addr, pci_address, len + 1);
53 
54  return DOCA_SUCCESS;
55 }
56 
57 /*
58  * Get NIC PCIe address input.
59  *
60  * @param [in]: Command line parameter
61  * @config [in]: Application configuration structure
62  * @return: DOCA_SUCCESS on success and DOCA_ERROR_INVALID_VALUE otherwise
63  */
64 static doca_error_t nic_pci_address_callback(void *param, void *config)
65 {
66  struct sample_simple_send_cfg *sample_cfg = (struct sample_simple_send_cfg *)config;
67  char *pci_address = (char *)param;
68  size_t len;
69 
70  len = strnlen(pci_address, MAX_PCI_ADDRESS_LEN);
71  if (len >= MAX_PCI_ADDRESS_LEN) {
72  DOCA_LOG_ERR("PCI address too long. Max %d", MAX_PCI_ADDRESS_LEN - 1);
74  }
75 
76  strncpy(sample_cfg->nic_pcie_addr, pci_address, len + 1);
77 
78  return DOCA_SUCCESS;
79 }
80 
81 /*
82  * Get packet size input.
83  *
84  * @param [in]: Command line parameter
85  * @config [in]: Application configuration structure
86  * @return: DOCA_SUCCESS on success and DOCA_ERROR_INVALID_VALUE otherwise
87  */
88 static doca_error_t packet_size_callback(void *param, void *config)
89 {
90  struct sample_simple_send_cfg *sample_cfg = (struct sample_simple_send_cfg *)config;
91  const int pkt_size = *(uint32_t *)param;
92 
93  if (pkt_size <= 0) {
94  DOCA_LOG_ERR("Packet size must be a positive value.");
96  }
97 
98  sample_cfg->pkt_size = pkt_size;
99 
100  return DOCA_SUCCESS;
101 }
102 
103 /*
104  * Number of CUDA threads in the CUDA kernel send
105  *
106  * @param [in]: Command line parameter
107  * @config [in]: Application configuration structure
108  * @return: DOCA_SUCCESS on success and DOCA_ERROR_INVALID_VALUE otherwise
109  */
110 static doca_error_t cuda_threads_callback(void *param, void *config)
111 {
112  struct sample_simple_send_cfg *sample_cfg = (struct sample_simple_send_cfg *)config;
113  const int cuda_threads = *(uint32_t *)param;
114 
115  if (cuda_threads <= 0) {
116  DOCA_LOG_ERR("Packet size must be a positive value.");
118  }
119 
120  sample_cfg->cuda_threads = cuda_threads;
121 
122  return DOCA_SUCCESS;
123 }
124 
125 /*
126  * Register sample command line parameters.
127  *
128  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
129  */
131 {
133  struct doca_argp_param *gpu_param, *nic_param, *size_param, *thread_param;
134 
135  result = doca_argp_param_create(&gpu_param);
136  if (result != DOCA_SUCCESS) {
137  DOCA_LOG_ERR("Failed to create ARGP param: %s", doca_error_get_descr(result));
138  return result;
139  }
140 
141  doca_argp_param_set_short_name(gpu_param, "g");
142  doca_argp_param_set_long_name(gpu_param, "gpu");
143  doca_argp_param_set_arguments(gpu_param, "<GPU PCIe address>");
144  doca_argp_param_set_description(gpu_param, "GPU PCIe address to be used by the sample");
148  result = doca_argp_register_param(gpu_param);
149  if (result != DOCA_SUCCESS) {
150  DOCA_LOG_ERR("Failed to register ARGP param: %s", doca_error_get_descr(result));
151  return result;
152  }
153 
154  result = doca_argp_param_create(&nic_param);
155  if (result != DOCA_SUCCESS) {
156  DOCA_LOG_ERR("Failed to create ARGP param: %s", doca_error_get_descr(result));
157  return result;
158  }
159 
160  doca_argp_param_set_short_name(nic_param, "n");
161  doca_argp_param_set_long_name(nic_param, "nic");
162  doca_argp_param_set_arguments(nic_param, "<NIC PCIe address>");
163  doca_argp_param_set_description(nic_param, "DOCA device PCIe address used by the sample");
167  result = doca_argp_register_param(nic_param);
168  if (result != DOCA_SUCCESS) {
169  DOCA_LOG_ERR("Failed to register ARGP param: %s", doca_error_get_descr(result));
170  return result;
171  }
172 
173  result = doca_argp_param_create(&size_param);
174  if (result != DOCA_SUCCESS) {
175  DOCA_LOG_ERR("Failed to create ARGP param: %s", doca_error_get_descr(result));
176  return result;
177  }
178 
179  doca_argp_param_set_short_name(size_param, "s");
180  doca_argp_param_set_long_name(size_param, "size");
181  doca_argp_param_set_arguments(size_param, "<packet size>");
182  doca_argp_param_set_description(size_param, "Packet size to send");
185  doca_argp_param_set_mandatory(size_param);
186  result = doca_argp_register_param(size_param);
187  if (result != DOCA_SUCCESS) {
188  DOCA_LOG_ERR("Failed to register ARGP param: %s", doca_error_get_descr(result));
189  return result;
190  }
191 
192  result = doca_argp_param_create(&thread_param);
193  if (result != DOCA_SUCCESS) {
194  DOCA_LOG_ERR("Failed to create ARGP param: %s", doca_error_get_descr(result));
195  return result;
196  }
197 
198  doca_argp_param_set_short_name(thread_param, "t");
199  doca_argp_param_set_long_name(thread_param, "threads");
200  doca_argp_param_set_arguments(thread_param, "<CUDA threads in send kernel>");
201  doca_argp_param_set_description(thread_param, "Number of CUDA threads in send kernel");
204  doca_argp_param_set_mandatory(thread_param);
205  result = doca_argp_register_param(thread_param);
206  if (result != DOCA_SUCCESS) {
207  DOCA_LOG_ERR("Failed to register ARGP param: %s", doca_error_get_descr(result));
208  return result;
209  }
210 
211  return DOCA_SUCCESS;
212 }
213 
214 /*
215  * Sample main function
216  *
217  * @argc [in]: command line arguments size
218  * @argv [in]: array of command line arguments
219  * @return: EXIT_SUCCESS on success and EXIT_FAILURE otherwise
220  */
221 int main(int argc, char **argv)
222 {
224  struct doca_log_backend *sdk_log;
225  struct sample_simple_send_cfg sample_cfg;
226  int exit_status = EXIT_FAILURE;
227  int cuda_id;
228  cudaError_t cuda_ret;
229 
230  /* Register a logger backend */
232  if (result != DOCA_SUCCESS)
233  goto sample_exit;
234 
235  /* Register a logger backend for internal SDK errors and warnings */
236  result = doca_log_backend_create_with_file_sdk(stderr, &sdk_log);
237  if (result != DOCA_SUCCESS)
238  goto sample_exit;
240  if (result != DOCA_SUCCESS)
241  goto sample_exit;
242 
243  DOCA_LOG_INFO("Starting the sample");
244 
245  result = doca_argp_init(NULL, &sample_cfg);
246  if (result != DOCA_SUCCESS) {
247  DOCA_LOG_ERR("Failed to init ARGP resources: %s", doca_error_get_descr(result));
248  goto sample_exit;
249  }
250 
252  if (result != DOCA_SUCCESS) {
253  DOCA_LOG_ERR("Failed to parse application input: %s", doca_error_get_descr(result));
254  goto argp_cleanup;
255  }
256 
257  result = doca_argp_start(argc, argv);
258  if (result != DOCA_SUCCESS) {
259  DOCA_LOG_ERR("Failed to parse application input: %s", doca_error_get_descr(result));
260  goto argp_cleanup;
261  }
262 
263  /* In a multi-GPU system, ensure CUDA refers to the right GPU device */
264  cuda_ret = cudaDeviceGetByPCIBusId(&cuda_id, sample_cfg.gpu_pcie_addr);
265  if (cuda_ret != cudaSuccess) {
266  DOCA_LOG_ERR("Invalid GPU bus id provided %s", sample_cfg.gpu_pcie_addr);
268  }
269 
270  cudaFree(0);
271  cudaSetDevice(cuda_id);
272 
273  DOCA_LOG_INFO("Sample configuration:\n\tGPU %s\n\tNIC %s\n\tPacket size %d\n\tCUDA threads %d\n\t",
274  sample_cfg.gpu_pcie_addr,
275  sample_cfg.nic_pcie_addr,
276  sample_cfg.pkt_size,
277  sample_cfg.cuda_threads);
278 
279  result = gpunetio_simple_send(&sample_cfg);
280  if (result != DOCA_SUCCESS) {
281  DOCA_LOG_ERR("gpunetio_simple_send() encountered an error: %s", doca_error_get_descr(result));
282  goto argp_cleanup;
283  }
284 
285  exit_status = EXIT_SUCCESS;
286 
287 argp_cleanup:
289 sample_exit:
290  if (exit_status == EXIT_SUCCESS)
291  DOCA_LOG_INFO("Sample finished successfully");
292  else
293  DOCA_LOG_INFO("Sample finished with errors");
294  return exit_status;
295 }
#define NULL
Definition: __stddef_null.h:26
int32_t result
uint64_t len
#define MAX_PCI_ADDRESS_LEN
doca_error_t gpunetio_simple_send(struct sample_simple_send_cfg *sample_cfg)
DOCA_LOG_REGISTER(GPU_SEND_WAIT_TIME::MAIN)
static doca_error_t register_sample_params(void)
int main(int argc, char **argv)
static doca_error_t nic_pci_address_callback(void *param, void *config)
static doca_error_t cuda_threads_callback(void *param, void *config)
static doca_error_t packet_size_callback(void *param, void *config)
static doca_error_t gpu_pci_address_callback(void *param, void *config)
DOCA_EXPERIMENTAL void doca_argp_param_set_description(struct doca_argp_param *param, const char *description)
Set the description of the program param, used during program usage.
DOCA_EXPERIMENTAL void doca_argp_param_set_long_name(struct doca_argp_param *param, const char *name)
Set the long name of the program param.
DOCA_EXPERIMENTAL doca_error_t doca_argp_start(int argc, char **argv)
Parse incoming arguments (cmd line/json).
DOCA_EXPERIMENTAL void doca_argp_param_set_arguments(struct doca_argp_param *param, const char *arguments)
Set the description of the expected arguments of the program param, used during program usage.
DOCA_EXPERIMENTAL doca_error_t doca_argp_init(const char *program_name, void *program_config)
Initialize the parser interface.
DOCA_EXPERIMENTAL void doca_argp_param_set_callback(struct doca_argp_param *param, doca_argp_param_cb_t callback)
Set the callback function of the program param.
DOCA_EXPERIMENTAL void doca_argp_param_set_mandatory(struct doca_argp_param *param)
Mark the program param as mandatory.
DOCA_EXPERIMENTAL doca_error_t doca_argp_param_create(struct doca_argp_param **param)
Create new program param.
DOCA_EXPERIMENTAL void doca_argp_param_set_type(struct doca_argp_param *param, enum doca_argp_type type)
Set the type of the param arguments.
DOCA_EXPERIMENTAL void doca_argp_param_set_short_name(struct doca_argp_param *param, const char *name)
Set the short name of the program param.
DOCA_EXPERIMENTAL doca_error_t doca_argp_destroy(void)
ARG Parser destroy.
DOCA_EXPERIMENTAL doca_error_t doca_argp_register_param(struct doca_argp_param *input_param)
Register a program flag.
@ DOCA_ARGP_TYPE_STRING
Definition: doca_argp.h:56
@ DOCA_ARGP_TYPE_INT
Definition: doca_argp.h:57
enum doca_error doca_error_t
DOCA API return codes.
DOCA_STABLE const char * doca_error_get_descr(doca_error_t error)
Returns the description string of an error code.
@ DOCA_ERROR_INVALID_VALUE
Definition: doca_error.h:44
@ DOCA_SUCCESS
Definition: doca_error.h:38
DOCA_EXPERIMENTAL doca_error_t doca_log_backend_create_standard(void)
Create default, non configurable backend for application messages.
#define DOCA_LOG_ERR(format,...)
Generates an ERROR application log message.
Definition: doca_log.h:466
#define DOCA_LOG_INFO(format,...)
Generates an INFO application log message.
Definition: doca_log.h:486
DOCA_EXPERIMENTAL doca_error_t doca_log_backend_create_with_file_sdk(FILE *fptr, struct doca_log_backend **backend)
Create a logging backend with a FILE* stream for SDK messages.
DOCA_EXPERIMENTAL doca_error_t doca_log_backend_set_sdk_level(struct doca_log_backend *backend, uint32_t level)
Set the log level limit for SDK logging backends.
@ DOCA_LOG_LEVEL_WARNING
Definition: doca_log.h:47
char nic_pcie_addr[MAX_PCI_ADDRESS_LEN]
char gpu_pcie_addr[MAX_PCI_ADDRESS_LEN]