Mercurial > hg > GearsTemplate
view src/parallel_execution/examples/bitonicSort/bitonicSort.cbc @ 383:300c18700ca5
Add split to bitonicSort
author | Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Tue, 25 Jul 2017 04:52:18 +0900 |
parents | f1d111e293c4 |
children | c5cd9888bf2a |
line wrap: on
line source
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <unistd.h> #include <math.h> #include "../../../context.h" int cpu_num = 1; int length = 1024; int split = 8; int gpu_num = 0; int CPU_ANY = -1; int CPU_CUDA = -1; void *start_taskManager(struct Context *context) { goto initDataGears(context, Gearef(context, LoopCounter), Gearef(context, TaskManager)); return 0; } #ifdef USE_CUDAWorker #ifdef USE_CUDA_MAIN_THREAD extern volatile int cuda_initialized; #endif #endif __code initDataGears(struct LoopCounter* loopCounter, struct TaskManager* taskManager) { // loopCounter->tree = createRedBlackTree(context); loopCounter->i = 0; taskManager->taskManager = (union Data*)createTaskManagerImpl(context, cpu_num, gpu_num, 0); #ifdef USE_CUDAWorker #ifdef USE_CUDA_MAIN_THREAD while(! cuda_initialized) {}; #endif #endif goto meta(context, C_code1); } __code initDataGears_stub(struct Context* context) { struct TaskManager* taskManager = Gearef(context, TaskManager); taskManager->taskManager = 0; #if (! defined(USE_CUDAWorker) || ! defined(USE_CUDA_MAIN_THREAD)) struct LoopCounter* loopCounter = Gearef(context, LoopCounter); goto initDataGears(context, loopCounter, taskManager); #else cuda_initialized = 0; pthread_t thread; pthread_create(&thread, NULL, (void*)&start_taskManager, context); while (taskManager->taskManager == 0); TaskManager *t = (TaskManager*)taskManager->taskManager; TaskManagerImpl *im = (TaskManagerImpl*)t->taskManager; struct Queue *q = (Queue *)im->workers[0]; createCUDAWorker(context,0,q, im); pthread_join(thread,0); exit(0); #endif } __code code1(struct Time* time) { printf("cpus:\t\t%d\n", cpu_num); printf("gpus:\t\t%d\n", gpu_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"); */ goto meta(context, C_createTask1); } __code createTask1(struct LoopCounter* loopCounter, struct TaskManager* taskManager) { int logN = (int)log2((float)length); struct Context** tasks = (struct Context**)ALLOC_ARRAY(context, Context, logN*(1+logN)/2 + 2); int taskCount = 0; struct SortArray* outputSortArray = &ALLOCATE_DATA_GEAR(context, SortArray)->SortArray; struct SortArray* inputSortArray = outputSortArray; struct Time* time = createTimeImpl(context); // par goto makeArray(outputSortArray, time, _exit); // par goto makeArray(outputSortArray, time, time->start); ? struct Context* task = NEW(struct Context); initContext(task); task->next = C_makeArray; task->idgCount = 0; task->idg = task->dataNum; task->maxIdg = task->idg; task->odg = task->maxIdg; task->data[task->odg] = (union Data*)outputSortArray; task->data[task->odg+1] = (union Data*)time; task->maxOdg = task->odg + 2; tasks[taskCount] = task; taskCount++; for (int i=2; i <= length; i=2*i) { int first = 1; for (int j=i>>1; j > 0; j=j>>1) { outputSortArray = &ALLOCATE_DATA_GEAR(context, SortArray)->SortArray; struct Context* task = NEW(struct Context); initContext(task); struct Integer* integer1 = &ALLOCATE_DATA_GEAR(context, Integer)->Integer; struct Integer* integer2 = &ALLOCATE_DATA_GEAR(context, Integer)->Integer; integer1->value = j; integer2->value = first; inputSortArray->prefix = length/2/split; task->next = C_bitonicSwap; task->iterator = createOneDimIterator(context, split); task->idgCount = 1; task->idg = task->dataNum; task->data[task->idg] = (union Data*)inputSortArray; task->data[task->idg+1] = (union Data*)integer1; task->data[task->idg+2] = (union Data*)integer2; task->maxIdg = task->idg + 3; task->odg = task->maxIdg; task->data[task->odg] = (union Data*)outputSortArray; task->maxOdg = task->odg + 1; tasks[taskCount] = task; taskCount++; first = 0; inputSortArray = outputSortArray; } } // par goto printArray(inputSortArray, time, __exit) // par goto printArray(inputSortArray, time, time->exit)? task = NEW(struct Context); initContext(task); task->next = C_printArray; task->idgCount = 2; task->idg = task->dataNum; task->data[task->idg] = (union Data*)inputSortArray; task->data[task->idg+1] = (union Data*)time; task->maxIdg = task->idg + 2; task->odg = task->maxIdg; task->maxOdg = task->odg; tasks[taskCount] = task; taskManager->contexts = tasks; // goto code2(); taskManager->next1 = C_code2; goto meta(context, taskManager->taskManager->TaskManager.spawnTasks); //goto meta(context, taskManager->taskManager->TaskManager.shutdown); } __code code2(struct LoopCounter* loopCounter, struct TaskManager* taskManager, struct Time* time) { sleep(40); taskManager->next = C_exit_code; goto meta(context, taskManager->taskManager->TaskManager.shutdown); } 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]); else if (strcmp(argv[i], "-cuda") == 0) { gpu_num = 1; CPU_CUDA = 0; } } } int main(int argc, char** argv) { init(argc, argv); struct Context* main_context = NEW(struct Context); initContext(main_context); main_context->next = C_initDataGears; goto start_code(main_context); } __code makeArray(struct Time* timeInterface, __code next(struct SortArray* output, struct Time* output1, ...)){ struct SortArray* output = *O_output; struct Time* output1 = *O_output1; if (output->loopCounter == 0){ output->array = (Integer**)ALLOC_ARRAY(context, Integer, length); srand((unsigned) time(NULL)); } if (output->loopCounter == GET_SIZE(output->array)){ printf("created Array\n"); output->loopCounter = 0; timeInterface->time = (union Data*)output1; timeInterface->next = context->next; *O_output = output; *O_output1 = output1; goto meta(context, output1->start); } struct Integer* integer = new Integer(); integer->value = rand() % 1000; output->array[output->loopCounter] = integer; //printf("%d\n", output->array[output->loopCounter]->value); output->loopCounter++; *O_output = output; *O_output1 = output1; goto meta(context, C_makeArray); } __code makeArray_stub(struct Context* context) { SortArray** O_output = (struct SortArray**)&context->data[context->odg]; Time** O_output1 = (struct Time**)&context->data[context->odg+1]; goto makeArray(context, Gearef(context, Time), context->next, O_output, O_output1); } __code printArray(struct Time* timeInterface, struct SortArray* inputArray, struct Time* inputTime, __code next(...)){ timeInterface->time = (union Data*)inputTime; timeInterface->next = C_printArray1; goto meta(context, inputTime->end); } __code printArray_stub(struct Context* context) { goto printArray(context, Gearef(context, Time), &context->data[context->idg]->SortArray, &context->data[context->idg+1]->Time, context->next); } __code printArray1(struct SortArray* inputArray, __code next(...)){ //printf("%d\n", inputArray->array[inputArray->loopCounter]->value); inputArray->loopCounter++; if (inputArray->loopCounter == GET_SIZE(inputArray->array)){ inputArray->loopCounter = 0; goto meta(context, next); } if (inputArray->array[inputArray->loopCounter-1]->value > inputArray->array[inputArray->loopCounter]->value) { printf("wrong result\n"); goto meta(context, next); } goto meta(context, C_printArray1); } __code printArray1_stub(struct Context* context) { goto printArray1(context, &context->data[context->idg]->SortArray, context->next); }