Skip to contentSkip to conversation

gpudev: introduce memory API

PATCH In review Thomas Monjalon 11 participants 128 messages Jun 2, 2021 – Nov 8, 2021 Page 2 of 2

Subsystems (3)
lore
Share & tools
lore
#106 lore raw

Re: [dpdk-dev] [EXT] Re: [PATCH v3 0/9] GPU library

In reply to Thomas Monjalon
Quoted 37 lines

-----Original Message-----
From: dev <[email protected]> On Behalf Of Thomas Monjalon
Sent: Wednesday, October 20, 2021 12:42 AM
To: Jerin Jacob <[email protected]>
Cc: Elena Agostini <[email protected]>; [email protected]; dpdk-dev <[email protected]>
Subject: [EXT] Re: [dpdk-dev] [PATCH v3 0/9] GPU library

External Email

----------------------------------------------------------------------
19/10/2021 20:47, Jerin Jacob:

Quoted 17 lines
Quoted 2 lines

As I stated multiple times, let’s start with something simple that works and then think about how to enhance the library/driver.

I have submitted RFC[1] to abstract in a generic way so that workload-specific _driver_ aspects are NOT EXPOSED to the application.

The RFC explains with application code, how any workload can be used by the application without exposing the driver details. Also, shows how to enable a framework to abstract a different form of workload acceletor(DPU, GPU, XPU, IPU....)

In order to map to this discussion with RFC:
You may need to add a new host port for GPU which as lib/dwa/rte_dwa_port_host_xxxxx.h and adding new workload as lib/dwa/rte_dwa_profile_xxxx.h(Which can be reused by all dataplane workload accelerator)

Quoted 3 lines

[1]
https://urldefense.proofpoint.com/v2/url?u=http-3A__mails.dpdk.org_arc
hives_dev_2021-

2DOctober_226070.html&d=DwIFaQ&c=nKjWec2b6R0mOyPaz7xtfQ

Quoted 1 line

&r=1DGob4H4rxz6H8uITozGOCa0s5f4wCNtTa4UUKvcsvI&m=8RtZAL1mpqAKtA i3Fpryk

Quoted 1 line
Quoted 7 lines

gq7CJ_9Xlb_cnR8gVPZPAc&s=aWBFPWzbVCCBZ9HSTIwBD5Mw9wdBYje1ad8zY j7oeiI&e

Quoted 1 line

=

That's really 2 different points of view:
- expose only some predefined dataplane workloads
- or allow to build any custom workload

That’s one way to look at. Other way to look at, If you split the workload as quantifiable piece. We could chain the workload to create any custom workload.
Another aspect is how can application writer custom Dataplane workload which is not specific to acceletor HW rather it express what need to do for the application .

Quoted 2 lines
#107 lore raw

v4 cover GPU library

In reply to Thomas Monjalon

From: Elena Agostini <[email protected]>

In heterogeneous computing system, processing is not only in the CPU. Some tasks can be delegated to devices working in parallel.

The goal of this new library is to enhance the collaboration between DPDK, that's primarily a CPU framework, and GPU devices.

When mixing network activity with task processing on a non-CPU device, there may be the need to put in communication the CPU with the device in order to manage the memory, synchronize operations, exchange info, etc..

This library provides a number of new features:
- Interoperability with GPU-specific library with generic handlers
- Possibility to allocate and free memory on the GPU
- Possibility to allocate and free memory on the CPU but visible from the GPU
- Communication functions to enhance the dialog between the CPU and the GPU

The infrastructure is prepared to welcome drivers in drivers/gpu/ as the CUDA one, sent as draft:
https://patches.dpdk.org/project/dpdk/patch/[email protected]/

Changelog:
- Patches updated to latest DPDK commit
- Communication list item has an array of mbufs instead of opaque objects
- Communication list free doesn't release mbufs anymore

Elena Agostini (6):
gpudev: introduce GPU device class library
gpudev: add memory API
gpudev: add memory barrier
gpudev: add communication flag
gpudev: add communication list
doc: add CUDA example in GPU guide

Thomas Monjalon (3):
gpudev: add event notification
gpudev: add child device representing a device context
gpudev: support multi-process

 .gitignore                             |   1 +
 MAINTAINERS                            |   6 +
 app/meson.build                        |   1 +
 app/test-gpudev/main.c                 | 394 +++++++++++
 app/test-gpudev/meson.build            |   5 +
 doc/api/doxy-api.conf.in               |   1 +
 doc/guides/conf.py                     |   8 +
 doc/guides/gpus/features/default.ini   |  13 +
 doc/guides/gpus/index.rst              |  11 +
 doc/guides/gpus/overview.rst           |  10 +
 doc/guides/index.rst                   |   1 +
 doc/guides/prog_guide/gpudev.rst       | 226 ++++++
 doc/guides/prog_guide/index.rst        |   1 +
 doc/guides/rel_notes/release_21_11.rst |   6 +
 drivers/gpu/meson.build                |   4 +
 drivers/meson.build                    |   1 +
 lib/gpudev/gpudev.c                    | 908 +++++++++++++++++++++++++
 lib/gpudev/gpudev_driver.h             | 102 +++
 lib/gpudev/meson.build                 |  12 +
 lib/gpudev/rte_gpudev.h                | 649 ++++++++++++++++++
 lib/gpudev/version.map                 |  38 ++
 lib/meson.build                        |   1 +
 22 files changed, 2399 insertions(+)
 create mode 100644 app/test-gpudev/main.c
 create mode 100644 app/test-gpudev/meson.build
 create mode 100644 doc/guides/gpus/features/default.ini
 create mode 100644 doc/guides/gpus/index.rst
 create mode 100644 doc/guides/gpus/overview.rst
 create mode 100644 doc/guides/prog_guide/gpudev.rst
 create mode 100644 drivers/gpu/meson.build
 create mode 100644 lib/gpudev/gpudev.c
 create mode 100644 lib/gpudev/gpudev_driver.h
 create mode 100644 lib/gpudev/meson.build
 create mode 100644 lib/gpudev/rte_gpudev.h
 create mode 100644 lib/gpudev/version.map
Signature · 1 line
2.17.1
#108 lore raw

PATCH v4 1/9 gpudev: introduce GPU device class library

From: Elena Agostini <[email protected]>

In heterogeneous computing system, processing is not only in the CPU. Some tasks can be delegated to devices working in parallel.

The new library gpudev is for dealing with GPGPU computing devices from a DPDK application running on the CPU.

The infrastructure is prepared to welcome drivers in drivers/gpu/.

---
 .gitignore                             |   1 +
 MAINTAINERS                            |   6 +
 app/meson.build                        |   1 +
 app/test-gpudev/main.c                 | 107 +++++++++++
 app/test-gpudev/meson.build            |   5 +
 doc/api/doxy-api.conf.in               |   1 +
 doc/guides/conf.py                     |   8 +
 doc/guides/gpus/features/default.ini   |  10 +
 doc/guides/gpus/index.rst              |  11 ++
 doc/guides/gpus/overview.rst           |  10 +
 doc/guides/index.rst                   |   1 +
 doc/guides/prog_guide/gpudev.rst       |  36 ++++
 doc/guides/prog_guide/index.rst        |   1 +
 doc/guides/rel_notes/release_21_11.rst |   4 +
 drivers/gpu/meson.build                |   4 +
 drivers/meson.build                    |   1 +
 lib/gpudev/gpudev.c                    | 249 +++++++++++++++++++++++++
 lib/gpudev/gpudev_driver.h             |  67 +++++++
 lib/gpudev/meson.build                 |  10 +
 lib/gpudev/rte_gpudev.h                | 168 +++++++++++++++++
 lib/gpudev/version.map                 |  20 ++
 lib/meson.build                        |   1 +
 22 files changed, 722 insertions(+)
 create mode 100644 app/test-gpudev/main.c
 create mode 100644 app/test-gpudev/meson.build
 create mode 100644 doc/guides/gpus/features/default.ini
 create mode 100644 doc/guides/gpus/index.rst
 create mode 100644 doc/guides/gpus/overview.rst
 create mode 100644 doc/guides/prog_guide/gpudev.rst
 create mode 100644 drivers/gpu/meson.build
 create mode 100644 lib/gpudev/gpudev.c
 create mode 100644 lib/gpudev/gpudev_driver.h
 create mode 100644 lib/gpudev/meson.build
 create mode 100644 lib/gpudev/rte_gpudev.h
 create mode 100644 lib/gpudev/version.map
.gitignore +1 -0
index b19c0717e6..49494e0c6c 100644
@@ -14,6 +14,7 @@ doc/guides/compressdevs/overview_feature_table.txt
 doc/guides/regexdevs/overview_feature_table.txt
 doc/guides/vdpadevs/overview_feature_table.txt
 doc/guides/bbdevs/overview_feature_table.txt
+doc/guides/gpus/overview_feature_table.txt
 
 # ignore generated ctags/cscope files
 cscope.out.po
MAINTAINERS +6 -0
index 0e5951f8f1..0f71736816 100644
@@ -467,6 +467,12 @@ M: Bruce Richardson <[email protected]>
 F: examples/dma/
 F: doc/guides/sample_app_ug/dma.rst
 
+General-Purpose Graphics Processing Unit (GPU) API - EXPERIMENTAL
+M: Elena Agostini <[email protected]>
+F: lib/gpudev/
+F: doc/guides/prog_guide/gpudev.rst
+F: doc/guides/gpus/features/default.ini
+
 Eventdev API
 M: Jerin Jacob <[email protected]>
 T: git://dpdk.org/next/dpdk-next-eventdev
app/meson.build +1 -0
index e41a2e3902..500d86750d 100644
@@ -13,6 +13,7 @@ apps = [
         'test-eventdev',
         'test-fib',
         'test-flow-perf',
+        'test-gpudev',
         'test-pipeline',
         'test-pmd',
         'test-regex',
app/test-gpudev/main.c new file +107 -0

107 lines not on this page · raw message

app/test-gpudev/meson.build new file +5 -0
new file mode 100644
index 0000000000..17bdef3646
@@ -0,0 +1,5 @@
+# SPDX-License-Identifier: BSD-3-Clause
+# Copyright (c) 2021 NVIDIA Corporation & Affiliates
+
+sources = files('main.c')
+deps = ['gpudev', 'ethdev']
doc/api/doxy-api.conf.in +1 -0
index 096ebbaf0d..db2ca9b6ed 100644
@@ -41,6 +41,7 @@ INPUT                   = @TOPDIR@/doc/api/doxy-api-index.md \
                           @TOPDIR@/lib/eventdev \
                           @TOPDIR@/lib/fib \
                           @TOPDIR@/lib/flow_classify \
+                          @TOPDIR@/lib/gpudev \
                           @TOPDIR@/lib/graph \
                           @TOPDIR@/lib/gro \
                           @TOPDIR@/lib/gso \
doc/guides/conf.py +8 -0
index 67d2dd62c7..7930da9ceb 100644
@@ -152,6 +152,9 @@ def generate_overview_table(output_filename, table_id, section, table_name, titl
         name = ini_filename[:-4]
         name = name.replace('_vf', 'vf')
         pmd_names.append(name)
+    if not pmd_names:
+        # Add an empty column if table is empty (required by RST syntax)
+        pmd_names.append(' ')
 
     # Pad the table header names.
     max_header_len = len(max(pmd_names, key=len))
@@ -388,6 +391,11 @@ def setup(app):
                             'Features',
                             'Features availability in bbdev drivers',
                             'Feature')
+    table_file = dirname(__file__) + '/gpus/overview_feature_table.txt'
+    generate_overview_table(table_file, 1,
+                            'Features',
+                            'Features availability in GPU drivers',
+                            'Feature')
 
     if LooseVersion(sphinx_version) < LooseVersion('1.3.1'):
         print('Upgrade sphinx to version >= 1.3.1 for '
doc/guides/gpus/features/default.ini new file +10 -0
new file mode 100644
index 0000000000..ec7a545eb7
@@ -0,0 +1,10 @@
+;
+; Features of GPU drivers.
+;
+; This file defines the features that are valid for inclusion in
+; the other driver files and also the order that they appear in
+; the features table in the documentation. The feature description
+; string should not exceed feature_str_len defined in conf.py.
+;
+[Features]
+Get device info                =
doc/guides/gpus/index.rst new file +11 -0
new file mode 100644
index 0000000000..1878423239
@@ -0,0 +1,11 @@
+.. SPDX-License-Identifier: BSD-3-Clause
+   Copyright (c) 2021 NVIDIA Corporation & Affiliates
+
+General-Purpose Graphics Processing Unit Drivers
+================================================
+
+.. toctree::
+   :maxdepth: 2
+   :numbered:
+
+   overview
doc/guides/gpus/overview.rst new file +10 -0
new file mode 100644
index 0000000000..4830348818
@@ -0,0 +1,10 @@
+.. SPDX-License-Identifier: BSD-3-Clause
+   Copyright (c) 2021 NVIDIA Corporation & Affiliates
+
+Overview of GPU Drivers
+=======================
+
+General-Purpose computing on Graphics Processing Unit (GPGPU)
+is the use of GPU to perform parallel computation.
+
+.. include:: overview_feature_table.txt
doc/guides/index.rst +1 -0
index 919825992e..5eb5bd9c9a 100644
@@ -22,6 +22,7 @@ DPDK documentation
    vdpadevs/index
    regexdevs/index
    dmadevs/index
+   gpus/index
    eventdevs/index
    rawdevs/index
    mempool/index
doc/guides/prog_guide/gpudev.rst new file +36 -0
new file mode 100644
index 0000000000..6ea7239159
@@ -0,0 +1,36 @@
+.. SPDX-License-Identifier: BSD-3-Clause
+   Copyright (c) 2021 NVIDIA Corporation & Affiliates
+
+General-Purpose Graphics Processing Unit Library
+================================================
+
+When mixing networking activity with task processing on a GPU device,
+there may be the need to put in communication the CPU with the device
+in order to manage the memory, synchronize operations, exchange info, etc..
+
+By means of the generic GPU interface provided by this library,
+it is possible to allocate a chunk of GPU memory and use it
+to create a DPDK mempool with external mbufs having the payload
+on the GPU memory, enabling any network interface card
+(which support this feature like Mellanox NIC)
+to directly transmit and receive packets using GPU memory.
+
+Additionally, this library provides a number of functions
+to enhance the dialog between CPU and GPU.
+
+Out of scope of this library is to provide a wrapper for GPU specific libraries
+(e.g. CUDA Toolkit or OpenCL), thus it is not possible to launch workload
+on the device or create GPU specific objects
+(e.g. CUDA Driver context or CUDA Streams in case of NVIDIA GPUs).
+
+
+Features
+--------
+
+This library provides a number of features:
+
+- Interoperability with device-specific library through generic handlers.
+
+
+API Overview
+------------
doc/guides/prog_guide/index.rst +1 -0
index 20e5155cf4..7090b5589a 100644
@@ -28,6 +28,7 @@ Programmer's Guide
     compressdev
     regexdev
     dmadev
+    gpudev
     rte_security
     rawdev
     link_bonding_poll_mode_drv_lib
doc/guides/rel_notes/release_21_11.rst +4 -0
index 502cc5ceb2..57851c63af 100644
@@ -91,6 +91,10 @@ New Features
   Added ``rte_eth_macaddrs_get`` to allow user to retrieve all Ethernet
   addresses assigned to given ethernet port.
 
+* **Introduced GPU device class with first features:**
+
+  * Device information
+
 * **Added new RSS offload types for IPv4/L4 checksum in RSS flow.**
 
   Added macros ETH_RSS_IPV4_CHKSUM and ETH_RSS_L4_CHKSUM, now IPv4 and
drivers/gpu/meson.build new file +4 -0
new file mode 100644
index 0000000000..e51ad3381b
@@ -0,0 +1,4 @@
+# SPDX-License-Identifier: BSD-3-Clause
+# Copyright (c) 2021 NVIDIA Corporation & Affiliates
+
+drivers = []
drivers/meson.build +1 -0
index 34c0276487..d5f4e1c1f2 100644
@@ -19,6 +19,7 @@ subdirs = [
         'vdpa',           # depends on common, bus and mempool.
         'event',          # depends on common, bus, mempool and net.
         'baseband',       # depends on common and bus.
+        'gpu',            # depends on common and bus.
 ]
 
 if meson.is_cross_build()
lib/gpudev/gpudev.c new file +249 -0

249 lines not on this page · raw message

lib/gpudev/gpudev_driver.h new file +67 -0

67 lines not on this page · raw message

lib/gpudev/meson.build new file +10 -0
new file mode 100644
index 0000000000..608154817b
@@ -0,0 +1,10 @@
+# SPDX-License-Identifier: BSD-3-Clause
+# Copyright (c) 2021 NVIDIA Corporation & Affiliates
+
+headers = files(
+        'rte_gpudev.h',
+)
+
+sources = files(
+        'gpudev.c',
+)
lib/gpudev/rte_gpudev.h new file +168 -0

168 lines not on this page · raw message

lib/gpudev/version.map new file +20 -0
new file mode 100644
index 0000000000..6ac6b327e2
@@ -0,0 +1,20 @@
+EXPERIMENTAL {
+	global:
+
+	# added in 21.11
+	rte_gpu_close;
+	rte_gpu_count_avail;
+	rte_gpu_find_next;
+	rte_gpu_info_get;
+	rte_gpu_init;
+	rte_gpu_is_valid;
+};
+
+INTERNAL {
+	global:
+
+	rte_gpu_allocate;
+	rte_gpu_complete_new;
+	rte_gpu_get_by_name;
+	rte_gpu_release;
+};
lib/meson.build +1 -0
index 499d26060f..8537a5ab80 100644
@@ -34,6 +34,7 @@ libraries = [
         'distributor',
         'efd',
         'eventdev',
+        'gpudev',
         'gro',
         'gso',
         'ip_frag',
Signature · 1 line
2.17.1
#109 lore raw

PATCH v4 2/9 gpudev: add event notification

In reply to [email protected]

From: Thomas Monjalon <[email protected]>

Callback functions may be registered for a device event. Callback management is per-process and not thread-safe.

The events RTE_GPU_EVENT_NEW and RTE_GPU_EVENT_DEL are notified respectively after creation and before removal of a device, as part of the library functions. Some future events may be emitted from drivers.

---
 lib/gpudev/gpudev.c        | 148 +++++++++++++++++++++++++++++++++++++
 lib/gpudev/gpudev_driver.h |   7 ++
 lib/gpudev/rte_gpudev.h    |  70 ++++++++++++++++++
 lib/gpudev/version.map     |   3 +
 4 files changed, 228 insertions(+)
lib/gpudev/gpudev.c +148 -0
index c839c530c8..d57e23df7c 100644
@@ -3,6 +3,7 @@
  */
 
 #include <rte_eal.h>
+#include <rte_tailq.h>
 #include <rte_string_fns.h>
 #include <rte_errno.h>
 #include <rte_log.h>
@@ -27,6 +28,16 @@ static int16_t gpu_max;
 /* Number of currently valid devices */
 static int16_t gpu_count;
 
+/* Event callback object */
+struct rte_gpu_callback {
+	TAILQ_ENTRY(rte_gpu_callback) next;
+	rte_gpu_callback_t *function;
+	void *user_data;
+	enum rte_gpu_event event;
+};
+static rte_rwlock_t gpu_callback_lock = RTE_RWLOCK_INITIALIZER;
+static void gpu_free_callbacks(struct rte_gpu *dev);
+
 int
 rte_gpu_init(size_t dev_max)
 {
@@ -166,6 +177,7 @@ rte_gpu_allocate(const char *name)
 	dev->info.name = dev->name;
 	dev->info.dev_id = dev_id;
 	dev->info.numa_node = -1;
+	TAILQ_INIT(&dev->callbacks);
 
 	gpu_count++;
 	GPU_LOG(DEBUG, "new device %s (id %d) of total %d",
@@ -180,6 +192,8 @@ rte_gpu_complete_new(struct rte_gpu *dev)
 		return;
 
 	dev->state = RTE_GPU_STATE_INITIALIZED;
+	dev->state = RTE_GPU_STATE_INITIALIZED;
+	rte_gpu_notify(dev, RTE_GPU_EVENT_NEW);
 }
 
 int
@@ -192,6 +206,9 @@ rte_gpu_release(struct rte_gpu *dev)
 
 	GPU_LOG(DEBUG, "free device %s (id %d)",
 			dev->info.name, dev->info.dev_id);
+	rte_gpu_notify(dev, RTE_GPU_EVENT_DEL);
+
+	gpu_free_callbacks(dev);
 	dev->state = RTE_GPU_STATE_UNUSED;
 	gpu_count--;
 
@@ -224,6 +241,137 @@ rte_gpu_close(int16_t dev_id)
 	return firsterr;
 }
 
+int
+rte_gpu_callback_register(int16_t dev_id, enum rte_gpu_event event,
+		rte_gpu_callback_t *function, void *user_data)
+{
+	int16_t next_dev, last_dev;
+	struct rte_gpu_callback_list *callbacks;
+	struct rte_gpu_callback *callback;
+
+	if (!rte_gpu_is_valid(dev_id) && dev_id != RTE_GPU_ID_ANY) {
+		GPU_LOG(ERR, "register callback of invalid ID %d", dev_id);
+		rte_errno = ENODEV;
+		return -rte_errno;
+	}
+	if (function == NULL) {
+		GPU_LOG(ERR, "cannot register callback without function");
+		rte_errno = EINVAL;
+		return -rte_errno;
+	}
+
+	if (dev_id == RTE_GPU_ID_ANY) {
+		next_dev = 0;
+		last_dev = gpu_max - 1;
+	} else {
+		next_dev = last_dev = dev_id;
+	}
+
+	rte_rwlock_write_lock(&gpu_callback_lock);
+	do {
+		callbacks = &gpus[next_dev].callbacks;
+
+		/* check if not already registered */
+		TAILQ_FOREACH(callback, callbacks, next) {
+			if (callback->event == event &&
+					callback->function == function &&
+					callback->user_data == user_data) {
+				GPU_LOG(INFO, "callback already registered");
+				return 0;
+			}
+		}
+
+		callback = malloc(sizeof(*callback));
+		if (callback == NULL) {
+			GPU_LOG(ERR, "cannot allocate callback");
+			return -ENOMEM;
+		}
+		callback->function = function;
+		callback->user_data = user_data;
+		callback->event = event;
+		TAILQ_INSERT_TAIL(callbacks, callback, next);
+
+	} while (++next_dev <= last_dev);
+	rte_rwlock_write_unlock(&gpu_callback_lock);
+
+	return 0;
+}
+
+int
+rte_gpu_callback_unregister(int16_t dev_id, enum rte_gpu_event event,
+		rte_gpu_callback_t *function, void *user_data)
+{
+	int16_t next_dev, last_dev;
+	struct rte_gpu_callback_list *callbacks;
+	struct rte_gpu_callback *callback, *nextcb;
+
+	if (!rte_gpu_is_valid(dev_id) && dev_id != RTE_GPU_ID_ANY) {
+		GPU_LOG(ERR, "unregister callback of invalid ID %d", dev_id);
+		rte_errno = ENODEV;
+		return -rte_errno;
+	}
+	if (function == NULL) {
+		GPU_LOG(ERR, "cannot unregister callback without function");
+		rte_errno = EINVAL;
+		return -rte_errno;
+	}
+
+	if (dev_id == RTE_GPU_ID_ANY) {
+		next_dev = 0;
+		last_dev = gpu_max - 1;
+	} else {
+		next_dev = last_dev = dev_id;
+	}
+
+	rte_rwlock_write_lock(&gpu_callback_lock);
+	do {
+		callbacks = &gpus[next_dev].callbacks;
+		RTE_TAILQ_FOREACH_SAFE(callback, callbacks, next, nextcb) {
+			if (callback->event != event ||
+					callback->function != function ||
+					(callback->user_data != user_data &&
+					user_data != (void *)-1))
+				continue;
+			TAILQ_REMOVE(callbacks, callback, next);
+			free(callback);
+		}
+	} while (++next_dev <= last_dev);
+	rte_rwlock_write_unlock(&gpu_callback_lock);
+
+	return 0;
+}
+
+static void
+gpu_free_callbacks(struct rte_gpu *dev)
+{
+	struct rte_gpu_callback_list *callbacks;
+	struct rte_gpu_callback *callback, *nextcb;
+
+	callbacks = &dev->callbacks;
+	rte_rwlock_write_lock(&gpu_callback_lock);
+	RTE_TAILQ_FOREACH_SAFE(callback, callbacks, next, nextcb) {
+		TAILQ_REMOVE(callbacks, callback, next);
+		free(callback);
+	}
+	rte_rwlock_write_unlock(&gpu_callback_lock);
+}
+
+void
+rte_gpu_notify(struct rte_gpu *dev, enum rte_gpu_event event)
+{
+	int16_t dev_id;
+	struct rte_gpu_callback *callback;
+
+	dev_id = dev->info.dev_id;
+	rte_rwlock_read_lock(&gpu_callback_lock);
+	TAILQ_FOREACH(callback, &dev->callbacks, next) {
+		if (callback->event != event || callback->function == NULL)
+			continue;
+		callback->function(dev_id, event, callback->user_data);
+	}
+	rte_rwlock_read_unlock(&gpu_callback_lock);
+}
+
 int
 rte_gpu_info_get(int16_t dev_id, struct rte_gpu_info *info)
 {
lib/gpudev/gpudev_driver.h +7 -0
index 9e096e3b64..2a7089aa52 100644
@@ -12,6 +12,7 @@
 #define RTE_GPUDEV_DRIVER_H
 
 #include <stdint.h>
+#include <sys/queue.h>
 
 #include <rte_dev.h>
 
@@ -43,6 +44,8 @@ struct rte_gpu {
 	struct rte_gpu_info info;
 	/* Driver functions. */
 	struct rte_gpu_ops ops;
+	/* Event callback list. */
+	TAILQ_HEAD(rte_gpu_callback_list, rte_gpu_callback) callbacks;
 	/* Current state (used or not) in the running process. */
 	enum rte_gpu_state state; /* Updated by this library. */
 	/* Driver-specific private data for the running process. */
@@ -64,4 +67,8 @@ void rte_gpu_complete_new(struct rte_gpu *dev);
 __rte_internal
 int rte_gpu_release(struct rte_gpu *dev);
 
+/* Call registered callbacks. No multi-process event. */
+__rte_internal
+void rte_gpu_notify(struct rte_gpu *dev, enum rte_gpu_event);
+
 #endif /* RTE_GPUDEV_DRIVER_H */
lib/gpudev/rte_gpudev.h +70 -0

88 lines not on this page · raw message

lib/gpudev/version.map +3 -0
index 6ac6b327e2..b3b6b76c1c 100644
@@ -2,6 +2,8 @@ EXPERIMENTAL {
 	global:
 
 	# added in 21.11
+	rte_gpu_callback_register;
+	rte_gpu_callback_unregister;
 	rte_gpu_close;
 	rte_gpu_count_avail;
 	rte_gpu_find_next;
@@ -16,5 +18,6 @@ INTERNAL {
 	rte_gpu_allocate;
 	rte_gpu_complete_new;
 	rte_gpu_get_by_name;
+	rte_gpu_notify;
 	rte_gpu_release;
 };
Signature · 1 line
2.17.1
#110 lore raw

PATCH v4 3/9 gpudev: add child device representing a device context

In reply to [email protected]

From: Thomas Monjalon <[email protected]>

The computing device may operate in some isolated contexts. Memory and processing are isolated in a silo represented by a child device.
The context is provided as an opaque by the caller of rte_gpu_add_child().

---
 doc/guides/prog_guide/gpudev.rst | 12 ++++++
 lib/gpudev/gpudev.c              | 45 +++++++++++++++++++-
 lib/gpudev/gpudev_driver.h       |  2 +-
 lib/gpudev/rte_gpudev.h          | 71 +++++++++++++++++++++++++++++---
 lib/gpudev/version.map           |  1 +
 5 files changed, 123 insertions(+), 8 deletions(-)
doc/guides/prog_guide/gpudev.rst +12 -0
index 6ea7239159..7694639489 100644
@@ -34,3 +34,15 @@ This library provides a number of features:
 
 API Overview
 ------------
+
+Child Device
+~~~~~~~~~~~~
+
+By default, DPDK PCIe module detects and registers physical GPU devices
+in the system.
+With the gpudev library is also possible to add additional non-physical devices
+through an ``uint64_t`` generic handler (e.g. CUDA Driver context)
+that will be registered internally by the driver as an additional device (child)
+connected to a physical device (parent).
+Each device (parent or child) is represented through a ID
+required to indicate which device a given operation should be executed on.
lib/gpudev/gpudev.c +43 -2

78 lines not on this page · raw message

lib/gpudev/gpudev_driver.h +1 -1
index 2a7089aa52..4d0077161c 100644
@@ -31,7 +31,7 @@ typedef int (rte_gpu_info_get_t)(struct rte_gpu *dev, struct rte_gpu_info *info)
 struct rte_gpu_ops {
 	/* Get device info. If NULL, info is just copied. */
 	rte_gpu_info_get_t *dev_info_get;
-	/* Close device. */
+	/* Close device or child context. */
 	rte_gpu_close_t *dev_close;
 };
 
lib/gpudev/rte_gpudev.h +66 -5

108 lines not on this page · raw message

lib/gpudev/version.map +1 -0
index b3b6b76c1c..4a934ed933 100644
@@ -2,6 +2,7 @@ EXPERIMENTAL {
 	global:
 
 	# added in 21.11
+	rte_gpu_add_child;
 	rte_gpu_callback_register;
 	rte_gpu_callback_unregister;
 	rte_gpu_close;
Signature · 1 line
2.17.1
#111 lore raw

PATCH v4 4/9 gpudev: support multi-process

In reply to [email protected]

From: Thomas Monjalon <[email protected]>

The device data shared between processes are moved in a struct allocated in a shared memory (a new memzone for all GPUs). The main struct rte_gpu references the shared memory via the pointer mpshared.

The API function rte_gpu_attach() is added to attach a device from the secondary process.
The function rte_gpu_allocate() can be used only by primary process.

---
 lib/gpudev/gpudev.c        | 127 +++++++++++++++++++++++++++++++------
 lib/gpudev/gpudev_driver.h |  25 ++++++--
 lib/gpudev/version.map     |   1 +
 3 files changed, 127 insertions(+), 26 deletions(-)
lib/gpudev/gpudev.c +107 -20
index 74cdd7f20b..f0690cf730 100644
@@ -5,6 +5,7 @@
 #include <rte_eal.h>
 #include <rte_tailq.h>
 #include <rte_string_fns.h>
+#include <rte_memzone.h>
 #include <rte_errno.h>
 #include <rte_log.h>
 
@@ -28,6 +29,12 @@ static int16_t gpu_max;
 /* Number of currently valid devices */
 static int16_t gpu_count;
 
+/* Shared memory between processes. */
+static const char *GPU_MEMZONE = "rte_gpu_shared";
+static struct {
+	__extension__ struct rte_gpu_mpshared gpus[0];
+} *gpu_shared_mem;
+
 /* Event callback object */
 struct rte_gpu_callback {
 	TAILQ_ENTRY(rte_gpu_callback) next;
@@ -75,7 +82,7 @@ bool
 rte_gpu_is_valid(int16_t dev_id)
 {
 	if (dev_id >= 0 && dev_id < gpu_max &&
-		gpus[dev_id].state == RTE_GPU_STATE_INITIALIZED)
+		gpus[dev_id].process_state == RTE_GPU_STATE_INITIALIZED)
 		return true;
 	return false;
 }
@@ -85,7 +92,7 @@ gpu_match_parent(int16_t dev_id, int16_t parent)
 {
 	if (parent == RTE_GPU_ID_ANY)
 		return true;
-	return gpus[dev_id].info.parent == parent;
+	return gpus[dev_id].mpshared->info.parent == parent;
 }
 
 int16_t
@@ -94,7 +101,7 @@ rte_gpu_find_next(int16_t dev_id, int16_t parent)
 	if (dev_id < 0)
 		dev_id = 0;
 	while (dev_id < gpu_max &&
-			(gpus[dev_id].state == RTE_GPU_STATE_UNUSED ||
+			(gpus[dev_id].process_state == RTE_GPU_STATE_UNUSED ||
 			!gpu_match_parent(dev_id, parent)))
 		dev_id++;
 
@@ -109,7 +116,7 @@ gpu_find_free_id(void)
 	int16_t dev_id;
 
 	for (dev_id = 0; dev_id < gpu_max; dev_id++) {
-		if (gpus[dev_id].state == RTE_GPU_STATE_UNUSED)
+		if (gpus[dev_id].process_state == RTE_GPU_STATE_UNUSED)
 			return dev_id;
 	}
 	return RTE_GPU_ID_NONE;
@@ -136,12 +143,35 @@ rte_gpu_get_by_name(const char *name)
 
 	RTE_GPU_FOREACH(dev_id) {
 		dev = &gpus[dev_id];
-		if (strncmp(name, dev->name, RTE_DEV_NAME_MAX_LEN) == 0)
+		if (strncmp(name, dev->mpshared->name, RTE_DEV_NAME_MAX_LEN) == 0)
 			return dev;
 	}
 	return NULL;
 }
 
+static int
+gpu_shared_mem_init(void)
+{
+	const struct rte_memzone *memzone;
+
+	if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
+		memzone = rte_memzone_reserve(GPU_MEMZONE,
+				sizeof(*gpu_shared_mem) +
+				sizeof(*gpu_shared_mem->gpus) * gpu_max,
+				SOCKET_ID_ANY, 0);
+	} else {
+		memzone = rte_memzone_lookup(GPU_MEMZONE);
+	}
+	if (memzone == NULL) {
+		GPU_LOG(ERR, "cannot initialize shared memory");
+		rte_errno = ENOMEM;
+		return -rte_errno;
+	}
+
+	gpu_shared_mem = memzone->addr;
+	return 0;
+}
+
 struct rte_gpu *
 rte_gpu_allocate(const char *name)
 {
@@ -163,6 +193,10 @@ rte_gpu_allocate(const char *name)
 	if (gpus == NULL && rte_gpu_init(RTE_GPU_DEFAULT_MAX) < 0)
 		return NULL;
 
+	/* initialize shared memory before adding first device */
+	if (gpu_shared_mem == NULL && gpu_shared_mem_init() < 0)
+		return NULL;
+
 	if (rte_gpu_get_by_name(name) != NULL) {
 		GPU_LOG(ERR, "device with name %s already exists", name);
 		rte_errno = EEXIST;
@@ -178,16 +212,20 @@ rte_gpu_allocate(const char *name)
 	dev = &gpus[dev_id];
 	memset(dev, 0, sizeof(*dev));
 
-	if (rte_strscpy(dev->name, name, RTE_DEV_NAME_MAX_LEN) < 0) {
+	dev->mpshared = &gpu_shared_mem->gpus[dev_id];
+	memset(dev->mpshared, 0, sizeof(*dev->mpshared));
+
+	if (rte_strscpy(dev->mpshared->name, name, RTE_DEV_NAME_MAX_LEN) < 0) {
 		GPU_LOG(ERR, "device name too long: %s", name);
 		rte_errno = ENAMETOOLONG;
 		return NULL;
 	}
-	dev->info.name = dev->name;
-	dev->info.dev_id = dev_id;
-	dev->info.numa_node = -1;
-	dev->info.parent = RTE_GPU_ID_NONE;
+	dev->mpshared->info.name = dev->mpshared->name;
+	dev->mpshared->info.dev_id = dev_id;
+	dev->mpshared->info.numa_node = -1;
+	dev->mpshared->info.parent = RTE_GPU_ID_NONE;
 	TAILQ_INIT(&dev->callbacks);
+	__atomic_fetch_add(&dev->mpshared->process_refcnt, 1, __ATOMIC_RELAXED);
 
 	gpu_count++;
 	GPU_LOG(DEBUG, "new device %s (id %d) of total %d",
@@ -195,6 +233,55 @@ rte_gpu_allocate(const char *name)
 	return dev;
 }
 
+struct rte_gpu *
+rte_gpu_attach(const char *name)
+{
+	int16_t dev_id;
+	struct rte_gpu *dev;
+	struct rte_gpu_mpshared *shared_dev;
+
+	if (rte_eal_process_type() != RTE_PROC_SECONDARY) {
+		GPU_LOG(ERR, "only secondary process can attach device");
+		rte_errno = EPERM;
+		return NULL;
+	}
+	if (name == NULL) {
+		GPU_LOG(ERR, "attach device without a name");
+		rte_errno = EINVAL;
+		return NULL;
+	}
+
+	/* implicit initialization of library before adding first device */
+	if (gpus == NULL && rte_gpu_init(RTE_GPU_DEFAULT_MAX) < 0)
+		return NULL;
+
+	/* initialize shared memory before adding first device */
+	if (gpu_shared_mem == NULL && gpu_shared_mem_init() < 0)
+		return NULL;
+
+	for (dev_id = 0; dev_id < gpu_max; dev_id++) {
+		shared_dev = &gpu_shared_mem->gpus[dev_id];
+		if (strncmp(name, shared_dev->name, RTE_DEV_NAME_MAX_LEN) == 0)
+			break;
+	}
+	if (dev_id >= gpu_max) {
+		GPU_LOG(ERR, "device with name %s not found", name);
+		rte_errno = ENOENT;
+		return NULL;
+	}
+	dev = &gpus[dev_id];
+	memset(dev, 0, sizeof(*dev));
+
+	TAILQ_INIT(&dev->callbacks);
+	dev->mpshared = shared_dev;
+	__atomic_fetch_add(&dev->mpshared->process_refcnt, 1, __ATOMIC_RELAXED);
+
+	gpu_count++;
+	GPU_LOG(DEBUG, "attached device %s (id %d) of total %d",
+			name, dev_id, gpu_count);
+	return dev;
+}
+
 int16_t
 rte_gpu_add_child(const char *name, int16_t parent, uint64_t child_context)
 {
@@ -210,11 +297,11 @@ rte_gpu_add_child(const char *name, int16_t parent, uint64_t child_context)
 	if (dev == NULL)
 		return -rte_errno;
 
-	dev->info.parent = parent;
-	dev->info.context = child_context;
+	dev->mpshared->info.parent = parent;
+	dev->mpshared->info.context = child_context;
 
 	rte_gpu_complete_new(dev);
-	return dev->info.dev_id;
+	return dev->mpshared->info.dev_id;
 }
 
 void
@@ -223,8 +310,7 @@ rte_gpu_complete_new(struct rte_gpu *dev)
 	if (dev == NULL)
 		return;
 
-	dev->state = RTE_GPU_STATE_INITIALIZED;
-	dev->state = RTE_GPU_STATE_INITIALIZED;
+	dev->process_state = RTE_GPU_STATE_INITIALIZED;
 	rte_gpu_notify(dev, RTE_GPU_EVENT_NEW);
 }
 
@@ -237,7 +323,7 @@ rte_gpu_release(struct rte_gpu *dev)
 		rte_errno = ENODEV;
 		return -rte_errno;
 	}
-	dev_id = dev->info.dev_id;
+	dev_id = dev->mpshared->info.dev_id;
 	RTE_GPU_FOREACH_CHILD(child, dev_id) {
 		GPU_LOG(ERR, "cannot release device %d with child %d",
 				dev_id, child);
@@ -246,11 +332,12 @@ rte_gpu_release(struct rte_gpu *dev)
 	}
 
 	GPU_LOG(DEBUG, "free device %s (id %d)",
-			dev->info.name, dev->info.dev_id);
+			dev->mpshared->info.name, dev->mpshared->info.dev_id);
 	rte_gpu_notify(dev, RTE_GPU_EVENT_DEL);
 
 	gpu_free_callbacks(dev);
-	dev->state = RTE_GPU_STATE_UNUSED;
+	dev->process_state = RTE_GPU_STATE_UNUSED;
+	__atomic_fetch_sub(&dev->mpshared->process_refcnt, 1, __ATOMIC_RELAXED);
 	gpu_count--;
 
 	return 0;
@@ -403,7 +490,7 @@ rte_gpu_notify(struct rte_gpu *dev, enum rte_gpu_event event)
 	int16_t dev_id;
 	struct rte_gpu_callback *callback;
 
-	dev_id = dev->info.dev_id;
+	dev_id = dev->mpshared->info.dev_id;
 	rte_rwlock_read_lock(&gpu_callback_lock);
 	TAILQ_FOREACH(callback, &dev->callbacks, next) {
 		if (callback->event != event || callback->function == NULL)
@@ -431,7 +518,7 @@ rte_gpu_info_get(int16_t dev_id, struct rte_gpu_info *info)
 	}
 
 	if (dev->ops.dev_info_get == NULL) {
-		*info = dev->info;
+		*info = dev->mpshared->info;
 		return 0;
 	}
 	return GPU_DRV_RET(dev->ops.dev_info_get(dev, info));
lib/gpudev/gpudev_driver.h +19 -6
index 4d0077161c..9459c7e30f 100644
@@ -35,19 +35,28 @@ struct rte_gpu_ops {
 	rte_gpu_close_t *dev_close;
 };
 
-struct rte_gpu {
-	/* Backing device. */
-	struct rte_device *device;
+struct rte_gpu_mpshared {
 	/* Unique identifier name. */
 	char name[RTE_DEV_NAME_MAX_LEN]; /* Updated by this library. */
+	/* Driver-specific private data shared in multi-process. */
+	void *dev_private;
 	/* Device info structure. */
 	struct rte_gpu_info info;
+	/* Counter of processes using the device. */
+	uint16_t process_refcnt; /* Updated by this library. */
+};
+
+struct rte_gpu {
+	/* Backing device. */
+	struct rte_device *device;
+	/* Data shared between processes. */
+	struct rte_gpu_mpshared *mpshared;
 	/* Driver functions. */
 	struct rte_gpu_ops ops;
 	/* Event callback list. */
 	TAILQ_HEAD(rte_gpu_callback_list, rte_gpu_callback) callbacks;
 	/* Current state (used or not) in the running process. */
-	enum rte_gpu_state state; /* Updated by this library. */
+	enum rte_gpu_state process_state; /* Updated by this library. */
 	/* Driver-specific private data for the running process. */
 	void *process_private;
 } __rte_cache_aligned;
@@ -55,15 +64,19 @@ struct rte_gpu {
 __rte_internal
 struct rte_gpu *rte_gpu_get_by_name(const char *name);
 
-/* First step of initialization */
+/* First step of initialization in primary process. */
 __rte_internal
 struct rte_gpu *rte_gpu_allocate(const char *name);
 
+/* First step of initialization in secondary process. */
+__rte_internal
+struct rte_gpu *rte_gpu_attach(const char *name);
+
 /* Last step of initialization. */
 __rte_internal
 void rte_gpu_complete_new(struct rte_gpu *dev);
 
-/* Last step of removal. */
+/* Last step of removal (primary or secondary process). */
 __rte_internal
 int rte_gpu_release(struct rte_gpu *dev);
 
lib/gpudev/version.map +1 -0
index 4a934ed933..58dc632393 100644
@@ -17,6 +17,7 @@ INTERNAL {
 	global:
 
 	rte_gpu_allocate;
+	rte_gpu_attach;
 	rte_gpu_complete_new;
 	rte_gpu_get_by_name;
 	rte_gpu_notify;
Signature · 1 line
2.17.1
#112 lore raw

PATCH v4 5/9 gpudev: add memory API

In reply to [email protected]

From: Elena Agostini <[email protected]>

In heterogeneous computing system, processing is not only in the CPU. Some tasks can be delegated to devices working in parallel. Such workload distribution can be achieved by sharing some memory.

As a first step, the features are focused on memory management. A function allows to allocate memory inside the device, or in the main (CPU) memory while making it visible for the device. This memory may be used to save packets or for synchronization data.

The next step should focus on GPU processing task control.

---
 app/test-gpudev/main.c                 | 118 +++++++++++++++++++++++++
 doc/guides/gpus/features/default.ini   |   3 +
 doc/guides/prog_guide/gpudev.rst       |  19 ++++
 doc/guides/rel_notes/release_21_11.rst |   1 +
 lib/gpudev/gpudev.c                    | 101 +++++++++++++++++++++
 lib/gpudev/gpudev_driver.h             |  12 +++
 lib/gpudev/rte_gpudev.h                |  95 ++++++++++++++++++++
 lib/gpudev/version.map                 |   4 +
 8 files changed, 353 insertions(+)
app/test-gpudev/main.c +118 -0
index 6a73a54e84..98c02a3ee0 100644
@@ -62,6 +62,110 @@ args_parse(int argc, char **argv)
 	}
 }
 
+static int
+alloc_gpu_memory(uint16_t gpu_id)
+{
+	void * ptr_1 = NULL;
+	void * ptr_2 = NULL;
+	size_t buf_bytes = 1024;
+	int ret = 0;
+
+	printf("\n=======> TEST: Allocate GPU memory\n");
+
+	/* Alloc memory on GPU 0 */
+	ptr_1 = rte_gpu_malloc(gpu_id, buf_bytes);
+	if(ptr_1 == NULL)
+	{
+		fprintf(stderr, "rte_gpu_malloc GPU memory returned error\n");
+		return -1;
+	}
+	printf("GPU memory allocated at 0x%p %zdB\n", ptr_1, buf_bytes);
+
+	ptr_2 = rte_gpu_malloc(gpu_id, buf_bytes);
+	if(ptr_2 == NULL)
+	{
+		fprintf(stderr, "rte_gpu_malloc GPU memory returned error\n");
+		return -1;
+	}
+	printf("GPU memory allocated at 0x%p %zdB\n", ptr_2, buf_bytes);
+
+	ret = rte_gpu_free(gpu_id, (uint8_t*)(ptr_1)+0x700);
+	if(ret < 0)
+	{
+		printf("GPU memory 0x%p + 0x700 NOT freed because of memory address not recognized by driver\n", ptr_1);
+	}
+	else
+	{
+		fprintf(stderr, "rte_gpu_free erroneusly freed GPU memory 0x%p + 0x700\n", ptr_1);
+		return -1;
+	}
+
+	ret = rte_gpu_free(gpu_id, ptr_2);
+	if(ret < 0)
+	{
+		fprintf(stderr, "rte_gpu_free returned error %d\n", ret);
+		return -1;
+	}
+	printf("GPU memory 0x%p freed\n", ptr_2);
+
+	ret = rte_gpu_free(gpu_id, ptr_1);
+	if(ret < 0)
+	{
+		fprintf(stderr, "rte_gpu_free returned error %d\n", ret);
+		return -1;
+	}
+	printf("GPU memory 0x%p freed\n", ptr_1);
+
+	return 0;
+}
+
+static int
+register_cpu_memory(uint16_t gpu_id)
+{
+	void * ptr = NULL;
+	size_t buf_bytes = 1024;
+	int ret = 0;
+
+	printf("\n=======> TEST: Register CPU memory\n");
+
+	/* Alloc memory on CPU visible from GPU 0 */
+	ptr = rte_zmalloc(NULL, buf_bytes, 0);
+	if (ptr == NULL) {
+		fprintf(stderr, "Failed to allocate CPU memory.\n");
+		return -1;
+	}
+
+	ret = rte_gpu_register(gpu_id, buf_bytes, ptr);
+	if(ret < 0)
+	{
+		fprintf(stderr, "rte_gpu_register CPU memory returned error %d\n", ret);
+		return -1;
+	}
+	printf("CPU memory registered at 0x%p %zdB\n", ptr, buf_bytes);
+
+	ret = rte_gpu_unregister(gpu_id, (uint8_t*)(ptr)+0x700);
+	if(ret < 0)
+	{
+		printf("CPU memory 0x%p + 0x700 NOT unregistered because of memory address not recognized by driver\n", ptr);
+	}
+	else
+	{
+		fprintf(stderr, "rte_gpu_free erroneusly freed GPU memory 0x%p + 0x700\n", ptr);
+		return -1;
+	}
+	printf("CPU memory 0x%p unregistered\n", ptr);
+
+	ret = rte_gpu_unregister(gpu_id, ptr);
+	if(ret < 0)
+	{
+		fprintf(stderr, "rte_gpu_unregister returned error %d\n", ret);
+		return -1;
+	}
+	printf("CPU memory 0x%p unregistered\n", ptr);
+
+	return 0;
+}
+
 int
 main(int argc, char **argv)
 {
@@ -99,6 +203,20 @@ main(int argc, char **argv)
 	}
 	printf("\n\n");
 
+	if(nb_gpus == 0)
+	{
+		fprintf(stderr, "Need at least one GPU on the system to run the example\n");
+		return EXIT_FAILURE;
+	}
+
+	gpu_id = 0;
+
+	/**
+	 * Memory tests
+	 */
+	alloc_gpu_memory(gpu_id);
+	register_cpu_memory(gpu_id);
+
 	/* clean up the EAL */
 	rte_eal_cleanup();
 	printf("Bye...\n");
doc/guides/gpus/features/default.ini +3 -0
index ec7a545eb7..87e9966424 100644
@@ -8,3 +8,6 @@
 ;
 [Features]
 Get device info                =
+Share CPU memory with device   =
+Allocate device memory         =
+Free memory                    =
doc/guides/prog_guide/gpudev.rst +19 -0
index 7694639489..9aca69038c 100644
@@ -30,6 +30,8 @@ Features
 This library provides a number of features:
 
 - Interoperability with device-specific library through generic handlers.
+- Allocate and free memory on the device.
+- Register CPU memory to make it visible from the device.
 
 
 API Overview
@@ -46,3 +48,20 @@ that will be registered internally by the driver as an additional device (child)
 connected to a physical device (parent).
 Each device (parent or child) is represented through a ID
 required to indicate which device a given operation should be executed on.
+
+Memory Allocation
+~~~~~~~~~~~~~~~~~
+
+gpudev can allocate on an input given GPU device a memory area
+returning the pointer to that memory.
+Later, it's also possible to free that memory with gpudev.
+GPU memory allocated outside of the gpudev library
+(e.g. with GPU-specific library) cannot be freed by the gpudev library.
+
+Memory Registration
+~~~~~~~~~~~~~~~~~~~
+
+gpudev can register a CPU memory area to make it visible from a GPU device.
+Later, it's also possible to unregister that memory with gpudev.
+CPU memory registered outside of the gpudev library
+(e.g. with GPU specific library) cannot be unregistered by the gpudev library.
doc/guides/rel_notes/release_21_11.rst +1 -0
index 57851c63af..f70680dad3 100644
@@ -94,6 +94,7 @@ New Features
 * **Introduced GPU device class with first features:**
 
   * Device information
+  * Memory management
 
 * **Added new RSS offload types for IPv4/L4 checksum in RSS flow.**
 
lib/gpudev/gpudev.c +101 -0

110 lines not on this page · raw message

lib/gpudev/gpudev_driver.h +12 -0
index 9459c7e30f..11015944a6 100644
@@ -27,12 +27,24 @@ enum rte_gpu_state {
 struct rte_gpu;
 typedef int (rte_gpu_close_t)(struct rte_gpu *dev);
 typedef int (rte_gpu_info_get_t)(struct rte_gpu *dev, struct rte_gpu_info *info);
+typedef int (rte_gpu_mem_alloc_t)(struct rte_gpu *dev, size_t size, void **ptr);
+typedef int (rte_gpu_free_t)(struct rte_gpu *dev, void *ptr);
+typedef int (rte_gpu_mem_register_t)(struct rte_gpu *dev, size_t size, void *ptr);
+typedef int (rte_gpu_mem_unregister_t)(struct rte_gpu *dev, void *ptr);
 
 struct rte_gpu_ops {
 	/* Get device info. If NULL, info is just copied. */
 	rte_gpu_info_get_t *dev_info_get;
 	/* Close device or child context. */
 	rte_gpu_close_t *dev_close;
+	/* Allocate memory in device. */
+	rte_gpu_mem_alloc_t *mem_alloc;
+	/* Register CPU memory in device. */
+	rte_gpu_mem_register_t *mem_register;
+	/* Free memory allocated or registered in device. */
+	rte_gpu_free_t *mem_free;
+	/* Unregister CPU memory in device. */
+	rte_gpu_mem_unregister_t *mem_unregister;
 };
 
 struct rte_gpu_mpshared {
lib/gpudev/rte_gpudev.h +95 -0

107 lines not on this page · raw message

lib/gpudev/version.map +4 -0
index 58dc632393..d4a65ebd52 100644
@@ -8,9 +8,13 @@ EXPERIMENTAL {
 	rte_gpu_close;
 	rte_gpu_count_avail;
 	rte_gpu_find_next;
+	rte_gpu_free;
 	rte_gpu_info_get;
 	rte_gpu_init;
 	rte_gpu_is_valid;
+	rte_gpu_malloc;
+	rte_gpu_register;
+	rte_gpu_unregister;
 };
 
 INTERNAL {
Signature · 1 line
2.17.1
#113 lore raw

PATCH v4 6/9 gpudev: add memory barrier

In reply to [email protected]

From: Elena Agostini <[email protected]>

Add a function for the application to ensure the coherency of the writes executed by another device into the GPU memory.

---
 doc/guides/prog_guide/gpudev.rst |  8 ++++++++
 lib/gpudev/gpudev.c              | 19 +++++++++++++++++++
 lib/gpudev/gpudev_driver.h       |  3 +++
 lib/gpudev/rte_gpudev.h          | 18 ++++++++++++++++++
 lib/gpudev/version.map           |  1 +
 5 files changed, 49 insertions(+)
doc/guides/prog_guide/gpudev.rst +8 -0
index 9aca69038c..eb5f0af817 100644
@@ -65,3 +65,11 @@ gpudev can register a CPU memory area to make it visible from a GPU device.
 Later, it's also possible to unregister that memory with gpudev.
 CPU memory registered outside of the gpudev library
 (e.g. with GPU specific library) cannot be unregistered by the gpudev library.
+
+Memory Barrier
+~~~~~~~~~~~~~~
+
+Some GPU drivers may need, under certain conditions,
+to enforce the coherency of external devices writes (e.g. NIC receiving packets)
+into the GPU memory.
+gpudev abstracts and exposes this capability.
lib/gpudev/gpudev.c +19 -0
index 1d8318f769..cefefd737a 100644
@@ -624,3 +624,22 @@ rte_gpu_free(int16_t dev_id, void *ptr)
 	}
 	return GPU_DRV_RET(dev->ops.mem_free(dev, ptr));
 }
+
+int
+rte_gpu_mbw(int16_t dev_id)
+{
+	struct rte_gpu *dev;
+
+	dev = gpu_get_by_id(dev_id);
+	if (dev == NULL) {
+		GPU_LOG(ERR, "memory barrier for invalid device ID %d", dev_id);
+		rte_errno = ENODEV;
+		return -rte_errno;
+	}
+
+	if (dev->ops.mbw == NULL) {
+		rte_errno = ENOTSUP;
+		return -rte_errno;
+	}
+	return GPU_DRV_RET(dev->ops.mbw(dev));
+}
lib/gpudev/gpudev_driver.h +3 -0
index 11015944a6..ab24de9e28 100644
@@ -31,6 +31,7 @@ typedef int (rte_gpu_mem_alloc_t)(struct rte_gpu *dev, size_t size, void **ptr);
 typedef int (rte_gpu_free_t)(struct rte_gpu *dev, void *ptr);
 typedef int (rte_gpu_mem_register_t)(struct rte_gpu *dev, size_t size, void *ptr);
 typedef int (rte_gpu_mem_unregister_t)(struct rte_gpu *dev, void *ptr);
+typedef int (rte_gpu_mbw_t)(struct rte_gpu *dev);
 
 struct rte_gpu_ops {
 	/* Get device info. If NULL, info is just copied. */
@@ -45,6 +46,8 @@ struct rte_gpu_ops {
 	rte_gpu_free_t *mem_free;
 	/* Unregister CPU memory in device. */
 	rte_gpu_mem_unregister_t *mem_unregister;
+	/* Enforce GPU memory write barrier. */
+	rte_gpu_mbw_t *mbw;
 };
 
 struct rte_gpu_mpshared {
lib/gpudev/rte_gpudev.h +18 -0
index 3c276581c0..e790b3e2b7 100644
@@ -387,6 +387,24 @@ int rte_gpu_register(int16_t dev_id, size_t size, void * ptr);
 __rte_experimental
 int rte_gpu_unregister(int16_t dev_id, void *ptr);
 
+/**
+ * @warning
+ * @b EXPERIMENTAL: this API may change without prior notice.
+ *
+ * Enforce a GPU memory write barrier.
+ *
+ * @param dev_id
+ *   Reference device ID.
+ *
+ * @return
+ *   0 on success, -rte_errno otherwise:
+ *   - ENODEV if invalid dev_id
+ *   - ENOTSUP if operation not supported by the driver
+ *   - EPERM if driver error
+ */
+__rte_experimental
+int rte_gpu_mbw(int16_t dev_id);
+
 #ifdef __cplusplus
 }
 #endif
lib/gpudev/version.map +1 -0
index d4a65ebd52..d72d470d8e 100644
@@ -13,6 +13,7 @@ EXPERIMENTAL {
 	rte_gpu_init;
 	rte_gpu_is_valid;
 	rte_gpu_malloc;
+	rte_gpu_mbw;
 	rte_gpu_register;
 	rte_gpu_unregister;
 };
Signature · 1 line
2.17.1
#114 lore raw

PATCH v4 7/9 gpudev: add communication flag

In reply to [email protected]

From: Elena Agostini <[email protected]>

In heterogeneous computing system, processing is not only in the CPU. Some tasks can be delegated to devices working in parallel. When mixing network activity with task processing there may be the need to put in communication the CPU with the device in order to synchronize operations.

The purpose of this flag is to allow the CPU and the GPU to exchange ACKs. A possible use-case is described below.

CPU:
- Trigger some task on the GPU
- Prepare some data
- Signal to the GPU the data is ready updating the communication flag

GPU:
- Do some pre-processing
- Wait for more data from the CPU polling on the communication flag
- Consume the data prepared by the CPU

---
 app/test-gpudev/main.c                 |  66 +++++++++++++++
 doc/guides/prog_guide/gpudev.rst       |  13 +++
 doc/guides/rel_notes/release_21_11.rst |   1 +
 lib/gpudev/gpudev.c                    |  94 +++++++++++++++++++++
 lib/gpudev/rte_gpudev.h                | 108 +++++++++++++++++++++++++
 lib/gpudev/version.map                 |   4 +
 6 files changed, 286 insertions(+)
app/test-gpudev/main.c +66 -0
index 98c02a3ee0..22f5c950b2 100644
@@ -166,6 +166,67 @@ register_cpu_memory(uint16_t gpu_id)
 	return 0;
 }
 
+static int
+create_update_comm_flag(uint16_t gpu_id)
+{
+	struct rte_gpu_comm_flag devflag;
+	int ret = 0;
+	uint32_t set_val;
+	uint32_t get_val;
+
+	printf("\n=======> TEST: Communication flag\n");
+
+	ret = rte_gpu_comm_create_flag(gpu_id, &devflag, RTE_GPU_COMM_FLAG_CPU);
+	if(ret < 0)
+	{
+		fprintf(stderr, "rte_gpu_comm_create_flag returned error %d\n", ret);
+		return -1;
+	}
+
+	set_val = 25;
+	ret = rte_gpu_comm_set_flag(&devflag, set_val);
+	if(ret < 0)
+	{
+		fprintf(stderr, "rte_gpu_comm_set_flag returned error %d\n", ret);
+		return -1;
+	}
+
+	ret = rte_gpu_comm_get_flag_value(&devflag, &get_val);
+	if(ret < 0)
+	{
+		fprintf(stderr, "rte_gpu_comm_get_flag_value returned error %d\n", ret);
+		return -1;
+	}
+
+	printf("Communication flag value at 0x%p was set to %d and current value is %d\n", devflag.ptr, set_val, get_val);
+
+	set_val = 38;
+	ret = rte_gpu_comm_set_flag(&devflag, set_val);
+	if(ret < 0)
+	{
+		fprintf(stderr, "rte_gpu_comm_set_flag returned error %d\n", ret);
+		return -1;
+	}
+
+	ret = rte_gpu_comm_get_flag_value(&devflag, &get_val);
+	if(ret < 0)
+	{
+		fprintf(stderr, "rte_gpu_comm_get_flag_value returned error %d\n", ret);
+		return -1;
+	}
+
+	printf("Communication flag value at 0x%p was set to %d and current value is %d\n", devflag.ptr, set_val, get_val);
+
+	ret = rte_gpu_comm_destroy_flag(&devflag);
+	if(ret < 0)
+	{
+		fprintf(stderr, "rte_gpu_comm_destroy_flags returned error %d\n", ret);
+		return -1;
+	}
+
+	return 0;
+}
+
 int
 main(int argc, char **argv)
 {
@@ -217,6 +278,11 @@ main(int argc, char **argv)
 	alloc_gpu_memory(gpu_id);
 	register_cpu_memory(gpu_id);
 
+	/**
+	 * Communication items test
+	 */
+	create_update_comm_flag(gpu_id);
+
 	/* clean up the EAL */
 	rte_eal_cleanup();
 	printf("Bye...\n");
doc/guides/prog_guide/gpudev.rst +13 -0
index eb5f0af817..e0db627aed 100644
@@ -32,6 +32,10 @@ This library provides a number of features:
 - Interoperability with device-specific library through generic handlers.
 - Allocate and free memory on the device.
 - Register CPU memory to make it visible from the device.
+- Communication between the CPU and the device.
+
+The whole CPU - GPU communication is implemented
+using CPU memory visible from the GPU.
 
 
 API Overview
@@ -73,3 +77,12 @@ Some GPU drivers may need, under certain conditions,
 to enforce the coherency of external devices writes (e.g. NIC receiving packets)
 into the GPU memory.
 gpudev abstracts and exposes this capability.
+
+Communication Flag
+~~~~~~~~~~~~~~~~~~
+
+Considering an application with some GPU task
+that's waiting to receive a signal from the CPU
+to move forward with the execution.
+The communication flag allocates a CPU memory GPU-visible ``uint32_t`` flag
+that can be used by the CPU to communicate with a GPU task.
doc/guides/rel_notes/release_21_11.rst +1 -0
index f70680dad3..16d10bb14c 100644
@@ -95,6 +95,7 @@ New Features
 
   * Device information
   * Memory management
+  * Communication flag
 
 * **Added new RSS offload types for IPv4/L4 checksum in RSS flow.**
 
lib/gpudev/gpudev.c +94 -0

97 lines not on this page · raw message

lib/gpudev/rte_gpudev.h +108 -0

126 lines not on this page · raw message

lib/gpudev/version.map +4 -0
index d72d470d8e..2fc039373a 100644
@@ -6,6 +6,10 @@ EXPERIMENTAL {
 	rte_gpu_callback_register;
 	rte_gpu_callback_unregister;
 	rte_gpu_close;
+	rte_gpu_comm_create_flag;
+	rte_gpu_comm_destroy_flag;
+	rte_gpu_comm_get_flag_value;
+	rte_gpu_comm_set_flag;
 	rte_gpu_count_avail;
 	rte_gpu_find_next;
 	rte_gpu_free;
Signature · 1 line
2.17.1
#115 lore raw

PATCH v4 8/9 gpudev: add communication list

In reply to [email protected]

From: Elena Agostini <[email protected]>

In heterogeneous computing system, processing is not only in the CPU. Some tasks can be delegated to devices working in parallel. When mixing network activity with task processing there may be the need to put in communication the CPU with the device in order to synchronize operations.

An example could be a receive-and-process application
where CPU is responsible for receiving packets in multiple mbufs and the GPU is responsible for processing the content of those packets.

The purpose of this list is to provide a buffer in CPU memory visible from the GPU that can be treated as a circular buffer
to let the CPU provide fondamental info of received packets to the GPU.

A possible use-case is described below.

CPU:
- Trigger some task on the GPU
- in a loop:
- receive a number of packets
- provide packets info to the GPU

GPU:
- Do some pre-processing
- Wait to receive a new set of packet to be processed

Layout of a communication list would be:

     -------
    |   0    | => pkt_list
    | status |
    | #pkts  |
     -------
    |   1    | => pkt_list
    | status |
    | #pkts  |
     -------
    |   2    | => pkt_list
    | status |
    | #pkts  |
     -------
    |  ....  | => pkt_list
     -------
---
 app/test-gpudev/main.c                 | 103 +++++++++++++++
 doc/guides/prog_guide/gpudev.rst       |  16 +++
 doc/guides/rel_notes/release_21_11.rst |   2 +-
 lib/gpudev/gpudev.c                    | 169 +++++++++++++++++++++++++
 lib/gpudev/meson.build                 |   2 +
 lib/gpudev/rte_gpudev.h                | 129 +++++++++++++++++++
 lib/gpudev/version.map                 |   4 +
 7 files changed, 424 insertions(+), 1 deletion(-)
app/test-gpudev/main.c +103 -0
index 22f5c950b2..8f7ffa4c63 100644
@@ -227,6 +227,108 @@ create_update_comm_flag(uint16_t gpu_id)
 	return 0;
 }
 
+static int
+simulate_gpu_task(struct rte_gpu_comm_list *comm_list_item, int num_pkts)
+{
+	int idx;
+
+	if(comm_list_item == NULL)
+		return -1;
+
+	for (idx = 0; idx < num_pkts; idx++) {
+		/**
+		 * consume(comm_list_item->pkt_list[idx].addr);
+		 */
+	}
+	comm_list_item->status = RTE_GPU_COMM_LIST_DONE;
+
+	return 0;
+}
+
+static int
+create_update_comm_list(uint16_t gpu_id)
+{
+	int ret = 0;
+	int i = 0;
+	struct rte_gpu_comm_list * comm_list;
+	uint32_t num_comm_items = 1024;
+	struct rte_mbuf * mbufs[10];
+
+	printf("\n=======> TEST: Communication list\n");
+
+	comm_list = rte_gpu_comm_create_list(gpu_id, num_comm_items);
+	if(comm_list == NULL)
+	{
+		fprintf(stderr, "rte_gpu_comm_create_list returned error %d\n", ret);
+		return -1;
+	}
+
+	/**
+	 * Simulate DPDK receive functions like rte_eth_rx_burst()
+	 */
+	for(i = 0; i < 10; i++)
+	{
+		mbufs[i] = rte_zmalloc(NULL, sizeof(struct rte_mbuf), 0);
+		if (mbufs[i] == NULL) {
+			fprintf(stderr, "Failed to allocate fake mbufs in CPU memory.\n");
+			return -1;
+		}
+
+		memset(mbufs[i], 0, sizeof(struct rte_mbuf));
+	}
+
+	/**
+	 * Populate just the first item of  the list
+	 */
+	ret = rte_gpu_comm_populate_list_pkts(&(comm_list[0]), mbufs, 10);
+	if(ret < 0)
+	{
+		fprintf(stderr, "rte_gpu_comm_populate_list_pkts returned error %d\n", ret);
+		return -1;
+	}
+
+	ret = rte_gpu_comm_cleanup_list(&(comm_list[0]));
+	if(ret == 0)
+	{
+		fprintf(stderr, "rte_gpu_comm_cleanup_list erroneusly cleaned the list even if packets have not beeing consumed yet\n");
+		return -1;
+	}
+	else
+	{
+		fprintf(stderr, "rte_gpu_comm_cleanup_list correctly didn't clean up the packets because they have not beeing consumed yet\n");
+	}
+
+	/**
+	 * Simulate a GPU tasks going through the packet list to consume
+	 * mbufs packets and release them
+	 */
+	simulate_gpu_task(&(comm_list[0]), 10);
+
+	/**
+	 * Packets have been consumed, now the communication item
+	 * and the related mbufs can be all released
+	 */
+	ret = rte_gpu_comm_cleanup_list(&(comm_list[0]));
+	if(ret < 0)
+	{
+		fprintf(stderr, "rte_gpu_comm_cleanup_list returned error %d\n", ret);
+		return -1;
+	}
+
+	ret = rte_gpu_comm_destroy_list(comm_list, num_comm_items);
+	if(ret < 0)
+	{
+		fprintf(stderr, "rte_gpu_comm_destroy_list returned error %d\n", ret);
+		return -1;
+	}
+
+	for(i = 0; i < 10; i++)
+		rte_free(mbufs[i]);
+
+	printf("\nCommunication list test passed!\n");
+	return 0;
+}
+
 int
 main(int argc, char **argv)
 {
@@ -282,6 +384,7 @@ main(int argc, char **argv)
 	 * Communication items test
 	 */
 	create_update_comm_flag(gpu_id);
+	create_update_comm_list(gpu_id);
 
 	/* clean up the EAL */
 	rte_eal_cleanup();
doc/guides/prog_guide/gpudev.rst +16 -0
index e0db627aed..cbaec5a1e4 100644
@@ -86,3 +86,19 @@ that's waiting to receive a signal from the CPU
 to move forward with the execution.
 The communication flag allocates a CPU memory GPU-visible ``uint32_t`` flag
 that can be used by the CPU to communicate with a GPU task.
+
+Communication list
+~~~~~~~~~~~~~~~~~~
+
+By default, DPDK pulls free mbufs from a mempool to receive packets.
+Best practice, expecially in a multithreaded application,
+is to no make any assumption on which mbufs will be used
+to receive the next bursts of packets.
+Considering an application with a GPU memory mempool
+attached to a receive queue having some task waiting on the GPU
+to receive a new burst of packets to be processed,
+there is the need to communicate from the CPU
+the list of mbuf payload addresses where received packet have been stored.
+The ``rte_gpu_comm_*()`` functions are responsible to create a list of packets
+that can be populated with receive mbuf payload addresses
+and communicated to the task running on the GPU.
doc/guides/rel_notes/release_21_11.rst +1 -1
index 16d10bb14c..8dd0982d12 100644
@@ -95,7 +95,7 @@ New Features
 
   * Device information
   * Memory management
-  * Communication flag
+  * Communication flag & list
 
 * **Added new RSS offload types for IPv4/L4 checksum in RSS flow.**
 
lib/gpudev/gpudev.c +169 -0

172 lines not on this page · raw message

lib/gpudev/meson.build +2 -0
index 608154817b..89a118f357 100644
@@ -8,3 +8,5 @@ headers = files(
 sources = files(
         'gpudev.c',
 )
+
+deps += ['mbuf']
lib/gpudev/rte_gpudev.h +129 -0

153 lines not on this page · raw message

lib/gpudev/version.map +4 -0
index 2fc039373a..45a35fa6e4 100644
@@ -6,9 +6,13 @@ EXPERIMENTAL {
 	rte_gpu_callback_register;
 	rte_gpu_callback_unregister;
 	rte_gpu_close;
+	rte_gpu_comm_cleanup_list;
 	rte_gpu_comm_create_flag;
+	rte_gpu_comm_create_list;
 	rte_gpu_comm_destroy_flag;
+	rte_gpu_comm_destroy_list;
 	rte_gpu_comm_get_flag_value;
+	rte_gpu_comm_populate_list_pkts;
 	rte_gpu_comm_set_flag;
 	rte_gpu_count_avail;
 	rte_gpu_find_next;
Signature · 1 line
2.17.1
#116 lore raw

PATCH v4 9/9 doc: add CUDA example in GPU guide

In reply to [email protected]

From: Elena Agostini <[email protected]>

---
 doc/guides/prog_guide/gpudev.rst | 122 +++++++++++++++++++++++++++++++
 1 file changed, 122 insertions(+)
doc/guides/prog_guide/gpudev.rst +122 -0
index cbaec5a1e4..1baf0c6772 100644
@@ -102,3 +102,125 @@ the list of mbuf payload addresses where received packet have been stored.
 The ``rte_gpu_comm_*()`` functions are responsible to create a list of packets
 that can be populated with receive mbuf payload addresses
 and communicated to the task running on the GPU.
+
+
+CUDA Example
+------------
+
+In the example below, there is a pseudo-code to give an example
+about how to use functions in this library in case of a CUDA application.
+
+.. code-block:: c
+
+   //////////////////////////////////////////////////////////////////////////
+   ///// gpudev library + CUDA functions
+   //////////////////////////////////////////////////////////////////////////
+   #define GPU_PAGE_SHIFT 16
+   #define GPU_PAGE_SIZE (1UL << GPU_PAGE_SHIFT)
+
+   int main() {
+       struct rte_gpu_flag quit_flag;
+       struct rte_gpu_comm_list *comm_list;
+       int nb_rx = 0;
+       int comm_list_entry = 0;
+       struct rte_mbuf * rx_mbufs[max_rx_mbufs];
+       cudaStream_t cstream;
+       struct rte_mempool *mpool_payload, *mpool_header;
+       struct rte_pktmbuf_extmem ext_mem;
+       int16_t dev_id;
+       int16_t port_id = 0;
+
+       /** Initialize CUDA objects (cstream, context, etc..). */
+       /** Use gpudev library to register a new CUDA context if any */
+       /** Let's assume the application wants to use the default context of the GPU device 0 */
+
+       dev_id = 0;
+
+       /**
+        * Create an external memory mempool using memory allocated on the GPU.
+        */
+       ext_mem.elt_size = mbufs_headroom_size;
+                   ext_mem.buf_len = RTE_ALIGN_CEIL(mbufs_num * ext_mem.elt_size, GPU_PAGE_SIZE);
+       ext_mem.buf_iova = RTE_BAD_IOVA;
+       ext_mem.buf_ptr = rte_gpu_malloc(dev_id, ext_mem.buf_len, 0);
+       rte_extmem_register(ext_mem.buf_ptr, ext_mem.buf_len, NULL, ext_mem.buf_iova, GPU_PAGE_SIZE);
+       rte_dev_dma_map(rte_eth_devices[port_id].device, ext_mem.buf_ptr, ext_mem.buf_iova, ext_mem.buf_len);
+       mpool_payload = rte_pktmbuf_pool_create_extbuf("gpu_mempool", mbufs_num,
+                                                       0, 0, ext_mem.elt_size,
+                                                       rte_socket_id(), &ext_mem, 1);
+
+       /**
+        * Create CPU - device communication flag. With this flag, the CPU can tell to the CUDA kernel
+        * to exit from the main loop.
+        */
+       rte_gpu_comm_create_flag(dev_id, &quit_flag, RTE_GPU_COMM_FLAG_CPU);
+       rte_gpu_comm_set_flag(&quit_flag , 0);
+
+       /**
+        * Create CPU - device communication list. Each entry of this list will be populated by the CPU
+        * with a new set of received mbufs that the CUDA kernel has to process.
+        */
+       comm_list = rte_gpu_comm_create_list(dev_id, num_entries);
+
+       /** A very simple CUDA kernel with just 1 CUDA block and RTE_GPU_COMM_LIST_PKTS_MAX CUDA threads. */
+       cuda_kernel_packet_processing<<<1, RTE_GPU_COMM_LIST_PKTS_MAX, 0, cstream>>>(quit_flag->ptr, comm_list, num_entries, ...);
+
+       /**
+        * For simplicity, the CPU here receives only 2 bursts of mbufs.
+        * In a real application, network activity and device processing should overlap.
+        */
+       nb_rx = rte_eth_rx_burst(port_id, queue_id, &(rx_mbufs[0]), max_rx_mbufs);
+       rte_gpu_comm_populate_list_pkts(comm_list[0], rx_mbufs, nb_rx);
+       nb_rx = rte_eth_rx_burst(port_id, queue_id, &(rx_mbufs[0]), max_rx_mbufs);
+       rte_gpu_comm_populate_list_pkts(comm_list[1], rx_mbufs, nb_rx);
+
+       /**
+        * CPU waits for the completion of the packets' processing on the CUDA kernel
+        * and then it does a cleanup of the received mbufs.
+        */
+       while(rte_gpu_comm_cleanup_list(comm_list[0]));
+       while(rte_gpu_comm_cleanup_list(comm_list[1]));
+
+       /** CPU notifies the CUDA kernel that it has to terminate */
+       rte_gpu_comm_set_flag(&quit_flag, 1);
+
+       /** gpudev objects cleanup/destruction */
+       /** CUDA cleanup */
+
+       rte_gpu_free(dev_id, ext_mem.buf_len);
+
+       /** DPDK cleanup */
+
+       return 0;
+   }
+
+   //////////////////////////////////////////////////////////////////////////
+   ///// CUDA kernel
+   //////////////////////////////////////////////////////////////////////////
+
+   void cuda_kernel(uint32_t * quit_flag_ptr, struct rte_gpu_comm_list *comm_list, int comm_list_entries) {
+      int comm_list_index = 0;
+      struct rte_gpu_comm_pkt *pkt_list = NULL;
+
+      /** Do some pre-processing operations. */
+
+      /** GPU kernel keeps checking this flag to know if it has to quit or wait for more packets. */
+      while(*quit_flag_ptr == 0)
+      {
+         if(comm_list[comm_list_index]->status != RTE_GPU_COMM_LIST_READY)
+         continue;
+
+         if(threadIdx.x < comm_list[comm_list_index]->num_pkts)
+         {
+            /** Each CUDA thread processes a different packet. */
+            packet_processing(comm_list[comm_list_index]->addr, comm_list[comm_list_index]->size, ..);
+         }
+         __threadfence();
+         __syncthreads();
+
+         /** Wait for new packets on the next communication list entry. */
+         comm_list_index = (comm_list_index+1) % comm_list_entries;
+      }
+
+      /** Do some post-processing operations. */
+   }
Signature · 1 line
2.17.1
#117 lore raw

v5 cover GPU library

In reply to Thomas Monjalon

From: Elena Agostini <[email protected]>

In heterogeneous computing system, processing is not only in the CPU. Some tasks can be delegated to devices working in parallel.

The goal of this new library is to enhance the collaboration between DPDK, that's primarily a CPU framework, and GPU devices.

When mixing network activity with task processing on a non-CPU device, there may be the need to put in communication the CPU with the device in order to manage the memory, synchronize operations, exchange info, etc..

This library provides a number of new features:
- Interoperability with GPU-specific library with generic handlers
- Possibility to allocate and free memory on the GPU
- Possibility to allocate and free memory on the CPU but visible from the GPU
- Communication functions to enhance the dialog between the CPU and the GPU

The infrastructure is prepared to welcome drivers in drivers/gpu/ as the CUDA one:
http://patches.dpdk.org/project/dpdk/cover/[email protected]

Changelog:
- Patches updated to latest DPDK commit
- Communication list item has an array of mbufs instead of opaque objects
- Communication list free doesn't release mbufs anymore
- Fixed styling reported by checkpatch

Elena Agostini (6):
gpudev: introduce GPU device class library
gpudev: add memory API
gpudev: add memory barrier
gpudev: add communication flag
gpudev: add communication list
doc: add CUDA example in GPU guide

Thomas Monjalon (3):
gpudev: add event notification
gpudev: add child device representing a device context
gpudev: support multi-process

 .gitignore                             |   1 +
 MAINTAINERS                            |   6 +
 app/meson.build                        |   1 +
 app/test-gpudev/main.c                 | 367 ++++++++++
 app/test-gpudev/meson.build            |   5 +
 doc/api/doxy-api.conf.in               |   1 +
 doc/guides/conf.py                     |   8 +
 doc/guides/gpus/features/default.ini   |  13 +
 doc/guides/gpus/index.rst              |  11 +
 doc/guides/gpus/overview.rst           |  10 +
 doc/guides/index.rst                   |   1 +
 doc/guides/prog_guide/gpudev.rst       | 226 +++++++
 doc/guides/prog_guide/index.rst        |   1 +
 doc/guides/rel_notes/release_21_11.rst |   6 +
 drivers/gpu/meson.build                |   4 +
 drivers/meson.build                    |   1 +
 lib/gpudev/gpudev.c                    | 901 +++++++++++++++++++++++++
 lib/gpudev/gpudev_driver.h             | 102 +++
 lib/gpudev/meson.build                 |  12 +
 lib/gpudev/rte_gpudev.h                | 649 ++++++++++++++++++
 lib/gpudev/version.map                 |  38 ++
 lib/meson.build                        |   1 +
 22 files changed, 2365 insertions(+)
 create mode 100644 app/test-gpudev/main.c
 create mode 100644 app/test-gpudev/meson.build
 create mode 100644 doc/guides/gpus/features/default.ini
 create mode 100644 doc/guides/gpus/index.rst
 create mode 100644 doc/guides/gpus/overview.rst
 create mode 100644 doc/guides/prog_guide/gpudev.rst
 create mode 100644 drivers/gpu/meson.build
 create mode 100644 lib/gpudev/gpudev.c
 create mode 100644 lib/gpudev/gpudev_driver.h
 create mode 100644 lib/gpudev/meson.build
 create mode 100644 lib/gpudev/rte_gpudev.h
 create mode 100644 lib/gpudev/version.map
Signature · 1 line
2.17.1
#118 lore raw

PATCH v5 1/9 gpudev: introduce GPU device class library

From: Elena Agostini <[email protected]>

In heterogeneous computing system, processing is not only in the CPU. Some tasks can be delegated to devices working in parallel.

The new library gpudev is for dealing with GPGPU computing devices from a DPDK application running on the CPU.

The infrastructure is prepared to welcome drivers in drivers/gpu/.

---
 .gitignore                             |   1 +
 MAINTAINERS                            |   6 +
 app/meson.build                        |   1 +
 app/test-gpudev/main.c                 | 107 +++++++++++
 app/test-gpudev/meson.build            |   5 +
 doc/api/doxy-api.conf.in               |   1 +
 doc/guides/conf.py                     |   8 +
 doc/guides/gpus/features/default.ini   |  10 +
 doc/guides/gpus/index.rst              |  11 ++
 doc/guides/gpus/overview.rst           |  10 +
 doc/guides/index.rst                   |   1 +
 doc/guides/prog_guide/gpudev.rst       |  36 ++++
 doc/guides/prog_guide/index.rst        |   1 +
 doc/guides/rel_notes/release_21_11.rst |   4 +
 drivers/gpu/meson.build                |   4 +
 drivers/meson.build                    |   1 +
 lib/gpudev/gpudev.c                    | 249 +++++++++++++++++++++++++
 lib/gpudev/gpudev_driver.h             |  67 +++++++
 lib/gpudev/meson.build                 |  10 +
 lib/gpudev/rte_gpudev.h                | 168 +++++++++++++++++
 lib/gpudev/version.map                 |  20 ++
 lib/meson.build                        |   1 +
 22 files changed, 722 insertions(+)
 create mode 100644 app/test-gpudev/main.c
 create mode 100644 app/test-gpudev/meson.build
 create mode 100644 doc/guides/gpus/features/default.ini
 create mode 100644 doc/guides/gpus/index.rst
 create mode 100644 doc/guides/gpus/overview.rst
 create mode 100644 doc/guides/prog_guide/gpudev.rst
 create mode 100644 drivers/gpu/meson.build
 create mode 100644 lib/gpudev/gpudev.c
 create mode 100644 lib/gpudev/gpudev_driver.h
 create mode 100644 lib/gpudev/meson.build
 create mode 100644 lib/gpudev/rte_gpudev.h
 create mode 100644 lib/gpudev/version.map
.gitignore +1 -0
index 7ec8688342..b98a43a601 100644
@@ -15,6 +15,7 @@ doc/guides/compressdevs/overview_feature_table.txt
 doc/guides/regexdevs/overview_feature_table.txt
 doc/guides/vdpadevs/overview_feature_table.txt
 doc/guides/bbdevs/overview_feature_table.txt
+doc/guides/gpus/overview_feature_table.txt
 
 # ignore generated ctags/cscope files
 cscope.out.po
MAINTAINERS +6 -0
index 3459187e26..a2e67fb1e6 100644
@@ -467,6 +467,12 @@ M: Bruce Richardson <[email protected]>
 F: examples/dma/
 F: doc/guides/sample_app_ug/dma.rst
 
+General-Purpose Graphics Processing Unit (GPU) API - EXPERIMENTAL
+M: Elena Agostini <[email protected]>
+F: lib/gpudev/
+F: doc/guides/prog_guide/gpudev.rst
+F: doc/guides/gpus/features/default.ini
+
 Eventdev API
 M: Jerin Jacob <[email protected]>
 T: git://dpdk.org/next/dpdk-next-eventdev
app/meson.build +1 -0
index 986c1a4ad4..310e83076f 100644
@@ -13,6 +13,7 @@ apps = [
         'test-eventdev',
         'test-fib',
         'test-flow-perf',
+        'test-gpudev',
         'test-pipeline',
         'test-pmd',
         'test-regex',
app/test-gpudev/main.c new file +107 -0

107 lines not on this page · raw message

app/test-gpudev/meson.build new file +5 -0
new file mode 100644
index 0000000000..17bdef3646
@@ -0,0 +1,5 @@
+# SPDX-License-Identifier: BSD-3-Clause
+# Copyright (c) 2021 NVIDIA Corporation & Affiliates
+
+sources = files('main.c')
+deps = ['gpudev', 'ethdev']
doc/api/doxy-api.conf.in +1 -0
index 096ebbaf0d..db2ca9b6ed 100644
@@ -41,6 +41,7 @@ INPUT                   = @TOPDIR@/doc/api/doxy-api-index.md \
                           @TOPDIR@/lib/eventdev \
                           @TOPDIR@/lib/fib \
                           @TOPDIR@/lib/flow_classify \
+                          @TOPDIR@/lib/gpudev \
                           @TOPDIR@/lib/graph \
                           @TOPDIR@/lib/gro \
                           @TOPDIR@/lib/gso \
doc/guides/conf.py +8 -0
index 0494b0efe7..e6ce929bc8 100644
@@ -152,6 +152,9 @@ def generate_overview_table(output_filename, table_id, section, table_name, titl
         name = ini_filename[:-4]
         name = name.replace('_vf', 'vf')
         pmd_names.append(name)
+    if not pmd_names:
+        # Add an empty column if table is empty (required by RST syntax)
+        pmd_names.append(' ')
 
     # Pad the table header names.
     max_header_len = len(max(pmd_names, key=len))
@@ -393,6 +396,11 @@ def setup(app):
                             'Features',
                             'Features availability in bbdev drivers',
                             'Feature')
+    table_file = dirname(__file__) + '/gpus/overview_feature_table.txt'
+    generate_overview_table(table_file, 1,
+                            'Features',
+                            'Features availability in GPU drivers',
+                            'Feature')
 
     if LooseVersion(sphinx_version) < LooseVersion('1.3.1'):
         print('Upgrade sphinx to version >= 1.3.1 for '
doc/guides/gpus/features/default.ini new file +10 -0
new file mode 100644
index 0000000000..ec7a545eb7
@@ -0,0 +1,10 @@
+;
+; Features of GPU drivers.
+;
+; This file defines the features that are valid for inclusion in
+; the other driver files and also the order that they appear in
+; the features table in the documentation. The feature description
+; string should not exceed feature_str_len defined in conf.py.
+;
+[Features]
+Get device info                =
doc/guides/gpus/index.rst new file +11 -0
new file mode 100644
index 0000000000..1878423239
@@ -0,0 +1,11 @@
+.. SPDX-License-Identifier: BSD-3-Clause
+   Copyright (c) 2021 NVIDIA Corporation & Affiliates
+
+General-Purpose Graphics Processing Unit Drivers
+================================================
+
+.. toctree::
+   :maxdepth: 2
+   :numbered:
+
+   overview
doc/guides/gpus/overview.rst new file +10 -0
new file mode 100644
index 0000000000..4830348818
@@ -0,0 +1,10 @@
+.. SPDX-License-Identifier: BSD-3-Clause
+   Copyright (c) 2021 NVIDIA Corporation & Affiliates
+
+Overview of GPU Drivers
+=======================
+
+General-Purpose computing on Graphics Processing Unit (GPGPU)
+is the use of GPU to perform parallel computation.
+
+.. include:: overview_feature_table.txt
doc/guides/index.rst +1 -0
index 919825992e..5eb5bd9c9a 100644
@@ -22,6 +22,7 @@ DPDK documentation
    vdpadevs/index
    regexdevs/index
    dmadevs/index
+   gpus/index
    eventdevs/index
    rawdevs/index
    mempool/index
doc/guides/prog_guide/gpudev.rst new file +36 -0
new file mode 100644
index 0000000000..6ea7239159
@@ -0,0 +1,36 @@
+.. SPDX-License-Identifier: BSD-3-Clause
+   Copyright (c) 2021 NVIDIA Corporation & Affiliates
+
+General-Purpose Graphics Processing Unit Library
+================================================
+
+When mixing networking activity with task processing on a GPU device,
+there may be the need to put in communication the CPU with the device
+in order to manage the memory, synchronize operations, exchange info, etc..
+
+By means of the generic GPU interface provided by this library,
+it is possible to allocate a chunk of GPU memory and use it
+to create a DPDK mempool with external mbufs having the payload
+on the GPU memory, enabling any network interface card
+(which support this feature like Mellanox NIC)
+to directly transmit and receive packets using GPU memory.
+
+Additionally, this library provides a number of functions
+to enhance the dialog between CPU and GPU.
+
+Out of scope of this library is to provide a wrapper for GPU specific libraries
+(e.g. CUDA Toolkit or OpenCL), thus it is not possible to launch workload
+on the device or create GPU specific objects
+(e.g. CUDA Driver context or CUDA Streams in case of NVIDIA GPUs).
+
+
+Features
+--------
+
+This library provides a number of features:
+
+- Interoperability with device-specific library through generic handlers.
+
+
+API Overview
+------------
doc/guides/prog_guide/index.rst +1 -0
index 20e5155cf4..7090b5589a 100644
@@ -28,6 +28,7 @@ Programmer's Guide
     compressdev
     regexdev
     dmadev
+    gpudev
     rte_security
     rawdev
     link_bonding_poll_mode_drv_lib
doc/guides/rel_notes/release_21_11.rst +4 -0
index 8da19c613a..9cf59e73bb 100644
@@ -101,6 +101,10 @@ New Features
   Added ``rte_eth_macaddrs_get`` to allow user to retrieve all Ethernet
   addresses assigned to given ethernet port.
 
+* **Introduced GPU device class with first features:**
+
+  * Device information
+
 * **Added new RSS offload types for IPv4/L4 checksum in RSS flow.**
 
   Added macros ETH_RSS_IPV4_CHKSUM and ETH_RSS_L4_CHKSUM, now IPv4 and
drivers/gpu/meson.build new file +4 -0
new file mode 100644
index 0000000000..e51ad3381b
@@ -0,0 +1,4 @@
+# SPDX-License-Identifier: BSD-3-Clause
+# Copyright (c) 2021 NVIDIA Corporation & Affiliates
+
+drivers = []
drivers/meson.build +1 -0
index 34c0276487..d5f4e1c1f2 100644
@@ -19,6 +19,7 @@ subdirs = [
         'vdpa',           # depends on common, bus and mempool.
         'event',          # depends on common, bus, mempool and net.
         'baseband',       # depends on common and bus.
+        'gpu',            # depends on common and bus.
 ]
 
 if meson.is_cross_build()
lib/gpudev/gpudev.c new file +249 -0

249 lines not on this page · raw message

lib/gpudev/gpudev_driver.h new file +67 -0

67 lines not on this page · raw message

lib/gpudev/meson.build new file +10 -0
new file mode 100644
index 0000000000..608154817b
@@ -0,0 +1,10 @@
+# SPDX-License-Identifier: BSD-3-Clause
+# Copyright (c) 2021 NVIDIA Corporation & Affiliates
+
+headers = files(
+        'rte_gpudev.h',
+)
+
+sources = files(
+        'gpudev.c',
+)
lib/gpudev/rte_gpudev.h new file +168 -0

168 lines not on this page · raw message

lib/gpudev/version.map new file +20 -0
new file mode 100644
index 0000000000..6ac6b327e2
@@ -0,0 +1,20 @@
+EXPERIMENTAL {
+	global:
+
+	# added in 21.11
+	rte_gpu_close;
+	rte_gpu_count_avail;
+	rte_gpu_find_next;
+	rte_gpu_info_get;
+	rte_gpu_init;
+	rte_gpu_is_valid;
+};
+
+INTERNAL {
+	global:
+
+	rte_gpu_allocate;
+	rte_gpu_complete_new;
+	rte_gpu_get_by_name;
+	rte_gpu_release;
+};
lib/meson.build +1 -0
index 499d26060f..8537a5ab80 100644
@@ -34,6 +34,7 @@ libraries = [
         'distributor',
         'efd',
         'eventdev',
+        'gpudev',
         'gro',
         'gso',
         'ip_frag',
Signature · 1 line
2.17.1
#119 lore raw

PATCH v5 2/9 gpudev: add event notification

In reply to [email protected]

From: Thomas Monjalon <[email protected]>

Callback functions may be registered for a device event. Callback management is per-process and not thread-safe.

The events RTE_GPU_EVENT_NEW and RTE_GPU_EVENT_DEL are notified respectively after creation and before removal of a device, as part of the library functions. Some future events may be emitted from drivers.

---
 lib/gpudev/gpudev.c        | 148 +++++++++++++++++++++++++++++++++++++
 lib/gpudev/gpudev_driver.h |   7 ++
 lib/gpudev/rte_gpudev.h    |  70 ++++++++++++++++++
 lib/gpudev/version.map     |   3 +
 4 files changed, 228 insertions(+)
lib/gpudev/gpudev.c +148 -0
index aeb021f2cc..07572ae040 100644
@@ -3,6 +3,7 @@
  */
 
 #include <rte_eal.h>
+#include <rte_tailq.h>
 #include <rte_string_fns.h>
 #include <rte_errno.h>
 #include <rte_log.h>
@@ -27,6 +28,16 @@ static int16_t gpu_max;
 /* Number of currently valid devices */
 static int16_t gpu_count;
 
+/* Event callback object */
+struct rte_gpu_callback {
+	TAILQ_ENTRY(rte_gpu_callback) next;
+	rte_gpu_callback_t *function;
+	void *user_data;
+	enum rte_gpu_event event;
+};
+static rte_rwlock_t gpu_callback_lock = RTE_RWLOCK_INITIALIZER;
+static void gpu_free_callbacks(struct rte_gpu *dev);
+
 int
 rte_gpu_init(size_t dev_max)
 {
@@ -166,6 +177,7 @@ rte_gpu_allocate(const char *name)
 	dev->info.name = dev->name;
 	dev->info.dev_id = dev_id;
 	dev->info.numa_node = -1;
+	TAILQ_INIT(&dev->callbacks);
 
 	gpu_count++;
 	GPU_LOG(DEBUG, "new device %s (id %d) of total %d",
@@ -180,6 +192,8 @@ rte_gpu_complete_new(struct rte_gpu *dev)
 		return;
 
 	dev->state = RTE_GPU_STATE_INITIALIZED;
+	dev->state = RTE_GPU_STATE_INITIALIZED;
+	rte_gpu_notify(dev, RTE_GPU_EVENT_NEW);
 }
 
 int
@@ -192,6 +206,9 @@ rte_gpu_release(struct rte_gpu *dev)
 
 	GPU_LOG(DEBUG, "free device %s (id %d)",
 			dev->info.name, dev->info.dev_id);
+	rte_gpu_notify(dev, RTE_GPU_EVENT_DEL);
+
+	gpu_free_callbacks(dev);
 	dev->state = RTE_GPU_STATE_UNUSED;
 	gpu_count--;
 
@@ -224,6 +241,137 @@ rte_gpu_close(int16_t dev_id)
 	return firsterr;
 }
 
+int
+rte_gpu_callback_register(int16_t dev_id, enum rte_gpu_event event,
+		rte_gpu_callback_t *function, void *user_data)
+{
+	int16_t next_dev, last_dev;
+	struct rte_gpu_callback_list *callbacks;
+	struct rte_gpu_callback *callback;
+
+	if (!rte_gpu_is_valid(dev_id) && dev_id != RTE_GPU_ID_ANY) {
+		GPU_LOG(ERR, "register callback of invalid ID %d", dev_id);
+		rte_errno = ENODEV;
+		return -rte_errno;
+	}
+	if (function == NULL) {
+		GPU_LOG(ERR, "cannot register callback without function");
+		rte_errno = EINVAL;
+		return -rte_errno;
+	}
+
+	if (dev_id == RTE_GPU_ID_ANY) {
+		next_dev = 0;
+		last_dev = gpu_max - 1;
+	} else {
+		next_dev = last_dev = dev_id;
+	}
+
+	rte_rwlock_write_lock(&gpu_callback_lock);
+	do {
+		callbacks = &gpus[next_dev].callbacks;
+
+		/* check if not already registered */
+		TAILQ_FOREACH(callback, callbacks, next) {
+			if (callback->event == event &&
+					callback->function == function &&
+					callback->user_data == user_data) {
+				GPU_LOG(INFO, "callback already registered");
+				return 0;
+			}
+		}
+
+		callback = malloc(sizeof(*callback));
+		if (callback == NULL) {
+			GPU_LOG(ERR, "cannot allocate callback");
+			return -ENOMEM;
+		}
+		callback->function = function;
+		callback->user_data = user_data;
+		callback->event = event;
+		TAILQ_INSERT_TAIL(callbacks, callback, next);
+
+	} while (++next_dev <= last_dev);
+	rte_rwlock_write_unlock(&gpu_callback_lock);
+
+	return 0;
+}
+
+int
+rte_gpu_callback_unregister(int16_t dev_id, enum rte_gpu_event event,
+		rte_gpu_callback_t *function, void *user_data)
+{
+	int16_t next_dev, last_dev;
+	struct rte_gpu_callback_list *callbacks;
+	struct rte_gpu_callback *callback, *nextcb;
+
+	if (!rte_gpu_is_valid(dev_id) && dev_id != RTE_GPU_ID_ANY) {
+		GPU_LOG(ERR, "unregister callback of invalid ID %d", dev_id);
+		rte_errno = ENODEV;
+		return -rte_errno;
+	}
+	if (function == NULL) {
+		GPU_LOG(ERR, "cannot unregister callback without function");
+		rte_errno = EINVAL;
+		return -rte_errno;
+	}
+
+	if (dev_id == RTE_GPU_ID_ANY) {
+		next_dev = 0;
+		last_dev = gpu_max - 1;
+	} else {
+		next_dev = last_dev = dev_id;
+	}
+
+	rte_rwlock_write_lock(&gpu_callback_lock);
+	do {
+		callbacks = &gpus[next_dev].callbacks;
+		RTE_TAILQ_FOREACH_SAFE(callback, callbacks, next, nextcb) {
+			if (callback->event != event ||
+					callback->function != function ||
+					(callback->user_data != user_data &&
+					user_data != (void *)-1))
+				continue;
+			TAILQ_REMOVE(callbacks, callback, next);
+			free(callback);
+		}
+	} while (++next_dev <= last_dev);
+	rte_rwlock_write_unlock(&gpu_callback_lock);
+
+	return 0;
+}
+
+static void
+gpu_free_callbacks(struct rte_gpu *dev)
+{
+	struct rte_gpu_callback_list *callbacks;
+	struct rte_gpu_callback *callback, *nextcb;
+
+	callbacks = &dev->callbacks;
+	rte_rwlock_write_lock(&gpu_callback_lock);
+	RTE_TAILQ_FOREACH_SAFE(callback, callbacks, next, nextcb) {
+		TAILQ_REMOVE(callbacks, callback, next);
+		free(callback);
+	}
+	rte_rwlock_write_unlock(&gpu_callback_lock);
+}
+
+void
+rte_gpu_notify(struct rte_gpu *dev, enum rte_gpu_event event)
+{
+	int16_t dev_id;
+	struct rte_gpu_callback *callback;
+
+	dev_id = dev->info.dev_id;
+	rte_rwlock_read_lock(&gpu_callback_lock);
+	TAILQ_FOREACH(callback, &dev->callbacks, next) {
+		if (callback->event != event || callback->function == NULL)
+			continue;
+		callback->function(dev_id, event, callback->user_data);
+	}
+	rte_rwlock_read_unlock(&gpu_callback_lock);
+}
+
 int
 rte_gpu_info_get(int16_t dev_id, struct rte_gpu_info *info)
 {
lib/gpudev/gpudev_driver.h +7 -0
index 9e096e3b64..2a7089aa52 100644
@@ -12,6 +12,7 @@
 #define RTE_GPUDEV_DRIVER_H
 
 #include <stdint.h>
+#include <sys/queue.h>
 
 #include <rte_dev.h>
 
@@ -43,6 +44,8 @@ struct rte_gpu {
 	struct rte_gpu_info info;
 	/* Driver functions. */
 	struct rte_gpu_ops ops;
+	/* Event callback list. */
+	TAILQ_HEAD(rte_gpu_callback_list, rte_gpu_callback) callbacks;
 	/* Current state (used or not) in the running process. */
 	enum rte_gpu_state state; /* Updated by this library. */
 	/* Driver-specific private data for the running process. */
@@ -64,4 +67,8 @@ void rte_gpu_complete_new(struct rte_gpu *dev);
 __rte_internal
 int rte_gpu_release(struct rte_gpu *dev);
 
+/* Call registered callbacks. No multi-process event. */
+__rte_internal
+void rte_gpu_notify(struct rte_gpu *dev, enum rte_gpu_event);
+
 #endif /* RTE_GPUDEV_DRIVER_H */
lib/gpudev/rte_gpudev.h +70 -0

88 lines not on this page · raw message

lib/gpudev/version.map +3 -0
index 6ac6b327e2..b3b6b76c1c 100644
@@ -2,6 +2,8 @@ EXPERIMENTAL {
 	global:
 
 	# added in 21.11
+	rte_gpu_callback_register;
+	rte_gpu_callback_unregister;
 	rte_gpu_close;
 	rte_gpu_count_avail;
 	rte_gpu_find_next;
@@ -16,5 +18,6 @@ INTERNAL {
 	rte_gpu_allocate;
 	rte_gpu_complete_new;
 	rte_gpu_get_by_name;
+	rte_gpu_notify;
 	rte_gpu_release;
 };
Signature · 1 line
2.17.1
#120 lore raw

PATCH v5 3/9 gpudev: add child device representing a device context

In reply to [email protected]

From: Thomas Monjalon <[email protected]>

The computing device may operate in some isolated contexts. Memory and processing are isolated in a silo represented by a child device.
The context is provided as an opaque by the caller of rte_gpu_add_child().

---
 doc/guides/prog_guide/gpudev.rst | 12 ++++++
 lib/gpudev/gpudev.c              | 45 +++++++++++++++++++-
 lib/gpudev/gpudev_driver.h       |  2 +-
 lib/gpudev/rte_gpudev.h          | 71 +++++++++++++++++++++++++++++---
 lib/gpudev/version.map           |  1 +
 5 files changed, 123 insertions(+), 8 deletions(-)
doc/guides/prog_guide/gpudev.rst +12 -0
index 6ea7239159..7694639489 100644
@@ -34,3 +34,15 @@ This library provides a number of features:
 
 API Overview
 ------------
+
+Child Device
+~~~~~~~~~~~~
+
+By default, DPDK PCIe module detects and registers physical GPU devices
+in the system.
+With the gpudev library is also possible to add additional non-physical devices
+through an ``uint64_t`` generic handler (e.g. CUDA Driver context)
+that will be registered internally by the driver as an additional device (child)
+connected to a physical device (parent).
+Each device (parent or child) is represented through a ID
+required to indicate which device a given operation should be executed on.
lib/gpudev/gpudev.c +43 -2

78 lines not on this page · raw message

lib/gpudev/gpudev_driver.h +1 -1
index 2a7089aa52..4d0077161c 100644
@@ -31,7 +31,7 @@ typedef int (rte_gpu_info_get_t)(struct rte_gpu *dev, struct rte_gpu_info *info)
 struct rte_gpu_ops {
 	/* Get device info. If NULL, info is just copied. */
 	rte_gpu_info_get_t *dev_info_get;
-	/* Close device. */
+	/* Close device or child context. */
 	rte_gpu_close_t *dev_close;
 };
 
lib/gpudev/rte_gpudev.h +66 -5

108 lines not on this page · raw message

lib/gpudev/version.map +1 -0
index b3b6b76c1c..4a934ed933 100644
@@ -2,6 +2,7 @@ EXPERIMENTAL {
 	global:
 
 	# added in 21.11
+	rte_gpu_add_child;
 	rte_gpu_callback_register;
 	rte_gpu_callback_unregister;
 	rte_gpu_close;
Signature · 1 line
2.17.1
#121 lore raw

PATCH v5 4/9 gpudev: support multi-process

In reply to [email protected]

From: Thomas Monjalon <[email protected]>

The device data shared between processes are moved in a struct allocated in a shared memory (a new memzone for all GPUs). The main struct rte_gpu references the shared memory via the pointer mpshared.

The API function rte_gpu_attach() is added to attach a device from the secondary process.
The function rte_gpu_allocate() can be used only by primary process.

---
 lib/gpudev/gpudev.c        | 127 +++++++++++++++++++++++++++++++------
 lib/gpudev/gpudev_driver.h |  25 ++++++--
 lib/gpudev/version.map     |   1 +
 3 files changed, 127 insertions(+), 26 deletions(-)
lib/gpudev/gpudev.c +107 -20
index aaf41e6071..17e371102a 100644
@@ -5,6 +5,7 @@
 #include <rte_eal.h>
 #include <rte_tailq.h>
 #include <rte_string_fns.h>
+#include <rte_memzone.h>
 #include <rte_errno.h>
 #include <rte_log.h>
 
@@ -28,6 +29,12 @@ static int16_t gpu_max;
 /* Number of currently valid devices */
 static int16_t gpu_count;
 
+/* Shared memory between processes. */
+static const char *GPU_MEMZONE = "rte_gpu_shared";
+static struct {
+	__extension__ struct rte_gpu_mpshared gpus[0];
+} *gpu_shared_mem;
+
 /* Event callback object */
 struct rte_gpu_callback {
 	TAILQ_ENTRY(rte_gpu_callback) next;
@@ -75,7 +82,7 @@ bool
 rte_gpu_is_valid(int16_t dev_id)
 {
 	if (dev_id >= 0 && dev_id < gpu_max &&
-		gpus[dev_id].state == RTE_GPU_STATE_INITIALIZED)
+		gpus[dev_id].process_state == RTE_GPU_STATE_INITIALIZED)
 		return true;
 	return false;
 }
@@ -85,7 +92,7 @@ gpu_match_parent(int16_t dev_id, int16_t parent)
 {
 	if (parent == RTE_GPU_ID_ANY)
 		return true;
-	return gpus[dev_id].info.parent == parent;
+	return gpus[dev_id].mpshared->info.parent == parent;
 }
 
 int16_t
@@ -94,7 +101,7 @@ rte_gpu_find_next(int16_t dev_id, int16_t parent)
 	if (dev_id < 0)
 		dev_id = 0;
 	while (dev_id < gpu_max &&
-			(gpus[dev_id].state == RTE_GPU_STATE_UNUSED ||
+			(gpus[dev_id].process_state == RTE_GPU_STATE_UNUSED ||
 			!gpu_match_parent(dev_id, parent)))
 		dev_id++;
 
@@ -109,7 +116,7 @@ gpu_find_free_id(void)
 	int16_t dev_id;
 
 	for (dev_id = 0; dev_id < gpu_max; dev_id++) {
-		if (gpus[dev_id].state == RTE_GPU_STATE_UNUSED)
+		if (gpus[dev_id].process_state == RTE_GPU_STATE_UNUSED)
 			return dev_id;
 	}
 	return RTE_GPU_ID_NONE;
@@ -136,12 +143,35 @@ rte_gpu_get_by_name(const char *name)
 
 	RTE_GPU_FOREACH(dev_id) {
 		dev = &gpus[dev_id];
-		if (strncmp(name, dev->name, RTE_DEV_NAME_MAX_LEN) == 0)
+		if (strncmp(name, dev->mpshared->name, RTE_DEV_NAME_MAX_LEN) == 0)
 			return dev;
 	}
 	return NULL;
 }
 
+static int
+gpu_shared_mem_init(void)
+{
+	const struct rte_memzone *memzone;
+
+	if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
+		memzone = rte_memzone_reserve(GPU_MEMZONE,
+				sizeof(*gpu_shared_mem) +
+				sizeof(*gpu_shared_mem->gpus) * gpu_max,
+				SOCKET_ID_ANY, 0);
+	} else {
+		memzone = rte_memzone_lookup(GPU_MEMZONE);
+	}
+	if (memzone == NULL) {
+		GPU_LOG(ERR, "cannot initialize shared memory");
+		rte_errno = ENOMEM;
+		return -rte_errno;
+	}
+
+	gpu_shared_mem = memzone->addr;
+	return 0;
+}
+
 struct rte_gpu *
 rte_gpu_allocate(const char *name)
 {
@@ -163,6 +193,10 @@ rte_gpu_allocate(const char *name)
 	if (gpus == NULL && rte_gpu_init(RTE_GPU_DEFAULT_MAX) < 0)
 		return NULL;
 
+	/* initialize shared memory before adding first device */
+	if (gpu_shared_mem == NULL && gpu_shared_mem_init() < 0)
+		return NULL;
+
 	if (rte_gpu_get_by_name(name) != NULL) {
 		GPU_LOG(ERR, "device with name %s already exists", name);
 		rte_errno = EEXIST;
@@ -178,16 +212,20 @@ rte_gpu_allocate(const char *name)
 	dev = &gpus[dev_id];
 	memset(dev, 0, sizeof(*dev));
 
-	if (rte_strscpy(dev->name, name, RTE_DEV_NAME_MAX_LEN) < 0) {
+	dev->mpshared = &gpu_shared_mem->gpus[dev_id];
+	memset(dev->mpshared, 0, sizeof(*dev->mpshared));
+
+	if (rte_strscpy(dev->mpshared->name, name, RTE_DEV_NAME_MAX_LEN) < 0) {
 		GPU_LOG(ERR, "device name too long: %s", name);
 		rte_errno = ENAMETOOLONG;
 		return NULL;
 	}
-	dev->info.name = dev->name;
-	dev->info.dev_id = dev_id;
-	dev->info.numa_node = -1;
-	dev->info.parent = RTE_GPU_ID_NONE;
+	dev->mpshared->info.name = dev->mpshared->name;
+	dev->mpshared->info.dev_id = dev_id;
+	dev->mpshared->info.numa_node = -1;
+	dev->mpshared->info.parent = RTE_GPU_ID_NONE;
 	TAILQ_INIT(&dev->callbacks);
+	__atomic_fetch_add(&dev->mpshared->process_refcnt, 1, __ATOMIC_RELAXED);
 
 	gpu_count++;
 	GPU_LOG(DEBUG, "new device %s (id %d) of total %d",
@@ -195,6 +233,55 @@ rte_gpu_allocate(const char *name)
 	return dev;
 }
 
+struct rte_gpu *
+rte_gpu_attach(const char *name)
+{
+	int16_t dev_id;
+	struct rte_gpu *dev;
+	struct rte_gpu_mpshared *shared_dev;
+
+	if (rte_eal_process_type() != RTE_PROC_SECONDARY) {
+		GPU_LOG(ERR, "only secondary process can attach device");
+		rte_errno = EPERM;
+		return NULL;
+	}
+	if (name == NULL) {
+		GPU_LOG(ERR, "attach device without a name");
+		rte_errno = EINVAL;
+		return NULL;
+	}
+
+	/* implicit initialization of library before adding first device */
+	if (gpus == NULL && rte_gpu_init(RTE_GPU_DEFAULT_MAX) < 0)
+		return NULL;
+
+	/* initialize shared memory before adding first device */
+	if (gpu_shared_mem == NULL && gpu_shared_mem_init() < 0)
+		return NULL;
+
+	for (dev_id = 0; dev_id < gpu_max; dev_id++) {
+		shared_dev = &gpu_shared_mem->gpus[dev_id];
+		if (strncmp(name, shared_dev->name, RTE_DEV_NAME_MAX_LEN) == 0)
+			break;
+	}
+	if (dev_id >= gpu_max) {
+		GPU_LOG(ERR, "device with name %s not found", name);
+		rte_errno = ENOENT;
+		return NULL;
+	}
+	dev = &gpus[dev_id];
+	memset(dev, 0, sizeof(*dev));
+
+	TAILQ_INIT(&dev->callbacks);
+	dev->mpshared = shared_dev;
+	__atomic_fetch_add(&dev->mpshared->process_refcnt, 1, __ATOMIC_RELAXED);
+
+	gpu_count++;
+	GPU_LOG(DEBUG, "attached device %s (id %d) of total %d",
+			name, dev_id, gpu_count);
+	return dev;
+}
+
 int16_t
 rte_gpu_add_child(const char *name, int16_t parent, uint64_t child_context)
 {
@@ -210,11 +297,11 @@ rte_gpu_add_child(const char *name, int16_t parent, uint64_t child_context)
 	if (dev == NULL)
 		return -rte_errno;
 
-	dev->info.parent = parent;
-	dev->info.context = child_context;
+	dev->mpshared->info.parent = parent;
+	dev->mpshared->info.context = child_context;
 
 	rte_gpu_complete_new(dev);
-	return dev->info.dev_id;
+	return dev->mpshared->info.dev_id;
 }
 
 void
@@ -223,8 +310,7 @@ rte_gpu_complete_new(struct rte_gpu *dev)
 	if (dev == NULL)
 		return;
 
-	dev->state = RTE_GPU_STATE_INITIALIZED;
-	dev->state = RTE_GPU_STATE_INITIALIZED;
+	dev->process_state = RTE_GPU_STATE_INITIALIZED;
 	rte_gpu_notify(dev, RTE_GPU_EVENT_NEW);
 }
 
@@ -237,7 +323,7 @@ rte_gpu_release(struct rte_gpu *dev)
 		rte_errno = ENODEV;
 		return -rte_errno;
 	}
-	dev_id = dev->info.dev_id;
+	dev_id = dev->mpshared->info.dev_id;
 	RTE_GPU_FOREACH_CHILD(child, dev_id) {
 		GPU_LOG(ERR, "cannot release device %d with child %d",
 				dev_id, child);
@@ -246,11 +332,12 @@ rte_gpu_release(struct rte_gpu *dev)
 	}
 
 	GPU_LOG(DEBUG, "free device %s (id %d)",
-			dev->info.name, dev->info.dev_id);
+			dev->mpshared->info.name, dev->mpshared->info.dev_id);
 	rte_gpu_notify(dev, RTE_GPU_EVENT_DEL);
 
 	gpu_free_callbacks(dev);
-	dev->state = RTE_GPU_STATE_UNUSED;
+	dev->process_state = RTE_GPU_STATE_UNUSED;
+	__atomic_fetch_sub(&dev->mpshared->process_refcnt, 1, __ATOMIC_RELAXED);
 	gpu_count--;
 
 	return 0;
@@ -403,7 +490,7 @@ rte_gpu_notify(struct rte_gpu *dev, enum rte_gpu_event event)
 	int16_t dev_id;
 	struct rte_gpu_callback *callback;
 
-	dev_id = dev->info.dev_id;
+	dev_id = dev->mpshared->info.dev_id;
 	rte_rwlock_read_lock(&gpu_callback_lock);
 	TAILQ_FOREACH(callback, &dev->callbacks, next) {
 		if (callback->event != event || callback->function == NULL)
@@ -431,7 +518,7 @@ rte_gpu_info_get(int16_t dev_id, struct rte_gpu_info *info)
 	}
 
 	if (dev->ops.dev_info_get == NULL) {
-		*info = dev->info;
+		*info = dev->mpshared->info;
 		return 0;
 	}
 	return GPU_DRV_RET(dev->ops.dev_info_get(dev, info));
lib/gpudev/gpudev_driver.h +19 -6
index 4d0077161c..9459c7e30f 100644
@@ -35,19 +35,28 @@ struct rte_gpu_ops {
 	rte_gpu_close_t *dev_close;
 };
 
-struct rte_gpu {
-	/* Backing device. */
-	struct rte_device *device;
+struct rte_gpu_mpshared {
 	/* Unique identifier name. */
 	char name[RTE_DEV_NAME_MAX_LEN]; /* Updated by this library. */
+	/* Driver-specific private data shared in multi-process. */
+	void *dev_private;
 	/* Device info structure. */
 	struct rte_gpu_info info;
+	/* Counter of processes using the device. */
+	uint16_t process_refcnt; /* Updated by this library. */
+};
+
+struct rte_gpu {
+	/* Backing device. */
+	struct rte_device *device;
+	/* Data shared between processes. */
+	struct rte_gpu_mpshared *mpshared;
 	/* Driver functions. */
 	struct rte_gpu_ops ops;
 	/* Event callback list. */
 	TAILQ_HEAD(rte_gpu_callback_list, rte_gpu_callback) callbacks;
 	/* Current state (used or not) in the running process. */
-	enum rte_gpu_state state; /* Updated by this library. */
+	enum rte_gpu_state process_state; /* Updated by this library. */
 	/* Driver-specific private data for the running process. */
 	void *process_private;
 } __rte_cache_aligned;
@@ -55,15 +64,19 @@ struct rte_gpu {
 __rte_internal
 struct rte_gpu *rte_gpu_get_by_name(const char *name);
 
-/* First step of initialization */
+/* First step of initialization in primary process. */
 __rte_internal
 struct rte_gpu *rte_gpu_allocate(const char *name);
 
+/* First step of initialization in secondary process. */
+__rte_internal
+struct rte_gpu *rte_gpu_attach(const char *name);
+
 /* Last step of initialization. */
 __rte_internal
 void rte_gpu_complete_new(struct rte_gpu *dev);
 
-/* Last step of removal. */
+/* Last step of removal (primary or secondary process). */
 __rte_internal
 int rte_gpu_release(struct rte_gpu *dev);
 
lib/gpudev/version.map +1 -0
index 4a934ed933..58dc632393 100644
@@ -17,6 +17,7 @@ INTERNAL {
 	global:
 
 	rte_gpu_allocate;
+	rte_gpu_attach;
 	rte_gpu_complete_new;
 	rte_gpu_get_by_name;
 	rte_gpu_notify;
Signature · 1 line
2.17.1
#122 lore raw

PATCH v5 5/9 gpudev: add memory API

In reply to [email protected]

From: Elena Agostini <[email protected]>

In heterogeneous computing system, processing is not only in the CPU. Some tasks can be delegated to devices working in parallel. Such workload distribution can be achieved by sharing some memory.

As a first step, the features are focused on memory management. A function allows to allocate memory inside the device, or in the main (CPU) memory while making it visible for the device. This memory may be used to save packets or for synchronization data.

The next step should focus on GPU processing task control.

---
 app/test-gpudev/main.c                 | 105 +++++++++++++++++++++++++
 doc/guides/gpus/features/default.ini   |   3 +
 doc/guides/prog_guide/gpudev.rst       |  19 +++++
 doc/guides/rel_notes/release_21_11.rst |   1 +
 lib/gpudev/gpudev.c                    | 101 ++++++++++++++++++++++++
 lib/gpudev/gpudev_driver.h             |  12 +++
 lib/gpudev/rte_gpudev.h                |  95 ++++++++++++++++++++++
 lib/gpudev/version.map                 |   4 +
 8 files changed, 340 insertions(+)
app/test-gpudev/main.c +105 -0
index 438cfdac54..e3aca2225a 100644
@@ -62,6 +62,98 @@ args_parse(int argc, char **argv)
 	}
 }
 
+static int
+alloc_gpu_memory(uint16_t gpu_id)
+{
+	void *ptr_1 = NULL;
+	void *ptr_2 = NULL;
+	size_t buf_bytes = 1024;
+	int ret;
+
+	printf("\n=======> TEST: Allocate GPU memory\n");
+
+	/* Alloc memory on GPU 0 */
+	ptr_1 = rte_gpu_malloc(gpu_id, buf_bytes);
+	if (ptr_1 == NULL) {
+		fprintf(stderr, "rte_gpu_malloc GPU memory returned error\n");
+		return -1;
+	}
+	printf("GPU memory allocated at 0x%p %zdB\n", ptr_1, buf_bytes);
+
+	ptr_2 = rte_gpu_malloc(gpu_id, buf_bytes);
+	if (ptr_2 == NULL) {
+		fprintf(stderr, "rte_gpu_malloc GPU memory returned error\n");
+		return -1;
+	}
+	printf("GPU memory allocated at 0x%p %zdB\n", ptr_2, buf_bytes);
+
+	ret = rte_gpu_free(gpu_id, (uint8_t *)(ptr_1)+0x700);
+	if (ret < 0) {
+		printf("GPU memory 0x%p + 0x700 NOT freed because of memory address not recognized by driver\n", ptr_1);
+	} else {
+		fprintf(stderr, "rte_gpu_free erroneusly freed GPU memory 0x%p + 0x700\n", ptr_1);
+		return -1;
+	}
+
+	ret = rte_gpu_free(gpu_id, ptr_2);
+	if (ret < 0) {
+		fprintf(stderr, "rte_gpu_free returned error %d\n", ret);
+		return -1;
+	}
+	printf("GPU memory 0x%p freed\n", ptr_2);
+
+	ret = rte_gpu_free(gpu_id, ptr_1);
+	if (ret < 0) {
+		fprintf(stderr, "rte_gpu_free returned error %d\n", ret);
+		return -1;
+	}
+	printf("GPU memory 0x%p freed\n", ptr_1);
+
+	return 0;
+}
+
+static int
+register_cpu_memory(uint16_t gpu_id)
+{
+	void *ptr = NULL;
+	size_t buf_bytes = 1024;
+	int ret;
+
+	printf("\n=======> TEST: Register CPU memory\n");
+
+	/* Alloc memory on CPU visible from GPU 0 */
+	ptr = rte_zmalloc(NULL, buf_bytes, 0);
+	if (ptr == NULL) {
+		fprintf(stderr, "Failed to allocate CPU memory.\n");
+		return -1;
+	}
+
+	ret = rte_gpu_register(gpu_id, buf_bytes, ptr);
+	if (ret < 0) {
+		fprintf(stderr, "rte_gpu_register CPU memory returned error %d\n", ret);
+		return -1;
+	}
+	printf("CPU memory registered at 0x%p %zdB\n", ptr, buf_bytes);
+
+	ret = rte_gpu_unregister(gpu_id, (uint8_t *)(ptr)+0x700);
+	if (ret < 0) {
+		printf("CPU memory 0x%p + 0x700 NOT unregistered because of memory address not recognized by driver\n", ptr);
+	} else {
+		fprintf(stderr, "rte_gpu_free erroneusly freed GPU memory 0x%p + 0x700\n", ptr);
+		return -1;
+	}
+	printf("CPU memory 0x%p unregistered\n", ptr);
+
+	ret = rte_gpu_unregister(gpu_id, ptr);
+	if (ret < 0) {
+		fprintf(stderr, "rte_gpu_unregister returned error %d\n", ret);
+		return -1;
+	}
+	printf("CPU memory 0x%p unregistered\n", ptr);
+
+	return 0;
+}
+
 int
 main(int argc, char **argv)
 {
@@ -99,6 +191,19 @@ main(int argc, char **argv)
 	}
 	printf("\n\n");
 
+	if (nb_gpus == 0) {
+		fprintf(stderr, "Need at least one GPU on the system to run the example\n");
+		return EXIT_FAILURE;
+	}
+
+	gpu_id = 0;
+
+	/**
+	 * Memory tests
+	 */
+	alloc_gpu_memory(gpu_id);
+	register_cpu_memory(gpu_id);
+
 	/* clean up the EAL */
 	rte_eal_cleanup();
 	printf("Bye...\n");
doc/guides/gpus/features/default.ini +3 -0
index ec7a545eb7..87e9966424 100644
@@ -8,3 +8,6 @@
 ;
 [Features]
 Get device info                =
+Share CPU memory with device   =
+Allocate device memory         =
+Free memory                    =
doc/guides/prog_guide/gpudev.rst +19 -0
index 7694639489..9aca69038c 100644
@@ -30,6 +30,8 @@ Features
 This library provides a number of features:
 
 - Interoperability with device-specific library through generic handlers.
+- Allocate and free memory on the device.
+- Register CPU memory to make it visible from the device.
 
 
 API Overview
@@ -46,3 +48,20 @@ that will be registered internally by the driver as an additional device (child)
 connected to a physical device (parent).
 Each device (parent or child) is represented through a ID
 required to indicate which device a given operation should be executed on.
+
+Memory Allocation
+~~~~~~~~~~~~~~~~~
+
+gpudev can allocate on an input given GPU device a memory area
+returning the pointer to that memory.
+Later, it's also possible to free that memory with gpudev.
+GPU memory allocated outside of the gpudev library
+(e.g. with GPU-specific library) cannot be freed by the gpudev library.
+
+Memory Registration
+~~~~~~~~~~~~~~~~~~~
+
+gpudev can register a CPU memory area to make it visible from a GPU device.
+Later, it's also possible to unregister that memory with gpudev.
+CPU memory registered outside of the gpudev library
+(e.g. with GPU specific library) cannot be unregistered by the gpudev library.
doc/guides/rel_notes/release_21_11.rst +1 -0
index 9cf59e73bb..a4d07bda9b 100644
@@ -104,6 +104,7 @@ New Features
 * **Introduced GPU device class with first features:**
 
   * Device information
+  * Memory management
 
 * **Added new RSS offload types for IPv4/L4 checksum in RSS flow.**
 
lib/gpudev/gpudev.c +101 -0

110 lines not on this page · raw message

lib/gpudev/gpudev_driver.h +12 -0
index 9459c7e30f..11015944a6 100644
@@ -27,12 +27,24 @@ enum rte_gpu_state {
 struct rte_gpu;
 typedef int (rte_gpu_close_t)(struct rte_gpu *dev);
 typedef int (rte_gpu_info_get_t)(struct rte_gpu *dev, struct rte_gpu_info *info);
+typedef int (rte_gpu_mem_alloc_t)(struct rte_gpu *dev, size_t size, void **ptr);
+typedef int (rte_gpu_free_t)(struct rte_gpu *dev, void *ptr);
+typedef int (rte_gpu_mem_register_t)(struct rte_gpu *dev, size_t size, void *ptr);
+typedef int (rte_gpu_mem_unregister_t)(struct rte_gpu *dev, void *ptr);
 
 struct rte_gpu_ops {
 	/* Get device info. If NULL, info is just copied. */
 	rte_gpu_info_get_t *dev_info_get;
 	/* Close device or child context. */
 	rte_gpu_close_t *dev_close;
+	/* Allocate memory in device. */
+	rte_gpu_mem_alloc_t *mem_alloc;
+	/* Register CPU memory in device. */
+	rte_gpu_mem_register_t *mem_register;
+	/* Free memory allocated or registered in device. */
+	rte_gpu_free_t *mem_free;
+	/* Unregister CPU memory in device. */
+	rte_gpu_mem_unregister_t *mem_unregister;
 };
 
 struct rte_gpu_mpshared {
lib/gpudev/rte_gpudev.h +95 -0

107 lines not on this page · raw message

lib/gpudev/version.map +4 -0
index 58dc632393..d4a65ebd52 100644
@@ -8,9 +8,13 @@ EXPERIMENTAL {
 	rte_gpu_close;
 	rte_gpu_count_avail;
 	rte_gpu_find_next;
+	rte_gpu_free;
 	rte_gpu_info_get;
 	rte_gpu_init;
 	rte_gpu_is_valid;
+	rte_gpu_malloc;
+	rte_gpu_register;
+	rte_gpu_unregister;
 };
 
 INTERNAL {
Signature · 1 line
2.17.1
#123 lore raw

PATCH v5 6/9 gpudev: add memory barrier

In reply to [email protected]

From: Elena Agostini <[email protected]>

Add a function for the application to ensure the coherency of the writes executed by another device into the GPU memory.

---
 doc/guides/prog_guide/gpudev.rst |  8 ++++++++
 lib/gpudev/gpudev.c              | 19 +++++++++++++++++++
 lib/gpudev/gpudev_driver.h       |  3 +++
 lib/gpudev/rte_gpudev.h          | 18 ++++++++++++++++++
 lib/gpudev/version.map           |  1 +
 5 files changed, 49 insertions(+)
doc/guides/prog_guide/gpudev.rst +8 -0
index 9aca69038c..eb5f0af817 100644
@@ -65,3 +65,11 @@ gpudev can register a CPU memory area to make it visible from a GPU device.
 Later, it's also possible to unregister that memory with gpudev.
 CPU memory registered outside of the gpudev library
 (e.g. with GPU specific library) cannot be unregistered by the gpudev library.
+
+Memory Barrier
+~~~~~~~~~~~~~~
+
+Some GPU drivers may need, under certain conditions,
+to enforce the coherency of external devices writes (e.g. NIC receiving packets)
+into the GPU memory.
+gpudev abstracts and exposes this capability.
lib/gpudev/gpudev.c +19 -0
index d0826ec881..49526b335f 100644
@@ -624,3 +624,22 @@ rte_gpu_free(int16_t dev_id, void *ptr)
 	}
 	return GPU_DRV_RET(dev->ops.mem_free(dev, ptr));
 }
+
+int
+rte_gpu_mbw(int16_t dev_id)
+{
+	struct rte_gpu *dev;
+
+	dev = gpu_get_by_id(dev_id);
+	if (dev == NULL) {
+		GPU_LOG(ERR, "memory barrier for invalid device ID %d", dev_id);
+		rte_errno = ENODEV;
+		return -rte_errno;
+	}
+
+	if (dev->ops.mbw == NULL) {
+		rte_errno = ENOTSUP;
+		return -rte_errno;
+	}
+	return GPU_DRV_RET(dev->ops.mbw(dev));
+}
lib/gpudev/gpudev_driver.h +3 -0
index 11015944a6..ab24de9e28 100644
@@ -31,6 +31,7 @@ typedef int (rte_gpu_mem_alloc_t)(struct rte_gpu *dev, size_t size, void **ptr);
 typedef int (rte_gpu_free_t)(struct rte_gpu *dev, void *ptr);
 typedef int (rte_gpu_mem_register_t)(struct rte_gpu *dev, size_t size, void *ptr);
 typedef int (rte_gpu_mem_unregister_t)(struct rte_gpu *dev, void *ptr);
+typedef int (rte_gpu_mbw_t)(struct rte_gpu *dev);
 
 struct rte_gpu_ops {
 	/* Get device info. If NULL, info is just copied. */
@@ -45,6 +46,8 @@ struct rte_gpu_ops {
 	rte_gpu_free_t *mem_free;
 	/* Unregister CPU memory in device. */
 	rte_gpu_mem_unregister_t *mem_unregister;
+	/* Enforce GPU memory write barrier. */
+	rte_gpu_mbw_t *mbw;
 };
 
 struct rte_gpu_mpshared {
lib/gpudev/rte_gpudev.h +18 -0
index fee71d60e7..650ebfd700 100644
@@ -387,6 +387,24 @@ int rte_gpu_register(int16_t dev_id, size_t size, void *ptr);
 __rte_experimental
 int rte_gpu_unregister(int16_t dev_id, void *ptr);
 
+/**
+ * @warning
+ * @b EXPERIMENTAL: this API may change without prior notice.
+ *
+ * Enforce a GPU memory write barrier.
+ *
+ * @param dev_id
+ *   Reference device ID.
+ *
+ * @return
+ *   0 on success, -rte_errno otherwise:
+ *   - ENODEV if invalid dev_id
+ *   - ENOTSUP if operation not supported by the driver
+ *   - EPERM if driver error
+ */
+__rte_experimental
+int rte_gpu_mbw(int16_t dev_id);
+
 #ifdef __cplusplus
 }
 #endif
lib/gpudev/version.map +1 -0
index d4a65ebd52..d72d470d8e 100644
@@ -13,6 +13,7 @@ EXPERIMENTAL {
 	rte_gpu_init;
 	rte_gpu_is_valid;
 	rte_gpu_malloc;
+	rte_gpu_mbw;
 	rte_gpu_register;
 	rte_gpu_unregister;
 };
Signature · 1 line
2.17.1
#124 lore raw

PATCH v5 7/9 gpudev: add communication flag

In reply to [email protected]

From: Elena Agostini <[email protected]>

In heterogeneous computing system, processing is not only in the CPU. Some tasks can be delegated to devices working in parallel. When mixing network activity with task processing there may be the need to put in communication the CPU with the device in order to synchronize operations.

The purpose of this flag is to allow the CPU and the GPU to exchange ACKs. A possible use-case is described below.

CPU:
- Trigger some task on the GPU
- Prepare some data
- Signal to the GPU the data is ready updating the communication flag

GPU:
- Do some pre-processing
- Wait for more data from the CPU polling on the communication flag
- Consume the data prepared by the CPU

---
 app/test-gpudev/main.c                 |  60 ++++++++++++++
 doc/guides/prog_guide/gpudev.rst       |  13 +++
 doc/guides/rel_notes/release_21_11.rst |   1 +
 lib/gpudev/gpudev.c                    |  92 +++++++++++++++++++++
 lib/gpudev/rte_gpudev.h                | 108 +++++++++++++++++++++++++
 lib/gpudev/version.map                 |   4 +
 6 files changed, 278 insertions(+)
app/test-gpudev/main.c +60 -0
index e3aca2225a..516a01b927 100644
@@ -154,6 +154,61 @@ register_cpu_memory(uint16_t gpu_id)
 	return 0;
 }
 
+static int
+create_update_comm_flag(uint16_t gpu_id)
+{
+	struct rte_gpu_comm_flag devflag;
+	int ret = 0;
+	uint32_t set_val;
+	uint32_t get_val;
+
+	printf("\n=======> TEST: Communication flag\n");
+
+	ret = rte_gpu_comm_create_flag(gpu_id, &devflag, RTE_GPU_COMM_FLAG_CPU);
+	if (ret < 0) {
+		fprintf(stderr, "rte_gpu_comm_create_flag returned error %d\n", ret);
+		return -1;
+	}
+
+	set_val = 25;
+	ret = rte_gpu_comm_set_flag(&devflag, set_val);
+	if (ret < 0) {
+		fprintf(stderr, "rte_gpu_comm_set_flag returned error %d\n", ret);
+		return -1;
+	}
+
+	ret = rte_gpu_comm_get_flag_value(&devflag, &get_val);
+	if (ret < 0) {
+		fprintf(stderr, "rte_gpu_comm_get_flag_value returned error %d\n", ret);
+		return -1;
+	}
+
+	printf("Communication flag value at 0x%p was set to %d and current value is %d\n", devflag.ptr, set_val, get_val);
+
+	set_val = 38;
+	ret = rte_gpu_comm_set_flag(&devflag, set_val);
+	if (ret < 0) {
+		fprintf(stderr, "rte_gpu_comm_set_flag returned error %d\n", ret);
+		return -1;
+	}
+
+	ret = rte_gpu_comm_get_flag_value(&devflag, &get_val);
+	if (ret < 0) {
+		fprintf(stderr, "rte_gpu_comm_get_flag_value returned error %d\n", ret);
+		return -1;
+	}
+
+	printf("Communication flag value at 0x%p was set to %d and current value is %d\n", devflag.ptr, set_val, get_val);
+
+	ret = rte_gpu_comm_destroy_flag(&devflag);
+	if (ret < 0) {
+		fprintf(stderr, "rte_gpu_comm_destroy_flags returned error %d\n", ret);
+		return -1;
+	}
+
+	return 0;
+}
+
 int
 main(int argc, char **argv)
 {
@@ -204,6 +259,11 @@ main(int argc, char **argv)
 	alloc_gpu_memory(gpu_id);
 	register_cpu_memory(gpu_id);
 
+	/**
+	 * Communication items test
+	 */
+	create_update_comm_flag(gpu_id);
+
 	/* clean up the EAL */
 	rte_eal_cleanup();
 	printf("Bye...\n");
doc/guides/prog_guide/gpudev.rst +13 -0
index eb5f0af817..e0db627aed 100644
@@ -32,6 +32,10 @@ This library provides a number of features:
 - Interoperability with device-specific library through generic handlers.
 - Allocate and free memory on the device.
 - Register CPU memory to make it visible from the device.
+- Communication between the CPU and the device.
+
+The whole CPU - GPU communication is implemented
+using CPU memory visible from the GPU.
 
 
 API Overview
@@ -73,3 +77,12 @@ Some GPU drivers may need, under certain conditions,
 to enforce the coherency of external devices writes (e.g. NIC receiving packets)
 into the GPU memory.
 gpudev abstracts and exposes this capability.
+
+Communication Flag
+~~~~~~~~~~~~~~~~~~
+
+Considering an application with some GPU task
+that's waiting to receive a signal from the CPU
+to move forward with the execution.
+The communication flag allocates a CPU memory GPU-visible ``uint32_t`` flag
+that can be used by the CPU to communicate with a GPU task.
doc/guides/rel_notes/release_21_11.rst +1 -0
index a4d07bda9b..78b29d9a25 100644
@@ -105,6 +105,7 @@ New Features
 
   * Device information
   * Memory management
+  * Communication flag
 
 * **Added new RSS offload types for IPv4/L4 checksum in RSS flow.**
 
lib/gpudev/gpudev.c +92 -0

95 lines not on this page · raw message

lib/gpudev/rte_gpudev.h +108 -0

126 lines not on this page · raw message

lib/gpudev/version.map +4 -0
index d72d470d8e..2fc039373a 100644
@@ -6,6 +6,10 @@ EXPERIMENTAL {
 	rte_gpu_callback_register;
 	rte_gpu_callback_unregister;
 	rte_gpu_close;
+	rte_gpu_comm_create_flag;
+	rte_gpu_comm_destroy_flag;
+	rte_gpu_comm_get_flag_value;
+	rte_gpu_comm_set_flag;
 	rte_gpu_count_avail;
 	rte_gpu_find_next;
 	rte_gpu_free;
Signature · 1 line
2.17.1
#125 lore raw

PATCH v5 8/9 gpudev: add communication list

In reply to [email protected]

From: Elena Agostini <[email protected]>

In heterogeneous computing system, processing is not only in the CPU. Some tasks can be delegated to devices working in parallel. When mixing network activity with task processing there may be the need to put in communication the CPU with the device in order to synchronize operations.

An example could be a receive-and-process application
where CPU is responsible for receiving packets in multiple mbufs and the GPU is responsible for processing the content of those packets.

The purpose of this list is to provide a buffer in CPU memory visible from the GPU that can be treated as a circular buffer
to let the CPU provide fondamental info of received packets to the GPU.

A possible use-case is described below.

CPU:
- Trigger some task on the GPU
- in a loop:
- receive a number of packets
- provide packets info to the GPU

GPU:
- Do some pre-processing
- Wait to receive a new set of packet to be processed

Layout of a communication list would be:

     -------
    |   0    | => pkt_list
    | status |
    | #pkts  |
     -------
    |   1    | => pkt_list
    | status |
    | #pkts  |
     -------
    |   2    | => pkt_list
    | status |
    | #pkts  |
     -------
    |  ....  | => pkt_list
     -------
---
 app/test-gpudev/main.c                 |  95 ++++++++++++++
 doc/guides/prog_guide/gpudev.rst       |  16 +++
 doc/guides/rel_notes/release_21_11.rst |   2 +-
 lib/gpudev/gpudev.c                    | 164 +++++++++++++++++++++++++
 lib/gpudev/meson.build                 |   2 +
 lib/gpudev/rte_gpudev.h                | 129 +++++++++++++++++++
 lib/gpudev/version.map                 |   4 +
 7 files changed, 411 insertions(+), 1 deletion(-)
app/test-gpudev/main.c +95 -0
index 516a01b927..111ed6d415 100644
@@ -209,6 +209,100 @@ create_update_comm_flag(uint16_t gpu_id)
 	return 0;
 }
 
+static int
+simulate_gpu_task(struct rte_gpu_comm_list *comm_list_item, int num_pkts)
+{
+	int idx;
+
+	if (comm_list_item == NULL)
+		return -1;
+
+	for (idx = 0; idx < num_pkts; idx++) {
+		/**
+		 * consume(comm_list_item->pkt_list[idx].addr);
+		 */
+	}
+	comm_list_item->status = RTE_GPU_COMM_LIST_DONE;
+
+	return 0;
+}
+
+static int
+create_update_comm_list(uint16_t gpu_id)
+{
+	int ret = 0;
+	int i = 0;
+	struct rte_gpu_comm_list *comm_list;
+	uint32_t num_comm_items = 1024;
+	struct rte_mbuf *mbufs[10];
+
+	printf("\n=======> TEST: Communication list\n");
+
+	comm_list = rte_gpu_comm_create_list(gpu_id, num_comm_items);
+	if (comm_list == NULL) {
+		fprintf(stderr, "rte_gpu_comm_create_list returned error %d\n", ret);
+		return -1;
+	}
+
+	/**
+	 * Simulate DPDK receive functions like rte_eth_rx_burst()
+	 */
+	for (i = 0; i < 10; i++) {
+		mbufs[i] = rte_zmalloc(NULL, sizeof(struct rte_mbuf), 0);
+		if (mbufs[i] == NULL) {
+			fprintf(stderr, "Failed to allocate fake mbufs in CPU memory.\n");
+			return -1;
+		}
+
+		memset(mbufs[i], 0, sizeof(struct rte_mbuf));
+	}
+
+	/**
+	 * Populate just the first item of  the list
+	 */
+	ret = rte_gpu_comm_populate_list_pkts(&(comm_list[0]), mbufs, 10);
+	if (ret < 0) {
+		fprintf(stderr, "rte_gpu_comm_populate_list_pkts returned error %d\n", ret);
+		return -1;
+	}
+
+	ret = rte_gpu_comm_cleanup_list(&(comm_list[0]));
+	if (ret == 0) {
+		fprintf(stderr, "rte_gpu_comm_cleanup_list erroneusly cleaned the list even if packets have not beeing consumed yet\n");
+		return -1;
+	} else {
+		fprintf(stderr, "rte_gpu_comm_cleanup_list correctly didn't clean up the packets because they have not beeing consumed yet\n");
+	}
+
+	/**
+	 * Simulate a GPU tasks going through the packet list to consume
+	 * mbufs packets and release them
+	 */
+	simulate_gpu_task(&(comm_list[0]), 10);
+
+	/**
+	 * Packets have been consumed, now the communication item
+	 * and the related mbufs can be all released
+	 */
+	ret = rte_gpu_comm_cleanup_list(&(comm_list[0]));
+	if (ret < 0) {
+		fprintf(stderr, "rte_gpu_comm_cleanup_list returned error %d\n", ret);
+		return -1;
+	}
+
+	ret = rte_gpu_comm_destroy_list(comm_list, num_comm_items);
+	if (ret < 0) {
+		fprintf(stderr, "rte_gpu_comm_destroy_list returned error %d\n", ret);
+		return -1;
+	}
+
+	for (i = 0; i < 10; i++)
+		rte_free(mbufs[i]);
+
+	printf("\nCommunication list test passed!\n");
+	return 0;
+}
+
 int
 main(int argc, char **argv)
 {
@@ -263,6 +357,7 @@ main(int argc, char **argv)
 	 * Communication items test
 	 */
 	create_update_comm_flag(gpu_id);
+	create_update_comm_list(gpu_id);
 
 	/* clean up the EAL */
 	rte_eal_cleanup();
doc/guides/prog_guide/gpudev.rst +16 -0
index e0db627aed..cbaec5a1e4 100644
@@ -86,3 +86,19 @@ that's waiting to receive a signal from the CPU
 to move forward with the execution.
 The communication flag allocates a CPU memory GPU-visible ``uint32_t`` flag
 that can be used by the CPU to communicate with a GPU task.
+
+Communication list
+~~~~~~~~~~~~~~~~~~
+
+By default, DPDK pulls free mbufs from a mempool to receive packets.
+Best practice, expecially in a multithreaded application,
+is to no make any assumption on which mbufs will be used
+to receive the next bursts of packets.
+Considering an application with a GPU memory mempool
+attached to a receive queue having some task waiting on the GPU
+to receive a new burst of packets to be processed,
+there is the need to communicate from the CPU
+the list of mbuf payload addresses where received packet have been stored.
+The ``rte_gpu_comm_*()`` functions are responsible to create a list of packets
+that can be populated with receive mbuf payload addresses
+and communicated to the task running on the GPU.
doc/guides/rel_notes/release_21_11.rst +1 -1
index 78b29d9a25..23d8591f40 100644
@@ -105,7 +105,7 @@ New Features
 
   * Device information
   * Memory management
-  * Communication flag
+  * Communication flag & list
 
 * **Added new RSS offload types for IPv4/L4 checksum in RSS flow.**
 
lib/gpudev/gpudev.c +164 -0

167 lines not on this page · raw message

lib/gpudev/meson.build +2 -0
index 608154817b..89a118f357 100644
@@ -8,3 +8,5 @@ headers = files(
 sources = files(
         'gpudev.c',
 )
+
+deps += ['mbuf']
lib/gpudev/rte_gpudev.h +129 -0

153 lines not on this page · raw message

lib/gpudev/version.map +4 -0
index 2fc039373a..45a35fa6e4 100644
@@ -6,9 +6,13 @@ EXPERIMENTAL {
 	rte_gpu_callback_register;
 	rte_gpu_callback_unregister;
 	rte_gpu_close;
+	rte_gpu_comm_cleanup_list;
 	rte_gpu_comm_create_flag;
+	rte_gpu_comm_create_list;
 	rte_gpu_comm_destroy_flag;
+	rte_gpu_comm_destroy_list;
 	rte_gpu_comm_get_flag_value;
+	rte_gpu_comm_populate_list_pkts;
 	rte_gpu_comm_set_flag;
 	rte_gpu_count_avail;
 	rte_gpu_find_next;
Signature · 1 line
2.17.1
#126 lore raw

PATCH v5 9/9 doc: add CUDA example in GPU guide

In reply to [email protected]

From: Elena Agostini <[email protected]>

---
 doc/guides/prog_guide/gpudev.rst | 122 +++++++++++++++++++++++++++++++
 1 file changed, 122 insertions(+)
doc/guides/prog_guide/gpudev.rst +122 -0
index cbaec5a1e4..1baf0c6772 100644
@@ -102,3 +102,125 @@ the list of mbuf payload addresses where received packet have been stored.
 The ``rte_gpu_comm_*()`` functions are responsible to create a list of packets
 that can be populated with receive mbuf payload addresses
 and communicated to the task running on the GPU.
+
+
+CUDA Example
+------------
+
+In the example below, there is a pseudo-code to give an example
+about how to use functions in this library in case of a CUDA application.
+
+.. code-block:: c
+
+   //////////////////////////////////////////////////////////////////////////
+   ///// gpudev library + CUDA functions
+   //////////////////////////////////////////////////////////////////////////
+   #define GPU_PAGE_SHIFT 16
+   #define GPU_PAGE_SIZE (1UL << GPU_PAGE_SHIFT)
+
+   int main() {
+       struct rte_gpu_flag quit_flag;
+       struct rte_gpu_comm_list *comm_list;
+       int nb_rx = 0;
+       int comm_list_entry = 0;
+       struct rte_mbuf * rx_mbufs[max_rx_mbufs];
+       cudaStream_t cstream;
+       struct rte_mempool *mpool_payload, *mpool_header;
+       struct rte_pktmbuf_extmem ext_mem;
+       int16_t dev_id;
+       int16_t port_id = 0;
+
+       /** Initialize CUDA objects (cstream, context, etc..). */
+       /** Use gpudev library to register a new CUDA context if any */
+       /** Let's assume the application wants to use the default context of the GPU device 0 */
+
+       dev_id = 0;
+
+       /**
+        * Create an external memory mempool using memory allocated on the GPU.
+        */
+       ext_mem.elt_size = mbufs_headroom_size;
+                   ext_mem.buf_len = RTE_ALIGN_CEIL(mbufs_num * ext_mem.elt_size, GPU_PAGE_SIZE);
+       ext_mem.buf_iova = RTE_BAD_IOVA;
+       ext_mem.buf_ptr = rte_gpu_malloc(dev_id, ext_mem.buf_len, 0);
+       rte_extmem_register(ext_mem.buf_ptr, ext_mem.buf_len, NULL, ext_mem.buf_iova, GPU_PAGE_SIZE);
+       rte_dev_dma_map(rte_eth_devices[port_id].device, ext_mem.buf_ptr, ext_mem.buf_iova, ext_mem.buf_len);
+       mpool_payload = rte_pktmbuf_pool_create_extbuf("gpu_mempool", mbufs_num,
+                                                       0, 0, ext_mem.elt_size,
+                                                       rte_socket_id(), &ext_mem, 1);
+
+       /**
+        * Create CPU - device communication flag. With this flag, the CPU can tell to the CUDA kernel
+        * to exit from the main loop.
+        */
+       rte_gpu_comm_create_flag(dev_id, &quit_flag, RTE_GPU_COMM_FLAG_CPU);
+       rte_gpu_comm_set_flag(&quit_flag , 0);
+
+       /**
+        * Create CPU - device communication list. Each entry of this list will be populated by the CPU
+        * with a new set of received mbufs that the CUDA kernel has to process.
+        */
+       comm_list = rte_gpu_comm_create_list(dev_id, num_entries);
+
+       /** A very simple CUDA kernel with just 1 CUDA block and RTE_GPU_COMM_LIST_PKTS_MAX CUDA threads. */
+       cuda_kernel_packet_processing<<<1, RTE_GPU_COMM_LIST_PKTS_MAX, 0, cstream>>>(quit_flag->ptr, comm_list, num_entries, ...);
+
+       /**
+        * For simplicity, the CPU here receives only 2 bursts of mbufs.
+        * In a real application, network activity and device processing should overlap.
+        */
+       nb_rx = rte_eth_rx_burst(port_id, queue_id, &(rx_mbufs[0]), max_rx_mbufs);
+       rte_gpu_comm_populate_list_pkts(comm_list[0], rx_mbufs, nb_rx);
+       nb_rx = rte_eth_rx_burst(port_id, queue_id, &(rx_mbufs[0]), max_rx_mbufs);
+       rte_gpu_comm_populate_list_pkts(comm_list[1], rx_mbufs, nb_rx);
+
+       /**
+        * CPU waits for the completion of the packets' processing on the CUDA kernel
+        * and then it does a cleanup of the received mbufs.
+        */
+       while(rte_gpu_comm_cleanup_list(comm_list[0]));
+       while(rte_gpu_comm_cleanup_list(comm_list[1]));
+
+       /** CPU notifies the CUDA kernel that it has to terminate */
+       rte_gpu_comm_set_flag(&quit_flag, 1);
+
+       /** gpudev objects cleanup/destruction */
+       /** CUDA cleanup */
+
+       rte_gpu_free(dev_id, ext_mem.buf_len);
+
+       /** DPDK cleanup */
+
+       return 0;
+   }
+
+   //////////////////////////////////////////////////////////////////////////
+   ///// CUDA kernel
+   //////////////////////////////////////////////////////////////////////////
+
+   void cuda_kernel(uint32_t * quit_flag_ptr, struct rte_gpu_comm_list *comm_list, int comm_list_entries) {
+      int comm_list_index = 0;
+      struct rte_gpu_comm_pkt *pkt_list = NULL;
+
+      /** Do some pre-processing operations. */
+
+      /** GPU kernel keeps checking this flag to know if it has to quit or wait for more packets. */
+      while(*quit_flag_ptr == 0)
+      {
+         if(comm_list[comm_list_index]->status != RTE_GPU_COMM_LIST_READY)
+         continue;
+
+         if(threadIdx.x < comm_list[comm_list_index]->num_pkts)
+         {
+            /** Each CUDA thread processes a different packet. */
+            packet_processing(comm_list[comm_list_index]->addr, comm_list[comm_list_index]->size, ..);
+         }
+         __threadfence();
+         __syncthreads();
+
+         /** Wait for new packets on the next communication list entry. */
+         comm_list_index = (comm_list_index+1) % comm_list_entries;
+      }
+
+      /** Do some post-processing operations. */
+   }
Signature · 1 line
2.17.1
#127 lore raw

Re: [dpdk-dev] [PATCH v5 0/9] GPU library

In reply to [email protected]

08/11/2021 19:57, [email protected]:

Quoted 8 lines

Elena Agostini (6):
gpudev: introduce GPU device class library
gpudev: add memory API
gpudev: add memory barrier
gpudev: add communication flag
gpudev: add communication list
doc: add CUDA example in GPU guide

Quoted 4 lines

Thomas Monjalon (3):
gpudev: add event notification
gpudev: add child device representing a device context
gpudev: support multi-process

Applied with last details fixed.

For reference, the techboard approved gpudev integration:
http://inbox.dpdk.org/dev/DM6PR11MB44917C0FF9926C1BADBCA7149A8B9@DM6PR11MB4491.namprd11.prod.outlook.com/