From d8a93c5379ab0951c10e433cf99a6b81ce9b2be9 Mon Sep 17 00:00:00 2001 From: Ma Pengfei Date: Sat, 26 Feb 2022 16:31:50 +0800 Subject: [PATCH 1/6] add algebra solver and finish the nonlinear solver graph. --- .gitignore | 2 + nonlinear.zsg | 2884 ++++++----------- .../Algebra/AlgebraSolver/BiCGSTAB.h | 143 + .../Algebra/AlgebraSolver/CMakeLists.txt | 2 + .../Algebra/AlgebraSolver/ConjugateGradient.h | 53 + .../Algebra/AlgebraSolver/GenericVector.h | 183 ++ .../Algebra/AlgebraSolver/GpuVector.h | 225 ++ .../Algebra/AlgebraSolver/LinearProblem.h | 37 + .../Algebra/AlgebraSolver/LinearSolver.h | 74 + .../Functional/Algebra/AlgebraSolver/MINRES.h | 10 + .../AlgebraSolver/NewtonLineSearchSolver.h | 13 + .../Algebra/AlgebraSolver/NewtonSolver.h | 101 + .../Algebra/AlgebraSolver/NonlinearProblem.h | 109 + .../Algebra/AlgebraSolver/NonlinearSolver.h | 96 + .../Algebra/AlgebraSolver/StdVector.h | 152 + projects/Functional/Algebra/CMakeLists.txt | 46 + .../Functional/Algebra/GPULIB/BasicMesh.cu | 0 .../Functional/Algebra/GPULIB/CMakeLists.txt | 33 + .../Functional/Algebra/GPULIB/GpuVector.cu | 173 + .../Algebra/GPULIB/ImmersedBoudnaryMethod.cpp | 111 + .../Algebra/GPULIB/ImmersedBoundaryMethod.cu | 262 ++ .../Algebra/GPULIB/ImmersedBoundaryMethod.h | 32 + .../Algebra/GPULIB/PiecewisePolynomial.cu | 184 ++ .../Algebra/GPULIB/include/double_math.h | 74 + .../Algebra/GPULIB/include/gpu_lib.h | 75 + .../Algebra/GPULIB/include/test_gpu_vector.h | 17 + .../Algebra/GPULIB/test_gpu_vector.cu | 77 + .../Functional/Algebra/GPULIB/utilities.cu | 52 + .../Functional/Algebra/GPULIB/utilities.h | 30 + projects/Functional/Algebra/Timer.h | 44 + .../Functional/Algebra/loguru/CMakeLists.txt | 3 + projects/Functional/Algebra/loguru/loguru.cpp | 2005 ++++++++++++ projects/Functional/Algebra/loguru/loguru.hpp | 1445 +++++++++ projects/Functional/Algebra/main.cpp | 71 + projects/Functional/CMakeLists.txt | 15 +- projects/Functional/NonlinearSolver.cpp | 198 +- tests/CMakeLists.txt | 3 - tests/catch.cpp | 2 - tests/test_any.cpp | 56 - 39 files changed, 7148 insertions(+), 1944 deletions(-) create mode 100644 projects/Functional/Algebra/AlgebraSolver/BiCGSTAB.h create mode 100644 projects/Functional/Algebra/AlgebraSolver/CMakeLists.txt create mode 100644 projects/Functional/Algebra/AlgebraSolver/ConjugateGradient.h create mode 100644 projects/Functional/Algebra/AlgebraSolver/GenericVector.h create mode 100644 projects/Functional/Algebra/AlgebraSolver/GpuVector.h create mode 100644 projects/Functional/Algebra/AlgebraSolver/LinearProblem.h create mode 100644 projects/Functional/Algebra/AlgebraSolver/LinearSolver.h create mode 100644 projects/Functional/Algebra/AlgebraSolver/MINRES.h create mode 100644 projects/Functional/Algebra/AlgebraSolver/NewtonLineSearchSolver.h create mode 100644 projects/Functional/Algebra/AlgebraSolver/NewtonSolver.h create mode 100644 projects/Functional/Algebra/AlgebraSolver/NonlinearProblem.h create mode 100644 projects/Functional/Algebra/AlgebraSolver/NonlinearSolver.h create mode 100644 projects/Functional/Algebra/AlgebraSolver/StdVector.h create mode 100644 projects/Functional/Algebra/CMakeLists.txt create mode 100644 projects/Functional/Algebra/GPULIB/BasicMesh.cu create mode 100644 projects/Functional/Algebra/GPULIB/CMakeLists.txt create mode 100644 projects/Functional/Algebra/GPULIB/GpuVector.cu create mode 100644 projects/Functional/Algebra/GPULIB/ImmersedBoudnaryMethod.cpp create mode 100644 projects/Functional/Algebra/GPULIB/ImmersedBoundaryMethod.cu create mode 100644 projects/Functional/Algebra/GPULIB/ImmersedBoundaryMethod.h create mode 100644 projects/Functional/Algebra/GPULIB/PiecewisePolynomial.cu create mode 100644 projects/Functional/Algebra/GPULIB/include/double_math.h create mode 100644 projects/Functional/Algebra/GPULIB/include/gpu_lib.h create mode 100644 projects/Functional/Algebra/GPULIB/include/test_gpu_vector.h create mode 100644 projects/Functional/Algebra/GPULIB/test_gpu_vector.cu create mode 100644 projects/Functional/Algebra/GPULIB/utilities.cu create mode 100644 projects/Functional/Algebra/GPULIB/utilities.h create mode 100644 projects/Functional/Algebra/Timer.h create mode 100644 projects/Functional/Algebra/loguru/CMakeLists.txt create mode 100644 projects/Functional/Algebra/loguru/loguru.cpp create mode 100644 projects/Functional/Algebra/loguru/loguru.hpp create mode 100644 projects/Functional/Algebra/main.cpp delete mode 100644 tests/CMakeLists.txt delete mode 100644 tests/catch.cpp delete mode 100644 tests/test_any.cpp diff --git a/.gitignore b/.gitignore index 7a6de2ad0f..a3d6ad52ae 100644 --- a/.gitignore +++ b/.gitignore @@ -68,3 +68,5 @@ projects/Euler # xinlei files assets/upper_slate_v01 assets/character + +projects/Functional/Algebra/GPULIB/cuda-samples/Common/helper_timer.h diff --git a/nonlinear.zsg b/nonlinear.zsg index 0efae99d1b..fd1611b1bd 100644 --- a/nonlinear.zsg +++ b/nonlinear.zsg @@ -2,11 +2,11 @@ "graph": { "main": { "nodes": { - "e1d74bcd-CalculateResidual2": { - "name": "CalculateResidual2", + "1b9b8c32-CalculateResidual": { + "name": "CalculateResidual", "inputs": { "args": [ - "71e17506-FuncBegin", + "e83899a7-FuncBegin", "args", null ], @@ -18,14 +18,19 @@ }, "params": {}, "uipos": [ - 37.36754966887429, - 176.7384105960265 + 167.18541987169112, + 689.0947667081855 ], "options": [] }, - "fa9f69fd-MakeJFNKSolver2": { - "name": "MakeJFNKSolver2", + "e83899a7-FuncBegin": { + "name": "FuncBegin", "inputs": { + "extraArgs": [ + null, + null, + null + ], "SRC": [ null, null, @@ -34,17 +39,22 @@ }, "params": {}, "uipos": [ - 646.3576158940394, - 179.76821192052978 + -31.94034056802775, + 915.9211704855329 ], "options": [] }, - "564e9642-MakeNonlinearProblemObject2": { - "name": "MakeNonlinearProblemObject2", + "82a64254-FuncEnd": { + "name": "FuncEnd", "inputs": { - "function": [ - "c16225c4-FuncEnd", - "function", + "rets": [ + "1b9b8c32-CalculateResidual", + "rets", + null + ], + "FUNC": [ + "e83899a7-FuncBegin", + "FUNC", null ], "SRC": [ @@ -55,29 +65,14 @@ }, "params": {}, "uipos": [ - 655.4470198675498, - 422.15231788079467 + 268.05965943197236, + 915.9211704855329 ], "options": [] }, - "679fe11c-SolveNonlinearProblem2": { - "name": "SolveNonlinearProblem2", + "c227fd2f-MakeJFNKSolver": { + "name": "MakeJFNKSolver", "inputs": { - "JFNKSolverObject2": [ - null, - null, - null - ], - "NonlinearProblemObject2": [ - null, - null, - null - ], - "function": [ - null, - null, - null - ], "SRC": [ null, null, @@ -86,17 +81,19 @@ }, "params": {}, "uipos": [ - 912.9801324503312, - 180.77814569536417 + 532.6876457800946, + 696.0303855025977 ], - "options": [] + "options": [ + "ONCE" + ] }, - "71e17506-FuncBegin": { - "name": "FuncBegin", + "7211c4a7-MakeNonlinearProblemObject": { + "name": "MakeNonlinearProblemObject", "inputs": { - "extraArgs": [ - null, - null, + "function": [ + "82a64254-FuncEnd", + "function", null ], "SRC": [ @@ -107,22 +104,29 @@ }, "params": {}, "uipos": [ - -267.6324503311259, - 365.59602649006627 + 520.6057345172964, + 985.190795058912 ], - "options": [] + "options": [ + "ONCE" + ] }, - "c16225c4-FuncEnd": { - "name": "FuncEnd", + "f09bc162-SolveNonlinearProblem": { + "name": "SolveNonlinearProblem", "inputs": { - "rets": [ - "e1d74bcd-CalculateResidual2", - "rets", + "JFNKSolverObject": [ + "c227fd2f-MakeJFNKSolver", + "JFNKSolverObject", null ], - "FUNC": [ - "71e17506-FuncBegin", - "FUNC", + "NonlinearProblemObject": [ + "7211c4a7-MakeNonlinearProblemObject", + "NonlinearProblemObject", + null + ], + "RawDoubleObject": [ + "86a3b179-MakeRawDoubleObject", + "RawDoubleObject", null ], "SRC": [ @@ -133,27 +137,37 @@ }, "params": {}, "uipos": [ - 315.14900662251654, - 358.52649006622516 + 841.5818437323131, + 1180.9177575162494 ], "options": [] }, - "d5d6d49d-SolveNonlinearProblem2": { - "name": "SolveNonlinearProblem2", + "86a3b179-MakeRawDoubleObject": { + "name": "MakeRawDoubleObject", "inputs": { - "JFNKSolverObject2": [ - "fa9f69fd-MakeJFNKSolver2", - "JFNKSolverObject2", - null - ], - "NonlinearProblemObject2": [ - "564e9642-MakeNonlinearProblemObject2", - "NonlinearProblemObject2", + "SRC": [ + null, + null, null - ], - "function": [ - "c16225c4-FuncEnd", - "function", + ] + }, + "params": { + "value": 2 + }, + "uipos": [ + 392.9402055070574, + 1212.9348223626657 + ], + "options": [ + "ONCE" + ] + }, + "28e77680-PrintRawDoubleObject": { + "name": "PrintRawDoubleObject", + "inputs": { + "RawDoubleObject": [ + "f09bc162-SolveNonlinearProblem", + "RawDoubleObject", null ], "SRC": [ @@ -164,8 +178,8 @@ }, "params": {}, "uipos": [ - 932.1688741721855, - 390.84437086092714 + 1160.0581030150756, + 1224.3640075376884 ], "options": [ "VIEW" @@ -173,10 +187,10 @@ } }, "view_rect": { - "x": 0.0, - "y": 0.0, - "width": 1200.0, - "height": 610.0 + "x": -282.0665515892738, + "y": 544.7659543646841, + "width": 1950.0, + "height": 884.9999999999985 } } }, @@ -678,7 +692,7 @@ "control" ] }, - "CalculateResidual2": { + "CalculateResidual": { "inputs": [ [ "", @@ -1506,16 +1520,21 @@ "functional" ] }, - "GLBindFramebufferTexture": { + "GeoVertexVel": { "inputs": [ [ "", - "framebuffer", + "dt", + "" + ], + [ + "", + "TargetMesh", "" ], [ "", - "texture", + "OriginMesh", "" ], [ @@ -1527,7 +1546,7 @@ "outputs": [ [ "", - "framebuffer", + "MeshVel", "" ], [ @@ -1538,21 +1557,11 @@ ], "params": [], "categories": [ - "EasyGL" + "trimesh" ] }, - "GLClearColor": { + "GetFrameNum": { "inputs": [ - [ - "", - "color", - "" - ], - [ - "", - "alpha", - "" - ], [ "", "SRC", @@ -1560,6 +1569,11 @@ ] ], "outputs": [ + [ + "", + "FrameNum", + "" + ], [ "", "DST", @@ -1568,10 +1582,10 @@ ], "params": [], "categories": [ - "EasyGL" + "frame" ] }, - "GLCreateFramebuffer": { + "GetFramePortion": { "inputs": [ [ "", @@ -1582,7 +1596,7 @@ "outputs": [ [ "", - "framebuffer", + "FramePortion", "" ], [ @@ -1593,16 +1607,11 @@ ], "params": [], "categories": [ - "EasyGL" + "frame" ] }, - "GLCreateProgram": { + "GetFrameTime": { "inputs": [ - [ - "", - "shaderList", - "" - ], [ "", "SRC", @@ -1612,7 +1621,7 @@ "outputs": [ [ "", - "program", + "time", "" ], [ @@ -1623,16 +1632,11 @@ ], "params": [], "categories": [ - "EasyGL" + "frame" ] }, - "GLCreateShader": { + "GetFrameTimeElapsed": { "inputs": [ - [ - "", - "source", - "" - ], [ "", "SRC", @@ -1642,7 +1646,7 @@ "outputs": [ [ "", - "shader", + "time", "" ], [ @@ -1651,18 +1655,12 @@ "" ] ], - "params": [ - [ - "string", - "type", - "vertex" - ] - ], + "params": [], "categories": [ - "EasyGL" + "frame" ] }, - "GLCreateTexture": { + "GetTime": { "inputs": [ [ "", @@ -1673,7 +1671,7 @@ "outputs": [ [ "", - "texture", + "time", "" ], [ @@ -1684,14 +1682,14 @@ ], "params": [], "categories": [ - "EasyGL" + "frame" ] }, - "GLDrawArrayTriangles": { + "GetUserData": { "inputs": [ [ "", - "prim", + "object", "" ], [ @@ -1701,22 +1699,48 @@ ] ], "outputs": [ + [ + "", + "data", + "" + ], + [ + "bool", + "hasValue", + "" + ], [ "", "DST", "" ] ], - "params": [], + "params": [ + [ + "string", + "key", + "" + ] + ], "categories": [ - "EasyGL" + "portal" ] }, - "GLGetFramebufferTexture": { + "IfElse": { "inputs": [ [ "", - "framebuffer", + "true", + "" + ], + [ + "", + "false", + "" + ], + [ + "bool", + "cond", "" ], [ @@ -1728,7 +1752,7 @@ "outputs": [ [ "", - "texture", + "result", "" ], [ @@ -1739,11 +1763,16 @@ ], "params": [], "categories": [ - "EasyGL" + "control" ] }, - "GLNoFramebuffer": { + "ImportObjMesh": { "inputs": [ + [ + "", + "path", + "" + ], [ "", "SRC", @@ -1753,7 +1782,7 @@ "outputs": [ [ "", - "framebuffer", + "mesh", "" ], [ @@ -1764,19 +1793,14 @@ ], "params": [], "categories": [ - "EasyGL" + "trimesh" ] }, - "GLUTMainLoop": { + "ImportObjPrimitive": { "inputs": [ [ - "", - "drawFunc", - "" - ], - [ - "", - "resolution", + "readpath", + "path", "" ], [ @@ -1788,26 +1812,25 @@ "outputs": [ [ "", - "DST", + "prim", "" - ] - ], - "params": [ + ], [ - "int", - "interval", - "17 0" + "", + "DST", + "" ] ], + "params": [], "categories": [ - "EasyGL" + "primitive" ] }, - "GLUseFramebuffer": { + "ImportParticles": { "inputs": [ [ "", - "framebuffer", + "path", "" ], [ @@ -1817,6 +1840,11 @@ ] ], "outputs": [ + [ + "", + "pars", + "" + ], [ "", "DST", @@ -1825,14 +1853,14 @@ ], "params": [], "categories": [ - "EasyGL" + "particles" ] }, - "GLUseProgram": { + "ImportZpmPrimitive": { "inputs": [ [ - "", - "program", + "readpath", + "path", "" ], [ @@ -1842,6 +1870,11 @@ ] ], "outputs": [ + [ + "PrimitiveObject", + "prim", + "" + ], [ "", "DST", @@ -1850,19 +1883,14 @@ ], "params": [], "categories": [ - "EasyGL" + "primitive" ] }, - "GLUseTexture": { + "IntegrateFrameTime": { "inputs": [ [ "", - "texture", - "" - ], - [ - "", - "index", + "desired_dt", "" ], [ @@ -1872,32 +1900,38 @@ ] ], "outputs": [ + [ + "", + "actual_dt", + "" + ], [ "", "DST", "" ] ], - "params": [], + "params": [ + [ + "float", + "min_scale", + "0.0001" + ] + ], "categories": [ - "EasyGL" + "frame" ] }, - "GeoVertexVel": { + "ListGetItem": { "inputs": [ [ "", - "dt", - "" - ], - [ - "", - "TargetMesh", + "list", "" ], [ - "", - "OriginMesh", + "int", + "index", "" ], [ @@ -1909,7 +1943,7 @@ "outputs": [ [ "", - "MeshVel", + "object", "" ], [ @@ -1920,11 +1954,16 @@ ], "params": [], "categories": [ - "trimesh" + "list" ] }, - "GetFrameNum": { + "ListLength": { "inputs": [ + [ + "", + "list", + "" + ], [ "", "SRC", @@ -1934,7 +1973,7 @@ "outputs": [ [ "", - "FrameNum", + "length", "" ], [ @@ -1945,630 +1984,20 @@ ], "params": [], "categories": [ - "frame" + "list" ] }, - "GetFramePortion": { + "Make1DLinePrimitive": { "inputs": [ [ - "", - "SRC", - "" - ] - ], - "outputs": [ - [ - "", - "FramePortion", - "" - ], - [ - "", - "DST", - "" - ] - ], - "params": [], - "categories": [ - "frame" - ] - }, - "GetFrameTime": { - "inputs": [ - [ - "", - "SRC", - "" - ] - ], - "outputs": [ - [ - "", - "time", - "" - ], - [ - "", - "DST", - "" - ] - ], - "params": [], - "categories": [ - "frame" - ] - }, - "GetFrameTimeElapsed": { - "inputs": [ - [ - "", - "SRC", - "" - ] - ], - "outputs": [ - [ - "", - "time", - "" - ], - [ - "", - "DST", - "" - ] - ], - "params": [], - "categories": [ - "frame" - ] - }, - "GetTime": { - "inputs": [ - [ - "", - "SRC", - "" - ] - ], - "outputs": [ - [ - "", - "time", - "" - ], - [ - "", - "DST", - "" - ] - ], - "params": [], - "categories": [ - "frame" - ] - }, - "GetUserData": { - "inputs": [ - [ - "", - "object", - "" - ], - [ - "", - "SRC", - "" - ] - ], - "outputs": [ - [ - "", - "data", - "" - ], - [ - "bool", - "hasValue", - "" - ], - [ - "", - "DST", - "" - ] - ], - "params": [ - [ - "string", - "key", - "" - ] - ], - "categories": [ - "portal" - ] - }, - "IfElse": { - "inputs": [ - [ - "", - "true", - "" - ], - [ - "", - "false", - "" - ], - [ - "bool", - "cond", - "" - ], - [ - "", - "SRC", - "" - ] - ], - "outputs": [ - [ - "", - "result", - "" - ], - [ - "", - "DST", - "" - ] - ], - "params": [], - "categories": [ - "control" - ] - }, - "ImportObjMesh": { - "inputs": [ - [ - "", - "path", - "" - ], - [ - "", - "SRC", - "" - ] - ], - "outputs": [ - [ - "", - "mesh", - "" - ], - [ - "", - "DST", - "" - ] - ], - "params": [], - "categories": [ - "trimesh" - ] - }, - "ImportObjPrimitive": { - "inputs": [ - [ - "readpath", - "path", - "" - ], - [ - "", - "SRC", - "" - ] - ], - "outputs": [ - [ - "", - "prim", - "" - ], - [ - "", - "DST", - "" - ] - ], - "params": [], - "categories": [ - "primitive" - ] - }, - "ImportParticles": { - "inputs": [ - [ - "", - "path", - "" - ], - [ - "", - "SRC", - "" - ] - ], - "outputs": [ - [ - "", - "pars", - "" - ], - [ - "", - "DST", - "" - ] - ], - "params": [], - "categories": [ - "particles" - ] - }, - "ImportZpmPrimitive": { - "inputs": [ - [ - "readpath", - "path", - "" - ], - [ - "", - "SRC", - "" - ] - ], - "outputs": [ - [ - "PrimitiveObject", - "prim", - "" - ], - [ - "", - "DST", - "" - ] - ], - "params": [], - "categories": [ - "primitive" - ] - }, - "IntegrateFrameTime": { - "inputs": [ - [ - "", - "desired_dt", - "" - ], - [ - "", - "SRC", - "" - ] - ], - "outputs": [ - [ - "", - "actual_dt", - "" - ], - [ - "", - "DST", - "" - ] - ], - "params": [ - [ - "float", - "min_scale", - "0.0001" - ] - ], - "categories": [ - "frame" - ] - }, - "ListGetItem": { - "inputs": [ - [ - "", - "list", - "" - ], - [ - "int", - "index", - "" - ], - [ - "", - "SRC", - "" - ] - ], - "outputs": [ - [ - "", - "object", - "" - ], - [ - "", - "DST", - "" - ] - ], - "params": [], - "categories": [ - "list" - ] - }, - "ListLength": { - "inputs": [ - [ - "", - "list", - "" - ], - [ - "", - "SRC", - "" - ] - ], - "outputs": [ - [ - "", - "length", - "" - ], - [ - "", - "DST", - "" - ] - ], - "params": [], - "categories": [ - "list" - ] - }, - "Make1DLinePrimitive": { - "inputs": [ - [ - "int", - "n", - "2" - ], - [ - "vec3f", - "direction", - "1,0,0" - ], - [ - "float", - "scale", - "1" - ], - [ - "vec3f", - "origin", - "0,0,0" - ], - [ - "", - "SRC", - "" - ] - ], - "outputs": [ - [ - "PrimitiveObject", - "prim", - "" - ], - [ - "", - "DST", - "" - ] - ], - "params": [ - [ - "enum X Y Z", - "Direction", - "X" - ], - [ - "bool", - "isCentered", - "0" - ], - [ - "bool", - "hasLines", - "1" - ] - ], - "categories": [ - "primitive" - ] - }, - "Make2DGridPrimitive": { - "inputs": [ - [ - "int", - "nx", - "2" - ], - [ - "int", - "ny", - "0" - ], - [ - "vec3f", - "sizeX", - "1,0,0" - ], - [ - "vec3f", - "sizeY", - "0,1,0" - ], - [ - "float", - "scale", - "1" - ], - [ - "vec3f", - "origin", - "0,0,0" - ], - [ - "", - "SRC", - "" - ] - ], - "outputs": [ - [ - "PrimitiveObject", - "prim", - "" - ], - [ - "", - "DST", - "" - ] - ], - "params": [ - [ - "enum XZ XY YZ", - "Direction", - "XZ" - ], - [ - "bool", - "isCentered", - "0" - ], - [ - "bool", - "hasFaces", - "1" - ] - ], - "categories": [ - "primitive" - ] - }, - "Make3DGridPointsInAABB": { - "inputs": [ - [ - "int", - "nx", - "4" - ], - [ - "int", - "ny", - "0" - ], - [ - "int", - "nz", - "0" - ], - [ - "vec3f", - "bmin", - "-1,-1,-1" - ], - [ - "vec3f", - "bmax", - "1,1,1" - ], - [ - "", - "SRC", - "" - ] - ], - "outputs": [ - [ - "", - "prim", - "" - ], - [ - "", - "DST", - "" - ] - ], - "params": [ - [ - "bool", - "isStaggered", - "1" - ] - ], - "categories": [ - "primitive" - ] - }, - "Make3DGridPrimitive": { - "inputs": [ - [ - "int", - "nx", - "2" - ], - [ - "int", - "ny", - "0" - ], - [ - "int", - "nz", - "0" - ], - [ - "vec3f", - "sizeX", - "1,0,0" - ], - [ - "vec3f", - "sizeY", - "0,1,0" + "int", + "n", + "2" ], [ "vec3f", - "sizeZ", - "0,0,1" + "direction", + "1,0,0" ], [ "float", @@ -2588,7 +2017,7 @@ ], "outputs": [ [ - "", + "PrimitiveObject", "prim", "" ], @@ -2599,119 +2028,58 @@ ] ], "params": [ + [ + "enum X Y Z", + "Direction", + "X" + ], [ "bool", "isCentered", "0" - ] - ], - "categories": [ - "primitive" - ] - }, - "MakeBoxPrimitive": { - "inputs": [ - [ - "float", - "size_x", - "2.0" - ], - [ - "float", - "size_y", - "2.0" - ], - [ - "float", - "size_z", - "2.0" - ], - [ - "vec3f", - "origin", - "0,0,0" - ], - [ - "", - "SRC", - "" - ] - ], - "outputs": [ - [ - "", - "prim", - "" ], - [ - "", - "DST", - "" - ] - ], - "params": [ [ "bool", - "use_quads", - "0" + "hasLines", + "1" ] ], "categories": [ "primitive" ] }, - "MakeCubePrimitive": { + "Make2DGridPrimitive": { "inputs": [ [ - "", - "spacing", - "" - ], - [ - "", + "int", "nx", - "" + "2" ], [ - "", + "int", "ny", - "" + "0" ], [ - "", - "nz", - "" + "vec3f", + "sizeX", + "1,0,0" ], [ - "", - "origin", - "" + "vec3f", + "sizeY", + "0,1,0" ], [ - "", - "SRC", - "" - ] - ], - "outputs": [ - [ - "", - "prim", - "" + "float", + "scale", + "1" ], [ - "", - "DST", - "" - ] - ], - "params": [], - "categories": [ - "primitive" - ] - }, - "MakeCurvemap": { - "inputs": [ + "vec3f", + "origin", + "0,0,0" + ], [ "", "SRC", @@ -2720,8 +2088,8 @@ ], "outputs": [ [ - "", - "curvemap", + "PrimitiveObject", + "prim", "" ], [ @@ -2732,144 +2100,52 @@ ], "params": [ [ - "float", - "input_min", - "0" - ], - [ - "float", - "input_max", - "1" + "enum XZ XY YZ", + "Direction", + "XZ" ], [ - "float", - "output_min", + "bool", + "isCentered", "0" ], [ - "float", - "output_max", + "bool", + "hasFaces", "1" ] ], "categories": [ - "numeric" - ] - }, - "MakeDict": { - "inputs": [ - [ - "", - "SRC", - "" - ] - ], - "outputs": [ - [ - "DictObject", - "dict", - "" - ], - [ - "", - "DST", - "" - ] - ], - "params": [], - "categories": [ - "dict" + "primitive" ] }, - "MakeFullscreenRect": { + "Make3DGridPointsInAABB": { "inputs": [ [ - "", - "SRC", - "" - ] - ], - "outputs": [ - [ - "", - "prim", - "" + "int", + "nx", + "4" ], [ - "", - "DST", - "" - ] - ], - "params": [], - "categories": [ - "EasyGL" - ] - }, - "MakeGCTest": { - "inputs": [ - [ - "", - "SRC", - "" - ] - ], - "outputs": [ - [ - "", - "value", - "" + "int", + "ny", + "0" ], - [ - "", - "DST", - "" - ] - ], - "params": [ [ "int", - "value", - "42" - ] - ], - "categories": [ - "debug" - ] - }, - "MakeHeatmap": { - "inputs": [ - [ - "", - "SRC", - "" - ] - ], - "outputs": [ - [ - "", - "heatmap", - "" + "nz", + "0" ], [ - "", - "DST", - "" - ] - ], - "params": [ - [ - "int", - "nres", - "1024" - ] - ], - "categories": [ - "visualize" - ] - }, - "MakeJFNKSolver2": { - "inputs": [ + "vec3f", + "bmin", + "-1,-1,-1" + ], + [ + "vec3f", + "bmax", + "1,1,1" + ], [ "", "SRC", @@ -2879,7 +2155,7 @@ "outputs": [ [ "", - "JFNKSolverObject2", + "prim", "" ], [ @@ -2888,13 +2164,59 @@ "" ] ], - "params": [], + "params": [ + [ + "bool", + "isStaggered", + "1" + ] + ], "categories": [ - "Zentricle" + "primitive" ] }, - "MakeList": { + "Make3DGridPrimitive": { "inputs": [ + [ + "int", + "nx", + "2" + ], + [ + "int", + "ny", + "0" + ], + [ + "int", + "nz", + "0" + ], + [ + "vec3f", + "sizeX", + "1,0,0" + ], + [ + "vec3f", + "sizeY", + "0,1,0" + ], + [ + "vec3f", + "sizeZ", + "0,0,1" + ], + [ + "float", + "scale", + "1" + ], + [ + "vec3f", + "origin", + "0,0,0" + ], [ "", "SRC", @@ -2904,7 +2226,7 @@ "outputs": [ [ "", - "list", + "prim", "" ], [ @@ -2913,13 +2235,39 @@ "" ] ], - "params": [], + "params": [ + [ + "bool", + "isCentered", + "0" + ] + ], "categories": [ - "list" + "primitive" ] }, - "MakeMultilineString": { + "MakeBoxPrimitive": { "inputs": [ + [ + "float", + "size_x", + "2.0" + ], + [ + "float", + "size_y", + "2.0" + ], + [ + "float", + "size_z", + "2.0" + ], + [ + "vec3f", + "origin", + "0,0,0" + ], [ "", "SRC", @@ -2928,8 +2276,8 @@ ], "outputs": [ [ - "string", - "value", + "", + "prim", "" ], [ @@ -2940,50 +2288,40 @@ ], "params": [ [ - "multiline_string", - "value", - "" + "bool", + "use_quads", + "0" ] ], "categories": [ - "string" + "primitive" ] }, - "MakeMutable": { + "MakeCubePrimitive": { "inputs": [ [ "", - "anyobj", + "spacing", "" ], [ "", - "SRC", + "nx", "" - ] - ], - "outputs": [ + ], [ "", - "mutable", + "ny", "" ], [ "", - "DST", + "nz", "" - ] - ], - "params": [], - "categories": [ - "control" - ] - }, - "MakeNonlinearProblemObject2": { - "inputs": [ + ], [ "", - "function", + "origin", "" ], [ @@ -2995,7 +2333,7 @@ "outputs": [ [ "", - "NonlinearProblemObject2", + "prim", "" ], [ @@ -3006,21 +2344,11 @@ ], "params": [], "categories": [ - "Zentricle" + "primitive" ] }, - "MakeOrthonormalBase": { + "MakeCurvemap": { "inputs": [ - [ - "vec3f", - "normal", - "0,0,1" - ], - [ - "vec3f", - "tangent", - "0,1,0" - ], [ "", "SRC", @@ -3029,18 +2357,8 @@ ], "outputs": [ [ - "vec3f", - "normal", - "" - ], - [ - "vec3f", - "tangent", - "" - ], - [ - "vec3f", - "bitangent", + "", + "curvemap", "" ], [ @@ -3049,48 +2367,34 @@ "" ] ], - "params": [], - "categories": [ - "math" - ] - }, - "MakePrimitive": { - "inputs": [ + "params": [ [ - "int", - "size", - "" + "float", + "input_min", + "0" ], [ - "", - "SRC", - "" - ] - ], - "outputs": [ + "float", + "input_max", + "1" + ], [ - "", - "prim", - "" + "float", + "output_min", + "0" ], [ - "", - "DST", - "" + "float", + "output_max", + "1" ] ], - "params": [], "categories": [ - "primitive" + "numeric" ] }, - "MakePrimitiveFromList": { + "MakeDict": { "inputs": [ - [ - "ListObject", - "list", - "" - ], [ "", "SRC", @@ -3099,8 +2403,8 @@ ], "outputs": [ [ - "", - "prim", + "DictObject", + "dict", "" ], [ @@ -3111,10 +2415,10 @@ ], "params": [], "categories": [ - "primitive" + "dict" ] }, - "MakeReadPath": { + "MakeGCTest": { "inputs": [ [ "", @@ -3124,8 +2428,8 @@ ], "outputs": [ [ - "string", - "path", + "", + "value", "" ], [ @@ -3136,16 +2440,16 @@ ], "params": [ [ - "readpath", - "path", - "" + "int", + "value", + "42" ] ], "categories": [ - "string" + "debug" ] }, - "MakeSimpleTriangle": { + "MakeHeatmap": { "inputs": [ [ "", @@ -3156,7 +2460,7 @@ "outputs": [ [ "", - "prim", + "heatmap", "" ], [ @@ -3165,43 +2469,44 @@ "" ] ], - "params": [], + "params": [ + [ + "int", + "nres", + "1024" + ] + ], "categories": [ - "EasyGL" + "visualize" ] }, - "MakeSmallList": { + "MakeJFNKSolver": { "inputs": [ [ "", - "obj0", - "" - ], - [ - "", - "obj1", - "" - ], - [ - "", - "obj2", - "" - ], - [ - "", - "obj3", + "SRC", "" - ], + ] + ], + "outputs": [ [ "", - "obj4", + "JFNKSolverObject", "" ], [ "", - "obj5", + "DST", "" - ], + ] + ], + "params": [], + "categories": [ + "Zentricle" + ] + }, + "MakeList": { + "inputs": [ [ "", "SRC", @@ -3220,18 +2525,12 @@ "" ] ], - "params": [ - [ - "bool", - "doConcat", - "1" - ] - ], + "params": [], "categories": [ "list" ] }, - "MakeString": { + "MakeMultilineString": { "inputs": [ [ "", @@ -3253,7 +2552,7 @@ ], "params": [ [ - "string", + "multiline_string", "value", "" ] @@ -3262,27 +2561,12 @@ "string" ] }, - "MakeVisualAABBPrimitive": { + "MakeMutable": { "inputs": [ [ - "float", - "dx", - "1" - ], - [ - "vec3f", - "boundMin", - "-0.5,-0.5,-0.5" - ], - [ - "vec3f", - "boundMax", - "0.5,0.5,0.5" - ], - [ - "int", - "OpenTop", - "0" + "", + "anyobj", + "" ], [ "", @@ -3293,7 +2577,7 @@ "outputs": [ [ "", - "prim", + "mutable", "" ], [ @@ -3302,19 +2586,18 @@ "" ] ], - "params": [ - [ - "enum points edges trifaces quadfaces", - "type", - "edges" - ] - ], + "params": [], "categories": [ - "visualize" + "control" ] }, - "MakeWritePath": { + "MakeNonlinearProblemObject": { "inputs": [ + [ + "", + "function", + "" + ], [ "", "SRC", @@ -3323,8 +2606,8 @@ ], "outputs": [ [ - "string", - "path", + "", + "NonlinearProblemObject", "" ], [ @@ -3333,28 +2616,22 @@ "" ] ], - "params": [ - [ - "writepath", - "path", - "" - ] - ], + "params": [], "categories": [ - "string" + "Zentricle" ] }, - "MeshCopy": { + "MakeOrthonormalBase": { "inputs": [ [ - "", - "copyFrom", - "" + "vec3f", + "normal", + "0,0,1" ], [ - "", - "copyTo", - "" + "vec3f", + "tangent", + "0,1,0" ], [ "", @@ -3363,6 +2640,21 @@ ] ], "outputs": [ + [ + "vec3f", + "normal", + "" + ], + [ + "vec3f", + "tangent", + "" + ], + [ + "vec3f", + "bitangent", + "" + ], [ "", "DST", @@ -3371,24 +2663,14 @@ ], "params": [], "categories": [ - "trimesh" + "math" ] }, - "MeshMix": { + "MakePrimitive": { "inputs": [ [ - "", - "meshA", - "" - ], - [ - "", - "meshB", - "" - ], - [ - "", - "coef", + "int", + "size", "" ], [ @@ -3400,7 +2682,7 @@ "outputs": [ [ "", - "mesh", + "prim", "" ], [ @@ -3411,14 +2693,14 @@ ], "params": [], "categories": [ - "trimesh" + "primitive" ] }, - "MeshToPrimitive": { + "MakePrimitiveFromList": { "inputs": [ [ - "", - "mesh", + "ListObject", + "list", "" ], [ @@ -3444,18 +2726,8 @@ "primitive" ] }, - "MoveAssign": { + "MakeRawDoubleObject": { "inputs": [ - [ - "", - "dst", - "" - ], - [ - "", - "src", - "" - ], [ "", "SRC", @@ -3465,7 +2737,7 @@ "outputs": [ [ "", - "dst", + "RawDoubleObject", "" ], [ @@ -3474,18 +2746,19 @@ "" ] ], - "params": [], + "params": [ + [ + "int", + "value", + "0" + ] + ], "categories": [ - "portal" + "Zentricle" ] }, - "MoveClone": { + "MakeReadPath": { "inputs": [ - [ - "", - "object", - "" - ], [ "", "SRC", @@ -3494,8 +2767,8 @@ ], "outputs": [ [ - "", - "newObject", + "string", + "path", "" ], [ @@ -3504,16 +2777,47 @@ "" ] ], - "params": [], + "params": [ + [ + "readpath", + "path", + "" + ] + ], "categories": [ - "portal" + "string" ] }, - "MoveDelete": { + "MakeSmallList": { "inputs": [ [ "", - "object", + "obj0", + "" + ], + [ + "", + "obj1", + "" + ], + [ + "", + "obj2", + "" + ], + [ + "", + "obj3", + "" + ], + [ + "", + "obj4", + "" + ], + [ + "", + "obj5", "" ], [ @@ -3523,18 +2827,29 @@ ] ], "outputs": [ + [ + "", + "list", + "" + ], [ "", "DST", "" ] ], - "params": [], + "params": [ + [ + "bool", + "doConcat", + "1" + ] + ], "categories": [ - "portal" + "list" ] }, - "NumericCounter": { + "MakeString": { "inputs": [ [ "", @@ -3544,8 +2859,8 @@ ], "outputs": [ [ - "int", - "count", + "string", + "value", "" ], [ @@ -3554,13 +2869,39 @@ "" ] ], - "params": [], + "params": [ + [ + "string", + "value", + "" + ] + ], "categories": [ - "numeric" + "string" ] }, - "NumericFloat": { + "MakeVisualAABBPrimitive": { "inputs": [ + [ + "float", + "dx", + "1" + ], + [ + "vec3f", + "boundMin", + "-0.5,-0.5,-0.5" + ], + [ + "vec3f", + "boundMax", + "0.5,0.5,0.5" + ], + [ + "int", + "OpenTop", + "0" + ], [ "", "SRC", @@ -3569,8 +2910,8 @@ ], "outputs": [ [ - "float", - "value", + "", + "prim", "" ], [ @@ -3581,16 +2922,16 @@ ], "params": [ [ - "float", - "value", - "0" + "enum points edges trifaces quadfaces", + "type", + "edges" ] ], "categories": [ - "numeric" + "visualize" ] }, - "NumericInt": { + "MakeWritePath": { "inputs": [ [ "", @@ -3600,8 +2941,8 @@ ], "outputs": [ [ - "int", - "value", + "string", + "path", "" ], [ @@ -3612,94 +2953,92 @@ ], "params": [ [ - "int", - "value", - "0" + "writepath", + "path", + "" ] ], "categories": [ - "numeric" + "string" ] }, - "NumericIntVec2": { + "MeshCopy": { "inputs": [ [ "", - "SRC", + "copyFrom", "" - ] - ], - "outputs": [ + ], [ - "vec2i", - "vec2", + "", + "copyTo", "" ], [ "", - "DST", + "SRC", "" ] ], - "params": [ - [ - "int", - "x", - "0" - ], + "outputs": [ [ - "int", - "y", - "0" + "", + "DST", + "" ] ], + "params": [], "categories": [ - "numeric" + "trimesh" ] }, - "NumericIntVec3": { + "MeshMix": { "inputs": [ [ "", - "SRC", + "meshA", "" - ] - ], - "outputs": [ + ], [ - "vec3i", - "vec3", + "", + "meshB", "" ], [ "", - "DST", + "coef", + "" + ], + [ + "", + "SRC", "" ] ], - "params": [ - [ - "int", - "x", - "0" - ], + "outputs": [ [ - "int", - "y", - "0" + "", + "mesh", + "" ], [ - "int", - "z", - "0" + "", + "DST", + "" ] ], + "params": [], "categories": [ - "numeric" + "trimesh" ] }, - "NumericIntVec4": { + "MeshToPrimitive": { "inputs": [ + [ + "", + "mesh", + "" + ], [ "", "SRC", @@ -3708,8 +3047,8 @@ ], "outputs": [ [ - "vec4f", - "vec4", + "", + "prim", "" ], [ @@ -3718,58 +3057,22 @@ "" ] ], - "params": [ - [ - "float", - "x", - "0" - ], - [ - "float", - "y", - "0" - ], - [ - "float", - "z", - "0" - ], - [ - "float", - "w", - "0" - ] - ], + "params": [], "categories": [ - "numeric" + "primitive" ] }, - "NumericInterpolation": { + "MoveAssign": { "inputs": [ [ - "NumericObject", - "src", + "", + "dst", "" ], [ - "NumericObject", - "srcMin", - "0" - ], - [ - "NumericObject", - "srcMax", - "1" - ], - [ - "NumericObject", - "dstMin", - "0" - ], - [ - "NumericObject", - "dstMax", - "1" + "", + "src", + "" ], [ "", @@ -3779,7 +3082,7 @@ ], "outputs": [ [ - "NumericObject", + "", "dst", "" ], @@ -3789,27 +3092,16 @@ "" ] ], - "params": [ - [ - "bool", - "isClamped", - "0" - ] - ], + "params": [], "categories": [ - "numeric" + "portal" ] }, - "NumericOperator": { + "MoveClone": { "inputs": [ [ - "NumericObject", - "lhs", - "" - ], - [ - "NumericObject", - "rhs", + "", + "object", "" ], [ @@ -3820,8 +3112,8 @@ ], "outputs": [ [ - "NumericObject", - "ret", + "", + "newObject", "" ], [ @@ -3830,23 +3122,17 @@ "" ] ], - "params": [ - [ - "enum add sub mul div mod and or xor shr shl cmpge cmple cmpgt cmplt cmpne cmpeq land lor pos neg inv not atan2 pow max min fmod dot cross distance length normalize abs sqrt sin cos tan asin acos atan exp log floor ceil toint tofloat anytrue alltrue copy copyr", - "op_type", - "add" - ] - ], + "params": [], "categories": [ - "numeric" + "portal" ] }, - "NumericRandom": { + "MoveDelete": { "inputs": [ [ - "float", - "scale", - "1" + "", + "object", + "" ], [ "", @@ -3855,50 +3141,19 @@ ] ], "outputs": [ - [ - "NumericObject", - "value", - "" - ], [ "", "DST", "" ] ], - "params": [ - [ - "int", - "dim", - "1" - ], - [ - "bool", - "symmetric", - "0" - ] - ], + "params": [], "categories": [ - "numeric" + "portal" ] }, - "NumericRangeList": { + "NumericCounter": { "inputs": [ - [ - "int", - "start", - "0" - ], - [ - "int", - "end", - "1" - ], - [ - "int", - "skip", - "1" - ], [ "", "SRC", @@ -3906,9 +3161,9 @@ ] ], "outputs": [ - [ - "", - "list", + [ + "int", + "count", "" ], [ @@ -3919,10 +3174,10 @@ ], "params": [], "categories": [ - "list" + "numeric" ] }, - "NumericVec2": { + "NumericFloat": { "inputs": [ [ "", @@ -3932,8 +3187,8 @@ ], "outputs": [ [ - "vec2f", - "vec2", + "float", + "value", "" ], [ @@ -3945,12 +3200,7 @@ "params": [ [ "float", - "x", - "0" - ], - [ - "float", - "y", + "value", "0" ] ], @@ -3958,7 +3208,7 @@ "numeric" ] }, - "NumericVec3": { + "NumericInt": { "inputs": [ [ "", @@ -3968,8 +3218,8 @@ ], "outputs": [ [ - "vec3f", - "vec3", + "int", + "value", "" ], [ @@ -3980,18 +3230,8 @@ ], "params": [ [ - "float", - "x", - "0" - ], - [ - "float", - "y", - "0" - ], - [ - "float", - "z", + "int", + "value", "0" ] ], @@ -3999,7 +3239,7 @@ "numeric" ] }, - "NumericVec4": { + "NumericIntVec2": { "inputs": [ [ "", @@ -4009,8 +3249,8 @@ ], "outputs": [ [ - "vec4f", - "vec4", + "vec2i", + "vec2", "" ], [ @@ -4021,42 +3261,22 @@ ], "params": [ [ - "float", + "int", "x", "0" ], [ - "float", + "int", "y", "0" - ], - [ - "float", - "z", - "0" - ], - [ - "float", - "w", - "0" ] ], "categories": [ "numeric" ] }, - "NumericWrangle": { + "NumericIntVec3": { "inputs": [ - [ - "DictObject:NumericObject", - "params", - "" - ], - [ - "string", - "zfxCode", - "" - ], [ "", "SRC", @@ -4065,8 +3285,8 @@ ], "outputs": [ [ - "DictObject:NumericObject", - "result", + "vec3i", + "vec3", "" ], [ @@ -4075,13 +3295,7 @@ "" ] ], - "params": [], - "categories": [ - "zenofx" - ] - }, - "PackNumericIntVec2": { - "inputs": [ + "params": [ [ "int", "x", @@ -4093,50 +3307,17 @@ "0" ], [ - "", - "SRC", - "" - ] - ], - "outputs": [ - [ - "vec2i", - "vec2", - "" - ], - [ - "", - "DST", - "" + "int", + "z", + "0" ] ], - "params": [], "categories": [ "numeric" ] }, - "PackNumericVec": { + "NumericIntVec4": { "inputs": [ - [ - "float", - "x", - "0" - ], - [ - "float", - "y", - "0" - ], - [ - "float", - "z", - "0" - ], - [ - "float", - "w", - "0" - ], [ "", "SRC", @@ -4145,8 +3326,8 @@ ], "outputs": [ [ - "", - "vec", + "vec4f", + "vec4", "" ], [ @@ -4157,87 +3338,56 @@ ], "params": [ [ - "enum float vec2f vec3f vec4f", - "type", - "vec3f" - ] - ], - "categories": [ - "numeric" - ] - }, - "PackNumericVecInt": { - "inputs": [ - [ - "int", + "float", "x", "0" ], [ - "int", + "float", "y", "0" ], [ - "int", + "float", "z", "0" ], [ - "int", + "float", "w", "0" - ], - [ - "", - "SRC", - "" - ] - ], - "outputs": [ - [ - "", - "veci", - "" - ], - [ - "", - "DST", - "" - ] - ], - "params": [ - [ - "enum int vec2i vec3i vec4i", - "type", - "vec3i" ] ], "categories": [ "numeric" ] }, - "ParticleParticleWrangle": { + "NumericInterpolation": { "inputs": [ [ - "PrimitiveObject", - "prim1", + "NumericObject", + "src", "" ], [ - "PrimitiveObject", - "prim2", - "" + "NumericObject", + "srcMin", + "0" ], [ - "string", - "zfxCode", - "" + "NumericObject", + "srcMax", + "1" ], [ - "DictObject:NumericObject", - "params", - "" + "NumericObject", + "dstMin", + "0" + ], + [ + "NumericObject", + "dstMax", + "1" ], [ "", @@ -4247,8 +3397,8 @@ ], "outputs": [ [ - "PrimitiveObject", - "prim", + "NumericObject", + "dst", "" ], [ @@ -4257,66 +3407,27 @@ "" ] ], - "params": [], - "categories": [ - "zenofx" - ] - }, - "ParticlesBuildBvh": { - "inputs": [ - [ - "PrimitiveObject", - "primNei", - "" - ], - [ - "numeric:float", - "radius", - "" - ], - [ - "numeric:float", - "radiusMin", - "" - ], - [ - "", - "SRC", - "" - ] - ], - "outputs": [ - [ - "LBvh", - "lbvh", - "" - ], + "params": [ [ - "", - "DST", - "" + "bool", + "isClamped", + "0" ] ], - "params": [], "categories": [ - "zenofx" + "numeric" ] }, - "ParticlesBuildHashGrid": { - "inputs": [ - [ - "PrimitiveObject", - "primNei", - "" - ], + "NumericOperator": { + "inputs": [ [ - "numeric:float", - "radius", + "NumericObject", + "lhs", "" ], [ - "numeric:float", - "radiusMin", + "NumericObject", + "rhs", "" ], [ @@ -4327,8 +3438,8 @@ ], "outputs": [ [ - "hashgrid", - "hashGrid", + "NumericObject", + "ret", "" ], [ @@ -4337,37 +3448,23 @@ "" ] ], - "params": [], + "params": [ + [ + "enum add sub mul div mod and or xor shr shl cmpge cmple cmpgt cmplt cmpne cmpeq land lor pos neg inv not atan2 pow max min fmod dot cross distance length normalize abs sqrt sin cos tan asin acos atan exp log floor ceil toint tofloat anytrue alltrue copy copyr", + "op_type", + "add" + ] + ], "categories": [ - "zenofx" + "numeric" ] }, - "ParticlesNeighborBvhWrangle": { + "NumericRandom": { "inputs": [ [ - "PrimitiveObject", - "prim", - "" - ], - [ - "PrimitiveObject", - "primNei", - "" - ], - [ - "LBvh", - "lbvh", - "" - ], - [ - "string", - "zfxCode", - "" - ], - [ - "DictObject:NumericObject", - "params", - "" + "float", + "scale", + "1" ], [ "", @@ -4377,8 +3474,8 @@ ], "outputs": [ [ - "PrimitiveObject", - "prim", + "NumericObject", + "value", "" ], [ @@ -4387,37 +3484,38 @@ "" ] ], - "params": [], + "params": [ + [ + "int", + "dim", + "1" + ], + [ + "bool", + "symmetric", + "0" + ] + ], "categories": [ - "zenofx" + "numeric" ] }, - "ParticlesNeighborWrangle": { + "NumericRangeList": { "inputs": [ [ - "PrimitiveObject", - "prim", - "" - ], - [ - "PrimitiveObject", - "primNei", - "" - ], - [ - "HashGrid", - "hashGrid", - "" + "int", + "start", + "0" ], [ - "string", - "zfxCode", - "" + "int", + "end", + "1" ], [ - "DictObject:NumericObject", - "params", - "" + "int", + "skip", + "1" ], [ "", @@ -4427,8 +3525,8 @@ ], "outputs": [ [ - "PrimitiveObject", - "prim", + "", + "list", "" ], [ @@ -4439,16 +3537,11 @@ ], "params": [], "categories": [ - "zenofx" + "list" ] }, - "ParticlesToPrimitive": { + "NumericVec2": { "inputs": [ - [ - "", - "pars", - "" - ], [ "", "SRC", @@ -4457,8 +3550,8 @@ ], "outputs": [ [ - "", - "prim", + "vec2f", + "vec2", "" ], [ @@ -4467,28 +3560,24 @@ "" ] ], - "params": [], + "params": [ + [ + "float", + "x", + "0" + ], + [ + "float", + "y", + "0" + ] + ], "categories": [ - "primitive" + "numeric" ] }, - "ParticlesWrangle": { + "NumericVec3": { "inputs": [ - [ - "PrimitiveObject", - "prim", - "" - ], - [ - "string", - "zfxCode", - "" - ], - [ - "DictObject:NumericObject", - "params", - "" - ], [ "", "SRC", @@ -4497,8 +3586,8 @@ ], "outputs": [ [ - "PrimitiveObject", - "prim", + "vec3f", + "vec3", "" ], [ @@ -4507,31 +3596,83 @@ "" ] ], - "params": [], + "params": [ + [ + "float", + "x", + "0" + ], + [ + "float", + "y", + "0" + ], + [ + "float", + "z", + "0" + ] + ], "categories": [ - "zenofx" + "numeric" ] }, - "PassToyApplyShader": { + "NumericVec4": { "inputs": [ [ "", - "shader", + "SRC", + "" + ] + ], + "outputs": [ + [ + "vec4f", + "vec4", "" ], [ "", - "uniforms", + "DST", "" + ] + ], + "params": [ + [ + "float", + "x", + "0" ], [ - "", - "textureIn", + "float", + "y", + "0" + ], + [ + "float", + "z", + "0" + ], + [ + "float", + "w", + "0" + ] + ], + "categories": [ + "numeric" + ] + }, + "NumericWrangle": { + "inputs": [ + [ + "DictObject:NumericObject", + "params", "" ], [ - "", - "textureOut", + "string", + "zfxCode", "" ], [ @@ -4542,8 +3683,8 @@ ], "outputs": [ [ - "", - "textureOut", + "DictObject:NumericObject", + "result", "" ], [ @@ -4554,15 +3695,20 @@ ], "params": [], "categories": [ - "PassToy" + "zenofx" ] }, - "PassToyDownscaleResolution": { + "PackNumericIntVec2": { "inputs": [ [ - "", - "scale", - "" + "int", + "x", + "0" + ], + [ + "int", + "y", + "0" ], [ "", @@ -4571,6 +3717,11 @@ ] ], "outputs": [ + [ + "vec2i", + "vec2", + "" + ], [ "", "DST", @@ -4579,11 +3730,31 @@ ], "params": [], "categories": [ - "PassToy" + "numeric" ] }, - "PassToyGetResolution": { + "PackNumericVec": { "inputs": [ + [ + "float", + "x", + "0" + ], + [ + "float", + "y", + "0" + ], + [ + "float", + "z", + "0" + ], + [ + "float", + "w", + "0" + ], [ "", "SRC", @@ -4593,7 +3764,7 @@ "outputs": [ [ "", - "resolution", + "vec", "" ], [ @@ -4602,17 +3773,38 @@ "" ] ], - "params": [], + "params": [ + [ + "enum float vec2f vec3f vec4f", + "type", + "vec3f" + ] + ], "categories": [ - "PassToy" + "numeric" ] }, - "PassToyGetTextureInteger": { + "PackNumericVecInt": { "inputs": [ [ - "", - "texture", - "" + "int", + "x", + "0" + ], + [ + "int", + "y", + "0" + ], + [ + "int", + "z", + "0" + ], + [ + "int", + "w", + "0" ], [ "", @@ -4623,7 +3815,7 @@ "outputs": [ [ "", - "id", + "veci", "" ], [ @@ -4632,16 +3824,37 @@ "" ] ], - "params": [], + "params": [ + [ + "enum int vec2i vec3i vec4i", + "type", + "vec3i" + ] + ], "categories": [ - "PassToy" + "numeric" ] }, - "PassToyGetTextureResolution": { + "ParticleParticleWrangle": { "inputs": [ [ - "", - "texture", + "PrimitiveObject", + "prim1", + "" + ], + [ + "PrimitiveObject", + "prim2", + "" + ], + [ + "string", + "zfxCode", + "" + ], + [ + "DictObject:NumericObject", + "params", "" ], [ @@ -4652,8 +3865,8 @@ ], "outputs": [ [ - "", - "resolution", + "PrimitiveObject", + "prim", "" ], [ @@ -4664,14 +3877,24 @@ ], "params": [], "categories": [ - "PassToy" + "zenofx" ] }, - "PassToyImageTextureFromVoidPtr": { + "ParticlesBuildBvh": { "inputs": [ [ - "", - "voidPtr", + "PrimitiveObject", + "primNei", + "" + ], + [ + "numeric:float", + "radius", + "" + ], + [ + "numeric:float", + "radiusMin", "" ], [ @@ -4682,8 +3905,8 @@ ], "outputs": [ [ - "", - "texture", + "LBvh", + "lbvh", "" ], [ @@ -4694,19 +3917,24 @@ ], "params": [], "categories": [ - "PassToy" + "zenofx" ] }, - "PassToyImageTextureFromVoidPtrAndRes": { + "ParticlesBuildHashGrid": { "inputs": [ [ - "", - "voidPtr", + "PrimitiveObject", + "primNei", "" ], [ - "", - "resolution", + "numeric:float", + "radius", + "" + ], + [ + "numeric:float", + "radiusMin", "" ], [ @@ -4717,8 +3945,8 @@ ], "outputs": [ [ - "", - "texture", + "hashgrid", + "hashGrid", "" ], [ @@ -4729,45 +3957,34 @@ ], "params": [], "categories": [ - "PassToy" + "zenofx" ] }, - "PassToyLoadImageTexture": { + "ParticlesNeighborBvhWrangle": { "inputs": [ [ - "", - "SRC", + "PrimitiveObject", + "prim", "" - ] - ], - "outputs": [ + ], [ - "", - "texture", + "PrimitiveObject", + "primNei", "" ], [ - "", - "DST", + "LBvh", + "lbvh", "" - ] - ], - "params": [ + ], [ "string", - "path", + "zfxCode", "" - ] - ], - "categories": [ - "PassToy" - ] - }, - "PassToyMakeShader": { - "inputs": [ + ], [ - "", - "source", + "DictObject:NumericObject", + "params", "" ], [ @@ -4778,8 +3995,8 @@ ], "outputs": [ [ - "", - "shader", + "PrimitiveObject", + "prim", "" ], [ @@ -4790,83 +4007,36 @@ ], "params": [], "categories": [ - "PassToy" + "zenofx" ] }, - "PassToyMakeTexture": { + "ParticlesNeighborWrangle": { "inputs": [ [ - "", - "resolution", + "PrimitiveObject", + "prim", "" ], [ - "", - "SRC", - "" - ] - ], - "outputs": [ - [ - "", - "texture", + "PrimitiveObject", + "primNei", "" ], [ - "", - "DST", + "HashGrid", + "hashGrid", "" - ] - ], - "params": [ + ], [ "string", - "format", - "rgb16f" - ] - ], - "categories": [ - "PassToy" - ] - }, - "PassToyMakeTexturePair": { - "inputs": [ - [ - "", - "resolution", + "zfxCode", "" ], [ - "", - "SRC", - "" - ] - ], - "outputs": [ - [ - "", - "texturePair", + "DictObject:NumericObject", + "params", "" ], - [ - "", - "DST", - "" - ] - ], - "params": [ - [ - "string", - "format", - "rgb16f" - ] - ], - "categories": [ - "PassToy" - ] - }, - "PassToyScreenTexture": { - "inputs": [ [ "", "SRC", @@ -4875,8 +4045,8 @@ ], "outputs": [ [ - "", - "texture", + "PrimitiveObject", + "prim", "" ], [ @@ -4887,14 +4057,14 @@ ], "params": [], "categories": [ - "PassToy" + "zenofx" ] }, - "PassToyTexturePairSwap": { + "ParticlesToPrimitive": { "inputs": [ [ "", - "texturePair", + "pars", "" ], [ @@ -4906,12 +4076,7 @@ "outputs": [ [ "", - "texturePair", - "" - ], - [ - "", - "oldTexture", + "prim", "" ], [ @@ -4922,14 +4087,24 @@ ], "params": [], "categories": [ - "PassToy" + "primitive" ] }, - "PassToyUpscaleResolution": { + "ParticlesWrangle": { "inputs": [ [ - "", - "scale", + "PrimitiveObject", + "prim", + "" + ], + [ + "string", + "zfxCode", + "" + ], + [ + "DictObject:NumericObject", + "params", "" ], [ @@ -4939,6 +4114,11 @@ ] ], "outputs": [ + [ + "PrimitiveObject", + "prim", + "" + ], [ "", "DST", @@ -4947,7 +4127,7 @@ ], "params": [], "categories": [ - "PassToy" + "zenofx" ] }, "PlaneProjectPrimitive2DAABB": { @@ -6909,6 +6089,31 @@ "numeric" ] }, + "PrintRawDoubleObject": { + "inputs": [ + [ + "", + "RawDoubleObject", + "" + ], + [ + "", + "SRC", + "" + ] + ], + "outputs": [ + [ + "", + "DST", + "" + ] + ], + "params": [], + "categories": [ + "Zentricle" + ] + }, "PrintString": { "inputs": [ [ @@ -7293,21 +6498,21 @@ "portal" ] }, - "SolveNonlinearProblem2": { + "SolveNonlinearProblem": { "inputs": [ [ "", - "JFNKSolverObject2", + "JFNKSolverObject", "" ], [ "", - "NonlinearProblemObject2", + "NonlinearProblemObject", "" ], [ "", - "function", + "RawDoubleObject", "" ], [ @@ -7317,6 +6522,11 @@ ] ], "outputs": [ + [ + "", + "RawDoubleObject", + "" + ], [ "", "DST", diff --git a/projects/Functional/Algebra/AlgebraSolver/BiCGSTAB.h b/projects/Functional/Algebra/AlgebraSolver/BiCGSTAB.h new file mode 100644 index 0000000000..2206843c66 --- /dev/null +++ b/projects/Functional/Algebra/AlgebraSolver/BiCGSTAB.h @@ -0,0 +1,143 @@ +/** + * @file BiCGSTAB.h + * @author Ma Pengfei (mapengfei@mail.nwpu.edu.cn) + * @brief + * @version 0.1 + * @date 2021-12-04 + * + * @copyright Copyright (c) 2021 Ma Pengfei + * + */ + + +// Algrithm described on page 21 of thesis +// +// @article{2016Comparison, +// title={Comparison of Some Preconditioners for the Incompressible Navier-Stokes Equations}, +// author={ He, X. and Vuik, C. }, +// journal={Numerical Mathematics Theory Methods & Applications}, +// volume={9}, +// number={02}, +// pages={239-261}, +// year={2016}, +// } + +// NOTE : Handle the breakdowns +// (t_j, t_j) = 0, (t_j, s_j) = 0, \sqrt{(t_{j+1}, t_{j+1})} = 0, \beta = 0 + +// TODO : Preconditioner, it might be difficult for matrix-free solver. + +#ifndef _BICGSTAB_H_ +#define _BICGSTAB_H_ + +#include "LinearSolver.h" +#include +#include + +template +class BiCGSTAB : public LinearSolver +{ +private: + double rho, alpha, omega, beta; + VectorType r, v, s, t, p, r0, temp; +public: + + + // Initial. Do not allocate memory inside the for loop for efficiency. + virtual void Initialize(size_t n) final { + + // Set the size of intermediate variables. + r.resize(n); + r0.resize(n); + v.resize(n); + s.resize(n); + t.resize(n); + p.resize(n); + temp.resize(n); + + } + + // NOTE : the definition of relative_error : r = b - Av + // The definition of Ae = −r, e is difficult to solve. + // we can give a bound of error : $\|e\|\le\|A^{-1}\|\|e\|$ + // relative error norms relate to the conditioning of A: + // $\frac{1}{\kappa(A)} \frac{\|\mathbf{r}\|}{\|\mathbf{b}\|} \leq \frac{\|\mathbf{e}\|}{\|\mathbf{u}\|} \leq \kappa(A) \frac{\|\mathbf{r}\|}{\|\mathbf{b}\|}$ + // Which means it can be controled by $ \frac{\|r\|}{\|b\|} $. + // + // NOTE : We use 2-norm of relative_error as return instead of relative error. + virtual std::pair> Solve( + std::shared_ptr> linear_problem, + VectorType &x0, + const VectorType &b) final { + + // TODO : check if the sizes of rhs and linear_problem match the size of ref. + + // r0 = b - Ax + linear_problem->form(x0,temp); // temp = Ax + r0.axpy(-1.0,temp,b); // r0 = -1.0*temp + b; + r = r0; // r = r0 + p = r0; // p = r0 + + int iter = 0; + double norm_b = std::sqrt(b.inner(b)); + if(norm_b == 0) { + x0 = 0.0; + return std::make_pair(true, std::make_pair(0.0, iter)); + } + double relative_error = std::sqrt(r.inner(r))/norm_b; + + double delta, gamma; + + // start the iterations + while (relative_error > LinearSolver::get_tolerance() && iter < LinearSolver::get_max_iteration()) + { + LOG_F(INFO, "BiCGSTAB solver(start) : iteration %d.", iter); + + linear_problem->form(p,v); // v = Ap + rho = r.inner(r0); // rho = (r,r0) + alpha = rho/v.inner(r0); // alpha = rho/(v,r0) + + if (!std::isnormal(alpha)) return std::make_pair(true, std::make_pair(relative_error,iter)); + + s.axpy(-alpha,v,r); // s = r - apha * v; + linear_problem->form(s,t); // t = As + + gamma = t.inner(t); + delta = t.inner(s); + omega = delta / gamma; // omega = (t,s)/(t,t) + + if (!std::isnormal(omega)) return std::make_pair(true, std::make_pair(relative_error,iter)); + // x = x + alpha*p + omega*s + temp.axpy(omega,s,x0); // temp = x + omega*s + x0.axpy(alpha,p,temp); // x = temp + alpha*p + // r = s - omega*t; + temp = r; // r_j + r.axpy(-omega, t, s); // r_{j+1} + + relative_error = std::sqrt(r.inner(r))/norm_b; + LOG_F(INFO, "BiCGSTAB solver(end) : iteration %d, relative_error norm : %.12lf.", iter, relative_error); + if (relative_error < LinearSolver::get_tolerance()) return std::make_pair(true, std::make_pair(relative_error,iter+1)); + + beta = r.inner(r0)/temp.inner(r0)*alpha/omega; // (r,r0)/(temp,r0)*alpha/omega + + if (!std::isnormal(beta)) return std::make_pair(true, std::make_pair(relative_error,iter+1)); + + temp.axpy(-omega, v, p); // p = r + beta*(p-omega*v); + p.axpy(beta,temp, r); + + iter++; + } + return std::make_pair(false, std::make_pair(relative_error,iter)); + } + + + BiCGSTAB(size_t n){ + Initialize(n); + } + + ~BiCGSTAB(){ + + } +}; + +#endif \ No newline at end of file diff --git a/projects/Functional/Algebra/AlgebraSolver/CMakeLists.txt b/projects/Functional/Algebra/AlgebraSolver/CMakeLists.txt new file mode 100644 index 0000000000..185c3329be --- /dev/null +++ b/projects/Functional/Algebra/AlgebraSolver/CMakeLists.txt @@ -0,0 +1,2 @@ +set(SOURCES + PARENT_SCOPE) \ No newline at end of file diff --git a/projects/Functional/Algebra/AlgebraSolver/ConjugateGradient.h b/projects/Functional/Algebra/AlgebraSolver/ConjugateGradient.h new file mode 100644 index 0000000000..90efe43ecb --- /dev/null +++ b/projects/Functional/Algebra/AlgebraSolver/ConjugateGradient.h @@ -0,0 +1,53 @@ +/** + * @file ConjugateGradient.h + * @author Ma Pengfei (mapengfei@mail.nwpu.edu.cn) + * @brief + * @version 0.1 + * @date 2021-12-04 + * + * @copyright Copyright (c) 2021 Ma Pengfei + * + */ + +#ifndef _CONJUGATEGRADIENT_H_ +#define _CONJUGATEGRADIENT_H_ + +#include +#include "LinearSolver.h" + +class ConjugateGradient : public LinearSolver +{ +private: + /* data */ + std::vector m, z, s, r, temp; + +public: + ConjugateGradient(/* args */){ + + } + + ~ConjugateGradient(){ + + } + + // Initial approximation. + virtual void Initialize(size_t n) final { + + // TODO : + + } + + // Solve. + virtual std::pair> Solve( + LinearProblem &forceOperator, + std::vector &forceDofs, + const std::vector &rhs) final { + + // TODO : + return std::make_pair(false, std::make_pair(0.0,0)); + } +}; + +// TODO : Nonlinear solver. + +#endif \ No newline at end of file diff --git a/projects/Functional/Algebra/AlgebraSolver/GenericVector.h b/projects/Functional/Algebra/AlgebraSolver/GenericVector.h new file mode 100644 index 0000000000..4a05e42cba --- /dev/null +++ b/projects/Functional/Algebra/AlgebraSolver/GenericVector.h @@ -0,0 +1,183 @@ +/** + * @file GenericVector.h + * @author Ma Pengfei (mapengfei@mail.nwpu.edu.cn) + * @brief + * @version 0.1 + * @date 2022-01-08 + * + * @copyright Copyright (c) 2022 Ma Pengfei + * + */ +#ifndef _GENERIC_VECTOR_H_ +#define _GENERIC_VECTOR_H_ + +// STANDARD +#include + +// CUDA +#include +#include + +// THIRD PARTY +#include + +// NOTE : reason for designing this class +// After studied xinxin's code and dolfin's code, I designed GenericVector +// which is an interface class. I want GpuVector and StdVector have the same +// interface so that BiCONSTAB can be applied on both GPU and CPU without +// modification. Besides, GenericVector can interact whith std::vector. + +// Two classes : Array1D and GenericVector; +// One function : Array1D flatten(const GenericVector& x); + +// ----------------------------------------------------------------------------------- +// GenericVector is an interface class, but I don't know how to complete it now. +// blogs for future study : +// C++:如何正确的使用接口类 https://blog.csdn.net/netyeaxi/article/details/80887646 +// C++:如何正确的定义一个接口类 https://blog.csdn.net/netyeaxi/article/details/80724557 +template class GenericVector; + +// wrap an raw pointer and size of an array. +template class Array1D; + +template +Array1D flatten(const GenericVector& x){ + Array1D res; + res.data = (T *) x.data(); + res.num = x.value_size() * x.size(); + return res; + +} + +template +class Array1D { +public: + T *data; + size_t num; +}; + +// TV Including : float, float3, float4, double, double3, double4 +// T Including : float, double +template +class GenericVector +{ +protected: + int3 _dim; +public: + + GenericVector():_dim({0,1,1}){} + + size_t size() const { return _dim.x*_dim.y*_dim.z; } + + void resize(size_t new_size) { + + CHECK_F(new_size >= 1, "Wrong size."); + + if (new_size != this->size()){ + _dim = make_int3(new_size, 1, 1); + init(); + } + _dim= make_int3(new_size, 1, 1); + } + + void resize(int3 dim) { + + CHECK_F(dim.x>=1 && dim.y >= 1 && dim.z >=1, "Wrong dim."); + + size_t new_size = dim.x*dim.y*dim.z; + + if (new_size != this->size()) { + _dim = dim; + init(); + } else { + _dim = dim; + } + }; + + int3 dim() const {return _dim;} + + constexpr size_t value_size() const { return sizeof(TV) / sizeof(T); } + + virtual bool use_gpu() const = 0; + + // Allocate memory + virtual void init() = 0; + + virtual TV* data() = 0; + + virtual const TV* data() const = 0; + + // NOTE : Try not to use these operator, because it is expensive for data stored in GPU memory. + // virtual double& operator[](std::size_t __n) = 0; + // virtual const double& operator[](std::size_t __n) const = 0; + + // /// Set all entries to zero. + // virtual void zero() = 0; + + // /// Return copy of vector + // virtual std::shared_ptr copy() const = 0; + + // /// Get block of values + virtual void get(std::vector& data) const = 0; + + // /// Add block of values + // virtual void add(const double* block, std::size_t m) = 0; + + // /// Set block of values + virtual void set(const std::vector& data) = 0; + + // /// Add multiple of given vector (AXPY operation : y = y + a*x) + virtual void axpy(T a, const GenericVector& x) = 0; + + virtual void axpy(T a, const GenericVector& x, const GenericVector& y) = 0; + + // /// Replace all entries in the vector by their absolute values + // virtual void abs() = 0; + + // /// Return inner product with given vector + virtual double inner(const GenericVector& x) const = 0; + + // /// Return norm of vector ("l2" by default?) + // virtual double norm(std::string norm_type) const = 0; + + /// Return minimum value of vector + virtual double min() const = 0; + + /// Return maximum value of vector + virtual double max() const = 0; + + /// Return sum of vector + virtual double sum() const = 0; + + // /// Multiply vector by given number + // virtual const GenericVector& operator*= (double a) = 0; + + // /// Multiply vector by another vector pointwise + // virtual const GenericVector& operator*= (const GenericVector& x) = 0; + + // /// Divide vector by given number + // virtual const GenericVector& operator/= (double a) = 0; + + // /// Add given vector + // virtual const GenericVector& operator+= (const GenericVector& x) = 0; + + // /// Add number to all components of a vector + // virtual const GenericVector& operator+= (double a) = 0; + + // /// Subtract given vector + // virtual const GenericVector& operator-= (const GenericVector& x) = 0; + + /// Subtract number from all components of a vector + virtual const GenericVector& operator-= (T a) = 0; + + /// Assignment operator + // TODO : what's the difference between GenericVector& and const GenericVector& as return type? + // StdVector& StdVector::operator=(const StdVector&) do not override this function. + // virtual GenericVector& operator= (const GenericVector& x) = default; + + /// Assignment operator + virtual const GenericVector& operator= (T a) = 0; + +}; + +#endif \ No newline at end of file diff --git a/projects/Functional/Algebra/AlgebraSolver/GpuVector.h b/projects/Functional/Algebra/AlgebraSolver/GpuVector.h new file mode 100644 index 0000000000..4584723bd3 --- /dev/null +++ b/projects/Functional/Algebra/AlgebraSolver/GpuVector.h @@ -0,0 +1,225 @@ +/** + * @file GpuVector.h + * @author Ma Pengfei (mapengfei@mail.nwpu.edu.cn) + * @brief + * @version 0.1 + * @date 2022-01-08 + * + * @copyright Copyright (c) 2022 Ma Pengfei + * + */ + +#ifndef _GPU_VECTOR_H_ +#define _GPU_VECTOR_H_ + +#include "GenericVector.h" +#include "StdVector.h" +#include + + +// xinxin use an unnecessary temp array to compute inner product. I think it +// is better to use algrithms of thrust. Here is the reference : +// Thrust快速入门教程(三) —— Algorithms https://blog.csdn.net/zerolover/article/details/44458985 + +template +class GpuVector : public GenericVector +{ + +private: + TV* _data; + using GenericVector::_dim; + + virtual void init() override { + + // free old memory + if(_data != nullptr) gpu::freeGPUBuffer(_data); + + // allocate GPU memory + gpu::gpu_malloc((void **)&_data, sizeof(TV) * this->size()); + } + + +public: + virtual bool use_gpu() const { return true;} + + virtual TV* data() override { return _data;} + + virtual const TV* data() const override { return _data;} + + /// interaction with std::vector + /////////////////////////////////////////////////////////////////////////////////////////////////// + /** + * @brief copy data from GPU memory to HOST memory + * @param data + */ + virtual void get(std::vector& data) const override { + CHECK_F(data.size() == this->size(), "Wrong size."); + gpu::gpu_to_cpu((char*)data.data(), (char*)this->data(), sizeof(TV)*this->size()); + } + + /** + * @brief copy data from HOST memory to GPU memory + * @param data + */ + virtual void set(const std::vector& data) override { + CHECK_F(data.size() == this->size(), "Wrong size."); + gpu::cpu_to_gpu((char*)this->data(), (char*)data.data(), sizeof(TV)*this->size()); + + } + + /// linear operations + /////////////////////////////////////////////////////////////////////////////////////////////////// + + // max(abs(x)); + T abs_max(){ + auto array = flatten(*this); + return gpu::gpu_abs_max(array.data, array.num); + } + + virtual double sum() const override { + auto array = flatten(*this); + return gpu::gpu_sum(array.data, array.num); + } + + /// Return minimum value of vector + virtual double min() const override { + auto array = flatten(*this); + return gpu::gpu_min(array.data, array.num); + } + + /// Return maximum value of vector + virtual double max() const override { + auto array = flatten(*this); + return gpu::gpu_max(array.data, array.num); + } + + + + // dot(a,b); + // a.inner(b); + virtual double inner(const GenericVector& x) const override { + + CHECK_F(this->use_gpu() == x.use_gpu(), "not on GPU"); + + auto _x = flatten(x); + auto _m = flatten(*this); + + CHECK_F(_x.num == _m.num, "Wrong size."); + + return gpu::gpu_inner(_x.data, _m.data, _x.num); + } + + // x = y + a*x + // x.axpy(a,y); + virtual void axpy(T a, const GenericVector& y)override{ + + CHECK_F(this->use_gpu() == y.use_gpu(), "not on GPU."); + + auto _x = flatten(*this); + auto _y = flatten(y); + + CHECK_F(_x.num == _y.num, "Wrong size."); + + gpu::gpu_axpy(_x.data, _x.data, _y.data, a, _x.num); + } + + // z = a*x + y + // z.axpy(a,x,y); + virtual void axpy(T a, const GenericVector& x, const GenericVector& y)override{ + + CHECK_F(this->use_gpu() == x.use_gpu(), "not on GPU."); + CHECK_F(this->use_gpu() == y.use_gpu(), "not on GPU."); + + auto _x = flatten(x); + auto _y = flatten(y); + auto _z = flatten(*this); + + CHECK_F(_x.num == _y.num, "Wrong size."); + CHECK_F(_x.num == _z.num, "Wrong size."); + + gpu::gpu_axpy(_z.data, _x.data, _y.data, a, _x.num); + } + + // // x = a*x = (a-1)*x + x; + // // x.axpy(a-1,x); + // // x *= a; + // virtual const GenericVector& operator*= (T a) override { + // this->axpy(a-1,*this); + // return *this; + // } + + // x = a; + virtual const GenericVector& operator= (T a) override { + auto _x = flatten(*this); + gpu::gpu_fill(_x.data, a, _x.num); + return *this; + } + + virtual const GenericVector& operator-= (T a) override { + CHECK_F(false, "not implemented"); + } + + // construction functions. + ////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + + GpuVector() : GenericVector(), _data(nullptr){ } + + explicit GpuVector(int3 dim) : GpuVector() { + CHECK_F(dim.x>=1 && dim.y >= 1 && dim.z >=1, "Wrong dim."); + _dim = dim; + init(); + }; + + explicit GpuVector(const StdVector& cpu_vector): GpuVector() { + _dim = cpu_vector.dim(); + init(); + gpu::cpu_to_gpu((char*)_data, (char*)cpu_vector.data(), sizeof(TV)*this->size()); + } + // Five functions. + ////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + + virtual GpuVector& operator=(const GpuVector& x) + { + CHECK_F(this->use_gpu() == x.use_gpu(), "not on GPU."); + CHECK_F(x.size() == this->size(), "Wrong size."); + CHECK_F(x.dim().x == this->dim().x && x.dim().y == this->dim().y && x.dim().z == this->dim().z, "Wrong dim."); + LOG_F(WARNING, "gpu_copy on assignment method."); + gpu::gpu_copy((char*)this->data(), (char*)x.data(), this->size()*sizeof(TV)); + return *this; + } + + virtual GpuVector& operator=(GpuVector&& gpu_vector){ + if(this->_data != nullptr) gpu::freeGPUBuffer(this->_data); + this->_data = gpu_vector.data(); + this->_dim = gpu_vector.dim(); + gpu_vector.set_nullptr(); + return *this; + } + + explicit GpuVector(const GpuVector& gpu_vector): GpuVector() { + _dim = gpu_vector.dim(); + init(); + LOG_F(WARNING, "gpu_copy on construction method."); + gpu::gpu_copy((char*)_data, (char*)gpu_vector.data(), sizeof(TV)*this->size()); + } + + explicit GpuVector(GpuVector&& gpu_vector) : GpuVector() { + // there is no need to care about freeing memory, because it is a constructor. + this->_data = gpu_vector.data(); + this->_dim = gpu_vector.dim(); + gpu_vector.set_nullptr(); + }; + ~GpuVector(){ + if(_data != nullptr) gpu::freeGPUBuffer(_data); + } + ////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + +private: + + void set_nullptr(){ _data = nullptr;} + + + +}; + +#endif diff --git a/projects/Functional/Algebra/AlgebraSolver/LinearProblem.h b/projects/Functional/Algebra/AlgebraSolver/LinearProblem.h new file mode 100644 index 0000000000..7c494755df --- /dev/null +++ b/projects/Functional/Algebra/AlgebraSolver/LinearProblem.h @@ -0,0 +1,37 @@ +/** + * @file LinearProblem.h + * @author Ma Pengfei (mapengfei@mail.nwpu.edu.cn) + * @brief + * @version 0.1 + * @date 2021-12-04 + * + * @copyright Copyright (c) 2021 Ma Pengfei + * + */ + +#ifndef _LINEARPROBLEM_H_ +#define _LINEARPROBLEM_H_ +#include + +template +class LinearProblem +{ +private: + /* data */ +public: + LinearProblem(/* args */){ + + } + + // TODO : Virtual function? + virtual ~LinearProblem(){ + + } + + // Function called by matrix free linear solver. + // must be supplied by the user. + virtual void form(const VectorType& x, VectorType& r) = 0; + +}; + +#endif \ No newline at end of file diff --git a/projects/Functional/Algebra/AlgebraSolver/LinearSolver.h b/projects/Functional/Algebra/AlgebraSolver/LinearSolver.h new file mode 100644 index 0000000000..2e314cc772 --- /dev/null +++ b/projects/Functional/Algebra/AlgebraSolver/LinearSolver.h @@ -0,0 +1,74 @@ +/** + * @file LinearSolver.h + * @author Ma Pengfei (mapengfei@mail.nwpu.edu.cn) + * @brief + * @version 0.1 + * @date 2021-12-04 + * + * @copyright Copyright (c) 2021 Ma Pengfei + * + */ + +#ifndef _LINEARSOLVER_H_ +#define _LINEARSOLVER_H_ + +#include"LinearProblem.h" +#include +#include + +// TODO : Take it as an abstract class. +template +class LinearSolver +{ +private: + /* data */ +public: + + // TODO : How to use initialize inside constructor. + LinearSolver() + { + + } + + virtual ~LinearSolver() + { + } + + void SetMaxIteration(int iter) { max_linear_iteration = iter;} + + int get_max_iteration() const { return max_linear_iteration;} + + void set_tolerance(double tol) { max_linear_tolerance = tol;} + + double get_tolerance() const { return max_linear_tolerance;} + + /** + * @brief Allocate memories for all variables used in the iteration. + * @param n + */ + virtual void Initialize(size_t n) = 0; + + // + // TODO: is it better to use smart pointer for linear_problem? + // FEniCS uses const reference in such situation. + // Xinxin also uses reference. + /** + * @brief Solve the linear problem with initial guess "x0" and right hand side "b" + * @param linear_problem + * @param x0 + * @param b + * @return std::pair> + */ + virtual std::pair> Solve( + std::shared_ptr> linear_problem, + VectorType &x0, + const VectorType &b) = 0; + +private: + int max_linear_iteration = 1000; + double max_linear_tolerance = 1e-5; +}; + + + +#endif \ No newline at end of file diff --git a/projects/Functional/Algebra/AlgebraSolver/MINRES.h b/projects/Functional/Algebra/AlgebraSolver/MINRES.h new file mode 100644 index 0000000000..f22ebc52e2 --- /dev/null +++ b/projects/Functional/Algebra/AlgebraSolver/MINRES.h @@ -0,0 +1,10 @@ +/** + * @file MINRES.h + * @author Ma Pengfei (mapengfei@mail.nwpu.edu.cn) + * @brief symmetric linear solver + * @version 0.1 + * @date 2021-12-12 + * + * @copyright Copyright (c) 2021 Ma Pengfei + * + */ \ No newline at end of file diff --git a/projects/Functional/Algebra/AlgebraSolver/NewtonLineSearchSolver.h b/projects/Functional/Algebra/AlgebraSolver/NewtonLineSearchSolver.h new file mode 100644 index 0000000000..1e720c06f7 --- /dev/null +++ b/projects/Functional/Algebra/AlgebraSolver/NewtonLineSearchSolver.h @@ -0,0 +1,13 @@ +/** + * @file NewtonLineSearchSolver.h + * @author Ma Pengfei (mapengfei@mail.nwpu.edu.cn) + * @brief + * @version 0.1 + * @date 2022-01-13 + * + * @copyright Copyright (c) 2022 Ma Pengfei + * + */ + +// for robustness, we need to use Newton line search method because +// Newton method dosen't converge sometimes. \ No newline at end of file diff --git a/projects/Functional/Algebra/AlgebraSolver/NewtonSolver.h b/projects/Functional/Algebra/AlgebraSolver/NewtonSolver.h new file mode 100644 index 0000000000..cac4f692e1 --- /dev/null +++ b/projects/Functional/Algebra/AlgebraSolver/NewtonSolver.h @@ -0,0 +1,101 @@ +/** + * @file NewtonSolver.h + * @author Ma Pengfei (mapengfei@mail.nwpu.edu.cn) + * @brief + * @version 0.1 + * @date 2021-12-09 + * + * @copyright Copyright (c) 2021 Ma Pengfei + * + */ + +#ifndef _NEWTON_SOLVER_ +#define _NEWTON_SOLVER_ + +#include +#include +#include +#include +#include "NonlinearProblem.h" +#include "NewtonSolver.h" +#include "NonlinearSolver.h" +template +class NewtonSolver : public NonlinearSolver +{ +private: + + // using NonlinearSolver::data_; + +public: + + + NewtonSolver(std::shared_ptr> _linear_solver) + : NonlinearSolver::NonlinearSolver(_linear_solver) + { + } + + virtual std::string method() const final { + return "Default newton solver"; + }; + + // TODO : param b is useless here. + virtual std::pair> Solve( + std::shared_ptr> nonlinear_problem, + VectorType &x0, + const VectorType &b) final { + + // NOTE : it will not the number of calls of cudaMalloc for GpuVector. + // Therefore, it is not very expensive. + nonlinear_problem->resize(x0.size()); + + auto& xk = nonlinear_problem->get_xk(); + auto& rhs = nonlinear_problem->get_rhs(); + auto& delta_x = nonlinear_problem->get_delta_x(); + auto& residual = nonlinear_problem->get_residual(); + + auto linear_solver = this->get_linear_solver(); + double norm_b = std::sqrt(b.inner(b)); + double norm_r; + int iter = 0; + + // NOTE : Set initial value for newton iteration. + xk = x0; + + do{ + iter++; + // NOTE : rhs = h(xk), here we solve A(-dx)=rhs, and finnally xk = xk - (-dx); + nonlinear_problem->Residual(xk, rhs); + + // NOTE : initial guess for the linear solver. + delta_x = rhs; + + // solve delta_x, which is (-dx) actually. + auto linear_result = linear_solver->Solve(nonlinear_problem, delta_x, rhs); + if (linear_result.first) LOG_F(WARNING, "Linear solver succeed."); + else LOG_F(WARNING, "Linear solver failed."); + LOG_F(WARNING, "residual : %lf, iter : %d", linear_result.second.first, linear_result.second.second); + // LOG_F(WARNING, "xk: %.8lf, %.8lf", xk[0], xk[1]); + + // update xk = xk - (-dx); + xk.axpy(-1.0,delta_x, xk); + x0 = xk; + + // calculate the residual. + nonlinear_problem->Residual(residual); + norm_r = std::sqrt(residual.inner(residual)); + LOG_F(WARNING, "norm_r : %lf", norm_r); + + // TODO : assuming norm_e = norm_r/norm_b + if (norm_r < this->max_nonlinear_tolerance) return std::make_pair(true, std::make_pair(norm_r,iter)); + + }while(iter < this->max_nonlinear_iteration); + + return std::make_pair(false, std::make_pair(norm_r,iter)); + }; + + virtual ~NewtonSolver(){ + + } +}; + +#endif \ No newline at end of file diff --git a/projects/Functional/Algebra/AlgebraSolver/NonlinearProblem.h b/projects/Functional/Algebra/AlgebraSolver/NonlinearProblem.h new file mode 100644 index 0000000000..c1951e8513 --- /dev/null +++ b/projects/Functional/Algebra/AlgebraSolver/NonlinearProblem.h @@ -0,0 +1,109 @@ +/** + * @file NonlinearProblem.h + * @author Ma Pengfei (mapengfei@mail.nwpu.edu.cn) + * @brief + * @version 0.1 + * @date 2021-12-09 + * + * @copyright Copyright (c) 2021 Ma Pengfei + * + */ + +#ifndef _NONLINEAR_PROBLEM_ +#define _NONLINEAR_PROBLEM_ + +#include "LinearSolver.h" +#include "LinearProblem.h" +#include "StdVector.h" + +template +class NonlinearProblem : public LinearProblem +{ +private: + + VectorType xk; + VectorType rhs; + VectorType delta_x; + VectorType residual; + + VectorType r1; + VectorType r2; + VectorType xk_plus_epsilon_x; + + // used to calculate Jx = (h(x+epsilon*x0)-h(x))/epsilon + double epsilon = 1e-6; + +public: + + VectorType& get_xk() { return xk; } + VectorType& get_rhs() { return rhs; } + VectorType& get_delta_x() { return delta_x; } + VectorType& get_residual() { return residual; } + + void resize(size_t __size){ + + xk.resize(__size); + rhs.resize(__size); + delta_x.resize(__size); + residual.resize(__size); + r1.resize(__size); + r2.resize(__size); + xk_plus_epsilon_x.resize(__size); + + } + + void set_epsilon(double a) { epsilon = a; } + double get_epsilon() const { return epsilon; } + + NonlinearProblem(){ + + } + + virtual void form(const VectorType& x, VectorType& r) final { + + // Timer timer("function residual in class MyProblem"); + LOG_F(WARNING, "Nonlinear form.\n\n"); + + CHECK_F(x.size() == r.size(), "Wrong size."); + CHECK_F(xk.size() == r.size(), "Wrong size."); + + + + // z = a*x + y + // z.axpy(a,x,y); + // x_k + \epsilon x = xk + epsilon * x; + xk_plus_epsilon_x.axpy(epsilon, x, xk); + + // calculate residuals + Residual(xk, r1); + Residual(xk_plus_epsilon_x, r2); + + // (h(x0+e*x)-h(x0))/e + // r = (r2 - r1)/epsilon + // divided into two steps : + // r = r2 - r1 + // r = r*(1/epsilon) = r + (-1+1/epsilon)*r + + // z = a*x + y + // z.axpy(a,x,y); + r.axpy(-1,r1,r2); + + // x = a*x + y + // x.axpy(a,y); + r.axpy(1.0/epsilon-1,r); + } + + + virtual void Residual(const VectorType& x, VectorType& r) = 0; + + void Residual(VectorType& r){ + Residual(xk, r); + } + + virtual ~NonlinearProblem(){ + + } +}; + + +#endif \ No newline at end of file diff --git a/projects/Functional/Algebra/AlgebraSolver/NonlinearSolver.h b/projects/Functional/Algebra/AlgebraSolver/NonlinearSolver.h new file mode 100644 index 0000000000..ff671b9777 --- /dev/null +++ b/projects/Functional/Algebra/AlgebraSolver/NonlinearSolver.h @@ -0,0 +1,96 @@ +/** + * @file NonlinearSolver.h + * @author Ma Pengfei (mapengfei@mail.nwpu.edu.cn) + * @brief + * @version 0.1 + * @date 2021-12-05 create + * @date 2021-12-09 define NonlinearSolver + * + * @copyright Copyright (c) 2021 Ma Pengfei + * + */ + + +// +// Despite the perception that it is fragile or scary, Newton’s method works well on many nonlinear systems +// if one has a good initial iterate x0 and one adds some important protections. A line search, which +// sometimes moves a shorter distance than computed, is such a “protection”. +// -- PETSc for Partial Differenctial Equations +// +// Here is a list of nonlinear solver copied from dolfin/nls/PETScSNESSolver.h +// +// { +// {"default", {"default SNES method", ""}}, +// {"newtonls", {"Line search method", SNESNEWTONLS}}, +// {"newtontr", {"Trust region method", SNESNEWTONTR}}, +// {"ngmres", {"Nonlinear generalised minimum residual method", SNESNGMRES}}, +// {"nrichardson", {"Richardson nonlinear method (Picard iteration)", SNESNRICHARDSON}}, +// {"vinewtonrsls", {"Reduced space active set solver method (for bounds)", SNESVINEWTONRSLS}}, +// {"vinewtonssls", {"Reduced space active set solver method (for bounds)", SNESVINEWTONSSLS}}, +// {"qn", {"Limited memory quasi-Newton", SNESQN}}, +// {"ncg", {"Nonlinear conjugate gradient method", SNESNCG}}, +// {"fas", {"Full Approximation Scheme nonlinear multigrid method", SNESFAS}}, +// {"nasm", {"Nonlinear Additive Schwartz", SNESNASM}}, +// {"anderson", {"Anderson mixing method", SNESANDERSON}}, +// {"aspin", {"Additive-Schwarz Preconditioned Inexact Newton", SNESASPIN}}, +// {"ms", {"Multistage smoothers", SNESMS}} +// }; +// + +#ifndef _NONLINEAR_SOLVER_ +#define _NONLINEAR_SOLVER_ + +#include +#include +#include +#include "NonlinearProblem.h" +#include "LinearProblem.h" + +template +class NonlinearSolver +{ + +// TODO : private members. +protected: + // Set the problem size. + size_t size; + + // control the convergence. + int max_nonlinear_iteration = 1000; + double max_nonlinear_tolerance = 1e-4; + std::shared_ptr> linear_solver; + +public: + + /** + * @brief Get the size object + * @return size_t + */ + size_t get_size() const { return size; } + + /** + * @brief Get the linear solver object + * @return std::shared_ptr + */ + std::shared_ptr> get_linear_solver() const { return linear_solver; } + + + NonlinearSolver(std::shared_ptr> _linear_solver) : linear_solver(_linear_solver) + { + } + + virtual std::string method() const = 0; + + + virtual std::pair> Solve( + std::shared_ptr> nonlinear_problem, + VectorType &x0, + const VectorType &b) = 0; + + // 类声明外部的说明符无效 + virtual ~NonlinearSolver(){ + + } +}; + +#endif \ No newline at end of file diff --git a/projects/Functional/Algebra/AlgebraSolver/StdVector.h b/projects/Functional/Algebra/AlgebraSolver/StdVector.h new file mode 100644 index 0000000000..6534583730 --- /dev/null +++ b/projects/Functional/Algebra/AlgebraSolver/StdVector.h @@ -0,0 +1,152 @@ +/** + * @file StdVector.h + * @author Ma Pengfei (mapengfei@mail.nwpu.edu.cn) + * @brief + * @version 0.1 + * @date 2021-12-30 + * + * @copyright Copyright (c) 2021 Ma Pengfei + * + */ +#ifndef _STD_VECTOR_H_ +#define _STD_VECTOR_H_ + +#include "GenericVector.h" +#include + +template +class StdVector : public GenericVector +{ + +private: + + using GenericVector::_dim; + + std::vector _data; + + virtual void init() override { _data.resize(this->size()); } + +public: + + + virtual bool use_gpu() const override { return false;} + + virtual TV* data() override { return _data.data();} + + virtual const TV* data() const override { return _data.data();} + + virtual void get(std::vector& data) const override { data = _data; } + + virtual void set(const std::vector& data) override { + _dim = make_int3(static_cast(data.size()),1,1); + _data = data; + } + + virtual double sum() const override { + auto array = flatten(*this); + T sum = T(0); + for (size_t i = 0; i < array.num; i++) + { + sum += array.data[i]; + } + return sum; + } + + /// Return minimum value of vector + virtual double min() const override { + auto array = flatten(*this); + auto result = std::numeric_limits::max(); + for (size_t i = 0; i < array.num; i++) + { + if (array.data[i] < result) result = array.data[i]; + } + return result; + } + + /// Return maximum value of vector + virtual double max() const override { + auto array = flatten(*this); + auto result = std::numeric_limits::min(); + for (size_t i = 0; i < array.num; i++) + { + if (array.data[i] > result) result = array.data[i]; + } + return result; + } + + // dot(a,b); + // a.inner(b); + virtual double inner(const GenericVector& x) const override { + auto _x = flatten(x); + auto _m = flatten(*this); + CHECK_F(_x.num == _m.num, "Wrong size."); + + T sum = 0.0; + for (size_t i = 0; i < _x.num; i++) sum += _x.data[i]*_m.data[i]; + return sum; + } + + // x = y + a*x + // x.axpy(a,y); + + // x = a*x = (a-1)*x + x; + // x.axpy(a-1,x); + + virtual void axpy(T a, const GenericVector& y) override { + + auto _x = flatten(*this); + auto _y = flatten(y); + + CHECK_F(_x.num == _y.num, "Wrong size."); + + for (size_t i = 0; i < _x.num; i++) _x.data[i] = _y.data[i] + a*_x.data[i]; + } + + // z = a*x + y + // z.axpy(a,x,y); + virtual void axpy(T a, const GenericVector& x, const GenericVector& y) override { + + auto _x = flatten(x); + auto _y = flatten(y); + auto _z = flatten(*this); + + CHECK_F(_x.num == _y.num, "Wrong size."); + CHECK_F(_x.num == _z.num, "Wrong size."); + + for (size_t i = 0; i < _x.num; i++) _z.data[i] = a*_x.data[i] + _y.data[i]; + } + + virtual const GenericVector& operator= (T a) override { + auto _x = flatten(*this); + for (size_t i = 0; i < _x.num; i++) _x.data[i] = a; + return *this; + } + + virtual const GenericVector& operator-= (T a) override { + auto _x = flatten(*this); + for (size_t i = 0; i < _x.num; i++) _x.data[i] -= a; + return *this; + } + // virtual StdVector& operator=(const StdVector& x) + // { + // _data = x._data; + // return *this; + // } + + + StdVector() : GenericVector(), _data(0){ + + } + + explicit StdVector(int3 dim) : StdVector() { + CHECK_F(dim.x>=1 && dim.y >= 1 && dim.z >=1, "Wrong dim."); + _dim = dim; + init(); + } + + + ~StdVector(){} + +}; + +#endif diff --git a/projects/Functional/Algebra/CMakeLists.txt b/projects/Functional/Algebra/CMakeLists.txt new file mode 100644 index 0000000000..98a29be64a --- /dev/null +++ b/projects/Functional/Algebra/CMakeLists.txt @@ -0,0 +1,46 @@ +cmake_minimum_required(VERSION 3.16) +set(PROJECT_NAME IBFE) +project(${PROJECT_NAME}) + +# add subdirectories +set(MY_DIRS loguru AlgebraSolver) +set(SOURCE_FILE) +set(MY_SOURCES) + +foreach(DIR ${MY_DIRS}) + add_subdirectory(${DIR}) + message("subdirectory : " ${DIR} "\n") + # Add files in ${DIR} + foreach(SOURCE_FILE ${SOURCES}) + list(APPEND MY_SOURCES ${DIR}/${SOURCE_FILE}) + message("source file : " ${DIR}/${SOURCE_FILE} "\n") + endforeach() +endforeach() + +# GPU_lib is compiled seperately. +add_subdirectory(GPULIB) + +# add executable +add_library(${PROJECT_NAME} OBJECT ${MY_SOURCES}) + +# find and use packages +find_package(DOLFIN REQUIRED) +include(${DOLFIN_USE_FILE}) + +# include headers +target_include_directories(${PROJECT_NAME} PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}") + +# link libraries +target_link_libraries(${PROJECT_NAME} PRIVATE pthread dl) +# target_link_libraries(${PROJECT_NAME} PRIVATE dolfin mshr) +target_link_libraries(${PROJECT_NAME} PUBLIC gpu_lib2) + +# add compiling flags +set_target_properties(${PROJECT_NAME} PROPERTIES COMPILE_FLAGS "${CMAKE_CXX_FLAGS} -fPIC -Wall -g -std=c++17") + + +# add compiling definitions +# target_compile_definitions(${PROJECT_NAME} PUBLIC DEBUG) + +# add tests +# add_subdirectory(test) diff --git a/projects/Functional/Algebra/GPULIB/BasicMesh.cu b/projects/Functional/Algebra/GPULIB/BasicMesh.cu new file mode 100644 index 0000000000..e69de29bb2 diff --git a/projects/Functional/Algebra/GPULIB/CMakeLists.txt b/projects/Functional/Algebra/GPULIB/CMakeLists.txt new file mode 100644 index 0000000000..e7be10fb76 --- /dev/null +++ b/projects/Functional/Algebra/GPULIB/CMakeLists.txt @@ -0,0 +1,33 @@ +find_package(CUDA REQUIRED) + +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC") + +# set(CUDA_TOOLKIT_ROOT_DIR /usr/local/cuda-11.6) +message("The cuda toolkit directory is : " ${CUDA_TOOLKIT_ROOT_DIR} "\n") + +set(CUDA_NVCC_FLAGS ${CUDA_NVCC_FLAGS}; + -use_fast_math; + -gencode arch=compute_60,code=sm_60; + -gencode arch=compute_70,code=sm_70;) + +set(CUDA_LINK_LIBRARIES_KEYWORD PUBLIC) + +cuda_add_library(gpu_lib2 utilities.cu ImmersedBoundaryMethod.cu ImmersedBoudnaryMethod.cpp GpuVector.cu PiecewisePolynomial.cu test_gpu_vector.cu) + +target_include_directories(gpu_lib2 ${CUDA_LINK_LIBRARIES_KEYWORD} ${CUDA_INCLUDE_DIRS}) +target_link_libraries(gpu_lib2 ${CUDA_LINK_LIBRARIES_KEYWORD} ${CUDA_TOOLKIT_ROOT_DIR}/lib64/libcudart_static.a) +target_include_directories(gpu_lib2 ${CUDA_LINK_LIBRARIES_KEYWORD} ${CUDA_TOOLKIT_ROOT_DIR}/samples/common/inc) +target_include_directories(gpu_lib2 PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/cuda-samples/Common") + +# Expose the interface +target_include_directories(gpu_lib2 PUBLIC ./include) + +set_target_properties(gpu_lib2 PROPERTIES CXX_STANDARD_REQUIRED ON) + +set_property(TARGET gpu_lib2 PROPERTY + CMAKE_CXX_STANDARD 17 + CXX_STANDARD_REQUIRED ON + CUDA_STANDARD 17 + CUDA_ARCHITECTURES "60;72") + + diff --git a/projects/Functional/Algebra/GPULIB/GpuVector.cu b/projects/Functional/Algebra/GPULIB/GpuVector.cu new file mode 100644 index 0000000000..1221cc65d9 --- /dev/null +++ b/projects/Functional/Algebra/GPULIB/GpuVector.cu @@ -0,0 +1,173 @@ +/** + * @file GpuVector.cu + * @author Ma Pengfei (mapengfei@mail.nwpu.edu.cn) + * @brief + * @version 0.1 + * @date 2022-01-09 + * + * @copyright Copyright (c) 2022 Ma Pengfei + * + */ + + +#include +#include +#include + +#include + +/// TODO : should there be operations for size_t ? +namespace gpu { + +template +struct absolute_value : public thrust::unary_function +{ + __forceinline__ __host__ __device__ T operator()(const T &x) const + { + return x < T(0) ? -x : x; + } +}; + +template +T gpu_abs_max(T* a, size_t num) +{ + return thrust::transform_reduce(thrust::device_ptr(a), + thrust::device_ptr(a+num), + absolute_value(), + 0, + thrust::maximum()); +} + +template int gpu_abs_max(int* a, size_t num); +template float gpu_abs_max(float* a, size_t num); +template double gpu_abs_max(double* a, size_t num); + +template +T gpu_sum(T* a, size_t num) +{ + return thrust::reduce( thrust::device_ptr(a), + thrust::device_ptr(a+num), + T(0), + thrust::plus()); +} + +template int gpu_sum(int* a, size_t num); +template ulong gpu_sum(ulong* a, size_t num); +template float gpu_sum(float* a, size_t num); +template double gpu_sum(double* a, size_t num); + +template +T gpu_min(T* a, size_t num) +{ + return thrust::reduce( thrust::device_ptr(a), + thrust::device_ptr(a+num), + std::numeric_limits::max(), + thrust::minimum()); +} + +template int gpu_min(int* a, size_t num); +template ulong gpu_min(ulong* a, size_t num); +template float gpu_min(float* a, size_t num); +template double gpu_min(double* a, size_t num); + +template +T gpu_max(T* a, size_t num) +{ + return thrust::reduce( thrust::device_ptr(a), + thrust::device_ptr(a+num), + std::numeric_limits::min(), + thrust::maximum()); +} + +template int gpu_max(int* a, size_t num); +template ulong gpu_max(ulong* a, size_t num); +template float gpu_max(float* a, size_t num); +template double gpu_max(double* a, size_t num); + +template +struct transform_multiple : public thrust::unary_function,T> +{ + __forceinline__ __host__ __device__ T operator()(const thrust::tuple &x) const + { + return thrust::get<0>(x)*thrust::get<1>(x); + } +}; + +// TODO : use const T* ? +template +T gpu_inner(T* a, T* b, size_t num) +{ + // use thrust::transform_reduce and thrust::make_zip_iterator to do this job. + // link for official example : + // https://github.com/NVIDIA/thrust/blob/383fb9a245fcd61e4b6d50a2bab872d7f1a6cc83/examples/padded_grid_reduction.cu#L21-L39 + + return thrust::transform_reduce( + thrust::make_zip_iterator(thrust::make_tuple(thrust::device_ptr(a), thrust::device_ptr(b))), + thrust::make_zip_iterator(thrust::make_tuple(thrust::device_ptr(a), thrust::device_ptr(b))) + num, + transform_multiple(), + T(0), + thrust::plus()); +} + +template int gpu_inner(int* a, int* b, size_t num); +template size_t gpu_inner(size_t* a, size_t* b, size_t num); +template float gpu_inner(float* a, float* b, size_t num); +template double gpu_inner(double* a, double* b, size_t num); + +template +struct transform_axpy : public thrust::binary_function +{ + const T a; + + transform_axpy(T _a) : a(_a) {} + + __forceinline__ __host__ __device__ + T operator()(const T& x, const T& y) const + { + return a*x + y; + } +}; + + +// z = a*x + y +template +void gpu_axpy(T* z, const T* x, const T* y, T a, size_t num) +{ + // usage of thrust::transform: + // Y <- A * X + Y + // thrust::transform(X.begin(), X.end(), Y.begin(), Y.begin(), saxpy_functor(A)); + + // link of example for thrust::transform + // https://github.com/NVIDIA/thrust/blob/1669350bcbc026e2df10ab75bbc4f088761024d1/examples/dot_products_with_zip.cu#L99-L106 + + thrust::transform( thrust::device_ptr(x), + thrust::device_ptr(x+num), + thrust::device_ptr(y), + thrust::device_ptr(z), + transform_axpy(a) ); +} + +template void gpu_axpy(float* z, const float* x, const float* y, float a, size_t num); +template void gpu_axpy(double* z, const double* x, const double* y, double a, size_t num); +template void gpu_axpy(int* z, const int* x, const int* y, int a, size_t num); +template void gpu_axpy(size_t* z, const size_t* x, const size_t* y, size_t a, size_t num); + + +// z = a*x + y +template +void gpu_fill(T* x, T a, size_t num) +{ + thrust::fill(thrust::device_ptr(x), thrust::device_ptr(x+num), a); +} + + + +template void gpu_fill(double* x, double a, size_t num); +template void gpu_fill(float* x, float a, size_t num); +template void gpu_fill(int* x, int a, size_t num); +template void gpu_fill(size_t* x, size_t a, size_t num); + + + + +} \ No newline at end of file diff --git a/projects/Functional/Algebra/GPULIB/ImmersedBoudnaryMethod.cpp b/projects/Functional/Algebra/GPULIB/ImmersedBoudnaryMethod.cpp new file mode 100644 index 0000000000..ee22dabf70 --- /dev/null +++ b/projects/Functional/Algebra/GPULIB/ImmersedBoudnaryMethod.cpp @@ -0,0 +1,111 @@ +/** + * @file ImmersedBoundaryMethod.cpp + * @author Ma Pengfei (mapengfei@mail.nwpu.edu.cn) + * @brief + * @version 0.1 + * @date 2021-12-07 + * + * @copyright Copyright (c) 2021 Ma Pengfei + * + */ +#include +#include + +namespace gpu{ + +double ibm_phi(double r) +{ + double res=0; + double r2 = r*r; + if(r<=2) + res = 0.125*(5-2*r-sqrt(-7+12*r-4*r2)); + if(r<=1) + res = 0.125*(3-2*r+sqrt(1+4*r-4*r2)); + return res; +} + +double ibm_delta3(double3 x, double3 g) +{ + + return ibm_phi(abs(x.x-g.x))*ibm_phi(abs(x.y-g.y))*ibm_phi(abs(x.z-g.z)); +} + +void distribute_force_cpu( + const double3* solid_forces, /// solid_forces + const double4* quadrature_rules, /// quadrature quadrature_rules + double3* fluid_forces, + int num, double h, int3 dim) +{ + for (size_t gidx = 0; gidx < num; gidx++) + { + double4 quadrature_rule = quadrature_rules[gidx]; + int i = floor(quadrature_rule.x/h); + int j = floor(quadrature_rule.y/h); + int k = floor(quadrature_rule.z/h); + double w = quadrature_rule.w; + double3 f = solid_forces[gidx]; + double inv_h3 = 1.0/h/h/h; + + for(int kk=k-1;kk<=k+2;kk++) + { + for(int jj=j-1;jj<=j+2;jj++) + { + for(int ii=i-1;ii<=i+2;ii++) + { + if(!(kk>=0&&kk=0&&jj=0&&ii= 0 && kk < dim.z && jj >= 0 && jj < dim.y && ii >= 0 && ii < dim.x)) { + continue; + } + // NOTE : which one is correct? + // int ridx = ii + jj * dim.x + kk * dim.x * dim.y; + int ridx = ii + jj * dim.x + kk * dim.x * dim.y; + double weight = ibm_delta3(make_double3(pos.x / h, pos.y / h, pos.z / h), + make_double3(ii, jj, kk)); + // BUG : __syncthreads() here is a bug. + // __syncthreads(); + double3 gvalue = fluid_velocities[ridx]; + sum.x += weight * gvalue.x; + sum.y += weight * gvalue.y; + sum.z += weight * gvalue.z; + // printf("interpolate_velocity_kernel : %d, %lf, %lf, %lf\n", ridx, gvalue.x, gvalue.y, gvalue.z); + } + } + } + solid_velocities[gidx] = sum; + } +} +} \ No newline at end of file diff --git a/projects/Functional/Algebra/GPULIB/ImmersedBoundaryMethod.cu b/projects/Functional/Algebra/GPULIB/ImmersedBoundaryMethod.cu new file mode 100644 index 0000000000..f0d8ee67f1 --- /dev/null +++ b/projects/Functional/Algebra/GPULIB/ImmersedBoundaryMethod.cu @@ -0,0 +1,262 @@ +/** + * @file ImmersedBoundaryMethod.cu + * @author Ma Pengfei (mapengfei@mail.nwpu.edu.cn) + * @brief + * @version 0.1 + * @date 2021-12-07 + * + * @copyright Copyright (c) 2021 Ma Pengfei + * + */ +#include"ImmersedBoundaryMethod.h" + +namespace gpu{ +__forceinline__ __device__ double ibm_phi(double r) +{ + double res=0; + double r2 = r*r; + if(r<=2) + res = 0.125*(5-2*r-sqrt(-7+12*r-4*r2)); + if(r<=1) + res = 0.125*(3-2*r+sqrt(1+4*r-4*r2)); + return res; +} + +__forceinline__ __device__ double ibm_delta3(double3 x, double3 g, double h) +{ + + return ibm_phi(abs(x.x-g.x))*ibm_phi(abs(x.y-g.y))*ibm_phi(abs(x.z-g.z)); +} + +__forceinline__ __device__ double ibm_delta3(double3 x, double3 g) +{ + + return ibm_phi(abs(x.x-g.x))*ibm_phi(abs(x.y-g.y))*ibm_phi(abs(x.z-g.z)); +} + +/// points Spread force from solid points to grid points +/// inspired by xinxin's immersed boudnary method +__global__ void distribute_force_kernel( + const double3* solid_forces, /// solid_forces + const double4* quadrature_rules, /// quadrature quadrature_rules + double3* fluid_forces, + int num, double h, int3 dim) +{ + int gidx = blockIdx.x * blockDim.x + threadIdx.x; + if(gidx=0&&kk=0&&jj=0&&ii 0.01) + // printf("distribute_force_kernel : %d, %lf, %lf, %lf\n", widx, fluid_forces[widx].x, fluid_forces[widx].y, fluid_forces[widx].z); + } + } + } + } +} + + +__global__ void interpolate_velocity_kernel( + double3* solid_velocities, + const double4* quadrature_rules, + const double3* fluid_velocities, + int num, double h, int3 dim) +{ + int gidx = blockIdx.x * blockDim.x + threadIdx.x; + if(gidx= 0 && kk < dim.z && jj >= 0 && jj < dim.y && ii >= 0 && ii < dim.x)) { + continue; + } + // NOTE : which one is correct? + // int ridx = ii + jj * dim.x + kk * dim.x * dim.y; + int ridx = ii + jj * dim.x + kk * dim.x * dim.y; + double weight = ibm_delta3(make_double3(pos.x / h, pos.y / h, pos.z / h), + make_double3(ii, jj, kk), h); + // BUG : __syncthreads() here is a bug. + // __syncthreads(); + double3 gvalue = fluid_velocities[ridx]; + sum.x += weight * gvalue.x; + sum.y += weight * gvalue.y; + sum.z += weight * gvalue.z; + // printf("interpolate_velocity_kernel : %d, %lf, %lf, %lf\n", ridx, gvalue.x, gvalue.y, gvalue.z); + } + } + } + // BUG : __syncthreads() here is a bug. + // __syncthreads(); + solid_velocities[gidx] = sum; + } +} + + + void distribute_force_gpu( + const double3* solid_forces, /// solid_forces + const double4* quadrature_rules, /// quadrature quadrature_rules + double3* fluid_forces, + size_t num, double h, int3 dim) + { + std::cout << "\n\n call kernel distribute_force.\n\n" << std::endl; + uint numThreads, numBlocks; + computeGridSize(num, 256, numBlocks, numThreads); + distribute_force_kernel<<>>(solid_forces, quadrature_rules, fluid_forces, num, h, dim); + cudaDeviceSynchronize(); + getLastCudaError("Kernel execution failed"); + } + + + void interpolate_velocity_gpu( + double3* solid_velocities, + const double4* quadrature_rules, + const double3* fluid_velocities, + size_t num, double h, int3 dim + ){ + std::cout << "\n\n call function interpolate_velocity_gpu:\n\n" << std::endl; + uint numThreads, numBlocks; + computeGridSize(num, 256, numBlocks, numThreads); + interpolate_velocity_kernel<<>>(solid_velocities, quadrature_rules, fluid_velocities, num, h, dim); + cudaDeviceSynchronize(); + getLastCudaError("Kernel execution failed"); + } + + // Stay the night because I need this more than I knew, more than I would like, more than you do. + // I need this function now not in the future. + void distribute_force( + const double3* solid_forces, /// solid_forces + const double4* quadrature_rules, /// quadrature quadrature_rules + double3* fluid_forces, + size_t num, double h, int3 dim, bool useCUDA = true) + { + if(useCUDA){ + cudaDeviceSynchronize(); + Timer timer("distribute_force with cuda."); + + double3* solid_forces_dev; + double4* quadrature_rules_dev; + double3* fluid_forces_dev; + + // Malloc memory + checkCudaErrors(cudaMalloc((void **)&solid_forces_dev, num*sizeof(double3))); + checkCudaErrors(cudaMalloc((void **)&quadrature_rules_dev, num*sizeof(double4))); + checkCudaErrors(cudaMalloc((void **)&fluid_forces_dev, dim.x*dim.y*dim.z*sizeof(double3))); + + // Copy + checkCudaErrors(cudaMemcpy(solid_forces_dev, solid_forces, num*sizeof(double3), cudaMemcpyHostToDevice)); + checkCudaErrors(cudaMemcpy(quadrature_rules_dev, quadrature_rules, num*sizeof(double4), cudaMemcpyHostToDevice)); + + // Call + distribute_force_gpu(solid_forces_dev, quadrature_rules_dev, fluid_forces_dev, num, h, dim); + + // Copy back the results + checkCudaErrors(cudaMemcpy(fluid_forces, fluid_forces_dev, dim.x*dim.y*dim.z*sizeof(double3), cudaMemcpyDeviceToHost)); + + // for (size_t i = 0; i < dim.x*dim.y*dim.z; i++) + // { + // if (fabs(fluid_forces[i].x) > 0.01) + // printf("distribute_force_kernel : %d, %lf, %lf, %lf\n", i, fluid_forces[i].x, fluid_forces[i].y, fluid_forces[i].z); + // } + + // Free + checkCudaErrors(cudaFree(solid_forces_dev)); + checkCudaErrors(cudaFree(quadrature_rules_dev)); + checkCudaErrors(cudaFree(fluid_forces_dev)); + cudaDeviceSynchronize(); + } else { + cudaDeviceSynchronize(); + Timer timer("distribute_force without cuda."); + distribute_force_cpu(solid_forces, quadrature_rules, fluid_forces, num, h, dim); + cudaDeviceSynchronize(); + } + } + + + void interpolate_velocity( + double3* solid_velocities, /// solid_forces + const double4* quadrature_rules, /// quadrature quadrature_rules + const double3* fluid_velocities, + size_t num, double h, int3 dim, bool useCUDA = true) + { + if (useCUDA) + { + cudaDeviceSynchronize(); + Timer timer("interpolate_velocity with cuda."); + + double3* solid_velocities_dev; + double4* quadrature_rules_dev; + double3* fluid_velocities_dev; + + // Malloc memory + checkCudaErrors(cudaMalloc((void **)&solid_velocities_dev, num*sizeof(double3))); + checkCudaErrors(cudaMalloc((void **)&quadrature_rules_dev, num*sizeof(double4))); + checkCudaErrors(cudaMalloc((void **)&fluid_velocities_dev, dim.x*dim.y*dim.z*sizeof(double3))); + + // for (size_t i = 0; i < dim.x*dim.y*dim.z; i++) + // { + // printf("interpolate_velocity_cpu : %d, %lf, %lf, %lf\n", i, fluid_velocities[i].x, fluid_velocities[i].y, fluid_velocities[i].z); + // /* code */ + // } + + // Copy + checkCudaErrors(cudaMemcpy(fluid_velocities_dev, fluid_velocities, dim.x*dim.y*dim.z*sizeof(double3), cudaMemcpyHostToDevice)); + checkCudaErrors(cudaMemcpy(quadrature_rules_dev, quadrature_rules, num*sizeof(double4), cudaMemcpyHostToDevice)); + + // Call + interpolate_velocity_gpu(solid_velocities_dev, quadrature_rules_dev, fluid_velocities_dev, num, h, dim); + + // Copy back the results + checkCudaErrors(cudaMemcpy(solid_velocities, solid_velocities_dev, num*sizeof(double3), cudaMemcpyDeviceToHost)); + + // Free + checkCudaErrors(cudaFree(solid_velocities_dev)); + checkCudaErrors(cudaFree(quadrature_rules_dev)); + checkCudaErrors(cudaFree(fluid_velocities_dev)); + cudaDeviceSynchronize(); + } + else { + cudaDeviceSynchronize(); + Timer timer("interpolate_velocity without cuda."); + interpolate_velocity_cpu(solid_velocities, quadrature_rules, fluid_velocities, num, h, dim); + cudaDeviceSynchronize(); + } + } + + +} \ No newline at end of file diff --git a/projects/Functional/Algebra/GPULIB/ImmersedBoundaryMethod.h b/projects/Functional/Algebra/GPULIB/ImmersedBoundaryMethod.h new file mode 100644 index 0000000000..b9e26dc050 --- /dev/null +++ b/projects/Functional/Algebra/GPULIB/ImmersedBoundaryMethod.h @@ -0,0 +1,32 @@ +/** + * @file ImmersedBoundaryMethod.h + * @author Ma Pengfei (mapengfei@mail.nwpu.edu.cn) + * @brief + * @version 0.1 + * @date 2021-12-07 + * + * @copyright Copyright (c) 2021 Ma Pengfei + * + */ +#ifndef __IMMERSEDBOUNDARYMETHOD_H__ +#define __IMMERSEDBOUNDARYMETHOD_H__ +#include "utilities.h" +#include "../Timer.h" + +namespace gpu{ +void distribute_force_cpu( + const double3* solid_forces, /// solid_forces + const double4* quadrature_rules, /// quadrature quadrature_rules + double3* fluid_forces, + int num, double h, int3 dim); + + +void interpolate_velocity_cpu( + double3* solid_velocities, + const double4* quadrature_rules, + const double3* fluid_velocities, + int num, double h, int3 dim); + +} + +#endif \ No newline at end of file diff --git a/projects/Functional/Algebra/GPULIB/PiecewisePolynomial.cu b/projects/Functional/Algebra/GPULIB/PiecewisePolynomial.cu new file mode 100644 index 0000000000..5d3f3b4d18 --- /dev/null +++ b/projects/Functional/Algebra/GPULIB/PiecewisePolynomial.cu @@ -0,0 +1,184 @@ +#include "utilities.h" + +namespace gpu { + +__device__ double det_3x3(const double* m){ + return m[0]*(m[4]*m[8]-m[7]*m[5]) - m[1]*(m[3]*m[8]-m[6]*m[5])+m[2]*(m[3]*m[7]-m[6]*m[4]); +} + +__device__ void inverse_3x3(const double* input, double* output){ + double det = det_3x3(input); + output[0] = (input[4]*input[8]-input[7]*input[5]) / det; + output[3] = (input[6]*input[5]-input[3]*input[8]) / det; + output[6] = (input[3]*input[7]-input[6]*input[4]) / det; + + output[1] = (input[7]*input[2]-input[1]*input[8]) / det; + output[4] = (input[0]*input[8]-input[6]*input[2]) / det; + output[7] = (input[1]*input[6]-input[0]*input[7]) / det; + + output[2] = (input[1]*input[5]-input[4]*input[2]) / det; + output[5] = (input[3]*input[2]-input[0]*input[5]) / det; + output[8] = (input[0]*input[4]-input[1]*input[3]) / det; +} + +__device__ double tetrahedron_volume(const double3 *points) +{ + // Check that we get a tetrahedr + // Get the coordinates of the four vertices + const double *x0 = (double *)&(points[0]); + const double *x1 = (double *)&(points[1]); + const double *x2 = (double *)&(points[2]); + const double *x3 = (double *)&(points[3]); + + // Formula for volume from http://mathworld.wolfram.com + // I see this formula in /dolfin/mesh/TetrahedronCell.cpp which is a part of fenics. + const double v = (x0[0] * (x1[1] * x2[2] + x3[1] * x1[2] + x2[1] * x3[2] - x2[1] * x1[2] - x1[1] * x3[2] - x3[1] * x2[2]) + - x1[0] * (x0[1] * x2[2] + x3[1] * x0[2] + x2[1] * x3[2] - x2[1] * x0[2] - x0[1] * x3[2] - x3[1] * x2[2]) + + x2[0] * (x0[1] * x1[2] + x3[1] * x0[2] + x1[1] * x3[2] - x1[1] * x0[2] - x0[1] * x3[2] - x3[1] * x1[2]) + - x3[0] * (x0[1] * x1[2] + x1[1] * x2[2] + x2[1] * x0[2] - x1[1] * x0[2] - x2[1] * x1[2] - x0[1] * x2[2])); + + return std::abs(v) / 6.0; +} + +__device__ void get_transformation_operator(const double* p, double* H, double* b, double *inv_H, double* inv_Hb) { + + H[0] = p[3] - p[0]; + H[1] = p[6] - p[0]; + H[2] = p[9] - p[0]; + + H[3] = p[4] - p[1]; + H[4] = p[7] - p[1]; + H[5] = p[10] - p[1]; + + H[6] = p[5] - p[2]; + H[7] = p[8] - p[2]; + H[8] = p[11] - p[2]; + + inverse_3x3(H, inv_H); + + b[0] = p[0]; + b[1] = p[1]; + b[2] = p[2]; + + inv_Hb[0] = - inv_H[0]*p[0] - inv_H[1]*p[1] - inv_H[2]*p[2]; + inv_Hb[1] = - inv_H[3]*p[0] - inv_H[4]*p[1] - inv_H[5]*p[2]; + inv_Hb[2] = - inv_H[6]*p[0] - inv_H[7]*p[1] - inv_H[8]*p[2]; +} + +__device__ void transform_a_point(double* point_out, const double* point_in, const double* A, const double* b) { + + point_out[0] = A[0*3+0]*point_in[0] + A[0*3+1]*point_in[1] + A[0*3+2]*point_in[2]; + point_out[1] = A[1*3+0]*point_in[0] + A[1*3+1]*point_in[1] + A[1*3+2]*point_in[2]; + point_out[2] = A[2*3+0]*point_in[0] + A[2*3+1]*point_in[1] + A[2*3+2]*point_in[2]; + + point_out[0] += b[0]; + point_out[1] += b[1]; + point_out[2] += b[2]; +} + +// NOTE: How the dofs are arranged? +// [x,y,z,x,y,z,x,y,z,.....] FAULSE +// [x,x,...,y,y,...,z,z,...] TRUE +__device__ void transform_dofs_vector(double* dofs_output, const double* dofs_input){ + for (size_t i = 0; i < 3; i++) // a vector of dimension 3 + { + dofs_output[i+3*0] = 1.0*dofs_input[i+3*0]; + + dofs_output[i+3*1] = -3.0*dofs_input[i+3*0] - 1.0*dofs_input[i+3*1] + 4.0*dofs_input[i+3*9]; + dofs_output[i+3*2] = -3.0*dofs_input[i+3*0] - 1.0*dofs_input[i+3*2] + 4.0*dofs_input[i+3*8]; + dofs_output[i+3*3] = -3.0*dofs_input[i+3*0] - 1.0*dofs_input[i+3*3] + 4.0*dofs_input[i+3*7]; + + dofs_output[i+3*4] = 4.0*dofs_input[i+3*0] + 4.0*dofs_input[i+3*6] - 4.0*dofs_input[i+3*8] - 4.0*dofs_input[i+3*9]; + dofs_output[i+3*5] = 4.0*dofs_input[i+3*0] + 4.0*dofs_input[i+3*5] - 4.0*dofs_input[i+3*7] - 4.0*dofs_input[i+3*9]; + dofs_output[i+3*6] = 4.0*dofs_input[i+3*0] + 4.0*dofs_input[i+3*4] - 4.0*dofs_input[i+3*7] - 4.0*dofs_input[i+3*8]; + + dofs_output[i+3*7] = 2.0*dofs_input[i+3*0] + 2.0*dofs_input[i+3*1] - 4.0*dofs_input[i+3*9]; + dofs_output[i+3*8] = 2.0*dofs_input[i+3*0] + 2.0*dofs_input[i+3*2] - 4.0*dofs_input[i+3*8]; + dofs_output[i+3*9] = 2.0*dofs_input[i+3*0] + 2.0*dofs_input[i+3*3] - 4.0*dofs_input[i+3*7]; + } +} + +__device__ void transform_dofs_scalar(double* dofs_output, const double* dofs_input){ + + dofs_output[0] = 1.0*dofs_input[0]; + + dofs_output[1] = -3.0*dofs_input[0] - 1.0*dofs_input[1] + 4.0*dofs_input[9]; + dofs_output[2] = -3.0*dofs_input[0] - 1.0*dofs_input[2] + 4.0*dofs_input[8]; + dofs_output[3] = -3.0*dofs_input[0] - 1.0*dofs_input[3] + 4.0*dofs_input[7]; + + dofs_output[4] = 4.0*dofs_input[0] + 4.0*dofs_input[6] - 4.0*dofs_input[8] - 4.0*dofs_input[9]; + dofs_output[5] = 4.0*dofs_input[0] + 4.0*dofs_input[5] - 4.0*dofs_input[7] - 4.0*dofs_input[9]; + dofs_output[6] = 4.0*dofs_input[0] + 4.0*dofs_input[4] - 4.0*dofs_input[7] - 4.0*dofs_input[8]; + + dofs_output[7] = 2.0*dofs_input[0] + 2.0*dofs_input[1] - 4.0*dofs_input[9]; + dofs_output[8] = 2.0*dofs_input[0] + 2.0*dofs_input[2] - 4.0*dofs_input[8]; + dofs_output[9] = 2.0*dofs_input[0] + 2.0*dofs_input[3] - 4.0*dofs_input[7]; +} + +__device__ void evaluate_vector(const double *dofs, const double *points, double *results, size_t num) +{ + for (size_t i = 0; i < num; i++) + { + double x = points[3*i]; + double y = points[3*i+1]; + double z = points[3*i+2]; + for (size_t j = 0; j < 3; j++) + { + results[i*3+j] = dofs[j+3*0] + x*dofs[j+3*1] + y*dofs[j+3*2] + z*dofs[j+3*3] + + x*y*dofs[j+3*4] + x*z*dofs[j+3*5] + y*z*dofs[j+3*6] + + x*x*dofs[j+3*7] + y*y*dofs[j+3*8] + z*z*dofs[j+3*9]; + } + } +} + + +__global__ void evaluate_vector_function_for_quadrature_points_kernel( + const double3* function, + const size_t* dofmap, + const double* quadrature_points, + double3* results, + size_t num_gauss, + size_t num_cells) +{ + // There is no need to know the current position of points when evaluating a function + uint index = __umul24(blockIdx.x, blockDim.x) + threadIdx.x; + if (index < num_cells) + // for (size_t index = 0; index < num_cells; index++) + { + double *result = (double*)&(results[num_gauss * index]); + + // Get local dofs from function and dofmap + double3 dof[10]; + for (size_t i = 0; i < 10; i++) + { + dof[i] = function[dofmap[index*10+i]]; + } + + // Evaluate at quadrature points + double dof_params[30]; + transform_dofs_vector(dof_params, (double*)dof); + evaluate_vector(dof_params, quadrature_points, result, num_gauss); + } +} + + +void evaluate_vector_function_for_quadrature_points( + const double3* function, + const size_t* dofmap, + const double* quadrature_points, + double3* results, + size_t num_gauss, + size_t num_cells) +{ + std::cout << "\n\n call kernel get_transformation_operator.\n\n" << std::endl; + uint numThreads, numBlocks; + computeGridSize(num_cells, 256, numBlocks, numThreads); + evaluate_vector_function_for_quadrature_points_kernel<<>>(function, dofmap, quadrature_points, results, num_gauss, num_cells); + cudaDeviceSynchronize(); + getLastCudaError("Kernel execution failed"); +} + + + + +} \ No newline at end of file diff --git a/projects/Functional/Algebra/GPULIB/include/double_math.h b/projects/Functional/Algebra/GPULIB/include/double_math.h new file mode 100644 index 0000000000..cf01d02500 --- /dev/null +++ b/projects/Functional/Algebra/GPULIB/include/double_math.h @@ -0,0 +1,74 @@ + +#ifndef DOUBLE_MATH_H +#define DOUBLE_MATH_H + + +#include "cuda_runtime.h" + + +inline __device__ __host__ double3 operator-(double3 &a) +{ + return make_double3(-a.x, -a.y, -a.z); +} + +inline __device__ __host__ double3 operator+(double3 &a) +{ + return make_double3(-a.x, -a.y, -a.z); +} + +inline __device__ __host__ double3 operator+(double3 a, double3 b) +{ + return make_double3(a.x + b.x, a.y + b.y, a.z + b.z); +} + +inline __device__ __host__ double3 operator-(double3 a, double3 b) +{ + return make_double3(a.x - b.x, a.y - b.y, a.z - b.z); +} + +inline __device__ __host__ double3 operator/(double3 a, double b) +{ + return make_double3(a.x/b, a.y/b, a.z/b); +} + +inline __device__ __host__ double3 operator*(double3 a, double b) +{ + return make_double3(a.x*b, a.y*b, a.z*b); +} + +inline __device__ __host__ double3 operator*(double3 a, double3 b) +{ + return make_double3(a.x*b.x, a.y*b.y, a.z*b.z); +} + +inline __device__ __host__ double3 operator*(double b, double3 a) +{ + return make_double3(a.x*b, a.y*b, a.z*b); +} + +inline __device__ __host__ double4 operator+(double4 a, double4 b) +{ + return make_double4(a.x + b.x, a.y + b.y, a.z + b.z, a.w + a.w); +} + + + + +inline __device__ __host__ int toInt(char a){ + return a; +} + +inline __device__ __host__ int2 toInt(char2 a){ + return make_int2(a.x, a.y); +} + +inline __device__ __host__ int3 toInt(char3 a){ + return make_int3(a.x, a.y, a.z); +} + +inline __device__ __host__ int4 toInt(char4 a){ + return make_int4(a.x, a.y, a.z, a.w); +} + + +#endif \ No newline at end of file diff --git a/projects/Functional/Algebra/GPULIB/include/gpu_lib.h b/projects/Functional/Algebra/GPULIB/include/gpu_lib.h new file mode 100644 index 0000000000..44e691b231 --- /dev/null +++ b/projects/Functional/Algebra/GPULIB/include/gpu_lib.h @@ -0,0 +1,75 @@ +#include +#include +#include + +namespace gpu { +// why we use extern "C" +// https://blog.csdn.net/junparadox/article/details/52704108 + + + /** + * @brief distribute force from solid to fluid + * @param solid_forces variable defined on quadrture points + * @param quadrature_rules quadrature points and quadrature weights + * @param fluid_forces number of quadrature points + * @param num variable defined on regular mesh + * @param h the spacing of regular mesh + * @param dim + */ + void distribute_force( + const double3* solid_forces, + const double4* quadrature_rules, + double3* fluid_forces, + size_t num, double h, int3 dim, bool useCUDA); + + // I do not smile like before. + // I wish I could be someone you need. + + + // ImmersedBoundaryMethod.cu + /** + * @brief + * @param solid_velocities + * @param quadrature_rules + * @param fluid_velocities + * @param num + * @param h + * @param dim + */ + void interpolate_velocity( + double3* solid_velocities, + const double4* quadrature_rules, // weihgts can not be used here. + const double3* fluid_velocities, + size_t num, double h, int3 dim, bool useCUDA); + + void gpu_copy(char* dst, char * src, size_t size); + + void gpu_to_cpu(char* dst, char * src, size_t size); + + void cpu_to_gpu(char* dst, char * src, size_t size); + + void gpu_malloc(void ** buffer, size_t size); + + void freeGPUBuffer(void* buffer); + + // GpuVector.cu + template T gpu_abs_max(T* a, size_t num); + + template T gpu_inner(T* a, T* b, size_t num); + + template void gpu_axpy(T* z, const T* x, const T* y, T a, size_t num); + + template void gpu_fill(T* x, T a, size_t num); + + template T gpu_sum(T* a, size_t num); + template T gpu_min(T* a, size_t num); + template T gpu_max(T* a, size_t num); + + void evaluate_vector_function_for_quadrature_points( + const double3* function, + const size_t* dofmap, + const double* quadrature_points, + double3* results, + size_t num_gauss, + size_t num_cells); +} \ No newline at end of file diff --git a/projects/Functional/Algebra/GPULIB/include/test_gpu_vector.h b/projects/Functional/Algebra/GPULIB/include/test_gpu_vector.h new file mode 100644 index 0000000000..53b8e3dc0d --- /dev/null +++ b/projects/Functional/Algebra/GPULIB/include/test_gpu_vector.h @@ -0,0 +1,17 @@ + +#include +#include + +namespace gpu{ +template void gpu_feprojection_form(const T* x, T* Ax, size_t num); + + +template +void gpu_nonlinear_residual(const T* x, T* r, size_t num); + + + + + + +} \ No newline at end of file diff --git a/projects/Functional/Algebra/GPULIB/test_gpu_vector.cu b/projects/Functional/Algebra/GPULIB/test_gpu_vector.cu new file mode 100644 index 0000000000..fafbbeb32f --- /dev/null +++ b/projects/Functional/Algebra/GPULIB/test_gpu_vector.cu @@ -0,0 +1,77 @@ + + + +#include "utilities.h" + + +namespace gpu { + + +template +__global__ void feprojection_form_kernel(const T* x, T* Ax, size_t num) +{ + double A[3][3] = { + {1.0, 1.0, 3.0}, + {4.0, 3.0, 0.0}, + {8.0, 0.0, 0.0} + }; + + int gidx = blockIdx.x * blockDim.x + threadIdx.x; + + if(gidx +void gpu_feprojection_form(const T* x, T* Ax, size_t num){ + uint numThreads, numBlocks; + computeGridSize(num, 8, numBlocks, numThreads); + feprojection_form_kernel<<>>(x,Ax,num); + cudaDeviceSynchronize(); + getLastCudaError("Kernel execution failed"); +} + + +template void gpu_feprojection_form(const double* x, double* Ax, size_t num); +template void gpu_feprojection_form(const float* x, float* Ax, size_t num); + + + + + +// gpu::gpu_nonlinear_residual(_x.data, _r.data, _x.num); + + + +template +__global__ void nonlinear_residual_kernel(const T* x, T* r, size_t num) +{ + int gidx = blockIdx.x * blockDim.x + threadIdx.x; + + if(gidx == 0){ + r[0] = exp(2.0*x[0])/2.0 - x[1]; + r[1] = x[0]*x[0] + x[1]*x[1]-1.0; + } +} +template +void gpu_nonlinear_residual(const T* x, T* r, size_t num){ + uint numThreads, numBlocks; + computeGridSize(num, 8, numBlocks, numThreads); + nonlinear_residual_kernel<<>>(x,r,num); + cudaDeviceSynchronize(); + getLastCudaError("Kernel execution failed"); +} + + +template void gpu_nonlinear_residual(const double* x, double* r, size_t num); +template void gpu_nonlinear_residual(const float* x, float* r, size_t num); + + + + +} diff --git a/projects/Functional/Algebra/GPULIB/utilities.cu b/projects/Functional/Algebra/GPULIB/utilities.cu new file mode 100644 index 0000000000..4bf65d627d --- /dev/null +++ b/projects/Functional/Algebra/GPULIB/utilities.cu @@ -0,0 +1,52 @@ + +#include "utilities.h" + +namespace gpu { +//TODO : do not use cuda functions outside this directory. +void gpu_copy(char* dst, char * src, size_t size) +{ + //TODO use a compiling tag to switch between gpu buffer type + cudaMemcpy(dst, src, size, cudaMemcpyDeviceToDevice); + cudaDeviceSynchronize(); +} + +void gpu_to_cpu(char* dst, char * src, size_t size) +{ + //TODO use a compiling tag to switch between gpu buffer type + checkCudaErrors(cudaMemcpy(dst, src, size, cudaMemcpyDeviceToHost)); + cudaDeviceSynchronize(); +} + +void cpu_to_gpu(char* dst, char * src, size_t size) +{ + //TODO use a compiling tag to switch between gpu buffer type + checkCudaErrors(cudaMemcpy(dst, src, size, cudaMemcpyHostToDevice)); + cudaDeviceSynchronize(); +} + +void gpu_malloc(void ** buffer, size_t size) +{ + //TODO use a compiling tag to switch between gpu buffer type + cudaMalloc(buffer, size); +} + +void freeGPUBuffer(void* buffer) +{ + + cudaFree(buffer); +} + + +//Round a / b to nearest higher integer value +uint iDivUp(uint a, uint b) +{ + return (a % b != 0) ? (a / b + 1) : (a / b); +} + +// compute grid and thread block size for a given number of elements +void computeGridSize(uint n, uint blockSize, uint &numBlocks, uint &numThreads) +{ + numThreads = min(blockSize, n); + numBlocks = iDivUp(n, numThreads); +} +} \ No newline at end of file diff --git a/projects/Functional/Algebra/GPULIB/utilities.h b/projects/Functional/Algebra/GPULIB/utilities.h new file mode 100644 index 0000000000..0f73ea5cde --- /dev/null +++ b/projects/Functional/Algebra/GPULIB/utilities.h @@ -0,0 +1,30 @@ +#ifndef __UTILITIES_H_ +#define __UTILITIES_H_ +#include +#include +#include +#include "gpu_lib.h" + +#include "../Timer.h" + +namespace gpu { +//TODO : do not use cuda functions outside this directory. +void gpu_copy(char* dst, char * src, size_t size); + +void gpu_to_cpu(char* dst, char * src, size_t size); + +void cpu_to_gpu(char* dst, char * src, size_t size); + +void gpu_malloc(void ** buffer, size_t size); + +void freeGPUBuffer(void* buffer); + +//Round a / b to nearest higher integer value +uint iDivUp(uint a, uint b); + +// compute grid and thread block size for a given number of elements +void computeGridSize(uint n, uint blockSize, uint &numBlocks, uint &numThreads); + +} + +#endif \ No newline at end of file diff --git a/projects/Functional/Algebra/Timer.h b/projects/Functional/Algebra/Timer.h new file mode 100644 index 0000000000..1301fcf9af --- /dev/null +++ b/projects/Functional/Algebra/Timer.h @@ -0,0 +1,44 @@ +/** + * @file Timer.h + * @author Ma Pengfei (mapengfei@mail.nwpu.edu.cn) + * @brief + * @version 0.1 + * @date 2021-12-10 + * + * @copyright Copyright (c) 2021 Ma Pengfei + * + */ + + +#ifndef _TIMER_H_ +#define _TIMER_H_ + +#include +#include +#include + + class Timer + { + public: + + /// Create timer with logging + Timer(std::string task) : _task(task){ + std::cout << task; + } + + /// Destructor + ~Timer() + { + std::cout <<"Time for "<< _task << " : \n" << _timer.format(); + } + + private: + + // Name of task + std::string _task; + + // Implementation of timer + boost::timer::cpu_timer _timer; + +}; +#endif \ No newline at end of file diff --git a/projects/Functional/Algebra/loguru/CMakeLists.txt b/projects/Functional/Algebra/loguru/CMakeLists.txt new file mode 100644 index 0000000000..d4fe9f715f --- /dev/null +++ b/projects/Functional/Algebra/loguru/CMakeLists.txt @@ -0,0 +1,3 @@ +set(SOURCES + loguru.cpp + PARENT_SCOPE) \ No newline at end of file diff --git a/projects/Functional/Algebra/loguru/loguru.cpp b/projects/Functional/Algebra/loguru/loguru.cpp new file mode 100644 index 0000000000..157fbf53b8 --- /dev/null +++ b/projects/Functional/Algebra/loguru/loguru.cpp @@ -0,0 +1,2005 @@ +#if defined(__GNUC__) || defined(__clang__) +// Disable all warnings from gcc/clang: +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wpragmas" + +#pragma GCC diagnostic ignored "-Wc++98-compat" +#pragma GCC diagnostic ignored "-Wc++98-compat-pedantic" +#pragma GCC diagnostic ignored "-Wexit-time-destructors" +#pragma GCC diagnostic ignored "-Wformat-nonliteral" +#pragma GCC diagnostic ignored "-Wglobal-constructors" +#pragma GCC diagnostic ignored "-Wgnu-zero-variadic-macro-arguments" +#pragma GCC diagnostic ignored "-Wmissing-prototypes" +#pragma GCC diagnostic ignored "-Wpadded" +#pragma GCC diagnostic ignored "-Wsign-conversion" +#pragma GCC diagnostic ignored "-Wunknown-pragmas" +#pragma GCC diagnostic ignored "-Wunused-macros" +#pragma GCC diagnostic ignored "-Wzero-as-null-pointer-constant" +#elif defined(_MSC_VER) +#pragma warning(push) +#pragma warning(disable:4365) // conversion from 'X' to 'Y', signed/unsigned mismatch +#endif + +#include + +#ifndef LOGURU_HAS_BEEN_IMPLEMENTED +#define LOGURU_HAS_BEEN_IMPLEMENTED + +#define LOGURU_PREAMBLE_WIDTH (53 + LOGURU_THREADNAME_WIDTH + LOGURU_FILENAME_WIDTH) + +#undef min +#undef max + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#if LOGURU_SYSLOG +#include +#else +#define LOG_USER 0 +#endif + +#ifdef _WIN32 + #include + + #define localtime_r(a, b) localtime_s(b, a) // No localtime_r with MSVC, but arguments are swapped for localtime_s +#else + #include + #include // mkdir + #include // STDERR_FILENO +#endif + +#ifdef __linux__ + #include // PATH_MAX +#elif !defined(_WIN32) + #include // PATH_MAX +#endif + +#ifndef PATH_MAX + #define PATH_MAX 1024 +#endif + +#ifdef __APPLE__ + #include "TargetConditionals.h" +#endif + +// TODO: use defined(_POSIX_VERSION) for some of these things? + +#if defined(_WIN32) || defined(__CYGWIN__) + #define LOGURU_PTHREADS 0 + #define LOGURU_WINTHREADS 1 + #ifndef LOGURU_STACKTRACES + #define LOGURU_STACKTRACES 0 + #endif +#elif defined(__rtems__) || defined(__ANDROID__) || defined(__FreeBSD__) + #define LOGURU_PTHREADS 1 + #define LOGURU_WINTHREADS 0 + #ifndef LOGURU_STACKTRACES + #define LOGURU_STACKTRACES 0 + #endif +#else + #define LOGURU_PTHREADS 1 + #define LOGURU_WINTHREADS 0 + #ifndef LOGURU_STACKTRACES + #define LOGURU_STACKTRACES 1 + #endif +#endif + +#if LOGURU_STACKTRACES + #include // for __cxa_demangle + #include // for dladdr + #include // for backtrace +#endif // LOGURU_STACKTRACES + +#if LOGURU_PTHREADS + #include + #if defined(__FreeBSD__) + #include + #include + #elif defined(__OpenBSD__) + #include + #endif + + #ifdef __linux__ + /* On Linux, the default thread name is the same as the name of the binary. + Additionally, all new threads inherit the name of the thread it got forked from. + For this reason, Loguru use the pthread Thread Local Storage + for storing thread names on Linux. */ + #ifndef LOGURU_PTLS_NAMES + #define LOGURU_PTLS_NAMES 1 + #endif + #endif +#endif + +#if LOGURU_WINTHREADS + #ifndef _WIN32_WINNT + #define _WIN32_WINNT 0x0502 + #endif + #define WIN32_LEAN_AND_MEAN + #define NOMINMAX + #include +#endif + +#ifndef LOGURU_PTLS_NAMES + #define LOGURU_PTLS_NAMES 0 +#endif + + +namespace loguru +{ + using namespace std::chrono; + +#if LOGURU_WITH_FILEABS + struct FileAbs + { + char path[PATH_MAX]; + char mode_str[4]; + Verbosity verbosity; + struct stat st; + FILE* fp; + bool is_reopening = false; // to prevent recursive call in file_reopen. + decltype(steady_clock::now()) last_check_time = steady_clock::now(); + }; +#else + typedef FILE* FileAbs; +#endif + + struct Callback + { + std::string id; + log_handler_t callback; + void* user_data; + Verbosity verbosity; // Does not change! + close_handler_t close; + flush_handler_t flush; + unsigned indentation; + }; + + using CallbackVec = std::vector; + + using StringPair = std::pair; + using StringPairList = std::vector; + + const auto s_start_time = steady_clock::now(); + + Verbosity g_stderr_verbosity = Verbosity_0; + bool g_colorlogtostderr = true; + unsigned g_flush_interval_ms = 0; + bool g_preamble_header = true; + bool g_preamble = true; + + Verbosity g_internal_verbosity = Verbosity_0; + + // Preamble details + bool g_preamble_date = true; + bool g_preamble_time = true; + bool g_preamble_uptime = true; + bool g_preamble_thread = true; + bool g_preamble_file = true; + bool g_preamble_verbose = true; + bool g_preamble_pipe = true; + + static std::recursive_mutex s_mutex; + static Verbosity s_max_out_verbosity = Verbosity_OFF; + static std::string s_argv0_filename; + static std::string s_arguments; + static char s_current_dir[PATH_MAX]; + static CallbackVec s_callbacks; + static fatal_handler_t s_fatal_handler = nullptr; + static verbosity_to_name_t s_verbosity_to_name_callback = nullptr; + static name_to_verbosity_t s_name_to_verbosity_callback = nullptr; + static StringPairList s_user_stack_cleanups; + static bool s_strip_file_path = true; + static std::atomic s_stderr_indentation { 0 }; + + // For periodic flushing: + static std::thread* s_flush_thread = nullptr; + static bool s_needs_flushing = false; + + static SignalOptions s_signal_options = SignalOptions::none(); + + static const bool s_terminal_has_color = [](){ + #ifdef _WIN32 + #ifndef ENABLE_VIRTUAL_TERMINAL_PROCESSING + #define ENABLE_VIRTUAL_TERMINAL_PROCESSING 0x0004 + #endif + + HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); + if (hOut != INVALID_HANDLE_VALUE) { + DWORD dwMode = 0; + GetConsoleMode(hOut, &dwMode); + dwMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING; + return SetConsoleMode(hOut, dwMode) != 0; + } + return false; + #else + if (!isatty(STDERR_FILENO)) { + return false; + } + if (const char* term = getenv("TERM")) { + return 0 == strcmp(term, "cygwin") + || 0 == strcmp(term, "linux") + || 0 == strcmp(term, "rxvt-unicode-256color") + || 0 == strcmp(term, "screen") + || 0 == strcmp(term, "screen-256color") + || 0 == strcmp(term, "screen.xterm-256color") + || 0 == strcmp(term, "tmux-256color") + || 0 == strcmp(term, "xterm") + || 0 == strcmp(term, "xterm-256color") + || 0 == strcmp(term, "xterm-termite") + || 0 == strcmp(term, "xterm-color"); + } else { + return false; + } + #endif + }(); + + static void print_preamble_header(char* out_buff, size_t out_buff_size); + + // ------------------------------------------------------------------------------ + // Colors + + bool terminal_has_color() { return s_terminal_has_color; } + + // Colors + +#ifdef _WIN32 +#define VTSEQ(ID) ("\x1b[1;" #ID "m") +#else +#define VTSEQ(ID) ("\x1b[" #ID "m") +#endif + + const char* terminal_black() { return s_terminal_has_color ? VTSEQ(30) : ""; } + const char* terminal_red() { return s_terminal_has_color ? VTSEQ(31) : ""; } + const char* terminal_green() { return s_terminal_has_color ? VTSEQ(32) : ""; } + const char* terminal_yellow() { return s_terminal_has_color ? VTSEQ(33) : ""; } + const char* terminal_blue() { return s_terminal_has_color ? VTSEQ(34) : ""; } + const char* terminal_purple() { return s_terminal_has_color ? VTSEQ(35) : ""; } + const char* terminal_cyan() { return s_terminal_has_color ? VTSEQ(36) : ""; } + const char* terminal_light_gray() { return s_terminal_has_color ? VTSEQ(37) : ""; } + const char* terminal_white() { return s_terminal_has_color ? VTSEQ(37) : ""; } + const char* terminal_light_red() { return s_terminal_has_color ? VTSEQ(91) : ""; } + const char* terminal_dim() { return s_terminal_has_color ? VTSEQ(2) : ""; } + + // Formating + const char* terminal_bold() { return s_terminal_has_color ? VTSEQ(1) : ""; } + const char* terminal_underline() { return s_terminal_has_color ? VTSEQ(4) : ""; } + + // You should end each line with this! + const char* terminal_reset() { return s_terminal_has_color ? VTSEQ(0) : ""; } + + // ------------------------------------------------------------------------------ +#if LOGURU_WITH_FILEABS + void file_reopen(void* user_data); + inline FILE* to_file(void* user_data) { return reinterpret_cast(user_data)->fp; } +#else + inline FILE* to_file(void* user_data) { return reinterpret_cast(user_data); } +#endif + + void file_log(void* user_data, const Message& message) + { +#if LOGURU_WITH_FILEABS + FileAbs* file_abs = reinterpret_cast(user_data); + if (file_abs->is_reopening) { + return; + } + // It is better checking file change every minute/hour/day, + // instead of doing this every time we log. + // Here check_interval is set to zero to enable checking every time; + const auto check_interval = seconds(0); + if (duration_cast(steady_clock::now() - file_abs->last_check_time) > check_interval) { + file_abs->last_check_time = steady_clock::now(); + file_reopen(user_data); + } + FILE* file = to_file(user_data); + if (!file) { + return; + } +#else + FILE* file = to_file(user_data); +#endif + fprintf(file, "%s%s%s%s\n", + message.preamble, message.indentation, message.prefix, message.message); + if (g_flush_interval_ms == 0) { + fflush(file); + } + } + + void file_close(void* user_data) + { + FILE* file = to_file(user_data); + if (file) { + fclose(file); + } +#if LOGURU_WITH_FILEABS + delete reinterpret_cast(user_data); +#endif + } + + void file_flush(void* user_data) + { + FILE* file = to_file(user_data); + fflush(file); + } + +#if LOGURU_WITH_FILEABS + void file_reopen(void* user_data) + { + FileAbs * file_abs = reinterpret_cast(user_data); + struct stat st; + int ret; + if (!file_abs->fp || (ret = stat(file_abs->path, &st)) == -1 || (st.st_ino != file_abs->st.st_ino)) { + file_abs->is_reopening = true; + if (file_abs->fp) { + fclose(file_abs->fp); + } + if (!file_abs->fp) { + VLOG_F(g_internal_verbosity, "Reopening file '" LOGURU_FMT(s) "' due to previous error", file_abs->path); + } + else if (ret < 0) { + const auto why = errno_as_text(); + VLOG_F(g_internal_verbosity, "Reopening file '" LOGURU_FMT(s) "' due to '" LOGURU_FMT(s) "'", file_abs->path, why.c_str()); + } else { + VLOG_F(g_internal_verbosity, "Reopening file '" LOGURU_FMT(s) "' due to file changed", file_abs->path); + } + // try reopen current file. + if (!create_directories(file_abs->path)) { + LOG_F(ERROR, "Failed to create directories to '" LOGURU_FMT(s) "'", file_abs->path); + } + file_abs->fp = fopen(file_abs->path, file_abs->mode_str); + if (!file_abs->fp) { + LOG_F(ERROR, "Failed to open '" LOGURU_FMT(s) "'", file_abs->path); + } else { + stat(file_abs->path, &file_abs->st); + } + file_abs->is_reopening = false; + } + } +#endif + // ------------------------------------------------------------------------------ + // ------------------------------------------------------------------------------ +#if LOGURU_SYSLOG + void syslog_log(void* /*user_data*/, const Message& message) + { + /* + Level 0: Is reserved for kernel panic type situations. + Level 1: Is for Major resource failure. + Level 2->7 Application level failures + */ + int level; + if (message.verbosity < Verbosity_FATAL) { + level = 1; // System Alert + } else { + switch(message.verbosity) { + case Verbosity_FATAL: level = 2; break; // System Critical + case Verbosity_ERROR: level = 3; break; // System Error + case Verbosity_WARNING: level = 4; break; // System Warning + case Verbosity_INFO: level = 5; break; // System Notice + case Verbosity_1: level = 6; break; // System Info + default: level = 7; break; // System Debug + } + } + + // Note: We don't add the time info. + // This is done automatically by the syslog deamon. + // Otherwise log all information that the file log does. + syslog(level, "%s%s%s", message.indentation, message.prefix, message.message); + } + + void syslog_close(void* /*user_data*/) + { + closelog(); + } + + void syslog_flush(void* /*user_data*/) + {} +#endif +// ------------------------------------------------------------------------------ + // Helpers: + + Text::~Text() { free(_str); } + +#if LOGURU_USE_FMTLIB + Text vtextprintf(const char* format, fmt::format_args args) + { + return Text(STRDUP(fmt::vformat(format, args).c_str())); + } +#else + LOGURU_PRINTF_LIKE(1, 0) + static Text vtextprintf(const char* format, va_list vlist) + { +#ifdef _WIN32 + int bytes_needed = _vscprintf(format, vlist); + CHECK_F(bytes_needed >= 0, "Bad string format: '%s'", format); + char* buff = (char*)malloc(bytes_needed+1); + vsnprintf(buff, bytes_needed+1, format, vlist); + return Text(buff); +#else + char* buff = nullptr; + int result = vasprintf(&buff, format, vlist); + CHECK_F(result >= 0, "Bad string format: '" LOGURU_FMT(s) "'", format); + return Text(buff); +#endif + } + + Text textprintf(const char* format, ...) + { + va_list vlist; + va_start(vlist, format); + auto result = vtextprintf(format, vlist); + va_end(vlist); + return result; + } +#endif + + // Overloaded for variadic template matching. + Text textprintf() + { + return Text(static_cast(calloc(1, 1))); + } + + static const char* indentation(unsigned depth) + { + static const char buff[] = + ". . . . . . . . . . " ". . . . . . . . . . " + ". . . . . . . . . . " ". . . . . . . . . . " + ". . . . . . . . . . " ". . . . . . . . . . " + ". . . . . . . . . . " ". . . . . . . . . . " + ". . . . . . . . . . " ". . . . . . . . . . "; + static const size_t INDENTATION_WIDTH = 4; + static const size_t NUM_INDENTATIONS = (sizeof(buff) - 1) / INDENTATION_WIDTH; + depth = std::min(depth, NUM_INDENTATIONS); + return buff + INDENTATION_WIDTH * (NUM_INDENTATIONS - depth); + } + + static void parse_args(int& argc, char* argv[], const char* verbosity_flag) + { + int arg_dest = 1; + int out_argc = argc; + + for (int arg_it = 1; arg_it < argc; ++arg_it) { + auto cmd = argv[arg_it]; + auto arg_len = strlen(verbosity_flag); + if (strncmp(cmd, verbosity_flag, arg_len) == 0 && !std::isalpha(cmd[arg_len], std::locale(""))) { + out_argc -= 1; + auto value_str = cmd + arg_len; + if (value_str[0] == '\0') { + // Value in separate argument + arg_it += 1; + CHECK_LT_F(arg_it, argc, "Missing verbosiy level after " LOGURU_FMT(s) "", verbosity_flag); + value_str = argv[arg_it]; + out_argc -= 1; + } + if (*value_str == '=') { value_str += 1; } + + auto req_verbosity = get_verbosity_from_name(value_str); + if (req_verbosity != Verbosity_INVALID) { + g_stderr_verbosity = req_verbosity; + } else { + char* end = 0; + g_stderr_verbosity = static_cast(strtol(value_str, &end, 10)); + CHECK_F(end && *end == '\0', + "Invalid verbosity. Expected integer, INFO, WARNING, ERROR or OFF, got '" LOGURU_FMT(s) "'", value_str); + } + } else { + argv[arg_dest++] = argv[arg_it]; + } + } + + argc = out_argc; + argv[argc] = nullptr; + } + + static long long now_ns() + { + return duration_cast(high_resolution_clock::now().time_since_epoch()).count(); + } + + // Returns the part of the path after the last / or \ (if any). + const char* filename(const char* path) + { + for (auto ptr = path; *ptr; ++ptr) { + if (*ptr == '/' || *ptr == '\\') { + path = ptr + 1; + } + } + return path; + } + + // ------------------------------------------------------------------------------ + + static void on_atexit() + { + VLOG_F(g_internal_verbosity, "atexit"); + flush(); + } + + static void install_signal_handlers(const SignalOptions& signal_options); + + static void write_hex_digit(std::string& out, unsigned num) + { + DCHECK_LT_F(num, 16u); + if (num < 10u) { out.push_back(char('0' + num)); } + else { out.push_back(char('A' + num - 10)); } + } + + static void write_hex_byte(std::string& out, uint8_t n) + { + write_hex_digit(out, n >> 4u); + write_hex_digit(out, n & 0x0f); + } + + static void escape(std::string& out, const std::string& str) + { + for (char c : str) { + /**/ if (c == '\a') { out += "\\a"; } + else if (c == '\b') { out += "\\b"; } + else if (c == '\f') { out += "\\f"; } + else if (c == '\n') { out += "\\n"; } + else if (c == '\r') { out += "\\r"; } + else if (c == '\t') { out += "\\t"; } + else if (c == '\v') { out += "\\v"; } + else if (c == '\\') { out += "\\\\"; } + else if (c == '\'') { out += "\\\'"; } + else if (c == '\"') { out += "\\\""; } + else if (c == ' ') { out += "\\ "; } + else if (0 <= c && c < 0x20) { // ASCI control character: + // else if (c < 0x20 || c != (c & 127)) { // ASCII control character or UTF-8: + out += "\\x"; + write_hex_byte(out, static_cast(c)); + } else { out += c; } + } + } + + Text errno_as_text() + { + char buff[256]; + #if defined(__GLIBC__) && defined(_GNU_SOURCE) + // GNU Version + return Text(STRDUP(strerror_r(errno, buff, sizeof(buff)))); + #elif defined(__APPLE__) || _POSIX_C_SOURCE >= 200112L + // XSI Version + strerror_r(errno, buff, sizeof(buff)); + return Text(strdup(buff)); + #elif defined(_WIN32) + strerror_s(buff, sizeof(buff), errno); + return Text(STRDUP(buff)); + #else + // Not thread-safe. + return Text(STRDUP(strerror(errno))); + #endif + } + + void init(int& argc, char* argv[], const Options& options) + { + CHECK_GT_F(argc, 0, "Expected proper argc/argv"); + CHECK_EQ_F(argv[argc], nullptr, "Expected proper argc/argv"); + + s_argv0_filename = filename(argv[0]); + + #ifdef _WIN32 + #define getcwd _getcwd + #endif + + if (!getcwd(s_current_dir, sizeof(s_current_dir))) { + const auto error_text = errno_as_text(); + LOG_F(WARNING, "Failed to get current working directory: " LOGURU_FMT(s) "", error_text.c_str()); + } + + s_arguments = ""; + for (int i = 0; i < argc; ++i) { + escape(s_arguments, argv[i]); + if (i + 1 < argc) { + s_arguments += " "; + } + } + + if (options.verbosity_flag) { + parse_args(argc, argv, options.verbosity_flag); + } + + if (const auto main_thread_name = options.main_thread_name) { + #if LOGURU_PTLS_NAMES || LOGURU_WINTHREADS + set_thread_name(main_thread_name); + #elif LOGURU_PTHREADS + char old_thread_name[16] = {0}; + auto this_thread = pthread_self(); + #if defined(__APPLE__) || defined(__linux__) || defined(__sun) + pthread_getname_np(this_thread, old_thread_name, sizeof(old_thread_name)); + #endif + if (old_thread_name[0] == 0) { + #ifdef __APPLE__ + pthread_setname_np(main_thread_name); + #elif defined(__FreeBSD__) || defined(__OpenBSD__) + pthread_set_name_np(this_thread, main_thread_name); + #elif defined(__linux__) || defined(__sun) + pthread_setname_np(this_thread, main_thread_name); + #endif + } + #endif // LOGURU_PTHREADS + } + + if (g_stderr_verbosity >= Verbosity_INFO) { + if (g_preamble_header) { + char preamble_explain[LOGURU_PREAMBLE_WIDTH]; + print_preamble_header(preamble_explain, sizeof(preamble_explain)); + if (g_colorlogtostderr && s_terminal_has_color) { + fprintf(stderr, "%s%s%s\n", terminal_reset(), terminal_dim(), preamble_explain); + } else { + fprintf(stderr, "%s\n", preamble_explain); + } + } + fflush(stderr); + } + VLOG_F(g_internal_verbosity, "arguments: " LOGURU_FMT(s) "", s_arguments.c_str()); + if (strlen(s_current_dir) != 0) + { + VLOG_F(g_internal_verbosity, "Current dir: " LOGURU_FMT(s) "", s_current_dir); + } + VLOG_F(g_internal_verbosity, "stderr verbosity: " LOGURU_FMT(d) "", g_stderr_verbosity); + VLOG_F(g_internal_verbosity, "-----------------------------------"); + + install_signal_handlers(options.signal_options); + + atexit(on_atexit); + } + + void shutdown() + { + VLOG_F(g_internal_verbosity, "loguru::shutdown()"); + remove_all_callbacks(); + set_fatal_handler(nullptr); + set_verbosity_to_name_callback(nullptr); + set_name_to_verbosity_callback(nullptr); + } + + void write_date_time(char* buff, size_t buff_size) + { + auto now = system_clock::now(); + long long ms_since_epoch = duration_cast(now.time_since_epoch()).count(); + time_t sec_since_epoch = time_t(ms_since_epoch / 1000); + tm time_info; + localtime_r(&sec_since_epoch, &time_info); + snprintf(buff, buff_size, "%04d%02d%02d_%02d%02d%02d.%03lld", + 1900 + time_info.tm_year, 1 + time_info.tm_mon, time_info.tm_mday, + time_info.tm_hour, time_info.tm_min, time_info.tm_sec, ms_since_epoch % 1000); + } + + const char* argv0_filename() + { + return s_argv0_filename.c_str(); + } + + const char* arguments() + { + return s_arguments.c_str(); + } + + const char* current_dir() + { + return s_current_dir; + } + + const char* home_dir() + { + #ifdef __MINGW32__ + auto home = getenv("USERPROFILE"); + CHECK_F(home != nullptr, "Missing USERPROFILE"); + return home; + #elif defined(_WIN32) + char* user_profile; + size_t len; + errno_t err = _dupenv_s(&user_profile, &len, "USERPROFILE"); + CHECK_F(err == 0, "Missing USERPROFILE"); + return user_profile; + #else // _WIN32 + auto home = getenv("HOME"); + CHECK_F(home != nullptr, "Missing HOME"); + return home; + #endif // _WIN32 + } + + void suggest_log_path(const char* prefix, char* buff, unsigned buff_size) + { + if (prefix[0] == '~') { + snprintf(buff, buff_size - 1, "%s%s", home_dir(), prefix + 1); + } else { + snprintf(buff, buff_size - 1, "%s", prefix); + } + + // Check for terminating / + size_t n = strlen(buff); + if (n != 0) { + if (buff[n - 1] != '/') { + CHECK_F(n + 2 < buff_size, "Filename buffer too small"); + buff[n] = '/'; + buff[n + 1] = '\0'; + } + } + + #ifdef _WIN32 + strncat_s(buff, buff_size - strlen(buff) - 1, s_argv0_filename.c_str(), buff_size - strlen(buff) - 1); + strncat_s(buff, buff_size - strlen(buff) - 1, "/", buff_size - strlen(buff) - 1); + write_date_time(buff + strlen(buff), buff_size - strlen(buff)); + strncat_s(buff, buff_size - strlen(buff) - 1, ".log", buff_size - strlen(buff) - 1); + #else + strncat(buff, s_argv0_filename.c_str(), buff_size - strlen(buff) - 1); + strncat(buff, "/", buff_size - strlen(buff) - 1); + write_date_time(buff + strlen(buff), buff_size - strlen(buff)); + strncat(buff, ".log", buff_size - strlen(buff) - 1); + #endif + } + + bool create_directories(const char* file_path_const) + { + CHECK_F(file_path_const && *file_path_const); + char* file_path = STRDUP(file_path_const); + for (char* p = strchr(file_path + 1, '/'); p; p = strchr(p + 1, '/')) { + *p = '\0'; + + #ifdef _WIN32 + if (_mkdir(file_path) == -1) { + #else + if (mkdir(file_path, 0755) == -1) { + #endif + if (errno != EEXIST) { + LOG_F(ERROR, "Failed to create directory '" LOGURU_FMT(s) "'", file_path); + LOG_IF_F(ERROR, errno == EACCES, "EACCES"); + LOG_IF_F(ERROR, errno == ENAMETOOLONG, "ENAMETOOLONG"); + LOG_IF_F(ERROR, errno == ENOENT, "ENOENT"); + LOG_IF_F(ERROR, errno == ENOTDIR, "ENOTDIR"); + LOG_IF_F(ERROR, errno == ELOOP, "ELOOP"); + + *p = '/'; + free(file_path); + return false; + } + } + *p = '/'; + } + free(file_path); + return true; + } + bool add_file(const char* path_in, FileMode mode, Verbosity verbosity) + { + char path[PATH_MAX]; + if (path_in[0] == '~') { + snprintf(path, sizeof(path) - 1, "%s%s", home_dir(), path_in + 1); + } else { + snprintf(path, sizeof(path) - 1, "%s", path_in); + } + + if (!create_directories(path)) { + LOG_F(ERROR, "Failed to create directories to '" LOGURU_FMT(s) "'", path); + } + + const char* mode_str = (mode == FileMode::Truncate ? "w" : "a"); + FILE* file; + #ifdef _WIN32 + errno_t file_error = fopen_s(&file, path, mode_str); + if (file_error) { + #else + file = fopen(path, mode_str); + if (!file) { + #endif + LOG_F(ERROR, "Failed to open '" LOGURU_FMT(s) "'", path); + return false; + } +#if LOGURU_WITH_FILEABS + FileAbs* file_abs = new FileAbs(); // this is deleted in file_close; + snprintf(file_abs->path, sizeof(file_abs->path) - 1, "%s", path); + snprintf(file_abs->mode_str, sizeof(file_abs->mode_str) - 1, "%s", mode_str); + stat(file_abs->path, &file_abs->st); + file_abs->fp = file; + file_abs->verbosity = verbosity; + add_callback(path_in, file_log, file_abs, verbosity, file_close, file_flush); +#else + add_callback(path_in, file_log, file, verbosity, file_close, file_flush); +#endif + + if (mode == FileMode::Append) { + fprintf(file, "\n\n\n\n\n"); + } + if (!s_arguments.empty()) { + fprintf(file, "arguments: %s\n", s_arguments.c_str()); + } + if (strlen(s_current_dir) != 0) { + fprintf(file, "Current dir: %s\n", s_current_dir); + } + fprintf(file, "File verbosity level: %d\n", verbosity); + if (g_preamble_header) { + char preamble_explain[LOGURU_PREAMBLE_WIDTH]; + print_preamble_header(preamble_explain, sizeof(preamble_explain)); + fprintf(file, "%s\n", preamble_explain); + } + fflush(file); + + VLOG_F(g_internal_verbosity, "Logging to '" LOGURU_FMT(s) "', mode: '" LOGURU_FMT(s) "', verbosity: " LOGURU_FMT(d) "", path, mode_str, verbosity); + return true; + } + + /* + Will add syslog as a standard sink for log messages + Any logging message with a verbosity lower or equal to + the given verbosity will be included. + + This works for Unix like systems (i.e. Linux/Mac) + There is no current implementation for Windows (as I don't know the + equivalent calls or have a way to test them). If you know please + add and send a pull request. + + The code should still compile under windows but will only generate + a warning message that syslog is unavailable. + + Search for LOGURU_SYSLOG to find and fix. + */ + bool add_syslog(const char* app_name, Verbosity verbosity) + { + return add_syslog(app_name, verbosity, LOG_USER); + } + bool add_syslog(const char* app_name, Verbosity verbosity, int facility) + { +#if LOGURU_SYSLOG + if (app_name == nullptr) { + app_name = argv0_filename(); + } + openlog(app_name, 0, facility); + add_callback("'syslog'", syslog_log, nullptr, verbosity, syslog_close, syslog_flush); + + VLOG_F(g_internal_verbosity, "Logging to 'syslog' , verbosity: " LOGURU_FMT(d) "", verbosity); + return true; +#else + (void)app_name; + (void)verbosity; + (void)facility; + VLOG_F(g_internal_verbosity, "syslog not implemented on this system. Request to install syslog logging ignored."); + return false; +#endif + } + // Will be called right before abort(). + void set_fatal_handler(fatal_handler_t handler) + { + s_fatal_handler = handler; + } + + fatal_handler_t get_fatal_handler() + { + return s_fatal_handler; + } + + void set_verbosity_to_name_callback(verbosity_to_name_t callback) + { + s_verbosity_to_name_callback = callback; + } + + void set_name_to_verbosity_callback(name_to_verbosity_t callback) + { + s_name_to_verbosity_callback = callback; + } + + void add_stack_cleanup(const char* find_this, const char* replace_with_this) + { + if (strlen(find_this) <= strlen(replace_with_this)) { + LOG_F(WARNING, "add_stack_cleanup: the replacement should be shorter than the pattern!"); + return; + } + + s_user_stack_cleanups.push_back(StringPair(find_this, replace_with_this)); + } + + static void on_callback_change() + { + s_max_out_verbosity = Verbosity_OFF; + for (const auto& callback : s_callbacks) { + s_max_out_verbosity = std::max(s_max_out_verbosity, callback.verbosity); + } + } + + void add_callback( + const char* id, + log_handler_t callback, + void* user_data, + Verbosity verbosity, + close_handler_t on_close, + flush_handler_t on_flush) + { + std::lock_guard lock(s_mutex); + s_callbacks.push_back(Callback{id, callback, user_data, verbosity, on_close, on_flush, 0}); + on_callback_change(); + } + + // Returns a custom verbosity name if one is available, or nullptr. + // See also set_verbosity_to_name_callback. + const char* get_verbosity_name(Verbosity verbosity) + { + auto name = s_verbosity_to_name_callback + ? (*s_verbosity_to_name_callback)(verbosity) + : nullptr; + + // Use standard replacements if callback fails: + if (!name) + { + if (verbosity <= Verbosity_FATAL) { + name = "FATL"; + } else if (verbosity == Verbosity_ERROR) { + name = "ERR"; + } else if (verbosity == Verbosity_WARNING) { + name = "WARN"; + } else if (verbosity == Verbosity_INFO) { + name = "INFO"; + } + } + + return name; + } + + // Returns Verbosity_INVALID if the name is not found. + // See also set_name_to_verbosity_callback. + Verbosity get_verbosity_from_name(const char* name) + { + auto verbosity = s_name_to_verbosity_callback + ? (*s_name_to_verbosity_callback)(name) + : Verbosity_INVALID; + + // Use standard replacements if callback fails: + if (verbosity == Verbosity_INVALID) { + if (strcmp(name, "OFF") == 0) { + verbosity = Verbosity_OFF; + } else if (strcmp(name, "INFO") == 0) { + verbosity = Verbosity_INFO; + } else if (strcmp(name, "WARNING") == 0) { + verbosity = Verbosity_WARNING; + } else if (strcmp(name, "ERROR") == 0) { + verbosity = Verbosity_ERROR; + } else if (strcmp(name, "FATAL") == 0) { + verbosity = Verbosity_FATAL; + } + } + + return verbosity; + } + + bool remove_callback(const char* id) + { + std::lock_guard lock(s_mutex); + auto it = std::find_if(begin(s_callbacks), end(s_callbacks), [&](const Callback& c) { return c.id == id; }); + if (it != s_callbacks.end()) { + if (it->close) { it->close(it->user_data); } + s_callbacks.erase(it); + on_callback_change(); + return true; + } else { + LOG_F(ERROR, "Failed to locate callback with id '" LOGURU_FMT(s) "'", id); + return false; + } + } + + void remove_all_callbacks() + { + std::lock_guard lock(s_mutex); + for (auto& callback : s_callbacks) { + if (callback.close) { + callback.close(callback.user_data); + } + } + s_callbacks.clear(); + on_callback_change(); + } + + // Returns the maximum of g_stderr_verbosity and all file/custom outputs. + Verbosity current_verbosity_cutoff() + { + return g_stderr_verbosity > s_max_out_verbosity ? + g_stderr_verbosity : s_max_out_verbosity; + } + + // ------------------------------------------------------------------------ + // Threads names + +#if LOGURU_PTLS_NAMES + static pthread_once_t s_pthread_key_once = PTHREAD_ONCE_INIT; + static pthread_key_t s_pthread_key_name; + + void make_pthread_key_name() + { + (void)pthread_key_create(&s_pthread_key_name, free); + } +#endif + +#if LOGURU_WINTHREADS + // Where we store the custom thread name set by `set_thread_name` + char* thread_name_buffer() + { + __declspec( thread ) static char thread_name[LOGURU_THREADNAME_WIDTH + 1] = {0}; + return &thread_name[0]; + } +#endif // LOGURU_WINTHREADS + + void set_thread_name(const char* name) + { + #if LOGURU_PTLS_NAMES + // Store thread name in thread-local storage at `s_pthread_key_name` + (void)pthread_once(&s_pthread_key_once, make_pthread_key_name); + (void)pthread_setspecific(s_pthread_key_name, STRDUP(name)); + #elif LOGURU_PTHREADS + // Tell the OS the thread name + #ifdef __APPLE__ + pthread_setname_np(name); + #elif defined(__FreeBSD__) || defined(__OpenBSD__) + pthread_set_name_np(pthread_self(), name); + #elif defined(__linux__) || defined(__sun) + pthread_setname_np(pthread_self(), name); + #endif + #elif LOGURU_WINTHREADS + // Store thread name in a thread-local storage: + strncpy_s(thread_name_buffer(), LOGURU_THREADNAME_WIDTH + 1, name, _TRUNCATE); + #else // LOGURU_PTHREADS + // TODO: on these weird platforms we should also store the thread name + // in a generic thread-local storage. + (void)name; + #endif // LOGURU_PTHREADS + } + + void get_thread_name(char* buffer, unsigned long long length, bool right_align_hex_id) + { + CHECK_NE_F(length, 0u, "Zero length buffer in get_thread_name"); + CHECK_NOTNULL_F(buffer, "nullptr in get_thread_name"); + + #if LOGURU_PTLS_NAMES + (void)pthread_once(&s_pthread_key_once, make_pthread_key_name); + if (const char* name = static_cast(pthread_getspecific(s_pthread_key_name))) { + snprintf(buffer, static_cast(length), "%s", name); + } else { + buffer[0] = 0; + } + #elif LOGURU_PTHREADS + // Ask the OS about the thread name. + // This is what we *want* to do on all platforms, but + // only some platforms support it (currently). + pthread_getname_np(pthread_self(), buffer, length); + #elif LOGURU_WINTHREADS + snprintf(buffer, static_cast(length), "%s", thread_name_buffer()); + #else + // Thread names unsupported + buffer[0] = 0; + #endif + + if (buffer[0] == 0) { + // We failed to get a readable thread name. + // Write a HEX thread ID instead. + // We try to get an ID that is the same as the ID you could + // read in your debugger, system monitor etc. + + #ifdef __APPLE__ + uint64_t thread_id; + pthread_threadid_np(pthread_self(), &thread_id); + #elif defined(__FreeBSD__) + long thread_id; + (void)thr_self(&thread_id); + #elif LOGURU_PTHREADS + uint64_t thread_id = pthread_self(); + #else + // This ID does not correllate to anything we can get from the OS, + // so this is the worst way to get the ID. + const auto thread_id = std::hash{}(std::this_thread::get_id()); + #endif + + if (right_align_hex_id) { + snprintf(buffer, static_cast(length), "%*X", static_cast(length - 1), static_cast(thread_id)); + } else { + snprintf(buffer, static_cast(length), "%X", static_cast(thread_id)); + } + } + } + + // ------------------------------------------------------------------------ + // Stack traces + +#if LOGURU_STACKTRACES + Text demangle(const char* name) + { + int status = -1; + char* demangled = abi::__cxa_demangle(name, 0, 0, &status); + Text result{status == 0 ? demangled : STRDUP(name)}; + return result; + } + + #if LOGURU_RTTI + template + std::string type_name() + { + auto demangled = demangle(typeid(T).name()); + return demangled.c_str(); + } + #endif // LOGURU_RTTI + + static const StringPairList REPLACE_LIST = { + #if LOGURU_RTTI + { type_name(), "std::string" }, + { type_name(), "std::wstring" }, + { type_name(), "std::u16string" }, + { type_name(), "std::u32string" }, + #endif // LOGURU_RTTI + { "std::__1::", "std::" }, + { "__thiscall ", "" }, + { "__cdecl ", "" }, + }; + + void do_replacements(const StringPairList& replacements, std::string& str) + { + for (auto&& p : replacements) { + if (p.first.size() <= p.second.size()) { + // On gcc, "type_name()" is "std::string" + continue; + } + + size_t it; + while ((it=str.find(p.first)) != std::string::npos) { + str.replace(it, p.first.size(), p.second); + } + } + } + + std::string prettify_stacktrace(const std::string& input) + { + std::string output = input; + + do_replacements(s_user_stack_cleanups, output); + do_replacements(REPLACE_LIST, output); + + try { + std::regex std_allocator_re(R"(,\s*std::allocator<[^<>]+>)"); + output = std::regex_replace(output, std_allocator_re, std::string("")); + + std::regex template_spaces_re(R"(<\s*([^<> ]+)\s*>)"); + output = std::regex_replace(output, template_spaces_re, std::string("<$1>")); + } catch (std::regex_error&) { + // Probably old GCC. + } + + return output; + } + + std::string stacktrace_as_stdstring(int skip) + { + // From https://gist.github.com/fmela/591333 + void* callstack[128]; + const auto max_frames = sizeof(callstack) / sizeof(callstack[0]); + int num_frames = backtrace(callstack, max_frames); + char** symbols = backtrace_symbols(callstack, num_frames); + + std::string result; + // Print stack traces so the most relevant ones are written last + // Rationale: http://yellerapp.com/posts/2015-01-22-upside-down-stacktraces.html + for (int i = num_frames - 1; i >= skip; --i) { + char buf[1024]; + Dl_info info; + if (dladdr(callstack[i], &info) && info.dli_sname) { + char* demangled = NULL; + int status = -1; + if (info.dli_sname[0] == '_') { + demangled = abi::__cxa_demangle(info.dli_sname, 0, 0, &status); + } + snprintf(buf, sizeof(buf), "%-3d %*p %s + %zd\n", + i - skip, int(2 + sizeof(void*) * 2), callstack[i], + status == 0 ? demangled : + info.dli_sname == 0 ? symbols[i] : info.dli_sname, + static_cast(callstack[i]) - static_cast(info.dli_saddr)); + free(demangled); + } else { + snprintf(buf, sizeof(buf), "%-3d %*p %s\n", + i - skip, int(2 + sizeof(void*) * 2), callstack[i], symbols[i]); + } + result += buf; + } + free(symbols); + + if (num_frames == max_frames) { + result = "[truncated]\n" + result; + } + + if (!result.empty() && result[result.size() - 1] == '\n') { + result.resize(result.size() - 1); + } + + return prettify_stacktrace(result); + } + +#else // LOGURU_STACKTRACES + Text demangle(const char* name) + { + return Text(STRDUP(name)); + } + + std::string stacktrace_as_stdstring(int) + { + // No stacktraces available on this platform" + return ""; + } + +#endif // LOGURU_STACKTRACES + + Text stacktrace(int skip) + { + auto str = stacktrace_as_stdstring(skip + 1); + return Text(STRDUP(str.c_str())); + } + + // ------------------------------------------------------------------------ + + static void print_preamble_header(char* out_buff, size_t out_buff_size) + { + if (out_buff_size == 0) { return; } + out_buff[0] = '\0'; + size_t pos = 0; + if (g_preamble_date && pos < out_buff_size) { + int bytes = snprintf(out_buff + pos, out_buff_size - pos, "date "); + if (bytes > 0) { + pos += bytes; + } + } + if (g_preamble_time && pos < out_buff_size) { + int bytes = snprintf(out_buff + pos, out_buff_size - pos, "time "); + if (bytes > 0) { + pos += bytes; + } + } + if (g_preamble_uptime && pos < out_buff_size) { + int bytes = snprintf(out_buff + pos, out_buff_size - pos, "( uptime ) "); + if (bytes > 0) { + pos += bytes; + } + } + if (g_preamble_thread && pos < out_buff_size) { + int bytes = snprintf(out_buff + pos, out_buff_size - pos, "[%-*s]", LOGURU_THREADNAME_WIDTH, " thread name/id"); + if (bytes > 0) { + pos += bytes; + } + } + if (g_preamble_file && pos < out_buff_size) { + int bytes = snprintf(out_buff + pos, out_buff_size - pos, "%*s:line ", LOGURU_FILENAME_WIDTH, "file"); + if (bytes > 0) { + pos += bytes; + } + } + if (g_preamble_verbose && pos < out_buff_size) { + int bytes = snprintf(out_buff + pos, out_buff_size - pos, " v"); + if (bytes > 0) { + pos += bytes; + } + } + if (g_preamble_pipe && pos < out_buff_size) { + int bytes = snprintf(out_buff + pos, out_buff_size - pos, "| "); + if (bytes > 0) { + pos += bytes; + } + } + } + + static void print_preamble(char* out_buff, size_t out_buff_size, Verbosity verbosity, const char* file, unsigned line) + { + if (out_buff_size == 0) { return; } + out_buff[0] = '\0'; + if (!g_preamble) { return; } + long long ms_since_epoch = duration_cast(system_clock::now().time_since_epoch()).count(); + time_t sec_since_epoch = time_t(ms_since_epoch / 1000); + tm time_info; + localtime_r(&sec_since_epoch, &time_info); + + auto uptime_ms = duration_cast(steady_clock::now() - s_start_time).count(); + auto uptime_sec = static_cast (uptime_ms) / 1000.0; + + char thread_name[LOGURU_THREADNAME_WIDTH + 1] = {0}; + get_thread_name(thread_name, LOGURU_THREADNAME_WIDTH + 1, true); + + if (s_strip_file_path) { + file = filename(file); + } + + char level_buff[6]; + const char* custom_level_name = get_verbosity_name(verbosity); + if (custom_level_name) { + snprintf(level_buff, sizeof(level_buff) - 1, "%s", custom_level_name); + } else { + snprintf(level_buff, sizeof(level_buff) - 1, "% 4d", verbosity); + } + + size_t pos = 0; + + if (g_preamble_date && pos < out_buff_size) { + int bytes = snprintf(out_buff + pos, out_buff_size - pos, "%04d-%02d-%02d ", + 1900 + time_info.tm_year, 1 + time_info.tm_mon, time_info.tm_mday); + if (bytes > 0) { + pos += bytes; + } + } + if (g_preamble_time && pos < out_buff_size) { + int bytes = snprintf(out_buff + pos, out_buff_size - pos, "%02d:%02d:%02d.%03lld ", + time_info.tm_hour, time_info.tm_min, time_info.tm_sec, ms_since_epoch % 1000); + if (bytes > 0) { + pos += bytes; + } + } + if (g_preamble_uptime && pos < out_buff_size) { + int bytes = snprintf(out_buff + pos, out_buff_size - pos, "(%8.3fs) ", + uptime_sec); + if (bytes > 0) { + pos += bytes; + } + } + if (g_preamble_thread && pos < out_buff_size) { + int bytes = snprintf(out_buff + pos, out_buff_size - pos, "[%-*s]", + LOGURU_THREADNAME_WIDTH, thread_name); + if (bytes > 0) { + pos += bytes; + } + } + if (g_preamble_file && pos < out_buff_size) { + char shortened_filename[LOGURU_FILENAME_WIDTH + 1]; + snprintf(shortened_filename, LOGURU_FILENAME_WIDTH + 1, "%s", file); + int bytes = snprintf(out_buff + pos, out_buff_size - pos, "%*s:%-5u ", + LOGURU_FILENAME_WIDTH, shortened_filename, line); + if (bytes > 0) { + pos += bytes; + } + } + if (g_preamble_verbose && pos < out_buff_size) { + int bytes = snprintf(out_buff + pos, out_buff_size - pos, "%4s", + level_buff); + if (bytes > 0) { + pos += bytes; + } + } + if (g_preamble_pipe && pos < out_buff_size) { + int bytes = snprintf(out_buff + pos, out_buff_size - pos, "| "); + if (bytes > 0) { + pos += bytes; + } + } + } + + // stack_trace_skip is just if verbosity == FATAL. + static void log_message(int stack_trace_skip, Message& message, bool with_indentation, bool abort_if_fatal) + { + const auto verbosity = message.verbosity; + std::lock_guard lock(s_mutex); + + if (message.verbosity == Verbosity_FATAL) { + auto st = loguru::stacktrace(stack_trace_skip + 2); + if (!st.empty()) { + RAW_LOG_F(ERROR, "Stack trace:\n" LOGURU_FMT(s) "", st.c_str()); + } + + auto ec = loguru::get_error_context(); + if (!ec.empty()) { + RAW_LOG_F(ERROR, "" LOGURU_FMT(s) "", ec.c_str()); + } + } + + if (with_indentation) { + message.indentation = indentation(s_stderr_indentation); + } + + if (verbosity <= g_stderr_verbosity) { + if (g_colorlogtostderr && s_terminal_has_color) { + if (verbosity > Verbosity_WARNING) { + fprintf(stderr, "%s%s%s%s%s%s%s%s\n", + terminal_reset(), + terminal_dim(), + message.preamble, + message.indentation, + verbosity == Verbosity_INFO ? terminal_reset() : "", // un-dim for info + message.prefix, + message.message, + terminal_reset()); + } else { + fprintf(stderr, "%s%s%s%s%s%s%s\n", + terminal_reset(), + verbosity == Verbosity_WARNING ? terminal_yellow() : terminal_red(), + message.preamble, + message.indentation, + message.prefix, + message.message, + terminal_reset()); + } + } else { + fprintf(stderr, "%s%s%s%s\n", + message.preamble, message.indentation, message.prefix, message.message); + } + + if (g_flush_interval_ms == 0) { + fflush(stderr); + } else { + s_needs_flushing = true; + } + } + + for (auto& p : s_callbacks) { + if (verbosity <= p.verbosity) { + if (with_indentation) { + message.indentation = indentation(p.indentation); + } + p.callback(p.user_data, message); + if (g_flush_interval_ms == 0) { + if (p.flush) { p.flush(p.user_data); } + } else { + s_needs_flushing = true; + } + } + } + + if (g_flush_interval_ms > 0 && !s_flush_thread) { + s_flush_thread = new std::thread([](){ + for (;;) { + if (s_needs_flushing) { + flush(); + } + std::this_thread::sleep_for(std::chrono::milliseconds(g_flush_interval_ms)); + } + }); + } + + if (message.verbosity == Verbosity_FATAL) { + flush(); + + if (s_fatal_handler) { + s_fatal_handler(message); + flush(); + } + + if (abort_if_fatal) { +#if !defined(_WIN32) + if (s_signal_options.sigabrt) { + // Make sure we don't catch our own abort: + signal(SIGABRT, SIG_DFL); + } +#endif + abort(); + } + } + } + + // stack_trace_skip is just if verbosity == FATAL. + void log_to_everywhere(int stack_trace_skip, Verbosity verbosity, + const char* file, unsigned line, + const char* prefix, const char* buff) + { + char preamble_buff[LOGURU_PREAMBLE_WIDTH]; + print_preamble(preamble_buff, sizeof(preamble_buff), verbosity, file, line); + auto message = Message{verbosity, file, line, preamble_buff, "", prefix, buff}; + log_message(stack_trace_skip + 1, message, true, true); + } + +#if LOGURU_USE_FMTLIB + void vlog(Verbosity verbosity, const char* file, unsigned line, const char* format, fmt::format_args args) + { + auto formatted = fmt::vformat(format, args); + log_to_everywhere(1, verbosity, file, line, "", formatted.c_str()); + } + + void raw_vlog(Verbosity verbosity, const char* file, unsigned line, const char* format, fmt::format_args args) + { + auto formatted = fmt::vformat(format, args); + auto message = Message{verbosity, file, line, "", "", "", formatted.c_str()}; + log_message(1, message, false, true); + } +#else + void log(Verbosity verbosity, const char* file, unsigned line, const char* format, ...) + { + va_list vlist; + va_start(vlist, format); + auto buff = vtextprintf(format, vlist); + log_to_everywhere(1, verbosity, file, line, "", buff.c_str()); + va_end(vlist); + } + + void raw_log(Verbosity verbosity, const char* file, unsigned line, const char* format, ...) + { + va_list vlist; + va_start(vlist, format); + auto buff = vtextprintf(format, vlist); + auto message = Message{verbosity, file, line, "", "", "", buff.c_str()}; + log_message(1, message, false, true); + va_end(vlist); + } +#endif + + void flush() + { + std::lock_guard lock(s_mutex); + fflush(stderr); + for (const auto& callback : s_callbacks) + { + if (callback.flush) { + callback.flush(callback.user_data); + } + } + s_needs_flushing = false; + } + + LogScopeRAII::LogScopeRAII(Verbosity verbosity, const char* file, unsigned line, const char* format, ...) + : _verbosity(verbosity), _file(file), _line(line) + { + if (verbosity <= current_verbosity_cutoff()) { + std::lock_guard lock(s_mutex); + _indent_stderr = (verbosity <= g_stderr_verbosity); + _start_time_ns = now_ns(); + va_list vlist; + va_start(vlist, format); + vsnprintf(_name, sizeof(_name), format, vlist); + log_to_everywhere(1, _verbosity, file, line, "{ ", _name); + va_end(vlist); + + if (_indent_stderr) { + ++s_stderr_indentation; + } + + for (auto& p : s_callbacks) { + if (verbosity <= p.verbosity) { + ++p.indentation; + } + } + } else { + _file = nullptr; + } + } + + LogScopeRAII::~LogScopeRAII() + { + if (_file) { + std::lock_guard lock(s_mutex); + if (_indent_stderr && s_stderr_indentation > 0) { + --s_stderr_indentation; + } + for (auto& p : s_callbacks) { + // Note: Callback indentation cannot change! + if (_verbosity <= p.verbosity) { + // in unlikely case this callback is new + if (p.indentation > 0) { + --p.indentation; + } + } + } +#if LOGURU_VERBOSE_SCOPE_ENDINGS + auto duration_sec = static_cast(now_ns() - _start_time_ns) / 1e9; +#if LOGURU_USE_FMTLIB + auto buff = textprintf("{:.{}f} s: {:s}", duration_sec, LOGURU_SCOPE_TIME_PRECISION, _name); +#else + auto buff = textprintf("%.*f s: %s", LOGURU_SCOPE_TIME_PRECISION, duration_sec, _name); +#endif + log_to_everywhere(1, _verbosity, _file, _line, "} ", buff.c_str()); +#else + log_to_everywhere(1, _verbosity, _file, _line, "}", ""); +#endif + } + } + +#if LOGURU_USE_FMTLIB + void vlog_and_abort(int stack_trace_skip, const char* expr, const char* file, unsigned line, const char* format, fmt::format_args args) + { + auto formatted = fmt::vformat(format, args); + log_to_everywhere(stack_trace_skip + 1, Verbosity_FATAL, file, line, expr, formatted.c_str()); + abort(); // log_to_everywhere already does this, but this makes the analyzer happy. + } +#else + void log_and_abort(int stack_trace_skip, const char* expr, const char* file, unsigned line, const char* format, ...) + { + va_list vlist; + va_start(vlist, format); + auto buff = vtextprintf(format, vlist); + log_to_everywhere(stack_trace_skip + 1, Verbosity_FATAL, file, line, expr, buff.c_str()); + va_end(vlist); + abort(); // log_to_everywhere already does this, but this makes the analyzer happy. + } +#endif + + void log_and_abort(int stack_trace_skip, const char* expr, const char* file, unsigned line) + { + log_and_abort(stack_trace_skip + 1, expr, file, line, " "); + } + + // ---------------------------------------------------------------------------- + // Streams: + +#if LOGURU_USE_FMTLIB + template + std::string vstrprintf(const char* format, const Args&... args) + { + auto text = textprintf(format, args...); + std::string result = text.c_str(); + return result; + } + + template + std::string strprintf(const char* format, const Args&... args) + { + return vstrprintf(format, args...); + } +#else + std::string vstrprintf(const char* format, va_list vlist) + { + auto text = vtextprintf(format, vlist); + std::string result = text.c_str(); + return result; + } + + std::string strprintf(const char* format, ...) + { + va_list vlist; + va_start(vlist, format); + auto result = vstrprintf(format, vlist); + va_end(vlist); + return result; + } +#endif + + #if LOGURU_WITH_STREAMS + + StreamLogger::~StreamLogger() noexcept(false) + { + auto message = _ss.str(); + log(_verbosity, _file, _line, LOGURU_FMT(s), message.c_str()); + } + + AbortLogger::~AbortLogger() noexcept(false) + { + auto message = _ss.str(); + loguru::log_and_abort(1, _expr, _file, _line, LOGURU_FMT(s), message.c_str()); + } + + #endif // LOGURU_WITH_STREAMS + + // ---------------------------------------------------------------------------- + // 888888 88""Yb 88""Yb dP"Yb 88""Yb dP""b8 dP"Yb 88b 88 888888 888888 Yb dP 888888 + // 88__ 88__dP 88__dP dP Yb 88__dP dP `" dP Yb 88Yb88 88 88__ YbdP 88 + // 88"" 88"Yb 88"Yb Yb dP 88"Yb Yb Yb dP 88 Y88 88 88"" dPYb 88 + // 888888 88 Yb 88 Yb YbodP 88 Yb YboodP YbodP 88 Y8 88 888888 dP Yb 88 + // ---------------------------------------------------------------------------- + + struct StringStream + { + std::string str; + }; + + // Use this in your EcPrinter implementations. + void stream_print(StringStream& out_string_stream, const char* text) + { + out_string_stream.str += text; + } + + // ---------------------------------------------------------------------------- + + using ECPtr = EcEntryBase*; + +#if defined(_WIN32) || (defined(__APPLE__) && !TARGET_OS_IPHONE) + #ifdef __APPLE__ + #define LOGURU_THREAD_LOCAL __thread + #else + #define LOGURU_THREAD_LOCAL thread_local + #endif + static LOGURU_THREAD_LOCAL ECPtr thread_ec_ptr = nullptr; + + ECPtr& get_thread_ec_head_ref() + { + return thread_ec_ptr; + } +#else // !thread_local + static pthread_once_t s_ec_pthread_once = PTHREAD_ONCE_INIT; + static pthread_key_t s_ec_pthread_key; + + void free_ec_head_ref(void* io_error_context) + { + delete reinterpret_cast(io_error_context); + } + + void ec_make_pthread_key() + { + (void)pthread_key_create(&s_ec_pthread_key, free_ec_head_ref); + } + + ECPtr& get_thread_ec_head_ref() + { + (void)pthread_once(&s_ec_pthread_once, ec_make_pthread_key); + auto ec = reinterpret_cast(pthread_getspecific(s_ec_pthread_key)); + if (ec == nullptr) { + ec = new ECPtr(nullptr); + (void)pthread_setspecific(s_ec_pthread_key, ec); + } + return *ec; + } +#endif // !thread_local + + // ---------------------------------------------------------------------------- + + EcHandle get_thread_ec_handle() + { + return get_thread_ec_head_ref(); + } + + Text get_error_context() + { + return get_error_context_for(get_thread_ec_head_ref()); + } + + Text get_error_context_for(const EcEntryBase* ec_head) + { + std::vector stack; + while (ec_head) { + stack.push_back(ec_head); + ec_head = ec_head->_previous; + } + std::reverse(stack.begin(), stack.end()); + + StringStream result; + if (!stack.empty()) { + result.str += "------------------------------------------------\n"; + for (auto entry : stack) { + const auto description = std::string(entry->_descr) + ":"; +#if LOGURU_USE_FMTLIB + auto prefix = textprintf("[ErrorContext] {.{}s}:{:-5u} {:-20s} ", + filename(entry->_file), LOGURU_FILENAME_WIDTH, entry->_line, description.c_str()); +#else + auto prefix = textprintf("[ErrorContext] %*s:%-5u %-20s ", + LOGURU_FILENAME_WIDTH, filename(entry->_file), entry->_line, description.c_str()); +#endif + result.str += prefix.c_str(); + entry->print_value(result); + result.str += "\n"; + } + result.str += "------------------------------------------------"; + } + return Text(STRDUP(result.str.c_str())); + } + + EcEntryBase::EcEntryBase(const char* file, unsigned line, const char* descr) + : _file(file), _line(line), _descr(descr) + { + EcEntryBase*& ec_head = get_thread_ec_head_ref(); + _previous = ec_head; + ec_head = this; + } + + EcEntryBase::~EcEntryBase() + { + get_thread_ec_head_ref() = _previous; + } + + // ------------------------------------------------------------------------ + + Text ec_to_text(const char* value) + { + // Add quotes around the string to make it obvious where it begin and ends. + // This is great for detecting erroneous leading or trailing spaces in e.g. an identifier. + auto str = "\"" + std::string(value) + "\""; + return Text{STRDUP(str.c_str())}; + } + + Text ec_to_text(char c) + { + // Add quotes around the character to make it obvious where it begin and ends. + std::string str = "'"; + + auto write_hex_digit = [&](unsigned num) + { + if (num < 10u) { str += char('0' + num); } + else { str += char('a' + num - 10); } + }; + + auto write_hex_16 = [&](uint16_t n) + { + write_hex_digit((n >> 12u) & 0x0f); + write_hex_digit((n >> 8u) & 0x0f); + write_hex_digit((n >> 4u) & 0x0f); + write_hex_digit((n >> 0u) & 0x0f); + }; + + if (c == '\\') { str += "\\\\"; } + else if (c == '\"') { str += "\\\""; } + else if (c == '\'') { str += "\\\'"; } + else if (c == '\0') { str += "\\0"; } + else if (c == '\b') { str += "\\b"; } + else if (c == '\f') { str += "\\f"; } + else if (c == '\n') { str += "\\n"; } + else if (c == '\r') { str += "\\r"; } + else if (c == '\t') { str += "\\t"; } + else if (0 <= c && c < 0x20) { + str += "\\u"; + write_hex_16(static_cast(c)); + } else { str += c; } + + str += "'"; + + return Text{STRDUP(str.c_str())}; + } + + #define DEFINE_EC(Type) \ + Text ec_to_text(Type value) \ + { \ + auto str = std::to_string(value); \ + return Text{STRDUP(str.c_str())}; \ + } + + DEFINE_EC(int) + DEFINE_EC(unsigned int) + DEFINE_EC(long) + DEFINE_EC(unsigned long) + DEFINE_EC(long long) + DEFINE_EC(unsigned long long) + DEFINE_EC(float) + DEFINE_EC(double) + DEFINE_EC(long double) + + #undef DEFINE_EC + + Text ec_to_text(EcHandle ec_handle) + { + Text parent_ec = get_error_context_for(ec_handle); + size_t buffer_size = strlen(parent_ec.c_str()) + 2; + char* with_newline = reinterpret_cast(malloc(buffer_size)); + with_newline[0] = '\n'; + #ifdef _WIN32 + strncpy_s(with_newline + 1, buffer_size, parent_ec.c_str(), buffer_size - 2); + #else + strcpy(with_newline + 1, parent_ec.c_str()); + #endif + return Text(with_newline); + } + + // ---------------------------------------------------------------------------- + +} // namespace loguru + +// ---------------------------------------------------------------------------- +// .dP"Y8 88 dP""b8 88b 88 db 88 .dP"Y8 +// `Ybo." 88 dP `" 88Yb88 dPYb 88 `Ybo." +// o.`Y8b 88 Yb "88 88 Y88 dP__Yb 88 .o o.`Y8b +// 8bodP' 88 YboodP 88 Y8 dP""""Yb 88ood8 8bodP' +// ---------------------------------------------------------------------------- + +#ifdef _WIN32 +namespace loguru { + void install_signal_handlers(const SignalOptions& signal_options) + { + (void)signal_options; + // TODO: implement signal handlers on windows + } +} // namespace loguru + +#else // _WIN32 + +namespace loguru +{ + void write_to_stderr(const char* data, size_t size) + { + auto result = write(STDERR_FILENO, data, size); + (void)result; // Ignore errors. + } + + void write_to_stderr(const char* data) + { + write_to_stderr(data, strlen(data)); + } + + void call_default_signal_handler(int signal_number) + { + struct sigaction sig_action; + memset(&sig_action, 0, sizeof(sig_action)); + sigemptyset(&sig_action.sa_mask); + sig_action.sa_handler = SIG_DFL; + sigaction(signal_number, &sig_action, NULL); + kill(getpid(), signal_number); + } + + void signal_handler(int signal_number, siginfo_t*, void*) + { + const char* signal_name = "UNKNOWN SIGNAL"; + + if (signal_number == SIGABRT) { signal_name = "SIGABRT"; } + if (signal_number == SIGBUS) { signal_name = "SIGBUS"; } + if (signal_number == SIGFPE) { signal_name = "SIGFPE"; } + if (signal_number == SIGILL) { signal_name = "SIGILL"; } + if (signal_number == SIGINT) { signal_name = "SIGINT"; } + if (signal_number == SIGSEGV) { signal_name = "SIGSEGV"; } + if (signal_number == SIGTERM) { signal_name = "SIGTERM"; } + + // -------------------------------------------------------------------- + /* There are few things that are safe to do in a signal handler, + but writing to stderr is one of them. + So we first print out what happened to stderr so we're sure that gets out, + then we do the unsafe things, like logging the stack trace. + */ + + if (g_colorlogtostderr && s_terminal_has_color) { + write_to_stderr(terminal_reset()); + write_to_stderr(terminal_bold()); + write_to_stderr(terminal_light_red()); + } + write_to_stderr("\n"); + write_to_stderr("Loguru caught a signal: "); + write_to_stderr(signal_name); + write_to_stderr("\n"); + if (g_colorlogtostderr && s_terminal_has_color) { + write_to_stderr(terminal_reset()); + } + + // -------------------------------------------------------------------- + + if (s_signal_options.unsafe_signal_handler) { + // -------------------------------------------------------------------- + /* Now we do unsafe things. This can for example lead to deadlocks if + the signal was triggered from the system's memory management functions + and the code below tries to do allocations. + */ + + flush(); + char preamble_buff[LOGURU_PREAMBLE_WIDTH]; + print_preamble(preamble_buff, sizeof(preamble_buff), Verbosity_FATAL, "", 0); + auto message = Message{Verbosity_FATAL, "", 0, preamble_buff, "", "Signal: ", signal_name}; + try { + log_message(1, message, false, false); + } catch (...) { + // This can happed due to s_fatal_handler. + write_to_stderr("Exception caught and ignored by Loguru signal handler.\n"); + } + flush(); + + // -------------------------------------------------------------------- + } + + call_default_signal_handler(signal_number); + } + + void install_signal_handlers(const SignalOptions& signal_options) + { + s_signal_options = signal_options; + + struct sigaction sig_action; + memset(&sig_action, 0, sizeof(sig_action)); + sigemptyset(&sig_action.sa_mask); + sig_action.sa_flags |= SA_SIGINFO; + sig_action.sa_sigaction = &signal_handler; + + if (signal_options.sigabrt) { + CHECK_F(sigaction(SIGABRT, &sig_action, NULL) != -1, "Failed to install handler for SIGABRT"); + } + if (signal_options.sigbus) { + CHECK_F(sigaction(SIGBUS, &sig_action, NULL) != -1, "Failed to install handler for SIGBUS"); + } + if (signal_options.sigfpe) { + CHECK_F(sigaction(SIGFPE, &sig_action, NULL) != -1, "Failed to install handler for SIGFPE"); + } + if (signal_options.sigill) { + CHECK_F(sigaction(SIGILL, &sig_action, NULL) != -1, "Failed to install handler for SIGILL"); + } + if (signal_options.sigint) { + CHECK_F(sigaction(SIGINT, &sig_action, NULL) != -1, "Failed to install handler for SIGINT"); + } + if (signal_options.sigsegv) { + CHECK_F(sigaction(SIGSEGV, &sig_action, NULL) != -1, "Failed to install handler for SIGSEGV"); + } + if (signal_options.sigterm) { + CHECK_F(sigaction(SIGTERM, &sig_action, NULL) != -1, "Failed to install handler for SIGTERM"); + } + } +} // namespace loguru + +#endif // _WIN32 + + +#if defined(__GNUC__) || defined(__clang__) +#pragma GCC diagnostic pop +#elif defined(_MSC_VER) +#pragma warning(pop) +#endif + +#endif // LOGURU_IMPLEMENTATION diff --git a/projects/Functional/Algebra/loguru/loguru.hpp b/projects/Functional/Algebra/loguru/loguru.hpp new file mode 100644 index 0000000000..bfdce06d50 --- /dev/null +++ b/projects/Functional/Algebra/loguru/loguru.hpp @@ -0,0 +1,1445 @@ +/* +Loguru logging library for C++, by Emil Ernerfeldt. +www.github.com/emilk/loguru +If you find Loguru useful, please let me know on twitter or in a mail! +Twitter: @ernerfeldt +Mail: emil.ernerfeldt@gmail.com +Website: www.ilikebigbits.com + +# License + This software is in the public domain. Where that dedication is not + recognized, you are granted a perpetual, irrevocable license to + copy, modify and distribute it as you see fit. + +# Inspiration + Much of Loguru was inspired by GLOG, https://code.google.com/p/google-glog/. + The choice of public domain is fully due Sean T. Barrett + and his wonderful stb libraries at https://github.com/nothings/stb. + +# Version history + * Version 0.1.0 - 2015-03-22 - Works great on Mac. + * Version 0.2.0 - 2015-09-17 - Removed the only dependency. + * Version 0.3.0 - 2015-10-02 - Drop-in replacement for most of GLOG + * Version 0.4.0 - 2015-10-07 - Single-file! + * Version 0.5.0 - 2015-10-17 - Improved file logging + * Version 0.6.0 - 2015-10-24 - Add stack traces + * Version 0.7.0 - 2015-10-27 - Signals + * Version 0.8.0 - 2015-10-30 - Color logging. + * Version 0.9.0 - 2015-11-26 - ABORT_S and proper handling of FATAL + * Version 1.0.0 - 2016-02-14 - ERROR_CONTEXT + * Version 1.1.0 - 2016-02-19 - -v OFF, -v INFO etc + * Version 1.1.1 - 2016-02-20 - textprintf vs strprintf + * Version 1.1.2 - 2016-02-22 - Remove g_alsologtostderr + * Version 1.1.3 - 2016-02-29 - ERROR_CONTEXT as linked list + * Version 1.2.0 - 2016-03-19 - Add get_thread_name() + * Version 1.2.1 - 2016-03-20 - Minor fixes + * Version 1.2.2 - 2016-03-29 - Fix issues with set_fatal_handler throwing an exception + * Version 1.2.3 - 2016-05-16 - Log current working directory in loguru::init(). + * Version 1.2.4 - 2016-05-18 - Custom replacement for -v in loguru::init() by bjoernpollex + * Version 1.2.5 - 2016-05-18 - Add ability to print ERROR_CONTEXT of parent thread. + * Version 1.2.6 - 2016-05-19 - Bug fix regarding VLOG verbosity argument lacking (). + * Version 1.2.7 - 2016-05-23 - Fix PATH_MAX problem. + * Version 1.2.8 - 2016-05-26 - Add shutdown() and remove_all_callbacks() + * Version 1.2.9 - 2016-06-09 - Use a monotonic clock for uptime. + * Version 1.3.0 - 2016-07-20 - Fix issues with callback flush/close not being called. + * Version 1.3.1 - 2016-07-20 - Add LOGURU_UNSAFE_SIGNAL_HANDLER to toggle stacktrace on signals. + * Version 1.3.2 - 2016-07-20 - Add loguru::arguments() + * Version 1.4.0 - 2016-09-15 - Semantic versioning + add loguru::create_directories + * Version 1.4.1 - 2016-09-29 - Customize formating with LOGURU_FILENAME_WIDTH + * Version 1.5.0 - 2016-12-22 - LOGURU_USE_FMTLIB by kolis and LOGURU_WITH_FILEABS by scinart + * Version 1.5.1 - 2017-08-08 - Terminal colors on Windows 10 thanks to looki + * Version 1.6.0 - 2018-01-03 - Add LOGURU_RTTI and LOGURU_STACKTRACES settings + * Version 1.7.0 - 2018-01-03 - Add ability to turn off the preamble with loguru::g_preamble + * Version 1.7.1 - 2018-04-05 - Add function get_fatal_handler + * Version 1.7.2 - 2018-04-22 - Fix a bug where large file names could cause stack corruption (thanks @ccamporesi) + * Version 1.8.0 - 2018-04-23 - Shorten long file names to keep preamble fixed width + * Version 1.9.0 - 2018-09-22 - Adjust terminal colors, add LOGURU_VERBOSE_SCOPE_ENDINGS, add LOGURU_SCOPE_TIME_PRECISION, add named log levels + * Version 2.0.0 - 2018-09-22 - Split loguru.hpp into loguru.hpp and loguru.cpp + * Version 2.1.0 - 2019-09-23 - Update fmtlib + add option to loguru::init to NOT set main thread name. + * Version 2.2.0 - 2020-07-31 - Replace LOGURU_CATCH_SIGABRT with struct SignalOptions + +# Compiling + Just include where you want to use Loguru. + Then, in one .cpp file #include + Make sure you compile with -std=c++11 -lstdc++ -lpthread -ldl + +# Usage + For details, please see the official documentation at emilk.github.io/loguru + + #include + + int main(int argc, char* argv[]) { + loguru::init(argc, argv); + + // Put every log message in "everything.log": + loguru::add_file("everything.log", loguru::Append, loguru::Verbosity_MAX); + + LOG_F(INFO, "The magic number is %d", 42); + } + +*/ + +#if defined(LOGURU_IMPLEMENTATION) + #error "You are defining LOGURU_IMPLEMENTATION. This is for older versions of Loguru. You should now instead include loguru.cpp (or build it and link with it)" +#endif + +// Disable all warnings from gcc/clang: +#if defined(__clang__) + #pragma clang system_header +#elif defined(__GNUC__) + #pragma GCC system_header +#endif + +#ifndef LOGURU_HAS_DECLARED_FORMAT_HEADER +#define LOGURU_HAS_DECLARED_FORMAT_HEADER + +// Semantic versioning. Loguru version can be printed with printf("%d.%d.%d", LOGURU_VERSION_MAJOR, LOGURU_VERSION_MINOR, LOGURU_VERSION_PATCH); +#define LOGURU_VERSION_MAJOR 2 +#define LOGURU_VERSION_MINOR 1 +#define LOGURU_VERSION_PATCH 0 + +#if defined(_MSC_VER) +#include // Needed for _In_z_ etc annotations +#endif + +#if defined(__linux__) || defined(__APPLE__) +#define LOGURU_SYSLOG 1 +#else +#define LOGURU_SYSLOG 0 +#endif + +// ---------------------------------------------------------------------------- + +#ifndef LOGURU_EXPORT + // Define to your project's export declaration if needed for use in a shared library. + #define LOGURU_EXPORT +#endif + +#ifndef LOGURU_SCOPE_TEXT_SIZE + // Maximum length of text that can be printed by a LOG_SCOPE. + // This should be long enough to get most things, but short enough not to clutter the stack. + #define LOGURU_SCOPE_TEXT_SIZE 196 +#endif + +#ifndef LOGURU_FILENAME_WIDTH + // Width of the column containing the file name + #define LOGURU_FILENAME_WIDTH 23 +#endif + +#ifndef LOGURU_THREADNAME_WIDTH + // Width of the column containing the thread name + #define LOGURU_THREADNAME_WIDTH 16 +#endif + +#ifndef LOGURU_SCOPE_TIME_PRECISION + // Resolution of scope timers. 3=ms, 6=us, 9=ns + #define LOGURU_SCOPE_TIME_PRECISION 3 +#endif + +#ifdef LOGURU_CATCH_SIGABRT + #error "You are defining LOGURU_CATCH_SIGABRT. This is for older versions of Loguru. You should now instead set the options passed to loguru::init" +#endif + +#ifndef LOGURU_VERBOSE_SCOPE_ENDINGS + // Show milliseconds and scope name at end of scope. + #define LOGURU_VERBOSE_SCOPE_ENDINGS 1 +#endif + +#ifndef LOGURU_REDEFINE_ASSERT + #define LOGURU_REDEFINE_ASSERT 0 +#endif + +#ifndef LOGURU_WITH_STREAMS + #define LOGURU_WITH_STREAMS 0 +#endif + +#ifndef LOGURU_REPLACE_GLOG + #define LOGURU_REPLACE_GLOG 0 +#endif + +#if LOGURU_REPLACE_GLOG + #undef LOGURU_WITH_STREAMS + #define LOGURU_WITH_STREAMS 1 +#endif + +#if defined(LOGURU_UNSAFE_SIGNAL_HANDLER) + #error "You are defining LOGURU_UNSAFE_SIGNAL_HANDLER. This is for older versions of Loguru. You should now instead set the unsafe_signal_handler option when you call loguru::init." +#endif + +#if LOGURU_IMPLEMENTATION + #undef LOGURU_WITH_STREAMS + #define LOGURU_WITH_STREAMS 1 +#endif + +#ifndef LOGURU_USE_FMTLIB + #define LOGURU_USE_FMTLIB 0 +#endif + +#ifndef LOGURU_WITH_FILEABS + #define LOGURU_WITH_FILEABS 0 +#endif + +#ifndef LOGURU_RTTI +#if defined(__clang__) + #if __has_feature(cxx_rtti) + #define LOGURU_RTTI 1 + #endif +#elif defined(__GNUG__) + #if defined(__GXX_RTTI) + #define LOGURU_RTTI 1 + #endif +#elif defined(_MSC_VER) + #if defined(_CPPRTTI) + #define LOGURU_RTTI 1 + #endif +#endif +#endif + +// -------------------------------------------------------------------- +// Utility macros + +#define LOGURU_CONCATENATE_IMPL(s1, s2) s1 ## s2 +#define LOGURU_CONCATENATE(s1, s2) LOGURU_CONCATENATE_IMPL(s1, s2) + +#ifdef __COUNTER__ +# define LOGURU_ANONYMOUS_VARIABLE(str) LOGURU_CONCATENATE(str, __COUNTER__) +#else +# define LOGURU_ANONYMOUS_VARIABLE(str) LOGURU_CONCATENATE(str, __LINE__) +#endif + +#if defined(__clang__) || defined(__GNUC__) + // Helper macro for declaring functions as having similar signature to printf. + // This allows the compiler to catch format errors at compile-time. + #define LOGURU_PRINTF_LIKE(fmtarg, firstvararg) __attribute__((__format__ (__printf__, fmtarg, firstvararg))) + #define LOGURU_FORMAT_STRING_TYPE const char* +#elif defined(_MSC_VER) + #define LOGURU_PRINTF_LIKE(fmtarg, firstvararg) + #define LOGURU_FORMAT_STRING_TYPE _In_z_ _Printf_format_string_ const char* +#else + #define LOGURU_PRINTF_LIKE(fmtarg, firstvararg) + #define LOGURU_FORMAT_STRING_TYPE const char* +#endif + +// Used to mark log_and_abort for the benefit of the static analyzer and optimizer. +#if defined(_MSC_VER) +#define LOGURU_NORETURN __declspec(noreturn) +#else +#define LOGURU_NORETURN __attribute__((noreturn)) +#endif + +#if defined(_MSC_VER) +#define LOGURU_PREDICT_FALSE(x) (x) +#define LOGURU_PREDICT_TRUE(x) (x) +#else +#define LOGURU_PREDICT_FALSE(x) (__builtin_expect(x, 0)) +#define LOGURU_PREDICT_TRUE(x) (__builtin_expect(!!(x), 1)) +#endif + +#if LOGURU_USE_FMTLIB + #include + #define LOGURU_FMT(x) "{:" #x "}" +#else + #define LOGURU_FMT(x) "%" #x +#endif + +#ifdef _WIN32 + #define STRDUP(str) _strdup(str) +#else + #define STRDUP(str) strdup(str) +#endif + +// -------------------------------------------------------------------- + +namespace loguru +{ + // Simple RAII ownership of a char*. + class LOGURU_EXPORT Text + { + public: + explicit Text(char* owned_str) : _str(owned_str) {} + ~Text(); + Text(Text&& t) + { + _str = t._str; + t._str = nullptr; + } + Text(Text& t) = delete; + Text& operator=(Text& t) = delete; + void operator=(Text&& t) = delete; + + const char* c_str() const { return _str; } + bool empty() const { return _str == nullptr || *_str == '\0'; } + + char* release() + { + auto result = _str; + _str = nullptr; + return result; + } + + private: + char* _str; + }; + + // Like printf, but returns the formated text. +#if LOGURU_USE_FMTLIB + LOGURU_EXPORT + Text vtextprintf(const char* format, fmt::format_args args); + + template + LOGURU_EXPORT + Text textprintf(LOGURU_FORMAT_STRING_TYPE format, const Args&... args) { + return vtextprintf(format, fmt::make_format_args(args...)); + } +#else + LOGURU_EXPORT + Text textprintf(LOGURU_FORMAT_STRING_TYPE format, ...) LOGURU_PRINTF_LIKE(1, 2); +#endif + + // Overloaded for variadic template matching. + LOGURU_EXPORT + Text textprintf(); + + using Verbosity = int; + +#undef FATAL +#undef ERROR +#undef WARNING +#undef INFO +#undef MAX + + enum NamedVerbosity : Verbosity + { + // Used to mark an invalid verbosity. Do not log to this level. + Verbosity_INVALID = -10, // Never do LOG_F(INVALID) + + // You may use Verbosity_OFF on g_stderr_verbosity, but for nothing else! + Verbosity_OFF = -9, // Never do LOG_F(OFF) + + // Prefer to use ABORT_F or ABORT_S over LOG_F(FATAL) or LOG_S(FATAL). + Verbosity_FATAL = -3, + Verbosity_ERROR = -2, + Verbosity_WARNING = -1, + + // Normal messages. By default written to stderr. + Verbosity_INFO = 0, + + // Same as Verbosity_INFO in every way. + Verbosity_0 = 0, + + // Verbosity levels 1-9 are generally not written to stderr, but are written to file. + Verbosity_1 = +1, + Verbosity_2 = +2, + Verbosity_3 = +3, + Verbosity_4 = +4, + Verbosity_5 = +5, + Verbosity_6 = +6, + Verbosity_7 = +7, + Verbosity_8 = +8, + Verbosity_9 = +9, + + // Do not use higher verbosity levels, as that will make grepping log files harder. + Verbosity_MAX = +9, + }; + + struct Message + { + // You would generally print a Message by just concatenating the buffers without spacing. + // Optionally, ignore preamble and indentation. + Verbosity verbosity; // Already part of preamble + const char* filename; // Already part of preamble + unsigned line; // Already part of preamble + const char* preamble; // Date, time, uptime, thread, file:line, verbosity. + const char* indentation; // Just a bunch of spacing. + const char* prefix; // Assertion failure info goes here (or ""). + const char* message; // User message goes here. + }; + + /* Everything with a verbosity equal or greater than g_stderr_verbosity will be + written to stderr. You can set this in code or via the -v argument. + Set to loguru::Verbosity_OFF to write nothing to stderr. + Default is 0, i.e. only log ERROR, WARNING and INFO are written to stderr. + */ + LOGURU_EXPORT extern Verbosity g_stderr_verbosity; + LOGURU_EXPORT extern bool g_colorlogtostderr; // True by default. + LOGURU_EXPORT extern unsigned g_flush_interval_ms; // 0 (unbuffered) by default. + LOGURU_EXPORT extern bool g_preamble_header; // Prepend each log start by a descriptions line with all columns name? True by default. + LOGURU_EXPORT extern bool g_preamble; // Prefix each log line with date, time etc? True by default. + + /* Specify the verbosity used by loguru to log its info messages including the header + logged when logged::init() is called or on exit. Default is 0 (INFO). + */ + LOGURU_EXPORT extern Verbosity g_internal_verbosity; + + // Turn off individual parts of the preamble + LOGURU_EXPORT extern bool g_preamble_date; // The date field + LOGURU_EXPORT extern bool g_preamble_time; // The time of the current day + LOGURU_EXPORT extern bool g_preamble_uptime; // The time since init call + LOGURU_EXPORT extern bool g_preamble_thread; // The logging thread + LOGURU_EXPORT extern bool g_preamble_file; // The file from which the log originates from + LOGURU_EXPORT extern bool g_preamble_verbose; // The verbosity field + LOGURU_EXPORT extern bool g_preamble_pipe; // The pipe symbol right before the message + + // May not throw! + typedef void (*log_handler_t)(void* user_data, const Message& message); + typedef void (*close_handler_t)(void* user_data); + typedef void (*flush_handler_t)(void* user_data); + + // May throw if that's how you'd like to handle your errors. + typedef void (*fatal_handler_t)(const Message& message); + + // Given a verbosity level, return the level's name or nullptr. + typedef const char* (*verbosity_to_name_t)(Verbosity verbosity); + + // Given a verbosity level name, return the verbosity level or + // Verbosity_INVALID if name is not recognized. + typedef Verbosity (*name_to_verbosity_t)(const char* name); + + struct SignalOptions + { + /// Make Loguru try to do unsafe but useful things, + /// like printing a stack trace, when catching signals. + /// This may lead to bad things like deadlocks in certain situations. + bool unsafe_signal_handler = true; + + /// Should Loguru catch SIGABRT ? + bool sigabrt = true; + + /// Should Loguru catch SIGBUS ? + bool sigbus = true; + + /// Should Loguru catch SIGFPE ? + bool sigfpe = true; + + /// Should Loguru catch SIGILL ? + bool sigill = true; + + /// Should Loguru catch SIGINT ? + bool sigint = true; + + /// Should Loguru catch SIGSEGV ? + bool sigsegv = true; + + /// Should Loguru catch SIGTERM ? + bool sigterm = true; + + static SignalOptions none() + { + SignalOptions options; + options.unsafe_signal_handler = false; + options.sigabrt = false; + options.sigbus = false; + options.sigfpe = false; + options.sigill = false; + options.sigint = false; + options.sigsegv = false; + options.sigterm = false; + return options; + } + }; + + // Runtime options passed to loguru::init + struct Options + { + // This allows you to use something else instead of "-v" via verbosity_flag. + // Set to nullptr if you don't want Loguru to parse verbosity from the args. + const char* verbosity_flag = "-v"; + + // loguru::init will set the name of the calling thread to this. + // If you don't want Loguru to set the name of the main thread, + // set this to nullptr. + // NOTE: on SOME platforms loguru::init will only overwrite the thread name + // if a thread name has not already been set. + // To always set a thread name, use loguru::set_thread_name instead. + const char* main_thread_name = "main thread"; + + SignalOptions signal_options; + }; + + /* Should be called from the main thread. + You don't *need* to call this, but if you do you get: + * Signal handlers installed + * Program arguments logged + * Working dir logged + * Optional -v verbosity flag parsed + * Main thread name set to "main thread" + * Explanation of the preamble (date, thread name, etc) logged + + loguru::init() will look for arguments meant for loguru and remove them. + Arguments meant for loguru are: + -v n Set loguru::g_stderr_verbosity level. Examples: + -v 3 Show verbosity level 3 and lower. + -v 0 Only show INFO, WARNING, ERROR, FATAL (default). + -v INFO Only show INFO, WARNING, ERROR, FATAL (default). + -v WARNING Only show WARNING, ERROR, FATAL. + -v ERROR Only show ERROR, FATAL. + -v FATAL Only show FATAL. + -v OFF Turn off logging to stderr. + + Tip: You can set g_stderr_verbosity before calling loguru::init. + That way you can set the default but have the user override it with the -v flag. + Note that -v does not affect file logging (see loguru::add_file). + + You can you something other than the -v flag by setting the verbosity_flag option. + */ + LOGURU_EXPORT + void init(int& argc, char* argv[], const Options& options = {}); + + // Will call remove_all_callbacks(). After calling this, logging will still go to stderr. + // You generally don't need to call this. + LOGURU_EXPORT + void shutdown(); + + // What ~ will be replaced with, e.g. "/home/your_user_name/" + LOGURU_EXPORT + const char* home_dir(); + + /* Returns the name of the app as given in argv[0] but without leading path. + That is, if argv[0] is "../foo/app" this will return "app". + */ + LOGURU_EXPORT + const char* argv0_filename(); + + // Returns all arguments given to loguru::init(), but escaped with a single space as separator. + LOGURU_EXPORT + const char* arguments(); + + // Returns the path to the current working dir when loguru::init() was called. + LOGURU_EXPORT + const char* current_dir(); + + // Returns the part of the path after the last / or \ (if any). + LOGURU_EXPORT + const char* filename(const char* path); + + // e.g. "foo/bar/baz.ext" will create the directories "foo/" and "foo/bar/" + LOGURU_EXPORT + bool create_directories(const char* file_path_const); + + // Writes date and time with millisecond precision, e.g. "20151017_161503.123" + LOGURU_EXPORT + void write_date_time(char* buff, unsigned buff_size); + + // Helper: thread-safe version strerror + LOGURU_EXPORT + Text errno_as_text(); + + /* Given a prefix of e.g. "~/loguru/" this might return + "/home/your_username/loguru/app_name/20151017_161503.123.log" + + where "app_name" is a sanitized version of argv[0]. + */ + LOGURU_EXPORT + void suggest_log_path(const char* prefix, char* buff, unsigned buff_size); + + enum FileMode { Truncate, Append }; + + /* Will log to a file at the given path. + Any logging message with a verbosity lower or equal to + the given verbosity will be included. + The function will create all directories in 'path' if needed. + If path starts with a ~, it will be replaced with loguru::home_dir() + To stop the file logging, just call loguru::remove_callback(path) with the same path. + */ + LOGURU_EXPORT + bool add_file(const char* path, FileMode mode, Verbosity verbosity); + + LOGURU_EXPORT + // Send logs to syslog with LOG_USER facility (see next call) + bool add_syslog(const char* app_name, Verbosity verbosity); + LOGURU_EXPORT + // Send logs to syslog with your own choice of facility (LOG_USER, LOG_AUTH, ...) + // see loguru.cpp: syslog_log() for more details. + bool add_syslog(const char* app_name, Verbosity verbosity, int facility); + + /* Will be called right before abort(). + You can for instance use this to print custom error messages, or throw an exception. + Feel free to call LOG:ing function from this, but not FATAL ones! */ + LOGURU_EXPORT + void set_fatal_handler(fatal_handler_t handler); + + // Get the current fatal handler, if any. Default value is nullptr. + LOGURU_EXPORT + fatal_handler_t get_fatal_handler(); + + /* Will be called on each log messages with a verbosity less or equal to the given one. + Useful for displaying messages on-screen in a game, for example. + The given on_close is also expected to flush (if desired). + */ + LOGURU_EXPORT + void add_callback( + const char* id, + log_handler_t callback, + void* user_data, + Verbosity verbosity, + close_handler_t on_close = nullptr, + flush_handler_t on_flush = nullptr); + + /* Set a callback that returns custom verbosity level names. If callback + is nullptr or returns nullptr, default log names will be used. + */ + LOGURU_EXPORT + void set_verbosity_to_name_callback(verbosity_to_name_t callback); + + /* Set a callback that returns the verbosity level matching a name. The + callback should return Verbosity_INVALID if the name is not + recognized. + */ + LOGURU_EXPORT + void set_name_to_verbosity_callback(name_to_verbosity_t callback); + + /* Get a custom name for a specific verbosity, if one exists, or nullptr. */ + LOGURU_EXPORT + const char* get_verbosity_name(Verbosity verbosity); + + /* Get the verbosity enum value from a custom 4-character level name, if one exists. + If the name does not match a custom level name, Verbosity_INVALID is returned. + */ + LOGURU_EXPORT + Verbosity get_verbosity_from_name(const char* name); + + // Returns true iff the callback was found (and removed). + LOGURU_EXPORT + bool remove_callback(const char* id); + + // Shut down all file logging and any other callback hooks installed. + LOGURU_EXPORT + void remove_all_callbacks(); + + // Returns the maximum of g_stderr_verbosity and all file/custom outputs. + LOGURU_EXPORT + Verbosity current_verbosity_cutoff(); + +#if LOGURU_USE_FMTLIB + // Internal functions + LOGURU_EXPORT + void vlog(Verbosity verbosity, const char* file, unsigned line, LOGURU_FORMAT_STRING_TYPE format, fmt::format_args args); + LOGURU_EXPORT + void raw_vlog(Verbosity verbosity, const char* file, unsigned line, LOGURU_FORMAT_STRING_TYPE format, fmt::format_args args); + + // Actual logging function. Use the LOG macro instead of calling this directly. + template + LOGURU_EXPORT + void log(Verbosity verbosity, const char* file, unsigned line, LOGURU_FORMAT_STRING_TYPE format, const Args &... args) { + vlog(verbosity, file, line, format, fmt::make_format_args(args...)); + } + + // Log without any preamble or indentation. + template + LOGURU_EXPORT + void raw_log(Verbosity verbosity, const char* file, unsigned line, LOGURU_FORMAT_STRING_TYPE format, const Args &... args) { + raw_vlog(verbosity, file, line, format, fmt::make_format_args(args...)); + } +#else // LOGURU_USE_FMTLIB? + // Actual logging function. Use the LOG macro instead of calling this directly. + LOGURU_EXPORT + void log(Verbosity verbosity, const char* file, unsigned line, LOGURU_FORMAT_STRING_TYPE format, ...) LOGURU_PRINTF_LIKE(4, 5); + + // Log without any preamble or indentation. + LOGURU_EXPORT + void raw_log(Verbosity verbosity, const char* file, unsigned line, LOGURU_FORMAT_STRING_TYPE format, ...) LOGURU_PRINTF_LIKE(4, 5); +#endif // !LOGURU_USE_FMTLIB + + // Helper class for LOG_SCOPE_F + class LOGURU_EXPORT LogScopeRAII + { + public: + LogScopeRAII() : _file(nullptr) {} // No logging + LogScopeRAII(Verbosity verbosity, const char* file, unsigned line, LOGURU_FORMAT_STRING_TYPE format, ...) LOGURU_PRINTF_LIKE(5, 6); + ~LogScopeRAII(); + +#if defined(_MSC_VER) && _MSC_VER > 1800 + // older MSVC default move ctors close the scope on move. See + // issue #43 + LogScopeRAII(LogScopeRAII&& other) + : _verbosity(other._verbosity) + , _file(other._file) + , _line(other._line) + , _indent_stderr(other._indent_stderr) + , _start_time_ns(other._start_time_ns) + { + // Make sure the tmp object's destruction doesn't close the scope: + other._file = nullptr; + + for (unsigned int i = 0; i < LOGURU_SCOPE_TEXT_SIZE; ++i) { + _name[i] = other._name[i]; + } + } +#else + LogScopeRAII(LogScopeRAII&&) = default; +#endif + + private: + LogScopeRAII(const LogScopeRAII&) = delete; + LogScopeRAII& operator=(const LogScopeRAII&) = delete; + void operator=(LogScopeRAII&&) = delete; + + Verbosity _verbosity; + const char* _file; // Set to null if we are disabled due to verbosity + unsigned _line; + bool _indent_stderr; // Did we? + long long _start_time_ns; + char _name[LOGURU_SCOPE_TEXT_SIZE]; + }; + + // Marked as 'noreturn' for the benefit of the static analyzer and optimizer. + // stack_trace_skip is the number of extrace stack frames to skip above log_and_abort. +#if LOGURU_USE_FMTLIB + LOGURU_EXPORT + LOGURU_NORETURN void vlog_and_abort(int stack_trace_skip, const char* expr, const char* file, unsigned line, LOGURU_FORMAT_STRING_TYPE format, fmt::format_args); + template + LOGURU_EXPORT + LOGURU_NORETURN void log_and_abort(int stack_trace_skip, const char* expr, const char* file, unsigned line, LOGURU_FORMAT_STRING_TYPE format, const Args&... args) { + vlog_and_abort(stack_trace_skip, expr, file, line, format, fmt::make_format_args(args...)); + } +#else + LOGURU_EXPORT + LOGURU_NORETURN void log_and_abort(int stack_trace_skip, const char* expr, const char* file, unsigned line, LOGURU_FORMAT_STRING_TYPE format, ...) LOGURU_PRINTF_LIKE(5, 6); +#endif + LOGURU_EXPORT + LOGURU_NORETURN void log_and_abort(int stack_trace_skip, const char* expr, const char* file, unsigned line); + + // Flush output to stderr and files. + // If g_flush_interval_ms is set to non-zero, this will be called automatically this often. + // If not set, you do not need to call this at all. + LOGURU_EXPORT + void flush(); + + template inline Text format_value(const T&) { return textprintf("N/A"); } + template<> inline Text format_value(const char& v) { return textprintf(LOGURU_FMT(c), v); } + template<> inline Text format_value(const int& v) { return textprintf(LOGURU_FMT(d), v); } + template<> inline Text format_value(const unsigned int& v) { return textprintf(LOGURU_FMT(u), v); } + template<> inline Text format_value(const long& v) { return textprintf(LOGURU_FMT(lu), v); } + template<> inline Text format_value(const unsigned long& v) { return textprintf(LOGURU_FMT(ld), v); } + template<> inline Text format_value(const long long& v) { return textprintf(LOGURU_FMT(llu), v); } + template<> inline Text format_value(const unsigned long long& v) { return textprintf(LOGURU_FMT(lld), v); } + template<> inline Text format_value(const float& v) { return textprintf(LOGURU_FMT(f), v); } + template<> inline Text format_value(const double& v) { return textprintf(LOGURU_FMT(f), v); } + + /* Thread names can be set for the benefit of readable logs. + If you do not set the thread name, a hex id will be shown instead. + These thread names may or may not be the same as the system thread names, + depending on the system. + Try to limit the thread name to 15 characters or less. */ + LOGURU_EXPORT + void set_thread_name(const char* name); + + /* Returns the thread name for this thread. + On most *nix systems this will return the system thread name (settable from both within and without Loguru). + On other systems it will return whatever you set in `set_thread_name()`; + If no thread name is set, this will return a hexadecimal thread id. + `length` should be the number of bytes available in the buffer. + 17 is a good number for length. + `right_align_hex_id` means any hexadecimal thread id will be written to the end of buffer. + */ + LOGURU_EXPORT + void get_thread_name(char* buffer, unsigned long long length, bool right_align_hex_id); + + /* Generates a readable stacktrace as a string. + 'skip' specifies how many stack frames to skip. + For instance, the default skip (1) means: + don't include the call to loguru::stacktrace in the stack trace. */ + LOGURU_EXPORT + Text stacktrace(int skip = 1); + + /* Add a string to be replaced with something else in the stack output. + + For instance, instead of having a stack trace look like this: + 0x41f541 some_function(std::basic_ofstream >&) + You can clean it up with: + auto verbose_type_name = loguru::demangle(typeid(std::ofstream).name()); + loguru::add_stack_cleanup(verbose_type_name.c_str(); "std::ofstream"); + So the next time you will instead see: + 0x41f541 some_function(std::ofstream&) + + `replace_with_this` must be shorter than `find_this`. + */ + LOGURU_EXPORT + void add_stack_cleanup(const char* find_this, const char* replace_with_this); + + // Example: demangle(typeid(std::ofstream).name()) -> "std::basic_ofstream >" + LOGURU_EXPORT + Text demangle(const char* name); + + // ------------------------------------------------------------------------ + /* + Not all terminals support colors, but if they do, and g_colorlogtostderr + is set, Loguru will write them to stderr to make errors in red, etc. + + You also have the option to manually use them, via the function below. + + Note, however, that if you do, the color codes could end up in your logfile! + + This means if you intend to use them functions you should either: + * Use them on the stderr/stdout directly (bypass Loguru). + * Don't add file outputs to Loguru. + * Expect some \e[1m things in your logfile. + + Usage: + printf("%sRed%sGreen%sBold green%sClear again\n", + loguru::terminal_red(), loguru::terminal_green(), + loguru::terminal_bold(), loguru::terminal_reset()); + + If the terminal at hand does not support colors the above output + will just not have funky \e[1m things showing. + */ + + // Do the output terminal support colors? + LOGURU_EXPORT + bool terminal_has_color(); + + // Colors + LOGURU_EXPORT const char* terminal_black(); + LOGURU_EXPORT const char* terminal_red(); + LOGURU_EXPORT const char* terminal_green(); + LOGURU_EXPORT const char* terminal_yellow(); + LOGURU_EXPORT const char* terminal_blue(); + LOGURU_EXPORT const char* terminal_purple(); + LOGURU_EXPORT const char* terminal_cyan(); + LOGURU_EXPORT const char* terminal_light_gray(); + LOGURU_EXPORT const char* terminal_light_red(); + LOGURU_EXPORT const char* terminal_white(); + + // Formating + LOGURU_EXPORT const char* terminal_bold(); + LOGURU_EXPORT const char* terminal_underline(); + + // You should end each line with this! + LOGURU_EXPORT const char* terminal_reset(); + + // -------------------------------------------------------------------- + // Error context related: + + struct StringStream; + + // Use this in your EcEntryBase::print_value overload. + LOGURU_EXPORT + void stream_print(StringStream& out_string_stream, const char* text); + + class LOGURU_EXPORT EcEntryBase + { + public: + EcEntryBase(const char* file, unsigned line, const char* descr); + ~EcEntryBase(); + EcEntryBase(const EcEntryBase&) = delete; + EcEntryBase(EcEntryBase&&) = delete; + EcEntryBase& operator=(const EcEntryBase&) = delete; + EcEntryBase& operator=(EcEntryBase&&) = delete; + + virtual void print_value(StringStream& out_string_stream) const = 0; + + EcEntryBase* previous() const { return _previous; } + + // private: + const char* _file; + unsigned _line; + const char* _descr; + EcEntryBase* _previous; + }; + + template + class EcEntryData : public EcEntryBase + { + public: + using Printer = Text(*)(T data); + + EcEntryData(const char* file, unsigned line, const char* descr, T data, Printer&& printer) + : EcEntryBase(file, line, descr), _data(data), _printer(printer) {} + + virtual void print_value(StringStream& out_string_stream) const override + { + const auto str = _printer(_data); + stream_print(out_string_stream, str.c_str()); + } + + private: + T _data; + Printer _printer; + }; + + // template + // class EcEntryLambda : public EcEntryBase + // { + // public: + // EcEntryLambda(const char* file, unsigned line, const char* descr, Printer&& printer) + // : EcEntryBase(file, line, descr), _printer(std::move(printer)) {} + + // virtual void print_value(StringStream& out_string_stream) const override + // { + // const auto str = _printer(); + // stream_print(out_string_stream, str.c_str()); + // } + + // private: + // Printer _printer; + // }; + + // template + // EcEntryLambda make_ec_entry_lambda(const char* file, unsigned line, const char* descr, Printer&& printer) + // { + // return {file, line, descr, std::move(printer)}; + // } + + template + struct decay_char_array { using type = T; }; + + template + struct decay_char_array { using type = const char*; }; + + template + struct make_const_ptr { using type = T; }; + + template + struct make_const_ptr { using type = const T*; }; + + template + struct make_ec_type { using type = typename make_const_ptr::type>::type; }; + + /* A stack trace gives you the names of the function at the point of a crash. + With ERROR_CONTEXT, you can also get the values of select local variables. + Usage: + + void process_customers(const std::string& filename) + { + ERROR_CONTEXT("Processing file", filename.c_str()); + for (int customer_index : ...) + { + ERROR_CONTEXT("Customer index", customer_index); + ... + } + } + + The context is in effect during the scope of the ERROR_CONTEXT. + Use loguru::get_error_context() to get the contents of the active error contexts. + + Example result: + + ------------------------------------------------ + [ErrorContext] main.cpp:416 Processing file: "customers.json" + [ErrorContext] main.cpp:417 Customer index: 42 + ------------------------------------------------ + + Error contexts are printed automatically on crashes, and only on crashes. + This makes them much faster than logging the value of a variable. + */ + #define ERROR_CONTEXT(descr, data) \ + const loguru::EcEntryData::type> \ + LOGURU_ANONYMOUS_VARIABLE(error_context_scope_)( \ + __FILE__, __LINE__, descr, data, \ + static_cast::type>::Printer>(loguru::ec_to_text) ) // For better error messages + +/* + #define ERROR_CONTEXT(descr, data) \ + const auto LOGURU_ANONYMOUS_VARIABLE(error_context_scope_)( \ + loguru::make_ec_entry_lambda(__FILE__, __LINE__, descr, \ + [=](){ return loguru::ec_to_text(data); })) +*/ + + using EcHandle = const EcEntryBase*; + + /* + Get a light-weight handle to the error context stack on this thread. + The handle is valid as long as the current thread has no changes to its error context stack. + You can pass the handle to loguru::get_error_context on another thread. + This can be very useful for when you have a parent thread spawning several working threads, + and you want the error context of the parent thread to get printed (too) when there is an + error on the child thread. You can accomplish this thusly: + + void foo(const char* parameter) + { + ERROR_CONTEXT("parameter", parameter) + const auto parent_ec_handle = loguru::get_thread_ec_handle(); + + std::thread([=]{ + loguru::set_thread_name("child thread"); + ERROR_CONTEXT("parent context", parent_ec_handle); + dangerous_code(); + }.join(); + } + + */ + LOGURU_EXPORT + EcHandle get_thread_ec_handle(); + + // Get a string describing the current stack of error context. Empty string if there is none. + LOGURU_EXPORT + Text get_error_context(); + + // Get a string describing the error context of the given thread handle. + LOGURU_EXPORT + Text get_error_context_for(EcHandle ec_handle); + + // ------------------------------------------------------------------------ + + LOGURU_EXPORT Text ec_to_text(const char* data); + LOGURU_EXPORT Text ec_to_text(char data); + LOGURU_EXPORT Text ec_to_text(int data); + LOGURU_EXPORT Text ec_to_text(unsigned int data); + LOGURU_EXPORT Text ec_to_text(long data); + LOGURU_EXPORT Text ec_to_text(unsigned long data); + LOGURU_EXPORT Text ec_to_text(long long data); + LOGURU_EXPORT Text ec_to_text(unsigned long long data); + LOGURU_EXPORT Text ec_to_text(float data); + LOGURU_EXPORT Text ec_to_text(double data); + LOGURU_EXPORT Text ec_to_text(long double data); + LOGURU_EXPORT Text ec_to_text(EcHandle); + + /* + You can add ERROR_CONTEXT support for your own types by overloading ec_to_text. Here's how: + + some.hpp: + namespace loguru { + Text ec_to_text(MySmallType data) + Text ec_to_text(const MyBigType* data) + } // namespace loguru + + some.cpp: + namespace loguru { + Text ec_to_text(MySmallType small_value) + { + // Called only when needed, i.e. on a crash. + std::string str = small_value.as_string(); // Format 'small_value' here somehow. + return Text{STRDUP(str.c_str())}; + } + + Text ec_to_text(const MyBigType* big_value) + { + // Called only when needed, i.e. on a crash. + std::string str = big_value->as_string(); // Format 'big_value' here somehow. + return Text{STRDUP(str.c_str())}; + } + } // namespace loguru + + Any file that include some.hpp: + void foo(MySmallType small, const MyBigType& big) + { + ERROR_CONTEXT("Small", small); // Copy ´small` by value. + ERROR_CONTEXT("Big", &big); // `big` should not change during this scope! + .... + } + */ +} // namespace loguru + +// -------------------------------------------------------------------- +// Logging macros + +// LOG_F(2, "Only logged if verbosity is 2 or higher: %d", some_number); +#define VLOG_F(verbosity, ...) \ + ((verbosity) > loguru::current_verbosity_cutoff()) ? (void)0 \ + : loguru::log(verbosity, __FILE__, __LINE__, __VA_ARGS__) + +// LOG_F(INFO, "Foo: %d", some_number); +#define LOG_F(verbosity_name, ...) VLOG_F(loguru::Verbosity_ ## verbosity_name, __VA_ARGS__) + +#define VLOG_IF_F(verbosity, cond, ...) \ + ((verbosity) > loguru::current_verbosity_cutoff() || (cond) == false) \ + ? (void)0 \ + : loguru::log(verbosity, __FILE__, __LINE__, __VA_ARGS__) + +#define LOG_IF_F(verbosity_name, cond, ...) \ + VLOG_IF_F(loguru::Verbosity_ ## verbosity_name, cond, __VA_ARGS__) + +#define VLOG_SCOPE_F(verbosity, ...) \ + loguru::LogScopeRAII LOGURU_ANONYMOUS_VARIABLE(error_context_RAII_) = \ + ((verbosity) > loguru::current_verbosity_cutoff()) ? loguru::LogScopeRAII() : \ + loguru::LogScopeRAII(verbosity, __FILE__, __LINE__, __VA_ARGS__) + +// Raw logging - no preamble, no indentation. Slightly faster than full logging. +#define RAW_VLOG_F(verbosity, ...) \ + ((verbosity) > loguru::current_verbosity_cutoff()) ? (void)0 \ + : loguru::raw_log(verbosity, __FILE__, __LINE__, __VA_ARGS__) + +#define RAW_LOG_F(verbosity_name, ...) RAW_VLOG_F(loguru::Verbosity_ ## verbosity_name, __VA_ARGS__) + +// Use to book-end a scope. Affects logging on all threads. +#define LOG_SCOPE_F(verbosity_name, ...) \ + VLOG_SCOPE_F(loguru::Verbosity_ ## verbosity_name, __VA_ARGS__) + +#define LOG_SCOPE_FUNCTION(verbosity_name) LOG_SCOPE_F(verbosity_name, __func__) + +// ----------------------------------------------- +// ABORT_F macro. Usage: ABORT_F("Cause of error: %s", error_str); + +// Message is optional +#define ABORT_F(...) loguru::log_and_abort(0, "ABORT: ", __FILE__, __LINE__, __VA_ARGS__) + +// -------------------------------------------------------------------- +// CHECK_F macros: + +#define CHECK_WITH_INFO_F(test, info, ...) \ + LOGURU_PREDICT_TRUE((test) == true) ? (void)0 : loguru::log_and_abort(0, "CHECK FAILED: " info " ", __FILE__, \ + __LINE__, ##__VA_ARGS__) + +/* Checked at runtime too. Will print error, then call fatal_handler (if any), then 'abort'. + Note that the test must be boolean. + CHECK_F(ptr); will not compile, but CHECK_F(ptr != nullptr); will. */ +#define CHECK_F(test, ...) CHECK_WITH_INFO_F(test, #test, ##__VA_ARGS__) + +#define CHECK_NOTNULL_F(x, ...) CHECK_WITH_INFO_F((x) != nullptr, #x " != nullptr", ##__VA_ARGS__) + +#define CHECK_OP_F(expr_left, expr_right, op, ...) \ + do \ + { \ + auto val_left = expr_left; \ + auto val_right = expr_right; \ + if (! LOGURU_PREDICT_TRUE(val_left op val_right)) \ + { \ + auto str_left = loguru::format_value(val_left); \ + auto str_right = loguru::format_value(val_right); \ + auto fail_info = loguru::textprintf("CHECK FAILED: " LOGURU_FMT(s) " " LOGURU_FMT(s) " " LOGURU_FMT(s) " (" LOGURU_FMT(s) " " LOGURU_FMT(s) " " LOGURU_FMT(s) ") ", \ + #expr_left, #op, #expr_right, str_left.c_str(), #op, str_right.c_str()); \ + auto user_msg = loguru::textprintf(__VA_ARGS__); \ + loguru::log_and_abort(0, fail_info.c_str(), __FILE__, __LINE__, \ + LOGURU_FMT(s), user_msg.c_str()); \ + } \ + } while (false) + +#ifndef LOGURU_DEBUG_LOGGING + #ifndef NDEBUG + #define LOGURU_DEBUG_LOGGING 1 + #else + #define LOGURU_DEBUG_LOGGING 0 + #endif +#endif + +#if LOGURU_DEBUG_LOGGING + // Debug logging enabled: + #define DLOG_F(verbosity_name, ...) LOG_F(verbosity_name, __VA_ARGS__) + #define DVLOG_F(verbosity, ...) VLOG_F(verbosity, __VA_ARGS__) + #define DLOG_IF_F(verbosity_name, ...) LOG_IF_F(verbosity_name, __VA_ARGS__) + #define DVLOG_IF_F(verbosity, ...) VLOG_IF_F(verbosity, __VA_ARGS__) + #define DRAW_LOG_F(verbosity_name, ...) RAW_LOG_F(verbosity_name, __VA_ARGS__) + #define DRAW_VLOG_F(verbosity, ...) RAW_VLOG_F(verbosity, __VA_ARGS__) +#else + // Debug logging disabled: + #define DLOG_F(verbosity_name, ...) + #define DVLOG_F(verbosity, ...) + #define DLOG_IF_F(verbosity_name, ...) + #define DVLOG_IF_F(verbosity, ...) + #define DRAW_LOG_F(verbosity_name, ...) + #define DRAW_VLOG_F(verbosity, ...) +#endif + +#define CHECK_EQ_F(a, b, ...) CHECK_OP_F(a, b, ==, ##__VA_ARGS__) +#define CHECK_NE_F(a, b, ...) CHECK_OP_F(a, b, !=, ##__VA_ARGS__) +#define CHECK_LT_F(a, b, ...) CHECK_OP_F(a, b, < , ##__VA_ARGS__) +#define CHECK_GT_F(a, b, ...) CHECK_OP_F(a, b, > , ##__VA_ARGS__) +#define CHECK_LE_F(a, b, ...) CHECK_OP_F(a, b, <=, ##__VA_ARGS__) +#define CHECK_GE_F(a, b, ...) CHECK_OP_F(a, b, >=, ##__VA_ARGS__) + +#ifndef LOGURU_DEBUG_CHECKS + #ifndef NDEBUG + #define LOGURU_DEBUG_CHECKS 1 + #else + #define LOGURU_DEBUG_CHECKS 0 + #endif +#endif + +#if LOGURU_DEBUG_CHECKS + // Debug checks enabled: + #define DCHECK_F(test, ...) CHECK_F(test, ##__VA_ARGS__) + #define DCHECK_NOTNULL_F(x, ...) CHECK_NOTNULL_F(x, ##__VA_ARGS__) + #define DCHECK_EQ_F(a, b, ...) CHECK_EQ_F(a, b, ##__VA_ARGS__) + #define DCHECK_NE_F(a, b, ...) CHECK_NE_F(a, b, ##__VA_ARGS__) + #define DCHECK_LT_F(a, b, ...) CHECK_LT_F(a, b, ##__VA_ARGS__) + #define DCHECK_LE_F(a, b, ...) CHECK_LE_F(a, b, ##__VA_ARGS__) + #define DCHECK_GT_F(a, b, ...) CHECK_GT_F(a, b, ##__VA_ARGS__) + #define DCHECK_GE_F(a, b, ...) CHECK_GE_F(a, b, ##__VA_ARGS__) +#else + // Debug checks disabled: + #define DCHECK_F(test, ...) + #define DCHECK_NOTNULL_F(x, ...) + #define DCHECK_EQ_F(a, b, ...) + #define DCHECK_NE_F(a, b, ...) + #define DCHECK_LT_F(a, b, ...) + #define DCHECK_LE_F(a, b, ...) + #define DCHECK_GT_F(a, b, ...) + #define DCHECK_GE_F(a, b, ...) +#endif // NDEBUG + + +#if LOGURU_REDEFINE_ASSERT + #undef assert + #ifndef NDEBUG + // Debug: + #define assert(test) CHECK_WITH_INFO_F(!!(test), #test) // HACK + #else + #define assert(test) + #endif +#endif // LOGURU_REDEFINE_ASSERT + +#endif // LOGURU_HAS_DECLARED_FORMAT_HEADER + +// ---------------------------------------------------------------------------- +// .dP"Y8 888888 88""Yb 888888 db 8b d8 .dP"Y8 +// `Ybo." 88 88__dP 88__ dPYb 88b d88 `Ybo." +// o.`Y8b 88 88"Yb 88"" dP__Yb 88YbdP88 o.`Y8b +// 8bodP' 88 88 Yb 888888 dP""""Yb 88 YY 88 8bodP' + +#if LOGURU_WITH_STREAMS +#ifndef LOGURU_HAS_DECLARED_STREAMS_HEADER +#define LOGURU_HAS_DECLARED_STREAMS_HEADER + +/* This file extends loguru to enable std::stream-style logging, a la Glog. + It's an optional feature behind the LOGURU_WITH_STREAMS settings + because including it everywhere will slow down compilation times. +*/ + +#include +#include // Adds about 38 kLoC on clang. +#include + +namespace loguru +{ + // Like sprintf, but returns the formated text. + LOGURU_EXPORT + std::string strprintf(LOGURU_FORMAT_STRING_TYPE format, ...) LOGURU_PRINTF_LIKE(1, 2); + + // Like vsprintf, but returns the formated text. + LOGURU_EXPORT + std::string vstrprintf(LOGURU_FORMAT_STRING_TYPE format, va_list) LOGURU_PRINTF_LIKE(1, 0); + + class LOGURU_EXPORT StreamLogger + { + public: + StreamLogger(Verbosity verbosity, const char* file, unsigned line) : _verbosity(verbosity), _file(file), _line(line) {} + ~StreamLogger() noexcept(false); + + template + StreamLogger& operator<<(const T& t) + { + _ss << t; + return *this; + } + + // std::endl and other iomanip:s. + StreamLogger& operator<<(std::ostream&(*f)(std::ostream&)) + { + f(_ss); + return *this; + } + + private: + Verbosity _verbosity; + const char* _file; + unsigned _line; + std::ostringstream _ss; + }; + + class LOGURU_EXPORT AbortLogger + { + public: + AbortLogger(const char* expr, const char* file, unsigned line) : _expr(expr), _file(file), _line(line) { } + LOGURU_NORETURN ~AbortLogger() noexcept(false); + + template + AbortLogger& operator<<(const T& t) + { + _ss << t; + return *this; + } + + // std::endl and other iomanip:s. + AbortLogger& operator<<(std::ostream&(*f)(std::ostream&)) + { + f(_ss); + return *this; + } + + private: + const char* _expr; + const char* _file; + unsigned _line; + std::ostringstream _ss; + }; + + class LOGURU_EXPORT Voidify + { + public: + Voidify() {} + // This has to be an operator with a precedence lower than << but higher than ?: + void operator&(const StreamLogger&) { } + void operator&(const AbortLogger&) { } + }; + + /* Helper functions for CHECK_OP_S macro. + GLOG trick: The (int, int) specialization works around the issue that the compiler + will not instantiate the template version of the function on values of unnamed enum type. */ + #define DEFINE_CHECK_OP_IMPL(name, op) \ + template \ + inline std::string* name(const char* expr, const T1& v1, const char* op_str, const T2& v2) \ + { \ + if (LOGURU_PREDICT_TRUE(v1 op v2)) { return NULL; } \ + std::ostringstream ss; \ + ss << "CHECK FAILED: " << expr << " (" << v1 << " " << op_str << " " << v2 << ") "; \ + return new std::string(ss.str()); \ + } \ + inline std::string* name(const char* expr, int v1, const char* op_str, int v2) \ + { \ + return name(expr, v1, op_str, v2); \ + } + + DEFINE_CHECK_OP_IMPL(check_EQ_impl, ==) + DEFINE_CHECK_OP_IMPL(check_NE_impl, !=) + DEFINE_CHECK_OP_IMPL(check_LE_impl, <=) + DEFINE_CHECK_OP_IMPL(check_LT_impl, < ) + DEFINE_CHECK_OP_IMPL(check_GE_impl, >=) + DEFINE_CHECK_OP_IMPL(check_GT_impl, > ) + #undef DEFINE_CHECK_OP_IMPL + + /* GLOG trick: Function is overloaded for integral types to allow static const integrals + declared in classes and not defined to be used as arguments to CHECK* macros. */ + template + inline const T& referenceable_value(const T& t) { return t; } + inline char referenceable_value(char t) { return t; } + inline unsigned char referenceable_value(unsigned char t) { return t; } + inline signed char referenceable_value(signed char t) { return t; } + inline short referenceable_value(short t) { return t; } + inline unsigned short referenceable_value(unsigned short t) { return t; } + inline int referenceable_value(int t) { return t; } + inline unsigned int referenceable_value(unsigned int t) { return t; } + inline long referenceable_value(long t) { return t; } + inline unsigned long referenceable_value(unsigned long t) { return t; } + inline long long referenceable_value(long long t) { return t; } + inline unsigned long long referenceable_value(unsigned long long t) { return t; } +} // namespace loguru + +// ----------------------------------------------- +// Logging macros: + +// usage: LOG_STREAM(INFO) << "Foo " << std::setprecision(10) << some_value; +#define VLOG_IF_S(verbosity, cond) \ + ((verbosity) > loguru::current_verbosity_cutoff() || (cond) == false) \ + ? (void)0 \ + : loguru::Voidify() & loguru::StreamLogger(verbosity, __FILE__, __LINE__) +#define LOG_IF_S(verbosity_name, cond) VLOG_IF_S(loguru::Verbosity_ ## verbosity_name, cond) +#define VLOG_S(verbosity) VLOG_IF_S(verbosity, true) +#define LOG_S(verbosity_name) VLOG_S(loguru::Verbosity_ ## verbosity_name) + +// ----------------------------------------------- +// ABORT_S macro. Usage: ABORT_S() << "Causo of error: " << details; + +#define ABORT_S() loguru::Voidify() & loguru::AbortLogger("ABORT: ", __FILE__, __LINE__) + +// ----------------------------------------------- +// CHECK_S macros: + +#define CHECK_WITH_INFO_S(cond, info) \ + LOGURU_PREDICT_TRUE((cond) == true) \ + ? (void)0 \ + : loguru::Voidify() & loguru::AbortLogger("CHECK FAILED: " info " ", __FILE__, __LINE__) + +#define CHECK_S(cond) CHECK_WITH_INFO_S(cond, #cond) +#define CHECK_NOTNULL_S(x) CHECK_WITH_INFO_S((x) != nullptr, #x " != nullptr") + +#define CHECK_OP_S(function_name, expr1, op, expr2) \ + while (auto error_string = loguru::function_name(#expr1 " " #op " " #expr2, \ + loguru::referenceable_value(expr1), #op, \ + loguru::referenceable_value(expr2))) \ + loguru::AbortLogger(error_string->c_str(), __FILE__, __LINE__) + +#define CHECK_EQ_S(expr1, expr2) CHECK_OP_S(check_EQ_impl, expr1, ==, expr2) +#define CHECK_NE_S(expr1, expr2) CHECK_OP_S(check_NE_impl, expr1, !=, expr2) +#define CHECK_LE_S(expr1, expr2) CHECK_OP_S(check_LE_impl, expr1, <=, expr2) +#define CHECK_LT_S(expr1, expr2) CHECK_OP_S(check_LT_impl, expr1, < , expr2) +#define CHECK_GE_S(expr1, expr2) CHECK_OP_S(check_GE_impl, expr1, >=, expr2) +#define CHECK_GT_S(expr1, expr2) CHECK_OP_S(check_GT_impl, expr1, > , expr2) + +#if LOGURU_DEBUG_LOGGING + // Debug logging enabled: + #define DVLOG_IF_S(verbosity, cond) VLOG_IF_S(verbosity, cond) + #define DLOG_IF_S(verbosity_name, cond) LOG_IF_S(verbosity_name, cond) + #define DVLOG_S(verbosity) VLOG_S(verbosity) + #define DLOG_S(verbosity_name) LOG_S(verbosity_name) +#else + // Debug logging disabled: + #define DVLOG_IF_S(verbosity, cond) \ + (true || (verbosity) > loguru::current_verbosity_cutoff() || (cond) == false) \ + ? (void)0 \ + : loguru::Voidify() & loguru::StreamLogger(verbosity, __FILE__, __LINE__) + + #define DLOG_IF_S(verbosity_name, cond) DVLOG_IF_S(loguru::Verbosity_ ## verbosity_name, cond) + #define DVLOG_S(verbosity) DVLOG_IF_S(verbosity, true) + #define DLOG_S(verbosity_name) DVLOG_S(loguru::Verbosity_ ## verbosity_name) +#endif + +#if LOGURU_DEBUG_CHECKS + // Debug checks enabled: + #define DCHECK_S(cond) CHECK_S(cond) + #define DCHECK_NOTNULL_S(x) CHECK_NOTNULL_S(x) + #define DCHECK_EQ_S(a, b) CHECK_EQ_S(a, b) + #define DCHECK_NE_S(a, b) CHECK_NE_S(a, b) + #define DCHECK_LT_S(a, b) CHECK_LT_S(a, b) + #define DCHECK_LE_S(a, b) CHECK_LE_S(a, b) + #define DCHECK_GT_S(a, b) CHECK_GT_S(a, b) + #define DCHECK_GE_S(a, b) CHECK_GE_S(a, b) +#else +// Debug checks disabled: + #define DCHECK_S(cond) CHECK_S(true || (cond)) + #define DCHECK_NOTNULL_S(x) CHECK_S(true || (x) != nullptr) + #define DCHECK_EQ_S(a, b) CHECK_S(true || (a) == (b)) + #define DCHECK_NE_S(a, b) CHECK_S(true || (a) != (b)) + #define DCHECK_LT_S(a, b) CHECK_S(true || (a) < (b)) + #define DCHECK_LE_S(a, b) CHECK_S(true || (a) <= (b)) + #define DCHECK_GT_S(a, b) CHECK_S(true || (a) > (b)) + #define DCHECK_GE_S(a, b) CHECK_S(true || (a) >= (b)) +#endif + +#if LOGURU_REPLACE_GLOG + #undef LOG + #undef VLOG + #undef LOG_IF + #undef VLOG_IF + #undef CHECK + #undef CHECK_NOTNULL + #undef CHECK_EQ + #undef CHECK_NE + #undef CHECK_LT + #undef CHECK_LE + #undef CHECK_GT + #undef CHECK_GE + #undef DLOG + #undef DVLOG + #undef DLOG_IF + #undef DVLOG_IF + #undef DCHECK + #undef DCHECK_NOTNULL + #undef DCHECK_EQ + #undef DCHECK_NE + #undef DCHECK_LT + #undef DCHECK_LE + #undef DCHECK_GT + #undef DCHECK_GE + #undef VLOG_IS_ON + + #define LOG LOG_S + #define VLOG VLOG_S + #define LOG_IF LOG_IF_S + #define VLOG_IF VLOG_IF_S + #define CHECK(cond) CHECK_S(!!(cond)) + #define CHECK_NOTNULL CHECK_NOTNULL_S + #define CHECK_EQ CHECK_EQ_S + #define CHECK_NE CHECK_NE_S + #define CHECK_LT CHECK_LT_S + #define CHECK_LE CHECK_LE_S + #define CHECK_GT CHECK_GT_S + #define CHECK_GE CHECK_GE_S + #define DLOG DLOG_S + #define DVLOG DVLOG_S + #define DLOG_IF DLOG_IF_S + #define DVLOG_IF DVLOG_IF_S + #define DCHECK DCHECK_S + #define DCHECK_NOTNULL DCHECK_NOTNULL_S + #define DCHECK_EQ DCHECK_EQ_S + #define DCHECK_NE DCHECK_NE_S + #define DCHECK_LT DCHECK_LT_S + #define DCHECK_LE DCHECK_LE_S + #define DCHECK_GT DCHECK_GT_S + #define DCHECK_GE DCHECK_GE_S + #define VLOG_IS_ON(verbosity) ((verbosity) <= loguru::current_verbosity_cutoff()) + +#endif // LOGURU_REPLACE_GLOG + +#endif // LOGURU_WITH_STREAMS + +#endif // LOGURU_HAS_DECLARED_STREAMS_HEADER diff --git a/projects/Functional/Algebra/main.cpp b/projects/Functional/Algebra/main.cpp new file mode 100644 index 0000000000..3622efc16a --- /dev/null +++ b/projects/Functional/Algebra/main.cpp @@ -0,0 +1,71 @@ +#include +#include +#include +#include + +using VectorType = StdVector; + +int main(){ + + loguru::add_file("test_ibm_everything.log", loguru::Append, loguru::Verbosity_MAX); + loguru::add_file("test_ibm_warning.log", loguru::Append, loguru::Verbosity_WARNING); + loguru::g_stderr_verbosity = loguru::Verbosity_FATAL; + + std::cout << "Create solid mesh.\n"; + auto domain = std::make_shared(dolfin::Point(0.6,0.5,0.5), 0.2); + auto solid_mesh = std::make_shared(mshr::generate_mesh(domain, 10)); + + std::cout << "Create background mesh.\n"; + int3 dim = {16,16,16}; + auto fluid_mesh = std::make_shared(dim); + + // ImmersedBoundaryMethod ibm(solid_mesh, fluid_mesh); + // ibm.forward_euler_scheme(); + + + auto ibm_problem = std::make_shared>(solid_mesh, fluid_mesh); + + // ibm_problem->forward_euler_scheme(); + + auto bicgstab = std::make_shared>(ibm_problem->unkown_size()/3); + NewtonSolver ns(bicgstab); + + // TODO : param b is useless here. + std::vector x0(ibm_problem->unkown_size()/3); + std::vector b(ibm_problem->unkown_size()/3); + + VectorType xx0; + VectorType bb; + + + + // set the initial value of x0; + x0 = ibm_problem->get_solid_positions(); + auto xn_1 = x0; // x_{n-1} + + + for (size_t i = 0; i < 100; i++) + { + Timer timer("nonlinear test.\n"); + // x0 = 2*x0 - xn_1; + // initial guess for x0; + for (size_t i = 0; i < x0.size(); i++) + { + double3 temp = {2.0*x0[i].x - xn_1[i].x, 2.0*x0[i].y - xn_1[i].y,2.0*x0[i].z - xn_1[i].z}; + xn_1[i] = x0[i]; + x0[i] = temp; + } + xx0.set(x0); + bb.set(b); + auto nonlinear_result = ns.Solve(ibm_problem, xx0, bb); + xx0.get(x0); + ibm_problem->advance(x0); + ibm_problem->record(i); + LOG_F(WARNING, "xn: %.8lf, %.8lf", x0[0].x, x0[1].y); + LOG_F(WARNING, "Noninear solver successful? %d", nonlinear_result.first); + LOG_F(WARNING, "residual : %lf, iter : %d", nonlinear_result.second.first, nonlinear_result.second.second); + } + + + return 0; +} \ No newline at end of file diff --git a/projects/Functional/CMakeLists.txt b/projects/Functional/CMakeLists.txt index bd800307b3..250b3aa061 100644 --- a/projects/Functional/CMakeLists.txt +++ b/projects/Functional/CMakeLists.txt @@ -1,3 +1,14 @@ -add_library(zeno_Functional SHARED NonlinearSolver.cpp) + + + +cmake_minimum_required(VERSION 3.10) +add_library(zeno_Functional SHARED + NonlinearSolver.cpp +) + +add_subdirectory(Algebra) +target_link_libraries(zeno_Functional PUBLIC IBFE) target_link_libraries(zeno_Functional PRIVATE zeno) -target_link_libraries(zeno_Functional PRIVATE zeno_nodep) \ No newline at end of file +target_link_libraries(zeno_Functional PRIVATE zeno_nodep) + +# cmake -B build -DEXTENSION_Functional:BOOL=ON \ No newline at end of file diff --git a/projects/Functional/NonlinearSolver.cpp b/projects/Functional/NonlinearSolver.cpp index 5974efcbf0..c29fd03232 100644 --- a/projects/Functional/NonlinearSolver.cpp +++ b/projects/Functional/NonlinearSolver.cpp @@ -1,47 +1,74 @@ +/** + * @file NonlinearSolverNodes.cpp + * @author Ma Pengfei (mapengfei@mail.nwpu.edu.cn) + * @brief An example for nonlinear solver on zeno. + * @version 0.1 + * @date 2022-02-04 + * + * @copyright Copyright (c) 2022 Ma Pengfei + * + */ + +#include +#include +#include +#include +#include +#include + + #include #include #include #include #include -#include -class NonlinearProblem { - virtual void Residual(const double &x, double &r) = 0; -}; +using VectorType = StdVector; -struct NonlinearProblemObject2 : zeno::IObject, NonlinearProblem +struct NonlinearProblemObject : zeno::IObject, NonlinearProblem { std::shared_ptr function = nullptr; - virtual void Residual(const double &x, double &r){ + virtual void Residual(const VectorType& x, VectorType& r){ auto args = std::make_shared(); auto rets = std::make_shared(); - std::cout << " Before calling func. \n"; - rets->lut = function->call(args->lut); - std::cout << " After calling func. \n"; + CHECK_F(x.size() == r.size(), "Wrong size."); + + args->lut["x"] = x; // 封装 + rets->lut = function->call(args->lut); // 调用 + r = zeno::safe_any_cast(rets->lut["r"]); // 解封 + } }; -struct CalculateResidual2 : zeno::INode { - +// User defined +struct CalculateResidual : zeno::INode { virtual void apply() override { auto args = get_input("args"); auto rets = std::make_shared(); - static int n = 0; + // std::cout << " Calculate Residual 1 \n"; + auto x = zeno::safe_any_cast(args->lut.at("x")); + VectorType r; + r.resize(1); + CHECK_F(x.size() == r.size(), "Wrong size."); + // std::cout << " Calculate Residual 1 \n"; + + auto _x = flatten(x).data; + auto _r = flatten(r).data; - n++; - std::cout << " Calculate Residual for the \n" << n << "th time.\n"; + _r[0] = std::exp(2.0*_x[0])/2.0 - _x[1]; + _r[1] = _x[0]*_x[0] + _x[1]*_x[1]-1.0; - rets->lut["n"] = n; + rets->lut["r"] = r; set_output("rets", std::move(rets)); } }; -ZENDEFNODE(CalculateResidual2, +ZENDEFNODE(CalculateResidual, { /* inputs: */ { "args", }, /* outputs: */ { @@ -52,22 +79,22 @@ ZENDEFNODE(CalculateResidual2, "Zentricle", }}); -struct MakeNonlinearProblemObject2 : zeno::INode { +struct MakeNonlinearProblemObject : zeno::INode { virtual void apply() override { - auto nlp = std::make_shared(); + auto nlp = std::make_shared(); // TODO : input a function constructed with FuncBegin and FuncEnd if (has_input("function")) nlp->function = get_input("function"); - // else LOG_F(WARNING, "no function input"); - set_output("NonlinearProblemObject2", std::move(nlp)); + else LOG_F(WARNING, "no function input"); + set_output("NonlinearProblemObject", std::move(nlp)); } }; -ZENDEFNODE(MakeNonlinearProblemObject2, +ZENDEFNODE(MakeNonlinearProblemObject, { /* inputs: */ { "function", }, /* outputs: */ { - "NonlinearProblemObject2", + "NonlinearProblemObject", }, /* params: */ { {}, // defl min max; defl min; defl }, /* category: */ { @@ -75,27 +102,34 @@ ZENDEFNODE(MakeNonlinearProblemObject2, }}); -struct JFNKSolverObject2 : zeno::IObject +struct JFNKSolverObject : zeno::IObject { - // std::shared_ptr> ns = nullptr; + std::shared_ptr> ns = nullptr; }; -struct MakeJFNKSolver2 : zeno::INode { +struct MakeJFNKSolver : zeno::INode { virtual void apply() override { - auto jfnk_solver = std::make_shared(); - // jfnk_solver->ns = ns; + // TODO : Need parameters such as problem size, line search newton solver, bicgstab - set_output("JFNKSolverObject2", std::move(jfnk_solver)); + auto bicgstab = std::make_shared>(1); + auto ns = std::make_shared>(bicgstab); + auto method = ns->method(); + LOG_F(INFO, "using %s. ", method.c_str()); + + auto jfnk_solver = std::make_shared(); + jfnk_solver->ns = ns; + + set_output("JFNKSolverObject", std::move(jfnk_solver)); } }; -ZENDEFNODE(MakeJFNKSolver2, +ZENDEFNODE(MakeJFNKSolver, { /* inputs: */ { }, /* outputs: */ { - "JFNKSolverObject2", + "JFNKSolverObject", }, /* params: */ { {}, // defl min max; defl min; defl }, /* category: */ { @@ -103,31 +137,107 @@ ZENDEFNODE(MakeJFNKSolver2, }}); -struct SolveNonlinearProblem2 : zeno::INode { +struct RawDoubleObject : zeno::IObject +{ + double * data = nullptr; + int num = 0; +}; +struct MakeRawDoubleObject : zeno::INode { virtual void apply() override { + auto raw_double_array = std::make_shared(); - auto jfnk_solver = get_input("JFNKSolverObject2"); - auto nlp = get_input("NonlinearProblemObject2"); + auto num = get_param("value"); + auto data = (double*)malloc(num*sizeof(double)); + std::fill(data, data+num, 1.0); + + raw_double_array->num = num; + raw_double_array->data = data; + + set_output("RawDoubleObject", std::move(raw_double_array)); + } +}; + +ZENDEFNODE(MakeRawDoubleObject, + { /* inputs: */ { + }, /* outputs: */ { + "RawDoubleObject", + }, /* params: */ { + {"int", "value", "0"}, // defl min max; defl min; defl + }, /* category: */ { + "Zentricle", + }}); - auto args = std::make_shared(); - auto rets = std::make_shared(); - - auto function = get_input("function"); - for (size_t i = 0; i < 100; i++){ - std::cout << " Before calling func. \n"; - rets->lut = function->call(args->lut); - std::cout << " After calling func. \n"; - } +struct PrintRawDoubleObject : zeno::INode { + virtual void apply() override { + auto x_raw = get_input("RawDoubleObject"); + + for (size_t i = 0; i < x_raw->num; i++) + { + printf("%lf\n", x_raw->data[i]); + } + } +}; + +ZENDEFNODE(PrintRawDoubleObject, + { /* inputs: */ { + "RawDoubleObject", + }, /* outputs: */ { + }, /* params: */ { + // defl min max; defl min; defl + }, /* category: */ { + "Zentricle", + }}); + +struct SolveNonlinearProblem : zeno::INode { + + virtual void apply() override { + + auto jfnk_solver = get_input("JFNKSolverObject"); + auto nlp = get_input("NonlinearProblemObject"); + auto x_raw = get_input("RawDoubleObject"); + + /////////////////////////////////////////////////////////////////////////////////////////// + // NOTE : we solve F(x) = b here. But b is not necessary. b is to be removed. + VectorType x0; + VectorType bb; + + auto size = x_raw->num/x0.value_size(); + CHECK_F(x_raw->num%x0.value_size()==0, "Wrong size."); + + x0.resize(size); + bb.resize(size); + + auto _x0 = flatten(x0); + auto _bb = flatten(bb); + + for (size_t i = 0; i < x_raw->num; i++) + { + _x0.data[i] = x_raw->data[i]; + _bb.data[i] = 0.0; + } + ///////////////////////////////////////////////////////////////////////////////////////////// + + auto nonlinear_result = jfnk_solver->ns->Solve(nlp, x0, bb); + for (size_t i = 0; i < x_raw->num; i++) + { + x_raw->data[i] = _x0.data[i]; + } + + LOG_F(WARNING, "Noninear solver successful??????? %d", nonlinear_result.first); + LOG_F(WARNING, "residual : %lf, iter : %d", nonlinear_result.second.first, nonlinear_result.second.second); + + set_output("RawDoubleObject", std::move(x_raw)); } }; -ZENDEFNODE(SolveNonlinearProblem2, +ZENDEFNODE(SolveNonlinearProblem, { /* inputs: */ { - "JFNKSolverObject2", "NonlinearProblemObject2", "function", + "JFNKSolverObject", "NonlinearProblemObject", "RawDoubleObject", }, /* outputs: */ { + "RawDoubleObject", }, /* params: */ { {}, // defl min max; defl min; defl }, /* category: */ { diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt deleted file mode 100644 index b96d27463b..0000000000 --- a/tests/CMakeLists.txt +++ /dev/null @@ -1,3 +0,0 @@ -file(GLOB TEST_SOURCE *.cpp) -add_executable(zeno_tests ${TEST_SOURCE}) -target_link_libraries(zeno_tests PRIVATE zeno) diff --git a/tests/catch.cpp b/tests/catch.cpp deleted file mode 100644 index 4ed06df1f7..0000000000 --- a/tests/catch.cpp +++ /dev/null @@ -1,2 +0,0 @@ -#define CATCH_CONFIG_MAIN -#include diff --git a/tests/test_any.cpp b/tests/test_any.cpp deleted file mode 100644 index 05bb331b35..0000000000 --- a/tests/test_any.cpp +++ /dev/null @@ -1,56 +0,0 @@ -#include -#include -#include -#include - -TEST_CASE("cast any of int to float", "[any]") { - int i = 42; - zeno::Any a = i; - float f = zeno::smart_any_cast(a); - REQUIRE(f == 42.0f); - - i = 32; - a = i; - f = zeno::smart_any_cast(a); - REQUIRE(f == 32.0f); -} - -TEST_CASE("cast any of float to int", "[any]") { - float f = 42.8f; - zeno::Any a = f; - int i = zeno::smart_any_cast(a); - REQUIRE(i == 42); - - f = 32.2f; - a = f; - i = zeno::smart_any_cast(a); - REQUIRE(i == 32); -} - -TEST_CASE("cast any of vec3i to vec3f", "[any]") { - zeno::vec3i i(42, 985, 211); - zeno::Any a = i; - zeno::vec3f f = zeno::smart_any_cast(a); - REQUIRE(alltrue(f == zeno::vec3f(42.0f, 985.0f, 211.0f))); -} - -TEST_CASE("cast any of vec3f to vec3i", "[any]") { - zeno::vec3f f(42.0f, 985.99f, 211.3f); - zeno::Any a = f; - zeno::vec3i i = zeno::smart_any_cast(a); - REQUIRE(alltrue(i == zeno::vec3i(42, 985, 211))); -} - -TEST_CASE("static assertion of underlying type", "[any]") { - REQUIRE(std::is_same_v< - zeno::any_underlying_type_t, - zeno::scalar_type_variant>); - REQUIRE(std::is_same_v< - zeno::any_underlying_type_t>, - std::shared_ptr>); - REQUIRE(std::is_same_v< - zeno::any_underlying_type_t, - zeno::vector_type_variant<4>>); -} - -// TODO: add shared_ptr tests too From d24c112c8a95c680166688cc77c7922c7bdd3cb2 Mon Sep 17 00:00:00 2001 From: Ma Pengfei Date: Sat, 26 Feb 2022 17:37:49 +0800 Subject: [PATCH 2/6] convert raw pointer to datatype inside nonlinear solver --- nonlinear.zsg | 217 +++++++++++++++++------- projects/Functional/NonlinearSolver.cpp | 164 ++++++++++-------- 2 files changed, 247 insertions(+), 134 deletions(-) diff --git a/nonlinear.zsg b/nonlinear.zsg index fd1611b1bd..db67cc904f 100644 --- a/nonlinear.zsg +++ b/nonlinear.zsg @@ -18,8 +18,8 @@ }, "params": {}, "uipos": [ - 167.18541987169112, - 689.0947667081855 + -300.5532735956456, + 541.4565757534112 ], "options": [] }, @@ -39,8 +39,8 @@ }, "params": {}, "uipos": [ - -31.94034056802775, - 915.9211704855329 + -580.6840591609928, + 699.03674837498 ], "options": [] }, @@ -65,14 +65,19 @@ }, "params": {}, "uipos": [ - 268.05965943197236, - 915.9211704855329 + -72.94536569365582, + 746.071924254377 ], "options": [] }, - "c227fd2f-MakeJFNKSolver": { - "name": "MakeJFNKSolver", + "7211c4a7-MakeNonlinearProblemObject": { + "name": "MakeNonlinearProblemObject", "inputs": { + "function": [ + "82a64254-FuncEnd", + "function", + null + ], "SRC": [ null, null, @@ -81,52 +86,39 @@ }, "params": {}, "uipos": [ - 532.6876457800946, - 696.0303855025977 + 179.60070939166815, + 815.3415488277561 ], "options": [ "ONCE" ] }, - "7211c4a7-MakeNonlinearProblemObject": { - "name": "MakeNonlinearProblemObject", + "86a3b179-MakeRawDoubleObject": { + "name": "MakeRawDoubleObject", "inputs": { - "function": [ - "82a64254-FuncEnd", - "function", - null - ], "SRC": [ null, null, null ] }, - "params": {}, + "params": { + "value": 2 + }, "uipos": [ - 520.6057345172964, - 985.190795058912 + -570.3010005230933, + 1253.7639680913087 ], "options": [ "ONCE" ] }, - "f09bc162-SolveNonlinearProblem": { - "name": "SolveNonlinearProblem", + "a0a524e0-MakeJFNKSolver": { + "name": "MakeJFNKSolver", "inputs": { - "JFNKSolverObject": [ - "c227fd2f-MakeJFNKSolver", - "JFNKSolverObject", - null - ], - "NonlinearProblemObject": [ - "7211c4a7-MakeNonlinearProblemObject", - "NonlinearProblemObject", - null - ], - "RawDoubleObject": [ - "86a3b179-MakeRawDoubleObject", - "RawDoubleObject", + "num": [ + "f1a83977-NumericInt", + "value", null ], "SRC": [ @@ -137,13 +129,13 @@ }, "params": {}, "uipos": [ - 841.5818437323131, - 1180.9177575162494 + -133.5929648241207, + 994.9246231155782 ], "options": [] }, - "86a3b179-MakeRawDoubleObject": { - "name": "MakeRawDoubleObject", + "f1a83977-NumericInt": { + "name": "NumericInt", "inputs": { "SRC": [ null, @@ -155,18 +147,16 @@ "value": 2 }, "uipos": [ - 392.9402055070574, - 1212.9348223626657 + -557.2361809045226, + 943.3165829145727 ], - "options": [ - "ONCE" - ] + "options": [] }, - "28e77680-PrintRawDoubleObject": { - "name": "PrintRawDoubleObject", + "8bb99697-ConvertRawdataToVecotor": { + "name": "ConvertRawdataToVecotor", "inputs": { "RawDoubleObject": [ - "f09bc162-SolveNonlinearProblem", + "86a3b179-MakeRawDoubleObject", "RawDoubleObject", null ], @@ -178,19 +168,78 @@ }, "params": {}, "uipos": [ - 1160.0581030150756, - 1224.3640075376884 + -147.6381909547739, + 1229.4472361809046 ], "options": [ "VIEW" ] + }, + "7c6673db-PrintVectorTypeObject": { + "name": "PrintVectorTypeObject", + "inputs": { + "VectorTypeObject": [ + "deb3c8a6-SolveNonlinearProblem", + "VectorTypeObject", + null + ], + "SRC": [ + null, + null, + null + ] + }, + "params": {}, + "uipos": [ + 979.8994974874373, + 891.0552763819096 + ], + "options": [ + "VIEW" + ] + }, + "deb3c8a6-SolveNonlinearProblem": { + "name": "SolveNonlinearProblem", + "inputs": { + "NonlinearProblemObject": [ + "7211c4a7-MakeNonlinearProblemObject", + "NonlinearProblemObject", + null + ], + "JFNKSolverObject": [ + "a0a524e0-MakeJFNKSolver", + "JFNKSolverObject", + null + ], + "RawDoubleObject": [ + null, + null, + null + ], + "VectorTypeObject": [ + "8bb99697-ConvertRawdataToVecotor", + "VectorTypeObject", + null + ], + "SRC": [ + null, + null, + null + ] + }, + "params": {}, + "uipos": [ + 645.427135678392, + 833.56783919598 + ], + "options": [] } }, "view_rect": { - "x": -282.0665515892738, - "y": 544.7659543646841, - "width": 1950.0, - "height": 884.9999999999985 + "x": -433.0653015729266, + "y": 684.1846227882302, + "width": 1560.0, + "height": 707.9999999999985 } } }, @@ -752,6 +801,36 @@ "portal" ] }, + "ConvertRawdataToVecotor": { + "inputs": [ + [ + "", + "RawDoubleObject", + "" + ], + [ + "", + "SRC", + "" + ] + ], + "outputs": [ + [ + "", + "VectorTypeObject", + "" + ], + [ + "", + "DST", + "" + ] + ], + "params": [], + "categories": [ + "Zentricle" + ] + }, "CopyAllUserData": { "inputs": [ [ @@ -2482,6 +2561,11 @@ }, "MakeJFNKSolver": { "inputs": [ + [ + "", + "num", + "" + ], [ "", "SRC", @@ -6089,11 +6173,11 @@ "numeric" ] }, - "PrintRawDoubleObject": { + "PrintString": { "inputs": [ [ - "", - "RawDoubleObject", + "string", + "str", "" ], [ @@ -6111,14 +6195,14 @@ ], "params": [], "categories": [ - "Zentricle" + "string" ] }, - "PrintString": { + "PrintVectorTypeObject": { "inputs": [ [ - "string", - "str", + "", + "VectorTypeObject", "" ], [ @@ -6136,7 +6220,7 @@ ], "params": [], "categories": [ - "string" + "Zentricle" ] }, "RandomParticles": { @@ -6502,12 +6586,12 @@ "inputs": [ [ "", - "JFNKSolverObject", + "NonlinearProblemObject", "" ], [ "", - "NonlinearProblemObject", + "JFNKSolverObject", "" ], [ @@ -6515,6 +6599,11 @@ "RawDoubleObject", "" ], + [ + "", + "VectorTypeObject", + "" + ], [ "", "SRC", @@ -6524,7 +6613,7 @@ "outputs": [ [ "", - "RawDoubleObject", + "VectorTypeObject", "" ], [ diff --git a/projects/Functional/NonlinearSolver.cpp b/projects/Functional/NonlinearSolver.cpp index c29fd03232..b47c507fb2 100644 --- a/projects/Functional/NonlinearSolver.cpp +++ b/projects/Functional/NonlinearSolver.cpp @@ -22,8 +22,78 @@ #include #include #include +#include + +using VectorType = StdVector; + + + +struct RawDoubleObject : zeno::IObject +{ + double * data = nullptr; + int num = 0; +}; + + +struct VectorTypeObject : zeno::IObject +{ + std::shared_ptr vector = nullptr; +}; + +struct ConvertRawdataToVecotor : zeno::INode { + virtual void apply() override { + + auto x_raw = get_input("RawDoubleObject"); + + auto vector_object = std::make_shared(); + vector_object->vector = std::make_shared(); + vector_object->vector->resize(x_raw->num); + + for (size_t i = 0; i < x_raw->num; i++) + { + vector_object->vector->data()[i] = x_raw->data[i]; + } + + set_output("VectorTypeObject", std::move(vector_object)); + } +}; + +ZENDEFNODE(ConvertRawdataToVecotor, + { /* inputs: */ { + "RawDoubleObject", + }, /* outputs: */ { + "VectorTypeObject", + }, /* params: */ { + // defl min max; defl min; defl + }, /* category: */ { + "Zentricle", + }}); + +struct MakeRawDoubleObject : zeno::INode { + virtual void apply() override { + auto raw_double_array = std::make_shared(); + + auto num = get_param("value"); + auto data = (double*)malloc(num*sizeof(double)); + std::fill(data, data+num, 1.0); + + raw_double_array->num = num; + raw_double_array->data = data; + + set_output("RawDoubleObject", std::move(raw_double_array)); + } +}; + +ZENDEFNODE(MakeRawDoubleObject, + { /* inputs: */ { + }, /* outputs: */ { + "RawDoubleObject", + }, /* params: */ { + {"int", "value", "0"}, // defl min max; defl min; defl + }, /* category: */ { + "Zentricle", + }}); -using VectorType = StdVector; struct NonlinearProblemObject : zeno::IObject, NonlinearProblem { @@ -50,12 +120,10 @@ struct CalculateResidual : zeno::INode { auto args = get_input("args"); auto rets = std::make_shared(); - // std::cout << " Calculate Residual 1 \n"; auto x = zeno::safe_any_cast(args->lut.at("x")); VectorType r; - r.resize(1); + r.resize(x.size()); CHECK_F(x.size() == r.size(), "Wrong size."); - // std::cout << " Calculate Residual 1 \n"; auto _x = flatten(x).data; auto _r = flatten(r).data; @@ -113,8 +181,8 @@ struct MakeJFNKSolver : zeno::INode { virtual void apply() override { // TODO : Need parameters such as problem size, line search newton solver, bicgstab - - auto bicgstab = std::make_shared>(1); + auto num = get_input("num")->get(); + auto bicgstab = std::make_shared>(num); auto ns = std::make_shared>(bicgstab); auto method = ns->method(); LOG_F(INFO, "using %s. ", method.c_str()); @@ -128,6 +196,7 @@ struct MakeJFNKSolver : zeno::INode { ZENDEFNODE(MakeJFNKSolver, { /* inputs: */ { + "num", }, /* outputs: */ { "JFNKSolverObject", }, /* params: */ { @@ -137,52 +206,20 @@ ZENDEFNODE(MakeJFNKSolver, }}); -struct RawDoubleObject : zeno::IObject -{ - double * data = nullptr; - int num = 0; -}; - -struct MakeRawDoubleObject : zeno::INode { +struct PrintVectorTypeObject : zeno::INode { virtual void apply() override { - auto raw_double_array = std::make_shared(); + auto x = get_input("VectorTypeObject")->vector; - auto num = get_param("value"); - auto data = (double*)malloc(num*sizeof(double)); - std::fill(data, data+num, 1.0); - - raw_double_array->num = num; - raw_double_array->data = data; - - set_output("RawDoubleObject", std::move(raw_double_array)); - } -}; - -ZENDEFNODE(MakeRawDoubleObject, - { /* inputs: */ { - }, /* outputs: */ { - "RawDoubleObject", - }, /* params: */ { - {"int", "value", "0"}, // defl min max; defl min; defl - }, /* category: */ { - "Zentricle", - }}); - - -struct PrintRawDoubleObject : zeno::INode { - virtual void apply() override { - auto x_raw = get_input("RawDoubleObject"); - - for (size_t i = 0; i < x_raw->num; i++) + for (size_t i = 0; i < x->size(); i++) { - printf("%lf\n", x_raw->data[i]); + printf("%lf\n", x->data()[i]); } } }; -ZENDEFNODE(PrintRawDoubleObject, +ZENDEFNODE(PrintVectorTypeObject, { /* inputs: */ { - "RawDoubleObject", + "VectorTypeObject", }, /* outputs: */ { }, /* params: */ { // defl min max; defl min; defl @@ -196,48 +233,35 @@ struct SolveNonlinearProblem : zeno::INode { auto jfnk_solver = get_input("JFNKSolverObject"); auto nlp = get_input("NonlinearProblemObject"); - auto x_raw = get_input("RawDoubleObject"); + + auto x_vector_object = get_input("VectorTypeObject"); + auto x_vector_ptr = x_vector_object->vector; /////////////////////////////////////////////////////////////////////////////////////////// // NOTE : we solve F(x) = b here. But b is not necessary. b is to be removed. - VectorType x0; VectorType bb; + bb.resize(x_vector_ptr->size()); + ///////////////////////////////////////////////////////////////////////////////////////////// - auto size = x_raw->num/x0.value_size(); - CHECK_F(x_raw->num%x0.value_size()==0, "Wrong size."); - - x0.resize(size); - bb.resize(size); + auto nonlinear_result = jfnk_solver->ns->Solve(nlp, *x_vector_ptr, bb); - auto _x0 = flatten(x0); - auto _bb = flatten(bb); - for (size_t i = 0; i < x_raw->num; i++) - { - _x0.data[i] = x_raw->data[i]; - _bb.data[i] = 0.0; + if ( nonlinear_result.first) { + LOG_F(WARNING, "Noninear solver succeed"); + LOG_F(WARNING, "residual : %lf, iter : %d", nonlinear_result.second.first, nonlinear_result.second.second); } - ///////////////////////////////////////////////////////////////////////////////////////////// + else LOG_F(WARNING, "Noninear solver failed"); - auto nonlinear_result = jfnk_solver->ns->Solve(nlp, x0, bb); - for (size_t i = 0; i < x_raw->num; i++) - { - x_raw->data[i] = _x0.data[i]; - } - - LOG_F(WARNING, "Noninear solver successful??????? %d", nonlinear_result.first); - LOG_F(WARNING, "residual : %lf, iter : %d", nonlinear_result.second.first, nonlinear_result.second.second); - - set_output("RawDoubleObject", std::move(x_raw)); + set_output("VectorTypeObject", std::move(x_vector_object)); } }; ZENDEFNODE(SolveNonlinearProblem, { /* inputs: */ { - "JFNKSolverObject", "NonlinearProblemObject", "RawDoubleObject", + "NonlinearProblemObject", "JFNKSolverObject", "VectorTypeObject", }, /* outputs: */ { - "RawDoubleObject", + "VectorTypeObject", }, /* params: */ { {}, // defl min max; defl min; defl }, /* category: */ { From f72937a3e42182c6d919f25ffd241d3fb51357cf Mon Sep 17 00:00:00 2001 From: Ma Pengfei Date: Sat, 26 Feb 2022 18:22:50 +0800 Subject: [PATCH 3/6] todo : use shared_ptr to wrap vector........ --- nonlinear.zsg | 158 ++++++++++++++---------- projects/Functional/NonlinearSolver.cpp | 14 ++- 2 files changed, 103 insertions(+), 69 deletions(-) diff --git a/nonlinear.zsg b/nonlinear.zsg index db67cc904f..938062617a 100644 --- a/nonlinear.zsg +++ b/nonlinear.zsg @@ -18,8 +18,8 @@ }, "params": {}, "uipos": [ - -300.5532735956456, - 541.4565757534112 + 54.810852248948095, + 837.3037041317889 ], "options": [] }, @@ -27,8 +27,8 @@ "name": "FuncBegin", "inputs": { "extraArgs": [ - null, - null, + "5225ba97-MakeDict", + "dict", null ], "SRC": [ @@ -39,8 +39,8 @@ }, "params": {}, "uipos": [ - -580.6840591609928, - 699.03674837498 + -234.28952791099368, + 994.883876753358 ], "options": [] }, @@ -65,8 +65,8 @@ }, "params": {}, "uipos": [ - -72.94536569365582, - 746.071924254377 + 328.76166555634313, + 1031.4545256057277 ], "options": [] }, @@ -86,28 +86,8 @@ }, "params": {}, "uipos": [ - 179.60070939166815, - 815.3415488277561 - ], - "options": [ - "ONCE" - ] - }, - "86a3b179-MakeRawDoubleObject": { - "name": "MakeRawDoubleObject", - "inputs": { - "SRC": [ - null, - null, - null - ] - }, - "params": { - "value": 2 - }, - "uipos": [ - -570.3010005230933, - 1253.7639680913087 + 580.1117946957213, + 1099.528204233161 ], "options": [ "ONCE" @@ -129,10 +109,12 @@ }, "params": {}, "uipos": [ - -133.5929648241207, - 994.9246231155782 + 586.5346745339856, + 1293.4626298723344 ], - "options": [] + "options": [ + "ONCE" + ] }, "f1a83977-NumericInt": { "name": "NumericInt", @@ -147,8 +129,8 @@ "value": 2 }, "uipos": [ - -557.2361809045226, - 943.3165829145727 + -855.1575280329002, + 1422.442427509166 ], "options": [] }, @@ -156,7 +138,7 @@ "name": "ConvertRawdataToVecotor", "inputs": { "RawDoubleObject": [ - "86a3b179-MakeRawDoubleObject", + "c28fe426-MakeRawDoubleObject", "RawDoubleObject", null ], @@ -168,18 +150,18 @@ }, "params": {}, "uipos": [ - -147.6381909547739, - 1229.4472361809046 + 452.29688083576525, + 1617.3822023971202 ], "options": [ - "VIEW" + "ONCE" ] }, "7c6673db-PrintVectorTypeObject": { "name": "PrintVectorTypeObject", "inputs": { "VectorTypeObject": [ - "deb3c8a6-SolveNonlinearProblem", + "eb17d827-SolveNonlinearProblem", "VectorTypeObject", null ], @@ -191,14 +173,37 @@ }, "params": {}, "uipos": [ - 979.8994974874373, - 891.0552763819096 + 1423.4646368455442, + 1425.1946344900164 ], "options": [ "VIEW" ] }, - "deb3c8a6-SolveNonlinearProblem": { + "c28fe426-MakeRawDoubleObject": { + "name": "MakeRawDoubleObject", + "inputs": { + "num": [ + "f1a83977-NumericInt", + "value", + null + ], + "SRC": [ + null, + null, + null + ] + }, + "params": {}, + "uipos": [ + 33.95365287162164, + 1575.5092905405374 + ], + "options": [ + "ONCE" + ] + }, + "eb17d827-SolveNonlinearProblem": { "name": "SolveNonlinearProblem", "inputs": { "NonlinearProblemObject": [ @@ -211,11 +216,6 @@ "JFNKSolverObject", null ], - "RawDoubleObject": [ - null, - null, - null - ], "VectorTypeObject": [ "8bb99697-ConvertRawdataToVecotor", "VectorTypeObject", @@ -229,17 +229,49 @@ }, "params": {}, "uipos": [ - 645.427135678392, - 833.56783919598 + 1042.7340582770244, + 1327.6494932432395 ], "options": [] + }, + "5225ba97-MakeDict": { + "name": "MakeDict", + "inputs": { + "SRC": [ + null, + null, + null + ], + "obj1": [ + null, + null, + null + ], + "num": [ + "f1a83977-NumericInt", + "value", + null + ] + }, + "params": { + "_KEYS": "num\nobj1" + }, + "uipos": [ + -497.64432010135005, + 972.7525337837817 + ], + "options": [], + "socket_keys": [ + "num", + "obj1" + ] } }, "view_rect": { - "x": -433.0653015729266, - "y": 684.1846227882302, - "width": 1560.0, - "height": 707.9999999999985 + "x": -1151.5443495670536, + "y": 643.9201353021823, + "width": 3046.875, + "height": 1382.8124999999966 } } }, @@ -2812,6 +2844,11 @@ }, "MakeRawDoubleObject": { "inputs": [ + [ + "", + "num", + "" + ], [ "", "SRC", @@ -2830,13 +2867,7 @@ "" ] ], - "params": [ - [ - "int", - "value", - "0" - ] - ], + "params": [], "categories": [ "Zentricle" ] @@ -6594,11 +6625,6 @@ "JFNKSolverObject", "" ], - [ - "", - "RawDoubleObject", - "" - ], [ "", "VectorTypeObject", diff --git a/projects/Functional/NonlinearSolver.cpp b/projects/Functional/NonlinearSolver.cpp index b47c507fb2..cd202be5c6 100644 --- a/projects/Functional/NonlinearSolver.cpp +++ b/projects/Functional/NonlinearSolver.cpp @@ -73,7 +73,9 @@ struct MakeRawDoubleObject : zeno::INode { virtual void apply() override { auto raw_double_array = std::make_shared(); - auto num = get_param("value"); + // auto num = get_param("value"); + auto num = get_input("num")->get(); + auto data = (double*)malloc(num*sizeof(double)); std::fill(data, data+num, 1.0); @@ -86,10 +88,11 @@ struct MakeRawDoubleObject : zeno::INode { ZENDEFNODE(MakeRawDoubleObject, { /* inputs: */ { + "num", }, /* outputs: */ { "RawDoubleObject", }, /* params: */ { - {"int", "value", "0"}, // defl min max; defl min; defl + // defl min max; defl min; defl }, /* category: */ { "Zentricle", }}); @@ -105,7 +108,10 @@ struct NonlinearProblemObject : zeno::IObject, NonlinearProblem auto rets = std::make_shared(); CHECK_F(x.size() == r.size(), "Wrong size."); - + + // TODO : x should be a pointer!!! + // use std::shared_ptr x; instead of const VectorType& x + // to avoid variable constructions and destructions. args->lut["x"] = x; // 封装 rets->lut = function->call(args->lut); // 调用 r = zeno::safe_any_cast(rets->lut["r"]); // 解封 @@ -121,6 +127,8 @@ struct CalculateResidual : zeno::INode { auto rets = std::make_shared(); auto x = zeno::safe_any_cast(args->lut.at("x")); + // auto r = zeno::safe_any_cast(args->lut.at("x")); + VectorType r; r.resize(x.size()); CHECK_F(x.size() == r.size(), "Wrong size."); From 07434293a327b6b34bc409b1682cab5424031231 Mon Sep 17 00:00:00 2001 From: Ma Pengfei Date: Sun, 27 Feb 2022 13:35:28 +0800 Subject: [PATCH 4/6] use shared_ptr in dict. --- .../Algebra/AlgebraSolver/BiCGSTAB.h | 77 +++++++++-------- .../Algebra/AlgebraSolver/LinearProblem.h | 3 +- .../Algebra/AlgebraSolver/LinearSolver.h | 9 +- .../Algebra/AlgebraSolver/NewtonSolver.h | 33 +++---- .../Algebra/AlgebraSolver/NonlinearProblem.h | 86 ++++++++----------- .../Algebra/AlgebraSolver/NonlinearSolver.h | 5 +- .../Algebra/AlgebraSolver/StdVector.h | 2 +- projects/Functional/NonlinearSolver.cpp | 40 ++++----- 8 files changed, 125 insertions(+), 130 deletions(-) diff --git a/projects/Functional/Algebra/AlgebraSolver/BiCGSTAB.h b/projects/Functional/Algebra/AlgebraSolver/BiCGSTAB.h index 2206843c66..71b88fdf63 100644 --- a/projects/Functional/Algebra/AlgebraSolver/BiCGSTAB.h +++ b/projects/Functional/Algebra/AlgebraSolver/BiCGSTAB.h @@ -39,21 +39,30 @@ class BiCGSTAB : public LinearSolver { private: double rho, alpha, omega, beta; - VectorType r, v, s, t, p, r0, temp; + std::shared_ptr r, v, s, t, p, r0, temp; public: - // Initial. Do not allocate memory inside the for loop for efficiency. + // Allocate memory. virtual void Initialize(size_t n) final { - // Set the size of intermediate variables. - r.resize(n); - r0.resize(n); - v.resize(n); - s.resize(n); - t.resize(n); - p.resize(n); - temp.resize(n); + this->_problem_size = n; + + r = std::make_shared(); + r0 = std::make_shared(); + v = std::make_shared(); + s = std::make_shared(); + t = std::make_shared(); + p = std::make_shared(); + temp = std::make_shared(); + + r->resize(n); + r0->resize(n); + v->resize(n); + s->resize(n); + t->resize(n); + p->resize(n); + temp->resize(n); } @@ -67,24 +76,24 @@ class BiCGSTAB : public LinearSolver // NOTE : We use 2-norm of relative_error as return instead of relative error. virtual std::pair> Solve( std::shared_ptr> linear_problem, - VectorType &x0, - const VectorType &b) final { - - // TODO : check if the sizes of rhs and linear_problem match the size of ref. + std::shared_ptr x0, + std::shared_ptr b) final { + + CHECK_F(r->size() == x0->size() && x0->size() == b->size(), "Wrong size!"); // r0 = b - Ax linear_problem->form(x0,temp); // temp = Ax - r0.axpy(-1.0,temp,b); // r0 = -1.0*temp + b; - r = r0; // r = r0 - p = r0; // p = r0 + r0->axpy(-1.0,*temp,*b); // r0 = -1.0*temp + b; + *r = *r0; // r = r0 + *p = *r0; // p = r0 int iter = 0; - double norm_b = std::sqrt(b.inner(b)); + double norm_b = std::sqrt(b->inner(*b)); if(norm_b == 0) { - x0 = 0.0; + *x0 = 0.0; return std::make_pair(true, std::make_pair(0.0, iter)); } - double relative_error = std::sqrt(r.inner(r))/norm_b; + double relative_error = std::sqrt(r->inner(*r))/norm_b; double delta, gamma; @@ -94,42 +103,42 @@ class BiCGSTAB : public LinearSolver LOG_F(INFO, "BiCGSTAB solver(start) : iteration %d.", iter); linear_problem->form(p,v); // v = Ap - rho = r.inner(r0); // rho = (r,r0) - alpha = rho/v.inner(r0); // alpha = rho/(v,r0) + rho = r->inner(*r0); // rho = (r,r0) + alpha = rho/v->inner(*r0); // alpha = rho/(v,r0) if (!std::isnormal(alpha)) return std::make_pair(true, std::make_pair(relative_error,iter)); - s.axpy(-alpha,v,r); // s = r - apha * v; + s->axpy(-alpha,*v,*r); // s = r - apha * v; linear_problem->form(s,t); // t = As - gamma = t.inner(t); - delta = t.inner(s); + gamma = t->inner(*t); + delta = t->inner(*s); omega = delta / gamma; // omega = (t,s)/(t,t) if (!std::isnormal(omega)) return std::make_pair(true, std::make_pair(relative_error,iter)); // x = x + alpha*p + omega*s - temp.axpy(omega,s,x0); // temp = x + omega*s - x0.axpy(alpha,p,temp); // x = temp + alpha*p + temp->axpy(omega,*s,*x0); // temp = x + omega*s + x0->axpy(alpha,*p,*temp); // x = temp + alpha*p // r = s - omega*t; - temp = r; // r_j - r.axpy(-omega, t, s); // r_{j+1} + *temp = *r; // r_j + r->axpy(-omega, *t, *s); // r_{j+1} - relative_error = std::sqrt(r.inner(r))/norm_b; + relative_error = std::sqrt(r->inner(*r))/norm_b; LOG_F(INFO, "BiCGSTAB solver(end) : iteration %d, relative_error norm : %.12lf.", iter, relative_error); if (relative_error < LinearSolver::get_tolerance()) return std::make_pair(true, std::make_pair(relative_error,iter+1)); - beta = r.inner(r0)/temp.inner(r0)*alpha/omega; // (r,r0)/(temp,r0)*alpha/omega + beta = r->inner(*r0)/temp->inner(*r0)*alpha/omega; // (r,r0)/(temp,r0)*alpha/omega if (!std::isnormal(beta)) return std::make_pair(true, std::make_pair(relative_error,iter+1)); - temp.axpy(-omega, v, p); // p = r + beta*(p-omega*v); - p.axpy(beta,temp, r); + temp->axpy(-omega, *v, *p); // p = r + beta*(p-omega*v); + p->axpy(beta, *temp, *r); iter++; } return std::make_pair(false, std::make_pair(relative_error,iter)); - } + } BiCGSTAB(size_t n){ Initialize(n); diff --git a/projects/Functional/Algebra/AlgebraSolver/LinearProblem.h b/projects/Functional/Algebra/AlgebraSolver/LinearProblem.h index 7c494755df..2b57357a4c 100644 --- a/projects/Functional/Algebra/AlgebraSolver/LinearProblem.h +++ b/projects/Functional/Algebra/AlgebraSolver/LinearProblem.h @@ -12,6 +12,7 @@ #ifndef _LINEARPROBLEM_H_ #define _LINEARPROBLEM_H_ #include +#include template class LinearProblem @@ -30,7 +31,7 @@ class LinearProblem // Function called by matrix free linear solver. // must be supplied by the user. - virtual void form(const VectorType& x, VectorType& r) = 0; + virtual void form(std::shared_ptr x, std::shared_ptr Ax) = 0; }; diff --git a/projects/Functional/Algebra/AlgebraSolver/LinearSolver.h b/projects/Functional/Algebra/AlgebraSolver/LinearSolver.h index 2e314cc772..534d2985bd 100644 --- a/projects/Functional/Algebra/AlgebraSolver/LinearSolver.h +++ b/projects/Functional/Algebra/AlgebraSolver/LinearSolver.h @@ -20,9 +20,12 @@ template class LinearSolver { -private: +protected: /* data */ + size_t _problem_size; + public: + size_t problem_size() const { return _problem_size; } // TODO : How to use initialize inside constructor. LinearSolver() @@ -61,8 +64,8 @@ class LinearSolver */ virtual std::pair> Solve( std::shared_ptr> linear_problem, - VectorType &x0, - const VectorType &b) = 0; + std::shared_ptr x0, + std::shared_ptr b) = 0; private: int max_linear_iteration = 1000; diff --git a/projects/Functional/Algebra/AlgebraSolver/NewtonSolver.h b/projects/Functional/Algebra/AlgebraSolver/NewtonSolver.h index cac4f692e1..c88c56ab32 100644 --- a/projects/Functional/Algebra/AlgebraSolver/NewtonSolver.h +++ b/projects/Functional/Algebra/AlgebraSolver/NewtonSolver.h @@ -41,25 +41,26 @@ class NewtonSolver : public NonlinearSolver // TODO : param b is useless here. virtual std::pair> Solve( std::shared_ptr> nonlinear_problem, - VectorType &x0, - const VectorType &b) final { + std::shared_ptr x0, + std::shared_ptr b) final { + + auto linear_solver = this->get_linear_solver(); + CHECK_F(linear_solver->problem_size() == x0->size() && b->size() == x0->size(), "Wrong size!"); - // NOTE : it will not the number of calls of cudaMalloc for GpuVector. - // Therefore, it is not very expensive. - nonlinear_problem->resize(x0.size()); + // NOTE : It will allocate memory for nonlinear_problem. For efficiency, it will not reallocate memory when VectorType = GpuVector if vectors exist. + nonlinear_problem->resize(x0->size()); - auto& xk = nonlinear_problem->get_xk(); - auto& rhs = nonlinear_problem->get_rhs(); - auto& delta_x = nonlinear_problem->get_delta_x(); - auto& residual = nonlinear_problem->get_residual(); + auto xk = nonlinear_problem->get_xk(); + auto rhs = nonlinear_problem->get_rhs(); + auto delta_x = nonlinear_problem->get_delta_x(); + auto residual = nonlinear_problem->get_residual(); - auto linear_solver = this->get_linear_solver(); - double norm_b = std::sqrt(b.inner(b)); + double norm_b = std::sqrt(b->inner(*b)); double norm_r; int iter = 0; // NOTE : Set initial value for newton iteration. - xk = x0; + *xk = *x0; do{ iter++; @@ -67,7 +68,7 @@ class NewtonSolver : public NonlinearSolver nonlinear_problem->Residual(xk, rhs); // NOTE : initial guess for the linear solver. - delta_x = rhs; + *delta_x = *rhs; // solve delta_x, which is (-dx) actually. auto linear_result = linear_solver->Solve(nonlinear_problem, delta_x, rhs); @@ -77,12 +78,12 @@ class NewtonSolver : public NonlinearSolver // LOG_F(WARNING, "xk: %.8lf, %.8lf", xk[0], xk[1]); // update xk = xk - (-dx); - xk.axpy(-1.0,delta_x, xk); - x0 = xk; + xk->axpy(-1.0,*delta_x, *xk); + *x0 = *xk; // calculate the residual. nonlinear_problem->Residual(residual); - norm_r = std::sqrt(residual.inner(residual)); + norm_r = std::sqrt(residual->inner(*residual)); LOG_F(WARNING, "norm_r : %lf", norm_r); // TODO : assuming norm_e = norm_r/norm_b diff --git a/projects/Functional/Algebra/AlgebraSolver/NonlinearProblem.h b/projects/Functional/Algebra/AlgebraSolver/NonlinearProblem.h index c1951e8513..c5c719d101 100644 --- a/projects/Functional/Algebra/AlgebraSolver/NonlinearProblem.h +++ b/projects/Functional/Algebra/AlgebraSolver/NonlinearProblem.h @@ -20,35 +20,42 @@ template class NonlinearProblem : public LinearProblem { private: + + std::shared_ptr xk; + std::shared_ptr rhs; + std::shared_ptr delta_x; + std::shared_ptr residual; - VectorType xk; - VectorType rhs; - VectorType delta_x; - VectorType residual; - - VectorType r1; - VectorType r2; - VectorType xk_plus_epsilon_x; + std::shared_ptr r1; + std::shared_ptr r2; + std::shared_ptr xk_plus_epsilon_x; // used to calculate Jx = (h(x+epsilon*x0)-h(x))/epsilon double epsilon = 1e-6; public: - VectorType& get_xk() { return xk; } - VectorType& get_rhs() { return rhs; } - VectorType& get_delta_x() { return delta_x; } - VectorType& get_residual() { return residual; } + std::shared_ptr get_xk() { return xk; } + std::shared_ptr get_rhs() { return rhs; } + std::shared_ptr get_delta_x() { return delta_x; } + std::shared_ptr get_residual() { return residual; } void resize(size_t __size){ - - xk.resize(__size); - rhs.resize(__size); - delta_x.resize(__size); - residual.resize(__size); - r1.resize(__size); - r2.resize(__size); - xk_plus_epsilon_x.resize(__size); + xk = std::make_shared(); + rhs = std::make_shared(); + delta_x = std::make_shared(); + residual = std::make_shared(); + r1 = std::make_shared(); + r2 = std::make_shared(); + xk_plus_epsilon_x = std::make_shared(); + + xk->resize(__size); + rhs->resize(__size); + delta_x->resize(__size); + residual->resize(__size); + r1->resize(__size); + r2->resize(__size); + xk_plus_epsilon_x->resize(__size); } @@ -59,44 +66,25 @@ class NonlinearProblem : public LinearProblem } - virtual void form(const VectorType& x, VectorType& r) final { - - // Timer timer("function residual in class MyProblem"); - LOG_F(WARNING, "Nonlinear form.\n\n"); - - CHECK_F(x.size() == r.size(), "Wrong size."); - CHECK_F(xk.size() == r.size(), "Wrong size."); - + virtual void form(std::shared_ptr x, std::shared_ptr r) final { + // Timer timer("function residual in class MyProblem"); + // LOG_F(WARNING, "Nonlinear form.\n\n"); - // z = a*x + y - // z.axpy(a,x,y); - // x_k + \epsilon x = xk + epsilon * x; - xk_plus_epsilon_x.axpy(epsilon, x, xk); + CHECK_F(x->size() == r->size() && xk->size() == r->size(), "Wrong size."); + + xk_plus_epsilon_x->axpy(epsilon, *x, *xk); - // calculate residuals Residual(xk, r1); Residual(xk_plus_epsilon_x, r2); - // (h(x0+e*x)-h(x0))/e - // r = (r2 - r1)/epsilon - // divided into two steps : - // r = r2 - r1 - // r = r*(1/epsilon) = r + (-1+1/epsilon)*r - - // z = a*x + y - // z.axpy(a,x,y); - r.axpy(-1,r1,r2); - - // x = a*x + y - // x.axpy(a,y); - r.axpy(1.0/epsilon-1,r); + r->axpy(-1,*r1,*r2); + r->axpy(1.0/epsilon-1,*r); } + virtual void Residual(std::shared_ptr x, std::shared_ptr r) = 0; - virtual void Residual(const VectorType& x, VectorType& r) = 0; - - void Residual(VectorType& r){ + void Residual(std::shared_ptr r) { Residual(xk, r); } diff --git a/projects/Functional/Algebra/AlgebraSolver/NonlinearSolver.h b/projects/Functional/Algebra/AlgebraSolver/NonlinearSolver.h index ff671b9777..91b2240b4c 100644 --- a/projects/Functional/Algebra/AlgebraSolver/NonlinearSolver.h +++ b/projects/Functional/Algebra/AlgebraSolver/NonlinearSolver.h @@ -81,11 +81,10 @@ class NonlinearSolver virtual std::string method() const = 0; - virtual std::pair> Solve( std::shared_ptr> nonlinear_problem, - VectorType &x0, - const VectorType &b) = 0; + std::shared_ptr x0, + std::shared_ptr b) = 0; // 类声明外部的说明符无效 virtual ~NonlinearSolver(){ diff --git a/projects/Functional/Algebra/AlgebraSolver/StdVector.h b/projects/Functional/Algebra/AlgebraSolver/StdVector.h index 6534583730..4e486294f5 100644 --- a/projects/Functional/Algebra/AlgebraSolver/StdVector.h +++ b/projects/Functional/Algebra/AlgebraSolver/StdVector.h @@ -22,12 +22,12 @@ class StdVector : public GenericVector using GenericVector::_dim; - std::vector _data; virtual void init() override { _data.resize(this->size()); } public: + std::vector _data; virtual bool use_gpu() const override { return false;} diff --git a/projects/Functional/NonlinearSolver.cpp b/projects/Functional/NonlinearSolver.cpp index cd202be5c6..ff51346847 100644 --- a/projects/Functional/NonlinearSolver.cpp +++ b/projects/Functional/NonlinearSolver.cpp @@ -102,23 +102,23 @@ struct NonlinearProblemObject : zeno::IObject, NonlinearProblem { std::shared_ptr function = nullptr; - virtual void Residual(const VectorType& x, VectorType& r){ + virtual void Residual(std::shared_ptr x, std::shared_ptr r) final { auto args = std::make_shared(); auto rets = std::make_shared(); - CHECK_F(x.size() == r.size(), "Wrong size."); + CHECK_F(x->size() == r->size(), "Wrong size."); - // TODO : x should be a pointer!!! - // use std::shared_ptr x; instead of const VectorType& x - // to avoid variable constructions and destructions. + // x is a samrt pointer. args->lut["x"] = x; // 封装 + args->lut["r"] = r; // 封装 rets->lut = function->call(args->lut); // 调用 - r = zeno::safe_any_cast(rets->lut["r"]); // 解封 + + // NOTE : It is unneccessary. + r = zeno::safe_any_cast>(rets->lut["r"]); // 解封 } }; - // User defined struct CalculateResidual : zeno::INode { virtual void apply() override { @@ -126,19 +126,16 @@ struct CalculateResidual : zeno::INode { auto args = get_input("args"); auto rets = std::make_shared(); - auto x = zeno::safe_any_cast(args->lut.at("x")); - // auto r = zeno::safe_any_cast(args->lut.at("x")); - - VectorType r; - r.resize(x.size()); - CHECK_F(x.size() == r.size(), "Wrong size."); + auto x = zeno::safe_any_cast>(args->lut.at("x")); + auto r = zeno::safe_any_cast>(args->lut.at("r")); - auto _x = flatten(x).data; - auto _r = flatten(r).data; + auto _x = flatten(*x).data; + auto _r = flatten(*r).data; _r[0] = std::exp(2.0*_x[0])/2.0 - _x[1]; _r[1] = _x[0]*_x[0] + _x[1]*_x[1]-1.0; + // NOTE : It is unneccessary. rets->lut["r"] = r; set_output("rets", std::move(rets)); } @@ -241,17 +238,14 @@ struct SolveNonlinearProblem : zeno::INode { auto jfnk_solver = get_input("JFNKSolverObject"); auto nlp = get_input("NonlinearProblemObject"); - auto x_vector_object = get_input("VectorTypeObject"); + auto x_vector_ptr = x_vector_object->vector; + auto b_vector_ptr = std::make_shared(); + b_vector_ptr->resize(x_vector_ptr->size()); - /////////////////////////////////////////////////////////////////////////////////////////// - // NOTE : we solve F(x) = b here. But b is not necessary. b is to be removed. - VectorType bb; - bb.resize(x_vector_ptr->size()); - ///////////////////////////////////////////////////////////////////////////////////////////// - - auto nonlinear_result = jfnk_solver->ns->Solve(nlp, *x_vector_ptr, bb); + // NOTE : we solve F(x) = b here. But b is not necessary. b is to be removed. + auto nonlinear_result = jfnk_solver->ns->Solve(nlp, x_vector_ptr, b_vector_ptr); if ( nonlinear_result.first) { From 09258900896ffc4bfda333a9a80ad2f601bfa49a Mon Sep 17 00:00:00 2001 From: Ma Pengfei Date: Sun, 27 Feb 2022 14:01:14 +0800 Subject: [PATCH 5/6] fix cmklists --- CMakeLists.txt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 37320a1efb..804eeeb75c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -83,9 +83,9 @@ if (ZENO_BUILD_EXTENSIONS) add_subdirectory(projects) endif() -if (ZENO_BUILD_TESTS) - add_subdirectory(tests) -endif() +# if (ZENO_BUILD_TESTS) +# add_subdirectory(tests) +# endif() #if (ZENO_BUILD_LAUNCHER) # add_subdirectory(scripts/launcher) From de50b19af462519231dd14733d1174df1eb537dd Mon Sep 17 00:00:00 2001 From: Ma Pengfei Date: Tue, 1 Mar 2022 15:45:09 +0800 Subject: [PATCH 6/6] do not use rets --- nonlinear.zsg | 648 ++++++++++++++++++++++-- projects/Functional/NonlinearSolver.cpp | 11 +- zeno/zeno/nodes/FuncNodes.cpp | 4 +- 3 files changed, 618 insertions(+), 45 deletions(-) diff --git a/nonlinear.zsg b/nonlinear.zsg index 938062617a..347eacdca6 100644 --- a/nonlinear.zsg +++ b/nonlinear.zsg @@ -11,8 +11,8 @@ null ], "SRC": [ - null, - null, + "e83899a7-FuncBegin", + "DST", null ] }, @@ -48,8 +48,8 @@ "name": "FuncEnd", "inputs": { "rets": [ - "1b9b8c32-CalculateResidual", - "rets", + null, + null, null ], "FUNC": [ @@ -58,8 +58,8 @@ null ], "SRC": [ - null, - null, + "48783b41-PrintMessage", + "DST", null ] }, @@ -89,9 +89,7 @@ 580.1117946957213, 1099.528204233161 ], - "options": [ - "ONCE" - ] + "options": [] }, "a0a524e0-MakeJFNKSolver": { "name": "MakeJFNKSolver", @@ -242,15 +240,15 @@ null, null ], - "obj1": [ - null, - null, - null - ], "num": [ "f1a83977-NumericInt", "value", null + ], + "obj1": [ + null, + null, + null ] }, "params": { @@ -265,13 +263,31 @@ "num", "obj1" ] + }, + "48783b41-PrintMessage": { + "name": "PrintMessage", + "inputs": { + "SRC": [ + "1b9b8c32-CalculateResidual", + "DST", + null + ] + }, + "params": { + "message": "hello" + }, + "uipos": [ + 52.33604863221868, + 1176.8724620060755 + ], + "options": [] } }, "view_rect": { - "x": -1151.5443495670536, - "y": 643.9201353021823, - "width": 3046.875, - "height": 1382.8124999999966 + "x": -315.41730386373575, + "y": 753.4341730699091, + "width": 1248.0, + "height": 566.3999999999984 } } }, @@ -1134,6 +1150,82 @@ "dict" ] }, + "DynamicNumber": { + "inputs": [ + [ + "", + "frame", + "" + ], + [ + "", + "SRC", + "" + ] + ], + "outputs": [ + [ + "", + "x", + "" + ], + [ + "", + "y", + "" + ], + [ + "", + "z", + "" + ], + [ + "", + "w", + "" + ], + [ + "", + "DST", + "" + ] + ], + "params": [ + [ + "enum 100 10 1 0.1 0.01 0.001", + "speed", + "1" + ], + [ + "floatslider", + "x", + "0" + ], + [ + "floatslider", + "y", + "0" + ], + [ + "floatslider", + "z", + "0" + ], + [ + "floatslider", + "w", + "0" + ], + [ + "enum clamp zero cycle", + "type", + "clamp" + ] + ], + "categories": [ + "numeric" + ] + }, "EmptyDict": { "inputs": [ [ @@ -2646,6 +2738,81 @@ "list" ] }, + "MakeLocalSys": { + "inputs": [ + [ + "vec3f", + "front", + "1,0,0" + ], + [ + "vec3f", + "up", + "0,1,0" + ], + [ + "vec3f", + "right", + "0,0,1" + ], + [ + "", + "SRC", + "" + ] + ], + "outputs": [ + [ + "", + "LocalSys", + "" + ], + [ + "", + "DST", + "" + ] + ], + "params": [], + "categories": [ + "primitive" + ] + }, + "MakeMaterial": { + "inputs": [ + [ + "string", + "vert", + "" + ], + [ + "string", + "frag", + "" + ], + [ + "", + "SRC", + "" + ] + ], + "outputs": [ + [ + "material", + "mtl", + "" + ], + [ + "", + "DST", + "" + ] + ], + "params": [], + "categories": [ + "material" + ] + }, "MakeMultilineString": { "inputs": [ [ @@ -3813,6 +3980,51 @@ "zenofx" ] }, + "OrthonormalBase": { + "inputs": [ + [ + "vec3f", + "normal", + "0,0,1" + ], + [ + "vec3f", + "tangent", + "0,1,0" + ], + [ + "", + "SRC", + "" + ] + ], + "outputs": [ + [ + "vec3f", + "normal", + "" + ], + [ + "vec3f", + "tangent", + "" + ], + [ + "vec3f", + "bitangent", + "" + ], + [ + "", + "DST", + "" + ] + ], + "params": [], + "categories": [ + "math" + ] + }, "PackNumericIntVec2": { "inputs": [ [ @@ -4480,6 +4692,16 @@ "limitMax", "1" ], + [ + "float", + "midPoint", + "0.5" + ], + [ + "float", + "biasDir", + "0.5" + ], [ "", "SRC", @@ -4498,7 +4720,13 @@ "" ] ], - "params": [], + "params": [ + [ + "bool", + "useOrigin", + "0" + ] + ], "categories": [ "primitive" ] @@ -5346,6 +5574,11 @@ "sealEnd", "1" ], + [ + "bool", + "lineSort", + "1" + ], [ "", "SRC", @@ -5369,16 +5602,11 @@ "primitive" ] }, - "PrimitiveMerge": { + "PrimitiveLineSort": { "inputs": [ [ - "", - "listPrim", - "" - ], - [ - "", - "dst", + "PrimitiveObject", + "prim", "" ], [ @@ -5389,7 +5617,7 @@ ], "outputs": [ [ - "", + "PrimitiveObject", "prim", "" ], @@ -5399,28 +5627,165 @@ "" ] ], - "params": [], + "params": [ + [ + "bool", + "reversed", + "0" + ] + ], "categories": [ "primitive" ] }, - "PrimitiveMix": { + "PrimitiveLinearMap": { "inputs": [ [ - "", - "primA", + "PrimitiveObject", + "prim", "" ], [ - "", - "primB", + "PrimitiveObject", + "refPrim", "" ], [ - "", - "primOut", - "" - ], + "string", + "attrNameSrc", + "pos" + ], + [ + "string", + "attrNameDst", + "pos" + ], + [ + "string", + "refAttrNameSrc", + "pos" + ], + [ + "string", + "refAttrNameDst", + "pos" + ], + [ + "float", + "limitMin", + "0" + ], + [ + "float", + "limitMax", + "1" + ], + [ + "enum X Y Z", + "axisSrc", + "X" + ], + [ + "enum X Y Z", + "axisDst", + "Y" + ], + [ + "enum X Y Z", + "refAxisSrc", + "X" + ], + [ + "enum X Y Z", + "refAxisDst", + "Y" + ], + [ + "", + "SRC", + "" + ] + ], + "outputs": [ + [ + "PrimitiveObject", + "prim", + "" + ], + [ + "", + "DST", + "" + ] + ], + "params": [ + [ + "bool", + "autoMinMax", + "1" + ], + [ + "bool", + "autoSort", + "1" + ] + ], + "categories": [ + "primitive" + ] + }, + "PrimitiveMerge": { + "inputs": [ + [ + "", + "listPrim", + "" + ], + [ + "", + "dst", + "" + ], + [ + "", + "SRC", + "" + ] + ], + "outputs": [ + [ + "", + "prim", + "" + ], + [ + "", + "DST", + "" + ] + ], + "params": [], + "categories": [ + "primitive" + ] + }, + "PrimitiveMix": { + "inputs": [ + [ + "", + "primA", + "" + ], + [ + "", + "primB", + "" + ], + [ + "", + "primOut", + "" + ], [ "", "coef", @@ -5699,6 +6064,51 @@ "primitive" ] }, + "PrimitiveScale": { + "inputs": [ + [ + "PrimitiveObject", + "prim", + "" + ], + [ + "vec3f", + "origin", + "0,0,0" + ], + [ + "vec3f", + "axis", + "0,1,0" + ], + [ + "float", + "scale", + "0" + ], + [ + "", + "SRC", + "" + ] + ], + "outputs": [ + [ + "PrimitiveObject", + "prim", + "" + ], + [ + "", + "DST", + "" + ] + ], + "params": [], + "categories": [ + "primitive" + ] + }, "PrimitiveSetAttrValue": { "inputs": [ [ @@ -6049,7 +6459,7 @@ "0,1,0" ], [ - "vec3f", + "", "tangent", "" ], @@ -6254,6 +6664,76 @@ "Zentricle" ] }, + "ProjectAndNormalize": { + "inputs": [ + [ + "vec3f", + "vec", + "" + ], + [ + "enum XY YX YZ ZY ZX XZ", + "plane", + "XY" + ], + [ + "float", + "directionScale", + "1" + ], + [ + "float", + "lengthScale", + "1" + ], + [ + "float", + "heightScale", + "1" + ], + [ + "float", + "heightOffset", + "0" + ], + [ + "", + "SRC", + "" + ] + ], + "outputs": [ + [ + "vec3f", + "direction", + "" + ], + [ + "float", + "length", + "" + ], + [ + "float", + "height", + "" + ], + [ + "float", + "phase", + "" + ], + [ + "", + "DST", + "" + ] + ], + "params": [], + "categories": [ + "math" + ] + }, "RandomParticles": { "inputs": [ [ @@ -6386,6 +6866,41 @@ "primitive" ] }, + "ReadObjPrimitiveDict": { + "inputs": [ + [ + "readpath", + "path", + "" + ], + [ + "", + "SRC", + "" + ] + ], + "outputs": [ + [ + "", + "prim", + "" + ], + [ + "", + "dict", + "" + ], + [ + "", + "DST", + "" + ] + ], + "params": [], + "categories": [ + "primitive" + ] + }, "ReadParticles": { "inputs": [ [ @@ -6537,6 +7052,41 @@ "frame" ] }, + "SetMaterial": { + "inputs": [ + [ + "primitive", + "prim", + "" + ], + [ + "material", + "mtl", + "" + ], + [ + "", + "SRC", + "" + ] + ], + "outputs": [ + [ + "primitive", + "prim", + "" + ], + [ + "", + "DST", + "" + ] + ], + "params": [], + "categories": [ + "material" + ] + }, "SetRandomSeed": { "inputs": [ [ @@ -7132,6 +7682,11 @@ "translation", "0,0,0" ], + [ + "vec3f", + "offset", + "0,0,0" + ], [ "vec3f", "eulerXYZ", @@ -7147,6 +7702,16 @@ "scaling", "1,1,1" ], + [ + "", + "Matrix", + "" + ], + [ + "", + "local", + "" + ], [ "", "SRC", @@ -7159,6 +7724,11 @@ "outPrim", "" ], + [ + "", + "Matrix", + "" + ], [ "", "DST", diff --git a/projects/Functional/NonlinearSolver.cpp b/projects/Functional/NonlinearSolver.cpp index ff51346847..6328ef1c65 100644 --- a/projects/Functional/NonlinearSolver.cpp +++ b/projects/Functional/NonlinearSolver.cpp @@ -112,10 +112,10 @@ struct NonlinearProblemObject : zeno::IObject, NonlinearProblem // x is a samrt pointer. args->lut["x"] = x; // 封装 args->lut["r"] = r; // 封装 - rets->lut = function->call(args->lut); // 调用 + function->call(args->lut); // 调用 // NOTE : It is unneccessary. - r = zeno::safe_any_cast>(rets->lut["r"]); // 解封 + // r = zeno::safe_any_cast>(rets->lut["r"]); // 解封 } }; @@ -135,9 +135,11 @@ struct CalculateResidual : zeno::INode { _r[0] = std::exp(2.0*_x[0])/2.0 - _x[1]; _r[1] = _x[0]*_x[0] + _x[1]*_x[1]-1.0; + std::cout << "residual \n" << std::endl; + // NOTE : It is unneccessary. - rets->lut["r"] = r; - set_output("rets", std::move(rets)); + // rets->lut["r"] = r; + // set_output("rets", std::move(rets)); } }; @@ -246,6 +248,7 @@ struct SolveNonlinearProblem : zeno::INode { // NOTE : we solve F(x) = b here. But b is not necessary. b is to be removed. auto nonlinear_result = jfnk_solver->ns->Solve(nlp, x_vector_ptr, b_vector_ptr); + nonlinear_result = jfnk_solver->ns->Solve(nlp, x_vector_ptr, b_vector_ptr); if ( nonlinear_result.first) { diff --git a/zeno/zeno/nodes/FuncNodes.cpp b/zeno/zeno/nodes/FuncNodes.cpp index b2a1a43c52..3055cd36d1 100644 --- a/zeno/zeno/nodes/FuncNodes.cpp +++ b/zeno/zeno/nodes/FuncNodes.cpp @@ -51,8 +51,8 @@ struct FuncEnd : zeno::ContextManagedNode { func->func = [this, fore] (zeno::FunctionObject::DictType const &args) { if (fore) fore->update_arguments(args); push_context(); - m_ctx->visited.clear(); - // zeno::INode::preApply(); + // m_ctx->visited.clear(); + zeno::INode::preApply(); pop_context(); zeno::FunctionObject::DictType rets{}; if (requireInput("rets")) {