From cedd8c821a6891cc2f225e805a58b9c16e6554ff Mon Sep 17 00:00:00 2001
From: Soeren Peters <peters@irmb.tu-bs.de>
Date: Thu, 11 Mar 2021 10:09:11 +0000
Subject: [PATCH] Build lbm lib twice static and shared. Add a test executable.

---
 CMake/CMakeSetCompilerFlags.cmake             | 10 +++----
 CMake/VirtualFluidsMacros.cmake               |  8 ++---
 CMakeLists.txt                                |  1 +
 apps/cpu/sphere/CMakeLists.txt                |  2 +-
 src/cpu/VirtualFluidsCore/CMakeLists.txt      |  2 +-
 src/cpu/VirtualFluidsCore/LBM/D3Q27System.cpp | 29 +++++++++++++++++++
 src/cpu/VirtualFluidsCore/LBM/D3Q27System.h   | 24 +++++----------
 src/cpu/VirtualFluidsCore/LBM/ILBMKernel.h    |  4 ++-
 src/cpu/VirtualFluidsCore/LBM/LBMSystem.h     |  9 ++++--
 src/gpu/VirtualFluids_GPU/CMakeLists.txt      |  5 ++--
 src/lbm/CMakeLists.txt                        | 12 ++++++--
 src/lbm/{CalcMac.cu => CalcMac.cpp}           |  0
 src/lbm/CalcMac.h                             | 10 +++----
 src/lbm/CalcMacTests.cpp                      | 12 ++++++++
 src/lbmApp/CMakeLists.txt                     |  8 +++++
 src/lbmApp/main.cpp                           | 25 ++++++++++++++++
 16 files changed, 120 insertions(+), 41 deletions(-)
 rename src/lbm/{CalcMac.cu => CalcMac.cpp} (100%)
 create mode 100644 src/lbm/CalcMacTests.cpp
 create mode 100644 src/lbmApp/CMakeLists.txt
 create mode 100644 src/lbmApp/main.cpp

diff --git a/CMake/CMakeSetCompilerFlags.cmake b/CMake/CMakeSetCompilerFlags.cmake
index 2ea8c0b2f..26eeca2a0 100644
--- a/CMake/CMakeSetCompilerFlags.cmake
+++ b/CMake/CMakeSetCompilerFlags.cmake
@@ -47,7 +47,7 @@ endmacro()
 ################################################################
 ###             ADD_COMPILER_FLAGS_TO_PROJECT                ###
 ################################################################
-function(addAdditionalFlags project_name)
+function(addAdditionalFlags library_name)
 
     status_lib("additional compiler flags CXX: ${CS_COMPILER_FLAGS_CXX}")
     status_lib("additional compiler flags CXX debug: ${CS_COMPILER_FLAGS_CXX_DEBUG}")
@@ -67,18 +67,18 @@ function(addAdditionalFlags project_name)
 
     # compile options
     foreach(flag IN LISTS CS_COMPILER_FLAGS_CXX)
-        target_compile_options(${project_name} PRIVATE "$<$<COMPILE_LANGUAGE:CXX>:${flag}>")
+        target_compile_options(${library_name} PRIVATE "$<$<COMPILE_LANGUAGE:CXX>:${flag}>")
         if(MSVC)
-            target_compile_options(${project_name} PRIVATE "$<$<COMPILE_LANGUAGE:CUDA>:-Xcompiler=${flag}>")
+            target_compile_options(${library_name} PRIVATE "$<$<COMPILE_LANGUAGE:CUDA>:-Xcompiler=${flag}>")
         endif()
     endforeach()
 
     foreach(flag IN LISTS CS_COMPILER_FLAGS_CXX_DEBUG)
-        target_compile_options(${project_name} PRIVATE "$<$<AND:$<COMPILE_LANGUAGE:CXX>,$<CONFIG:DEBUG>>:${flag}>")
+        target_compile_options(${library_name} PRIVATE "$<$<AND:$<COMPILE_LANGUAGE:CXX>,$<CONFIG:DEBUG>>:${flag}>")
     endforeach()
 
     foreach(flag IN LISTS CS_COMPILER_FLAGS_CXX_RELEASE)
-        target_compile_options(${project_name} PRIVATE "$<$<AND:$<COMPILE_LANGUAGE:CXX>,$<CONFIG:RELEASE>>:${flag}>")
+        target_compile_options(${library_name} PRIVATE "$<$<AND:$<COMPILE_LANGUAGE:CXX>,$<CONFIG:RELEASE>>:${flag}>")
     endforeach()
 
 endfunction()
\ No newline at end of file
diff --git a/CMake/VirtualFluidsMacros.cmake b/CMake/VirtualFluidsMacros.cmake
index debb5ee78..62388cdf3 100644
--- a/CMake/VirtualFluidsMacros.cmake
+++ b/CMake/VirtualFluidsMacros.cmake
@@ -80,7 +80,7 @@ endfunction()
 #################################################################################
 function (vf_get_library_test_name library_test_name)
     vf_get_library_name (folder_name)
-    set (${library_test_name} ${library_name}Tests PARENT_SCOPE)
+    set (${library_test_name} ${folder_name}Tests PARENT_SCOPE)
 endfunction()
 
 
@@ -103,11 +103,11 @@ endfunction()
 function(vf_add_library)
 
     set( options )
-    set( oneValueArgs )
-    set( multiValueArgs NAME BUILDTYPE PUBLIC_LINK PRIVATE_LINK FILES FOLDER EXCLUDE)
+    set( oneValueArgs NAME BUILDTYPE)
+    set( multiValueArgs PUBLIC_LINK PRIVATE_LINK FILES FOLDER EXCLUDE)
     cmake_parse_arguments( ARG "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN} )
 
-    if(${ARG_NAME})
+    if(DEFINED ARG_NAME)
         set(library_name ${ARG_NAME})
     else()
         vf_get_library_name (library_name)
diff --git a/CMakeLists.txt b/CMakeLists.txt
index ce961049d..d3e51f8dc 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -88,6 +88,7 @@ find_package(MPI REQUIRED)
 
 add_subdirectory(src/basics)
 add_subdirectory(src/lbm)
+add_subdirectory(src/lbmApp)
 
 #################################################################################
 #  VIRTUAL FLUIDS CPU / GPU
diff --git a/apps/cpu/sphere/CMakeLists.txt b/apps/cpu/sphere/CMakeLists.txt
index d80737cdd..8346e80d2 100644
--- a/apps/cpu/sphere/CMakeLists.txt
+++ b/apps/cpu/sphere/CMakeLists.txt
@@ -1,6 +1,6 @@
 ########################################################
 ## C++ PROJECT                                       ###
 ########################################################
-PROJECT(sphere)
+PROJECT(sphere LANGUAGES CXX)
 
 vf_add_library(BUILDTYPE binary PRIVATE_LINK VirtualFluidsCore basics ${MPI_CXX_LIBRARIES} FILES sphere.cpp )
\ No newline at end of file
diff --git a/src/cpu/VirtualFluidsCore/CMakeLists.txt b/src/cpu/VirtualFluidsCore/CMakeLists.txt
index 6b1eb2af2..36ac278fb 100644
--- a/src/cpu/VirtualFluidsCore/CMakeLists.txt
+++ b/src/cpu/VirtualFluidsCore/CMakeLists.txt
@@ -25,7 +25,7 @@ if(BUILD_USE_OPENMP)
    list(APPEND VF_LIBRARIES OpenMP::OpenMP_CXX)
 endif()
 
-vf_add_library(BUILDTYPE static PUBLIC_LINK basics muparser MPI::MPI_CXX ${VF_LIBRARIES} lbm)
+vf_add_library(BUILDTYPE static PUBLIC_LINK basics muparser MPI::MPI_CXX ${VF_LIBRARIES} PRIVATE_LINK lbm)
 
 
 vf_get_library_name(library_name)
diff --git a/src/cpu/VirtualFluidsCore/LBM/D3Q27System.cpp b/src/cpu/VirtualFluidsCore/LBM/D3Q27System.cpp
index 5e88242fa..616bc1ea3 100644
--- a/src/cpu/VirtualFluidsCore/LBM/D3Q27System.cpp
+++ b/src/cpu/VirtualFluidsCore/LBM/D3Q27System.cpp
@@ -1,5 +1,7 @@
 #include "D3Q27System.h"
 
+#include "lbm/CalcMac.h"
+
 namespace D3Q27System
 {
 using namespace UbMath;
@@ -18,4 +20,31 @@ const int INVDIR[] = { INV_E,   INV_W,   INV_N,   INV_S,   INV_T,   INV_B,   INV
                        INV_NW,  INV_TE,  INV_BW,  INV_BE,  INV_TW,  INV_TN,  INV_BS,  INV_BN, INV_TS,
                        INV_TNE, INV_TNW, INV_TSE, INV_TSW, INV_BNE, INV_BNW, INV_BSE, INV_BSW };
 
+
+
+
+LBMReal getDensity(const LBMReal *const &f /*[27]*/)
+{
+    return LBM::getDensity(f);
+}
+
+LBMReal getIncompVelocityX1(const LBMReal *const &f /*[27]*/)
+{
+    return LBM::getIncompVelocityX1(f);
+}
+
+LBMReal getIncompVelocityX2(const LBMReal *const &f /*[27]*/)
+{
+    return LBM::getIncompVelocityX2(f);
+}
+
+LBMReal getIncompVelocityX3(const LBMReal *const &f /*[27]*/)
+{
+    return LBM::getIncompVelocityX3(f);
+}
+
+
+
+
+
 } // namespace D3Q27System
\ No newline at end of file
diff --git a/src/cpu/VirtualFluidsCore/LBM/D3Q27System.h b/src/cpu/VirtualFluidsCore/LBM/D3Q27System.h
index d3b553084..04fe8a819 100644
--- a/src/cpu/VirtualFluidsCore/LBM/D3Q27System.h
+++ b/src/cpu/VirtualFluidsCore/LBM/D3Q27System.h
@@ -42,7 +42,6 @@
 #include "UbException.h"
 #include "UbMath.h"
 
-#include "lbm/CalcMac.h"
 
 //! \brief namespace for global system-functions
 namespace D3Q27System
@@ -149,30 +148,21 @@ static const int ET_BNW = 11;
 static const int ET_TSW = 12;
 static const int ET_BNE = 12;
 
+
 //////////////////////////////////////////////////////////////////////////
 // MACROSCOPIC VALUES
 /*=====================================================================*/
-static LBMReal getDensity(const LBMReal *const &f /*[27]*/)
-{
-    return LBM::getDensity(f);
-}
+LBMReal getDensity(const LBMReal *const &f /*[27]*/);
 /*=====================================================================*/
 static LBMReal getPressure(const LBMReal *const &f /*[27]*/) { return REAL_CAST(UbMath::c1o3) * getDensity(f); }
 /*=====================================================================*/
-static LBMReal getIncompVelocityX1(const LBMReal *const &f /*[27]*/)
-{
-    return LBM::getIncompVelocityX1(f);
-}
+LBMReal getIncompVelocityX1(const LBMReal *const &f /*[27]*/);
 /*=====================================================================*/
-static LBMReal getIncompVelocityX2(const LBMReal *const &f /*[27]*/)
-{
-    return LBM::getIncompVelocityX2(f);
-}
+LBMReal getIncompVelocityX2(const LBMReal *const &f /*[27]*/);
 /*=====================================================================*/
-static LBMReal getIncompVelocityX3(const LBMReal *const &f /*[27]*/)
-{
-    return LBM::getIncompVelocityX3(f);
-}
+LBMReal getIncompVelocityX3(const LBMReal *const &f /*[27]*/);
+
+
 /*=====================================================================*/
 static void calcDensity(const LBMReal *const &f /*[27]*/, LBMReal &rho)
 {
diff --git a/src/cpu/VirtualFluidsCore/LBM/ILBMKernel.h b/src/cpu/VirtualFluidsCore/LBM/ILBMKernel.h
index 4dbe8eee0..bde61d9d3 100644
--- a/src/cpu/VirtualFluidsCore/LBM/ILBMKernel.h
+++ b/src/cpu/VirtualFluidsCore/LBM/ILBMKernel.h
@@ -36,6 +36,8 @@
 
 #include <PointerDefinitions.h>
 
+#include "LBMSystem.h"
+
 class BCProcessor;
 class DataSet3D;
 
@@ -57,7 +59,7 @@ public:
     virtual void setCollisionFactor(double collFactor)                               = 0;
     virtual bool isInsideOfDomain(const int &x1, const int &x2, const int &x3) const = 0;
     virtual int getGhostLayerWidth() const                                           = 0;
-    virtual double getDeltaT() const                                                 = 0;
+    virtual LBMReal getDeltaT() const                                                = 0;
     virtual bool getWithForcing() const                                              = 0;
 };
 
diff --git a/src/cpu/VirtualFluidsCore/LBM/LBMSystem.h b/src/cpu/VirtualFluidsCore/LBM/LBMSystem.h
index d24775aef..14b4d223b 100644
--- a/src/cpu/VirtualFluidsCore/LBM/LBMSystem.h
+++ b/src/cpu/VirtualFluidsCore/LBM/LBMSystem.h
@@ -37,6 +37,8 @@
 #include <iostream>
 #include <string>
 
+#include "basics/Core/DataTypes.h"
+
 //! \brief namespace for global system-functions
 
 namespace LBMSystem
@@ -45,10 +47,10 @@ namespace LBMSystem
 //#define SINGLEPRECISION
 
 #ifdef SINGLEPRECISION
-typedef float real;
+//using real = float;
 #define REAL_CAST(x) ((LBMSystem::real)(x))
 #else
-using real = double;
+//using real = double;
 #define REAL_CAST(x) (x)
 #endif
 
@@ -82,6 +84,7 @@ static real calcOmega2(real viscosity, real deltaT) { return REAL_CAST(1.0 / (4.
 } // namespace LBMSystem
 
 // some typedefs for global namespace
-using LBMReal = LBMSystem::real;
+//using LBMReal = LBMSystem::real;
+using LBMReal = real;
 
 #endif
diff --git a/src/gpu/VirtualFluids_GPU/CMakeLists.txt b/src/gpu/VirtualFluids_GPU/CMakeLists.txt
index aa313a3e7..50ec20005 100644
--- a/src/gpu/VirtualFluids_GPU/CMakeLists.txt
+++ b/src/gpu/VirtualFluids_GPU/CMakeLists.txt
@@ -5,7 +5,7 @@ if(MSVC)
     set(additional_libraries ws2_32 Traffic) # ws_32 throws an error on Phoenix
 endif()
 
-vf_add_library(PRIVATE_LINK ${additional_libraries} GridGenerator basics lbm MPI::MPI_CXX)
+vf_add_library(BUILDTYPE static PRIVATE_LINK ${additional_libraries} GridGenerator basics MPI::MPI_CXX PUBLIC_LINK lbmStatic)
 
 linkBoost(COMPONENTS "serialization")
 
@@ -13,5 +13,6 @@ linkBoost(COMPONENTS "serialization")
 #https://stackoverflow.com/questions/6832666/lnk2019-when-including-asio-headers-solution-generated-with-cmake
 #https://stackoverflow.com/questions/27442885/syntax-error-with-stdnumeric-limitsmax
 
-
+#set_target_properties(VirtualFluids_GPU PROPERTIES CUDA_RESOLVE_DEVICE_SYMBOLS ON)
 set_target_properties(VirtualFluids_GPU PROPERTIES CUDA_SEPARABLE_COMPILATION ON)
+#set_target_properties(VirtualFluids_GPU PROPERTIES POSITION_INDEPENDENT_CODE ON)
\ No newline at end of file
diff --git a/src/lbm/CMakeLists.txt b/src/lbm/CMakeLists.txt
index c3895812c..d436a48a5 100644
--- a/src/lbm/CMakeLists.txt
+++ b/src/lbm/CMakeLists.txt
@@ -4,8 +4,16 @@ if(BUILD_VF_GPU)
     enable_language(CUDA)
 endif()
 
-vf_add_library(PUBLIC_LINK basics)
+vf_add_library(BUILDTYPE shared PUBLIC_LINK basics)
+vf_add_library(NAME lbmStatic BUILDTYPE static PUBLIC_LINK basics)
 
 if(BUILD_VF_GPU)
-set_target_properties(lbm PROPERTIES CUDA_SEPARABLE_COMPILATION ON)
+    #set_target_properties(lbm PROPERTIES CUDA_RESOLVE_DEVICE_SYMBOLS ON)
+    set_target_properties(lbmStatic PROPERTIES CUDA_SEPARABLE_COMPILATION ON)
+    #set_target_properties(lbm PROPERTIES POSITION_INDEPENDENT_CODE ON)
+    set_source_files_properties(CalcMac.cpp PROPERTIES LANGUAGE CUDA)
 endif()
+
+
+vf_add_tests()
+
diff --git a/src/lbm/CalcMac.cu b/src/lbm/CalcMac.cpp
similarity index 100%
rename from src/lbm/CalcMac.cu
rename to src/lbm/CalcMac.cpp
diff --git a/src/lbm/CalcMac.h b/src/lbm/CalcMac.h
index fa7e83bd5..a9356ce4f 100644
--- a/src/lbm/CalcMac.h
+++ b/src/lbm/CalcMac.h
@@ -1,10 +1,7 @@
 #ifndef LBM_CALCMAC_H
 #define LBM_CALCMAC_H
 
-#include "Core/DataTypes.h"
-
 #ifdef __CUDACC__
-#pragma message ( "C Preprocessor got here!" )
 #include <cuda_runtime.h>
 #else
 #ifndef __host__
@@ -13,9 +10,12 @@
 #ifndef __device__
 #define __device__
 #endif
-#endif 
+#endif
+
+#include "Core/DataTypes.h"
+#include "lbm_export.h"
 
-class LBM
+class LBM_EXPORT LBM
 {
 public:
 
diff --git a/src/lbm/CalcMacTests.cpp b/src/lbm/CalcMacTests.cpp
new file mode 100644
index 000000000..eeaeb993e
--- /dev/null
+++ b/src/lbm/CalcMacTests.cpp
@@ -0,0 +1,12 @@
+#include <gmock/gmock.h>
+
+#include "CalcMac.h"
+
+TEST(AAA,BBB)
+{
+    real f[27] = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
+
+    double density = LBM::getDensity(f);
+
+    ASSERT_THAT(density, testing::DoubleEq(1));
+}
\ No newline at end of file
diff --git a/src/lbmApp/CMakeLists.txt b/src/lbmApp/CMakeLists.txt
new file mode 100644
index 000000000..0bd8dc446
--- /dev/null
+++ b/src/lbmApp/CMakeLists.txt
@@ -0,0 +1,8 @@
+PROJECT(LBMAPP LANGUAGES CUDA CXX)
+
+vf_add_library(BUILDTYPE binary PRIVATE_LINK lbmStatic FILES main.cpp)
+
+set_source_files_properties(main.cpp PROPERTIES LANGUAGE CUDA)
+
+
+set_target_properties(lbmApp PROPERTIES CUDA_SEPARABLE_COMPILATION ON)
\ No newline at end of file
diff --git a/src/lbmApp/main.cpp b/src/lbmApp/main.cpp
new file mode 100644
index 000000000..16bca8434
--- /dev/null
+++ b/src/lbmApp/main.cpp
@@ -0,0 +1,25 @@
+#include <iostream>
+#include <stdio.h>
+
+#include "lbm/CalcMac.h"
+#include <cuda_runtime.h>
+
+__global__ void test()
+{
+    printf("Hello World from GPU!\n");
+    real f[27] = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
+
+    double density = LBM::getDensity(f);
+
+    printf("Hello density: %f \n", density);
+}
+
+int main()
+{
+    std::cout << "hello world \n";
+
+    test<<<1,1>>>();
+    cudaDeviceSynchronize();
+
+    return 0;
+}
\ No newline at end of file
-- 
GitLab