view src/parallel_execution/examples/twice/main.cbc @ 438:7679093bdd72

Work CUDAtwice
author Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
date Mon, 06 Nov 2017 00:11:43 +0900
parents 497b154141de
children ff2764cb5edb
line wrap: on
line source

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

#include "../../../context.h"

int cpu_num = 1;
int length = 102400;
int split = 8;
int* array_ptr;
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;
}

#ifdef USE_CUDAWorker
#ifdef USE_CUDA_MAIN_THREAD
extern volatile int cuda_initialized;
#endif
#endif

__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);
#ifdef USE_CUDAWorker
#ifdef USE_CUDA_MAIN_THREAD
    while(! cuda_initialized) {};
#endif
#endif
    goto meta(context, C_code1);
}

__code initDataGears_stub(struct Context* context) {
    struct TaskManager* taskManager =  Gearef(context, TaskManager);
    taskManager->taskManager = 0;
#if (! defined(USE_CUDAWorker) || ! defined(USE_CUDA_MAIN_THREAD))
    struct LoopCounter* loopCounter = Gearef(context, LoopCounter);
    goto initDataGears(context, loopCounter, taskManager);
#else
    cuda_initialized = 0;
    pthread_t thread;
    pthread_create(&thread, NULL, (void*)&start_taskManager, context);
    while (taskManager->taskManager == 0);
    TaskManager *t = (TaskManager*)taskManager->taskManager;
    TaskManagerImpl *im = (TaskManagerImpl*)t->taskManager;
    struct Queue *q = (Queue *)im->workers[0];
    createCUDAWorker(context,0,q, im);
    pthread_join(thread,0);
    exit(0);
#endif
}

__code code1(struct Time* time) {
    printf("cpus:\t\t%d\n", cpu_num);
    printf("gpus:\t\t%d\n", gpu_num);
    printf("length:\t\t%d\n", length);
    printf("length/task:\t%d\n", length/split);
    /* puts("queue"); */
    /* print_queue(context->data[ActiveQueue]->queue.first); */
    /* puts("tree"); */
    /* print_tree(context->data[Tree]->tree.root); */
    /* puts("result"); */
    goto meta(context, C_createTask1);
}


__code createTask1(struct LoopCounter* loopCounter, struct TaskManager* taskManager) {
    Array* array1 = &ALLOCATE_DATA_GEAR(context, Array)->Array;
    Array* array2 = &ALLOCATE_DATA_GEAR(context, Array)->Array;
    Time* time = createTimeImpl(context);

    par goto createArray(array1, time, __exit);
    par goto twice(array1, array2, iterate(split), __exit);
    par goto printArray(array2, time, __exit);
    goto code2();
}

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

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], "-s") == 0)
            split = (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);
    struct Context* main_context = NEW(struct Context);
    initContext(main_context);
    main_context->next = C_initDataGears;

    goto start_code(main_context);
}