Browse Source

Merge pull request #63 from cloudllama/check_os_for_c_compiler

Call the appropriate C++ compiler on Windows, Linux, and Mac
pull/64/head
Ed Donner 5 months ago committed by GitHub
parent
commit
f5a66d264a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 133
      week4/day4.ipynb

133
week4/day4.ipynb

@ -472,7 +472,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 107,
"id": "77f3ab5d-fcfb-4d3f-8728-9cacbf833ea6",
"metadata": {},
"outputs": [],
@ -480,8 +480,7 @@
"def execute_cpp(code):\n",
" write_output(code)\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 +640,117 @@
" 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": 115,
"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 if run_result.stdout else \"SUCCESS\"\n",
" except:\n",
" return \"\"\n",
"\n",
"def c_compiler_cmd(filename_base):\n",
" my_platform = platform.system()\n",
" my_compiler = []\n",
"\n",
" try:\n",
" with open(\"simple.cpp\", \"w\") as f:\n",
" f.write(simple_cpp)\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.exe\"]) == \"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.exe\"]) == \"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",
" my_compiler=[my_platform, \"Unavailable\", []]\n",
" \n",
" elif my_platform == \"Linux\":\n",
" if os.path.isfile(\"./simple\"):\n",
" os.remove(\"./simple\")\n",
" compile_cmd = [\"g++\", \"simple.cpp\", \"-o\", \"simple\"]\n",
" if run_cmd(compile_cmd):\n",
" if run_cmd([\"./simple\"]) == \"Hello\":\n",
" my_compiler = [\"Linux\", \"GCC (g++)\", [\"g++\", 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, \"Unavailable\", []]\n",
" \n",
" elif my_platform == \"Darwin\":\n",
" if os.path.isfile(\"./simple\"):\n",
" os.remove(\"./simple\")\n",
" compile_cmd = [\"clang++\", \"-Ofast\", \"-std=c++17\", \"-march=armv8.5-a\", \"-mtune=apple-m1\", \"-mcpu=apple-m1\", \"-o\", \"simple\", \"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, \"Unavailable\", []]\n",
" except:\n",
" my_compiler=[my_platform, \"Unavailable\", []]\n",
" \n",
" if my_compiler:\n",
" return my_compiler\n",
" else:\n",
" return [\"Unknown\", \"Unavailable\", []]\n"
]
},
{
"cell_type": "code",
"execution_count": null,
@ -648,22 +758,33 @@
"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",
" model = gr.Dropdown([\"GPT\", \"Claude\", \"CodeQwen\"], label=\"Select model\", value=\"GPT\")\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",
" python_run = gr.Button(\"Run Python\")\n",
" cpp_run = gr.Button(\"Run C++\")\n",
" if not compiler_cmd[1] == \"Unavailable\":\n",
" cpp_run = gr.Button(\"Run C++\")\n",
" else:\n",
" cpp_run = gr.Button(\"No compiler to run C++\", interactive=False)\n",
" with gr.Row():\n",
" 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 +817,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.11"
"version": "3.11.10"
}
},
"nbformat": 4,

Loading…
Cancel
Save