NVIDIA DOCA SDK Data Center on a Chip Framework Documentation
pe_multi_context_sample.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 <stdio.h>
27 #include <stdlib.h>
28 #include <stdint.h>
29 
30 #include <doca_mmap.h>
31 #include <doca_buf.h>
32 #include <doca_buf_inventory.h>
33 #include <doca_ctx.h>
34 #include <doca_dma.h>
35 #include <doca_types.h>
36 #include <doca_log.h>
37 #include <doca_pe.h>
38 
39 #include <samples/common.h>
40 #include "pe_common.h"
41 
42 DOCA_LOG_REGISTER(PE_POLLING::SAMPLE);
43 
58 #define EXIT_ON_FAILURE(_expression_) \
59  { \
60  doca_error_t _status_ = _expression_; \
61 \
62  if (_status_ != DOCA_SUCCESS) { \
63  DOCA_LOG_ERR("%s failed with status %s", __func__, doca_error_get_descr(_status_)); \
64  return _status_; \
65  } \
66  }
67 
68 #define NUM_CTX (4)
69 #define NUM_TASKS (16)
70 #define DMA_BUFFER_SIZE (1024)
71 #define BUFFER_SIZE (DMA_BUFFER_SIZE * 2 * NUM_TASKS * NUM_CTX)
72 #define BUF_INVENTORY_SIZE (NUM_TASKS * 2 * NUM_CTX)
73 
79 
80  struct doca_dma *dma[NUM_CTX];
81  struct doca_ctx *dma_ctx[NUM_CTX];
82  struct doca_dma_task_memcpy *tasks[NUM_CTX][NUM_TASKS];
83 };
84 
85 /*
86  * DMA Memcpy task completed callback
87  *
88  * @dma_task [in]: Completed task
89  * @task_user_data [in]: doca_data from the task
90  * @ctx_user_data [in]: doca_data from the context
91  */
92 static void dma_memcpy_completed_callback(struct doca_dma_task_memcpy *dma_task,
93  union doca_data task_user_data,
94  union doca_data ctx_user_data)
95 {
96  uint8_t expected_value = (uint8_t)task_user_data.u64;
97  struct pe_multi_ctx_sample_state *state = (struct pe_multi_ctx_sample_state *)ctx_user_data.ptr;
98 
99  state->base.num_completed_tasks++;
100 
112  (void)process_completed_dma_memcpy_task(dma_task, expected_value);
113 
114  /* The task is no longer required, therefore it can be freed */
115  (void)dma_task_free(dma_task);
116 }
117 
118 /*
119  * Memcpy task error callback
120  *
121  * @dma_task [in]: failed task
122  * @task_user_data [in]: doca_data from the task
123  * @ctx_user_data [in]: doca_data from the context
124  */
125 static void dma_memcpy_error_callback(struct doca_dma_task_memcpy *dma_task,
126  union doca_data task_user_data,
127  union doca_data ctx_user_data)
128 {
129  struct pe_multi_ctx_sample_state *state = (struct pe_multi_ctx_sample_state *)ctx_user_data.ptr;
130  struct doca_task *task = doca_dma_task_memcpy_as_task(dma_task);
131 
132  (void)task_user_data;
133 
134  /* This sample defines that a task is completed even if it is completed with error */
135  state->base.num_completed_tasks++;
136 
137  DOCA_LOG_ERR("Task failed with status %s", doca_error_get_descr(doca_task_get_status(task)));
138 
139  /* The task is no longer required, therefore it can be freed */
140  (void)dma_task_free(dma_task);
141 }
142 
150 {
151  union doca_data ctx_user_data = {0};
152  uint32_t i = 0;
153 
154  DOCA_LOG_INFO("Creating DMA");
155 
161  ctx_user_data.ptr = state;
162 
163  for (i = 0; i < NUM_CTX; i++) {
164  EXIT_ON_FAILURE(doca_dma_create(state->base.device, &state->dma[i]));
165  state->dma_ctx[i] = doca_dma_as_ctx(state->dma[i]);
166 
167  EXIT_ON_FAILURE(doca_ctx_set_user_data(state->dma_ctx[i], ctx_user_data));
168 
174  EXIT_ON_FAILURE(doca_pe_connect_ctx(state->base.pe, state->dma_ctx[i]));
175 
179  NUM_TASKS));
180  }
181 
182  return DOCA_SUCCESS;
183 }
184 
192 {
193  uint32_t i = 0;
194 
199  for (i = 0; i < NUM_CTX; i++)
201 
202  return DOCA_SUCCESS;
203 }
204 
213 {
214  uint32_t i = 0;
215 
216  DOCA_LOG_INFO("Allocating tasks");
217 
218  for (i = 0; i < NUM_CTX; i++)
220  allocate_dma_tasks(&state->base, state->dma[i], NUM_TASKS, DMA_BUFFER_SIZE, state->tasks[i]));
221 
222  return DOCA_SUCCESS;
223 }
224 
232 {
233  uint32_t i = 0;
234  uint32_t j = 0;
235 
236  DOCA_LOG_INFO("Submitting tasks");
237 
242  for (i = 0; i < NUM_CTX; i++)
243  for (j = 0; j < NUM_TASKS; j++)
245 
246  return DOCA_SUCCESS;
247 }
248 
258 {
263  uint32_t ctx_index = 0;
264 
265  for (ctx_index = 0; ctx_index < NUM_CTX; ctx_index++) {
266  if (state->dma_ctx[ctx_index] != NULL) {
267  (void)doca_ctx_stop(state->dma_ctx[ctx_index]);
268  (void)doca_dma_destroy(state->dma[ctx_index]);
269  }
270  }
271 
272  pe_sample_base_cleanup(&state->base);
273 }
274 
284 {
285  memset(state, 0, sizeof(*state));
286 
287  state->base.buffer_size = BUFFER_SIZE;
289 
291  EXIT_ON_FAILURE(open_device(&state->base));
292  EXIT_ON_FAILURE(create_mmap(&state->base));
294  EXIT_ON_FAILURE(create_pe(&state->base));
296  EXIT_ON_FAILURE(start_dmas(state));
300 
301  return DOCA_SUCCESS;
302 }
303 
310 {
311  struct pe_multi_ctx_sample_state state;
312  doca_error_t status = run(&state);
313 
314  cleanup(&state);
315 
316  return status;
317 }
#define NULL
Definition: __stddef_null.h:26
static doca_error_t allocate_buffer(struct cache_invalidate_sample_state *state)
static doca_error_t create_buf_inventory(struct cache_invalidate_sample_state *state)
static doca_error_t poll_for_completion(struct cache_invalidate_sample_state *state)
static doca_error_t create_pe(struct cache_invalidate_sample_state *state)
static doca_error_t create_mmap(struct doca_dev *doca_device, unsigned int mmap_permissions, void *memrange_addr, size_t memrange_len, struct doca_mmap **mmap, doca_dpa_dev_mmap_t *dpa_mmap_handle)
DOCA_STABLE doca_error_t doca_ctx_start(struct doca_ctx *ctx)
Finalizes all configurations, and starts the DOCA CTX.
DOCA_STABLE doca_error_t doca_ctx_set_user_data(struct doca_ctx *ctx, union doca_data user_data)
set user data to context
DOCA_STABLE doca_error_t doca_ctx_stop(struct doca_ctx *ctx)
Stops the context allowing reconfiguration.
DOCA_STABLE struct doca_task * doca_dma_task_memcpy_as_task(struct doca_dma_task_memcpy *task)
This method converts a memcpy task to doca_task.
DOCA_STABLE struct doca_ctx * doca_dma_as_ctx(struct doca_dma *dma)
DOCA_STABLE doca_error_t doca_dma_task_memcpy_set_conf(struct doca_dma *dma, doca_dma_task_memcpy_completion_cb_t task_completion_cb, doca_dma_task_memcpy_completion_cb_t task_error_cb, uint32_t num_memcpy_tasks)
This method sets the DMA memcpy tasks configuration.
DOCA_STABLE doca_error_t doca_dma_create(struct doca_dev *dev, struct doca_dma **dma)
DOCA_STABLE doca_error_t doca_dma_destroy(struct doca_dma *dma)
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_SUCCESS
Definition: doca_error.h:38
#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_STABLE doca_error_t doca_task_get_status(const struct doca_task *task)
Get task status.
DOCA_STABLE doca_error_t doca_pe_connect_ctx(struct doca_pe *pe, struct doca_ctx *ctx)
This method connects a context to a progress engine.
DOCA_STABLE doca_error_t doca_task_submit(struct doca_task *task)
Submit a task to a progress engine.
doca_dev * open_device(std::string const &identifier)
Definition: doca_utils.cpp:43
void pe_sample_base_cleanup(struct pe_sample_state_base *state)
Definition: pe_common.c:311
doca_error_t dma_task_free(struct doca_dma_task_memcpy *dma_task)
Definition: pe_common.c:108
doca_error_t allocate_dma_tasks(struct pe_sample_state_base *state, struct doca_dma *dma, uint32_t num_tasks, size_t dma_buffer_size, struct doca_dma_task_memcpy **tasks)
Definition: pe_common.c:155
doca_error_t process_completed_dma_memcpy_task(struct doca_dma_task_memcpy *dma_task, uint8_t expected_value)
Definition: pe_common.c:66
#define EXIT_ON_FAILURE(_expression_)
void cleanup(struct pe_multi_ctx_sample_state *state)
#define BUF_INVENTORY_SIZE
doca_error_t submit_tasks_for_multi_context(struct pe_multi_ctx_sample_state *state)
doca_error_t run(struct pe_multi_ctx_sample_state *state)
doca_error_t start_dmas(struct pe_multi_ctx_sample_state *state)
#define BUFFER_SIZE
DOCA_LOG_REGISTER(PE_POLLING::SAMPLE)
static void dma_memcpy_completed_callback(struct doca_dma_task_memcpy *dma_task, union doca_data task_user_data, union doca_data ctx_user_data)
doca_error_t run_pe_multi_ctx_sample(void)
#define NUM_TASKS
static void dma_memcpy_error_callback(struct doca_dma_task_memcpy *dma_task, union doca_data task_user_data, union doca_data ctx_user_data)
doca_error_t create_dmas(struct pe_multi_ctx_sample_state *state)
#define DMA_BUFFER_SIZE
doca_error_t allocate_tasks_for_multi_context(struct pe_multi_ctx_sample_state *state)
#define NUM_CTX
struct doca_dma_task_memcpy * tasks[NUM_CTX][NUM_TASKS]
struct doca_ctx * dma_ctx[NUM_CTX]
struct pe_sample_state_base base
struct doca_dma * dma[NUM_CTX]
struct doca_dev * device
Definition: pe_common.h:39
size_t buf_inventory_size
Definition: pe_common.h:45
struct doca_pe * pe
Definition: pe_common.h:42
uint32_t num_completed_tasks
Definition: pe_common.h:56
Convenience type for representing opaque data.
Definition: doca_types.h:56
uint64_t u64
Definition: doca_types.h:58
void * ptr
Definition: doca_types.h:57