From 0719d69a8ac4101ebb8a90b54c7f0d8ecded335f Mon Sep 17 00:00:00 2001 From: Joseph Capriotti Date: Tue, 14 Mar 2023 14:41:42 -0700 Subject: [PATCH 1/4] switch to try_emplace --- discretize/_extensions/tree.cpp | 39 +++++++++++++-------------------- setup.py | 1 + 2 files changed, 16 insertions(+), 24 deletions(-) diff --git a/discretize/_extensions/tree.cpp b/discretize/_extensions/tree.cpp index c68c0fafb..023de540c 100644 --- a/discretize/_extensions/tree.cpp +++ b/discretize/_extensions/tree.cpp @@ -137,15 +137,12 @@ Face::Face(Node& p1, Node& p2, Node& p3, Node& p4){ Node * set_default_node(node_map_t& nodes, int_t x, int_t y, int_t z, double *xs, double *ys, double *zs){ int_t key = key_func(x, y, z); - Node * point; - if(nodes.count(key) == 0){ - point = new Node(x, y, z, xs, ys, zs); - nodes[key] = point; + auto [it, inserted] = nodes.try_emplace(key, nullptr); + if(inserted){ + // construct a new item at the emplaced location + it->second = new Node(x, y, z, xs, ys, zs); } - else{ - point = nodes[key]; - } - return point; + return it->second; } Edge * set_default_edge(edge_map_t& edges, Node& p1, Node& p2){ @@ -153,15 +150,12 @@ Edge * set_default_edge(edge_map_t& edges, Node& p1, Node& p2){ int_t yC = (p1.location_ind[1]+p2.location_ind[1])/2; int_t zC = (p1.location_ind[2]+p2.location_ind[2])/2; int_t key = key_func(xC, yC, zC); - Edge * edge; - if(edges.count(key) == 0){ - edge = new Edge(p1, p2); - edges[key] = edge; - } - else{ - edge = edges[key]; + auto [it, inserted] = edges.try_emplace(key, nullptr); + if(inserted){ + // construct a new item at the emplaced location + it->second = new Edge(p1, p2); } - return edge; + return it->second; }; Face * set_default_face(face_map_t& faces, Node& p1, Node& p2, Node& p3, Node& p4){ @@ -170,15 +164,12 @@ Face * set_default_face(face_map_t& faces, Node& p1, Node& p2, Node& p3, Node& p y = (p1.location_ind[1]+p2.location_ind[1]+p3.location_ind[1]+p4.location_ind[1])/4; z = (p1.location_ind[2]+p2.location_ind[2]+p3.location_ind[2]+p4.location_ind[2])/4; key = key_func(x, y, z); - Face * face; - if(faces.count(key) == 0){ - face = new Face(p1, p2, p3, p4); - faces[key] = face; - } - else{ - face = faces[key]; + auto [it, inserted] = faces.try_emplace(key, nullptr); + if(inserted){ + // construct a new item at the emplaced location + it->second = new Face(p1, p2, p3, p4); } - return face; + return it->second; } Cell::Cell(Node *pts[8], int_t ndim, int_t maxlevel){ diff --git a/setup.py b/setup.py index d50231d5c..3dc7a3989 100644 --- a/setup.py +++ b/setup.py @@ -93,6 +93,7 @@ "discretize._extensions.tree_ext", ["discretize/_extensions/tree_ext.pyx", "discretize/_extensions/tree.cpp"], include_dirs=[np.get_include()], + extra_compile_args=["/std:c++17", "-std=c++17"], **ext_kwargs ), Extension( From 18e97993f6cdd97466a40df5b4e0c3f101933ed0 Mon Sep 17 00:00:00 2001 From: Joseph Capriotti Date: Tue, 14 Mar 2023 19:28:51 -0700 Subject: [PATCH 2/4] slightly change command for unix sytems --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 3dc7a3989..622e18924 100644 --- a/setup.py +++ b/setup.py @@ -93,7 +93,7 @@ "discretize._extensions.tree_ext", ["discretize/_extensions/tree_ext.pyx", "discretize/_extensions/tree.cpp"], include_dirs=[np.get_include()], - extra_compile_args=["/std:c++17", "-std=c++17"], + extra_compile_args=["-std:c++17", "-std=c++17"], **ext_kwargs ), Extension( From 56873988e94184c3670c028a8f35f4b61abee58b Mon Sep 17 00:00:00 2001 From: Joseph Capriotti Date: Wed, 15 Mar 2023 14:33:13 -0700 Subject: [PATCH 3/4] Add conditional standard flags --- setup.py | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 622e18924..fba97acca 100644 --- a/setup.py +++ b/setup.py @@ -75,6 +75,7 @@ metadata["install_requires"] = install_requires else: from setuptools.extension import Extension + from setuptools.command.build_ext import build_ext from Cython.Build import cythonize import numpy as np @@ -93,7 +94,7 @@ "discretize._extensions.tree_ext", ["discretize/_extensions/tree_ext.pyx", "discretize/_extensions/tree.cpp"], include_dirs=[np.get_include()], - extra_compile_args=["-std:c++17", "-std=c++17"], + #extra_compile_args=["-std:c++17", "-std=c++17"], **ext_kwargs ), Extension( @@ -104,6 +105,23 @@ ), ] + class build_ext_cpp_standard(build_ext): + # add compiler specific standard argument specifier + def build_extension(self, ext): + # This module requires c++17 standard + if ext.name == "discretize._extensions.tree_ext": + comp_type = self.compiler.compiler_type + if comp_type == 'msvc': + std_arg = "\std:c++17" + elif comp_type == 'bcpp': + raise Exception('Must use cpp compiler that support C++17 standard.') + else: + std_arg = "-std=c++17" + ext.extra_compile_args = [std_arg,] + super().build_extension(ext) + + metadata["ext_modules"] = cythonize(extensions) + metadata["cmdclass"] = {"build_ext":build_ext_cpp_standard} setup(**metadata) From 2c05f80d73634bedbd89ca3c360cbcfdaeaf4ffa Mon Sep 17 00:00:00 2001 From: Joseph Capriotti Date: Wed, 15 Mar 2023 17:06:08 -0700 Subject: [PATCH 4/4] Fix windows build --- setup.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/setup.py b/setup.py index fba97acca..3884d2f16 100644 --- a/setup.py +++ b/setup.py @@ -94,7 +94,6 @@ "discretize._extensions.tree_ext", ["discretize/_extensions/tree_ext.pyx", "discretize/_extensions/tree.cpp"], include_dirs=[np.get_include()], - #extra_compile_args=["-std:c++17", "-std=c++17"], **ext_kwargs ), Extension( @@ -111,17 +110,20 @@ def build_extension(self, ext): # This module requires c++17 standard if ext.name == "discretize._extensions.tree_ext": comp_type = self.compiler.compiler_type - if comp_type == 'msvc': - std_arg = "\std:c++17" - elif comp_type == 'bcpp': - raise Exception('Must use cpp compiler that support C++17 standard.') + if comp_type == "msvc": + std_arg = "/std:c++17" + elif comp_type == "bcpp": + raise Exception( + "Must use cpp compiler that support C++17 standard." + ) else: std_arg = "-std=c++17" - ext.extra_compile_args = [std_arg,] + ext.extra_compile_args = [ + std_arg, + ] super().build_extension(ext) - metadata["ext_modules"] = cythonize(extensions) - metadata["cmdclass"] = {"build_ext":build_ext_cpp_standard} + metadata["cmdclass"] = {"build_ext": build_ext_cpp_standard} setup(**metadata)