Mercurial > hg > Members > innparusu > Gears
view src/parallel_execution/main.c @ 127:a574ba0da60f
add comment rb_tree
author | ikkun |
---|---|
date | Wed, 14 Sep 2016 20:43:37 +0900 |
parents | f57e9ffa7960 0f0459653d77 |
children | 337fdbffa693 |
line wrap: on
line source
#include <stdio.h> #include <string.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("%d\n", ((struct Task *)(element->data))->key); 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 = CreateWorker; struct Time *t = &context->data[Time]->time; t->next = CreateWorker; goto meta(context, StartTime); } __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, Code2); } else puts("wrong result"); } goto meta(context, Exit); } __code code2_stub(struct Context* context) { goto code2(context, &context->data[Node]->node.value->array, &context->data[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, CreateData2); } loopCounter->i = 0; goto meta(context, Code1); } __code createData1_stub(struct Context* context) { goto createData1(context, &context->data[Allocate]->allocate, &context->data[LoopCounter]->loopCounter); } __code createData2(struct Context* context, struct LoopCounter* loopCounter, struct Array* array, struct Node* node) { int i = loopCounter->i; array->index = i; array->prefix = length/split; array->array = array_ptr; node->key = i; node->value = (union Data*)array; context->next = CreateTask1; goto meta(context, PutTree); } __code createData2_stub(struct Context* context) { goto createData2(context, &context->data[LoopCounter]->loopCounter, &context->data[context->dataNum]->array, &context->data[Node]->node); } __code createTask1(struct Context* context, struct Allocate* allocate) { allocate->size = sizeof(struct Task); allocator(context); goto meta(context, CreateTask2); } __code createTask1_stub(struct Context* context) { goto createTask1(context, &context->data[Allocate]->allocate); } __code createTask2(struct Context* context, struct Allocate* allocate) { allocate->size = sizeof(struct Queue); allocator(context); goto meta(context, CreateTask3); } __code createTask2_stub(struct Context* context) { goto createTask2(context, &context->data[Allocate]->allocate); } __code createTask3(struct Context* context, struct Allocate* allocate) { allocate->size = sizeof(struct Queue); allocator(context); goto meta(context, CreateTask4); } __code createTask3_stub(struct Context* context) { goto createTask3(context, &context->data[Allocate]->allocate); } __code meta_createTask4(struct Context* context, struct Queue* activeQueue, enum Code next) { context->data[Queue] = (union Data *)activeQueue; goto (context->code[next])(context); } __code createTask4(struct Context* context, struct LoopCounter* loopCounter, struct Task* task, struct Queue* waitMe, struct Queue* waitI, struct Element* element, struct Queue* activeQueue) { int i = loopCounter->i; waitMe->first = 0; waitMe->last = 0; waitMe->count = 0; waitI->first = 0; waitI->last = 0; waitI->count = 0; task->code = Twice; task->key = i; task->waitMe = waitMe; task->waitI = waitI; task->idsCount = 0; element->data = (union Data *)task; context->next = CreateData1; loopCounter->i++; goto meta(context, SpawnTask); } __code createTask4_stub(struct Context* context) { goto createTask4(context, &context->data[LoopCounter]->loopCounter, &context->data[context->dataNum-2]->task, &context->data[context->dataNum-1]->queue, &context->data[context->dataNum]->queue, &context->data[Element]->element, &context->data[ActiveQueue]->queue); } //__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 putQueue1(struct Context* context, struct Allocate* allocate) { allocate->size = sizeof(struct Element); allocator(context); goto meta(context, PutQueue2); } __code putQueue1_stub(struct Context* context) { goto putQueue1(context, &context->data[Allocate]->allocate); } __code putQueue2(struct Context* context, struct Element* new_element, struct Element* element, struct Queue* queue) { new_element->data = element->data; if (queue->first) goto meta(context, PutQueue3); else goto meta(context, PutQueue4); } __code putQueue2_stub(struct Context* context) { goto putQueue2(context, &context->data[context->dataNum]->element, &context->data[Element]->element, &context->data[Queue]->queue); } __code putQueue3(struct Context* context, struct Queue* queue, struct Element* new_element) { struct Element* last = queue->last; if (__sync_bool_compare_and_swap(&queue->last, last, new_element)) { last->next = new_element; queue->count++; goto meta(context, context->next); } else { goto meta(context, PutQueue3); } } __code putQueue3_stub(struct Context* context) { goto putQueue3(context, &context->data[Queue]->queue, &context->data[context->dataNum]->element); } __code putQueue4(struct Context* context, struct Queue* queue, struct Element* new_element) { if (__sync_bool_compare_and_swap(&queue->first, 0, new_element)) { queue->last = new_element; queue->count++; goto meta(context, context->next); } else { goto meta(context, PutQueue3); } } __code putQueue4_stub(struct Context* context) { goto putQueue4(context, &context->data[Queue]->queue, &context->data[context->dataNum]->element); } __code createWorker(struct Context* context, struct LoopCounter* loopCounter, struct Worker* worker) { int i = loopCounter->i; if (i < worker->num) { struct Context* worker_context = &worker->contexts[i]; worker_context->next = GetQueue; worker_context->data[Tree] = context->data[Tree]; worker_context->data[ActiveQueue] = context->data[ActiveQueue]; pthread_create(&worker_context->thread, NULL, (void*)&start_code, worker_context); worker_context->thread_num = i; loopCounter->i++; goto meta(context, CreateWorker); } loopCounter->i = 0; goto meta(context, TaskManager); } __code createWorker_stub(struct Context* context) { goto createWorker(context, &context->data[LoopCounter]->loopCounter, &context->data[Worker]->worker); } __code taskManager(struct Context* context, struct LoopCounter* loopCounter, struct Worker* worker) { int i = loopCounter->i; if (i < worker->num) { pthread_join(worker->contexts[i].thread, NULL); loopCounter->i++; goto meta(context, TaskManager); } loopCounter->i = 0; struct Time *t = &context->data[Time]->time; t->next = Code2; goto meta(context, EndTime); } __code taskManager_stub(struct Context* context) { goto taskManager(context, &context->data[LoopCounter]->loopCounter, &context->data[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 = CreateData1; struct Context* worker_contexts = NEWN(cpu_num, struct Context); struct Worker* worker = &main_context->data[Worker]->worker; worker->num = cpu_num; worker->contexts = worker_contexts; for (int i = 0;i<cpu_num;i++) initContext(&worker_contexts[i]); goto start_code(main_context); }