changeset 325:7d664be4efa5 examples_directory

Run docker container
author Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
date Tue, 11 Apr 2017 22:21:47 +0900
parents 131a04a70dd8
children f23f6d0aa4e9
files Dockerfile src/parallel_execution/CMakeLists.txt src/parallel_execution/CPUWorker.cbc src/parallel_execution/examples/twice.cbc
diffstat 4 files changed, 197 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/Dockerfile	Tue Mar 21 16:07:30 2017 +0900
+++ b/Dockerfile	Tue Apr 11 22:21:47 2017 +0900
@@ -12,7 +12,7 @@
 RUN /root/CbC_llvm/configure --enable-assertions
 RUN make -j 2
 RUN make install
-
+ENV CBC_COMPILER /usr/local/bin/clang
 WORKDIR /root
 RUN git clone https://github.com/choller/llcov
 
--- a/src/parallel_execution/CMakeLists.txt	Tue Mar 21 16:07:30 2017 +0900
+++ b/src/parallel_execution/CMakeLists.txt	Tue Apr 11 22:21:47 2017 +0900
@@ -7,6 +7,7 @@
 
 set(CMAKE_C_COMPILER $ENV{CBC_COMPILER})
 add_definitions("-Wall -g")
+set(CMAKE_C_FLAGS "-lpthread")
 
 
 if (${USE_CUDA})
@@ -59,7 +60,7 @@
   TARGET
       twice
   SOURCES 
-      main.cbc RedBlackTree.cbc compare.c SingleLinkedStack.cbc CPUWorker.cbc time.cbc twice.cbc TaskManagerImpl.cbc SingleLinkedQueue.cbc SynchronizedQueue.cbc SemaphoreImpl.cbc
+      examples/twice.cbc RedBlackTree.cbc compare.c SingleLinkedStack.cbc CPUWorker.cbc time.cbc twice.cbc TaskManagerImpl.cbc SingleLinkedQueue.cbc SynchronizedQueue.cbc SemaphoreImpl.cbc
 )
 
 if (${USE_CUDA})
--- a/src/parallel_execution/CPUWorker.cbc	Tue Mar 21 16:07:30 2017 +0900
+++ b/src/parallel_execution/CPUWorker.cbc	Tue Apr 11 22:21:47 2017 +0900
@@ -1,5 +1,3 @@
-#include <libkern/OSAtomic.h>
-
 #include "../context.h"
 
 static void start_worker(Worker* worker);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/parallel_execution/examples/twice.cbc	Tue Apr 11 22:21:47 2017 +0900
@@ -0,0 +1,194 @@
+#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 print_queue(struct Element* element) {
+    while (element) {
+        printf("%p\n", ((struct Task *)(element->data)));
+        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);
+    }
+}
+
+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);
+    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->next = C_code2;
+    goto meta(context, 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 code2(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_code2);
+        } else
+            puts("wrong result");
+
+    }
+
+    goto meta(context, C_exit_code);
+}
+
+__code createTask1(struct LoopCounter* loopCounter, struct TaskManager* taskManager) {
+    int i = loopCounter->i;
+
+    if ((length/split*i) < length) {
+        taskManager->next = C_createTask2;
+        goto meta(context, taskManager->taskManager->TaskManager.createTask);
+    }
+
+    loopCounter->i = 0;
+    taskManager->next = C_code1;
+#if ( defined(USE_CUDAWorker) && defined(USE_CUDA_MAIN_THREAD))
+sleep(5);
+#endif
+    goto meta(context, taskManager->taskManager->TaskManager.shutdown);
+}
+
+__code createTask2(LoopCounter* loopCounter, TaskManager* taskManager,struct Context* task, LoopCounter* loopCounter2, Array* array) {
+    int i = loopCounter->i;
+    array->index = i;
+    array->prefix = length/split;
+    array->array = array_ptr;
+    array->size = length;
+    loopCounter2->i = 0;
+    task->idgCount = 0;
+    if (gpu_num) {
+#ifdef USE_CUDAWorker
+        task->next = C_CUDAtwice;
+        task->workerId = CPU_CUDA;
+#else
+        task->next = C_twice;
+#endif
+    } else {
+        task->next = C_twice;
+    }
+    task->data[task->dataNum] = (union Data*)loopCounter2;
+    task->data[task->dataNum+1] = (union Data*)array;
+    task->odg = task->dataNum + 2;
+    task->maxOdg = task->odg;
+    taskManager->next = C_createTask1;
+    loopCounter->i++;
+    goto meta(context, taskManager->taskManager->TaskManager.spawn);
+}
+
+__code createTask2_stub(struct Context* context) {
+    LoopCounter* loopCounter = &ALLOCATE(context, LoopCounter)->LoopCounter;
+    Array* array = &ALLOCATE(context, Array)->Array;
+    goto createTask2(context,
+            Gearef(context, LoopCounter),
+            Gearef(context, TaskManager),
+            Gearef(context, TaskManager)->context,
+            loopCounter,
+            array);
+}
+
+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);
+}