changeset 493:82f0c49750f1

Add codeGear for boundedBuffer example
author Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
date Sun, 31 Dec 2017 01:36:18 +0900
parents 9333486471b9
children d8b2036c6942
files src/parallel_execution/CMakeLists.txt src/parallel_execution/context.h src/parallel_execution/examples/boundedBuffer/BoundedBuffer.cbc src/parallel_execution/examples/boundedBuffer/SemaphoreImpl.cbc src/parallel_execution/examples/boundedBuffer/consumer.cbc src/parallel_execution/examples/boundedBuffer/initQueue.cbc src/parallel_execution/examples/boundedBuffer/main.cbc src/parallel_execution/examples/boundedBuffer/producer.cbc src/parallel_execution/generate_stub.pl
diffstat 9 files changed, 189 insertions(+), 43 deletions(-) [+]
line wrap: on
line diff
--- a/src/parallel_execution/CMakeLists.txt	Sat Dec 30 22:03:33 2017 +0900
+++ b/src/parallel_execution/CMakeLists.txt	Sun Dec 31 01:36:18 2017 +0900
@@ -138,5 +138,5 @@
   TARGET
       boundedBuffer
   SOURCES
-      examples/boundedBuffer/SemaphoreImpl.cbc examples/boundedBuffer/BoundedBuffer.cbc examples/boundedBuffer/consumer.cbc examples/boundedBuffer/producer.cbc CPUWorker.cbc TaskManagerImpl.cbc SingleLinkedQueue.cbc SynchronizedQueue.cbc MultiDimIterator.cbc AtomicReference.cbc
+      examples/boundedBuffer/main.cbc examples/boundedBuffer/initQueue.cbc examples/boundedBuffer/SemaphoreImpl.cbc examples/boundedBuffer/BoundedBuffer.cbc examples/boundedBuffer/consumer.cbc examples/boundedBuffer/producer.cbc CPUWorker.cbc TaskManagerImpl.cbc SingleLinkedQueue.cbc SynchronizedQueue.cbc MultiDimIterator.cbc AtomicReference.cbc
 )
--- a/src/parallel_execution/context.h	Sat Dec 30 22:03:33 2017 +0900
+++ b/src/parallel_execution/context.h	Sun Dec 31 01:36:18 2017 +0900
@@ -326,6 +326,7 @@
     struct SemaphoreImpl {
         int value;
         pthread_mutex_t mutex;
+        struct Queue* waitThreadQueue;
     } SemaphoreImpl;
     struct Allocate {
         enum Code next;
@@ -399,6 +400,13 @@
         union Data* body;
         int hash;
     } Memory;
+    struct BoundedBuffer {
+        struct Element* top;
+        struct Element* last;
+        struct Semaphore* fullCount;
+        struct Semaphore* emptyCount;
+        struct Semaphore* lock;
+    } BoundedBuffer;
 }; // union Data end       this is necessary for context generator
 typedef union Data Data;
 
--- a/src/parallel_execution/examples/boundedBuffer/BoundedBuffer.cbc	Sat Dec 30 22:03:33 2017 +0900
+++ b/src/parallel_execution/examples/boundedBuffer/BoundedBuffer.cbc	Sun Dec 31 01:36:18 2017 +0900
@@ -1,6 +1,6 @@
-#include "../../context.h"
-#interface Queue.h
-#interface Semaphore.h
+#include "../../../context.h"
+#interface "Queue.h"
+#interface "Semaphore.h"
 
 Queue* createBoundedBuffer(struct Context* context, int size) {
     struct Queue* queue = new Queue();
@@ -14,63 +14,63 @@
     queue->queue = (union Data*)boundedBuffer;
     queue->take = C_takeBoundedBuffer;
     queue->put = C_putBoundedBuffer;
-    queue->isEmpty = C_isEmptyBoundedBuffer;
-    queue->clear = C_clearBoundedBuffer;
+    // queue->isEmpty = C_isEmptyBoundedBuffer;
+    // queue->clear = C_clearBoundedBuffer;
     return queue;
 }
 
-__code putBoudnedBuffer(struct BoundedBuffer* boundedBuffer, union Data* data, __code next(union Data* data, ...)) {
-    struct Semaphore sem = boundedBuffer->emptyCount;
-    goto sem->p(putBoudnedBuffer1);
+__code putBoundedBuffer(struct BoundedBuffer* queue, union Data* data, __code next(...)) {
+    struct Semaphore* sem = queue->emptyCount;
+    goto sem->p(putBoundedBuffer1);
 }
 
-__code putBoudnedBuffer1(struct BoundedBuffer* boundedBuffer, union Data* data, __code next(union Data* data, ...)) {
-    struct Semaphore sem = boundedBuffer->lock;
-    goto sem->p(putBoudnedBuffer2);
+__code putBoundedBuffer1(struct BoundedBuffer* queue, union Data* data, __code next(...)) {
+    struct Semaphore* sem = queue->lock;
+    goto sem->p(putBoundedBuffer2);
 }
 
-__code putBoudnedBuffer2(struct BoundedBuffer* boundedBuffer, union Data* data, __code next(union Data* data, ...)) {
+__code putBoundedBuffer2(struct BoundedBuffer* queue, union Data* data, __code next(...)) {
     struct Element* element = new Element();
     element->data = data;
     element->next = NULL;
     struct Element* last = queue->last;
     last->next = element;
-    struct Semaphore sem = boundedBuffer->lock;
-    goto sem->v(putBoudnedBuffer3);
+    struct Semaphore* sem = queue->lock;
+    goto sem->v(putBoundedBuffer3);
 }
 
-__code putBoudnedBuffer3(struct BoundedBuffer* boundedBuffer, union Data* data, __code next(union Data* data, ...)) {
-    struct Semaphore sem = boundedBuffer->fullCount;
-    goto sem->v(putBoudnedBuffer4);
+__code putBoundedBuffer3(struct BoundedBuffer* queue, union Data* data, __code next(...)) {
+    struct Semaphore* sem = queue->fullCount;
+    goto sem->v(putBoundedBuffer4);
 }
 
-__code putBoudnedBuffer4(struct BoundedBuffer* boundedBuffer, union Data* data, __code next(union Data* data, ...)) {
+__code putBoundedBuffer4(struct BoundedBuffer* queue, union Data* data, __code next(...)) {
     goto next(data, ...);
 }
-__code takeBoudnedBuffer(struct BoundedBuffer* boundedBuffer, __code next(union Data* data, ...)) {
-    struct Semaphore sem = boundedBuffer->fullCount;
-    goto sem->p(takeBoudnedBuffer1);
+__code takeBoundedBuffer(struct BoundedBuffer* queue, __code next(union Data* data, ...)) {
+    struct Semaphore* sem = queue->fullCount;
+    goto sem->p(takeBoundedBuffer1);
 }
 
-__code takeBoudnedBuffer1(struct BoundedBuffer* boundedBuffer, __code next(union Data* data, ...)) {
-    struct Semaphore sem = boundedBuffer->lock;
-    goto sem->p(takeBoudnedBuffer2);
+__code takeBoundedBuffer1(struct BoundedBuffer* queue, __code next(union Data* data, ...)) {
+    struct Semaphore* sem = queue->lock;
+    goto sem->p(takeBoundedBuffer2);
 }
 
-__code takeBoudnedBuffer2(struct BoundedBuffer* boundedBuffer, __code next(union Data* data, ...)) {
+__code takeBoundedBuffer2(struct BoundedBuffer* queue, __code next(union Data* data, ...)) {
     struct Element* top = queue->top;
     struct Element* nextElement = top->next;
     data = nextElement->data;
     queue->top = nextElement;
-    struct Semaphore sem = boundedBuffer->lock;
-    goto sem->v(takeBoudnedBuffer3);
+    struct  Semaphore* sem = queue->lock;
+    goto sem->v(takeBoundedBuffer3);
 }
 
-__code takeBoudnedBuffer3(struct BoundedBuffer* boundedBuffer, __code next(union Data* data, ...)) {
-    struct Semaphore sem = boundedBuffer->emptyCount;
-    goto sem->v(takeBoudnedBuffer4);
+__code takeBoundedBuffer3(struct BoundedBuffer* queue, __code next(union Data* data, ...)) {
+    struct Semaphore* sem = queue->emptyCount;
+    goto sem->v(takeBoundedBuffer4);
 }
 
-__code takeBoudnedBuffer4(struct BoundedBuffer* boundedBuffer, __code next(union Data* data, ...)) {
+__code takeBoundedBuffer4(struct BoundedBuffer* queue, __code next(union Data* data, ...)) {
     goto next(data, ...);
 }
--- a/src/parallel_execution/examples/boundedBuffer/SemaphoreImpl.cbc	Sat Dec 30 22:03:33 2017 +0900
+++ b/src/parallel_execution/examples/boundedBuffer/SemaphoreImpl.cbc	Sun Dec 31 01:36:18 2017 +0900
@@ -1,5 +1,7 @@
-#include "../context.h"
-#interface "semaphore.h"
+#include "../../../context.h"
+#interface "Semaphore.h"
+#interface "Queue.h"
+#interface "TaskManager.h"
 
 Semaphore* createSemaphoreImpl(struct Context* context, int n) {
     struct Semaphore* semaphore = new Semaphore();
@@ -21,7 +23,7 @@
 __code pOperationSemaphoreImpl1(struct SemaphoreImpl* semaphore, __code next(...)) {
     if(semaphore->value == 0) {
         context->next= C_pOperationSemaphoreImpl;
-        struct Queue* queue = semaphoreImpl->waitThreadQueue;
+        struct Queue* queue = semaphore->waitThreadQueue;
         goto queue->put(context, pOperationSemaphoreImpl2); // put this context(thread, process)
     }
     semaphore->value--;
@@ -34,24 +36,25 @@
     goto worker->taskReceive(); // goto shceduler
 }
 
-__code pOperationSemaphoreImpl2_stub(sturct Context* context) {
+__code pOperationSemaphoreImpl2_stub(struct Context* context) {
     // switch worker context
     struct Context* workerContext = context->worker->worker->CPUWorker.context;
     SemaphoreImpl* semaphoreImpl = (SemaphoreImpl*)GearImpl(context, Semaphore, semaphore);
-    goto odgCommitCPUWorker(workerContext,
-                            semaphoreImpl,
-                            context->worker,
-                            Gearef(context, Semaphore)->next);
+    goto pOperationSemaphoreImpl2(workerContext,
+                                  semaphoreImpl,
+                                  context->worker,
+                                  Gearef(context, Semaphore)->next);
 }
 
 __code vOperationSemaphoreImpl(struct SemaphoreImpl* semaphore, __code next(...)) {
     pthread_mutex_lock(&semaphore->mutex);
     semaphore->value++;
-    struct Queue* queue = semaphoreImpl->waitThreadQueue;
-    goto queue->take(context, vOperationSemaphoreImpl1);                        
+    struct Queue* queue = semaphore->waitThreadQueue;
+    goto queue->take(vOperationSemaphoreImpl1);                        
 }
 
 __code vOperationSemaphoreImpl1(struct SemaphoreImpl* semaphore, __code next(...), struct Context* waitTask) {
+    struct TaskManager* taskManager = waitTask->taskManager;
     goto taskManager->spawn(waitTask, vOperationSemaphoreImpl2); //notify
 }
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/parallel_execution/examples/boundedBuffer/consumer.cbc	Sun Dec 31 01:36:18 2017 +0900
@@ -0,0 +1,25 @@
+#include "../../../context.h"
+#include <stdio.h>
+#interface "Queue.h"
+
+__code consumer(struct Queue* queue, __code next(...)) {
+    goto queue->take(consumer1);
+}
+
+__code consumer_stub(struct Context* context) {
+    goto consumer(context,
+                  &context->data[context->idg]->Queue,
+                  context->next);
+}
+
+__code consumer1(struct Queue* queue, __code next(...), struct Node* node) {
+    printf("getData %d\n", node->value->Int);
+    goto consumer();
+}
+
+__code consumer1_stub(struct Context* context) {
+    goto consumer1(context,
+                   &context->data[context->idg]->Queue,
+                   context->next,
+                   &Gearef(context, Queue)->data->Node);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/parallel_execution/examples/boundedBuffer/initQueue.cbc	Sun Dec 31 01:36:18 2017 +0900
@@ -0,0 +1,13 @@
+#include "../../../context.h"
+
+__code initQueue(__code next(struct Queue* output, ...)) {
+    struct Queue* output = *O_output;
+    goto next(output, ...);
+}
+
+__code initQueue_stub(struct Context* context) {
+    struct Queue** O_output = (struct Queue**)&context->data[context->odg];
+    goto initQueue(context,
+                  context->next,
+                  O_output);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/parallel_execution/examples/boundedBuffer/main.cbc	Sun Dec 31 01:36:18 2017 +0900
@@ -0,0 +1,82 @@
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <assert.h>
+
+#include "../../../context.h"
+#interface "TaskManager.h"
+
+int cpu_num = 1;
+int length = 100;
+int queue_size = 10;
+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;
+}
+
+__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);
+    goto meta(context, C_code1);
+}
+
+__code initDataGears_stub(struct Context* context) {
+    struct TaskManager* taskManager =  Gearef(context, TaskManager);
+    taskManager->taskManager = 0;
+    struct LoopCounter* loopCounter = Gearef(context, LoopCounter);
+    goto initDataGears(context, loopCounter, taskManager);
+}
+
+__code code1(struct Timer* timer) {
+    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_createTask1);
+    //goto meta(context, C_start_time);
+}
+
+__code code1_stub(struct Context* context) {
+    goto code1(context, Gearef(context, Timer));
+}
+
+
+__code createTask1(struct LoopCounter* loopCounter, struct TaskManager* taskManager) {
+    struct Queue* queue = createBoundedBuffer(context, queue_size);
+    par goto producer(queue, __exit);
+    par goto consumer(queue, __exit);
+    par goto initQueue(queue, __exit);
+    goto createTask1();
+}
+
+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], "-queueSize") == 0)
+            queue_size = (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);
+    goto initDataGears();
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/parallel_execution/examples/boundedBuffer/producer.cbc	Sun Dec 31 01:36:18 2017 +0900
@@ -0,0 +1,15 @@
+#include "../../../context.h"
+#interface "Queue.h"
+
+__code producer(struct Queue* queue, __code next(...)) {
+    Node* node = new Node();
+    node->value = (union Data*)new Int();
+    node->value->Int = 10;
+    goto queue->put(node, producer);
+}
+
+__code producer_stub(struct Context* context) {
+    goto producer(context,
+                  &context->data[context->idg]->Queue,
+                  context->next);
+}
--- a/src/parallel_execution/generate_stub.pl	Sat Dec 30 22:03:33 2017 +0900
+++ b/src/parallel_execution/generate_stub.pl	Sun Dec 31 01:36:18 2017 +0900
@@ -230,7 +230,7 @@
                 if ($typeName eq $var{$interface}->{$ivar}) {
                     if ($output) {
                         $dataGearName{$codeGearName} .= "\t$typeName$ptrType* O_$varName = &Gearef(context, $interface)->$varName;\n";
-                        $outputVar{$codeGearName} .= "\t$typeName$ptrType $varName;\n";
+                        $outputVar{$codeGearName} .= "\t$typeName$ptrType $varName = *O_$varName;\n";
                         return 1;
                     }