From 8095100bee9a6ecf8323a1bdaba3f622e5de98ee Mon Sep 17 00:00:00 2001 From: Eric Harding Date: Fri, 15 Nov 2019 11:22:40 -0500 Subject: [PATCH] p/invoke for synthetic book computation --- src/client/spark_api/Co2/Co2NativeMethods.cs | 41 +++++++++++ src/lib/md/test/Co2ManagedTestHelper.cpp | 70 ++++++++++++++++++- src/lib/md/test/md_test_utils.cpp | 48 +++++++++++++ .../md/test/synthetic_book_merge_tests.cpp | 44 ------------ 4 files changed, 158 insertions(+), 45 deletions(-) diff --git a/src/client/spark_api/Co2/Co2NativeMethods.cs b/src/client/spark_api/Co2/Co2NativeMethods.cs index b2ae76044..9ea47866b 100644 --- a/src/client/spark_api/Co2/Co2NativeMethods.cs +++ b/src/client/spark_api/Co2/Co2NativeMethods.cs @@ -4,6 +4,8 @@ using System.Runtime.InteropServices; using System.Security; using Google.Protobuf; using security_key = System.UInt32; +using bts.co2; +using System.Linq; namespace mbts.co2 { @@ -85,6 +87,7 @@ namespace mbts.co2 [DllImport("libspark")] internal unsafe static extern void post_on_scan_security_thread(Action callback); + } internal static class Co2NativeTestMethods @@ -133,5 +136,43 @@ namespace mbts.co2 [DllImport("libspark")] internal static extern void co2_test_get_constants(out ulong max_depth, out ulong max_implied_depth, out sbyte nan_exponent); + internal unsafe delegate void raw_receive_pb_or_string(byte* buffer, int buffer_size); + [DllImport("libspark")] + private static unsafe extern void raw_co2_compute_synthetic_book( + byte[] security_buffer, + int security_length, + byte[][] leg_books, + int[] leg_lengths, + int leg_books_length, + raw_receive_pb_or_string callback); + + internal static unsafe BookMessageImmutable co2_compute_synthetic_book(SecurityImmutable synthetic, params BookMessageImmutable[] books) { + BookMessage result = null; + string error = null; + + var security_buffer = synthetic.ToByteArray(); + var book_buffers = books.Select(b => b.ToByteArray()).ToArray(); + var lengths = book_buffers.Select(b => b.Length).ToArray(); + raw_co2_compute_synthetic_book( + security_buffer, + security_buffer.Length, + book_buffers, + lengths, + book_buffers.Length, + (byte* buffer, int length) => { + if (length < 0) { + error = new string((char*)buffer,0,-1 * length); + } + else { + result = Co2NativeMethods.deserialize(buffer, (ulong)length); + } + }); + + if(error != null) { + // C# style multiple return values + throw new Exception(error); + } + return new BookMessageImmutable(result); + } } } diff --git a/src/lib/md/test/Co2ManagedTestHelper.cpp b/src/lib/md/test/Co2ManagedTestHelper.cpp index a59e9a618..cb2662cff 100644 --- a/src/lib/md/test/Co2ManagedTestHelper.cpp +++ b/src/lib/md/test/Co2ManagedTestHelper.cpp @@ -4,6 +4,7 @@ #include "lib/config/config.h" #include "lib/md/protos/co2.pb.h" #include "lib/utils/TimeStamp.h" +#include "lib/md/test/md_test_utils.h" #include #include @@ -15,7 +16,7 @@ using namespace co2; std::vector create_test_securities(MDProducer& producer, const char* postfix = "M"); -void assign_ids(std::vector::iterator begin, std::vector::iterator end, security_key start_key = 1000); +// void assign_ids(std::vector::iterator begin, std::vector::iterator end, security_key start_key = 1000); extern "C" BTS_EXPORT void co2_get_1234_handle(size_t& size, MDConsumerHandle& handle, uintptr_t* hotaddr, uintptr_t* coldaddr, uintptr_t* headeraddr) { // Test that marshaling is working @@ -169,5 +170,72 @@ extern "C" BTS_EXPORT void co2_get_sample_datetime(datetime::LLDateTime& now, da utc_now = datetime::ll_now(); } + + +// Book compute_synthetic_book(const api::Security& security, const std::vector& leg_books); + +typedef void(*raw_receive_pb_or_string)(const char*, int); +extern "C" BTS_EXPORT void raw_co2_compute_synthetic_book( + const char* security_buffer, + int security_len, + const char** book_buffers, + const int* book_lengths, + int book_buffers_length, + raw_receive_pb_or_string cb) { + + // Deserialize Security + api::Security security; + if (!security.ParseFromArray(security_buffer, security_len)) { + const char* err = "Security parse error"; + cb(err, -1 * strlen(err)); + return; + } + + // Deserialize BookMessage and convert to Book + std::vector books; + for (int x = 0; x < book_buffers_length; ++x) { + BookMessage bm; + if (!bm.ParseFromArray(book_buffers[x], book_lengths[x])) { + auto err = fmt::format("Book parse error: {}", x); + cb(err.c_str(), -1 * err.size()); + return; + } + Book book; + for (const auto& bid : bm.bids()) { + IntPrice ip { bid.mantissa(), (int8_t)bid.exponent() }; + book.bids.push_back({ ip.price(), bid.size() }); + } + for (const auto& ask : bm.asks()) { + IntPrice ip { ask.mantissa(), (int8_t)ask.exponent() }; + book.asks.push_back({ ip.price(), ask.size() }); + } + books.push_back(book); + } + + // Make the call we wanted to make in the first place + Book bres = compute_synthetic_book(security, books); + + // Copy back to a BookMessage + BookMessage bmres; + for (const auto & bid : bres.bids) { + PriceSizeMessage& ps = *bmres.add_bids(); + auto ip = IntPrice_from_double<11>(bid.price); + ps.set_mantissa(ip.mantissa()); + ps.set_exponent(ip.exponent()); + ps.set_size(bid.size); + } + for (const auto & ask : bres.asks) { + PriceSizeMessage& ps = *bmres.add_asks(); + auto ip = IntPrice_from_double<11>(ask.price); + ps.set_mantissa(ip.mantissa()); + ps.set_exponent(ip.exponent()); + ps.set_size(ask.size); + } + + // serialize to string + auto str = bmres.SerializeAsString(); + cb(str.c_str(), str.size()); +} + }}}//namespace #endif diff --git a/src/lib/md/test/md_test_utils.cpp b/src/lib/md/test/md_test_utils.cpp index bc2848c16..6dc438f71 100644 --- a/src/lib/md/test/md_test_utils.cpp +++ b/src/lib/md/test/md_test_utils.cpp @@ -1,5 +1,8 @@ #include "md_test_utils.h" #include "../MDProducer.h" +#include "lib/utils/sane_algorithm.h" +#include "../MDConsumerHandle.h" +#include "Co2TestInstrument.h" #include #include @@ -38,4 +41,49 @@ void assign_ids(std::vector::iterator begin, std::vector& leg_books) { + auto synthetic = std::make_shared(); + synthetic->security.CopyFrom(security); + std::vector leg_instruments; + for (const auto& leg : synthetic->security.synthetic().legs()) { + const auto& security = leg.security(); + Co2TestInstrument instr(security.symbol(), securitytype_of(security)); + convert_security(security, instr.cold().security, true); + leg_instruments.push_back(instr); + synthetic->legs.push_back(instr.consumer()); + } + + // Write leg books + for (size_t x = 0; x < leg_books.size(); ++x) { + std::array bids = {}, asks = {}; + std::transform( + leg_books[x].bids.begin(), + leg_books[x].bids.end(), + bids.begin(), + [](const PS& ps) { + return PriceSize_from_double<7>(ps.price, ps.size); + }); + std::transform( + leg_books[x].asks.begin(), + leg_books[x].asks.end(), + asks.begin(), + [](const PS& ps) { + return PriceSize_from_double<7>(ps.price, ps.size); + }); + auto ts = TimeStamp::now(); + leg_instruments[x].producer().update_direct(ts, bids, asks); + } + + // Read synthetic book back + MDConsumerHandle synthetic_handle { synthetic }; + std::array raw_bids, raw_asks; + TimeStamp t; + synthetic_handle.get_direct_book(t, COCONUT_MAX_DEPTH, raw_bids.data(), raw_asks.data()); + Book result {}; + sane::map(raw_bids, result.bids, [](PriceSize ps) -> PS { return {ps.price(), ps.size()}; }); + sane::map(raw_asks, result.asks, [](PriceSize ps) -> PS { return {ps.price(), ps.size()}; }); + + return result; +} + }}}//namespace \ No newline at end of file diff --git a/src/lib/md/test/synthetic_book_merge_tests.cpp b/src/lib/md/test/synthetic_book_merge_tests.cpp index 63d1914c9..01a978f70 100644 --- a/src/lib/md/test/synthetic_book_merge_tests.cpp +++ b/src/lib/md/test/synthetic_book_merge_tests.cpp @@ -142,50 +142,6 @@ void set_leg_params(SyntheticStrategy& strat, uint32_t security_key, const spark } -Book compute_synthetic_book(const api::Security& security, const std::vector& leg_books) { - auto synthetic = std::make_shared(); - synthetic->security.CopyFrom(security); - vector leg_instruments; - for (const auto& leg : synthetic->security.synthetic().legs()) { - const auto& security = leg.security(); - Co2TestInstrument instr(security.symbol(), securitytype_of(security)); - convert_security(security, instr.cold().security, true); - leg_instruments.push_back(instr); - synthetic->legs.push_back(instr.consumer()); - } - - // Write leg books - for (size_t x = 0; x < leg_books.size(); ++x) { - std::array bids = {}, asks = {}; - std::transform( - leg_books[x].bids.begin(), - leg_books[x].bids.end(), - bids.begin(), - [](const PS& ps) { - return PriceSize_from_double<7>(ps.price, ps.size); - }); - std::transform( - leg_books[x].asks.begin(), - leg_books[x].asks.end(), - asks.begin(), - [](const PS& ps) { - return PriceSize_from_double<7>(ps.price, ps.size); - }); - auto ts = TimeStamp::now(); - leg_instruments[x].producer().update_direct(ts, bids, asks); - } - - // Read synthetic book back - MDConsumerHandle synthetic_handle { synthetic }; - std::array raw_bids, raw_asks; - TimeStamp t; - synthetic_handle.get_direct_book(t, COCONUT_MAX_DEPTH, raw_bids.data(), raw_asks.data()); - Book result {}; - sane::map(raw_bids, result.bids, [](PriceSize ps) -> PS { return {ps.price(), ps.size()}; }); - sane::map(raw_asks, result.asks, [](PriceSize ps) -> PS { return {ps.price(), ps.size()}; }); - - return result; -} /////////////////////// // Test Cases