view src/parallel_execution/context.h @ 138:04a2f486a30d

insert works but is not balanced
author kono
date Tue, 08 Nov 2016 19:39:40 +0900
parents a747dc9d4da6
children 34a7a21edc36
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 = dseg;\
    context->heap += sizeof(struct Meta);\
    context->data[dseg] = context->heap; context->heap += sizeof(struct dseg); (struct dseg *)context->data[dseg]; })

#define ALLOC_DATA_TYPE(context, dseg, t) ({\
    struct Meta* meta = (struct Meta*)context->heap;\
    meta->type = t;\
    context->heap += sizeof(struct Meta);\
    context->data[dseg] = context->heap; context->heap += sizeof(struct t); (struct t *)context->data[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 = t; \
    data; })

#define GET_TYPE(dseg) ({ \
    struct Meta* meta = (struct Meta*)(((void*)dseg) - sizeof(struct Meta));\
    meta->type; })

enum Code {
    Code1,
    Code2,
    Code3,
    Code4,
    Code5,
    Find,
    Not_find,
    Code6,
    Allocator,
    PutTree,
    Replace,
    Replace1,
    Insert,
    Compare,
    RotateL,
    RotateL1,
    RotateR,
    RotateR1,
    SetTree,
    InsertCase1,
    InsertCase2,
    InsertCase3,
    InsertCase4,
    InsertCase5,
    InsertCase51,
    StackClear,
    Get,
    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,
    PushSingleLinkedStack,
    PopSingleLinkedStack,
    Pop2SingleLinkedStack,
    GetSingleLinkedStack,
    Get2SingleLinkedStack,
    IsEmptySingleLinkedStack,
    SpawnTask,
    Twice,
    StartTime,
    EndTime,
    Exit,
};

enum Relational {
    EQ,
    GT,
    LT,
};

enum DataType {
    Worker,
    Allocate,
    SingleLinkedStack,
    Stack,
    Tree,
    Traverse,
    Node,
    LoopCounter,
    Time,
    Element,
    ActiveQueue,
    WaitQueue,
    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;
    } 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 push;
        enum Code pop;
        enum Code pop2;
        enum Code isEmpty;
        enum Code whenEmpty;
        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 {
        struct Node* root;
    } tree;
    struct Traverse {
        enum Code next;
        enum Code rotateNext;
        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 Stack* nodeStack;
        int result;
    } traverse;
    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;
};

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