view src/parallel_execution/main.c @ 269:5170539348ec

rename TaskManagerImpl.cbc
author mir3636
date Sun, 29 Jan 2017 22:15:32 +0900
parents cd3486e4ba70
children
line wrap: on
line source

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

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

extern union Data* createRedBlackTree(struct Context* context);
extern union Data* createTaskManager(struct Context* context, int numCPU, int numGPU, int numIO);
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, cpu_num, 0, 0)->TaskManager;
    goto meta(context, C_createTask1);
}

__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_exit_code);
    //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) {
    int i = loopCounter->i;

    if ((length/split*i) < length) {
        taskManager->next = C_createTask2;
        goto meta(context, taskManager->taskManager->TaskManager.createTask);
    }

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

__code createTask1_stub(struct Context* context) {
    goto createTask1(context,
            Gearef(context, LoopCounter),
            Gearef(context, TaskManager));
}

__code createTask2(struct Context* context, LoopCounter* loopCounter, TaskManager* taskManager,struct Context* task, Array* array) {
    int i = loopCounter->i;

    if ((length/split*i) < length) {
        array->index = i;
        array->prefix = length/split;
        array->array = array_ptr;
        task->idgCount = 0;
        task->next = C_twice;
        // task->data[task->idg] = (union Data*)array;
        taskManager->next = C_createTask1;
        loopCounter->i++;

        goto meta(context, taskManager->taskManager->TaskManager.spawn);
    }
    loopCounter->i = 0;
    taskManager->next = C_code1;
    goto meta(context, taskManager->taskManager->TaskManager.shutdown);
}

__code createTask2_stub(struct Context* context) {
    Array* array = &ALLOCATE(context, Array)->Array;
    goto createTask2(context,
            Gearef(context, LoopCounter),
            Gearef(context, TaskManager),
            Gearef(context, TaskManager)->context,
            array);
}

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;

    goto start_code(main_context);
}