Check for appropriate C compiler
This commit is contained in:
134
week4/day4.ipynb
134
week4/day4.ipynb
@@ -453,6 +453,110 @@
|
||||
"ui.launch(inbrowser=True)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "e42286bc-085c-45dc-b101-234308e58269",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import platform\n",
|
||||
"\n",
|
||||
"VISUAL_STUDIO_2022_TOOLS = \"C:\\\\Program Files\\\\Microsoft Visual Studio\\\\2022\\\\Community\\\\Common7\\Tools\\\\VsDevCmd.bat\"\n",
|
||||
"VISUAL_STUDIO_2019_TOOLS = \"C:\\\\Program Files (x86)\\\\Microsoft Visual Studio\\\\2019\\\\BuildTools\\\\Common7\\\\Tools\\\\VsDevCmd.bat\"\n",
|
||||
"\n",
|
||||
"simple_cpp = \"\"\"\n",
|
||||
"#include <iostream>\n",
|
||||
"\n",
|
||||
"int main() {\n",
|
||||
" std::cout << \"Hello\";\n",
|
||||
" return 0;\n",
|
||||
"}\n",
|
||||
"\"\"\"\n",
|
||||
"\n",
|
||||
"def run_cmd(command_to_run):\n",
|
||||
" try:\n",
|
||||
" run_result = subprocess.run(command_to_run, check=True, text=True, capture_output=True)\n",
|
||||
" return run_result.stdout\n",
|
||||
" except:\n",
|
||||
" return \"\"\n",
|
||||
"\n",
|
||||
"def c_compiler_cmd(filename_base):\n",
|
||||
"\n",
|
||||
" with open(\"simple.cpp\", \"w\") as f:\n",
|
||||
" f.write(simple_cpp)\n",
|
||||
"\n",
|
||||
" \n",
|
||||
" my_platform = platform.system()\n",
|
||||
" my_compiler = []\n",
|
||||
" \n",
|
||||
" \n",
|
||||
" if my_platform == \"Windows\":\n",
|
||||
" if os.path.isfile(VISUAL_STUDIO_2022_TOOLS):\n",
|
||||
" if os.path.isfile(\"./simple.exe\"):\n",
|
||||
" os.remove(\"./simple.exe\")\n",
|
||||
" compile_cmd = [\"cmd\", \"/c\", VISUAL_STUDIO_2022_TOOLS, \"&\", \"cl\", \"simple.cpp\"]\n",
|
||||
" if run_cmd(compile_cmd):\n",
|
||||
" if run_cmd([\"./simple\"]) == \"Hello\":\n",
|
||||
" my_compiler = [\"Windows\", \"Visual Studio 2022\", [\"cmd\", \"/c\", VISUAL_STUDIO_2022_TOOLS, \"&\", \"cl\", f\"{filename_base}.cpp\"]]\n",
|
||||
" \n",
|
||||
" if not my_compiler:\n",
|
||||
" if os.path.isfile(VISUAL_STUDIO_2019_TOOLS):\n",
|
||||
" if os.path.isfile(\"./simple.exe\"):\n",
|
||||
" os.remove(\"./simple.exe\")\n",
|
||||
" compile_cmd = [\"cmd\", \"/c\", VISUAL_STUDIO_2019_TOOLS, \"&\", \"cl\", \"simple.cpp\"]\n",
|
||||
" if run_cmd(compile_cmd):\n",
|
||||
" if run_cmd([\"./simple\"]) == \"Hello\":\n",
|
||||
" my_compiler = [\"Windows\", \"Visual Studio 2019\", [\"cmd\", \"/c\", VISUAL_STUDIO_2019_TOOLS, \"&\", \"cl\", f\"{filename_base}.cpp\"]]\n",
|
||||
"\n",
|
||||
" if not my_compiler:\n",
|
||||
" if os.path.isfile(\"./simple.exe\"):\n",
|
||||
" os.remove(\"./simple.exe\")\n",
|
||||
" compile_cmd = [\"cmd\", \"/c\", \"gcc\", \"simple.cpp\"]\n",
|
||||
" if run_cmd(compile_cmd):\n",
|
||||
" print(\"here\")\n",
|
||||
" if run_cmd([\"./simple\"]) == \"Hello\":\n",
|
||||
" my_compiler = [\"Windows\", \"GCC\", [\"cmd\", \"/c\", \"gcc\", f\"{filename_base}.cpp\"]]\n",
|
||||
"\n",
|
||||
" if not my_compiler:\n",
|
||||
" my_compiler=[my_platform, \"Unknown\", []]\n",
|
||||
" \n",
|
||||
" elif my_platform == \"Linux\":\n",
|
||||
" if os.path.isfile(\"./simple\"):\n",
|
||||
" os.remove(\"./simple\")\n",
|
||||
" compile_cmd = [\"gcc\", \"simple.cpp\"]\n",
|
||||
" if run_cmd(compile_cmd):\n",
|
||||
" if run_cmd([\"./simple\"]) == \"Hello\":\n",
|
||||
" my_compiler = [\"Linux\", \"GCC\", [\"gcc\", f\"{filename_base}.cpp\", \"-o\", f\"{filename_base}\"]]\n",
|
||||
"\n",
|
||||
" if not my_compiler:\n",
|
||||
" if os.path.isfile(\"./simple\"):\n",
|
||||
" os.remove(\"./simple\")\n",
|
||||
" compile_cmd = [\"clang\", \"simple.cpp\", \"-o\", \"simple\"]\n",
|
||||
" if run_cmd(compile_cmd):\n",
|
||||
" if run_cmd([\"./simple\"]) == \"Hello\":\n",
|
||||
" my_compiler = [\"Linux\", \"CLang\", [\"clang\", f\"{filename_base}.cpp\", \"-o\", f\"{filename_base}\"]]\n",
|
||||
" \n",
|
||||
" if not my_compiler:\n",
|
||||
" my_compiler=[my_platform, \"Unknown\", []]\n",
|
||||
"\n",
|
||||
" elif my_platform == \"Darwin\":\n",
|
||||
" if os.path.isfile(\"./simple\"):\n",
|
||||
" os.remove(\"./simple\")\n",
|
||||
" compile_cmd = [\"gcc\", \"simple.cpp\"]\n",
|
||||
" if run_cmd(compile_cmd):\n",
|
||||
" if run_cmd([\"./simple\"]) == \"Hello\":\n",
|
||||
" my_compiler = [\"Linux\", \"CLang\", [\"clang++\", \"-Ofast\", \"-std=c++17\", \"-march=armv8.5-a\", \"-mtune=apple-m1\", \"-mcpu=apple-m1\", \"-o\", f\"{filename_base}\", f\"{filename_base}.cpp\"]]\n",
|
||||
"\n",
|
||||
" if not my_compiler:\n",
|
||||
" my_compiler=[my_platform, \"Unknown\", []]\n",
|
||||
"\n",
|
||||
" if my_compiler:\n",
|
||||
" return my_compiler\n",
|
||||
" else:\n",
|
||||
" return [\"Unknown\", \"Unknown\", []]\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
@@ -479,9 +583,9 @@
|
||||
"source": [
|
||||
"def execute_cpp(code):\n",
|
||||
" write_output(code)\n",
|
||||
" print(f\"DEBUG. execute_cpp. compiler_cmd[2]=\\\"{compiler_cmd[2]}\\\"\")\n",
|
||||
" try:\n",
|
||||
" compile_cmd = [\"clang++\", \"-Ofast\", \"-std=c++17\", \"-march=armv8.5-a\", \"-mtune=apple-m1\", \"-mcpu=apple-m1\", \"-o\", \"optimized\", \"optimized.cpp\"]\n",
|
||||
" compile_result = subprocess.run(compile_cmd, check=True, text=True, capture_output=True)\n",
|
||||
" compile_result = subprocess.run(compiler_cmd[2], check=True, text=True, capture_output=True)\n",
|
||||
" run_cmd = [\"./optimized\"]\n",
|
||||
" run_result = subprocess.run(run_cmd, check=True, text=True, capture_output=True)\n",
|
||||
" return run_result.stdout\n",
|
||||
@@ -641,6 +745,22 @@
|
||||
" yield stream_so_far "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "4ba311ec-c16a-4fe0-946b-4b940704cf65",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def select_sample_program(sample_program):\n",
|
||||
" if sample_program==\"pi\":\n",
|
||||
" return pi\n",
|
||||
" elif sample_program==\"python_hard\":\n",
|
||||
" return python_hard\n",
|
||||
" else:\n",
|
||||
" return \"Type your Python program here\""
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
@@ -648,13 +768,20 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"compiler_cmd = c_compiler_cmd(\"optimized\")\n",
|
||||
"\n",
|
||||
"with gr.Blocks(css=css) as ui:\n",
|
||||
" gr.Markdown(\"## Convert code from Python to C++\")\n",
|
||||
" with gr.Row():\n",
|
||||
" python = gr.Textbox(label=\"Python code:\", value=python_hard, lines=10)\n",
|
||||
" cpp = gr.Textbox(label=\"C++ code:\", lines=10)\n",
|
||||
" with gr.Row():\n",
|
||||
" with gr.Column():\n",
|
||||
" sample_program = gr.Radio([\"pi\", \"python_hard\"], label=\"Sample program\", value=\"python_hard\")\n",
|
||||
" model = gr.Dropdown([\"GPT\", \"Claude\", \"CodeQwen\"], label=\"Select model\", value=\"GPT\")\n",
|
||||
" with gr.Column():\n",
|
||||
" architecture = gr.Radio([compiler_cmd[0]], label=\"Architecture\", interactive=False, value=compiler_cmd[0])\n",
|
||||
" compiler = gr.Radio([compiler_cmd[1]], label=\"Compiler\", interactive=False, value=compiler_cmd[1])\n",
|
||||
" with gr.Row():\n",
|
||||
" convert = gr.Button(\"Convert code\")\n",
|
||||
" with gr.Row():\n",
|
||||
@@ -664,6 +791,7 @@
|
||||
" python_out = gr.TextArea(label=\"Python result:\", elem_classes=[\"python\"])\n",
|
||||
" cpp_out = gr.TextArea(label=\"C++ result:\", elem_classes=[\"cpp\"])\n",
|
||||
"\n",
|
||||
" sample_program.change(select_sample_program, inputs=[sample_program], outputs=[python])\n",
|
||||
" convert.click(optimize, inputs=[python, model], outputs=[cpp])\n",
|
||||
" python_run.click(execute_python, inputs=[python], outputs=[python_out])\n",
|
||||
" cpp_run.click(execute_cpp, inputs=[cpp], outputs=[cpp_out])\n",
|
||||
@@ -696,7 +824,7 @@
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.11.11"
|
||||
"version": "3.11.10"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
|
||||
BIN
week4/optimized
BIN
week4/optimized
Binary file not shown.
Reference in New Issue
Block a user