view src/parallel_execution/main.c @ 211:962fbfe777bb

temporarily change to createWorker1
author mir3636
date Mon, 02 Jan 2017 01:01:37 +0900
parents 119c035e0e36
children c34e6aa10967
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 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 code1(struct Context* context) {
    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"); */

    context->next = C_createWorker1;

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

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

__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) {
        allocate->size = sizeof(struct Array);
        allocator(context);

        goto meta(context, C_createData2);
    }

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

__code createData1_stub(struct Context* context) {
    goto createData1(context, &context->data[D_Allocate]->allocate, &context->data[D_LoopCounter]->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->next = C_createTask1;
    tree->node = node;

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

__code createData2_stub(struct Context* context) {
    goto createData2(context,
            &context->data[D_LoopCounter]->loopCounter,
            &context->data[context->dataNum]->Array,
            &context->data[D_Node]->node,
            Gearef(context, Tree));
}

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

    element->data = (union Data *)task;

    context->next = C_createData1;
    loopCounter->i++;

    goto meta(context, C_createTask1);
}

__code createTask1_stub(struct Context* context) {
    Task* task = &ALLOCATE(context, Task)->Task;
    goto createTask1(context,
            &context->data[D_LoopCounter]->loopCounter,
            task,
            &context->data[D_Element]->Element);
}

//__code createTask4(struct Context* context, struct LoopCounter* loopCounter, struct Task* task, struct Queue* waitMe, struct OdsQueue* waitI, struct Element* element, struct Queue* activeQueue) {
//    int i = loopCounter->i;
//
//    task->code = TaskC;
//    task->key = i;
//    task->waitMe = waitMe;
//    task->waitI = waitI;
//    task->idsCount = 1;
//
//    element->data = (union Data *)task;
//
//    context->next = CreateData1;
//    loopCounter->i++;
//
//    goto meta_createTask2(context, activeQueue, PutQueue1);
//}
//
//__code createTask4_stub(struct Context* context) {
//    goto createTask2(context,
//            &context->data[LoopCounter]->loopCounter,
//            &context->data[context->dataNum]->task,
//            &context->data[context->dataNum-1]->queue,
//            &context->data[context->dataNum-2]->odsQueue,
//            &context->data[Element]->element,
//            &context->data[ActiveQueue]->queue);
//}
//
//__code createTask5(struct Context* context, struct LoopCounter* loopCounter, struct Task* task, struct Task* slave, struct Element* element) {
//    int i = loopCounter->i;
//
//    task->code = TaskB;
//    task->key = i;
//    element->data = (union Data *)task;
//
//    context->next = CreateData1;
//    loopCounter->i++;
//
//    goto meta(context, WaitFor1);
//}
//
//__code createTask5_stub(struct Context* context) {
//    goto createTask2(context,
//            &context->data[context->dataNum]->task,
//            &context->data[Element]->element);
//}

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

    if (i < worker->id) {
        struct Context* worker_context = &worker->contexts[i];
        worker_context->next = C_getTask1;
        worker_context->data[D_Tree] = context->data[D_Tree];
        // worker_context->data[D_ActiveQueue] = context->data[D_ActiveQueue];
        pthread_create(&worker_context->thread, NULL, (void*)&start_code, worker_context);
        worker_context->thread_num = i;
        loopCounter->i++;

        goto meta(context, C_createWorker1);
    }

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

__code createWorker1_stub(struct Context* context) {
    goto createWorker1(context, &context->data[D_LoopCounter]->loopCounter, &context->data[D_Worker]->Worker);
}

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

    if (i < worker->id) {
        pthread_join(worker->contexts[i].thread, NULL);
        loopCounter->i++;

        goto meta(context, C_taskManager);
    }

    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_createData1;

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

    struct Worker* worker = &main_context->data[D_Worker]->Worker;
    worker->id = cpu_num;
    worker->contexts = worker_contexts;

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

    goto start_code(main_context);
}