ifndef CONTEXT_H

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);
meta->data = context->heap;
*context->metaData = meta;
context->metaData++;
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);
meta->data = context->heap;
*context->metaData = meta;
context->metaData++;
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;
meta->data = data;
*context->metaData = meta;
context->metaData++;
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;
meta->data = data;
*context->metaData = meta;
context->metaData++;
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;
meta->data = data;
*context->metaData = meta;
context->metaData++;
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 NALLOC(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 GET_PREV_META(dseg) ((Meta*)(((void*)dseg) - (sizeof(Meta) * 2)))

#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; struct Meta **metaDataStart; struct Meta metaData; 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; };

typedef int Int; #ifndef USE_CUDAWorker typedef unsigned long long CUdeviceptr; #endif union Data { ///home/anatofuz/src/firefly/hg/Gears/Gears/src/parallel_execution/../parallel_execution/Atomic.h #ifndef ATOMIC_STRUCT struct Atomic { union Data* atomic; union Data** ptr; union Data* oldData; union Data* newData; enum Code checkAndSet; enum Code next; enum Code fail; } Atomic; #define ATOMIC_STRUCT #else struct Atomic; #endif ///home/anatofuz/src/firefly/hg/Gears/Gears/src/parallel_execution/../parallel_execution/plautogen/impl/AtomicReference.h #ifndef ATOMICREFERENCE_STRUCT struct AtomicReference { } AtomicReference; #define ATOMICREFERENCE_STRUCT #else struct AtomicReference; #endif ///home/anatofuz/src/firefly/hg/Gears/Gears/src/parallel_execution/../parallel_execution/plautogen/interface/CPUWorker.h #ifndef CPUWORKER_STRUCT struct CPUWorker { pthread_mutex_t mutex; pthread_cond_t cond; struct Context* context; int id; int loopCounter; } CPUWorker; #define CPUWORKER_STRUCT #else struct CPUWorker; #endif ///home/anatofuz/src/firefly/hg/Gears/Gears/src/parallel_execution/../parallel_execution/plautogen/interface/Element.h #ifndef ELEMENT_STRUCT struct Element { union Data* data; struct Element* next; } Element; #define ELEMENT_STRUCT #else struct Element; #endif ///home/anatofuz/src/firefly/hg/Gears/Gears/src/parallel_execution/../parallel_execution/plautogen/interface/Integer.h #ifndef INTEGER_STRUCT struct Integer { int value; } Integer; #define INTEGER_STRUCT #else struct Integer; #endif ///home/anatofuz/src/firefly/hg/Gears/Gears/src/parallel_execution/../parallel_execution/Iterator.h #ifndef ITERATOR_STRUCT struct Iterator { union Data* iterator; struct Context* task; int numGPU; enum Code exec; enum Code barrier; enum Code whenWait; enum Code next; } Iterator; #define ITERATOR_STRUCT #else struct Iterator; #endif ///home/anatofuz/src/firefly/hg/Gears/Gears/src/parallel_execution/../parallel_execution/plautogen/interface/LoopCounter.h #ifndef LOOPCOUNTER_STRUCT struct LoopCounter { int i; } LoopCounter; #define LOOPCOUNTER_STRUCT #else struct LoopCounter; #endif ///home/anatofuz/src/firefly/hg/Gears/Gears/src/parallel_execution/../parallel_execution/plautogen/interface/Meta.h #ifndef META_STRUCT struct Meta { enum DataType type; long size; long len; union Data* data; struct Queue* wait; // tasks waiting this dataGear; } Meta; #define META_STRUCT #else struct Meta; #endif ///home/anatofuz/src/firefly/hg/Gears/Gears/src/parallel_execution/../parallel_execution/Queue.h #ifndef QUEUE_STRUCT 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; #define QUEUE_STRUCT #else struct Queue; #endif ///home/anatofuz/src/firefly/hg/Gears/Gears/src/parallel_execution/../parallel_execution/plautogen/impl/SynchronizedQueue.h #ifndef SYNCHRONIZEDQUEUE_STRUCT struct SynchronizedQueue { struct Element* top; struct Element* last; struct Atomic* atomic; } SynchronizedQueue; #define SYNCHRONIZEDQUEUE_STRUCT #else struct SynchronizedQueue; #endif ///home/anatofuz/src/firefly/hg/Gears/Gears/src/parallel_execution/../parallel_execution/interface/StackGenerics.h #ifndef STACKGENERICS_STRUCT struct StackGenerics { union Data* stack; TYPED_GENERICS_StackGenerics_T * data; TYPED_GENERICS_StackGenerics_T * data1; enum Code clear; enum Code push; enum Code pop; enum Code pop2; enum Code isEmpty; enum Code get; enum Code get2; enum Code next; enum Code whenEmpty; } StackGenerics; #define STACKGENERICS_STRUCT #else struct StackGenerics; #endif ///home/anatofuz/src/firefly/hg/Gears/Gears/src/parallel_execution/../parallel_execution/impl/StackGenericsImpl.h #ifndef STACKGENERICSIMPL_STRUCT struct StackGenericsImpl { enum Code next; } StackGenericsImpl; #define STACKGENERICSIMPL_STRUCT #else struct StackGenericsImpl; #endif ///home/anatofuz/src/firefly/hg/Gears/Gears/src/parallel_execution/../parallel_execution/TaskManager.h #ifndef TASKMANAGER_STRUCT struct TaskManager { union Data* taskManager; struct Context* task; union Data* taskManagerImpl; struct Element* taskList; enum Code spawn; enum Code spawnTasks; enum Code setWaitTask; enum Code shutdown; enum Code incrementTaskCount; enum Code decrementTaskCount; enum Code next; enum Code next1; } TaskManager; #define TASKMANAGER_STRUCT #else struct TaskManager; #endif ///home/anatofuz/src/firefly/hg/Gears/Gears/src/parallel_execution/../parallel_execution/plautogen/impl/TaskManagerImpl.h #ifndef TASKMANAGERIMPL_STRUCT struct TaskManagerImpl { 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; enum Code next; } TaskManagerImpl; #define TASKMANAGERIMPL_STRUCT #else struct TaskManagerImpl; #endif ///home/anatofuz/src/firefly/hg/Gears/Gears/src/parallel_execution/../parallel_execution/Worker.h #ifndef WORKER_STRUCT struct Worker { union Data* worker; struct Queue* tasks; struct Context* task; pthread_t thread; struct TaskManager* taskManager; enum Code taskReceive; enum Code shutdown; enum Code next; } Worker; #define WORKER_STRUCT #else struct Worker; #endif struct Context Context; }; // 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 Filehandle STDOUT reopened as $fd only for input at generate_context.pl line 145. Filehandle STDOUT reopened as $cx only for input at generate_context.pl line 159.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42



# Storeable

- `dclone`を使えばいい感じにdeep copyが可能

```perl
#!/usr/bin/env perl
use strict;
use warnings;
use Storable qw/dclone/;
use Data::Dumper;


my $test = {

    cat => {
      names => [qw/a a a/],
      color => [qw/ blue yelow/],
      say => {
        one => 1,
       two => 2,
      },
    },

    dog => {
      say => {
        one => 1,
       two => 2,
      },
    },
};


my $copy = dclone($test);

delete $copy->{cat}->{say};


print Dumper $copy;
print Dumper $test;
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
use strict;
use warnings;

my $line = 'Phils* createPhilsImpl(struct Context* context, int id, AtomicT<int> right, AtomicT<int> left) {';


if ($line =~ /^\s*(\w+)(<(.*)>)?\*\s*create(\w+)\(([<>,*\w\s]+)\)/) {
   my $interface = $1;
   my $generics  = $2;
   my $method    = $3;

   print "interface $interface\n";
   print "generics $generics\n";
 }
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
+kajika+anatofuz$ perl generate_stub.pl --debug AtomicTImpl.cbc
[getDataGear] match 175 : #interface "AtomicT.h"
[getDataGear] match 137 : typedef struct AtomicT <T>{
[getDataGear] match 315 :     union Data* atomic_t;
[getDataGear] match 315 :     T newData;
[getDataGear] match 315 :     T init;
[getDataGear] match 338 :     __code checkAndSet(Impl* atomic_t,T* ptr ,T init, T newData, __code next(...), __code fail(...));
[getDataGear] match 338 :     __code set(Impl* atomic_t,T* ptr ,T newData, __code next(...));
[getDataGear] match 338 :     __code next(...);
[getDataGear] match 338 :     __code fail(...);
[getDataGear] match 315 : } AtomicT;
[getCodeGear] match 393 : typedef struct AtomicT <T>{
[getCodeGear] match 398 :     __code checkAndSet(Impl* atomic_t,T* ptr ,T init, T newData, __code next(...), __code fail(...));
[getCodeGear] match 398 :     __code set(Impl* atomic_t,T* ptr ,T newData, __code next(...));
[getCodeGear] match 398 :     __code next(...);
[getCodeGear] match 398 :     __code fail(...);
[getDataGear] match 175 : #interface "AtomicTImpl.h"
[getDataGear] match 137 : typedef struct AtomicTImpl <T> impl AtomicT {
[getDataGear] match 315 :   T atomic;
[getDataGear] match 315 :   T init;
[getDataGear] match 338 :   __code next(...);
[getDataGear] match 315 : } AtomicTImpl;
[getDataGear] match 206 : __code checkAndSet_AtomicTImpl(struct AtomicTImpl* atomic_t_impl, T* ptr,T init, T newData, __code next(...), __code fail(...)) {
[getDataGear] match 206 : __code set_AtomicTImpl(struct AtomicTImpl* atomic_t_impl, T* ptr, T newData, __code next(...) ) {
[generateDataGear] match 667 : #include "../context.h"
[generateDataGear] match 662 : #interface "AtomicT.h"
[generateDataGear] match 662 : #interface "AtomicTImpl.h"
[generateDataGear] match 796 : #include <stdio.h>
[generateDataGear] match 796 :
[generateDataGear] match 796 : AtomicT<T>* createAtomicTImpl(struct Context* context,T init) {
[generateDataGear] match 796 :     struct AtomicT<T>* atomic_t  = new AtomicT();
[generateDataGear] match 796 :     struct AtomicTImpl<T>* atomic_t_impl = new AtomicTImpl();
[generateDataGear] match 796 :     atomic_t->atomic_t = (union Data*)atomic_t_impl;
[generateDataGear] match 796 :     atomic_t->checkAndSet = C_checkAndSetAtomicTImpl;
[generateDataGear] match 796 :     atomic_t->set = C_setAtomicTImpl;
[generateDataGear] match 796 :     atomic_t->fail = C_failAtomicTImpl;
[generateDataGear] match 796 :     atomic_t_impl->atomic = init;
[generateDataGear] match 796 :     atomic_t_impl->init = init;
[generateDataGear] match 796 :     return atomic_t;
[generateDataGear] match 796 :
[generateDataGear] match 796 : }
[generateDataGear] match 796 :
[generateDataGear] match 686 : __code checkAndSet_AtomicTImpl(struct AtomicTImpl* atomic_t_impl, T* ptr,T init, T newData, __code next(...), __code fail(...)) {
[generateDataGear] match 1049 :     if (__sync_bool_compare_and_swap(ptr, init, newData)) {
[generateDataGear] match 983 :         goto next(...);
[generateDataGear] match 1049 :     }
[generateDataGear] match 983 :     goto fail(...);
[generateDataGear] match 1046 : }
[generateDataGear] match 1070 : }
[generateDataGear] match 796 :
[generateDataGear] match 686 : __code set_AtomicTImpl(struct AtomicTImpl* atomic_t_impl, T* ptr, T newData, __code next(...) ) {
[generateDataGear] match 1049 :         *ptr = newData;
[generateDataGear] match 983 :    goto next(...);
[generateDataGear] match 1046 : }
[generateDataGear] match 1070 : }
[generateDataGear] match 796 :

AtomicTはわりとしょっぱいバグ

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
changeset:   827:6359940aeb58
tag:         tip
user:        anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
date:        Mon Jan 18 22:41:01 2021 +0900
summary:     ...

diff -r af4c7116ad28 -r 6359940aeb58 src/parallel_execution/AtomicT.h
--- a/src/parallel_execution/AtomicT.h  Mon Jan 18 22:27:36 2021 +0900
+++ b/src/parallel_execution/AtomicT.h  Mon Jan 18 22:41:01 2021 +0900
@@ -1,8 +1,4 @@
 typedef struct AtomicT <T>{
-    union Data* atomic_t;
-    T* ptr;
-    T newData;
-    T init;
     __code checkAndSet(Impl* atomic_t,T* ptr ,T init, T newData, __code next(...), __code fail(...));
     __code set(Impl* atomic_t,T* ptr ,T newData, __code next(...));
     __code next(...);
diff -r af4c7116ad28 -r 6359940aeb58 src/parallel_execution/AtomicTImpl.cbc
--- a/src/parallel_execution/AtomicTImpl.cbc    Mon Jan 18 22:27:36 2021 +0900
+++ b/src/parallel_execution/AtomicTImpl.cbc    Mon Jan 18 22:41:01 2021 +0900
@@ -1,6 +1,5 @@
 #include "../context.h"
-#interface "AtomicT.h"
-#interface "AtomicTImpl.h"
+#impl "AtomicT.h" for "AtomicTImpl.h"
 #include <stdio.h>

 AtomicT<T>* createAtomicTImpl(struct Context* context,T init) {

changeset:   826:af4c7116ad28
user:        anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
date:        Mon Jan 18 22:27:36 2021 +0900
summary:     bug fix : AtomicT.h

diff -r 4cb62fe4673a -r af4c7116ad28 src/parallel_execution/AtomicT.h
Licensed under CC BY-NC-SA 4.0
comments powered by Disqus
Built with Hugo
Theme Stack designed by Jimmy