Skip to content
This repository was archived by the owner on Mar 22, 2023. It is now read-only.

Commit

Permalink
Merge pull request #22 from kswiecicki/dml-mover
Browse files Browse the repository at this point in the history
masync: add dml mover implementation
  • Loading branch information
wlemkows authored Dec 22, 2021
2 parents b94a157 + 7c8c38a commit f893507
Show file tree
Hide file tree
Showing 16 changed files with 420 additions and 14 deletions.
6 changes: 6 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ option(BUILD_DOC "build documentation" ON)
option(BUILD_EXAMPLES "build examples" ON)
option(BUILD_TESTS "build tests" ON)
option(TESTS_USE_VALGRIND "enable tests with valgrind (if found)" ON)
option(COMPILE_DML "compile miniasync dml implementation library" OFF)

include(FindPerl)
include(FindThreads)
Expand Down Expand Up @@ -182,3 +183,8 @@ endif()
if(NOT "${CPACK_GENERATOR}" STREQUAL "")
include(${CMAKE_SOURCE_DIR}/cmake/packages.cmake)
endif()

# add CMakeLists.txt from the mover directory
if (COMPILE_DML)
add_subdirectory(extras/dml)
endif()
4 changes: 2 additions & 2 deletions examples/basic/basic.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ async_memcpy_print(struct vdm *vdm, void *dest, void *src, size_t n)
{
struct async_memcpy_print_fut chain;
FUTURE_CHAIN_ENTRY_INIT(&chain.data.memcpy,
vdm_memcpy(vdm, dest, src, n),
vdm_memcpy(vdm, dest, src, n, 0),
memcpy_to_print_map, (void *)0xd);
FUTURE_CHAIN_ENTRY_INIT(&chain.data.print, async_print(NULL), NULL,
NULL);
Expand All @@ -98,7 +98,7 @@ main(int argc, char *argv[])

struct vdm *pthread_mover = vdm_new(vdm_descriptor_pthreads_polled());
struct vdm_memcpy_future a_to_b =
vdm_memcpy(pthread_mover, buf_b, buf_a, testbuf_size);
vdm_memcpy(pthread_mover, buf_b, buf_a, testbuf_size, 0);

runtime_wait(r, FUTURE_AS_RUNNABLE(&a_to_b));

Expand Down
11 changes: 11 additions & 0 deletions extras/dml/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
.*
!.gitignore
!.gitattributes
!.github/
!.travis.yml
!.mailmap
!.version
build/
*~
*.swp
~*
42 changes: 42 additions & 0 deletions extras/dml/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#
# SPDX-License-Identifier: BSD-3-Clause
# Copyright 2021, Intel Corporation
#

project(miniasync-dml C)

set(MINIASYNC_DML_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/include)

add_cstyle(miniasync-dml
${CMAKE_CURRENT_SOURCE_DIR}/*.[ch]
${CMAKE_CURRENT_SOURCE_DIR}/include/*.h)

add_check_whitespace(miniasync-dml
${CMAKE_CURRENT_SOURCE_DIR}/*.[ch]
${CMAKE_CURRENT_SOURCE_DIR}/include/*.h)

set(DML_SOURCES
vdm_dml.c
)

add_library(miniasync-dml STATIC ${DML_SOURCES})

target_include_directories(miniasync-dml PRIVATE .
${MINIASYNC_DML_INCLUDE_DIR}
${MINIASYNC_INCLUDE_DIR}
${MINIASYNC_SOURCE_DIR})

set_target_properties(miniasync-dml PROPERTIES
PUBLIC_HEADER ${MINIASYNC_DML_INCLUDE_DIR}/libminiasync-dml.h
)

install(TARGETS miniasync-dml
PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})

# Install headers included in public header
install(DIRECTORY ${MINIASYNC_DML_INCLUDE_DIR}/libminiasync-dml
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
FILES_MATCHING PATTERN "*.h"
)
28 changes: 28 additions & 0 deletions extras/dml/include/libminiasync-dml.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* SPDX-License-Identifier: BSD-3-Clause */
/* Copyright 2021, Intel Corporation */

/*
* libminiasync-dml.h -- definition of miniasync dml implementation
*/

#ifndef MINIASYNC_DML_H
#define MINIASYNC_DML_H 1

#include <stddef.h>
#include <stdio.h>

#include "libminiasync-dml/vdm_dml.h"

#ifdef __cplusplus
extern "C" {
#endif

/* MINIASYNC_DML memory flags */
#define MINIASYNC_DML_F_MEM_DURABLE (1U << 0)
#define MINIASYNC_DML_F_MEM_VALID_FLAGS (MINIASYNC_DML_F_MEM_DURABLE)

#ifdef __cplusplus
}
#endif

#endif /* MINIASYNC_DML_H */
20 changes: 20 additions & 0 deletions extras/dml/include/libminiasync-dml/vdm_dml.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/* SPDX-License-Identifier: BSD-3-Clause */
/* Copyright 2021, Intel Corporation */

#ifndef VDM_DML_H
#define VDM_DML_H 1

#include <dml/dml.h>
#include <libminiasync.h>

#ifdef __cplusplus
extern "C" {
#endif

struct vdm_descriptor *vdm_descriptor_dml(void);
struct vdm_descriptor *vdm_descriptor_dml_async(void);

#ifdef __cplusplus
}
#endif
#endif /* VDM_DML_H */
212 changes: 212 additions & 0 deletions extras/dml/vdm_dml.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
// SPDX-License-Identifier: BSD-3-Clause
/* Copyright 2021, Intel Corporation */

#include <dml/dml.h>
#include <libminiasync.h>
#include <stdbool.h>
#include <stdlib.h>

#include "libminiasync-dml.h"
#include "core/out.h"
#include "core/util.h"

/*
* vdm_dml_translate_flags -- translate miniasync-dml flags into dml flags
*/
static uint64_t
vdm_dml_translate_flags(uint64_t flags)
{
ASSERTeq((flags & ~MINIASYNC_DML_F_MEM_VALID_FLAGS), 0);

uint64_t tflags = 0;
for (uint64_t iflag = 1; flags > 0; iflag = iflag << 1) {
if ((flags & iflag) == 0)
continue;

switch (iflag) {
case MINIASYNC_DML_F_MEM_DURABLE:
tflags |= DML_FLAG_DST1_DURABLE;
break;
default: /* shouldn't be possible */
ASSERT(0);
}

/* remove translated flag from the flags to be translated */
flags = flags & (~iflag);
}

return tflags;
}

/*
* vdm_dml_memcpy_job_new -- create a new memcpy job struct
*/
static dml_job_t *
vdm_dml_memcpy_job_new(void *dest, void *src, size_t n, uint64_t flags)
{
dml_status_t status;
uint32_t job_size;
dml_job_t *dml_job = NULL;

status = dml_get_job_size(DML_PATH_HW, &job_size);
ASSERTeq(status, DML_STATUS_OK);

dml_job = (dml_job_t *)malloc(job_size);

status = dml_init_job(DML_PATH_HW, dml_job);
ASSERTeq(status, DML_STATUS_OK);

dml_job->operation = DML_OP_MEM_MOVE;
dml_job->source_first_ptr = (uint8_t *)src;
dml_job->destination_first_ptr = (uint8_t *)dest;
dml_job->source_length = n;
dml_job->destination_length = n;
dml_job->flags = DML_FLAG_COPY_ONLY | flags;

return dml_job;
}

/*
* vdm_dml_memcpy_job_delete -- delete job struct
*/
static void
vdm_dml_memcpy_job_delete(dml_job_t **dml_job)
{
dml_finalize_job(*dml_job);
free(*dml_job);
}

/*
* vdm_dml_memcpy_job_execute -- execute memcpy job (blocking)
*/
static void *
vdm_dml_memcpy_job_execute(dml_job_t *dml_job)
{
dml_status_t status;
status = dml_execute_job(dml_job);
ASSERTeq(status, DML_STATUS_OK);

return dml_job->destination_first_ptr;
}

/*
* vdm_dml_memcpy_job_submit -- submit memcpy job (nonblocking)
*/
static void *
vdm_dml_memcpy_job_submit(dml_job_t *dml_job)
{
dml_status_t status;
status = dml_submit_job(dml_job);
ASSERTeq(status, DML_STATUS_OK);

return dml_job->destination_first_ptr;
}

/*
* vdm_dml_check -- check status of memcpy job executed synchronously
*/
static enum future_state
vdm_dml_check(struct future_context *context)
{
struct vdm_memcpy_data *data = future_context_get_data(context);

int complete;
util_atomic_load64(&data->complete, &complete);

return (complete) ? FUTURE_STATE_COMPLETE : FUTURE_STATE_RUNNING;
}

/*
* vdm_dml_check_delete_job -- check status of memcpy job executed
* asynchronously
*/
static enum future_state
vdm_dml_check_delete_job(struct future_context *context)
{
struct vdm_memcpy_data *data = future_context_get_data(context);
dml_job_t *dml_job = (dml_job_t *)data->extra;

dml_status_t status = dml_check_job(dml_job);
ASSERTne(status, DML_STATUS_JOB_CORRUPTED);

enum future_state state = (status == DML_STATUS_OK) ?
FUTURE_STATE_COMPLETE : FUTURE_STATE_RUNNING;

if (state == FUTURE_STATE_COMPLETE)
vdm_dml_memcpy_job_delete(&dml_job);

return state;
}

/*
* vdm_dml_memcpy_sync -- execute dml synchronous memcpy operation
*/
static void
vdm_dml_memcpy_sync(void *runner, struct future_notifier *notifier,
struct future_context *context)
{
struct vdm_memcpy_data *data = future_context_get_data(context);
struct vdm_memcpy_output *output = future_context_get_output(context);

uint64_t tflags = vdm_dml_translate_flags(data->flags);
dml_job_t *dml_job = vdm_dml_memcpy_job_new(data->dest, data->src,
data->n, tflags);
output->dest = vdm_dml_memcpy_job_execute(dml_job);
vdm_dml_memcpy_job_delete(&dml_job);
data->vdm_cb(context);
}

/*
* dml_synchronous_descriptor -- dml synchronous memcpy descriptor
*/
static struct vdm_descriptor dml_synchronous_descriptor = {
.vdm_data_init = NULL,
.vdm_data_fini = NULL,
.memcpy = vdm_dml_memcpy_sync,
.check = vdm_dml_check,
};

/*
* vdm_descriptor_dml -- return dml synchronous memcpy descriptor
*/
struct vdm_descriptor *
vdm_descriptor_dml(void)
{
return &dml_synchronous_descriptor;
}

/*
* vdm_dml_memcpy_async -- execute dml asynchronous memcpy operation
*/
static void
vdm_dml_memcpy_async(void *runner, struct future_notifier *notifier,
struct future_context *context)
{
struct vdm_memcpy_data *data = future_context_get_data(context);
struct vdm_memcpy_output *output = future_context_get_output(context);

uint64_t tflags = vdm_dml_translate_flags(data->flags);
dml_job_t *dml_job = vdm_dml_memcpy_job_new(data->dest, data->src,
data->n, tflags);
data->extra = dml_job;
output->dest = vdm_dml_memcpy_job_submit(dml_job);
}

/*
* dml_synchronous_descriptor -- dml asynchronous memcpy descriptor
*/
static struct vdm_descriptor dml_asynchronous_descriptor = {
.vdm_data_init = NULL,
.vdm_data_fini = NULL,
.memcpy = vdm_dml_memcpy_async,
.check = vdm_dml_check_delete_job,
};

/*
* vdm_descriptor_dml -- return dml asynchronous memcpy descriptor
*/
struct vdm_descriptor *
vdm_descriptor_dml_async(void)
{
return &dml_asynchronous_descriptor;
}
2 changes: 1 addition & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ set(SOURCES
vdm.c
)

add_library(miniasync SHARED ${SOURCES})
add_library(miniasync STATIC ${SOURCES})

target_include_directories(miniasync PRIVATE . include)

Expand Down
7 changes: 7 additions & 0 deletions src/include/libminiasync/future.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@
#include <unistd.h>
#include <stdint.h>

#ifdef __cplusplus
extern "C" {
#endif

enum future_state {
FUTURE_STATE_IDLE,
FUTURE_STATE_COMPLETE,
Expand Down Expand Up @@ -178,4 +182,7 @@ enum future_state async_chain_impl(struct future_context *ctx,
#define FUTURE_CHAIN_INIT(_futurep)\
FUTURE_INIT((_futurep), async_chain_impl)

#ifdef __cplusplus
}
#endif
#endif /* FUTURE_H */
7 changes: 7 additions & 0 deletions src/include/libminiasync/runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@

#include "future.h"

#ifdef __cplusplus
extern "C" {
#endif

struct runtime;

struct runtime *runtime_new(void);
Expand All @@ -30,4 +34,7 @@ void runtime_wait_multiple(struct runtime *runtime, struct future *futs[],

void runtime_wait(struct runtime *runtime, struct future *fut);

#ifdef __cplusplus
}
#endif
#endif /* RUNTIME_H */
Loading

0 comments on commit f893507

Please sign in to comment.