changeset 132:7c309e1aea73

Code Gears stack api
author one
date Thu, 27 Oct 2016 18:54:11 +0900
parents a4507906938c
children 568730b1239e
files src/parallel_execution/context.c src/parallel_execution/context.h src/parallel_execution/main.c src/parallel_execution/rb_tree.c src/parallel_execution/stack.c src/parallel_execution/stack.h
diffstat 6 files changed, 75 insertions(+), 69 deletions(-) [+]
line wrap: on
line diff
--- a/src/parallel_execution/context.c	Tue Oct 25 00:49:28 2016 +0900
+++ b/src/parallel_execution/context.c	Thu Oct 27 18:54:11 2016 +0900
@@ -1,6 +1,7 @@
 #include <stdlib.h>
 
 #include "context.h"
+#include "stack.h"
 
 extern __code code1_stub(struct Context*);
 extern __code code2_stub(struct Context*);
@@ -147,20 +148,13 @@
     allocate->size = 0;
 
     
-    struct SingleLinkedStack* singleLinkedStack = ALLOC_DATA(context, SingleLinkedStack);
-    singleLinkedStack->top = NULL;
-    singleLinkedStack->i.push = PushSingleLinkedStack;
-    singleLinkedStack->i.pop = PopSingleLinkedStack;
-    singleLinkedStack->i.isEmpty = NULL;
-    singleLinkedStack->i.stack = (union StackSelf*)singleLinkedStack;
-
-    context->data[Stack] = (union Data*)&singleLinkedStack->i;
+    ALLOC_DATA(context, Stack);
 
     struct Tree* tree = ALLOC_DATA(context, Tree);
     tree->root = 0;
 
     struct Traverse* traverse = ALLOC_DATA(context, Traverse);
-    traverse->nodeStack = NULL;
+    traverse->nodeStack = &createSingleLinkedStack(context)->stack;
 
     struct Node* node = ALLOC_DATA(context, Node);
     node->key = 0;
--- a/src/parallel_execution/context.h	Tue Oct 25 00:49:28 2016 +0900
+++ b/src/parallel_execution/context.h	Thu Oct 27 18:54:11 2016 +0900
@@ -1,9 +1,10 @@
 /* Context definition for llrb example */
+#ifndef CONTEXT_H
+#define CONTEXT_H
 #include <pthread.h>
 #ifdef USE_CUDA
 #include <cuda.h>
 #endif
-#include "stack.h"
 
 #define ALLOCATE_SIZE 20000000
 #define NEW(type) (type*)(calloc(1, sizeof(type)))
@@ -13,6 +14,18 @@
 
 #define ALLOC_DATA_TYPE(context, dseg, type) ({ context->data[dseg] = context->heap; context->heap += sizeof(struct type); (struct type *)context->data[dseg]; })
 
+#define ALLOCATE(context, t) ({ \
+  union Data* data = context->heap; \
+  context->heap += sizeof(struct t) + ((void *)(&data->element) - (void *)data) ; \
+  data->type = t; \
+  data; })
+
+#define GET_DATA(spesificData) ({ \
+  union Data dummy; \
+  void* ptr = (void *)spesificData; \
+  ptr -= (void *)(&dummy.element) - (void *)(&dummy); \
+  (union Data*)ptr; })
+
 enum Code {
     Code1,
     Code2,
@@ -163,25 +176,25 @@
         struct QueueInterface* i;
         enum Code next;
     } queue;
+    // Stack Interface
     struct Stack {
-        union StackSelf* stack;
+        union Data* stack;
         union Data* data;
         enum Code push;
         enum Code pop;
         enum Code isEmpty;
         enum Code next;
     } stack;
-    union StackSelf {
-        struct SingleLinkedStack {
-            struct Stack i;
-            struct Element* top;
-        } singleLinekedStack;
-        struct ArrayStack {
-            int size;
-            int limit;
-            struct Element* array;
-        } arrayStack;
-    };
+    // Stack implementations
+    struct SingleLinkedStack {
+        struct Element* top;
+    } singleLinekedStack;
+    struct ArrayStack {
+        int size;
+        int limit;
+        struct Element* array;
+    } arrayStack;
+    // Stack implementation end
     struct Element {
         union Data* data;
         struct Element* next;
@@ -199,7 +212,7 @@
         enum Code rotateNext;
         struct Node* current; // reading node of original tree
         struct Node* newNode; // writing node of new tree
-        struct Element* nodeStack;
+        struct Stack* nodeStack;
         int result;
     } traverse;
     struct Node {
@@ -226,4 +239,4 @@
     struct Queue waitMeTasks;
     struct Queue waitI;
 };
-
+#endif
--- a/src/parallel_execution/main.c	Tue Oct 25 00:49:28 2016 +0900
+++ b/src/parallel_execution/main.c	Thu Oct 27 18:54:11 2016 +0900
@@ -1,5 +1,6 @@
 #include <stdio.h>
 #include <string.h>
+#include <stdlib.h>
 
 #include "context.h"
 #include "origin_cs.h"
--- a/src/parallel_execution/rb_tree.c	Tue Oct 25 00:49:28 2016 +0900
+++ b/src/parallel_execution/rb_tree.c	Thu Oct 27 18:54:11 2016 +0900
@@ -7,14 +7,14 @@
 extern enum Relational compare(struct Node* node1, struct Node* node2);
 
 void printTree1(union Data* data) {
-    struct Node* node = (struct Node*)data;
+    struct Node* node = &data->node;
     if (node == NULL) {
         printf("NULL");
     } else {
         printf("key = %d (", node->key);
-        printTree1((union Data*)node->right);
+        printTree1(GET_DATA(node->right));
         printf("), (");
-        printTree1((union Data*)node->left);
+        printTree1(GET_DATA(node->left));
         printf(")");
     }
 }
@@ -25,19 +25,15 @@
 }
 
 __code put(struct Context* context, struct Stack* nodeStack,  struct Tree* tree, struct Node* node, struct Traverse* traverse, struct Node* root, struct Node* newNode) {
-    printTree((union Data*)tree->root);
+    printTree(GET_DATA(tree->root));
     traverse->newNode = newNode;
     tree->root = newNode; // this should done at stackClear
     if (root) {
         traverse->current = root;
-        // traverse->result=compare(...)
-        // traverse->result = traverse->compare(traverse->current, node);
         traverse->result = compare(traverse->current, node);
-        nodeStack->stack = traverse->nodeStack;
-        nodeStack->data = (union Data*)newNode;
+        nodeStack->data = GET_DATA(newNode);
         nodeStack->next = Replace1;
         goto meta(context, nodeStack->push);
-        // goto traverse->nodeStack->push(newNode, replaceNode1, traverse, node);
     }
 
     goto meta(context, Insert);
@@ -47,9 +43,8 @@
     struct Allocate* allocate = &context->data[Allocate]->allocate;
     allocate->size = sizeof(struct Node);
     allocator(context);
-    
     goto put(context,
-             &context->data[Stack]->stack,
+             context->data[Traverse]->traverse.nodeStack,
              &context->data[Tree]->tree,
              &context->data[Node]->node,
              &context->data[Traverse]->traverse,
@@ -58,25 +53,19 @@
              );
 }
 
-__code replaceNode(struct Context* context, struct Traverse* traverse, struct Node* oldNode, struct Node* newNode, struct Element* element) {
+__code replaceNode(struct Context* context, struct Traverse* traverse, struct Node* oldNode, struct Node* newNode, struct Stack* nodeStack) {
     *newNode = *oldNode;
-    element->next = traverse->nodeStack;
-    element->data = (union Data* )newNode;
-    traverse->nodeStack = element;
-    // goto replaceNode1(struct Traverse* traverse, struct Node* node, struct Node* oldNode, struct Node* newNode, +struct Node* newnewNode, int result)
-    goto meta(context, Replace1);
+    nodeStack->data = GET_DATA(newNode);
+    nodeStack->next = Replace1;
+    goto meta(context, nodeStack->push);
 }
 
 __code replaceNode_stub(struct Context* context) {
-    struct Allocate* allocate = &context->data[Allocate]->allocate;
-    allocate->size = sizeof(struct Element);
-    allocator(context);
-    struct Element* element = &context->data[context->dataNum]->element;
     goto replaceNode(context,
                   &context->data[Traverse]->traverse,
                   context->data[Traverse]->traverse.current,
                   context->data[Traverse]->traverse.newNode,
-                  element);
+                  context->data[Traverse]->traverse.nodeStack);
 }
 
 __code replaceNode1(struct Context* context, struct Traverse* traverse, struct Node* node, struct Node* oldNode, struct Node* newNode, struct Node* newnewNode, int result) {
@@ -93,7 +82,6 @@
     }
     traverse->newNode = newnewNode;
     if (traverse->current) {
-        // compare(context, traverse, traverse->current->key, node->key);
         traverse->result = compare(traverse->current, node);
         goto meta(context, Replace);
     }
@@ -103,10 +91,7 @@
 }
 
 __code replaceNode1_stub(struct Context* context) {
-    struct Allocate* allocate = &context->data[Allocate]->allocate;
-    allocate->size = sizeof(struct Node);
-    allocator(context);
-    struct Node* newnewNode = &context->data[context->dataNum]->node;
+    struct Node* newnewNode = &ALLOCATE(context, Node)->node;
     goto replaceNode1(context,
                      &context->data[Traverse]->traverse,
                      &context->data[Node]->node,
@@ -130,8 +115,8 @@
                     context->data[Traverse]->traverse.newNode);
 }
 
-__code insertCase1(struct Context* context, struct Tree* tree,struct Element* nodeStack) {
-    if (nodeStack!=NULL) {
+__code insertCase1(struct Context* context, struct Tree* tree, struct Stack* nodeStack) {
+    if (nodeStack != NULL) {
         goto meta(context, InsertCase2);
     }
     tree->root->color = Black;
--- a/src/parallel_execution/stack.c	Tue Oct 25 00:49:28 2016 +0900
+++ b/src/parallel_execution/stack.c	Thu Oct 27 18:54:11 2016 +0900
@@ -1,17 +1,16 @@
 #include "stack.h"
 #include "context.h"
 #include "origin_cs.h"
-extern union Data* allocator(struct Context* context);
 
-__code createSingleLinkedStack(struct Context* context, enum Code next) {
-    /*
-    struct SingleLinkedStack* stack = allocate(context, SignleLinkedStack);
-    stack->top = NULL;
-    stack->i.push = PushSingleLinkedStack;
-    stack->i.pop = PopsingleLinkedStack;
-    stack->i.isEmpty = _;
-    */
-    goto meta(context, next);
+union Data* createSingleLinkedStack(struct Context* context) {
+    struct Stack* stack = ALLOCATE(context, Stack);
+    struct SingleLinkedStack* singleLinkedStack = ALLOCATE(context, SignleLinkedStack);
+    stack->stack = singleLinkedStack;
+    singleLinkedStack->top = NULL;
+    stack->push = PushSingleLinkedStack;
+    stack->pop  = PopSingleLinkedStack;
+    stack->isEmpty = _;
+    return GET_DATA(stack);
 }
 
 __code pushSingleLinkedStack(struct Context* context, struct SingleLinkedStack* stack, struct Element* element, union Data* data, enum Code next) {
@@ -22,23 +21,36 @@
 }
 
 __code pushSingleLinkedStack_stub(struct Context* context) {
-    struct Allocate* allocate = &context->data[Allocate]->allocate;
-    allocate->size = sizeof(struct Element);
-    struct Element* element = &(allocator(context)->element);
+    struct Element* element = ALLOCATE(context, Element);
     goto pushSingleLinkedStack(context,
-                               &context->data[Stack]->stack.stack,
+                               (struct SignleLinkedStack *)context->data[Stack]->stack.stack,
                                element,
                                context->data[Stack]->stack.data,
                                context->data[Stack]->stack.next);
 }
 
-__code popSingleLinkedStack(struct Context* context, struct SingleLinkedStack* stack, enum Code next) {
+__code popSingleLinkedStack(struct Context* context, struct SingleLinkedStack* stack, union Data** data, enum Code next) {
+    *data = stack->top;
     stack->top = stack->top->next;
     goto meta(context, next);
 }
 
 __code popSingleLinkedStack_stub(struct Context* context) {
     goto popSingleLinkedStack(context,
-                               &context->data[Stack]->stack.stack,
+                               (struct SignleLinkedStack *)context->data[Stack]->stack.stack,
+                               &context->data[Stack]->stack.data,
                                context->data[Stack]->stack.next);
 }
+
+__code isEmptySingleLinkedStack(struct Context* context, struct SingleLinkedStack* stack, union Data** data, enum Code next) {
+    *data = stack->top;
+    goto meta(context, next);
+}
+
+__code isEmptySingleLinkedStack_stub(struct Context* context) {
+    goto isEmptySingleLinkedStack(context,
+                               (struct SignleLinkedStack *)context->data[Stack]->stack.stack,
+                               &context->data[Stack]->stack.data,
+                               context->data[Stack]->stack.next);
+}
+
--- a/src/parallel_execution/stack.h	Tue Oct 25 00:49:28 2016 +0900
+++ b/src/parallel_execution/stack.h	Thu Oct 27 18:54:11 2016 +0900
@@ -0,0 +1,1 @@
+extern union Data* createSingleLinkedStack(struct Context* context);