NVIDIA DOCA SDK Data Center on a Chip Framework Documentation
dpa_kernel_launch_sample.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2022 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 <pthread.h>
27 #include <stdlib.h>
28 #include <unistd.h>
29 
30 #include <doca_error.h>
31 #include <doca_log.h>
32 
33 #include "dpa_common.h"
34 
35 DOCA_LOG_REGISTER(KERNEL_LAUNCH::SAMPLE);
36 
37 /* Kernel function declaration */
39 
40 /*
41  * Waits for 5 seconds and then update a given event.
42  * This function is made to be used by a pthread, hence it needs to return void*, and it will always return NULL.
43  *
44  * @event [in]: event to be updated
45  * @return: NULL (dummy return)
46  */
47 static void *wait_event_update_thread(void *event)
48 {
50 
51  sleep(5);
52  result = doca_sync_event_update_set((struct doca_sync_event *)event, 5);
53  if (result != DOCA_SUCCESS)
54  DOCA_LOG_ERR("Failed to update DOCA sync event: %s", doca_error_get_descr(result));
55 
56  /* Dummy return */
57  return NULL;
58 }
59 
60 /*
61  * Run kernel_launch sample
62  *
63  * @resources [in]: DOCA DPA resources that the DPA sample will use
64  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
65  */
67 {
68  struct doca_sync_event *wait_event = NULL;
69  struct doca_sync_event *comp_event = NULL;
70  pthread_t tid = 0;
71  uint64_t wait_thresh = 4;
72  uint64_t comp_event_val = 10;
73  const unsigned int num_dpa_threads = 1;
74  doca_error_t result, tmp_result;
75  int res = 0;
76 
77  result = create_doca_dpa_wait_sync_event(resources->pf_dpa_ctx, resources->pf_doca_device, &wait_event);
78  if (result != DOCA_SUCCESS) {
79  DOCA_LOG_ERR("Failed to create DOCA sync event for DPA kernel wait: %s", doca_error_get_descr(result));
80  return result;
81  }
82 
84  resources->pf_doca_device,
85  &comp_event,
86  NULL);
87  if (result != DOCA_SUCCESS) {
88  DOCA_LOG_ERR("Failed to create DOCA sync event for DPA kernel completion: %s",
90  goto destroy_wait_event;
91  }
92 
94  wait_event,
95  wait_thresh,
96  comp_event,
97  comp_event_val,
98  num_dpa_threads,
99  &hello_world);
100  if (result != DOCA_SUCCESS) {
101  DOCA_LOG_ERR("Failed to launch hello_world kernel: %s", doca_error_get_descr(result));
102  goto destroy_comp_event;
103  }
104 
105  res = pthread_create(&tid, NULL, wait_event_update_thread, (void *)wait_event);
106  if (res != 0) {
107  DOCA_LOG_ERR("Failed to create thread");
109  goto destroy_comp_event;
110  }
111 
112  res = pthread_detach(tid);
113  if (res != 0) {
114  DOCA_LOG_ERR("pthread_detach() failed");
116  goto destroy_comp_event;
117  }
118 
119  result = doca_sync_event_wait_gt(comp_event, comp_event_val - 1, SYNC_EVENT_MASK_FFS);
120  if (result != DOCA_SUCCESS)
121  DOCA_LOG_ERR("Failed to wait for host completion event: %s", doca_error_get_descr(result));
122 
123 destroy_comp_event:
124  /* Sleep 2 seconds before destroying the events*/
125  sleep(2);
126  tmp_result = doca_sync_event_destroy(comp_event);
127  if (tmp_result != DOCA_SUCCESS) {
128  DOCA_LOG_ERR("Failed to destroy host completion event: %s", doca_error_get_descr(tmp_result));
129  DOCA_ERROR_PROPAGATE(result, tmp_result);
130  }
131 destroy_wait_event:
132  tmp_result = doca_sync_event_destroy(wait_event);
133  if (tmp_result != DOCA_SUCCESS) {
134  DOCA_LOG_ERR("Failed to destroy host wait event: %s", doca_error_get_descr(tmp_result));
135  DOCA_ERROR_PROPAGATE(result, tmp_result);
136  }
137  return result;
138 }
#define NULL
Definition: __stddef_null.h:26
int32_t result
static doca_error_t create_doca_dpa_completion_sync_event(struct doca_dpa *doca_dpa, struct doca_dev *doca_device, struct doca_sync_event **comp_event)
#define SYNC_EVENT_MASK_FFS
doca_error_t create_doca_dpa_wait_sync_event(struct doca_dpa *doca_dpa, struct doca_dev *doca_device, struct doca_sync_event **wait_event)
Create DOCA sync event to be published by the CPU and subscribed by the DPA.
Definition: dpa_common.c:313
doca_error_t kernel_launch(struct dpa_resources *resources)
static void * wait_event_update_thread(void *event)
doca_dpa_func_t hello_world
DOCA_LOG_REGISTER(KERNEL_LAUNCH::SAMPLE)
struct rdma_resources resources
DOCA_EXPERIMENTAL doca_error_t doca_dpa_kernel_launch_update_set(struct doca_dpa *dpa, struct doca_sync_event *wait_event, uint64_t wait_threshold, struct doca_sync_event *comp_event, uint64_t comp_count, unsigned int num_threads, doca_dpa_func_t *func,...)
Submit a kernel to DPA that sets completion event.
void() doca_dpa_func_t(void)
Generic function pointer type.
Definition: doca_dpa.h:118
#define DOCA_ERROR_PROPAGATE(r, t)
Save the first encountered doca_error_t.
Definition: doca_error.h:83
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_OPERATING_SYSTEM
Definition: doca_error.h:58
@ DOCA_SUCCESS
Definition: doca_error.h:38
#define DOCA_LOG_ERR(format,...)
Generates an ERROR application log message.
Definition: doca_log.h:466
DOCA_EXPERIMENTAL doca_error_t doca_sync_event_update_set(struct doca_sync_event *event, uint64_t value)
Set the value of a Sync Event to some value synchronously.
DOCA_EXPERIMENTAL doca_error_t doca_sync_event_wait_gt(struct doca_sync_event *event, uint64_t value, uint64_t mask)
Wait for the value of a Sync Event to be grater than some threshold value synchronously in a polling ...
DOCA_EXPERIMENTAL doca_error_t doca_sync_event_destroy(struct doca_sync_event *event)
Destroy a Sync Event instance.
A struct that includes all the resources needed for DPA.
Definition: dpa_common.h:85