Skip to content

Commit

Permalink
Merge pull request #138 from ZerataX/PaketeBewegen
Browse files Browse the repository at this point in the history
make yacx sbt compatible
  • Loading branch information
zeratax authored Feb 29, 2020
2 parents 1f1bf39 + 0dc267b commit 2cfb5e3
Show file tree
Hide file tree
Showing 55 changed files with 506 additions and 251 deletions.
57 changes: 30 additions & 27 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,38 @@ add_definitions(-DNVRTC_GET_TYPE_NAME=1)

include_directories(include)
include_directories(include/yacx)
include_directories(include/yacx/cexecutor)
include_directories(${CMAKE_CUDA_TOOLKIT_INCLUDE_DIRECTORIES})

link_directories("${CMAKE_CUDA_TOOLKIT_INCLUDE_DIRECTORIES}/../lib")

link_libraries(cuda nvrtc)

link_libraries(cuda nvrtc dl)

file(GLOB SOURCES "src/*.cpp" "src/cexecutor/*.cpp")
file(GLOB JNI_SOURCE_FILES "src/jni/*.cpp")
set(JAVA_SOURCE_FILES
"src/java/yacx/ArrayArg.java"
"src/java/yacx/BooleanArg.java"
"src/java/yacx/ByteArg.java"
"src/java/yacx/CProgram.java"
"src/java/yacx/Device.java"
"src/java/yacx/DoubleArg.java"
"src/java/yacx/Executor.java"
"src/java/yacx/ExecutorFailureException.java"
"src/java/yacx/FloatArg.java"
"src/java/yacx/HalfArg.java"
"src/java/yacx/Headers.java"
"src/java/yacx/IntArg.java"
"src/java/yacx/JNIHandle.java"
"src/java/yacx/Kernel.java"
"src/java/yacx/KernelArg.java"
"src/java/yacx/KernelTime.java"
"src/java/yacx/LongArg.java"
"src/java/yacx/Options.java"
"src/java/yacx/PaddingArg.java"
"src/java/yacx/Program.java"
"src/java/yacx/ShortArg.java"
"src/java/yacx/Utils.java")
"src/main/java/yacx/ArrayArg.java"
"src/main/java/yacx/BooleanArg.java"
"src/main/java/yacx/ByteArg.java"
"src/main/java/yacx/CProgram.java"
"src/main/java/yacx/Device.java"
"src/main/java/yacx/DoubleArg.java"
"src/main/java/yacx/Executor.java"
"src/main/java/yacx/ExecutorFailureException.java"
"src/main/java/yacx/FloatArg.java"
"src/main/java/yacx/HalfArg.java"
"src/main/java/yacx/Headers.java"
"src/main/java/yacx/IntArg.java"
"src/main/java/yacx/JNIHandle.java"
"src/main/java/yacx/Kernel.java"
"src/main/java/yacx/KernelArg.java"
"src/main/java/yacx/KernelTime.java"
"src/main/java/yacx/LongArg.java"
"src/main/java/yacx/Options.java"
"src/main/java/yacx/PaddingArg.java"
"src/main/java/yacx/Program.java"
"src/main/java/yacx/ShortArg.java"
"src/main/java/yacx/Utils.java")

add_library(yacx SHARED ${SOURCES})

Expand All @@ -61,10 +64,10 @@ if(JNI_ENABLED)
OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/java/bin/yacx/${_class_file}"
COMMAND
${Java_JAVAC_EXECUTABLE} ${CMAKE_JAVA_COMPILE_FLAGS} -sourcepath
"${PROJECT_SOURCE_DIR}/src/java/" -d
"${PROJECT_SOURCE_DIR}/src/main/java/" -d
"${CMAKE_CURRENT_BINARY_DIR}/java/bin"
"${PROJECT_SOURCE_DIR}/src/java/yacx/${_java_file}"
DEPENDS "${PROJECT_SOURCE_DIR}/src/java/yacx/${_java_file}")
"${PROJECT_SOURCE_DIR}/src/main/java/yacx/${_java_file}"
DEPENDS "${PROJECT_SOURCE_DIR}/src/main/java/yacx/${_java_file}")
list(APPEND class_files "java/bin/yacx/${_class_file}")
endforeach()
add_custom_target(JNIClasses ALL DEPENDS ${class_files})
Expand Down
23 changes: 23 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

ThisBuild / scalaVersion := "2.12.10"

lazy val buildExecutor = taskKey[Unit]("Builds C executor library")

buildExecutor := {
import scala.language.postfixOps
import scala.sys.process._
//noinspection PostfixMethodCall
"echo y" #| (baseDirectory.value + "/yacx.sh --buildj") !
}

lazy val CUexecutor = (project in file("."))
.settings(
name := "CUDA executor",
version := "0.4.1",
libraryDependencies += "junit" % "junit" % "4.11",

compileOrder := CompileOrder.JavaThenScala,

compile := ((compile in Compile) dependsOn buildExecutor).value,
test := ((test in Test) dependsOn buildExecutor).value
)
28 changes: 21 additions & 7 deletions include/yacx/cexecutor/CProgram.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,40 @@

#include "../JNIHandle.hpp"
#include "LibaryLoader.hpp"
#include "../Options.hpp"
#include <fstream>
#include <vector>

namespace yacx {
class CProgram : public JNIHandle {
public:
CProgram(const char *cProgram, const char *functionName, int numberParameters,
const char *compilerWithOptions);
//! Constructs new compiled CProgram. <br>
//! The decleration of structs, functions or variables with one of the following name within passed C-Code
//! is permitted: <br>
//! op<functionName>, opfn<functionName>, execute<functionName>
//! \param cProgram C-Code for program
//! \param functionName name of c-function, which should be executed
//! \param parameterTypes type of the parameters e.g <code>int</code> or <code>float*</code> <br>
//! pointer types can be abbreviated by *
//! \param compiler name of the compiler, which should be used to compile this cProgram
//! \param options options for the compiler
CProgram(const char *cProgram, const char *functionName, std::vector<std::string> &parameterTypes,
const char *compiler = "gcc", Options &options = DEFAULT_OPTIONS);
~CProgram();

void execute(void **arguments);
int getNumberArguments() const { return m_numberArguments; }
//! Executes a the cFunction
//! \param arguments arguments for the cFunction
void execute(std::vector<void*> arguments);

private:
void createSrcFile(const char *cProgram, const char *functionName,
int numberParameters, std::ofstream &fileOut);
std::vector<std::string> &parameterTypes, std::ofstream &fileOut);
void compile(const char *cProgram, const char *functionName,
int numberParameters,
const char *compilerWithOptions = "gcc -Wall");
std::vector<std::string> &parameterTypes,
std::string &compilerCommand);

static int id;
static Options DEFAULT_OPTIONS;
int m_numberArguments;
struct detail::dynop m_op;
std::string m_srcFile;
Expand Down
12 changes: 10 additions & 2 deletions include/yacx/cexecutor/LibaryLoader.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#include <string>

namespace yacx {
namespace detail {
struct dynop {
Expand All @@ -11,7 +13,13 @@ struct opfn {
void (*op)(void **parameter);
};

void load_op(struct dynop *dest, const char *filename);
//! loads a libary and search a specific operation
//! \param dest struct to store information
//! \param filename name of the libary
//! \param opSymbolName name of the opfn-struct containg the operation, wich should be loaded
void load_op(struct dynop *dest, std::string filename, std::string opSymbolName);
//! unloads the libary and cleans the struct
//! \param op struct to store pointer to loaded operation
void unload_op(struct dynop *op);
} // namespace detail
} // namespace yacx
} // namespace yacx
95 changes: 75 additions & 20 deletions src/cexecutor/CProgram.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,30 @@
#include <iostream>
#include <sstream>

using yacx::loglevel, yacx::CProgram, yacx::detail::load_op, yacx::detail::unload_op, yacx::detail::dynop;
using yacx::loglevel, yacx::CProgram, yacx::Options, yacx::detail::load_op, yacx::detail::unload_op, yacx::detail::dynop;

int CProgram::id = 0;

CProgram::CProgram(const char* cProgram, const char* functionName, int numberParameters,
const char* compilerWithOptions)
: m_numberArguments(numberParameters) {
Options createDefaultOptions(){
Options defaultOptions = Options();
defaultOptions.insert("-Wall");
defaultOptions.insert("-Wextra");
defaultOptions.insert("--pedantic");
return defaultOptions;
}

Options CProgram::DEFAULT_OPTIONS = createDefaultOptions();

CProgram::CProgram(const char* cProgram, const char* functionName, std::vector<std::string> &parameterTypes,
const char *compiler, Options &options) {
logger(loglevel::DEBUG) << "creating cProgram " << functionName << " with id: " << id
<< ",number of arguments: " << numberParameters << ", compiler: " << compilerWithOptions;
<< ", compiler: " << compiler;
logger(loglevel::DEBUG1) << "cFunction:\n" << cProgram;

id++;

m_numberArguments = parameterTypes.size();

//filename for srcFile
std::stringstream srcFileS;
srcFileS << "src_" << functionName << "_" << id << ".c";
Expand All @@ -31,11 +42,19 @@ CProgram::CProgram(const char* cProgram, const char* functionName, int numberPar

logger(loglevel::DEBUG1) << "compile it to " << m_srcFile << " and " << m_libFile;

//compilerCommand with options
std::stringstream compilerWithOptionsS;
compilerWithOptionsS << compiler << " ";
for (int i = 0; i < options.numOptions(); i++){
compilerWithOptionsS << options.content()[i] << " ";
}
std::string compilerWithOptions = compilerWithOptionsS.str();

//compile
compile(cProgram, functionName, numberParameters, compilerWithOptions);
compile(cProgram, functionName, parameterTypes, compilerWithOptions);

//open libary
load_op(&m_op, m_libFile.c_str());
load_op(&m_op, m_libFile, std::string("op") + functionName);
}

CProgram::~CProgram() {
Expand All @@ -47,11 +66,38 @@ CProgram::~CProgram() {
remove(m_libFile.c_str());
}

void CProgram::createSrcFile(const char* cProgram, const char* functionName, int numberParameters,
std::ofstream& fileOut) {
bool pointerArg(std::string &s){
for (std::string::reverse_iterator rit=s.rbegin(); rit!=s.rend(); ++rit){
if (*rit != ' '){
if (*rit == '*'){
return true;
} else {
return false;
}
}
}

throw std::runtime_error(std::string("invalid parameter type: ") + s);
}

void writeParam(std::vector<std::string> &parameterTypes, int i, std::ofstream& fileOut){
fileOut << "\n ";
if (pointerArg(parameterTypes[i])){
fileOut << "parameter[" << i << "]";
} else {
fileOut << "*((" << parameterTypes[i] << "*) " << "parameter[" << i << "])";
}
}

void CProgram::createSrcFile(const char* cProgram, const char* functionName,
std::vector<std::string> &parameterTypes, std::ofstream& fileOut) {
std::string executeFunctionName("execute");
executeFunctionName.append(functionName);

std::string opfnName("opfn");
opfnName.append(functionName);
std::string opName("op");
opName.append(functionName);

//write c function to be executed
fileOut << cProgram;
fileOut << "\n\n";
Expand All @@ -62,30 +108,32 @@ void CProgram::createSrcFile(const char* cProgram, const char* functionName, int
fileOut << ") {\n";
//run function to be executed
fileOut << " " << functionName << "(";
for (int i = 0; i < numberParameters-1; i++){
fileOut << "parameter[" << i << "], ";
int i = 0;
for (; i < parameterTypes.size()-1; i++){
writeParam(parameterTypes, i, fileOut);
fileOut << ",";
}
fileOut << "parameter[" << numberParameters-1 << "]";
writeParam(parameterTypes, i, fileOut);
fileOut << ");\n";
fileOut << "}\n\n";

//struct to store function pointer
fileOut << "struct opfn{ void (*op)(void** parameter);};\n\n";
fileOut << "struct " << opfnName << "{ void (*op)(void** parameter);};\n\n";

//create instance of struct as interface
fileOut << "struct opfn op = {.op = " << executeFunctionName << "};\n";
fileOut << "struct " << opfnName << " " << opName << " = {.op = " << executeFunctionName << "};\n";
}

void CProgram::compile(const char* cProgram, const char* functionName, int numberParameters,
const char* compilerWithOptions) {
void CProgram::compile(const char* cProgram, const char* functionName, std::vector<std::string> &parameterTypes,
std::string &compilerWithOptions) {
logger(loglevel::DEBUG) << "creating source file...";

//create and open output file
std::ofstream fileOut;
fileOut.open(m_srcFile);

//write srcfile
createSrcFile(cProgram, functionName, numberParameters, fileOut);
createSrcFile(cProgram, functionName, parameterTypes, fileOut);

//close file
fileOut.close();
Expand All @@ -106,7 +154,14 @@ void CProgram::compile(const char* cProgram, const char* functionName, int numbe
}
}

void CProgram::execute(void** arguments) {
void CProgram::execute(std::vector<void*> arguments) {
logger(loglevel::DEBUG) << "execute CProgram: " << m_srcFile;
m_op.op(arguments);

if (arguments.size() != m_numberArguments){
std::stringstream errorS;
errorS << "invalid number of arguments expected: " << m_numberArguments << " found: " << arguments.size();
throw std::runtime_error(errorS.str());
}

m_op.op(&arguments[0]);
}
11 changes: 6 additions & 5 deletions src/cexecutor/LibaryLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

using yacx::loglevel, yacx::detail::dynop, yacx::detail::opfn;

void yacx::detail::load_op(struct dynop *dest, const char *filename) {
void yacx::detail::load_op(struct dynop *dest, std::string filename, std::string opSymbolName) {
//open libary
void* handle = dlopen((std::string("./") + filename).c_str(), RTLD_LAZY);
char* error = dlerror();
Expand All @@ -25,16 +25,17 @@ void yacx::detail::load_op(struct dynop *dest, const char *filename) {
}

//Search op-struct in libary
void* op = dlsym(handle, "op");
void* op = dlsym(handle, opSymbolName.c_str());
error = dlerror();
if (op == NULL){
dlclose(handle);

if (error != NULL){
throw std::runtime_error(std::string("error while searching \"op\" in libary with"
"compiled function ") + error);
throw std::runtime_error(std::string("error while searching\"") + std::string(opSymbolName) +
std::string("\" in libary with compiled function ") + error);
} else {
throw std::runtime_error("error while searching \"op\" in libary with compiled function");
throw std::runtime_error(std::string("error while searching\"") + std::string(opSymbolName) +
std::string("\" in libary with compiled function "));
}
}

Expand Down
Loading

0 comments on commit 2cfb5e3

Please sign in to comment.