Mercurial > hg > Members > Moririn
changeset 409:4d1e3697a6b8
Add twice cbc file
author | Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Wed, 06 Sep 2017 22:01:27 +0900 |
parents | 8ee89eefbc6d |
children | 85b0ddbf458e |
files | src/parallel_execution/examples/twice/createArray.cbc src/parallel_execution/examples/twice/main.cbc |
diffstat | 2 files changed, 157 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/parallel_execution/examples/twice/createArray.cbc Wed Sep 06 22:01:27 2017 +0900 @@ -0,0 +1,23 @@ +#include <stdio.h> + +#include "../../../context.h" + +extern int length; +extern int split; +extern int* array_ptr; + +__code createArray(__code next(struct Array* output, ...)) { + struct Array* output = *O_output; + output->prefix = length/split; + output->array = array_ptr; + output->size = length; + *O_output = output; + goto meta(context, context->next); +} + +__code createArray_stub(struct Context* context) { + Array** O_output = (struct Array **)&context->data[context->odg]; + goto createArray(context, + context->next, + O_output); +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/parallel_execution/examples/twice/main.cbc Wed Sep 06 22:01:27 2017 +0900 @@ -0,0 +1,134 @@ +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +#include <unistd.h> + +#include "../../../context.h" + +int cpu_num = 1; +int length = 102400; +int split = 8; +int* array_ptr; +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"); */ + time->time = (union Data*)createTimeImpl(context); + time->next = C_createTask1; + goto meta(context, time->time->Time.start); +} + +__code code2(struct Time* time, struct TaskManager* taskManager) { + time->next = C_code3; + taskManager->next = time->time->Time.end; + goto meta(context, taskManager->taskManager->TaskManager.shutdown); +} + +__code code3(struct LoopCounter* loopCounter) { + int i = loopCounter->i; + + if (i < length) { + //printf("%d\n", array_ptr[i]); + if (array_ptr[i] == (i*2)) { + loopCounter->i++; + goto meta(context, C_code3); + } else + puts("wrong result"); + + } + + goto meta(context, C_exit_code); +} + +__code createTask1(struct LoopCounter* loopCounter, struct TaskManager* taskManager) { + Array* array = &ALLOCATE_DATA_GEAR(context, Array)->Array; + + par goto createArray(array, __exit); + + par goto twice(array, iterate(split), __exit); + goto code2(); +} + +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); + + 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); +}