commit d216e2d395bdd4d5be1fc0dc0aae1482d20eefe7
parent c468b48393f72e6ad92c9dd79d39d169fe7bac70
Author: Vincent Forest <vincent.forest@meso-star.com>
Date: Thu, 7 Dec 2017 13:52:38 +0100
Add a CMakeLists file and implement the Device API
Diffstat:
3 files changed, 240 insertions(+), 0 deletions(-)
diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt
@@ -0,0 +1,109 @@
+# Copyright (C) CNRS 2016-2017
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+cmake_minimum_required(VERSION 3.0)
+project(stardis C)
+enable_testing()
+
+set(SDIS_SOURCE_DIR ${PROJECT_SOURCE_DIR}/../src)
+option(NO_TEST "Do not build tests" OFF)
+
+################################################################################
+# Check dependencies
+################################################################################
+find_package(RCMake 0.3 REQUIRED)
+find_package(RSys 0.6 REQUIRED)
+find_package(OpenMP 1.2 REQUIRED)
+
+set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${RCMAKE_SOURCE_DIR})
+include(rcmake)
+include(rcmake_runtime)
+
+rcmake_append_runtime_dirs(_runtime_dirs RSys)
+
+################################################################################
+# Configure and define targets
+################################################################################
+set(VERSION_MAJOR 1)
+set(VERSION_MINOR 0)
+set(VERSION_PATCH 0)
+set(VERSION ${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH})
+
+set(SDIS_FILES_SRC
+ sdis_device.c)
+
+set(SDIS_FILES_INC_API
+ sdis.h)
+
+set(SDIS_FILES_INC
+ sdis_device_c.h)
+
+set(SDIS_FILES_DOC COPYING README.md)
+
+# Prepend each file by `SDIS_SOURCE_DIR'
+rcmake_prepend_path(SDIS_FILES_SRC ${SDIS_SOURCE_DIR})
+rcmake_prepend_path(SDIS_FILES_INC ${SDIS_SOURCE_DIR})
+rcmake_prepend_path(SDIS_FILES_INC_API ${SDIS_SOURCE_DIR})
+rcmake_prepend_path(SDIS_FILES_DOC ${PROJECT_SOURCE_DIR}/../)
+
+add_library(sdis SHARED
+ ${SDIS_FILES_SRC}
+ ${SDIS_FILES_INC}
+ ${SDIS_FILES_INC_API})
+target_link_libraries(sdis RSys)
+
+set_target_properties(sdis PROPERTIES
+ DEFINE_SYMBOL SDIS_SHARED_BUILD
+ COMPILE_FLAGS ${OpenMP_C_FLAGS}
+ VERSION ${VERSION}
+ SOVERSION ${VERSION_MAJOR})
+rcmake_copy_runtime_libraries(sdis)
+
+if(CMAKE_COMPILER_IS_GNUCC)
+ set_target_properties(sdis PROPERTIES LINK_FLAGS ${OpenMP_C_FLAGS})
+endif()
+
+rcmake_setup_devel(sdis Stardis ${VERSION} sdis_version.h)
+
+################################################################################
+# Add tests
+################################################################################
+if(NOT NO_TEST)
+ function(build_test _name)
+ add_executable(${_name} ${SDIS_SOURCE_DIR}/${_name}.c)
+ target_link_libraries(${_name} RSys sdis)
+ endfunction()
+
+ function(register_test _name)
+ add_test(${_name} ${ARGN})
+ rcmake_set_test_runtime_dirs(${_name} _runtime_dirs)
+ endfunction()
+
+ function(new_test _name)
+ build_test(${_name})
+ register_test(${_name} ${_name})
+ endfunction()
+endif()
+
+################################################################################
+# Define output & install directories
+################################################################################
+install(TARGETS sdis
+ ARCHIVE DESTINATION bin
+ LIBRARY DESTINATION lib
+ RUNTIME DESTINATION bin)
+install(FILES ${SDIS_FILES_INC_API} DESTINATION include/)
+install(FILES ${SDIS_FILES_DOC} DESTINATION share/doc/stardis)
+
diff --git a/src/sdis_device.c b/src/sdis_device.c
@@ -0,0 +1,101 @@
+/* Copyright (C) |Meso|Star> 2016-2017 (contact@meso-star.com)
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>. */
+
+#include "sdis.h"
+#include "sdis_device_c.h"
+
+#include <rsys/logger.h>
+#include <rsys/mem_allocator.h>
+
+#include <omp.h>
+
+/*******************************************************************************
+ * Helper functions
+ ******************************************************************************/
+static void
+device_release(ref_T* ref)
+{
+ struct sdis_device* dev;
+ ASSERT(ref);
+ dev = CONTAINER_OF(ref, struct sdis_device, ref);
+ MEM_RM(dev->allocator, dev);
+}
+
+/*******************************************************************************
+ * Exported functions
+ ******************************************************************************/
+res_T
+sdis_device_create
+ (struct logger* logger,
+ struct mem_allocator* mem_allocator,
+ const unsigned nthreads_hint,
+ const int verbose,
+ struct sdis_device** out_dev)
+{
+ struct logger* log = NULL;
+ struct sdis_device* dev = NULL;
+ struct mem_allocator* allocator = NULL;
+ res_T res = RES_BAD_ARG;
+
+ if(nthreads_hint == 0 || !out_dev) {
+ res = RES_BAD_ARG;
+ goto error;
+ }
+
+ log = logger ? logger : LOGGER_DEFAULT;
+ allocator = mem_allocator ? mem_allocator : &mem_default_allocator;
+ dev = MEM_CALLOC(allocator, 1, sizeof(struct sdis_device));
+ if(!dev) {
+ if(verbose) {
+ /* Do not use helper log functions since dev is not initialised */
+ CHK(logger_print
+ (log, LOG_ERROR, "Cannot allocate the Stardis device.\n") == RES_OK);
+ }
+ res = RES_MEM_ERR;
+ goto error;
+ }
+ dev->logger = log;
+ dev->allocator = allocator;
+ dev->verbose = verbose;
+ dev->nthreads = MMIN(nthreads_hint, (unsigned)omp_get_num_procs());
+ ref_init(&dev->ref);
+
+exit:
+ if(out_dev) *out_dev = dev;
+ return res;
+error:
+ if(dev) {
+ SDIS(device_ref_put(dev));
+ dev = NULL;
+ }
+ goto exit;
+}
+
+res_T
+sdis_device_ref_get(struct sdis_device* dev)
+{
+ if(!dev) return RES_BAD_ARG;
+ ref_get(&dev->ref);
+ return RES_OK;
+}
+
+res_T
+sdis_device_ref_put(struct sdis_device* dev)
+{
+ if(!dev) return RES_BAD_ARG;
+ ref_put(&dev->ref, device_release);
+ return RES_OK;
+}
+
diff --git a/src/sdis_device_c.h b/src/sdis_device_c.h
@@ -0,0 +1,30 @@
+/* Copyright (C) |Meso|Star> 2016-2017 (contact@meso-star.com)
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>. */
+
+#ifndef SDIS_DEVICE_C_H
+#define SDIS_DEVICE_C_H
+
+#include <rsys/ref_count.h>
+
+struct sdis_device {
+ struct logger* logger;
+ struct mem_allocator* allocator;
+ unsigned nthreads;
+ int verbose;
+
+ ref_T ref;
+};
+
+#endif /* SDIS_DEVICE_C_H */