From 247c1e8c21ac54246dd27e880bb0bc29b0ca0d7c Mon Sep 17 00:00:00 2001 From: Deukhyun Cha Date: Tue, 28 Mar 2023 22:01:58 -0500 Subject: [PATCH] Add kernel/include_occa and kernel/link_occa properties to control including and linking occa into kernels --- examples/cpp/18_nonblocking_streams/main.cpp | 1 + src/loops/typelessForLoop.cpp | 2 ++ src/occa/internal/lang/modes/serial.cpp | 11 ++++-- src/occa/internal/modes/cuda/device.cpp | 19 ++++++++--- src/occa/internal/modes/hip/device.cpp | 21 ++++++++---- src/occa/internal/modes/serial/device.cpp | 35 ++++++++++++++------ tests/src/c/kernel.cpp | 2 +- tests/src/core/kernel.cpp | 3 +- tests/src/math/fpMath.cpp | 27 ++++++++------- tests/src/math/intMath.cpp | 19 ++++++----- 10 files changed, 93 insertions(+), 47 deletions(-) diff --git a/examples/cpp/18_nonblocking_streams/main.cpp b/examples/cpp/18_nonblocking_streams/main.cpp index cd26e960b..f6b1cb954 100644 --- a/examples/cpp/18_nonblocking_streams/main.cpp +++ b/examples/cpp/18_nonblocking_streams/main.cpp @@ -27,6 +27,7 @@ int main(int argc, const char **argv) { occa::json kernelProps({ {"defines/block", block}, {"defines/group", group}, + {"serial/include_std", true}, }); occa::kernel powerOfPi = occa::buildKernel("powerOfPi.okl", "powerOfPi", diff --git a/src/loops/typelessForLoop.cpp b/src/loops/typelessForLoop.cpp index f269aacc3..335390134 100644 --- a/src/loops/typelessForLoop.cpp +++ b/src/loops/typelessForLoop.cpp @@ -34,6 +34,8 @@ namespace occa { loopScope.device = device; } + loopScope.props["kernel/include_occa"] = true; + const int outerIterationCount = (int) outerIterations.size(); const int innerIterationCount = (int) innerIterations.size(); diff --git a/src/occa/internal/lang/modes/serial.cpp b/src/occa/internal/lang/modes/serial.cpp index c7d044f9a..3a41b290c 100644 --- a/src/occa/internal/lang/modes/serial.cpp +++ b/src/occa/internal/lang/modes/serial.cpp @@ -34,8 +34,11 @@ namespace occa { void serialParser::setupHeaders() { strVector headers; - const bool includingStd = settings.get("serial/include_std", true); - headers.push_back("include \n"); + const bool includeOcca = settings.get("kernel/include_occa", false); + if (includeOcca) { + headers.push_back("include \n"); + } + const bool includingStd = settings.get("serial/include_std", false); if (includingStd) { headers.push_back("include "); headers.push_back("include "); @@ -51,7 +54,9 @@ namespace occa { if (includingStd) { header += "\nusing namespace std;"; } - header += "\nusing namespace occa;"; + if (includeOcca) { + header += "\nusing namespace occa;"; + } } directiveToken token(root.source->origin, header); diff --git a/src/occa/internal/modes/cuda/device.cpp b/src/occa/internal/modes/cuda/device.cpp index b9dd01e01..fb412c179 100644 --- a/src/occa/internal/modes/cuda/device.cpp +++ b/src/occa/internal/modes/cuda/device.cpp @@ -107,7 +107,8 @@ namespace occa { return ( occa::hash(props["compiler"]) ^ props["compiler_flags"] - ^ props["compiler_env_script"] + ^ props["kernel/include_occa"] + ^ props["kernel/link_occa"] ); } @@ -287,6 +288,9 @@ namespace occa { sys::addCompilerLibraryFlags(compilerFlags); } + const bool includeOcca = kernelProps.get("kernel/include_occa", false); + const bool linkOcca = kernelProps.get("kernel/link_occa", false); + //---[ Compiling Command ]-------- std::stringstream command; command << allProps["compiler"] @@ -295,10 +299,15 @@ namespace occa { #if (OCCA_OS == OCCA_WINDOWS_OS) << " -D OCCA_OS=OCCA_WINDOWS_OS -D _MSC_VER=1800" #endif - << " -I" << env::OCCA_DIR << "include" - << " -I" << env::OCCA_INSTALL_DIR << "include" - << " -L" << env::OCCA_INSTALL_DIR << "lib -locca" - << " -x cu " << sourceFilename + ; + if (includeOcca) { + command << " -I" << env::OCCA_DIR << "include" + << " -I" << env::OCCA_INSTALL_DIR << "include"; + } + if (linkOcca) { + command << " -L" << env::OCCA_INSTALL_DIR << "lib -locca"; + } + command << " -x cu " << sourceFilename << " -o " << binaryFilename << " 2>&1"; diff --git a/src/occa/internal/modes/hip/device.cpp b/src/occa/internal/modes/hip/device.cpp index 047740e99..dd6894970 100644 --- a/src/occa/internal/modes/hip/device.cpp +++ b/src/occa/internal/modes/hip/device.cpp @@ -105,6 +105,9 @@ namespace occa { occa::hash(props["compiler"]) ^ props["compiler_flags"] ^ props["compiler_env_script"] + ^ props["hipcc_compiler_flags"] + ^ props["kernel/include_occa"] + ^ props["kernel/link_occa"] ); } @@ -282,14 +285,20 @@ namespace occa { #else << " -f=\\\"" << compilerFlags << "\\\"" #endif - << ' ' << hipccCompilerFlags + << ' ' << hipccCompilerFlags; #if defined(__HIP_PLATFORM_NVCC___) || (HIP_VERSION >= 305) - << " -I" << env::OCCA_DIR << "include" - << " -I" << env::OCCA_INSTALL_DIR << "include" + const bool includeOcca = kernelProps.get("kernel/include_occa", false); + const bool linkOcca = kernelProps.get("kernel/link_occa", false); + if (includeOcca) { + command << " -I" << env::OCCA_DIR << "include" + << " -I" << env::OCCA_INSTALL_DIR << "include"; + } + if (linkOcca) { + /* NC: hipcc doesn't seem to like linking a library in */ + //<< " -L" << env::OCCA_INSTALL_DIR << "lib -locca"; + } #endif - /* NC: hipcc doesn't seem to like linking a library in */ - //<< " -L" << env::OCCA_INSTALL_DIR << "lib -locca" - << ' ' << sourceFilename + command << ' ' << sourceFilename << " -o " << binaryFilename << " 2>&1"; diff --git a/src/occa/internal/modes/serial/device.cpp b/src/occa/internal/modes/serial/device.cpp index 5a5fc3949..abb062efb 100644 --- a/src/occa/internal/modes/serial/device.cpp +++ b/src/occa/internal/modes/serial/device.cpp @@ -36,6 +36,8 @@ namespace occa { ^ props["compiler_language"] ^ props["compiler_linker_flags"] ^ props["compiler_shared_flags"] + ^ props["include_occa"] + ^ props["link_occa"] ); } @@ -315,6 +317,9 @@ namespace occa { sys::addCompilerLibraryFlags(compilerFlags); } + const bool includeOcca = kernelProps.get("kernel/include_occa", isLauncherKernel); + const bool linkOcca = kernelProps.get("kernel/link_occa", isLauncherKernel); + io::stageFile( binaryFilename, true, @@ -323,11 +328,15 @@ namespace occa { command << compiler << ' ' << compilerFlags << ' ' << sourceFilename - << " -o " << tempFilename - << " -I" << env::OCCA_DIR << "include" - << " -I" << env::OCCA_INSTALL_DIR << "include" - << " -L" << env::OCCA_INSTALL_DIR << "lib -locca" - << ' ' << compilerLinkerFlags + << " -o " << tempFilename; + if (includeOcca) { + command << " -I" << env::OCCA_DIR << "include" + << " -I" << env::OCCA_INSTALL_DIR << "include"; + } + if (linkOcca) { + command << " -L" << env::OCCA_INSTALL_DIR << "lib -locca"; + } + command << ' ' << compilerLinkerFlags << " 2>&1" << std::endl; #else @@ -336,12 +345,16 @@ namespace occa { << " /D OCCA_OS=OCCA_WINDOWS_OS" << " /EHsc" << " /wd4244 /wd4800 /wd4804 /wd4018" - << ' ' << compilerFlags - << " /I" << env::OCCA_DIR << "include" - << " /I" << env::OCCA_INSTALL_DIR << "include" - << ' ' << sourceFilename - << " /link " << env::OCCA_INSTALL_DIR << "lib/libocca.lib", - << ' ' << compilerLinkerFlags + << ' ' << compilerFlags; + if (includeOcca) { + command << " /I" << env::OCCA_DIR << "include" + << " /I" << env::OCCA_INSTALL_DIR << "include"; + } + command << ' ' << sourceFilename; + if (linkOcca) { + command << " /link " << env::OCCA_INSTALL_DIR << "lib/libocca.lib"; + } + command << ' ' << compilerLinkerFlags << " /OUT:" << tempFilename << std::endl; #endif diff --git a/tests/src/c/kernel.cpp b/tests/src/c/kernel.cpp index dbb719e67..ab7b92ca6 100644 --- a/tests/src/c/kernel.cpp +++ b/tests/src/c/kernel.cpp @@ -87,7 +87,7 @@ void testRun() { occa::env::OCCA_DIR + "tests/files/argKernel.okl" ); occaJson kernelProps = occaJsonParse( - "{type_validation: false}" + "{type_validation: false, serial: {include_std: true}}" ); occaKernel argKernel = ( occaBuildKernel(argKernelFile.c_str(), diff --git a/tests/src/core/kernel.cpp b/tests/src/core/kernel.cpp index 0cc21db00..7dbac9270 100644 --- a/tests/src/core/kernel.cpp +++ b/tests/src/core/kernel.cpp @@ -154,7 +154,8 @@ void testRun() { ); occa::kernel argKernel = occa::buildKernel(argKernelFile, "argKernel", - {{"type_validation", false}}); + {{"type_validation", false}, + {"serial/include_std", true}}); argKernel.setRunDims(occa::dim(1, 1, 1), occa::dim(1, 1, 1)); diff --git a/tests/src/math/fpMath.cpp b/tests/src/math/fpMath.cpp index e333e735c..91a8dc99f 100644 --- a/tests/src/math/fpMath.cpp +++ b/tests/src/math/fpMath.cpp @@ -55,45 +55,48 @@ std::string kernel_back_half = void testUnaryFunctions(const occa::device& d) { for (auto fp_type : arg_types) { - std::string arg_decl = + std::string arg_decl = " " + fp_type + " " + unary_args + ";\n"; for(auto func : unary_functions) { - std::string function_call = + std::string function_call = " " + fp_type + " w = " + func + "(" + unary_args + ");\n"; - std::string kernel_src = + std::string kernel_src = kernel_front_half + arg_decl + function_call +kernel_back_half; - occa::kernel k = d.buildKernelFromString(kernel_src,"f"); + occa::kernel k = d.buildKernelFromString(kernel_src, "f", + {{"serial/include_std", true}}); } } } void testBinaryFunctions(const occa::device& d) { for (auto fp_type : arg_types) { - std::string arg_decl = + std::string arg_decl = " " + fp_type + " " + binary_args + ";\n"; for(auto func : binary_functions) { - std::string function_call = + std::string function_call = " " + fp_type + " w = " + func + "(" + binary_args + ");\n"; - std::string kernel_src = + std::string kernel_src = kernel_front_half + arg_decl + function_call +kernel_back_half; - occa::kernel k = d.buildKernelFromString(kernel_src,"f"); + occa::kernel k = d.buildKernelFromString(kernel_src, "f", + {{"serial/include_std", true}}); } } } void testTernaryFunctions(const occa::device& d) { for (auto fp_type : arg_types) { - std::string arg_decl = + std::string arg_decl = " " + fp_type + " " + ternary_args + ";\n"; for(auto func : ternary_functions) { - std::string function_call = + std::string function_call = " " + fp_type + " w = " + func + "(" + ternary_args + ");\n"; - std::string kernel_src = + std::string kernel_src = kernel_front_half + arg_decl + function_call +kernel_back_half; - occa::kernel k = d.buildKernelFromString(kernel_src,"f"); + occa::kernel k = d.buildKernelFromString(kernel_src, "f", + {{"serial/include_std", true}}); } } } diff --git a/tests/src/math/intMath.cpp b/tests/src/math/intMath.cpp index aa1f76095..520ae5d14 100644 --- a/tests/src/math/intMath.cpp +++ b/tests/src/math/intMath.cpp @@ -29,30 +29,33 @@ std::string kernel_back_half = void testUnaryFunctions(const occa::device& d) { for (auto&& int_type : arg_types) { - const std::string arg_decl = + const std::string arg_decl = " " + int_type + " " + unary_args + "; \n"; for (auto&& func : unary_functions) { - const std::string function_call = + const std::string function_call = " " + int_type + " w = " + func + "(" + unary_args + "); \n"; - const std::string kernel_src = + const std::string kernel_src = kernel_front_half + arg_decl + function_call +kernel_back_half; - occa::kernel k = d.buildKernelFromString(kernel_src,"f"); + occa::kernel k = d.buildKernelFromString(kernel_src, "f", + {{"serial/include_std", true}}); } } } void testBinaryFunctions(const occa::device& d) { for (auto&& int_type : arg_types) { - const std::string arg_decl = + const std::string arg_decl = " " + int_type + " " + binary_args + "; \n"; for (auto&& func : binary_functions) { - const std::string function_call = + const std::string function_call = " " + int_type + " w = " + func + "(" + binary_args + "); \n"; - const std::string kernel_src = + const std::string kernel_src = kernel_front_half + arg_decl + function_call +kernel_back_half; - occa::kernel k = d.buildKernelFromString(kernel_src,"f"); + occa::kernel k = d.buildKernelFromString(kernel_src, "f", + {{"serial/include_std", true}, + {"kernel/include_occa", true}}); // For min/max } } }