node.h 13.8 KB
Newer Older
1
/*
Marcus Winter committed
2
 * Copyright (c) 2014-2016, Siemens AG. All rights reserved.
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
 *
 * 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 <embb/base/memory_allocation.h>
31
#include <embb/base/function.h>
32
#include <embb/mtapi/c/mtapi.h>
Marcus Winter committed
33
#include <embb/mtapi/execution_policy.h>
34 35
#include <embb/mtapi/status_exception.h>
#include <embb/mtapi/node_attributes.h>
36 37
#include <embb/mtapi/group.h>
#include <embb/mtapi/queue.h>
38 39 40
#include <embb/mtapi/task.h>
#include <embb/mtapi/task_attributes.h>
#include <embb/mtapi/job.h>
41
#include <embb/mtapi/action.h>
42 43 44 45 46 47
#include <embb/mtapi/task_context.h>

#ifdef GetJob
#undef GetJob
#endif

Marcus Winter committed
48 49 50 51
#ifdef MTAPI_CPP_AUTOMATIC_INITIALIZE
#define MTAPI_CPP_AUTOMATIC_DOMAIN_ID 1
#define MTAPI_CPP_AUTOMATIC_NODE_ID 1
#endif
52
#define EMBB_MTAPI_FUNCTION_JOB_ID 2
53 54 55 56 57 58 59 60 61 62 63 64 65 66

namespace embb {

namespace base {

class Allocation;

} // namespace base

namespace mtapi {

/**
 * A singleton representing the MTAPI runtime.
 *
67
 * \ingroup CPP_MTAPI
68 69 70
 */
class Node {
 public:
71 72
  typedef embb::base::Function<void, TaskContext &> SMPFunction;

73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
  /**
   * Initializes the runtime singleton using default values:
   *   - all available cores will be used
   *   - maximum number of tasks is 1024
   *   - maximum number of groups is 128
   *   - maximum number of queues is 16
   *   - maximum queue capacity is 1024
   *   - maximum number of priorities is 4.
   *
   * \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 */
90
  );
91 92 93 94 95 96 97 98 99 100 101 102

  /**
   * 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 */
    NodeAttributes const & attributes  /**< [in] Attributes to use */
103
    );
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119

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

  /**
   * Gets the instance of the runtime system.
   * \return Reference to the Node singleton
   * \threadsafe
   */
120
  static Node & GetInstance();
121 122 123 124 125 126

  /**
   * Shuts the runtime system down.
   * \throws ErrorException if the singleton is not initialized.
   * \notthreadsafe
   */
127
  static void Finalize();
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147

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

  /**
   * Returns the number of worker threads.
   * \return The number of worker threads.
   * \waitfree
   */
  mtapi_uint_t GetWorkerThreadCount() const {
    return worker_thread_count_;
  }

  /**
148 149 150 151 152 153 154 155 156
   * Returns the number of available queues.
   * \return The number of available queues
   * \waitfree
   */
  mtapi_uint_t GetQueueCount() const {
    return queue_count_;
  }

  /**
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
   * Returns the number of available groups.
   * \return The number of available groups
   * \waitfree
   */
  mtapi_uint_t GetGroupCount() const {
    return group_count_;
  }

  /**
    * Returns the number of available tasks.
    * \return The number of available tasks
    * \waitfree
    */
  mtapi_uint_t GetTaskLimit() const {
    return task_limit_;
  }

174 175 176 177 178 179
  /**
   * Starts a new Task.
   *
   * \returns The handle to the started Task.
   * \threadsafe
   */
180
  Task Start(
181
    SMPFunction const & func           /**< Function to use for the task. */
182 183 184 185 186 187 188
    ) {
    Job job = GetJob(EMBB_MTAPI_FUNCTION_JOB_ID);
    void * res = NULL;
    return Start(
      job, embb::base::Allocation::New<SMPFunction>(func), res);
  }

189 190 191 192 193 194
  /**
   * Starts a new Task with a given affinity and priority.
   *
   * \returns The handle to the started Task.
   * \threadsafe
   */
195
  Task Start(
196 197 198
    SMPFunction const & func,          /**< Function to use for the task. */
    ExecutionPolicy const & policy     /**< Affinity and priority of the
                                            task. */
199 200 201 202 203 204 205 206 207
  ) {
    Job job = GetJob(EMBB_MTAPI_FUNCTION_JOB_ID);
    void * res = NULL;
    TaskAttributes task_attr;
    task_attr.SetPolicy(policy);
    return Start(
      job, embb::base::Allocation::New<SMPFunction>(func), res, task_attr);
  }

208
  /**
209 210 211
   * Starts a new Task.
   *
   * \returns The handle to the started Task.
212
   * \threadsafe
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
   */
  template <typename ARGS, typename RES>
  Task Start(
    mtapi_task_id_t task_id,           /**< A user defined ID of the Task. */
    Job const & job,                   /**< The Job to execute. */
    const ARGS * arguments,            /**< Pointer to the arguments. */
    RES * results,                     /**< Pointer to the results. */
    TaskAttributes const & attributes  /**< Attributes of the Task */
    ) {
    return Start(task_id,
      job.GetInternal(),
      arguments, internal::SizeOfType<ARGS>(),
      results, internal::SizeOfType<RES>(),
      &attributes.GetInternal());
  }

  /**
   * Starts a new Task.
   *
   * \returns The handle to the started Task.
233
   * \threadsafe
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
   */
  template <typename ARGS, typename RES>
  Task Start(
    mtapi_task_id_t task_id,           /**< A user defined ID of the Task. */
    Job const & job,                   /**< The Job to execute. */
    const ARGS * arguments,            /**< Pointer to the arguments. */
    RES * results                      /**< Pointer to the results. */
    ) {
    return Start(task_id,
      job.GetInternal(),
      arguments, internal::SizeOfType<ARGS>(),
      results, internal::SizeOfType<RES>(),
      MTAPI_DEFAULT_TASK_ATTRIBUTES);
  }

  /**
   * Starts a new Task.
   *
   * \returns The handle to the started Task.
253
   * \threadsafe
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
   */
  template <typename ARGS, typename RES>
  Task Start(
    Job const & job,                   /**< The Job to execute. */
    const ARGS * arguments,            /**< Pointer to the arguments. */
    RES * results,                     /**< Pointer to the results. */
    TaskAttributes const & attributes  /**< Attributes of the Task */
    ) {
    return Start(MTAPI_TASK_ID_NONE,
      job.GetInternal(),
      arguments, internal::SizeOfType<ARGS>(),
      results, internal::SizeOfType<RES>(),
      &attributes.GetInternal());
  }

  /**
   * Starts a new Task.
   *
   * \returns The handle to the started Task.
273
   * \threadsafe
274 275 276 277 278 279 280 281 282 283 284 285 286 287
   */
  template <typename ARGS, typename RES>
  Task Start(
    Job const & job,                   /**< The Job to execute. */
    const ARGS * arguments,            /**< Pointer to the arguments. */
    RES * results                      /**< Pointer to the results. */
    ) {
    return Start(MTAPI_TASK_ID_NONE,
      job.GetInternal(),
      arguments, internal::SizeOfType<ARGS>(),
      results, internal::SizeOfType<RES>(),
      MTAPI_DEFAULT_TASK_ATTRIBUTES);
  }

288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
  Job GetJob(mtapi_job_id_t job_id) {
    return Job(job_id, domain_id_);
  }

  Job GetJob(mtapi_job_id_t job_id, mtapi_domain_t domain_id) {
    return Job(job_id, domain_id);
  }

  /**
   * Constructs an Action.
   */
  Action CreateAction(
    mtapi_job_id_t job_id,             /**< Job ID the Action belongs to */
    mtapi_action_function_t func,      /**< The action function */
    const void * node_local_data,      /**< Node local data available to all
                                       Tasks using this Action */
    mtapi_size_t node_local_data_size, /**< Size of node local data */
    ActionAttributes const & attributes
    /**< Attributes of the Action */
    ) {
    return Action(job_id, func, node_local_data, node_local_data_size,
      &attributes.GetInternal());
  }

  /**
   * Constructs an Action.
   */
  Action CreateAction(
    mtapi_job_id_t job_id,             /**< Job ID the Action belongs to */
    mtapi_action_function_t func,      /**< The action function */
    const void * node_local_data,      /**< Node local data available to all
                                       Tasks using this Action */
    mtapi_size_t node_local_data_size  /**< Size of node local data */
    ) {
    return Action(job_id, func, node_local_data, node_local_data_size,
      MTAPI_DEFAULT_ACTION_ATTRIBUTES);
  }

  /**
   * Constructs an Action.
   */
  Action CreateAction(
    mtapi_job_id_t job_id,             /**< Job ID the Action belongs to */
    mtapi_action_function_t func,      /**< The action function */
    ActionAttributes const & attributes
    /**< Attributes of the Action */
    ) {
    return Action(job_id, func, MTAPI_NULL, 0, &attributes.GetInternal());
  }

  /**
   * Constructs an Action.
   */
  Action CreateAction(
    mtapi_job_id_t job_id,             /**< Job ID the Action belongs to */
    mtapi_action_function_t func       /**< The action function */
    ) {
    return Action(job_id, func, MTAPI_NULL, 0, MTAPI_DEFAULT_ACTION_ATTRIBUTES);
  }

  /**
   * Constructs a Group object with default attributes.
   */
  Group CreateGroup() {
    return Group(MTAPI_GROUP_ID_NONE, MTAPI_DEFAULT_GROUP_ATTRIBUTES);
  }

  /**
   * Constructs a Group object with default attributes and the given ID.
   */
  Group CreateGroup(
    mtapi_group_id_t id                /**< A user defined ID of the Group. */
    ) {
    return Group(id, MTAPI_DEFAULT_GROUP_ATTRIBUTES);
  }

  /**
   * Constructs a Group object using the given Attributes.
   */
  Group CreateGroup(
    GroupAttributes const & group_attr) {
    return Group(MTAPI_GROUP_ID_NONE, &group_attr.GetInternal());
  }

  /**
   * Constructs a Group object with given attributes and ID.
   */
  Group CreateGroup(
    mtapi_group_id_t id,               /**< A user defined ID of the Group. */
    GroupAttributes const & group_attr /**< The GroupAttributes to use. */
    ) {
    return Group(id, &group_attr.GetInternal());
  }

  /**
   * Constructs a Queue with the given Job and default attributes.
   */
  Queue CreateQueue(
    Job & job                          /**< The Job to use for the Queue. */
    ) {
    return Queue(MTAPI_QUEUE_ID_NONE, job, MTAPI_DEFAULT_QUEUE_ATTRIBUTES);
  }

  /**
   * Constructs a Queue with the given Job and QueueAttributes.
   */
  Queue CreateQueue(
    Job const & job,                   /**< The Job to use for the Queue. */
    QueueAttributes const & attr       /**< The attributes to use. */
    ) {
    return Queue(MTAPI_QUEUE_ID_NONE, job, &attr.GetInternal());
  }

401 402 403 404 405 406 407 408 409 410 411 412 413
  friend class embb::base::Allocation;

 private:
  // not copyable
  Node(Node const & node);
  Node const & operator=(Node const & other);

  Node(
    mtapi_domain_t domain_id,
    mtapi_node_t node_id,
    NodeAttributes const & attr) {
    mtapi_status_t status;
    mtapi_info_t info;
414
    queue_count_ = attr.GetInternal().max_queues;
415 416
    group_count_ = attr.GetInternal().max_groups;
    task_limit_ = attr.GetInternal().max_tasks;
417 418 419 420 421 422 423
    mtapi_initialize(domain_id, node_id, &attr.GetInternal(), &info, &status);
    internal::CheckStatus(status);

    core_count_ = info.hardware_concurrency;
    worker_thread_count_ = embb_core_set_count(
      &attr.GetInternal().core_affinity);

424
    domain_id_ = domain_id;
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
  }

  Task Start(
    mtapi_task_id_t task_id,
    mtapi_job_hndl_t job,
    const void * arguments,
    mtapi_size_t arguments_size,
    void * results,
    mtapi_size_t results_size,
    mtapi_task_attributes_t const * attributes
    ) {
    mtapi_status_t status;
    mtapi_task_hndl_t task_hndl =
      mtapi_task_start(task_id, job, arguments, arguments_size,
      results, results_size, attributes, MTAPI_GROUP_NONE,
      &status);
    internal::CheckStatus(status);
    return Task(task_hndl);
  }

445 446 447 448 449 450 451 452 453 454
  static void ActionFunction(
    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) {
    TaskContext task_context(context);
    embb::base::Function<void, TaskContext &> * func =
Marcus Winter committed
455 456
      reinterpret_cast<embb::base::Function<void, TaskContext &>*>(
        const_cast<void*>(args));
457 458 459 460
    (*func)(task_context);
    embb::base::Allocation::Delete(func);
  }

461 462
  static embb::mtapi::Node * node_instance_;

463
  mtapi_domain_t domain_id_;
464 465
  mtapi_uint_t core_count_;
  mtapi_uint_t worker_thread_count_;
466
  mtapi_uint_t queue_count_;
467 468
  mtapi_uint_t group_count_;
  mtapi_uint_t task_limit_;
469
  Action function_action_;
470 471 472 473 474 475
};

} // namespace mtapi
} // namespace embb

#endif // EMBB_MTAPI_NODE_H_