view TaskManager/Gpu/GpuThreads.cc @ 1437:fa6723e7d329 draft

fix GpuTaskManagerImpl
author Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
date Sat, 07 Apr 2012 09:29:09 +0900
parents 42057124fb44
children 3c787a21ff10
line wrap: on
line source

#include "GpuThreads.h"
#include "GpuScheduler.h"
#include "TaskManagerImpl.h"

GpuThreads::GpuThreads()
{
    threads = new pthread_t;
    args = new gpu_arg;
}

GpuThreads::~GpuThreads()
{
    delete threads;
    delete args;
}

void
GpuThreads::init()
{
    clGetPlatformIDs(1, &platfrom_id, &ret_num_platforms); 
    clGetDeviceIds(platform_id, CL_DEVICE_TYPE_GPU, 1, &device_id, &ret_num_devices);
    // unavailable GPU
    if( ret_num_devices == 0) {
        exit(EXIT_FAILURE);
    }
    context = clCreateContext(NULL, 1, device_id, NULL, NULL, &ret);
    command_queue = clCreateCommandQueue(context, *device_id, 0, &ret);

    args.scheduler = new GpuScheduler();
    args.useRefDma = use_refdma;
    args.command_queue = command_queue;

    pthread_create(&threads, NULL, &cpu_thread_run, (void*)&args);

}

void *
GpuThreads::gpu_thread_run(void *args)
{
    gpu_arg *argt = (gpu_arg *) args;
    Scheduler *g_scheduler = argt->scheduler;
    // command_queueがThreadセーフじゃない。
    cl_command_queue *command_queue = argt->command_queue;

    TaskManagerImpl *manager = new GpuTaskManagerImpl();
    g_scheduler->init(manager, argt->useRefDma);
    g_scheduler->set_command_queue(command_queue);

    manager->set_scheduler(g_scheduler);

    g_scheduler->run();
    g_scheduler->finish();

    return NULL
}

int
GpuThreads::get_mail(memaddr *ret)
{
    *ret = args.scheduler->mail_read_from_host();
    return 1;
}

int
GpuThreads::has_mail(memaddr *ret)
{
    if (args.scheduler->has_mail_from_host() != 0) {
        return get_mail(ret);
    } else {
        return 0;
    }
}

void
CpuThreads::send_mail(int cpuid, int num, memaddr *data)
{
    args.scheduler->mail_write_from_host(*data);
}