-
Notifications
You must be signed in to change notification settings - Fork 1
ENH: Add RPC support #18
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
Open
HaoZeke
wants to merge
27
commits into
TheochemUI:main
Choose a base branch
from
HaoZeke:doRPC
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
8fa52dc
CI: Minor typo
HaoZeke fba4e20
BLD: Add capnp
HaoZeke dc50e91
BIN: Update lockfile
HaoZeke 40188d0
ENH: Use a meson format hook
HaoZeke 996f4f1
BLD: Format with meson-format and add rpc option
HaoZeke efc9ca0
ENH: First pass at an interface
HaoZeke e6e4e8a
MAINT: Ignore generated files
HaoZeke 39eb279
BLD: Add some temporarily dependencies for testing
HaoZeke 091fc98
BUG: Do not shadow existing classes
HaoZeke d3a3fcb
BIN: Update lockfile
HaoZeke 10a534e
ENH: Add a basic client and server
HaoZeke e136f0e
BLD: Setup basic build, bump cpp version
HaoZeke 4e420bf
MAINT: Simplify and lint
HaoZeke ba9bcd2
BUG: Ensure correct lifetime for server forces
HaoZeke 58e4598
MAINT: Minor lint and less variables
HaoZeke 88bd069
MAINT: Fix linting issues
HaoZeke 0e3d99c
BLD: More lint
HaoZeke 1dd36ad
ENH: Restructure into ForceInput
HaoZeke 5280115
BUG: Fixup access pattern
HaoZeke 2846942
MAINT: Only match meson build files
HaoZeke 57c430b
MAINT: Please linter
HaoZeke db8c33d
DOC: Fix typo
HaoZeke 5db7dd2
MAINT: Cleanup unused
HaoZeke 610c8ba
MAINT: Slightly nicer
HaoZeke 843d980
BLD: Rework to generate files
HaoZeke 6440e38
ENH: Generate and use a capnp adapter
HaoZeke 7bb041f
ENH: Be generic over potentials
HaoZeke File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,7 +8,7 @@ on: | |
| - main | ||
|
|
||
| jobs: | ||
| buildmamba: | ||
| buildpixi: | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - uses: actions/checkout@v3 | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,9 @@ | ||
| cuh2 = library('cuh2', | ||
| 'CuH2Pot.cc', | ||
| 'cuh2Utils.cc', | ||
| dependencies: _deps, | ||
| cpp_args: _args, | ||
| include_directories: _incdirs, | ||
| install : true) | ||
| cuh2 = library( | ||
| 'cuh2', | ||
| 'CuH2Pot.cc', | ||
| 'cuh2Utils.cc', | ||
| dependencies: _deps, | ||
| cpp_args: _args, | ||
| include_directories: _incdirs, | ||
| install: true, | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,8 @@ | ||
| lennard_jones = library('lennard_jones', | ||
| 'LJPot.cc', | ||
| dependencies: _deps, | ||
| cpp_args: _args, | ||
| include_directories: ['../../'], | ||
| install : true) | ||
| lennard_jones = library( | ||
| 'lennard_jones', | ||
| 'LJPot.cc', | ||
| dependencies: _deps, | ||
| cpp_args: _args, | ||
| include_directories: ['../../'], | ||
| install: true, | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| @0xbd1f89fa17369103; | ||
|
|
||
| # Design kanged from eOn v4 C-style structs | ||
| # TODO(rg): Should be more object oriented | ||
|
|
||
| struct ForceInput { | ||
| natm @0 :Int32; # TODO(rg): Do we really need this.. | ||
| pos @1 :List(Float64); | ||
| atmnrs @2 :List(Int32); | ||
| box @3 :List(Float64); | ||
| } | ||
|
|
||
| struct PotentialResult { | ||
| energy @0: Float64; | ||
| forces @1: List(Float64); | ||
| } | ||
|
|
||
| interface Potential { | ||
| calculate @0 (fip :ForceInput) | ||
| -> (result :PotentialResult); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| # Find dependencies and tools | ||
| capnp_rpc_dep = dependency('capnp-rpc') | ||
| capnpc = find_program('capnpc') | ||
|
|
||
| output_dir = meson.current_source_dir() / 'gen' | ||
|
|
||
| gen_rpc = custom_target( | ||
| 'gen_rpc', | ||
| input: 'Potentials.capnp', | ||
| output: ['Potentials.capnp.c++', 'Potentials.capnp.h'], | ||
| command: [ | ||
| 'sh', | ||
| '-c', | ||
| 'mkdir -p @0@ && @1@ -o c++:@0@ --src-prefix=@2@ @3@'.format( | ||
| output_dir, | ||
| capnpc.path(), | ||
| meson.current_source_dir(), | ||
| '@INPUT@', | ||
| ), | ||
| ], | ||
| ) | ||
|
|
||
| gen_inc = include_directories('gen') | ||
| _incdirs += gen_inc | ||
|
|
||
| ptlrpc = library( | ||
| 'ptlrpc', | ||
| gen_rpc, | ||
| dependencies: _deps + capnp_rpc_dep, | ||
| cpp_args: _args, | ||
| include_directories: _incdirs, | ||
| install: true, | ||
| ) | ||
| server = executable( | ||
| 'potserv', | ||
| ['server.cpp', gen_rpc], | ||
| link_with: [ptlrpc, _linkto], | ||
| dependencies: _deps + capnp_rpc_dep, | ||
| cpp_args: _args, | ||
| include_directories: _incdirs, | ||
| install: true, | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| #!/usr/bin/env python3 | ||
|
|
||
| import argparse | ||
| import asyncio | ||
|
|
||
| import capnp | ||
| import Potentials_capnp | ||
|
|
||
|
|
||
| def parse_args(): | ||
| parser = argparse.ArgumentParser( | ||
| usage="Connects to the Calculator server at the given address and does some RPCs" | ||
| ) | ||
| parser.add_argument("host", help="HOST:PORT") | ||
|
|
||
| return parser.parse_args() | ||
|
|
||
|
|
||
| async def main(connection): | ||
| client = capnp.TwoPartyClient(connection) | ||
| calculator = client.bootstrap().cast_as(Potentials_capnp.Potential) | ||
|
|
||
| print("Connection made ", end="") | ||
|
|
||
| # Create the atomic positions, atom types, and box matrix | ||
| positions = [ | ||
| 0.63940268750835, | ||
| 0.90484742551374, | ||
| 6.97516498544584, # Cu | ||
| 3.19652040936288, | ||
| 0.90417430354811, | ||
| 6.97547796369474, # Cu | ||
| 8.98363230369760, | ||
| 9.94703496017833, | ||
| 7.83556854923689, # H | ||
| 7.64080177576300, | ||
| 9.94703114803832, | ||
| 7.83556986121272, # H | ||
| ] | ||
|
|
||
| atom_types = [29, 29, 1, 1] # Atomic types for Cu and H | ||
|
|
||
| box_matrix = [15.0, 0.0, 0.0, 0.0, 20.0, 0.0, 0.0, 0.0, 30.0] | ||
|
|
||
| # Create the ForceInput message | ||
| force_input = Potentials_capnp.ForceInput.new_message() | ||
| force_input.natm = len(atom_types) # Number of atoms | ||
|
|
||
| # Set positions | ||
| pos_list = force_input.init("pos", len(positions)) | ||
| for idx, pos in enumerate(positions): | ||
| pos_list[idx] = pos | ||
|
|
||
| # Set atom types | ||
| atom_list = force_input.init("atmnrs", len(atom_types)) | ||
| for idx, atom in enumerate(atom_types): | ||
| atom_list[idx] = atom | ||
|
|
||
| # Set the box matrix | ||
| box_list = force_input.init("box", len(box_matrix)) | ||
| for idx, val in enumerate(box_matrix): | ||
| box_list[idx] = val | ||
|
|
||
| # Call the CuH2Pot.calculate method with ForceInput | ||
| response = calculator.calculate(force_input) | ||
|
|
||
| # Await and print the result | ||
| result = await response | ||
| result_dict = result.to_dict() | ||
| print("Energy:", result_dict["result"]["energy"]) | ||
| print("Forces:", result_dict["result"]["forces"]) | ||
|
|
||
|
|
||
| async def cmd_main(host): | ||
| host, port = host.split(":") | ||
| await main(await capnp.AsyncIoStream.create_connection(host=host, port=port)) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| asyncio.run(capnp.run(cmd_main(parse_args().host))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| // MIT License | ||
| // Copyright 2023--present Rohit Goswami <HaoZeke> | ||
| #include <capnp/ez-rpc.h> | ||
| #include <capnp/message.h> | ||
|
|
||
| #include "rgpot/CuH2/CuH2Pot.hpp" | ||
| #include "rgpot/LennardJones/LJPot.hpp" | ||
| #include "rgpot/types/AtomMatrix.hpp" | ||
| #include "rgpot/types/adapters/capnp/capnp_adapter.hpp" | ||
|
|
||
| class GenericPotImpl final : public Potential::Server { | ||
| private: | ||
| std::unique_ptr<rgpot::Potential> m_potential; | ||
|
|
||
| public: | ||
| // The constructor takes ownership of a potential object | ||
| GenericPotImpl(std::unique_ptr<rgpot::Potential> pot) | ||
| : m_potential(std::move(pot)) {} | ||
|
|
||
| kj::Promise<void> calculate(CalculateContext context) override { | ||
| // --- Use the adapter to convert FROM Cap'n Proto --- | ||
| auto fip = context.getParams().getFip(); | ||
| const auto numAtoms = fip.getNatm(); | ||
|
|
||
| rgpot::types::AtomMatrix nativePositions = | ||
| rgpot::types::adapt::capnp::convertPositionsFromCapnp(fip.getPos(), | ||
| numAtoms); | ||
| std::vector<int> nativeAtomTypes = | ||
| rgpot::types::adapt::capnp::convertAtomNumbersFromCapnp( | ||
| fip.getAtmnrs()); | ||
| std::array<std::array<double, 3>, 3> nativeBoxMatrix = | ||
| rgpot::types::adapt::capnp::convertBoxMatrixFromCapnp(fip.getBox()); | ||
|
|
||
| // --- Call the potential via the polymorphic interface --- | ||
| // This is the key change: no hardcoded type! | ||
| auto [energy, forces] = | ||
| (*m_potential)(nativePositions, nativeAtomTypes, nativeBoxMatrix); | ||
|
|
||
| // --- Use the adapter to populate TO Cap'n Proto --- | ||
| auto result = context.getResults(); | ||
| auto pres = result.initResult(); | ||
| pres.setEnergy(energy); | ||
|
|
||
| auto forcesList = pres.initForces(numAtoms * 3); | ||
| rgpot::types::adapt::capnp::populateForcesToCapnp(forcesList, forces); | ||
|
|
||
| return kj::READY_NOW; | ||
| } | ||
| }; | ||
|
|
||
| // Main server setup now acts as a factory | ||
| int main(int argc, char *argv[]) { | ||
| if (argc < 3) { | ||
| std::cerr << "Usage: " << argv[0] << " <port> <PotentialType>" << std::endl; | ||
| std::cerr << " Available PotentialTypes: CuH2" | ||
| << std::endl; // Add more as you implement them | ||
| return 1; | ||
| } | ||
|
|
||
| int port = 12345; | ||
| try { | ||
| port = std::stoi(argv[1]); | ||
| } catch (const std::exception &e) { | ||
| std::cerr << "Invalid port argument '" << argv[1] | ||
| << "'. Using default 12345." << std::endl; | ||
| } | ||
|
|
||
| std::string pot_type = argv[2]; | ||
| std::unique_ptr<rgpot::Potential> potential_to_use; | ||
|
|
||
| if (pot_type == "CuH2") { | ||
| std::cout << "Loading CuH2 potential..." << std::endl; | ||
| potential_to_use = std::make_unique<rgpot::CuH2Pot>(); | ||
| } else if (pot_type == "LJ") { | ||
| std::cout << "Loading LJ potential..." << std::endl; | ||
| potential_to_use = std::make_unique<rgpot::LJPot>(); | ||
| } else { | ||
| std::cerr << "Error: Unknown potential type '" << pot_type << "'" | ||
| << std::endl; | ||
| return 1; | ||
| } | ||
|
|
||
| capnp::EzRpcServer server( | ||
| kj::heap<GenericPotImpl>(std::move(potential_to_use)), "localhost", port); | ||
|
|
||
| // Keep the server running indefinitely | ||
| auto &waitScope = server.getWaitScope(); | ||
| std::cout << "Server running on port " << port << " with " << pot_type | ||
| << " potential." << std::endl; | ||
| kj::NEVER_DONE.wait(waitScope); | ||
|
|
||
| return 0; | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.