NVIDIA DOCA SDK Data Center on a Chip Framework Documentation
urom_ping_pong_main.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2023 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 <stdlib.h>
27 
28 #include <mpi.h>
29 
30 #include <doca_argp.h>
31 #include <doca_log.h>
32 
33 #include "urom_common.h"
34 
35 DOCA_LOG_REGISTER(UROM_PING_PONG::MAIN);
36 
37 #define MAX_MSG_LEN 100 /* Maximum length of ping pong message*/
38 
39 /* Sample's Logic */
40 doca_error_t urom_ping_pong(const char *message, const char *device, uint32_t rank, uint32_t size);
41 
45 struct urom_pp_cfg {
46  struct urom_common_cfg common; /* Common command line configuration arguments */
47  char message[MAX_MSG_LEN]; /* Ping-Pong user-message */
48 };
49 
50 /*
51  * ARGP Callback - Handle ping pong message parameter
52  *
53  * @param [in]: Input parameter
54  * @config [in/out]: Program configuration context
55  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
56  */
57 static doca_error_t message_callback(void *param, void *config)
58 {
59  struct urom_pp_cfg *cfg = (struct urom_pp_cfg *)config;
60  char *msg = (char *)param;
61  int len;
62 
63  len = strnlen(msg, MAX_MSG_LEN);
64  if (len == MAX_MSG_LEN) {
65  DOCA_LOG_ERR("Entered message exceeding the maximum size of %d", MAX_MSG_LEN - 1);
67  }
68 
69  strncpy(cfg->message, msg, len + 1);
70  return DOCA_SUCCESS;
71 }
72 
73 /*
74  * Register ping pong argp params
75  *
76  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
77  */
79 {
81  struct doca_argp_param *message;
82 
84  if (result != DOCA_SUCCESS)
85  return result;
86 
87  result = doca_argp_param_create(&message);
88  if (result != DOCA_SUCCESS) {
89  DOCA_LOG_ERR("Failed to create ARGP param: %s", doca_error_get_descr(result));
90  return result;
91  }
92 
93  doca_argp_param_set_short_name(message, "m");
94  doca_argp_param_set_long_name(message, "message");
95  doca_argp_param_set_description(message, "Specify message");
98 
100  if (result != DOCA_SUCCESS) {
101  DOCA_LOG_ERR("Failed to register program param: %s", doca_error_get_descr(result));
102  return result;
103  }
104 
105  return DOCA_SUCCESS;
106 }
107 
108 /*
109  * Sample main function
110  *
111  * @argc [in]: command line arguments size
112  * @argv [in]: array of command line arguments
113  * @return: EXIT_SUCCESS on success and EXIT_FAILURE otherwise
114  */
115 int main(int argc, char **argv)
116 {
118  struct urom_pp_cfg cfg;
119  struct doca_log_backend *sdk_log;
120  int exit_status = EXIT_FAILURE;
121  int rank, size;
122 
123  /* Set configuration default values */
124  strcpy(cfg.message, "hello world");
125  strcpy(cfg.common.device_name, "mlx5_0");
126 
127  /* Register a logger backend */
129  if (result != DOCA_SUCCESS)
130  return EXIT_FAILURE;
131 
132  /* Register a logger backend for internal SDK errors and warnings */
133  result = doca_log_backend_create_with_file_sdk(stderr, &sdk_log);
134  if (result != DOCA_SUCCESS)
135  goto sample_exit;
137  if (result != DOCA_SUCCESS)
138  goto sample_exit;
139 
140  DOCA_LOG_INFO("Starting the sample");
141 
142  /* Parse cmdline/json arguments */
144  if (result != DOCA_SUCCESS) {
145  DOCA_LOG_ERR("Failed to init ARGP resources: %s", doca_error_get_descr(result));
146  goto sample_exit;
147  }
148 
149  /* Register RegEx scan params */
151  if (result != DOCA_SUCCESS) {
152  DOCA_LOG_ERR("Failed to register sample parameters: %s", doca_error_get_descr(result));
153  goto argp_cleanup;
154  }
155 
156  /* Start parsing sample arguments */
157  result = doca_argp_start(argc, argv);
158  if (result != DOCA_SUCCESS) {
159  DOCA_LOG_ERR("Failed to parse sample input: %s", doca_error_get_descr(result));
160  goto argp_cleanup;
161  }
162 
163  MPI_Init(NULL, NULL);
164  MPI_Comm_rank(MPI_COMM_WORLD, &rank);
165  MPI_Comm_size(MPI_COMM_WORLD, &size);
166 
167  result = urom_ping_pong(cfg.message, cfg.common.device_name, rank, size);
168  if (result != DOCA_SUCCESS) {
169  DOCA_LOG_ERR("urom_ping_pong() encountered an error: %s", doca_error_get_descr(result));
170  goto mpi_barrier;
171  }
172 
173  exit_status = EXIT_SUCCESS;
174 
175 mpi_barrier:
176  MPI_Barrier(MPI_COMM_WORLD);
177  MPI_Finalize();
178 argp_cleanup:
180 sample_exit:
181  if (exit_status == EXIT_SUCCESS)
182  DOCA_LOG_INFO("Sample finished successfully");
183  else
184  DOCA_LOG_INFO("Sample finished with errors");
185  return exit_status;
186 }
#define NULL
Definition: __stddef_null.h:26
int32_t result
uint64_t len
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 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 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
const struct ip_frag_config * cfg
Definition: ip_frag_dp.c:0
char message[MAX_MSG_LEN]
struct urom_common_cfg common
doca_error_t register_urom_common_params(void)
Definition: urom_common.c:69
doca_error_t urom_ping_pong(const char *message, const char *device, uint32_t rank, uint32_t size)
int main(int argc, char **argv)
DOCA_LOG_REGISTER(UROM_PING_PONG::MAIN)
#define MAX_MSG_LEN
static doca_error_t message_callback(void *param, void *config)
static doca_error_t register_urom_pp_params(void)