changeset 371:c3a7a086e82a

Add bitonicSort
author Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
date Fri, 07 Jul 2017 14:58:26 +0900
parents 59c694722ce6
children d6ce4273e7d1
files src/parallel_execution/examples/InitIntegerDataGears.cbc src/parallel_execution/examples/bitonicSort.cbc src/parallel_execution/examples/swap.cbc
diffstat 3 files changed, 252 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/parallel_execution/examples/InitIntegerDataGears.cbc	Fri Jul 07 14:58:26 2017 +0900
@@ -0,0 +1,25 @@
+#include "../../context.h"
+#include <stdio.h>
+__code initIntegerDataGears(__code next(struct Integer* output1, struct Integer* output2, struct Integer* output3, ...)) {
+    struct Integer* output1 = *O_output1;
+    struct Integer* output2 = *O_output2;
+    struct Integer* output3 = *O_output3;
+    output1->value = 1;
+    output2->value = 2;
+    output3->value = 3;
+    *O_output1 = output1;
+    *O_output2 = output2;
+    *O_output3 = output3;
+    goto meta(context, next);
+}
+
+__code initIntegerDataGears_stub(struct Context* context) {
+    Integer** O_output1 = (struct Integer **)&context->data[context->odg];
+    Integer** O_output2 = (struct Integer **)&context->data[context->odg+1];
+    Integer** O_output3 = (struct Integer **)&context->data[context->odg+2];
+    goto initIntegerDataGears(context,
+            context->next,
+            O_output1,
+            O_output2,
+            O_output3);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/parallel_execution/examples/bitonicSort.cbc	Fri Jul 07 14:58:26 2017 +0900
@@ -0,0 +1,199 @@
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#include "../../context.h"
+
+int cpu_num = 1;
+int length = 100;
+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_createTask1);
+}
+
+__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);
+    /* puts("queue"); */
+    /* print_queue(context->data[ActiveQueue]->queue.first); */
+    /* puts("tree"); */
+    /* print_tree(context->data[Tree]->tree.root); */
+    /* puts("result"); */
+
+    //time->next = C_code2;
+    goto meta(context, C_exit_code);
+    //goto meta(context, C_start_time);
+}
+
+__code code1_stub(struct Context* context) {
+    goto code1(context, Gearef(context, Time));
+}
+
+
+__code createTask1(struct LoopCounter* loopCounter, struct TaskManager* taskManager) {
+    int waitFlag = 0;
+    struct SortArray* inputSortArray = &ALLOCATE_DATA_GEAR(context, sortArray)->sortArray;
+    inputSortArray->array = array;
+    for (int i=2; i <= length; i=2*i) {
+        int first = 1;
+        lastOne = (length <= i*2) ? true : false;
+        for (int j=i>>1; 0 < j; j=j>>1) {
+            struct SortArray* outputSortArray = &ALLOCATE_DATA_GEAR(context, sortArray)->sortArray;
+            outputSortArray->array = array;
+            for(int k=0; 0 < length/2; k++) {
+                struct Context* task = NEW(struct Context);
+                struct Integer* integer1 = &ALLOCATE_DATA_GEAR(context, Integer)->Integer;
+                struct Integer* integer2 = &ALLOCATE_DATA_GEAR(context, Integer)->Integer;
+                struct Integer* integer3 = &ALLOCATE_DATA_GEAR(context, Integer)->Integer;
+                integer1->value = j;
+                integer2->value = first;
+                integer3->value = k;
+
+                task->next = C_swap;
+                if (waitFlag) {
+                    task->idgCount = length/2;
+                } else {
+                    task->idgCount = 0;
+                }
+                task->idg = task->dataNum;
+                task->data[task->idg+1] = (union Data*)inputSortArray;
+                task->data[task->idg+2] = (union Data*)integer1;
+                task->data[task->idg+3] = (union Data*)integer2;
+                task->data[task->idg+4] = (union Data*)integer3;
+                task->maxIdg = task->idg + 4;
+                task->odg = task->maxIdg;
+                task->data[task->odg] = outputSortArray;
+                task->maxOdg = task->odg + 1;
+                first = 0;
+            }
+            inputSortArray = outputSortArray;
+        }
+        waitFlag = 1;
+    }
+    goto meta(context, taskManager->taskManager->TaskManager.shutdown);
+}
+
+__code createTask2(struct LoopCounter* loopCounter, struct TaskManager* taskManager) {
+    struct Context** tasks = (struct Context**)ALLOC_ARRAY(context, Context, 3);
+
+    Integer* integer1 = &ALLOCATE_DATA_GEAR(context, Integer)->Integer;
+    Integer* integer2 = &ALLOCATE_DATA_GEAR(context, Integer)->Integer;
+    Integer* integer3 = &ALLOCATE_DATA_GEAR(context, Integer)->Integer;
+    // par goto mult(integer1, integer2, integer3, __exit);
+    struct Context* task = NEW(struct Context);
+    initContext(task);
+    task->taskManager = &taskManager->taskManager->TaskManager;
+    task->next = C_mult;
+    task->idgCount = 2;
+    task->idg = task->dataNum;
+    task->data[task->idg] = (union Data*)integer1;
+    task->data[task->idg+1] = (union Data*)integer2;
+    task->maxIdg = task->idg + 2;
+    task->odg = task->maxIdg;
+    task->data[task->odg] = (union Data*)integer3;
+    task->maxOdg = task->odg + 1;
+    tasks[0] = task;
+
+    Integer* integer4 = &ALLOCATE_DATA_GEAR(context, Integer)->Integer;
+    Integer* integer5 = &ALLOCATE_DATA_GEAR(context, Integer)->Integer;
+    // par goto add(integer4, integer5, integer1, __exit);
+    task = NEW(struct Context);
+    initContext(task);
+    task->taskManager = &taskManager->taskManager->TaskManager;
+    task->next = C_add;
+    task->idgCount = 2;
+    task->idg = task->dataNum;
+    task->data[task->idg] = (union Data*)integer4;
+    task->data[task->idg+1] = (union Data*)integer5;
+    task->maxIdg = task->idg + 2;
+    task->odg = task->maxIdg;
+    task->data[task->odg] = (union Data*)integer1;
+    task->maxOdg = task->odg + 1;
+    tasks[1] = task;
+
+    // par goto initIntegerDataGears(integer2, integer4, integer5, __exit);
+    task = NEW(struct Context);
+    initContext(task);
+    task->taskManager = &taskManager->taskManager->TaskManager;
+    task->next = C_initIntegerDataGears;
+    task->idgCount = 0;
+    task->idg = task->dataNum;
+    task->maxIdg = task->idg;
+    task->odg = task->maxIdg;
+    task->data[task->odg] = (union Data*)integer2;
+    task->data[task->odg+1] = (union Data*)integer4;
+    task->data[task->odg+2] = (union Data*)integer5;
+    task->maxOdg = task->odg + 3;
+    tasks[2] = task;
+
+    taskManager->contexts = tasks;
+    // goto crateTask1();
+    taskManager->next1 = C_createTask1;
+    goto meta(context, taskManager->taskManager->TaskManager.spawnTasks);
+}
+
+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], "-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);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/parallel_execution/examples/swap.cbc	Fri Jul 07 14:58:26 2017 +0900
@@ -0,0 +1,28 @@
+#include "../../context.h"
+#include <stdio.h>
+__code swap(struct SortArray sortArray, struct Integer* block, struct Integer* first, struct Integer* i, __code next(struct SortArray* output, ...)) {
+    int position = i->value/block->value;
+    int index = i->value+block->value*position;
+
+    block->value = (first->value == 1)? ((block->value<<1)*(position+1))-(index%block->value)-1 : index+block->value;
+
+    if (sortArray->array[block] < sortArray->array[index]) {
+        int Integer *tmp = sortArray->array[index];
+        sortArray->array[index] = sortArray->array[block];
+        sortArray->array[block] = tmp;
+    }
+
+    *O_output = output;
+    goto meta(context, next);
+}
+
+__code swap_stub(struct Context* context) {
+    SortArray** O_output = (struct SortArray **)&context->data[context->odg];
+    goto add(context,
+            &context->data[context->idg]->SortArray,
+            &context->data[context->idg+1]->Integer,
+            &context->data[context->idg+2]->Integer,
+            &context->data[context->idg+3]->Integer,
+            context->next,
+            O_output);
+}