From 55209d22fedd5a0ea5323e81356cfe0c88603f30 Mon Sep 17 00:00:00 2001 From: ruki Date: Thu, 8 Oct 2020 23:05:45 +0800 Subject: add openmp example --- tests/projects/openmp/hello/src/main.c | 11 +++++++++++ tests/projects/openmp/hello/xmake.lua | 3 +++ 2 files changed, 14 insertions(+) create mode 100644 tests/projects/openmp/hello/src/main.c create mode 100644 tests/projects/openmp/hello/xmake.lua diff --git a/tests/projects/openmp/hello/src/main.c b/tests/projects/openmp/hello/src/main.c new file mode 100644 index 000000000..cea215566 --- /dev/null +++ b/tests/projects/openmp/hello/src/main.c @@ -0,0 +1,11 @@ +#include +#include + +int main(int argc, char** argv) +{ + #pragma omp parallel + { + printf("hello %d\n", omp_get_thread_num()); + } + return 0; +} diff --git a/tests/projects/openmp/hello/xmake.lua b/tests/projects/openmp/hello/xmake.lua new file mode 100644 index 000000000..685a6a34b --- /dev/null +++ b/tests/projects/openmp/hello/xmake.lua @@ -0,0 +1,3 @@ +target("hello") + set_kind("binary") + add_files("src/*.c") -- cgit v1.3.1 From c79cdeab5474507647f1c29b2cb759550f98e8b6 Mon Sep 17 00:00:00 2001 From: ruki Date: Mon, 12 Oct 2020 22:59:59 +0800 Subject: add libomp for openmp examples --- tests/projects/openmp/hello/src/main.c | 2 +- tests/projects/openmp/hello/xmake.lua | 3 ++ .../sandbox/modules/import/core/tool/compiler.lua | 21 +++++++++-- xmake/rules/c++/openmp/load.lua | 43 ++++++++++++++++++++++ xmake/rules/c++/openmp/xmake.lua | 31 ++++++++++++++++ 5 files changed, 95 insertions(+), 5 deletions(-) create mode 100644 xmake/rules/c++/openmp/load.lua create mode 100644 xmake/rules/c++/openmp/xmake.lua diff --git a/tests/projects/openmp/hello/src/main.c b/tests/projects/openmp/hello/src/main.c index cea215566..54ba9db96 100644 --- a/tests/projects/openmp/hello/src/main.c +++ b/tests/projects/openmp/hello/src/main.c @@ -5,7 +5,7 @@ int main(int argc, char** argv) { #pragma omp parallel { - printf("hello %d\n", omp_get_thread_num()); + printf("hello %d\n", omp_get_thread_num()); } return 0; } diff --git a/tests/projects/openmp/hello/xmake.lua b/tests/projects/openmp/hello/xmake.lua index 685a6a34b..c1f837287 100644 --- a/tests/projects/openmp/hello/xmake.lua +++ b/tests/projects/openmp/hello/xmake.lua @@ -1,3 +1,6 @@ +add_requires("libomp", {optional = true}) target("hello") set_kind("binary") add_files("src/*.c") + add_rules("c.openmp") + add_packages("libomp") diff --git a/xmake/core/sandbox/modules/import/core/tool/compiler.lua b/xmake/core/sandbox/modules/import/core/tool/compiler.lua index a60a8401a..ef7bb4371 100644 --- a/xmake/core/sandbox/modules/import/core/tool/compiler.lua +++ b/xmake/core/sandbox/modules/import/core/tool/compiler.lua @@ -32,14 +32,10 @@ local sandbox = require("sandbox/sandbox") -- load the compiler from the given source kind function sandbox_core_tool_compiler.load(sourcekind, opt) - - -- get the compiler instance local instance, errors = compiler.load(sourcekind, opt and opt.target or nil) if not instance then raise(errors) end - - -- ok return instance end @@ -166,6 +162,23 @@ function sandbox_core_tool_compiler.build(sourcefiles, targetfile, opt) end end +-- get the current compiler name of given language kind +-- +-- @param langkind the language kind, e.g. c, cxx, mm, mxx, swift, go, rust, d, as +-- +-- @return the compiler name +-- +function sandbox_core_tool_compiler.name(langkind, opt) + + -- get sourcekind from the language kind + local sourcekind = language.langkinds()[langkind] + assert(sourcekind, "unknown language kind: " .. langkind) + + -- get the compiler instance + local instance = sandbox_core_tool_compiler.load(sourcekind, opt) + return instance:name() +end + -- get all compiler features -- -- @param langkind the language kind, e.g. c, cxx, mm, mxx, swift, go, rust, d, as diff --git a/xmake/rules/c++/openmp/load.lua b/xmake/rules/c++/openmp/load.lua new file mode 100644 index 000000000..6678e46e3 --- /dev/null +++ b/xmake/rules/c++/openmp/load.lua @@ -0,0 +1,43 @@ +--!A cross-platform build utility based on Lua +-- +-- Licensed under the Apache License, Version 2.0 (the "License"); +-- you may not use this file except in compliance with the License. +-- You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, software +-- distributed under the License is distributed on an "AS IS" BASIS, +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +-- See the License for the specific language governing permissions and +-- limitations under the License. +-- +-- Copyright (C) 2015-2020, TBOOX Open Source Group. +-- +-- @author ruki +-- @file load.lua +-- + +-- imports +import("core.tool.compiler") + +-- main entry +function main(target, langkind) + local compiler_name = compiler.name(langkind) + local flag_name = langkind .. "flags" + if compiler_name == "cl" then + target:add(flag_name, "/openmp") + elseif compiler_name == "clang" or compiler_name == "clangxx" then + if target:is_plat("macosx") then + target:add(flag_name, "-Xpreprocessor -fopenmp") + else + target:add(flag_name, "-fopenmp") + end + elseif compiler_name == "gcc" or compiler_name == "gxx" then + target:add(flag_name, "-fopenmp") + elseif compiler_name == "icc" or compiler_name == "icpc" then + target:add(flag_name, "-qopenmp") + elseif compiler_name == "icl" then + target:add(flag_name, "-Qopenmp") + end +end diff --git a/xmake/rules/c++/openmp/xmake.lua b/xmake/rules/c++/openmp/xmake.lua new file mode 100644 index 000000000..6a43810ad --- /dev/null +++ b/xmake/rules/c++/openmp/xmake.lua @@ -0,0 +1,31 @@ +--!A cross-platform build utility based on Lua +-- +-- Licensed under the Apache License, Version 2.0 (the "License"); +-- you may not use this file except in compliance with the License. +-- You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, software +-- distributed under the License is distributed on an "AS IS" BASIS, +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +-- See the License for the specific language governing permissions and +-- limitations under the License. +-- +-- Copyright (C) 2015-2020, TBOOX Open Source Group. +-- +-- @author ruki +-- @file xmake.lua +-- + +-- define rule: c.openmp +rule("c.openmp") + on_load(function (target) + import("load")(target, "c") + end) + +-- define rule: c++.openmp +rule("c++.openmp") + on_load(function (target) + import("load")(target, "cxx") + end) -- cgit v1.3.1 From d7c0299b40a37fcf32c3f3a0a71ec60c60e4bd07 Mon Sep 17 00:00:00 2001 From: ruki Date: Mon, 12 Oct 2020 23:06:15 +0800 Subject: add loop tests for openmp --- tests/projects/openmp/loop/src/main.cpp | 12 ++++++++++++ tests/projects/openmp/loop/xmake.lua | 6 ++++++ 2 files changed, 18 insertions(+) create mode 100644 tests/projects/openmp/loop/src/main.cpp create mode 100644 tests/projects/openmp/loop/xmake.lua diff --git a/tests/projects/openmp/loop/src/main.cpp b/tests/projects/openmp/loop/src/main.cpp new file mode 100644 index 000000000..273a8538e --- /dev/null +++ b/tests/projects/openmp/loop/src/main.cpp @@ -0,0 +1,12 @@ +#include +#include + +int main(int argc, char** argv) +{ + #pragma omp parallel for + for (int i = 0; i < 10; ++i) + { + printf("hello(%d): i = %d\n", omp_get_thread_num(), i); + } + return 0; +} diff --git a/tests/projects/openmp/loop/xmake.lua b/tests/projects/openmp/loop/xmake.lua new file mode 100644 index 000000000..eb14be1a3 --- /dev/null +++ b/tests/projects/openmp/loop/xmake.lua @@ -0,0 +1,6 @@ +add_requires("libomp", {optional = true}) +target("loop") + set_kind("binary") + add_files("src/*.cpp") + add_rules("c++.openmp") + add_packages("libomp") -- cgit v1.3.1