changeset 575:3e3158198cb5

bug fix...
author anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
date Mon, 25 Nov 2019 17:03:33 +0900
parents 7d66643d837d
children c86da02a73bf
files src/parallel_execution/SingleLinkedQueue.cbc src/parallel_execution/Timer.h src/parallel_execution/Worker.h src/parallel_execution/auto_generate_context.pl src/parallel_execution/lib/Gears/Context.pm src/parallel_execution/lib/Gears/Util.pm src/parallel_execution/plautogen/interface/Meta.h src/parallel_execution/plautogen/interface/Node.h src/parallel_execution/tmp_tool/_orig_context.h src/parallel_execution/update_context.pl
diffstat 10 files changed, 520 insertions(+), 31 deletions(-) [+]
line wrap: on
line diff
--- a/src/parallel_execution/SingleLinkedQueue.cbc	Mon Nov 25 14:32:34 2019 +0900
+++ b/src/parallel_execution/SingleLinkedQueue.cbc	Mon Nov 25 17:03:33 2019 +0900
@@ -1,6 +1,7 @@
 #include "../context.h"
 #include <stdio.h>
 #interface "Queue.h"
+// use "Node.h"
 
 Queue* createSingleLinkedQueue(struct Context* context) {
     struct Queue* queue = new Queue();
--- a/src/parallel_execution/Timer.h	Mon Nov 25 14:32:34 2019 +0900
+++ b/src/parallel_execution/Timer.h	Mon Nov 25 17:03:33 2019 +0900
@@ -3,5 +3,5 @@
         __code start(Impl* timer, __code next(...));
         __code end(Impl* timer, __code next(...));
         __code next(...);
-} Queue;
+} Timer;
 
--- a/src/parallel_execution/Worker.h	Mon Nov 25 14:32:34 2019 +0900
+++ b/src/parallel_execution/Worker.h	Mon Nov 25 17:03:33 2019 +0900
@@ -2,6 +2,8 @@
     union Data* worker;
     struct Queue* tasks;
     struct Context* task;
+    pthread_t thread;
+    struct TaskManager* taskManager;
     __code taskReceive(Impl* worker, struct Queue* tasks);
     __code shutdown(Impl* worker);
     __code next(...);
--- a/src/parallel_execution/auto_generate_context.pl	Mon Nov 25 14:32:34 2019 +0900
+++ b/src/parallel_execution/auto_generate_context.pl	Mon Nov 25 17:03:33 2019 +0900
@@ -1,20 +1,26 @@
 #!/usr/bin/env perl
 use strict;
 use warnings;
+use FindBin;
+use lib "$FindBin::Bin/lib";
+
 use Gears::Context;
-use FindBin;
-#use DDP {deparse => 1};
-
+use Getopt::Std;
+my %opt;
+getopts("w" => \%opt);
 my @cbc_files = <DATA>;
 chomp @cbc_files;
 
-my $gears = Gears::Context->new(compile_sources => \@cbc_files, find_root => $FindBin::Bin);
-my $data_gears = $gears->extraction_dg_compile_sources();
-my $g = $gears->set_data_gear_header_path();
 
-my $dg2path = $gears->update_dg_each_header_path($data_gears,$g);
+my $output     = $opt{w} ? "$FindBin::Bin/context.h" : "stdout";
+my $gears      = Gears::Context->new(compile_sources => \@cbc_files, find_root => $FindBin::Bin, output => $output);
+my $data_gears = $gears->extraction_dg_compile_sources();
+my $g          = $gears->set_data_gear_header_path();
 
-$gears->tans2create_context_h($dg2path);
+my $dg2path    = $gears->update_dg_each_header_path($data_gears,$g);
+
+my $tree = $gears->createImplTree_from_header($dg2path);
+$gears->tree2create_context_h($tree);
 
 #
 __DATA__
--- a/src/parallel_execution/lib/Gears/Context.pm	Mon Nov 25 14:32:34 2019 +0900
+++ b/src/parallel_execution/lib/Gears/Context.pm	Mon Nov 25 17:03:33 2019 +0900
@@ -12,13 +12,14 @@
   my $self = {
     data_gears_with_count => {},
     find_root => $args{find_root} // ".",
-    output => $args{output} // *STDOUT,
+    output   => $args{output},
   };
 
   if ($args{compile_sources}) {
     $self->{compile_sources} = $args{compile_sources};
     map { Gears::Util->file_checking($_); } @{$self->{compile_sources}};
   }
+
   return bless $self, $class;
 }
 
@@ -29,7 +30,7 @@
   for my $cbc_file (@{$self->{compile_sources}}) {
     open my $fh , '<', $cbc_file;
     while (my $line = <$fh>) {
-       if ($line =~ /#interface\s*"(.*)\.h"/) {
+       if ($line =~ /#interface\s*"(.*)\.h"/ || $line =~ /^\/\/\s*use\s*"(.*)\.h"/) {
           $self->{data_gears_with_count}->{$1}->{caller}->{$cbc_file}++;
           $counter{interfaces}->{$1}++;
           next;
@@ -46,6 +47,8 @@
     }
     close $fh;
   }
+  $counter{interfaces}->{Meta}++;
+  $self->{data_gears_with_count}->{Meta}++;
   return \%counter;
 }
 
@@ -75,17 +78,29 @@
   return $new_dgs;
 }
 
-sub tans2create_context_h {
+sub tree2create_context_h {
   my ($self, $dg2path) = @_;
-  my $data_struct_str = $self->trans2data_struct_str($dg2path);
-  Gears::Context::Template->emit_top_header($self->{output});
-  Gears::Context::Template->emit_data_gears($self->{output},$data_struct_str);
-  Gears::Context::Template->emit_last_header($self->{output});
+  my $data_struct_str = $self->tree2data_struct_str($dg2path);
+
+  my $output = $self->_select_output();
+  Gears::Context::Template->emit_top_header($output);
+  Gears::Context::Template->emit_data_gears($output,$data_struct_str);
+  Gears::Context::Template->emit_last_header($output);
+  close $output;
 }
 
-sub trans2data_struct_str {
-  my ($self, $dg2path) = @_;
-  my $dg_str = $self->_createImplTree_from_header($dg2path);
+sub _select_output {
+  my $self = shift;
+  print "$self->{output}\n";
+  if ($self->{output} eq  'stdout') {
+    return *STDOUT;
+  }
+  open my $fh, '>', $self->{output};
+  return $fh;
+}
+
+sub tree2data_struct_str {
+  my ($self, $dg_str) = @_;
   my $data_struct_str  = "";
   for my $interface (sort keys %$dg_str) {
     $data_struct_str .= Gears::Util->h2context_str($dg_str->{$interface}->{elem});
@@ -97,7 +112,7 @@
   return $data_struct_str;
 }
 
-sub _createImplTree_from_header {
+sub createImplTree_from_header {
   my ($self, $dg2path) = @_;
   my %dg_str = ();
 
--- a/src/parallel_execution/lib/Gears/Util.pm	Mon Nov 25 14:32:34 2019 +0900
+++ b/src/parallel_execution/lib/Gears/Util.pm	Mon Nov 25 17:03:33 2019 +0900
@@ -63,7 +63,8 @@
       next;
     }
     next if ($line =~ /^\s+$/);
-    next if ($line =~ m[//|}]);
+    next if ($line =~ m[^//]);
+    next if ($line =~ m[^\}\s*$ir->{name};]);
 
     if ($line =~ m|__code (\w+)\(([()\.\*\s\w,_]+)\)|) {
       unless ($code_verbose) {
@@ -75,7 +76,8 @@
     }
 
     $line =~ s/\s*([\w\s\*]+);\s*/$1/;
-    push(@{$ir->{data}},$1);
+    chomp $line;
+    push(@{$ir->{data}},$line);
   }
 
   return $ir;
@@ -134,7 +136,15 @@
   $context =  "${space}struct $h2context->{name} {\n";
   if (exists $h2context->{data}) {
     for my $datum (@{$h2context->{data}}) {
-      $context .= "${space}${space}$datum;\n";
+      my $dd = $datum;
+      unless (($dd =~ /[{,.]$/) || ($dd =~ m{^\s+(//|/*)})) {
+        $dd =~ s{([\w\*\s]+)+((//|/\*)?.*)}{$1;$2};
+      }
+      if ($dd =~ /^\s*\}\s*\w+\s*$/ ) {
+        $dd .= ";";
+      }
+      $context .= "${space}${space}$dd";
+      $context .= "\n";
     }
   }
   if (exists $h2context->{codes}) {
--- a/src/parallel_execution/plautogen/interface/Meta.h	Mon Nov 25 14:32:34 2019 +0900
+++ b/src/parallel_execution/plautogen/interface/Meta.h	Mon Nov 25 17:03:33 2019 +0900
@@ -2,5 +2,5 @@
   enum DataType type;
   long size;
   long len;
-  struct Queue* wait // tasks waiting this dataGear;
+  struct Queue* wait; // tasks waiting this dataGear;
 } Meta;
--- a/src/parallel_execution/plautogen/interface/Node.h	Mon Nov 25 14:32:34 2019 +0900
+++ b/src/parallel_execution/plautogen/interface/Node.h	Mon Nov 25 17:03:33 2019 +0900
@@ -1,12 +1,12 @@
 typedef struct Node <Type, Impl> {
-  int key // comparable data segment;
+  int key; // comparable data segment;
   union Data* value;
   struct Node* left;
   struct Node* right;
-          // need to balancing;
-          enum Color {;
-              Red,;
-              Black,;
-              // Red eq 0,Black eq 1. enum name convert intager.;
-          }color;
+          // need to balancing
+  enum Color {
+      Red,
+      Black,
+      // Red eq 0,Black eq 1. enum name convert intager.
+  }color;
 } Node;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/parallel_execution/tmp_tool/_orig_context.h	Mon Nov 25 17:03:33 2019 +0900
@@ -0,0 +1,454 @@
+/* Context definition for llrb example */
+#ifndef CONTEXT_H
+#define CONTEXT_H
+#include <stdlib.h>
+#include <pthread.h>
+#ifdef USE_CUDAWorker
+#include <cuda.h>
+#include <driver_types.h>
+#include <cuda_runtime.h>
+#include "helper_cuda.h"
+#endif
+
+#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) ({\
+    Meta* meta = (Meta*)context->heap;\
+    meta->type = D_##dseg;\
+    meta->size = sizeof(dseg);\
+    meta->len = 1;\
+    context->heap += sizeof(Meta);\
+    context->data[D_##dseg] = context->heap; context->heap += sizeof(dseg); (dseg *)context->data[D_##dseg]; })
+
+#define ALLOC_DATA_TYPE(context, dseg, t) ({\
+    Meta* meta = (Meta*)context->heap;\
+    meta->type = D_##t;\
+    meta->size = sizeof(t);\
+    meta->len = 1;\
+    context->heap += sizeof(Meta);\
+    context->data[D_##dseg] = context->heap; context->heap += sizeof(t); (t *)context->data[D_##dseg]; })
+
+#define ALLOCATE(context, t) ({ \
+    Meta* meta = (Meta*)context->heap;\
+    context->heap += sizeof(Meta);\
+    union Data* data = context->heap; \
+    context->heap += sizeof(t); \
+    meta->type = D_##t; \
+    meta->size = sizeof(t);     \
+    meta->len = 1;\
+    data; })
+
+#define ALLOCATE_ARRAY(context, t, length) ({ \
+    Meta* meta = (Meta*)context->heap;\
+    context->heap += sizeof(Meta);\
+    union Data* data = context->heap; \
+    context->heap += sizeof(t)*length; \
+    meta->type = D_##t; \
+    meta->size = sizeof(t)*length; \
+    meta->len = length; \
+    data;   })
+
+#define ALLOCATE_PTR_ARRAY(context, dseg, length) ({\
+    Meta* meta = (Meta*)context->heap;\
+    context->heap += sizeof(Meta);\
+    union Data* data = context->heap; \
+    context->heap += sizeof(dseg *)*length; \
+    meta->type = D_##dseg; \
+    meta->size = sizeof(dseg *)*length; \
+    meta->len = length; \
+    data; })
+
+#define ALLOCATE_DATA_GEAR(context, t) ({ \
+        union Data* data = ALLOCATE(context, t); \
+        Meta* meta = GET_META(data); \
+        meta->wait = createSynchronizedQueue(context); \
+        data; })
+
+#define ALLOC(context, t) (&ALLOCATE(context, t)->t)
+
+#define GET_META(dseg) ((Meta*)(((void*)dseg) - sizeof(Meta)))
+#define GET_TYPE(dseg) (GET_META(dseg)->type)
+#define GET_SIZE(dseg) (GET_META(dseg)->size)
+#define GET_LEN(dseg) (GET_META(dseg)->len)
+#define GET_WAIT_LIST(dseg) (GET_META(dseg)->wait)
+
+#define Gearef(context, t) (&(context)->data[D_##t]->t)
+
+// (SingleLinkedStack *)context->data[D_Stack]->Stack.stack->Stack.stack
+
+#define GearImpl(context, intf, name) (Gearef(context, intf)->name->intf.name)
+
+#include "c/enumCode.h"
+
+enum Relational {
+    EQ,
+    GT,
+    LT,
+};
+
+#include "c/enumData.h"
+
+struct Context {
+    enum Code next;
+    struct Worker* worker;
+    struct TaskManager* taskManager;
+    int codeNum;
+    __code (**code) (struct Context*);
+    union Data **data;
+    void* heapStart;
+    void* heap;
+    long heapLimit;
+    int dataNum;
+
+    // task parameter
+    int idgCount; //number of waiting dataGear
+    int idg;
+    int maxIdg;
+    int odg;
+    int maxOdg;
+    int gpu; // GPU task
+    struct Context* task;
+    struct Element* taskList;
+#ifdef USE_CUDAWorker
+    int num_exec;
+    CUmodule module;
+    CUfunction function;
+#endif
+    /* multi dimension parameter */
+    int iterate;
+    struct Iterator* iterator;
+    enum Code before;
+};
+
+typedef int Int;
+#ifndef USE_CUDAWorker
+typedef unsigned long long CUdeviceptr;
+#endif
+union Data {
+    struct Meta {
+        enum DataType type;
+        long size;
+        long len;
+        struct Queue* wait; // tasks waiting this dataGear
+    } Meta;
+    struct Context Context;
+    struct Timer {
+        union Data* timer;
+        enum Code start;
+        enum Code end;
+        enum Code next;
+    } Timer;
+    struct TimerImpl {
+        double time;
+    } TimerImpl;
+    struct LoopCounter {
+        int i;
+    } LoopCounter;
+    struct TaskManager {
+        union Data* taskManager;
+        enum Code spawn;      // start NEW context on the worker
+        enum Code spawnTasks; // start NEW tasks on the worker
+        enum Code shutdown;
+        enum Code incrementTaskCount;
+        enum Code decrementTaskCount;
+        enum Code next;
+        enum Code next1;
+        enum Code setWaitTask;
+        struct Context* task;
+        struct Element* taskList;
+        union Data* data;
+    } TaskManager;
+    struct TaskManagerImpl {
+        enum Code next;
+        int numWorker;
+        int sendCPUWorkerIndex;
+        int sendGPUWorkerIndex;
+        int taskCount;
+        pthread_mutex_t mutex;
+        struct Queue* activeQueue;
+        struct Worker** workers;
+        struct Element* taskList;
+        int loopCounter;
+        int cpu;
+        int gpu;
+        int io;
+        int maxCPU;
+    } TaskManagerImpl;
+    struct Worker {
+        union Data* worker;
+        enum Code taskReceive;
+        enum Code shutdown;
+        enum Code next;
+        struct Queue* tasks;
+        pthread_t thread;
+        struct TaskManager* taskManager;
+        struct Context* task;
+    } Worker;
+    struct CPUWorker {
+        pthread_mutex_t mutex;
+        pthread_cond_t cond;
+        struct Context* context;
+        int id;
+        int loopCounter;
+    } CPUWorker;
+#ifdef USE_CUDAWorker
+    struct CUDAWorker {
+        CUdevice device;
+        CUcontext cuCtx;
+        struct Context* context;
+        int id;
+        int loopCounter;
+        int deviceNum;
+        struct Queue* tasks;
+        int runFlag;
+        enum Code next;
+        int numStream;
+        struct Executor* executor;
+        CUstream *stream;
+    } CUDAWorker;
+#else
+    struct CUDAWorker {
+    } CUDAWorker;
+#endif
+    struct Main {
+        enum Code code;
+        enum Code next;
+        struct Queue* args;
+    } Main;
+    // Queue Interface
+    struct Queue {
+        union Data* queue;
+        union Data* data;
+        enum Code whenEmpty;
+        enum Code clear;
+        enum Code put;
+        enum Code take;
+        enum Code isEmpty;
+        enum Code next;
+    } Queue;
+    struct SingleLinkedQueue {
+        struct Element* top;
+        struct Element* last;
+    } SingleLinkedQueue;
+    struct SynchronizedQueue {
+        struct Element* top;
+        struct Element* last;
+        struct Atomic* atomic;
+    } SynchronizedQueue;
+    // Stack Interface
+    struct Stack {
+        union Data* stack;
+        union Data* data;
+        union Data* data1;
+        enum Code whenEmpty;
+        enum Code clear;
+        enum Code push;
+        enum Code pop;
+        enum Code pop2;
+        enum Code isEmpty;
+        enum Code get;
+        enum Code get2;
+        enum Code next;
+    } Stack;
+    // Stack implementations
+    struct SingleLinkedStack {
+        struct Element* top;
+    } SingleLinkedStack;
+    struct ArrayStack {
+        int size;
+        int limit;
+        struct Element* array;
+    } ArrayStack;
+    // Stack implementation end
+    struct Element {
+        union Data* data;
+        struct Element* next;
+    } Element;
+    struct Array {
+        int prefix;
+        Int* array;
+    } Array;
+    struct Tree {
+        union Data* tree;
+        struct Node* node;
+        enum Code put;
+        enum Code get;
+        enum Code remove;
+        enum Code clear;
+        enum Code next;
+    } Tree;
+    struct RedBlackTree {
+        struct Node* root;
+        struct Node* current; // reading node of original tree
+        struct Node* previous; // parent of reading node of original tree
+        struct Node* newNode; // writing node of new tree
+        struct Node* parent;
+        struct Node* grandparent;
+        struct Stack* nodeStack;
+        enum Code findNodeNext;
+        int result;
+    } RedBlackTree;
+    struct RotateTree {
+        enum Code next;
+        struct RedBlackTree* traverse;
+        struct Tree* tree;
+    } RotateTree;
+    struct Node {
+        int key; // comparable data segment
+        union Data* value;
+        struct Node* left;
+        struct Node* right;
+        // need to balancing
+        enum Color {
+            Red,
+            Black,
+            // Red eq 0,Black eq 1. enum name convert intager.
+        } color;
+    } Node;
+    struct Atomic {
+        union Data* atomic;
+        union Data** ptr;
+        union Data* oldData;
+        union Data* newData;
+        enum Code checkAndSet;
+        enum Code next;
+        enum Code fail;
+    } Atomic;
+    struct AtomicReference {
+    } AtomicReference;
+    struct Semaphore {
+        union Data* semaphore;
+        enum Code p;
+        enum Code v;
+        enum Code next;
+    } Semaphore;
+    struct SemaphoreImpl {
+        int value;
+        struct Lock* lock;
+        struct Queue* waitThreadQueue;
+    } SemaphoreImpl;
+    struct Allocate {
+        enum Code next;
+        long size;
+    } Allocate;
+    struct Integer {
+        int value;
+    } Integer;
+    struct SortArray {
+        union Data* sortArray;
+        struct Integer *array; //Array arrayじゃできない?
+        int loopCounter;
+        int block;
+        int first;
+        int prefix;
+    } SortArray;
+    struct Iterator {
+        union Data* iterator;
+        struct Context* task;
+        int numGPU;
+        enum Code exec;
+        enum Code barrier;
+        enum Code whenWait;
+        enum Code next;
+    } Iterator;
+    struct MultiDimIterator {
+        int x;
+        int y;
+        int z;
+        int count;
+        int counterX;
+        int counterY;
+        int counterZ;
+    } MultiDimIterator;
+    struct MultiDim {
+        int x;
+        int y;
+        int z;
+    } MultiDim;
+    struct Executor {
+        union Data* executor;
+        struct Context* task;
+        enum Code read;
+        enum Code exec;
+        enum Code write;
+        enum Code next;
+    } Executor;
+#ifdef USE_CUDAWorker
+    struct CUDAExecutor {
+        CUdeviceptr** kernelParams;
+        struct CUDABuffer* buffer;
+        int maxThreadPerBlock;
+        int maxThreadPerBlockX;
+        int maxThreadPerBlockY;
+        int maxThreadPerBlockZ;
+        struct Timer* timer;
+    } CUDAExecutor;
+    struct CUDABuffer {
+        int inputLen;
+        int outputLen;
+        union Data** inputData;
+        union Data** outputData;
+    } CUDABuffer;
+    CUdeviceptr CUdeviceptr;
+#else
+    struct CUDAExecutor {
+    } CUDAExecutor;
+    struct CUDABuffer {
+    } CUDABuffer;
+    CUdeviceptr CUdeviceptr;
+#endif
+    Int Int;
+    struct Memory {
+        union Data* adr;
+        int length;
+        union Data* body;
+        int hash;
+    } Memory;
+    struct Buffer {
+        union Data* buffer;
+        union Data* data;
+        enum Code put;
+        enum Code take;
+        enum Code next;
+    } Buffer;
+    struct BoundedBuffer {
+        struct Element* top;
+        struct Element* last;
+        struct Semaphore* fullCount;
+        struct Semaphore* emptyCount;
+        struct Semaphore* lock;
+    } BoundedBuffer;
+    struct Lock {
+        union Data* lock;
+        enum Code doLock;
+        enum Code doUnlock;
+        enum Code next;
+    } Lock;
+    struct LockImpl {
+        Int* lock;
+        struct Queue* waitThreadQueue;
+        struct Atomic* atomic;
+        struct Context* lockContext;
+    } LockImpl;
+    struct SpinLock {
+        volatile Int* lock;
+        struct Atomic* atomic;
+        struct Context* lockContext;
+    } SpinLock;
+}; // union Data end       this is necessary for context generator
+typedef union Data Data;
+
+#include "c/typedefData.h"
+
+#include "c/extern.h"
+
+extern __code start_code(struct Context* context);
+extern __code exit_code(struct Context* context);
+extern __code meta(struct Context* context, enum Code next);
+//extern __code par_meta(struct Context* context, enum Code spawns, enum Code next);
+extern __code parGotoMeta(struct Context* context, enum Code next);
+extern void initContext(struct Context* context);
+
+#endif
--- a/src/parallel_execution/update_context.pl	Mon Nov 25 14:32:34 2019 +0900
+++ b/src/parallel_execution/update_context.pl	Mon Nov 25 17:03:33 2019 +0900
@@ -12,6 +12,7 @@
 
 my $interface_file = shift or die "require itnerface file";
 my $h2context = Gears::Util->parse_interface($interface_file);
+
 my $context = Gears::Util->h2context_str($h2context);
 
 if ($opt{c}) {