node.h 7.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
/*
 * Copyright (c) 2014, Siemens AG. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef EMBB_MTAPI_NODE_H_
#define EMBB_MTAPI_NODE_H_

#include <list>
#include <embb/base/core_set.h>
#include <embb/mtapi/c/mtapi.h>
#include <embb/mtapi/action.h>
#include <embb/mtapi/task.h>
#include <embb/mtapi/continuation.h>
#include <embb/mtapi/group.h>
#include <embb/mtapi/queue.h>

namespace embb {

namespace base {

class Allocation;

} // namespace base

namespace mtapi {

/**
  * A singleton representing the MTAPI runtime.
  *
  * \ingroup CPP_MTAPI
  */
class Node {
 public:
  /**
    * Initializes the runtime singleton using default values.
    * \notthreadsafe
    * \throws ErrorException if the singleton was already initialized or the
    *         Node could not be initialized.
    * \memory Allocates about 200kb of memory.
    */
  static void Initialize(
    mtapi_domain_t domain_id,          /**< [in] The domain id to use */
    mtapi_node_t node_id               /**< [in] The node id to use */
    );

  /**
    * Initializes the runtime singleton.
    * \notthreadsafe
    * \throws ErrorException if the singleton was already initialized or the
    *         Node could not be initialized.
    * \memory Allocates some memory depending on the values given.
    */
  static void Initialize(
    mtapi_domain_t domain_id,          /**< [in] The domain id to use */
    mtapi_node_t node_id,              /**< [in] The node id to use */
    embb::base::CoreSet const & core_set,
                                       /**< [in] A set of cores MTAPI should
                                            use for its worker threads */
    mtapi_uint_t max_tasks,            /**< [in] Maximum number of concurrent
                                            \link Task Tasks \endlink */
    mtapi_uint_t max_groups,           /**< [in] Maximum number of concurrent
                                            \link Group Groups \endlink */
    mtapi_uint_t max_queues,           /**< [in] Maximum number of concurrent
                                            \link Queue Queues \endlink */
    mtapi_uint_t queue_limit,          /**< [in] Maximum Queue capacity */
    mtapi_uint_t max_priorities        /**< [in] Maximum number of priorities,
                                            priorities will be between 0 and
                                            max_priorities-1 */
    );

  /**
    * Checks if runtime is initialized.
    * \return \c true if the Node singleton is already initialized, false
    *         otherwise
    * \waitfree
    */
  static bool IsInitialized();

  /**
    * Gets the instance of the runtime system.
    * \return Reference to the Node singleton
    * \threadsafe
    */
  static Node & GetInstance();

  /**
    * Shuts the runtime system down.
    * \throws ErrorException if the singleton is not initialized.
    * \notthreadsafe
    */
  static void Finalize();

  /**
    * Returns the number of available cores.
    * \return The number of available cores
    * \waitfree
    */
  mtapi_uint_t GetCoreCount() const {
    return core_count_;
  }

  /**
    * Creates a Group to launch \link Task Tasks \endlink in.
    * \return A reference to the created Group
    * \throws ErrorException if the Group object could not be constructed.
    * \threadsafe
    * \memory Allocates some memory depending on the configuration of the
    *         runtime.
    */
  Group & CreateGroup();

  /**
    * Destroys a Group. \link Task Tasks \endlink running in the Group will
    * finish execution.
    * \threadsafe
    */
  void DestroyGroup(
    Group & group                      /**< [in,out] The Group to destroy */
    );

  /**
    * Creates a Queue for stream processing. The queue might execute its 
    * \link Task Tasks \endlink either in order or unordered.
    * \return A reference to the new Queue
    * \throws ErrorException if the Queue object could not be constructed.
    * \threadsafe
    * \memory Allocates some memory depending on the configuration of the
    *         runtime.
    */
  Queue & CreateQueue(
    mtapi_uint_t priority,             /**< [in] Priority of the Queue */
    bool ordered                       /**< [in] \c true if the Queue should be
                                            ordered, otherwise \c false */
    );

  /**
    * Destroys a Queue. Running \link Task Tasks \endlink will be canceled.
    * \threadsafe
    */
  void DestroyQueue(
    Queue & queue                      /**< [in,out] The Queue to destroy */
    );

  /**
    * Runs an Action.
    * \return A Task identifying the Action to run
    * \throws ErrorException if the Task object could not be constructed.
    * \threadsafe
    */
  Task Spawn(
    Action action                      /**< [in] The Action to execute */
    );

  /**
    * Runs an Action with the specified priority.
    * \return A Task identifying the Action to run
    * \throws ErrorException if the Task object could not be constructed.
    * \threadsafe
    */
  Task Spawn(
    Action action,                     /**< [in] The Action to execute */
    mtapi_uint_t priority              /**< [in] The priority to use */
    );

  /**
    * Creates a Continuation.
    * \return A Continuation chain
    * \threadsafe
    */
  Continuation First(
    Action action                      /**< [in] The first Action of the
                                            Continuation chain */
    );

  friend class embb::base::Allocation;

 private:
  Node(Node const & node);
  Node(
    mtapi_domain_t domain_id,
    mtapi_node_t node_id,
    mtapi_node_attributes_t * attr);
  ~Node();

  static void action_func(
    const void* args,
    mtapi_size_t args_size,
    void* result_buffer,
    mtapi_size_t result_buffer_size,
    const void* node_local_data,
    mtapi_size_t node_local_data_size,
    mtapi_task_context_t * context);

  mtapi_uint_t core_count_;
  mtapi_action_hndl_t action_handle_;
  std::list<Queue*> queues_;
  std::list<Group*> groups_;
};

} // namespace mtapi
} // namespace embb

#endif // EMBB_MTAPI_NODE_H_