view src/parallel_execution/examples/boundedBuffer/main.cbc @ 516:62a77785cb2b

Bounded Loop for boundedBuffer examples
author Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
date Thu, 04 Jan 2018 19:50:46 +0900
parents a7127917c736
children
line wrap: on
line source

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <assert.h>

#include "../../../context.h"
#interface "TaskManager.h"

int cpu_num = 1;
int length = 100;
int buffer_size = 10;
int gpu_num = 0;
int CPU_ANY = -1;
int CPU_CUDA = -1;

void *start_taskManager(struct Context *context) {
    goto initDataGears(context, Gearef(context, LoopCounter), Gearef(context, TaskManager));
    return 0;
}

__code initDataGears(struct LoopCounter* loopCounter, struct TaskManager* taskManager) {
    // loopCounter->tree = createRedBlackTree(context);
    loopCounter->i = 0;
    taskManager->taskManager = (union Data*)createTaskManagerImpl(context, cpu_num, gpu_num, 0);
    goto code1();
}

__code initDataGears_stub(struct Context* context) {
    struct TaskManager* taskManager =  Gearef(context, TaskManager);
    taskManager->taskManager = 0;
    struct LoopCounter* loopCounter = Gearef(context, LoopCounter);
    goto initDataGears(context, loopCounter, taskManager);
}

__code code1(struct Timer* timer) {
    printf("cpus:\t\t%d\n", cpu_num);
    printf("gpus:\t\t%d\n", gpu_num);
    printf("length:\t\t%d\n", length);
    goto createTask1();
}

__code code2(struct TaskManager* taskManager) {
    goto taskManager->shutdown(exit_code);
}

__code code2_stub(struct Context* context) {
    goto code2(context, &Gearef(context, TaskManager)->taskManager->TaskManager);
}

__code createTask1(struct TaskManager* taskManager) {
    struct Buffer* buffer = createBoundedBuffer(context, buffer_size);
    Int* len = new Int();
    *len = length;
    par goto producer(buffer, len, __exit);
    par goto producer(buffer, len, __exit);
    par goto consumer(buffer, len, __exit);
    par goto consumer(buffer, len, __exit);
    par goto initBuffer(buffer, len, __exit);
    goto code2();
}

void init(int argc, char** argv) {
    for (int i = 1; argv[i]; ++i) {
        if (strcmp(argv[i], "-cpu") == 0)
            cpu_num = (int)atoi(argv[i+1]);
        else if (strcmp(argv[i], "-l") == 0)
            length = (int)atoi(argv[i+1]);
        else if (strcmp(argv[i], "-bufferSize") == 0)
            buffer_size = (int)atoi(argv[i+1]);
        else if (strcmp(argv[i], "-cuda") == 0) {
            gpu_num = 1;
            CPU_CUDA = 0;
        }
    }
}

int main(int argc, char** argv) {
    init(argc, argv);
    goto initDataGears();
}