From patchwork Thu Sep 11 19:34:33 2008 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yuji Mano X-Patchwork-Id: 246 X-Patchwork-Delegate: yuji.mano@am.sony.com Return-Path: X-Original-To: patchwork@ozlabs.org Delivered-To: patchwork@ozlabs.org Received: from ozlabs.org (localhost [127.0.0.1]) by ozlabs.org (Postfix) with ESMTP id 6071CDEA90 for ; Fri, 12 Sep 2008 06:39:26 +1000 (EST) X-Original-To: cbe-oss-dev@ozlabs.org Delivered-To: cbe-oss-dev@ozlabs.org Received: from IE1EHSOBE003.bigfish.com (outbound-dub.frontbridge.com [213.199.154.16]) by ozlabs.org (Postfix) with ESMTP id 43712DE365 for ; Fri, 12 Sep 2008 06:37:22 +1000 (EST) Received: from mail15-dub-R.bigfish.com (10.5.252.3) by IE1EHSOBE003.bigfish.com (10.5.252.23) with Microsoft SMTP Server id 8.1.291.1; Thu, 11 Sep 2008 20:37:17 +0000 Received: from mail15-dub (localhost.localdomain [127.0.0.1]) by mail15-dub-R.bigfish.com (Postfix) with ESMTP id 81B1811D013F for ; Thu, 11 Sep 2008 20:37:17 +0000 (UTC) X-BigFish: VS-4(zz541L936fQ34f5jzz10c0j10d3izz2b68k1497iz2dh6bh61h) X-Spam-TCS-SCL: 0:0 Received: by mail15-dub (MessageSwitch) id 1221165429362158_10488; Thu, 11 Sep 2008 20:37:09 +0000 (UCT) Received: from mail8.fw-sd.sony.com (mail8.fw-sd.sony.com [160.33.66.75]) by mail15-dub.bigfish.com (Postfix) with ESMTP id C3FF91A5009B for ; Thu, 11 Sep 2008 20:37:07 +0000 (UTC) Received: from mail3.sjc.in.sel.sony.com (mail3.sjc.in.sel.sony.com [43.134.1.211]) by mail8.fw-sd.sony.com (8.14.2/8.14.2) with ESMTP id m8BKb6gi021119 for ; Thu, 11 Sep 2008 20:37:06 GMT Received: from USSDIXIM02.am.sony.com (ussdixim02.am.sony.com [43.130.140.34]) by mail3.sjc.in.sel.sony.com (8.12.11/8.12.11) with ESMTP id m8BKb6tS021717 for ; Thu, 11 Sep 2008 20:37:06 GMT Received: from ussdixms03.am.sony.com ([43.130.140.23]) by USSDIXIM02.am.sony.com with Microsoft SMTPSVC(5.0.2195.6713); Thu, 11 Sep 2008 13:37:06 -0700 Received: from [43.135.148.175] ([43.135.148.175]) by ussdixms03.am.sony.com with Microsoft SMTPSVC(5.0.2195.6713); Thu, 11 Sep 2008 13:37:05 -0700 Message-ID: <48C972C9.9040106@am.sony.com> Date: Thu, 11 Sep 2008 12:34:33 -0700 From: Yuji Mano User-Agent: Thunderbird 2.0.0.5 (X11/20070719) MIME-Version: 1.0 To: CBE Development X-Enigmail-Version: 0.95.7 X-OriginalArrivalTime: 11 Sep 2008 20:37:05.0850 (UTC) FILETIME=[277121A0:01C9144E] X-SEL-encryption-scan: scanned Subject: [Cbe-oss-dev] [PATCH 03/11]MARS: Add mars context header source X-BeenThere: cbe-oss-dev@ozlabs.org X-Mailman-Version: 2.1.11 Precedence: list List-Id: Discussion about Open Source Software for the Cell Broadband Engine List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: cbe-oss-dev-bounces+patchwork=ozlabs.org@ozlabs.org Errors-To: cbe-oss-dev-bounces+patchwork=ozlabs.org@ozlabs.org This adds a new header specifically for all MARS context types and API for the host-side library, and removes them from the general "mars/mars.h" header. Signed-off-by: Yuji Mano Acked-by: Kazunori Asayama --- a/include/Makefile.am +++ b/include/Makefile.am @@ -50,6 +50,7 @@ host_HEADERS = \ common/mars/mars_task_queue_types.h \ common/mars/mars_task_semaphore_types.h \ host/mars/mars.h \ + host/mars/mars_context.h \ host/mars/mars_mutex.h \ host/mars/mars_task.h \ host/mars/mars_task_barrier.h \ --- a/include/host/mars/mars.h +++ b/include/host/mars/mars.h @@ -44,92 +44,9 @@ * \brief [host] MARS API */ -#if defined(__cplusplus) -extern "C" { -#endif - -#include +#include "mars/mars_context.h" #include "mars/mars_error.h" #include "mars/mars_mutex.h" #include "mars/mars_task.h" -/** - * \ingroup group_mars_context - * \brief MARS params structure - * - * This structure is initialized by the user and passed into - * \ref mars_initialize for MARS context initialization. - */ -struct mars_params { - /** number of mpus utilized by each MARS context - * [between 0 and maximum available physical MPUs] - */ - unsigned int num_mpus; -}; - -/** - * \ingroup group_mars_context - * \brief MARS context structure - * - * An instance of this structure must be created and initialized before - * using any of the MARS API. - */ -struct mars_context { - /* parameters for the MARS context */ - struct mars_params *params; - /* parameters for the MARS kernel */ - struct mars_kernel_params *kernel_params; - /* process queue where process requests are added */ - struct mars_workload_queue *workload_queue; - /* array of mpu context threads */ - pthread_t *mpu_context_threads; - /* num of mpu context threads */ - unsigned int mpu_context_count; -}; - -/** - * \ingroup group_mars_context - * \brief [host] Initializes a single MARS context. - * - * This function initializes a single MARS context. A MARS context must be - * initialized before any of the MARS functionality can be used. This will - * create the MPU contexts that are each loaded with and run the MARS kernel. - * The MARS kernel on each MPU will continue to run until the MARS context - * is finalized through \ref mars_finalize. - * - * \note If there are multiple MARS contexts initialized in the system, then - * each MARS context will suffer the large over head of MPU context switches. - * - * \param[out] ctx - address of context instance to initialize - * \param[in] params - pointer to MARS parameters structure - * \return - * MARS_SUCCESS - successfully initialized MARS context - * \n MARS_ERROR_NULL - null pointer specified - * \n MARS_ERROR_PARAMS - bad MARS params specified - * \n MARS_ERROR_MEMORY - not enough memory - * \n MARS_ERROR_INTERNAL - some internal error occurred - */ -int mars_initialize(struct mars_context *ctx, struct mars_params *params); - -/** - * \ingroup group_mars_context - * \brief [host] Finalizes a single MARS context. - * - * This function finalizes a single MARS context that was previously - * initialized by \ref mars_initialize. In order to successfully finalize - * a MARS context, all workloads added to the workload queue must be - * completed and finalized so that the workload queue is empty. - * - * \param[in] ctx - address of context instance to finalize - * \return - * MARS_SUCCESS - successfully finalized MARS context - * \n MARS_ERROR_NULL - null pointer specified - * \n MARS_ERROR_STATE - workload queue is not empty - */ -int mars_finalize(struct mars_context *ctx); - -#if defined(__cplusplus) -} -#endif - #endif --- /dev/null +++ b/include/host/mars/mars_context.h @@ -0,0 +1,132 @@ +/* + * Copyright 2008 Sony Corporation of America + * + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this Library and associated documentation files (the + * "Library"), to deal in the Library without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Library, and to + * permit persons to whom the Library is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Library. + * + * If you modify the Library, you may copy and distribute your modified + * version of the Library in object code or as an executable provided + * that you also do one of the following: + * + * Accompany the modified version of the Library with the complete + * corresponding machine-readable source code for the modified version + * of the Library; or, + * + * Accompany the modified version of the Library with a written offer + * for a complete machine-readable copy of the corresponding source + * code of the modified version of the Library. + * + * + * THE LIBRARY IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * LIBRARY OR THE USE OR OTHER DEALINGS IN THE LIBRARY. + */ + +#ifndef MARS_CONTEXT_H +#define MARS_CONTEXT_H + +/** + * \file + * \ingroup group_mars_context + * \brief [host] MARS Context API + */ + +#include + +/** + * \ingroup group_mars_context + * \brief MARS params structure + * + * This structure is initialized by the user and passed into + * \ref mars_initialize for MARS context initialization. + */ +struct mars_params { + /** number of mpus utilized by each MARS context + * [between 0 and maximum available physical MPUs] + */ + unsigned int num_mpus; +}; + +/** + * \ingroup group_mars_context + * \brief MARS context structure + * + * An instance of this structure must be created and initialized before + * using any of the MARS API. + */ +struct mars_context { + /* parameters for the MARS context */ + struct mars_params *params; + /* parameters for the MARS kernel */ + struct mars_kernel_params *kernel_params; + /* process queue where process requests are added */ + struct mars_workload_queue *workload_queue; + /* array of mpu context threads */ + pthread_t *mpu_context_threads; + /* num of mpu context threads */ + unsigned int mpu_context_count; +}; + +#if defined(__cplusplus) +extern "C" { +#endif + +/** + * \ingroup group_mars_context + * \brief [host] Initializes a single MARS context. + * + * This function initializes a single MARS context. A MARS context must be + * initialized before any of the MARS functionality can be used. This will + * create the MPU contexts that are each loaded with and run the MARS kernel. + * The MARS kernel on each MPU will continue to run until the MARS context + * is finalized through \ref mars_finalize. + * + * \note If there are multiple MARS contexts initialized in the system, then + * each MARS context will suffer the large over head of MPU context switches. + * + * \param[out] ctx - address of context instance to initialize + * \param[in] params - pointer to MARS parameters structure + * \return + * MARS_SUCCESS - successfully initialized MARS context + * \n MARS_ERROR_NULL - null pointer specified + * \n MARS_ERROR_PARAMS - bad MARS params specified + * \n MARS_ERROR_MEMORY - not enough memory + * \n MARS_ERROR_INTERNAL - some internal error occurred + */ +int mars_initialize(struct mars_context *ctx, struct mars_params *params); + +/** + * \ingroup group_mars_context + * \brief [host] Finalizes a single MARS context. + * + * This function finalizes a single MARS context that was previously + * initialized by \ref mars_initialize. In order to successfully finalize + * a MARS context, all workloads added to the workload queue must be + * completed and finalized so that the workload queue is empty. + * + * \param[in] ctx - address of context instance to finalize + * \return + * MARS_SUCCESS - successfully finalized MARS context + * \n MARS_ERROR_NULL - null pointer specified + * \n MARS_ERROR_STATE - workload queue is not empty + */ +int mars_finalize(struct mars_context *ctx); + +#if defined(__cplusplus) +} +#endif + +#endif --- a/src/host/lib/Makefile.am +++ b/src/host/lib/Makefile.am @@ -69,7 +69,7 @@ libmars_la_LDFLAGS = \ -Wl,../../mpu/kernel/mars_kernel.eo libmars_la_SOURCES = \ - mars.c \ + mars_context.c \ mars_mutex.c \ mars_workload_queue.c \ mars_task.c \ --- a/src/host/lib/mars.c +++ /dev/null @@ -1,214 +0,0 @@ -/* - * Copyright 2008 Sony Corporation of America - * - * - * Permission is hereby granted, free of charge, to any person obtaining - * a copy of this Library and associated documentation files (the - * "Library"), to deal in the Library without restriction, including - * without limitation the rights to use, copy, modify, merge, publish, - * distribute, sublicense, and/or sell copies of the Library, and to - * permit persons to whom the Library is furnished to do so, subject to - * the following conditions: - * - * The above copyright notice and this permission notice shall be - * included in all copies or substantial portions of the Library. - * - * If you modify the Library, you may copy and distribute your modified - * version of the Library in object code or as an executable provided - * that you also do one of the following: - * - * Accompany the modified version of the Library with the complete - * corresponding machine-readable source code for the modified version - * of the Library; or, - * - * Accompany the modified version of the Library with a written offer - * for a complete machine-readable copy of the corresponding source - * code of the modified version of the Library. - * - * - * THE LIBRARY IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, - * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. - * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY - * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, - * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE - * LIBRARY OR THE USE OR OTHER DEALINGS IN THE LIBRARY. - */ - -#include -#include -#include -#include - -#include "mars/mars.h" -#include "mars/mars_workload_queue.h" -#include "mars/mars_kernel_types.h" -#include "mars/mars_error.h" -#include "mars/mars_debug.h" - -extern struct spe_program_handle mars_kernel_entry; - -static unsigned int num_mpus_max(void) -{ - return spe_cpu_info_get(SPE_COUNT_PHYSICAL_SPES, -1); -} - -static void *mpu_context_thread(void *arg) -{ - MARS_ASSERT(arg); - - int ret; - unsigned int entry = SPE_DEFAULT_ENTRY; - struct mars_kernel_params *params = (struct mars_kernel_params *)arg; - struct spe_context *spe; - - spe = spe_context_create(0, NULL); - MARS_CHECK_RET(spe, (void *)MARS_ERROR_INTERNAL); - - ret = spe_program_load(spe, &mars_kernel_entry); - MARS_CHECK_RET(!ret, (void *)MARS_ERROR_INTERNAL); - - ret = spe_context_run(spe, &entry, 0, params, NULL, NULL); - MARS_CHECK_RET(!ret, (void *)MARS_ERROR_INTERNAL); - - ret = spe_context_destroy(spe); - MARS_CHECK_RET(!ret, (void *)MARS_ERROR_INTERNAL); - - return (void *)MARS_SUCCESS; -} - -static int create_mpu_contexts(struct mars_context *mars, - unsigned int num_mpu_contexts) -{ - MARS_ASSERT(mars); - - int ret; - unsigned int i; - - /* create threads for each mpu context */ - for (i = 0; i < num_mpu_contexts; i++) { - struct mars_kernel_params *params = &mars->kernel_params[i]; - params->id = i; - params->mars_context_ea = - (uint64_t)(uintptr_t)mars; - params->workload_queue_ea = - (uint64_t)(uintptr_t)mars->workload_queue; - - ret = pthread_create(&mars->mpu_context_threads[i], NULL, - mpu_context_thread, params); - MARS_CHECK_RET(!ret, MARS_ERROR_INTERNAL); - - mars->mpu_context_count++; - } - - return MARS_SUCCESS; -} - -static int destroy_mpu_contexts(struct mars_context *mars) -{ - MARS_ASSERT(mars); - - int ret; - void *p_ret; - unsigned int i; - - /* join all mpu context threads */ - for (i = 0; i < mars->mpu_context_count; i++) { - ret = pthread_join(mars->mpu_context_threads[i], &p_ret); - MARS_CHECK_RET(!ret && !p_ret, MARS_ERROR_INTERNAL); - } - - return MARS_SUCCESS; -} - -int mars_initialize(struct mars_context *mars, struct mars_params *params) -{ - MARS_CHECK_RET(mars, MARS_ERROR_NULL); - - MARS_PRINT("Initialize MARS Context (%p)\n", mars); - - int ret; - unsigned int num_mpu_contexts; - - /* zero context */ - memset(mars, 0, sizeof(struct mars_context)); - - /* allocate and initialize params */ - if (params) { - MARS_CHECK_RET(params->num_mpus <= num_mpus_max(), - MARS_ERROR_PARAMS); - - mars->params = (struct mars_params *) - malloc(sizeof(struct mars_params)); - MARS_CHECK_RET(mars->params, MARS_ERROR_MEMORY); - memcpy(mars->params, params, sizeof(struct mars_params)); - - num_mpu_contexts = mars->params->num_mpus; - } else { - mars->params = NULL; - - num_mpu_contexts = num_mpus_max(); - } - - /* allocate kernel params */ - mars->kernel_params = (struct mars_kernel_params *) - memalign(MARS_KERNEL_PARAMS_ALIGN, - sizeof(struct mars_kernel_params) * num_mpu_contexts); - MARS_CHECK_CLEANUP_RET(mars->kernel_params, mars_finalize(mars), - MARS_ERROR_MEMORY); - - /* allocate workload queue */ - mars->workload_queue = (struct mars_workload_queue *) - memalign(MARS_WORKLOAD_QUEUE_ALIGN, - sizeof(struct mars_workload_queue)); - MARS_CHECK_CLEANUP_RET(mars->workload_queue, mars_finalize(mars), - MARS_ERROR_MEMORY); - - /* initialize workload queue */ - ret = workload_queue_initialize(mars->workload_queue); - MARS_CHECK_CLEANUP_RET(ret == MARS_SUCCESS, mars_finalize(mars), ret); - - /* allocate mpu context thread array */ - mars->mpu_context_threads = (pthread_t *) - malloc(sizeof(pthread_t) * num_mpu_contexts); - MARS_CHECK_CLEANUP_RET(mars->mpu_context_threads, mars_finalize(mars), - MARS_ERROR_MEMORY); - - /* create contexts */ - ret = create_mpu_contexts(mars, num_mpu_contexts); - MARS_CHECK_CLEANUP_RET(ret == MARS_SUCCESS, mars_finalize(mars), ret); - - return MARS_SUCCESS; -} - -int mars_finalize(struct mars_context *mars) -{ - MARS_CHECK_RET(mars, MARS_ERROR_NULL); - - int ret; - - /* finalize workload queue */ - if (mars->workload_queue) { - ret = workload_queue_finalize(mars->workload_queue); - MARS_CHECK_RET(ret == MARS_SUCCESS, ret); - } - - /* destroy contexts */ - if (mars->mpu_context_count) { - ret = destroy_mpu_contexts(mars); - MARS_CHECK_RET(ret == MARS_SUCCESS, ret); - } - - /* free allocated memory */ - free(mars->params); - free(mars->kernel_params); - free(mars->workload_queue); - free(mars->mpu_context_threads); - - /* zero context */ - memset(mars, 0, sizeof(struct mars_context)); - - MARS_PRINT("Finalize MARS Context (%p)\n", mars); - - return MARS_SUCCESS; -} --- /dev/null +++ b/src/host/lib/mars_context.c @@ -0,0 +1,213 @@ +/* + * Copyright 2008 Sony Corporation of America + * + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this Library and associated documentation files (the + * "Library"), to deal in the Library without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Library, and to + * permit persons to whom the Library is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Library. + * + * If you modify the Library, you may copy and distribute your modified + * version of the Library in object code or as an executable provided + * that you also do one of the following: + * + * Accompany the modified version of the Library with the complete + * corresponding machine-readable source code for the modified version + * of the Library; or, + * + * Accompany the modified version of the Library with a written offer + * for a complete machine-readable copy of the corresponding source + * code of the modified version of the Library. + * + * + * THE LIBRARY IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * LIBRARY OR THE USE OR OTHER DEALINGS IN THE LIBRARY. + */ + +#include +#include +#include +#include + +#include "mars/mars_context.h" +#include "mars/mars_workload_queue.h" +#include "mars/mars_kernel_types.h" +#include "mars/mars_error.h" +#include "mars/mars_debug.h" + +extern struct spe_program_handle mars_kernel_entry; + +static int num_mpus_max(void) +{ + return spe_cpu_info_get(SPE_COUNT_PHYSICAL_SPES, -1); +} + +static void *mpu_context_thread(void *arg) +{ + MARS_ASSERT(arg); + + int ret; + unsigned int entry = SPE_DEFAULT_ENTRY; + struct mars_kernel_params *params = (struct mars_kernel_params *)arg; + struct spe_context *spe; + + spe = spe_context_create(0, NULL); + MARS_CHECK_RET(spe, (void *)MARS_ERROR_INTERNAL); + + ret = spe_program_load(spe, &mars_kernel_entry); + MARS_CHECK_RET(!ret, (void *)MARS_ERROR_INTERNAL); + + ret = spe_context_run(spe, &entry, 0, params, NULL, NULL); + MARS_CHECK_RET(!ret, (void *)MARS_ERROR_INTERNAL); + + ret = spe_context_destroy(spe); + MARS_CHECK_RET(!ret, (void *)MARS_ERROR_INTERNAL); + + return (void *)MARS_SUCCESS; +} + +static int create_mpu_contexts(struct mars_context *mars, int num_mpu_contexts) +{ + MARS_ASSERT(mars); + + int ret; + unsigned int i; + + /* create threads for each mpu context */ + for (i = 0; i < num_mpu_contexts; i++) { + struct mars_kernel_params *params = &mars->kernel_params[i]; + params->id = i; + params->mars_context_ea = + (uint64_t)(uintptr_t)mars; + params->workload_queue_ea = + (uint64_t)(uintptr_t)mars->workload_queue; + + ret = pthread_create(&mars->mpu_context_threads[i], NULL, + mpu_context_thread, params); + MARS_CHECK_RET(!ret, MARS_ERROR_INTERNAL); + + mars->mpu_context_count++; + } + + return MARS_SUCCESS; +} + +static int destroy_mpu_contexts(struct mars_context *mars) +{ + MARS_ASSERT(mars); + + int ret; + void *p_ret; + unsigned int i; + + /* join all mpu context threads */ + for (i = 0; i < mars->mpu_context_count; i++) { + ret = pthread_join(mars->mpu_context_threads[i], &p_ret); + MARS_CHECK_RET(!ret && !p_ret, MARS_ERROR_INTERNAL); + } + + return MARS_SUCCESS; +} + +int mars_initialize(struct mars_context *mars, struct mars_params *params) +{ + MARS_CHECK_RET(mars, MARS_ERROR_NULL); + + MARS_PRINT("Initialize MARS Context (%p)\n", mars); + + int ret; + unsigned int num_mpu_contexts; + + /* zero context */ + memset(mars, 0, sizeof(struct mars_context)); + + /* allocate and initialize params */ + if (params) { + MARS_CHECK_RET(params->num_mpus <= num_mpus_max(), + MARS_ERROR_PARAMS); + + mars->params = (struct mars_params *) + malloc(sizeof(struct mars_params)); + MARS_CHECK_RET(mars->params, MARS_ERROR_MEMORY); + memcpy(mars->params, params, sizeof(struct mars_params)); + + num_mpu_contexts = mars->params->num_mpus; + } else { + mars->params = NULL; + + num_mpu_contexts = num_mpus_max(); + } + + /* allocate kernel params */ + mars->kernel_params = (struct mars_kernel_params *) + memalign(MARS_KERNEL_PARAMS_ALIGN, + sizeof(struct mars_kernel_params) * num_mpu_contexts); + MARS_CHECK_CLEANUP_RET(mars->kernel_params, mars_finalize(mars), + MARS_ERROR_MEMORY); + + /* allocate workload queue */ + mars->workload_queue = (struct mars_workload_queue *) + memalign(MARS_WORKLOAD_QUEUE_ALIGN, + sizeof(struct mars_workload_queue)); + MARS_CHECK_CLEANUP_RET(mars->workload_queue, mars_finalize(mars), + MARS_ERROR_MEMORY); + + /* initialize workload queue */ + ret = workload_queue_initialize(mars->workload_queue); + MARS_CHECK_CLEANUP_RET(ret == MARS_SUCCESS, mars_finalize(mars), ret); + + /* allocate mpu context thread array */ + mars->mpu_context_threads = (pthread_t *) + malloc(sizeof(pthread_t) * num_mpu_contexts); + MARS_CHECK_CLEANUP_RET(mars->mpu_context_threads, mars_finalize(mars), + MARS_ERROR_MEMORY); + + /* create contexts */ + ret = create_mpu_contexts(mars, num_mpu_contexts); + MARS_CHECK_CLEANUP_RET(ret == MARS_SUCCESS, mars_finalize(mars), ret); + + return MARS_SUCCESS; +} + +int mars_finalize(struct mars_context *mars) +{ + MARS_CHECK_RET(mars, MARS_ERROR_NULL); + + int ret; + + /* finalize workload queue */ + if (mars->workload_queue) { + ret = workload_queue_finalize(mars->workload_queue); + MARS_CHECK_RET(ret == MARS_SUCCESS, ret); + } + + /* destroy contexts */ + if (mars->mpu_context_count) { + ret = destroy_mpu_contexts(mars); + MARS_CHECK_RET(ret == MARS_SUCCESS, ret); + } + + /* free allocated memory */ + free(mars->params); + free(mars->kernel_params); + free(mars->workload_queue); + free(mars->mpu_context_threads); + + /* zero context */ + memset(mars, 0, sizeof(struct mars_context)); + + MARS_PRINT("Finalize MARS Context (%p)\n", mars); + + return MARS_SUCCESS; +} --- a/src/host/lib/mars_task.c +++ b/src/host/lib/mars_task.c @@ -41,7 +41,7 @@ #include "mars/mars_task.h" #include "mars/mars_workload_queue.h" -#include "mars/mars.h" +#include "mars/mars_context.h" #include "mars/mars_error.h" #include "mars/mars_debug.h" --- a/src/host/lib/mars_task_event_flag.c +++ b/src/host/lib/mars_task_event_flag.c @@ -40,7 +40,7 @@ #include "mars/mars_task_event_flag.h" #include "mars/mars_workload_queue.h" -#include "mars/mars.h" +#include "mars/mars_context.h" #include "mars/mars_mutex.h" #include "mars/mars_error.h" #include "mars/mars_debug.h" --- a/src/host/lib/mars_task_queue.c +++ b/src/host/lib/mars_task_queue.c @@ -40,7 +40,7 @@ #include "mars/mars_task_queue.h" #include "mars/mars_workload_queue.h" -#include "mars/mars.h" +#include "mars/mars_context.h" #include "mars/mars_mutex.h" #include "mars/mars_error.h" #include "mars/mars_debug.h" --- a/src/host/lib/mars_task_signal.c +++ b/src/host/lib/mars_task_signal.c @@ -36,7 +36,8 @@ */ #include "mars/mars_task_signal.h" -#include "mars/mars.h" +#include "mars/mars_workload_queue.h" +#include "mars/mars_context.h" #include "mars/mars_error.h" #include "mars/mars_debug.h" #include "mars/mars_workload_queue.h"