Mercurial > hg > Members > innparusu > Gears
view src/parallel_execution/main.c @ 91:1e074c3878c7
modify tree
author | Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp> |
---|---|
date | Tue, 26 Jan 2016 07:46:26 +0900 |
parents | 4b5bf5b40970 |
children | 851da1107223 |
line wrap: on
line source
#include <stdio.h> #include <unistd.h> #include "context.h" #include "origin_cs.h" extern __code initContext(struct Context* context); extern void allocator(struct Context* context); int length; int* array_ptr; void print_queue(struct Element* element) { while (element) { printf("%d\n", element->task->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) { puts("queue"); print_queue(context->data[ActiveQueue]->queue.first); puts("tree"); print_tree(context->data[Tree]->tree.root); goto meta(context, Exit); } __code code1_stub(struct Context* context) { goto code1(context); } __code createData1(struct Context* context, struct Allocate* allocate, struct LoopCounter* loopCounter) { int i = loopCounter->i; if (i < length) { allocate->size = sizeof(struct Array); allocator(context); goto meta(context, CreateData2); } 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->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 LoopCounter* loopCounter, struct Task* task, struct Element* element) { int i = loopCounter->i; task->code = Code1; task->key = i; element->task = task; context->next = CreateData1; loopCounter->i++; goto meta(context, PutQueue1); } __code createTask2_stub(struct Context* context) { goto createTask2(context, &context->data[LoopCounter]->loopCounter, &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->task = element->task; 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[ActiveQueue]->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[ActiveQueue]->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[ActiveQueue]->queue, &context->data[context->dataNum]->element); } __code getQueue(struct Context* context, struct Queue* queue) { if (queue->count == 0) return; struct Element* first = queue->first; if (__sync_bool_compare_and_swap(&queue->first, first, first->next)) { queue->count--; context->next = GetQueue; stack_push(context->code_stack, &context->next); context->next = first->task->code; stack_push(context->code_stack, &context->next); goto meta(context, Search); } else { goto meta(context, GetQueue); } } __code getQueue_stub(struct Context* context) { goto getQueue(context, &context->data[ActiveQueue]->queue); } __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); 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; goto meta(context, Exit); } __code taskManager_stub(struct Context* context) { goto taskManager(context, &context->data[LoopCounter]->loopCounter, &context->data[Worker]->worker); } int main(int argc, char** argv) { int cpu_num = (int)atoi(argv[1]); length = (int)atoi(argv[2]); array_ptr = (int*)malloc(sizeof(int)*length); for(int i=0; i<length; i++) array_ptr[i]=i; struct Context* main_context = (struct Context*)malloc(sizeof(struct Context)); initContext(main_context); //main_context->next = CreateWorker; main_context->next = CreateData1; struct Context* worker_contexts = (struct Context*)malloc(sizeof(struct Context)*cpu_num); 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); }