Mercurial > hg > Papers > 2017 > ikkun-sigos
changeset 4:177ab3da697b
fix tex
author | ikkun |
---|---|
date | Thu, 20 Apr 2017 18:54:57 +0900 |
parents | 227ce8abda7e |
children | b1ee47caade7 |
files | paper/src/CMakeLists.txt paper/src/CUDAWorker.cbc paper/src/sample.c |
diffstat | 3 files changed, 224 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/paper/src/CMakeLists.txt Thu Apr 20 18:54:57 2017 +0900 @@ -0,0 +1,88 @@ +cmake_minimum_required(VERSION 2.8) + +set(USE_CUDA,0) + +# -DUSE_CUDA +# add_definitions("-Wall -g -O") + +set(CMAKE_C_COMPILER $ENV{CBC_COMPILER}) +add_definitions("-Wall -g") + + +if (${USE_CUDA}) + include_directories("/usr/local/cuda/include") + set(NVCCFLAG "-std=c++11" "-g" "-O0" ) + set(CUDA_LINK_FLAGS "-framework CUDA -lc++ -Wl,-search_paths_first -Wl,-headerpad_max_install_names /Developer/NVIDIA/CUDA-8.0/lib/libcudart_static.a -Wl,-rpath,/usr/local/cuda/lib") + find_package(CUDA REQUIRED) + SET( CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${CUDA_LINK_FLAGS}" ) +endif() + + +macro( GearsCommand ) + set( _OPTIONS_ARGS ) + set( _ONE_VALUE_ARGS TARGET ) + set( _MULTI_VALUE_ARGS SOURCES ) + cmake_parse_arguments( _Gears "${_OPTIONS_ARGS}" "${_ONE_VALUE_ARGS}" "${_MULTI_VALUE_ARGS}" ${ARGN} ) + + set (_Gears_CSOURCES) + foreach(i ${_Gears_SOURCES}) + if (${i} MATCHES "\\.cbc") + string(REGEX REPLACE "(.*).cbc" "c/\\1.c" j ${i}) + add_custom_command ( + OUTPUT ${j} + DEPENDS ${i} + COMMAND "perl" "generate_stub.pl" "-o" ${j} ${i} + ) + elseif (${i} MATCHES "\\.cu") + string(REGEX REPLACE "(.*).cu" "c/\\1.ptx" j ${i}) + add_custom_command ( + OUTPUT ${j} + DEPENDS ${i} + COMMAND nvcc ${NVCCFLAG} -c -ptx -o ${j} ${i} + ) + else() + set(j ${i}) + endif() + list(APPEND _Gears_CSOURCES ${j}) + endforeach(i) + + add_custom_command ( + OUTPUT c/${_Gears_TARGET}-context.c + DEPENDS ${_Gears_CSOURCES} + COMMAND "perl" "generate_context.pl" "-o" ${_Gears_TARGET} ${_Gears_CSOURCES} + ) + add_executable(${_Gears_TARGET} ${_Gears_CSOURCES} c/${_Gears_TARGET}-context.c) +endmacro() + + +GearsCommand( + TARGET + twice + SOURCES + main.cbc RedBlackTree.cbc compare.c SingleLinkedStack.cbc CPUWorker.cbc time.cbc twice.cbc TaskManagerImpl.cbc SingleLinkedQueue.cbc SynchronizedQueue.cbc SemaphoreImpl.cbc +) + +if (${USE_CUDA}) + GearsCommand( + TARGET + CUDAtwice + SOURCES + main.cbc RedBlackTree.cbc compare.c SingleLinkedStack.cbc CPUWorker.cbc time.cbc twice.cbc TaskManagerImpl.cbc SingleLinkedQueue.cbc SynchronizedQueue.cbc SemaphoreImpl.cbc CUDAWorker.cbc CUDAtwice.cbc CUDAtwice.cu cuda.c + ) + set_target_properties(CUDAtwice PROPERTIES COMPILE_FLAGS "-Wall -g -DUSE_CUDAWorker=1 -DUSE_CUDA_MAIN_THREAD") +endif() + +GearsCommand( + TARGET + queue_test + SOURCES + test/queue_test.c SingleLinkedQueue.cbc +) + +GearsCommand( + TARGET + stack_test + SOURCES + test/stack_test.c SingleLinkedStack.cbc +) +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/paper/src/CUDAWorker.cbc Thu Apr 20 18:54:57 2017 +0900 @@ -0,0 +1,125 @@ +#include <stdio.h> +#include <sys/time.h> +#include <string.h> +#include <stdlib.h> +#include <libkern/OSAtomic.h> + +#include "../context.h" + +extern void cudaInit(struct CUDAWorker *cudaWorker,int phase) ; + +static void start_CUDAworker(Worker* worker); + +#ifndef USE_CUDA_MAIN_THREAD +volatile +#endif +int cuda_initialized = 0; + +Worker* createCUDAWorker(struct Context* context, int id, Queue* queue, TaskManagerImpl *im) { + struct Worker* worker = ALLOC(context, Worker); + struct CUDAWorker* cudaWorker = new CUDAWorker(); + + cudaInit(cudaWorker,0); + + worker->worker = (union Data*)cudaWorker; + worker->tasks = queue; + cudaWorker->id = id; + worker->shutdown = C_shutdownCUDAWorker; +#ifndef USE_CUDA_MAIN_THREAD + pthread_create(&worker->worker->CUDAWorker.thread, NULL, (void*)&start_CUDAworker, worker); +#else + if (im) { + im->workers[0] = worker; + } + cuda_initialized = 1; + start_CUDAworker(worker); +#endif + return worker; +} + + +static void start_CUDAworker(Worker* worker) { + CUDAWorker* cudaWorker = (CUDAWorker*)worker->worker; + cudaWorker->context = NEW(struct Context); + initContext(cudaWorker->context); + Gearef(cudaWorker->context, Worker)->worker = (union Data*)worker; + + goto meta(cudaWorker->context, C_taskReceiveCUDAWorker); +} + +__code taskReceiveCUDAWorker(struct Worker* worker,struct Queue* queue) { + queue->queue = (union Data*)worker->tasks; + queue->next = C_getTaskCUDA; + goto meta(context, worker->tasks->take); +} + +__code taskReceiveCUDAWorker_stub(struct Context* context) { + goto taskReceiveCUDAWorker(context, &Gearef(context, Worker)->worker->Worker, Gearef(context, Queue)); +} + +__code getTaskCUDA(struct Worker* worker, struct Context* task) { + if (!task) + return; // end thread + worker->taskReceive = C_taskReceiveCUDAWorker; + task->worker = worker; + enum Code taskCg = task->next; + task->next = C_odgCommitCUDA; // set CG after task exec + goto meta(task, taskCg); +} + +__code getTaskCUDA_stub(struct Context* context) { + Worker* worker = &Gearef(context,Worker)->worker->Worker; + struct Context* task = &Gearef(context, Queue)->data->Context; + goto getTaskCUDA(context, worker, task); +} + +__code odgCommitCUDA(struct LoopCounter* loopCounter, struct Queue* queue, struct Context* task) { + int i = loopCounter->i ; + if(task->odg + i < task->maxOdg) { + queue->queue = (union Data*)GET_WAIT_LIST(task->data[task->odg+i]); + queue->next = C_odgCommitCUDA1; + goto meta(context, queue->queue->Queue.take); + } + loopCounter->i = 0; + goto meta(context, C_taskReceiveCUDAWorker); +} + +__code odgCommitCUDA_stub(struct Context* context) { + struct Context* workerContext = context->worker->worker->CUDAWorker.context; + goto odgCommitCUDA(workerContext, + Gearef(workerContext, LoopCounter), + Gearef(workerContext, Queue), + context); +} + +__code odgCommitCUDA1(struct TaskManager* taskManager, struct Context* task) { + if(__sync_fetch_and_sub(&task->idgCount, 1)) { + if(task->idgCount == 0) { + taskManager->taskManager = (union Data*)task->taskManager; + taskManager->context = task; + taskManager->next = C_odgCommitCUDA; + goto meta(context, task->taskManager->spawn); + } + } else { + goto meta(context, C_odgCommitCUDA1); + } +} + +__code odgCommitCUDA1_stub(struct Context* context) { + struct Context* task = &Gearef(context, Queue)->data->Context; + goto odgCommitCUDA1(context, + Gearef(context, TaskManager), + task); + +} + +extern void cudaShutdown( CUDAWorker *cudaWorker) ; + +__code shutdownCUDAWorker(struct Context* context, CUDAWorker* worker) { + cudaShutdown( worker) ; +} + +__code shutdownCUDAWorker_stub(struct Context* context) { + CUDAWorker* worker = (CUDAWorker *)GearImpl(context, Worker, worker); + goto shutdownCUDAWorker(context,worker); +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/paper/src/sample.c Thu Apr 20 18:54:57 2017 +0900 @@ -0,0 +1,11 @@ +// Code Gear +__code code1(struct Allocate* allocate) { + allocate->size = sizeof(struct Data1); + + goto allocator(allocate, Code2); +} + +// Code Gear +__code code2(struct Data1* data1) { + // processing +}