Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:17:43

0001 import datetime as dt
0002 import os
0003 import random
0004 
0005 nfns = 4  # Functions per class
0006 nargs = 4  # Arguments per function
0007 
0008 
0009 def generate_dummy_code_pybind11(nclasses=10):
0010     decl = ""
0011     bindings = ""
0012 
0013     for cl in range(nclasses):
0014         decl += f"class cl{cl:03};\n"
0015     decl += "\n"
0016 
0017     for cl in range(nclasses):
0018         decl += f"class {cl:03} {{\n"
0019         decl += "public:\n"
0020         bindings += f'    py::class_<cl{cl:03}>(m, "cl{cl:03}")\n'
0021         for fn in range(nfns):
0022             ret = random.randint(0, nclasses - 1)
0023             params = [random.randint(0, nclasses - 1) for i in range(nargs)]
0024             decl += f"    cl{ret:03} *fn_{fn:03}("
0025             decl += ", ".join(f"cl{p:03} *" for p in params)
0026             decl += ");\n"
0027             bindings += f'        .def("fn_{fn:03}", &cl{cl:03}::fn_{fn:03})\n'
0028         decl += "};\n\n"
0029         bindings += "        ;\n"
0030 
0031     result = "#include <pybind11/pybind11.h>\n\n"
0032     result += "namespace py = pybind11;\n\n"
0033     result += decl + "\n"
0034     result += "PYBIND11_MODULE(example, m) {\n"
0035     result += bindings
0036     result += "}"
0037     return result
0038 
0039 
0040 def generate_dummy_code_boost(nclasses=10):
0041     decl = ""
0042     bindings = ""
0043 
0044     for cl in range(nclasses):
0045         decl += f"class cl{cl:03};\n"
0046     decl += "\n"
0047 
0048     for cl in range(nclasses):
0049         decl += "class cl%03i {\n" % cl
0050         decl += "public:\n"
0051         bindings += f'    py::class_<cl{cl:03}>("cl{cl:03}")\n'
0052         for fn in range(nfns):
0053             ret = random.randint(0, nclasses - 1)
0054             params = [random.randint(0, nclasses - 1) for i in range(nargs)]
0055             decl += f"    cl{ret:03} *fn_{fn:03}("
0056             decl += ", ".join(f"cl{p:03} *" for p in params)
0057             decl += ");\n"
0058             bindings += f'        .def("fn_{fn:03}", &cl{cl:03}::fn_{fn:03}, py::return_value_policy<py::manage_new_object>())\n'
0059         decl += "};\n\n"
0060         bindings += "        ;\n"
0061 
0062     result = "#include <boost/python.hpp>\n\n"
0063     result += "namespace py = boost::python;\n\n"
0064     result += decl + "\n"
0065     result += "BOOST_PYTHON_MODULE(example) {\n"
0066     result += bindings
0067     result += "}"
0068     return result
0069 
0070 
0071 for codegen in [generate_dummy_code_pybind11, generate_dummy_code_boost]:
0072     print("{")
0073     for i in range(0, 10):
0074         nclasses = 2**i
0075         with open("test.cpp", "w") as f:
0076             f.write(codegen(nclasses))
0077         n1 = dt.datetime.now()
0078         os.system(
0079             "g++ -Os -shared -rdynamic -undefined dynamic_lookup "
0080             "-fvisibility=hidden -std=c++14 test.cpp -I include "
0081             "-I /System/Library/Frameworks/Python.framework/Headers -o test.so"
0082         )
0083         n2 = dt.datetime.now()
0084         elapsed = (n2 - n1).total_seconds()
0085         size = os.stat("test.so").st_size
0086         print("   {%i, %f, %i}," % (nclasses * nfns, elapsed, size))
0087     print("}")