embb_mtapi_task_context_t.c 9.04 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 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
/*
 * 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.
 */

#include <assert.h>

#include <embb/mtapi/c/mtapi.h>

#include <embb/base/c/internal/unused.h>

#include <embb_mtapi_log.h>
#include <embb_mtapi_alloc.h>
#include <mtapi_status_t.h>
#include <embb_mtapi_task_context_t.h>
#include <embb_mtapi_thread_context_t.h>
#include <embb_mtapi_task_t.h>


/* ---- CLASS MEMBERS ------------------------------------------------------ */

void embb_mtapi_task_context_initialize_with_thread_context_and_task(
  embb_mtapi_task_context_t* that,
  embb_mtapi_thread_context_t* thread_context,
  embb_mtapi_task_t* task) {
  assert(MTAPI_NULL != that);
  assert(MTAPI_NULL != thread_context);
  assert(MTAPI_NULL != task);

  that->task = task;
  that->thread_context = thread_context;
  that->num_instances = task->attributes.num_instances;
  that->instance_num = embb_atomic_fetch_and_add_unsigned_int(
    &task->current_instance, 1);
}

void embb_mtapi_task_context_finalize(embb_mtapi_task_context_t* that) {
  assert(MTAPI_NULL != that);

  that->instance_num = 0;
  that->num_instances = 0;
  that->task = MTAPI_NULL;
  that->thread_context = MTAPI_NULL;
}


/* ---- INTERFACE FUNCTIONS ------------------------------------------------ */

void mtapi_context_status_set(
  MTAPI_INOUT mtapi_task_context_t* task_context,
  MTAPI_IN mtapi_status_t error_code,
  MTAPI_OUT mtapi_status_t* status) {
  mtapi_status_t local_status = MTAPI_ERR_UNKNOWN;

  embb_mtapi_log_trace("mtapi_action_status_set() called\n");

  if (MTAPI_NULL != task_context) {
    embb_mtapi_thread_context_t* local_context =
      (embb_mtapi_thread_context_t*)embb_tss_get(
        &(task_context->thread_context->tss_id));

    if (local_context == task_context->thread_context) {
      /* for remote actions the result shall be transferred to the
         waiting node at the end of the task */
      switch (error_code) {
      case MTAPI_SUCCESS:
      case MTAPI_ERR_ARG_SIZE:
      case MTAPI_ERR_RESULT_SIZE:
      case MTAPI_ERR_ACTION_CANCELLED:
      case MTAPI_ERR_ACTION_FAILED:
      case MTAPI_ERR_ACTION_DELETED:
        task_context->task->error_code = error_code;
        local_status = MTAPI_SUCCESS;
        break;
      case MTAPI_ERR_CORE_NUM:
      case MTAPI_ERR_RUNTIME_LOADBALANCING_NOTSUPPORTED:
      case MTAPI_ERR_RUNTIME_REMOTETASKS_NOTSUPPORTED:
      case MTAPI_ERR_ARG_NOT_IMPLEMENTED:
      case MTAPI_ERR_FUNC_NOT_IMPLEMENTED:
      case MTAPI_ERR_WAIT_PENDING:
      case MTAPI_ERR_BUFFER_SIZE:
      case MTAPI_ERR_UNKNOWN:
      case MTAPI_GROUP_COMPLETED:
      case MTAPI_ERR_GROUP_LIMIT:
      case MTAPI_ERR_GROUP_INVALID:
      case MTAPI_ERR_QUEUE_LIMIT:
      case MTAPI_ERR_QUEUE_DISABLED:
      case MTAPI_ERR_QUEUE_DELETED:
      case MTAPI_ERR_QUEUE_INVALID:
      case MTAPI_ERR_JOB_INVALID:
      case MTAPI_ERR_TASK_LIMIT:
      case MTAPI_ERR_TASK_INVALID:
      case MTAPI_ERR_CONTEXT_OUTOFCONTEXT:
      case MTAPI_ERR_CONTEXT_INVALID:
      case MTAPI_ERR_ACTION_DISABLED:
      case MTAPI_ERR_ACTION_NUM_INVALID:
      case MTAPI_ERR_ACTION_LIMIT:
      case MTAPI_ERR_ACTION_EXISTS:
      case MTAPI_ERR_ACTION_INVALID:
      case MTAPI_ERR_NODE_NOTINIT:
      case MTAPI_ERR_DOMAIN_INVALID:
      case MTAPI_ERR_NODE_INVALID:
      case MTAPI_ERR_NODE_INITIALIZED:
      case MTAPI_ERR_NODE_INITFAILED:
      case MTAPI_ERR_ATTR_SIZE:
      case MTAPI_ERR_ATTR_READONLY:
      case MTAPI_ERR_PARAMETER:
      case MTAPI_TIMEOUT:
      case MTAPI_ERR_ATTR_NUM:
      default:
        /* trying to set invalid error code */
        local_status = MTAPI_ERR_PARAMETER;
        break;
      }
    } else {
      local_status = MTAPI_ERR_CONTEXT_OUTOFCONTEXT;
    }
  } else {
    local_status = MTAPI_ERR_CONTEXT_INVALID;
  }

  mtapi_status_set(status, local_status);
}

void mtapi_context_runtime_notify(
  MTAPI_IN mtapi_task_context_t* task_context,
  MTAPI_IN mtapi_notification_t notification,
  MTAPI_IN void* data,
  MTAPI_IN mtapi_size_t data_size,
  MTAPI_OUT mtapi_status_t* status) {
  mtapi_status_t local_status = MTAPI_ERR_UNKNOWN;

  EMBB_UNUSED(notification);
  EMBB_UNUSED(data);
  EMBB_UNUSED(data_size);

  embb_mtapi_log_trace("mtapi_context_runtime_notify() called\n");

  if (MTAPI_NULL != task_context) {
    mtapi_task_context_t* local_context = (mtapi_task_context_t*) embb_tss_get(
      &(task_context->thread_context->tss_id));

    if (local_context == task_context) {
      local_status = MTAPI_SUCCESS;
    } else {
      local_status = MTAPI_ERR_CONTEXT_OUTOFCONTEXT;
    }
  } else {
    local_status = MTAPI_ERR_CONTEXT_INVALID;
  }

  mtapi_status_set(status, local_status);
}

mtapi_task_state_t mtapi_context_taskstate_get(
  MTAPI_IN mtapi_task_context_t* task_context,
  MTAPI_OUT mtapi_status_t* status) {
  mtapi_status_t local_status = MTAPI_ERR_UNKNOWN;
  mtapi_task_state_t task_state = MTAPI_TASK_ERROR;

  embb_mtapi_log_trace("mtapi_context_taskstate_get() called\n");

  if (MTAPI_NULL != task_context) {
    embb_mtapi_thread_context_t* local_context =
      (embb_mtapi_thread_context_t*)embb_tss_get(
        &(task_context->thread_context->tss_id));

    if (local_context == task_context->thread_context) {
      task_state = task_context->task->state;
      local_status = MTAPI_SUCCESS;
    } else {
      local_status = MTAPI_ERR_CONTEXT_OUTOFCONTEXT;
    }
  } else {
    local_status = MTAPI_ERR_CONTEXT_INVALID;
  }

  mtapi_status_set(status, local_status);
  return task_state;
}

/* derived from OpenMP's omp_get_thread_num(); */
mtapi_uint_t mtapi_context_instnum_get(
  MTAPI_IN mtapi_task_context_t* task_context,
  MTAPI_OUT mtapi_status_t* status) {
  mtapi_status_t local_status = MTAPI_ERR_UNKNOWN;
  mtapi_uint_t instnum = 0;

  embb_mtapi_log_trace("mtapi_context_instnum_get() called\n");

  if (MTAPI_NULL != task_context) {
    embb_mtapi_thread_context_t* local_context =
      (embb_mtapi_thread_context_t*)embb_tss_get(
        &(task_context->thread_context->tss_id));

    if (local_context == task_context->thread_context) {
      instnum = task_context->instance_num;
      local_status = MTAPI_SUCCESS;
    } else {
      local_status = MTAPI_ERR_CONTEXT_OUTOFCONTEXT;
    }
  } else {
    local_status = MTAPI_ERR_CONTEXT_INVALID;
  }

  mtapi_status_set(status, local_status);
  return instnum;
}

/* derived from OpenMP's omp_get_num_threads(); */
mtapi_uint_t mtapi_context_numinst_get(
  MTAPI_IN mtapi_task_context_t* task_context,
  MTAPI_OUT mtapi_status_t* status) {
  mtapi_status_t local_status = MTAPI_ERR_UNKNOWN;
  mtapi_uint_t numinst = 0;

  embb_mtapi_log_trace("mtapi_context_numinst_get() called\n");

  if (MTAPI_NULL != task_context) {
    embb_mtapi_thread_context_t* local_context =
      (embb_mtapi_thread_context_t*)embb_tss_get(
        &(task_context->thread_context->tss_id));

    if (local_context == task_context->thread_context) {
      numinst = task_context->num_instances;
      local_status = MTAPI_SUCCESS;
    } else {
      local_status = MTAPI_ERR_CONTEXT_OUTOFCONTEXT;
    }
  } else {
    local_status = MTAPI_ERR_CONTEXT_INVALID;
  }

  mtapi_status_set(status, local_status);
  return numinst;
}

mtapi_uint_t mtapi_context_corenum_get(
  MTAPI_IN mtapi_task_context_t* task_context,
  MTAPI_OUT mtapi_status_t* status) {
  mtapi_status_t local_status = MTAPI_ERR_UNKNOWN;
  mtapi_uint_t corenum = 0;

  embb_mtapi_log_trace("mtapi_context_corenum_get() called\n");

  if (MTAPI_NULL != task_context) {
    embb_mtapi_thread_context_t* local_context =
      (embb_mtapi_thread_context_t*)embb_tss_get(
        &(task_context->thread_context->tss_id));

    if (local_context == task_context->thread_context) {
      corenum = task_context->thread_context->core_num;
      local_status = MTAPI_SUCCESS;
    } else {
      local_status = MTAPI_ERR_CONTEXT_OUTOFCONTEXT;
    }
  } else {
    local_status = MTAPI_ERR_CONTEXT_INVALID;
  }

  mtapi_status_set(status, local_status);
  return corenum;
}