changeset 186:dbc064c26b98

generate context script
author mir3636
date Thu, 15 Dec 2016 19:38:18 +0900
parents 247a2c4a8908
children bacc37265386
files src/parallel_execution/context.c src/parallel_execution/context.h src/parallel_execution/generate_context.pl src/parallel_execution/main.c
diffstat 4 files changed, 126 insertions(+), 334 deletions(-) [+]
line wrap: on
line diff
--- a/src/parallel_execution/context.c	Thu Dec 15 18:10:48 2016 +0900
+++ b/src/parallel_execution/context.c	Thu Dec 15 19:38:18 2016 +0900
@@ -4,86 +4,7 @@
 #include "stack.h"
 #include "queue.h"
 
-extern __code code1_stub(struct Context*);
-extern __code code2_stub(struct Context*);
-extern __code code3_stub(struct Context*);
-extern __code code4_stub(struct Context*);
-extern __code code5_stub(struct Context*);
-extern __code find(struct Context*);
-extern __code not_find(struct Context*);
-extern __code code6(struct Context*);
-extern __code meta(struct Context*);
-extern __code put_stub(struct Context*);
-extern __code replaceNode_stub(struct Context*);
-extern __code replaceNode1_stub(struct Context*);
-extern __code insertNode_stub(struct Context*);
-extern __code rotateLeft_stub(struct Context*);
-extern __code rotateLeft1_stub(struct Context*);
-extern __code rotateRight_stub(struct Context*);
-extern __code rotateRight1_stub(struct Context*);
-extern __code colorFlip_stub(struct Context*);
-extern __code fixUp_stub(struct Context*);
-extern __code changeReference_stub(struct Context*);
-extern __code insertCase1_stub(struct Context*);
-extern __code insertCase2_stub(struct Context*);
-extern __code insertCase3_stub(struct Context*);
-extern __code insertCase4_stub(struct Context*);
-extern __code insertCase5_stub(struct Context*);
-extern __code insertCase51_stub(struct Context*);
-extern __code get_stub(struct Context*);
-extern __code search_stub(struct Context*);
-extern __code delete_stub(struct Context*);
-extern __code delete1_stub(struct Context*);
-extern __code delete2_stub(struct Context*);
-extern __code delete3_stub(struct Context*);
-extern __code replaceNodeForDelete1_stub(struct Context*);
-extern __code replaceNodeForDelete2_stub(struct Context*);
-extern __code findMax1_stub(struct Context*);
-extern __code findMax2_stub(struct Context*);
-extern __code deleteCase1_stub(struct Context*);
-extern __code deleteCase2_stub(struct Context*);
-extern __code deleteCase3_stub(struct Context*);
-extern __code deleteCase4_stub(struct Context*);
-extern __code deleteCase5_stub(struct Context*);
-extern __code deleteCase6_stub(struct Context*);
-extern __code createWorker_stub(struct Context*);
-extern __code taskManager_stub(struct Context*);
-extern __code createData1_stub(struct Context*);
-extern __code createData2_stub(struct Context*);
-extern __code createTask1_stub(struct Context*);
-extern __code createTask2_stub(struct Context*);
-extern __code createTask3_stub(struct Context*);
-extern __code createTask4_stub(struct Context*);
-extern __code putQueue1_stub(struct Context*);
-extern __code putQueue2_stub(struct Context*);
-extern __code putQueue3_stub(struct Context*);
-extern __code putQueue4_stub(struct Context*);
-extern __code getTask1_stub(struct Context*);
-extern __code getTask2_stub(struct Context*);
-extern __code queue_test1_stub(struct Context*);
-extern __code queue_test2_stub(struct Context*);
-extern __code queue_test3_stub(struct Context*);
-extern __code queue_test4_stub(struct Context*);
-extern __code spawnTask_stub(struct Context*);
-extern __code twice_stub(struct Context*);
-extern __code start_time_stub(struct Context*);
-extern __code end_time_stub(struct Context*);
-extern __code exit_code(struct Context*);
-extern __code clearSingleLinkedQueue_stub(struct Context*);
-extern __code putSingleLinkedQueue_stub(struct Context *);
-extern __code takeSingleLinkedQueue_stub(struct Context *);
-extern __code isEmptySingleLinkedQueue_stub(struct Context *);
-extern __code clearSynchronizedQueue_stub(struct Context*);
-extern __code putSynchronizedQueue_stub(struct Context *);
-extern __code takeSynchronizedQueue_stub(struct Context *);
-extern __code isEmptySynchronizedQueue_stub(struct Context *);
-extern __code spawnTaskManager_stub(struct Context*);
-extern __code shutdownTaskManager_stub(struct Context*);
-extern __code executeWorker_stub(struct Context*);
-extern __code taskSendWorker_stub(struct Context*);
-extern __code taskReciveWorker_stub(struct Context*);
-extern __code shutdownWorker_stub(struct Context*);
-
+#include "c/extern.h"
 
 __code initContext(struct Context* context) {
     context->heapLimit = sizeof(union Data)*ALLOCATE_SIZE;
@@ -94,118 +15,9 @@
 
     context->codeNum = Exit;
 
-    context->code[C_code1]        = code1_stub;
-    context->code[C_code2]        = code2_stub;
-    context->code[C_put]          = put_stub;
-    context->code[C_replaceNode]  = replaceNode_stub;
-    context->code[C_replaceNode1] = replaceNode1_stub;
-    context->code[C_insertNode]   = insertNode_stub;
-    context->code[C_rotateLeft]   = rotateLeft_stub;
-    context->code[C_rotateLeft1]  = rotateLeft1_stub;
-    context->code[C_rotateRight]  = rotateRight_stub;
-    context->code[C_rotateRight1] = rotateRight1_stub;
-    context->code[C_insertCase1]  = insertCase1_stub;
-    context->code[C_insertCase2]  = insertCase2_stub;
-    context->code[C_insertCase3]  = insertCase3_stub;
-    context->code[C_insertCase4]  = insertCase4_stub;
-    context->code[C_insertCase5]  = insertCase5_stub;
-    context->code[C_insertCase51] = insertCase51_stub;
-    context->code[C_get]          = get_stub;
-    context->code[C_search]       = search_stub;
-
-    context->code[C_clearSingleLinkedStack]   = clearSingleLinkedStack_stub;
-    context->code[C_pushSingleLinkedStack]    = pushSingleLinkedStack_stub;
-    context->code[C_popSingleLinkedStack]     = popSingleLinkedStack_stub;
-    context->code[C_pop2SingleLinkedStack]    = pop2SingleLinkedStack_stub;
-    context->code[C_getSingleLinkedStack]     = getSingleLinkedStack_stub;
-    context->code[C_get2SingleLinkedStack]    = get2SingleLinkedStack_stub;
-    context->code[C_isEmptySingleLinkedStack] = isEmptySingleLinkedStack_stub;
-
-    context->code[C_clearSingleLinkedQueue]   = clearSingleLinkedQueue_stub;
-    context->code[C_putSingleLinkedQueue]     = putSingleLinkedQueue_stub;
-    context->code[C_takeSingleLinkedQueue]    = takeSingleLinkedQueue_stub;
-    context->code[C_isEmptySingleLinkedQueue] = isEmptySingleLinkedQueue_stub;
-
-    context->code[C_clearSynchronizedQueue]   = clearSynchronizedQueue_stub;
-    context->code[C_putSynchronizedQueue]     = putSynchronizedQueue_stub;
-    context->code[C_takeSynchronizedQueue]    = takeSynchronizedQueue_stub;
-    context->code[C_isEmptySynchronizedQueue] = isEmptySynchronizedQueue_stub;
-
-    context->code[C_spawnTaskManager] = spawnTaskManager_stub;
-    context->code[C_shutdownTaskManager] = shutdownTaskManager_stub;
+#include "c/codeGearInit.c"
 
-    /* context->code[Delete]        = delete_stub; */
-    /* context->code[Delete1]       = delete1_stub; */
-    /* context->code[Delete2]       = delete2_stub; */
-    /* context->code[Delete3]       = delete3_stub; */
-    /* context->code[Replace_d1] = replaceNodeForDelete1_stub; */
-    /* context->code[Replace_d2] = replaceNodeForDelete2_stub; */
-    /* context->code[FindMax1]    = findMax1_stub; */
-    /* context->code[FindMax2]    = findMax2_stub; */
-    /* context->code[DeleteCase1]   = deleteCase1_stub; */
-    /* context->code[DeleteCase2]   = deleteCase2_stub; */
-    /* context->code[DeleteCase3]   = deleteCase3_stub; */
-    /* context->code[DeleteCase4]   = deleteCase4_stub; */
-    /* context->code[DeleteCase5]   = deleteCase5_stub; */
-    /* context->code[DeleteCase6]   = deleteCase6_stub; */
-    context->code[CreateWorker]  = createWorker_stub;
-    context->code[C_executeWorker]  = executeWorker_stub;
-    context->code[C_taskSendWorker]  = taskSendWorker_stub;
-    context->code[C_taskReciveWorker]  = taskReciveWorker_stub;
-    context->code[C_shutdownWorker]  = shutdownWorker_stub;
-    context->code[TaskManager]   = taskManager_stub;
-    context->code[CreateData1]   = createData1_stub;
-    context->code[CreateData2]   = createData2_stub;
-    context->code[CreateTask1]   = createTask1_stub;
-    context->code[CreateTask2]   = createTask2_stub;
-    context->code[CreateTask3]   = createTask3_stub;
-    context->code[CreateTask4]   = createTask4_stub;
-    context->code[PutQueue1]     = putQueue1_stub;
-    context->code[PutQueue2]     = putQueue2_stub;
-    context->code[PutQueue3]     = putQueue3_stub;
-    context->code[PutQueue4]     = putQueue4_stub;
-    context->code[C_getTask1]    = getTask1_stub;
-    context->code[C_getTask2]    = getTask2_stub;
-    context->code[SpawnTask]     = spawnTask_stub;
-    context->code[C_QueueTest1]  = queue_test1_stub;
-    context->code[C_QueueTest2]  = queue_test2_stub;
-    context->code[C_QueueTest3]  = queue_test3_stub;
-    context->code[C_QueueTest4]  = queue_test4_stub;
-    context->code[Twice]         = twice_stub;
-    context->code[StartTime]     = start_time_stub;
-    context->code[EndTime]       = end_time_stub;
-    context->code[Exit]          = exit_code;
-
-    struct Worker* worker = ALLOC_DATA(context, Worker);
-    worker->id = 0;
-    worker->contexts = 0;
-
-    struct Allocate* allocate = ALLOC_DATA(context, Allocate);
-    allocate->size = 0;
-
-    
-    ALLOC_DATA(context, Stack);
-
-    ALLOC_DATA(context, Tree);
-
-    ALLOC_DATA(context, RedBlackTree);
-
-    ALLOC_DATA(context, RotateTree);
-
-    struct Node* node = ALLOC_DATA(context, Node);
-    node->key = 0;
-    node->value = 0;
-    node->left = 0;
-    node->right = 0;
-    
-    struct LoopCounter* counter = ALLOC_DATA(context, LoopCounter);
-    counter->i = 0;
-
-    struct Element* element = ALLOC_DATA(context, Element);
-    element->data = 0;
-    element->next = 0;
-
-    ALLOC_DATA(context, Time);
+#include "c/dataGearInit.c"
 
     context->data[D_ActiveQueue] = createSynchronizedQueue(context);
     context->data[D_WaitQueue]   = createSynchronizedQueue(context);
--- a/src/parallel_execution/context.h	Thu Dec 15 18:10:48 2016 +0900
+++ b/src/parallel_execution/context.h	Thu Dec 15 19:38:18 2016 +0900
@@ -35,98 +35,8 @@
     meta->type; })
 
 #define Gearef(context, t) (&(context)->data[D_##t]->t)
-enum Code {
-    C_code1,
-    C_code2,
-    C_code3,
-    C_code4,
-    C_code5,
-    C_find,
-    Not_find,
-    Code6,
-    Allocator,
-    C_put,
-    C_replaceNode,
-    C_replaceNode1,
-    C_insertNode,
-    Compare,
-    C_rotateLeft,
-    C_rotateLeft1,
-    C_rotateRight,
-    C_rotateRight1,
-    SetTree,
-    C_insertCase1,
-    C_insertCase2,
-    C_insertCase3,
-    C_insertCase4,
-    C_insertCase5,
-    C_insertCase51,
-    C_stackClear,
-    C_get,
-    C_search,
-    Delete,
-    Delete1,
-    Delete2,
-    Delete3,
-    Replace_d1,
-    Replace_d2,
-    FindMax1,
-    FindMax2,
-    DeleteCase1,
-    DeleteCase2,
-    DeleteCase3,
-    DeleteCase4,
-    DeleteCase5,
-    DeleteCase6,
-    CreateWorker,
-    TaskManager,
-    C_spawnTaskManager,
-    C_shutdownTaskManager,
-    C_QueueTest1,
-    C_QueueTest2,
-    C_QueueTest3,
-    C_QueueTest4,
-    CreateData1,
-    CreateData2,
-    CreateTask1,
-    CreateTask2,
-    CreateTask3,
-    CreateTask4,
-    PutQueue1,
-    PutQueue2,
-    PutQueue3,
-    PutQueue4,
-    C_getTask1,
-    C_getTask2,
-    C_clearSingleLinkedStack,
-    C_pushSingleLinkedStack,
-    C_popSingleLinkedStack,
-    C_pop2SingleLinkedStack,
-    C_getSingleLinkedStack,
-    C_get2SingleLinkedStack,
-    C_isEmptySingleLinkedStack,
-    C_takeSingleLinkedQueue,
-    C_putSingleLinkedQueue,
-    C_isEmptySingleLinkedQueue,
-    C_clearSingleLinkedQueue,
-    C_takeSynchronizedQueue,
-    C_putSynchronizedQueue,
-    C_isEmptySynchronizedQueue,
-    C_clearSynchronizedQueue,
-    C_putRedBlackTree,
-    C_getRedBlackTree,
-    C_removeRedBlackTree,
-    C_clearRedBlackTree,
-    SpawnTask,
-    C_executeWorker,
-    C_taskSendWorker,
-    C_taskReciveWorker,
-    C_shutdownWorker,
-    Twice,
-    StartTime,
-    EndTime,
-    Exit,
-};
+
+#include "c/enumCode.h"
 
 enum Relational {
     EQ,
@@ -134,26 +44,7 @@
     LT,
 };
 
-enum DataType {
-    D_Worker,
-    D_Allocate,
-    D_SingleLinkedStack,
-    D_Stack,
-    D_RedBlackTree,
-    D_Tree,
-    D_Task,
-    D_Traverse,
-    D_RotateTree,
-    D_Node,
-    D_LoopCounter,
-    D_Time,
-    D_Element,
-    D_ActiveQueue,
-    D_WaitQueue,
-    D_SingleLinkedQueue,
-    D_SynchronizedQueue,
-    D_Queue
-};
+#include "c/enumData.h"
 
 struct Context {
     enum Code next;
@@ -312,28 +203,9 @@
     struct OutPutDataSegments {
         union Data **data;
     } ods;
-};
+}; // union Data end
 
-// typedef struct RotateTree D_RotateTree;
-typedef struct Time Time;
-typedef struct LoopCounter LoopCounter;
-typedef struct Worker Worker;
-typedef struct CudaTask CudaTask;
-typedef struct Task Task;
-typedef struct Queue Queue;
-typedef struct SingleLinkedQueue SingleLinkedQueue;
-typedef struct SynchronizedQueue SynchronizedQueue;
-typedef struct Stack Stack;
-typedef struct SingleLinkedStack SingleLinkedStack;
-typedef struct ArrayStack ArrayStack;
-typedef struct Element Element;
-typedef struct Array Array;
-typedef struct RedBlackTree RedBlackTree;
-typedef struct Tree Tree;
-typedef struct RotateTree RotateTree;
-typedef struct Node Node;
-typedef struct Allocate Allocate;
-typedef struct OutputDataSegments OutputDataSegments;
+#include "c/typedefData.h"
 
 union MetaData {
     struct Queue waitMeTasks;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/parallel_execution/generate_context.pl	Thu Dec 15 19:38:18 2016 +0900
@@ -0,0 +1,107 @@
+#!/usr/bin/perl
+
+
+# CodeGear
+# 
+# *.c
+# __code taskManager_stub(struct Context* context) {
+# 
+# context.h
+# C_taskManager,
+# 
+# context.c
+# extern __code taskManager_stub(struct Context*);
+# 
+# context->code[C_taskManager]   = taskManager_stub;
+# 
+# DataGear
+# 
+# context.h
+# struct Worker {
+#         int id;
+#         struct Context* contexts;
+#         enum Code execute;
+#         enum Code taskSend;
+#         enum Code taskRecive;
+#         enum Code shutdown;
+#         struct Queue* tasks;
+#     } Worker;
+# 
+# typedef struct Worker Worker;
+# D_Worker,
+# 
+# context.c
+# ALLOC_DATA(context, Worker);
+
+system "rm -rf c";
+system "mkdir c";
+
+while (<*.c>) {
+    next if (/context.c/);
+    &getStubInfo($_);
+}
+
+&generateContext();
+
+sub getStubInfo {
+    my ($filename) = @_;
+    open my $fd,"<",$filename or die("can't open $filename $!");
+    while (<$fd>) {
+        if (/__code (\w+)_stub\(struct Context\* context\)/) {
+            $codeGear{$1} = $filename;
+        }
+    }
+
+    open my $cx,"<","context.h" or die("can't open context.h $!");
+    while (<$cx>) {
+        if (! $inUnionData) {
+            if ( /^union Data/) {
+                $inUnionData = 1;
+            }
+            next;
+        }
+        last if (/union Data end/);
+        if (/struct (\w+) \{/) {
+            $dataGear{$1} = $1;
+        }
+    }
+}
+
+sub generateContext {
+    open my $fd,">","c/extern.h" or die("can't open c/extern.h $!");
+    for my $code ( sort keys %codeGear ) {
+        print $fd "extern __code ${code}_stub(struct Context*);\n";
+    }
+    print $fd "\n";
+
+    open my $fd,">","c/enumCode.h" or die("can't open c/enumCode.h $!");
+    print $fd "enum Code {\n";
+    for my $code ( sort keys %codeGear ) {
+        print $fd "    C_${code},\n";
+    }
+    print $fd "};\n";
+    
+    open my $fd,">","c/enumCodeInit.c" or die("can't open c/enumCodeInit.c $!");
+    for my $code ( sort keys %codeGear ) {
+        print $fd "    context->code[C_${code}]    = ${code}_stub;\n";
+    }
+
+    open my $fd,">","c/enumData.h" or die("can't open c/enumData.h $!");
+    print $fd "enum DataType {\n";
+    for my $data ( sort keys %dataGear ) {
+        print $fd "    D_${data},\n";
+    }
+    print $fd "};\n\n";
+    
+    open my $fd,">","c/typedefData.h" or die("can't open c/typedefData.h $!");
+    for my $data ( sort keys %dataGear ) {
+        print $fd "typedef struct ${data} ${data};\n";
+    }
+
+    open my $fd,">","c/enumDataInit.c" or die("can't open c/enumDataInit.c $!");
+    for my $data ( sort keys %dataGear ) {
+        print $fd "    ALLOC_DATA(context, ${data});\n";
+    }
+}
+
+# end
--- a/src/parallel_execution/main.c	Thu Dec 15 18:10:48 2016 +0900
+++ b/src/parallel_execution/main.c	Thu Dec 15 19:38:18 2016 +0900
@@ -38,11 +38,11 @@
     /* print_tree(context->data[Tree]->tree.root); */
     /* puts("result"); */
 
-    context->next = CreateWorker;
+    context->next = C_createWorker;
 
     struct Time *t = &context->data[D_Time]->Time;
-    t->next = CreateWorker;
-    goto meta(context, StartTime);
+    t->next = C_createWorker;
+    goto meta(context, C_start_time);
 }
 
 __code code1_stub(struct Context* context) {
@@ -62,7 +62,7 @@
 
     }
 
-    goto meta(context, Exit);
+    goto meta(context, C_code2);
 }
 
 __code code2_stub(struct Context* context) {
@@ -76,7 +76,7 @@
         allocate->size = sizeof(struct Array);
         allocator(context);
 
-        goto meta(context, CreateData2);
+        goto meta(context, C_createData2);
     }
 
     loopCounter->i = 0;
@@ -97,7 +97,7 @@
     node->key = i;
     node->value = (union Data*)array;
 
-    tree->next = CreateTask1;
+    tree->next = C_createTask1;
     tree->node = node;
 
     goto meta(context, loopCounter->tree->put);
@@ -112,15 +112,15 @@
 }
 
 __code createTask1(struct Context* context, struct LoopCounter* loopCounter, struct Task* task, struct Element* element) {
-    task->code = Twice;
+    task->code = C_twice;
     task->idsCount = 0;
 
     element->data = (union Data *)task;
 
-    context->next = CreateData1;
+    context->next = C_createData1;
     loopCounter->i++;
 
-    goto meta(context, SpawnTask);
+    goto meta(context, C_createTask1);
 }
 
 __code createTask1_stub(struct Context* context) {
@@ -189,7 +189,7 @@
         worker_context->thread_num = i;
         loopCounter->i++;
 
-        goto meta(context, CreateWorker);
+        goto meta(context, C_createWorker);
     }
 
     loopCounter->i = 0;
@@ -256,3 +256,4 @@
 
     goto start_code(main_context);
 }
+