changeset 101:8987cf13d5bb

Add Allocate macro
author innparusu
date Wed, 02 Mar 2016 19:23:00 +0900
parents 3d7ecced7e14
children 64c98838a291
files src/CMakeLists.txt src/parallel_execution/CMakeLists.txt src/parallel_execution/context.c src/parallel_execution/context.h src/parallel_execution/main.c src/parallel_execution/worker.c
diffstat 6 files changed, 37 insertions(+), 47 deletions(-) [+]
line wrap: on
line diff
--- a/src/CMakeLists.txt	Tue Feb 02 16:12:34 2016 +0900
+++ b/src/CMakeLists.txt	Wed Mar 02 19:23:00 2016 +0900
@@ -4,7 +4,7 @@
 set(CMAKE_VERBOSE_MAKEFILE 1)
 
 # set compiler
-set(CMAKE_C_COMPILER $ENV{CbC_Clang}/clang)
+set(CMAKE_C_COMPILER cbclang)
 
 # compile option
 add_definitions("-Wall -g -O0")
--- a/src/parallel_execution/CMakeLists.txt	Tue Feb 02 16:12:34 2016 +0900
+++ b/src/parallel_execution/CMakeLists.txt	Wed Mar 02 19:23:00 2016 +0900
@@ -1,8 +1,9 @@
 cmake_minimum_required(VERSION 2.8)
 
+# -DUSE_CUDA
 add_definitions("-Wall -g -O0")
 
-set(CMAKE_C_COMPILER cbc-clang)
+set(CMAKE_C_COMPILER cbclang)
 
 add_executable(twice
                main.c
--- a/src/parallel_execution/context.c	Tue Feb 02 16:12:34 2016 +0900
+++ b/src/parallel_execution/context.c	Wed Mar 02 19:23:00 2016 +0900
@@ -61,9 +61,10 @@
 
 __code initContext(struct Context* context) {
     context->heapLimit = sizeof(union Data)*ALLOCATE_SIZE;
-    context->code = malloc(sizeof(__code*)*ALLOCATE_SIZE);
-    context->data = malloc(sizeof(union Data*)*ALLOCATE_SIZE);
-    context->heapStart = malloc(context->heapLimit);
+    context->code = (__code(**) (struct Context*)) NEWN(ALLOCATE_SIZE, void*);
+    context->data = NEWN(ALLOCATE_SIZE, union Data*);
+    context->heapStart = NEWN(context->heapLimit, char);
+    context->heap = context->heapStart;
 
     context->codeNum = Exit;
 
@@ -119,65 +120,40 @@
     context->code[StartTime]     = start_time_stub;
     context->code[EndTime]     = end_time_stub;
     context->code[Exit]       = exit_code;
-    
-    context->heap = context->heapStart;
 
-    context->data[Worker] = context->heap;
-    context->heap += sizeof(struct Worker);
-
-    context->data[Allocate] = context->heap;
-    context->heap += sizeof(struct Allocate);
-
-    context->data[Tree] = context->heap;
-    context->heap += sizeof(struct Tree);
-
-    context->data[Traverse] = context->heap;
-    context->heap += sizeof(struct Traverse);
-
-    context->data[Node] = context->heap;
-    context->heap += sizeof(struct Node);
-    
-    context->data[LoopCounter] = context->heap;
-    context->heap += sizeof(struct LoopCounter);
-
-    context->data[Element] = context->heap;
-    context->heap += sizeof(struct Element);
-
-    context->data[Time] = context->heap;
-    context->heap += sizeof(struct Time);
-
-    context->data[ActiveQueue] = context->heap;
-    context->heap += sizeof(struct Queue);
-
-    context->dataNum = ActiveQueue;
-    
-    struct Worker* worker = &context->data[Worker]->worker;
+    struct Worker* worker = ALLOC_DATA(context, Worker);
     worker->num = 0;
     worker->contexts = 0;
 
-    struct Allocate* allocate = &context->data[Allocate]->allocate;
+    struct Allocate* allocate = ALLOC_DATA(context, Allocate);
     allocate->size = 0;
 
-    struct Tree* tree = &context->data[Tree]->tree;
+    struct Tree* tree = ALLOC_DATA(context, Tree);
     tree->root = 0;
 
-    struct Node* node = &context->data[Node]->node;
+    ALLOC_DATA(context, Traverse);
+
+    struct Node* node = ALLOC_DATA(context, Node);
     node->key = 0;
     node->value = 0;
     node->left = 0;
     node->right = 0;
-
-    struct LoopCounter* counter = &context->data[LoopCounter]->loopCounter;
+    
+    struct LoopCounter* counter = ALLOC_DATA(context, LoopCounter);
     counter->i = 0;
 
-    struct Element* element = &context->data[Element]->element;
+    struct Element* element = ALLOC_DATA(context, Element);
     element->task = 0;
     element->next = 0;
 
-    struct Queue* activeQueue = &context->data[ActiveQueue]->queue;
+    ALLOC_DATA(context, Time);
+
+    struct Queue* activeQueue = ALLOC_DATA_TYPE(context, ActiveQueue, Queue);
     activeQueue->first = 0;
     activeQueue->last = 0;
     activeQueue->count = 0;
+
+    context->dataNum = ActiveQueue;
     
     context->node_stack = stack_init(sizeof(struct Node*), 100);
     context->code_stack = stack_init(sizeof(enum Code), 100);
--- a/src/parallel_execution/context.h	Tue Feb 02 16:12:34 2016 +0900
+++ b/src/parallel_execution/context.h	Wed Mar 02 19:23:00 2016 +0900
@@ -1,9 +1,17 @@
 /* Context definition for llrb example */
 #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)))
+#define NEWN(n, type) (type*)(calloc(n, sizeof(type)))
+
+#define ALLOC_DATA(context, dseg) ({ context->data[dseg] = context->heap; context->heap += sizeof(struct dseg); (struct dseg *)context->data[dseg]; })
+
+#define ALLOC_DATA_TYPE(context, dseg, type) ({ context->data[dseg] = context->heap; context->heap += sizeof(struct type); (struct type *)context->data[dseg]; })
 
 enum Code {
     Code1,
@@ -107,6 +115,7 @@
         int num;
         struct Context* contexts;
     } worker;
+#ifdef USE_CUDA
     struct CudaTask {
         CUdevice device;
         CUcontext cuCtx;
@@ -114,6 +123,7 @@
         CUdeviceptr* deviceptr;
         CUstream stream;
     } cudatask;
+#endif
     struct Task {
         enum Code code;
         int key;
--- a/src/parallel_execution/main.c	Tue Feb 02 16:12:34 2016 +0900
+++ b/src/parallel_execution/main.c	Wed Mar 02 19:23:00 2016 +0900
@@ -258,16 +258,16 @@
 int main(int argc, char** argv) {
     init(argc, argv);
 
-    array_ptr = (int*)malloc(sizeof(int)*length);
+    array_ptr = NEWN(length, int);
 
     for(int i=0; i<length; i++)
         array_ptr[i]=i;
 
-    struct Context* main_context = (struct Context*)malloc(sizeof(struct Context));
+    struct Context* main_context = NEW(struct Context);
     initContext(main_context);
     main_context->next = CreateData1;
 
-    struct Context* worker_contexts = (struct Context*)malloc(sizeof(struct Context)*cpu_num);
+    struct Context* worker_contexts = NEWN(cpu_num, struct Context);
     
     struct Worker* worker = &main_context->data[Worker]->worker;
     worker->num = cpu_num;
--- a/src/parallel_execution/worker.c	Tue Feb 02 16:12:34 2016 +0900
+++ b/src/parallel_execution/worker.c	Wed Mar 02 19:23:00 2016 +0900
@@ -27,8 +27,11 @@
 __code getQueue_stub(struct Context* context) {
     goto getQueue(context, &context->data[ActiveQueue]->queue, &context->data[Node]->node);
 }
+
+#ifdef USE_CUDA
 __code twiceGpu(struct Context* context) {
     cuMemcpyHtoDAsync(context,context,context,context->stream);
     cuLaunchkanel();
     cuMemcpyDtoHAsync();
 }
+#endif