view src/parallel_execution/context.h @ 166:06cfc1289361

fix
author mir3636
date Fri, 18 Nov 2016 19:34:44 +0900
parents 1115367f03a4
children 34562e63981f
line wrap: on
line source

/* Context definition for llrb example */
#ifndef CONTEXT_H
#define CONTEXT_H
#include <pthread.h>
#ifdef USE_CUDA
#include <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) ({\
    struct Meta* meta = (struct Meta*)context->heap;\
    meta->type = D_##dseg;\
    context->heap += sizeof(struct Meta);\
    context->data[D_##dseg] = context->heap; context->heap += sizeof(struct dseg); (struct dseg *)context->data[D_##dseg]; })

#define ALLOC_DATA_TYPE(context, dseg, t) ({\
    struct Meta* meta = (struct Meta*)context->heap;\
    meta->type = D_##t;\
    context->heap += sizeof(struct Meta);\
    context->data[D_##dseg] = context->heap; context->heap += sizeof(struct t); (struct t *)context->data[D_##dseg]; })

#define ALLOCATE(context, t) ({ \
    struct Meta* meta = (struct Meta*)context->heap;\
    context->heap += sizeof(struct Meta);\
    union Data* data = context->heap; \
    context->heap += sizeof(struct t); \
    meta->type = D_##t; \
    data; })

#define GET_TYPE(dseg) ({ \
    struct Meta* meta = (struct Meta*)(((void*)dseg) - sizeof(struct Meta));\
    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,
    CreateData1,
    CreateData2,
    CreateTask1,
    CreateTask2,
    CreateTask3,
    CreateTask4,
    PutQueue1,
    PutQueue2,
    PutQueue3,
    PutQueue4,
    GetQueue,
    C_clearSingleLinkedStack,
    C_pushSingleLinkedStack,
    C_popSingleLinkedStack,
    C_pop2SingleLinkedStack,
    C_getSingleLinkedStack,
    C_get2SingleLinkedStack,
    C_isEmptySingleLinkedStack,
    SpawnTask,
    Twice,
    StartTime,
    EndTime,
    Exit,
};

enum Relational {
    EQ,
    GT,
    LT,
};

enum DataType {
    D_Worker,
    D_Allocate,
    D_SingleLinkedStack,
    D_Stack,
    D_Tree,
    D_Traverse,
    D_RotateTree,
    D_Node,
    D_LoopCounter,
    D_Time,
    D_Element,
    D_ActiveQueue,
    D_WaitQueue,
    D_Queue
};

struct Context {
    enum Code next;
    int codeNum;
    __code (**code) (struct Context*);
    void* heapStart;
    void* heap;
    long heapLimit;
    pthread_t thread;
    int thread_num;
    int dataNum;
    union Data **data;
};

union Data {
    struct Meta {
        enum DataType type;
    } meta;
    struct Time {
        enum Code next;
        double time;
    } Time;
    struct LoopCounter {
        int i;
        struct Tree* tree;
    } loopCounter;
    struct Worker {
        int num;
        struct Context* contexts;
    } worker;
#ifdef USE_CUDA
    struct CudaTask {
        CUdevice device;
        CUcontext cuCtx;
        CUfunction code;
        CUdeviceptr* deviceptr;
        CUstream stream;
    } cudatask;
#endif
    struct Task {
        enum Code code;
        int key;
        struct Queue* waitMe;
        struct Queue* waitI;
        int idsCount;
    } Task;
    struct Queue {
        struct Element* first;
        struct Element* last;
        int count;
        union Data* queue;
        enum Code next;
    } Queue;
    // 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 index;
        int prefix;
        int* array;
    } array;
    struct Tree {
        union Data* tree;
        struct Node* node;
        enum Code put;
        enum Code get;
        enum Code remove;
        enum Code next;
    } Tree;
    struct Traverse {
        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;
        int result;
    } Traverse;
    struct RotateTree {
        enum Code next;
        struct Traverse* 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,
        } color;
    } node;
    struct Allocate {
        enum Code next;
        long size;
    } allocate;
    struct OutPutDataSegments {
        union Data **data;
    } ods;
};

// 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 Stack Stack;
typedef struct SingleLinkedStack SingleLinkedStack;
typedef struct ArrayStack ArrayStack;
typedef struct Element Element;
typedef struct Array Array;
typedef struct Tree Tree;
typedef struct Traverse Traverse;
typedef struct RotateTree RotateTree;
typedef struct Node Node;
typedef struct Allocate Allocate;
typedef struct OutputDataSegments OutputDataSegments;

union MetaData {
    struct Queue waitMeTasks;
    struct Queue waitI;
};
#endif