NVIDIA DOCA SDK Data Center on a Chip Framework Documentation
gpunetio_simple_receive_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_send_wait_cfg *sample_cfg = (struct sample_send_wait_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_send_wait_cfg *sample_cfg = (struct sample_send_wait_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  * Register sample command line parameters.
83  *
84  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
85  */
87 {
89  struct doca_argp_param *gpu_param, *nic_param;
90 
91  result = doca_argp_param_create(&gpu_param);
92  if (result != DOCA_SUCCESS) {
93  DOCA_LOG_ERR("Failed to create ARGP param: %s", doca_error_get_descr(result));
94  return result;
95  }
96 
97  doca_argp_param_set_short_name(gpu_param, "g");
98  doca_argp_param_set_long_name(gpu_param, "gpu");
99  doca_argp_param_set_arguments(gpu_param, "<GPU PCIe address>");
100  doca_argp_param_set_description(gpu_param, "GPU PCIe address to be used by the sample");
104  result = doca_argp_register_param(gpu_param);
105  if (result != DOCA_SUCCESS) {
106  DOCA_LOG_ERR("Failed to register ARGP param: %s", doca_error_get_descr(result));
107  return result;
108  }
109 
110  result = doca_argp_param_create(&nic_param);
111  if (result != DOCA_SUCCESS) {
112  DOCA_LOG_ERR("Failed to create ARGP param: %s", doca_error_get_descr(result));
113  return result;
114  }
115 
116  doca_argp_param_set_short_name(nic_param, "n");
117  doca_argp_param_set_long_name(nic_param, "nic");
118  doca_argp_param_set_arguments(nic_param, "<NIC PCIe address>");
119  doca_argp_param_set_description(nic_param, "DOCA device PCIe address used by the sample");
123  result = doca_argp_register_param(nic_param);
124  if (result != DOCA_SUCCESS) {
125  DOCA_LOG_ERR("Failed to register ARGP param: %s", doca_error_get_descr(result));
126  return result;
127  }
128 
129  return DOCA_SUCCESS;
130 }
131 
132 /*
133  * Sample main function
134  *
135  * @argc [in]: command line arguments size
136  * @argv [in]: array of command line arguments
137  * @return: EXIT_SUCCESS on success and EXIT_FAILURE otherwise
138  */
139 int main(int argc, char **argv)
140 {
142  struct doca_log_backend *sdk_log;
143  struct sample_send_wait_cfg sample_cfg;
144  int exit_status = EXIT_FAILURE;
145  int cuda_id;
146  cudaError_t cuda_ret;
147 
148  /* Register a logger backend */
150  if (result != DOCA_SUCCESS)
151  goto sample_exit;
152 
153  /* Register a logger backend for internal SDK errors and warnings */
154  result = doca_log_backend_create_with_file_sdk(stderr, &sdk_log);
155  if (result != DOCA_SUCCESS)
156  goto sample_exit;
158  if (result != DOCA_SUCCESS)
159  goto sample_exit;
160 
161  DOCA_LOG_INFO("Starting the sample");
162 
163  result = doca_argp_init(NULL, &sample_cfg);
164  if (result != DOCA_SUCCESS) {
165  DOCA_LOG_ERR("Failed to init ARGP resources: %s", doca_error_get_descr(result));
166  goto sample_exit;
167  }
168 
170  if (result != DOCA_SUCCESS) {
171  DOCA_LOG_ERR("Failed to parse application input: %s", doca_error_get_descr(result));
172  goto argp_cleanup;
173  }
174 
175  result = doca_argp_start(argc, argv);
176  if (result != DOCA_SUCCESS) {
177  DOCA_LOG_ERR("Failed to parse application input: %s", doca_error_get_descr(result));
178  goto argp_cleanup;
179  }
180 
181  /* In a multi-GPU system, ensure CUDA refers to the right GPU device */
182  cuda_ret = cudaDeviceGetByPCIBusId(&cuda_id, sample_cfg.gpu_pcie_addr);
183  if (cuda_ret != cudaSuccess) {
184  DOCA_LOG_ERR("Invalid GPU bus id provided %s", sample_cfg.gpu_pcie_addr);
186  }
187 
188  cudaFree(0);
189  cudaSetDevice(cuda_id);
190 
191  DOCA_LOG_INFO("Sample configuration:\n\tGPU %s\n\tNIC %s\n\t",
192  sample_cfg.gpu_pcie_addr,
193  sample_cfg.nic_pcie_addr);
194 
195  result = gpunetio_simple_receive(&sample_cfg);
196  if (result != DOCA_SUCCESS) {
197  DOCA_LOG_ERR("gpunetio_simple_receive() encountered an error: %s", doca_error_get_descr(result));
198  goto argp_cleanup;
199  }
200 
201  exit_status = EXIT_SUCCESS;
202 
203 argp_cleanup:
205 sample_exit:
206  if (exit_status == EXIT_SUCCESS)
207  DOCA_LOG_INFO("Sample finished successfully");
208  else
209  DOCA_LOG_INFO("Sample finished with errors");
210  return exit_status;
211 }
#define NULL
Definition: __stddef_null.h:26
int32_t result
uint64_t len
doca_error_t gpunetio_simple_receive(struct sample_send_wait_cfg *sample_cfg)
#define MAX_PCI_ADDRESS_LEN
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 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
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 gpu_pcie_addr[DOCA_DEVINFO_PCI_ADDR_SIZE]
char nic_pcie_addr[DOCA_DEVINFO_PCI_ADDR_SIZE]