view src/parallel_execution/main.c @ 236:865179a0a56d

fix taskManager
author ikkun
date Mon, 23 Jan 2017 20:00:46 +0900
parents 123b0d277b84
children 421ea91dd76c
line wrap: on
line source

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "context.h"
#include "origin_cs.h"

extern __code initContext(struct Context* context);
extern union Data* createRedBlackTree(struct Context* context);
extern union Data* createTaskManager(struct Context* context);
extern void allocator(struct Context* context);

int cpu_num = 1;
int length = 102400;
int split = 8;
int* array_ptr;

void print_queue(struct Element* element) {
    while (element) {
        printf("%p\n", ((struct Task *)(element->data)));
        element = element->next;
    }
}

void print_tree(struct Node* node) {
    if (node != 0) {
        printf("%d\n", node->value->Array.index);
        print_tree(node->left);
        print_tree(node->right);
    }
}

__code initDataGears(struct Context* context, struct LoopCounter* loopCounter, struct TaskManager* taskManager) {
    loopCounter->tree = &createRedBlackTree(context)->Tree;
    loopCounter->i = 0;
    taskManager->taskManager = (union Data*)&createTaskManager(context)->TaskManager;
    goto meta(context, C_createData1);
}

__code initDataGears_stub(struct Context* context) {
    goto initDataGears(context, Gearef(context, LoopCounter), Gearef(context, TaskManager));
}

__code code1(struct Context* context, struct Time* time) {
    printf("cpus:\t\t%d\n", cpu_num);
    printf("length:\t\t%d\n", length);
    printf("length/task:\t%d\n", length/split);
    /* puts("queue"); */
    /* print_queue(context->data[ActiveQueue]->queue.first); */
    /* puts("tree"); */
    /* print_tree(context->data[Tree]->tree.root); */
    /* puts("result"); */

    time->next = C_code2;
    goto meta(context, C_start_time);
}

__code code1_stub(struct Context* context) {
    goto code1(context, Gearef(context, Time));
}

__code code2(struct Context* context, struct Array* array, struct LoopCounter* loopCounter) {
    int i = loopCounter->i;

    if (i < length) {
           //printf("%d\n", array->array[i]);
        if (array->array[i] == (i*2)) {
            loopCounter->i++;
            goto meta(context, C_code2);
        } else
            puts("wrong result");

    }

    goto meta(context, C_code2);
}

__code code2_stub(struct Context* context) {
    goto code2(context, &context->data[D_Node]->Node.value->Array, &context->data[D_LoopCounter]->LoopCounter);
}

__code createData1(struct Context* context, struct Allocate* allocate, struct LoopCounter* loopCounter) {
    int i = loopCounter->i;

    if ((length/split*i) < length) {
        goto meta(context, C_createData2);
    }

    loopCounter->i = 0;
    goto meta(context, C_code1);
}

__code createData1_stub(struct Context* context) {
    goto createData1(context, Gearef(context, Allocate), Gearef(context, LoopCounter));
}

__code createData2(struct Context* context, struct LoopCounter* loopCounter, struct Array* array, struct Node* node, Tree* tree) {
    int i = loopCounter->i;

    array->index = i;
    array->prefix = length/split;
    array->array = array_ptr;

    node->key = i;
    node->value = (union Data*)array;
    
    tree->tree = (union Data*)loopCounter->tree;

    tree->next = C_createTask1;
    tree->node = node;

    goto meta(context, loopCounter->tree->put);
}

__code createData2_stub(struct Context* context) {
    Array* array = &ALLOCATE(context, Array)->Array;
    goto createData2(context,
            Gearef(context, LoopCounter),
            array,
            Gearef(context, Node),
            Gearef(context, Tree));
}

__code createTask1(struct Context* context, struct LoopCounter* loopCounter, struct TaskManager* taskManager, struct Task* task) {
    task->code = C_twice;
    task->idsCount = 0;

//    taskManager->task = task;
    taskManager->next = C_createData1;
    loopCounter->i++;

    goto meta(context, taskManager->taskManager->TaskManager.spawn);
}

__code createTask1_stub(struct Context* context) {
    Task* task = &ALLOCATE(context, Task)->Task;
    goto createTask1(context,
            Gearef(context, LoopCounter),
            Gearef(context, TaskManager),
            task);
}

__code taskManager(struct Context* context, struct LoopCounter* loopCounter, struct Worker* worker) {
    loopCounter->i = 0;

    Time *t = &context->data[D_Time]->Time;
    t->next = C_code2;
    goto meta(context, C_end_time);
}

__code taskManager_stub(struct Context* context) {
    goto taskManager(context, &context->data[D_LoopCounter]->LoopCounter, &context->data[D_Worker]->Worker);
}

void init(int argc, char** argv) {
    for (int i = 1; argv[i]; ++i) {
        if (strcmp(argv[i], "-cpu") == 0)
            cpu_num = (int)atoi(argv[i+1]);
        else if (strcmp(argv[i], "-l") == 0)
            length = (int)atoi(argv[i+1]);
        else if (strcmp(argv[i], "-s") == 0)
            split = (int)atoi(argv[i+1]);
    }
}


int main(int argc, char** argv) {
    init(argc, argv);

    array_ptr = NEWN(length, int);

    for(int i=0; i<length; i++)
        array_ptr[i]=i;

    struct Context* main_context = NEW(struct Context);
    initContext(main_context);
    main_context->next = C_initDataGears;

    struct Context* worker_contexts = NEWN(cpu_num, struct Context);

    for (int i = 0;i<cpu_num;i++)
        initContext(&worker_contexts[i]);

    goto start_code(main_context);
}