view src/parallel_execution/dependency.c @ 123:4ff6f093b695

Fix segmentation fault
author Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
date Tue, 13 Sep 2016 11:54:25 +0900
parents a086857e1812
children acf0453b1c7a
line wrap: on
line source

#include <stdio.h>
#include "context.h"
#include "origin_cs.h"

__code meta_waitFor(struct Context* context, struct Queue* queue, enum Code next) {
    context->data[Queue] = (union Data*)queue;
    goto (context->code[next])(context);
}

__code waitFor(struct Context* context, struct Task* slave, struct Element* element, union Data* data) {
    struct Integer* integer   = (struct Integer *)data;
    struct Queue* waitMeTasks = (struct Queue *)(integer + integer-> mDataOffset);
    element->data = (union Data *)slave;
    goto meta_waitFor(context, waitMeTasks, PutQueue1);
}

__code waitFor_stub(struct Context* context) {
    goto waitFor(context,
            &context->data[context->dataNum-1]->task,
            &context->data[Element]->element,
            &context->data[Node]->node.value);
}

__code meta_spawnTask(struct Context* context, struct Queue* queue, enum Code next) {
    context->data[Queue] = (union Data *)queue;
    goto (context->code[next])(context);
}

__code spawnTask(struct Context* context, struct Task* task, struct Element* element, struct Queue* activeQueue, struct Queue* waitQueue) {
    if (task->idsCount == 0) {
        //printf("put ActiveQueue\n");
        // enqueue activeQueue
        goto meta_spawnTask(context, activeQueue, PutQueue1);
    } else {
        //printf("put WaitQueue\n");
        // enqueue waitQueue
        goto meta_spawnTask(context, waitQueue, PutQueue1);
    }
}

__code spawnTask_stub(struct Context* context) {
    goto spawnTask(context,
            (struct Task *)(context->data[Element]->element.data),
            &context->data[Element]->element,
            &context->data[ActiveQueue]->queue,
            &context->data[WaitQueue]->queue);
}

__code meta_checkTaskFinish1(struct Context* context, struct Queue* queue, enum Code next) {
    context->data[Queue] = (union Data *)queue;
    goto (context->code[next])(context);
}

__code checkTaskFinish1(struct Context* context, union Data* data) {
    struct Integer* integer = (struct Integer *)data;
    struct Queue* waitMeTasks = (struct Queue *)(integer + integer->mDataOffset);
    context->next = CheckTaskFinish2;
    goto meta_checkTaskFinish1(context, waitMeTasks, GetQueue1);
}

__code checkTaskFinish1_stub(struct Context* context) {
    goto checkTaskFinish1(context,
                          ((struct Task*)(context->data[Element]->element.data))->oargs[0]);
}

/*
 * ready input data Gear for waitme task
 */
__code checkTaskFinish2(struct Context* context, struct Task* task) {
    // using cas?
    //
    if (__sync_fetch_and_sub(&task->idsCount, 1)) {
        goto meta(context, GetQueue1);
    } else {
        goto meta(context, CheckTaskFinish2);
    }
}

__code checkTaskFinish2_stub(struct Context* context) {
    goto checkTaskFinish1(context,
                          (struct Task*)(context->data[Element]->element.data));
}

__code add(struct Context* context, struct Integer* input1, struct Integer* input2, struct Integer* output) {
    output->value = input1->value + input2->value;
    printf("%d + %d = %d\n", input1->value, input2->value, output->value);
    goto meta(context, context->next);
}

__code add_stub(struct Context* context) {
    goto add(context,
            (struct Integer*)((struct Task*)(context->data[Element]->element.data))->iargs[0],
            (struct Integer*)((struct Task*)(context->data[Element]->element.data))->iargs[1],
            (struct Integer*)((struct Task*)(context->data[Element]->element.data))->oargs[0]);
}

__code mult(struct Context* context, struct Integer* input1, struct Integer* input2, struct Integer* output) {
    output->value = input1->value * input2->value;
    printf("%d * %d = %d\n", input1->value, input2->value, output->value);
    goto meta(context, context->next);
}

__code mult_stub(struct Context* context) {
    goto mult(context,
             (struct Integer*)((struct Task*)(context->data[Element]->element.data))->iargs[0],
             (struct Integer*)((struct Task*)(context->data[Element]->element.data))->iargs[1],
             (struct Integer*)((struct Task*)(context->data[Element]->element.data))->oargs[0]);
}