-
Notifications
You must be signed in to change notification settings - Fork 36
implement sub-interpreters #380
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,216 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"id": "fb74181a-b881-4ae9-9224-495425707762", | ||
"metadata": {}, | ||
"source": [ | ||
"**Default Interpreter of the Kernel**" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"id": "e5375e1a-347b-461e-97ac-1a5bc708c896", | ||
"metadata": { | ||
"vscode": { | ||
"languageId": "c++" | ||
} | ||
}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"201703\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"#include <iostream>\n", | ||
"std::cout << __cplusplus << std::endl;" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "7ec1684f-ce9a-4614-8f7c-8391b68f7eb5", | ||
"metadata": {}, | ||
"source": [ | ||
"**Creating New Sub-Interpreter with `-std=c++23`**\n", | ||
"\n", | ||
"_We give it a name `cpp23` to re-use later_" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 2, | ||
"id": "756696a7-c8db-4302-8794-029664673952", | ||
"metadata": { | ||
"vscode": { | ||
"languageId": "c++" | ||
} | ||
}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"Elapsed time (C++23): 5.97e-06 seconds\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"%%subinterp -Wall -O3 --std=c++23 --name cpp23\n", | ||
"\n", | ||
"#include <iostream>\n", | ||
"#include <vector>\n", | ||
"#include <ranges>\n", | ||
"#include <chrono>\n", | ||
"\n", | ||
"void version() { std::cout << __cplusplus << std::endl; }\n", | ||
"\n", | ||
"template <std::ranges::range R> constexpr auto to_vector(R&& r) { return std::vector<std::decay_t<std::ranges::range_value_t<R>>>{r.begin(), r.end()}; }\n", | ||
"\n", | ||
"void run() {\n", | ||
" std::vector<int> numbers(10'000);\n", | ||
" for (int i = 0; i < 10'000; i++) numbers.push_back(i);\n", | ||
" \n", | ||
" auto start = std::chrono::high_resolution_clock::now();\n", | ||
" std::vector<int> result = to_vector(\n", | ||
" numbers \n", | ||
" | std::views::filter([](int n) { return n % 2 != 0; }) // filter out even numbers\n", | ||
" | std::views::transform([](int n) { return n * 2; })); // multiply remaining by 2\n", | ||
" auto end = std::chrono::high_resolution_clock::now();\n", | ||
" \n", | ||
" std::chrono::duration<double> elapsed = end - start;\n", | ||
" std::cout << \"Elapsed time (C++23): \" << elapsed.count() << \" seconds\\n\";\n", | ||
"}\n", | ||
"run();" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "1a31f103-b92a-44e9-bf42-61709340bdc4", | ||
"metadata": {}, | ||
"source": [ | ||
"**Creating Another Sub-Interpreter with `-std=c++11`**" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 3, | ||
"id": "4f3391f7-0a38-4cd9-af2b-33245a06db84", | ||
"metadata": { | ||
"vscode": { | ||
"languageId": "c++" | ||
} | ||
}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"Elapsed time (C++11): 5.22e-06 seconds\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"%%subinterp -O3 --std=c++11 --name cpp11\n", | ||
"\n", | ||
"#include <iostream>\n", | ||
"#include <vector>\n", | ||
"#include <algorithm>\n", | ||
"#include <chrono>\n", | ||
"\n", | ||
"void version() { std::cout << __cplusplus << std::endl; }\n", | ||
"\n", | ||
"void run() {\n", | ||
" std::vector<int> numbers(10000);\n", | ||
" for (int i = 0; i < 10000; i++) numbers.push_back(i);\n", | ||
" std::vector<int> result;\n", | ||
"\n", | ||
" auto start = std::chrono::high_resolution_clock::now();\n", | ||
" std::copy_if(numbers.begin(), numbers.end(), std::back_inserter(result),\n", | ||
" [](int n) { return n % 2 != 0; }); // filter out even numbers\n", | ||
" std::transform(result.begin(), result.end(), result.begin(),\n", | ||
" [](int n) { return n * 2; }); // multiply remaining by 2\n", | ||
" auto end = std::chrono::high_resolution_clock::now();\n", | ||
" \n", | ||
" std::chrono::duration<double> elapsed = end - start;\n", | ||
" std::cout << \"Elapsed time (C++11): \" << elapsed.count() << \" seconds\\n\";\n", | ||
"}\n", | ||
"run();" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "63cecedc-0e72-43fd-80e4-36a612a8042b", | ||
"metadata": {}, | ||
"source": [ | ||
"**Re-Using the Interpreter Created Previously**" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 4, | ||
"id": "600da197-f902-4cf4-a401-a848e08aa638", | ||
"metadata": { | ||
"vscode": { | ||
"languageId": "c++" | ||
} | ||
}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"202302\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"%%subinterp --use cpp23\n", | ||
"version();" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 5, | ||
"id": "2c2fa915-a6de-4477-b4f8-6c342720c224", | ||
"metadata": { | ||
"vscode": { | ||
"languageId": "c++" | ||
} | ||
}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"201103\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"%%subinterp --use cpp11\n", | ||
"version();" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "C++17", | ||
"language": "cpp", | ||
"name": "xcpp17" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": "text/x-c++src", | ||
"file_extension": ".cpp", | ||
"mimetype": "text/x-c++src", | ||
"name": "C++", | ||
"version": "17" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 5 | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,93 @@ | ||||||
/************************************************************************************ | ||||||
* Copyright (c) 2025, xeus-cpp contributors * | ||||||
* * | ||||||
* Distributed under the terms of the BSD 3-Clause License. * | ||||||
* * | ||||||
* The full license is in the file LICENSE, distributed with this software. * | ||||||
************************************************************************************/ | ||||||
|
||||||
#include "multi_interpreter.hpp" | ||||||
|
||||||
#include <iostream> | ||||||
#include <iterator> | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: included header iostream is not used directly [misc-include-cleaner]
Suggested change
|
||||||
#include <sstream> | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: included header iterator is not used directly [misc-include-cleaner]
Suggested change
|
||||||
#include <string> | ||||||
#include <vector> | ||||||
|
||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: included header vector is not used directly [misc-include-cleaner]
Suggested change
|
||||||
#include "CppInterOp/CppInterOp.h" | ||||||
|
||||||
static std::vector<std::string> split(const std::string& input) | ||||||
{ | ||||||
std::istringstream buffer(input); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: unused local variable 'buffer' of type 'std::istringstream' (aka 'basic_istringstream') [bugprone-unused-local-non-trivial-variable] std::istringstream buffer(input);
^ |
||||||
std::vector<std::string> ret( | ||||||
(std::istream_iterator<std::string>(buffer)), | ||||||
std::istream_iterator<std::string>() | ||||||
); | ||||||
return ret; | ||||||
} | ||||||
|
||||||
static constexpr size_t len(std::string_view n) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: no header providing "size_t" is directly included [misc-include-cleaner] src/xmagics/multi_interpreter.cpp:10: - #include <iostream>
+ #include <cstddef>
+ #include <iostream> There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: no header providing "std::string_view" is directly included [misc-include-cleaner] src/xmagics/multi_interpreter.cpp:14: - #include <vector>
+ #include <string_view>
+ #include <vector> |
||||||
{ | ||||||
return n.size(); | ||||||
} | ||||||
|
||||||
namespace xcpp | ||||||
{ | ||||||
void multi_interpreter::operator()(const std::string& line, const std::string& cell) | ||||||
{ | ||||||
auto Args0 = split(line.substr(len("subinterp"))); | ||||||
|
||||||
Cpp::TInterp_t OldI = Cpp::GetInterpreter(); // we need to restore the old interpreter | ||||||
Cpp::TInterp_t I = nullptr; | ||||||
std::string name; | ||||||
if (Args0[0] == "--use") | ||||||
{ | ||||||
I = interpreters[Args0[1]]; | ||||||
} | ||||||
else | ||||||
{ | ||||||
auto named = std::find(Args0.begin(), Args0.end(), "--name"); | ||||||
if (named != Args0.end()) | ||||||
{ | ||||||
name = *(named + 1); | ||||||
} | ||||||
} | ||||||
|
||||||
std::vector<const char*> Args(I ? 0 : Args0.size()); | ||||||
if (!I) | ||||||
{ | ||||||
for (auto start = Args0.begin(), end = Args0.end(); start != end; start++) | ||||||
{ | ||||||
if (*start == "--name") | ||||||
{ | ||||||
start++; | ||||||
continue; | ||||||
} | ||||||
Args.push_back((*start).c_str()); | ||||||
} | ||||||
} | ||||||
|
||||||
Cpp::BeginStdStreamCapture(Cpp::kStdErr); | ||||||
Cpp::BeginStdStreamCapture(Cpp::kStdOut); | ||||||
if (!I) | ||||||
{ | ||||||
I = Cpp::CreateInterpreter(Args); // TODO: error handling | ||||||
} | ||||||
if (I) | ||||||
{ | ||||||
Cpp::Declare(cell.c_str(), false, I); | ||||||
} | ||||||
std::cout << Cpp::EndStdStreamCapture(); | ||||||
std::cerr << Cpp::EndStdStreamCapture(); | ||||||
|
||||||
if (!name.empty()) | ||||||
{ | ||||||
interpreters[name] = I; // TODO: check if this is redefinition | ||||||
Cpp::ActivateInterpreter(OldI); // restoring old interpreter | ||||||
} | ||||||
else | ||||||
{ | ||||||
Cpp::DeleteInterpreter(I); | ||||||
} | ||||||
} | ||||||
} // namespace xcpp |
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,33 @@ | ||||||||
/************************************************************************************ | ||||||||
* Copyright (c) 2025, xeus-cpp contributors * | ||||||||
* * | ||||||||
* Distributed under the terms of the BSD 3-Clause License. * | ||||||||
* * | ||||||||
* The full license is in the file LICENSE, distributed with this software. * | ||||||||
************************************************************************************/ | ||||||||
|
||||||||
#ifndef XEUS_CPP_MULTI_INTERPRETER_MAGIC_HPP | ||||||||
#define XEUS_CPP_MULTI_INTERPRETER_MAGIC_HPP | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: header guard does not follow preferred style [llvm-header-guard]
Suggested change
src/xmagics/multi_interpreter.hpp:32: - #endif // XEUS_CPP_MULTI_INTERPRETER_MAGIC_HPP
+ #endif // GITHUB_WORKSPACE_SRC_XMAGICS_MULTI_INTERPRETER_HPP |
||||||||
|
||||||||
#include <string> | ||||||||
#include <unordered_map> | ||||||||
|
||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: included header unordered_map is not used directly [misc-include-cleaner]
Suggested change
|
||||||||
#include "CppInterOp/CppInterOp.h" | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: 'CppInterOp/CppInterOp.h' file not found [clang-diagnostic-error] #include "CppInterOp/CppInterOp.h"
^ |
||||||||
#include "xeus-cpp/xmagics.hpp" | ||||||||
|
||||||||
namespace xcpp | ||||||||
{ | ||||||||
class multi_interpreter : public xmagic_cell | ||||||||
{ | ||||||||
public: | ||||||||
|
||||||||
XEUS_CPP_API | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: no header providing "XEUS_CPP_API" is directly included [misc-include-cleaner] src/xmagics/multi_interpreter.hpp:15: - #include "xeus-cpp/xmagics.hpp"
+ #include "xeus-cpp/xeus_cpp_config.hpp"
+ #include "xeus-cpp/xmagics.hpp" |
||||||||
void operator()(const std::string& line, const std::string& cell) override; | ||||||||
|
||||||||
private: | ||||||||
|
||||||||
std::unordered_map<std::string, Cpp::TInterp_t> interpreters; | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: invalid case style for private member 'interpreters' [readability-identifier-naming]
Suggested change
|
||||||||
}; | ||||||||
} // namespace xcpp | ||||||||
|
||||||||
#endif // XEUS_CPP_MULTI_INTERPRETER_MAGIC_HPP |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
warning: included header multi_interpreter.hpp is not used directly [misc-include-cleaner]