From 856a9b80ea0bf0468330761af8dbcad65d2f802c Mon Sep 17 00:00:00 2001 From: andrey Date: Fri, 16 Jul 2021 14:11:55 +0300 Subject: [PATCH] PIVector reshape and etc... --- libs/main/containers/pivector.h | 49 ++++ libs/main/containers/pivector2d.h | 3 +- libs/main/math/pimathvector.h | 9 + main.cpp | 249 ++------------------ utils/cloud_dispatcher/dispatcherserver.cpp | 2 +- 5 files changed, 76 insertions(+), 236 deletions(-) diff --git a/libs/main/containers/pivector.h b/libs/main/containers/pivector.h index e7a3bd48..429cf924 100644 --- a/libs/main/containers/pivector.h +++ b/libs/main/containers/pivector.h @@ -233,6 +233,13 @@ public: elementNew(piv_data + i, f); return *this; } + inline PIVector & fill(std::function f) { + deleteT(piv_data, piv_size); + PIINTROSPECTION_CONTAINER_USED(T, piv_size) + for (size_t i = 0; i < piv_size; ++i) + elementNew(piv_data + i, f(i)); + return *this; + } inline PIVector & assign(const T & f = T()) {return fill(f);} template::value @@ -264,6 +271,21 @@ public: } return *this; } + inline PIVector & resize(size_t new_size, std::function f) { + if (new_size < piv_size) { + T * de = &(piv_data[new_size]); + deleteT(de, piv_size - new_size); + piv_size = new_size; + } + if (new_size > piv_size) { + size_t os = piv_size; + alloc(new_size); + PIINTROSPECTION_CONTAINER_USED(T, (new_size-os)) + for (size_t i = os; i < new_size; ++i) + elementNew(piv_data + i, f(i)); + } + return *this; + } template::value , int>::type = 0> @@ -448,6 +470,33 @@ public: return ret; } + inline PIVector> reshape(size_t rows, size_t cols) const { + assert(rows*cols == piv_size); + PIVector> ret; + ret.resize(rows); + for (size_t i = 0; i < rows; i++) { + ret[i] = PIVector(&(piv_data[i*cols]), cols); + } + return ret; + } + + template>::value + , int>::type = 0> + inline PIVector reshape() const { + PIVector ret; + size_t rows = size(); + if (rows) { + size_t cols = at(0).size(); + ret.reserve(rows * cols); + for (size_t i = 0; i < rows; i++) { + ret.append(at(i)); + } + ret.resize(rows * cols); + } + return ret; + } + private: inline void _reset() {piv_size = piv_rsize = 0; piv_data = 0;} inline size_t asize(size_t s) { diff --git a/libs/main/containers/pivector2d.h b/libs/main/containers/pivector2d.h index c39938f0..748f522b 100644 --- a/libs/main/containers/pivector2d.h +++ b/libs/main/containers/pivector2d.h @@ -50,10 +50,11 @@ public: inline PIVector2D(size_t rows, size_t cols, PIVector && v) : rows_(rows), cols_(cols), mat(std::move(v)) { mat.resize(rows*cols); } - inline PIVector2D(const PIVector > & v) { + inline PIVector2D(const PIVector> & v) { rows_ = v.size(); if (rows_) { cols_ = v[0].size(); + mat.reserve(rows_*cols_); for (size_t i = 0; i < rows_; i++) { mat.append(v[i]); } diff --git a/libs/main/math/pimathvector.h b/libs/main/math/pimathvector.h index 6dc54c69..9a5d0166 100644 --- a/libs/main/math/pimathvector.h +++ b/libs/main/math/pimathvector.h @@ -260,6 +260,7 @@ class PIP_EXPORT PIMathVector { public: PIMathVector(const uint size = 0, const Type & new_value = Type()) {c.resize(size, new_value);} PIMathVector(const PIVector & val) {c = val;} + PIMathVector(PIVector && val) : c(std::move(val)) {} PIMathVector(std::initializer_list init_list) {c = PIVector(init_list);} template @@ -271,6 +272,14 @@ public: for (uint i = 0; i < v.size(); ++i) v.c[i] = fn[i] - st[i]; } + static PIMathVector zeros(const uint size) {return PIMathVector(size, Type());} + static PIMathVector ones(const uint size) {return PIMathVector(size, Type(1));} + static PIMathVector arange(const Type start, const Type stop, const Type step = Type(1)) { + PIVector v; + for (Type i = start; i < stop; i+= step) v << i; + return PIMathVector(std::move(v)); + } + uint size() const {return c.size();} _CVector & resize(uint size, const Type & new_value = Type()) { c.resize(size, new_value); diff --git a/main.cpp b/main.cpp index a26a37b3..6afa8630 100644 --- a/main.cpp +++ b/main.cpp @@ -1,241 +1,22 @@ #include "pip.h" -PIPeer p0("p0"), p1("p1"); +template >::value + , int>::type = 0> PIVector rrr(PIVector) { + piCout << std::is_same>::value; + return PIVector(); +} + int main() { - - PIMap sends, recs; - PISet errors, missed; - - CONNECTL(&p0, dataReceivedEvent, ([&](const PIString & from, const PIByteArray & data){ - uint cnt = *(uint*)(data.data()); - piCout << "rec " << cnt << data.size_s(); - recs[cnt] = data.size_s(); - if (sends[cnt] != data.size_s()) { - piCout << " " << cnt << "ERROR"; - errors << cnt; - } - })); - - p0.start(); - p1.start(); - - piSleep(2); - - int count = 10; - for (int i = 0; i < count; ++i) { - PIByteArray msg; - msg << (uint)i; - msg.enlarge(randomi() % 3800 + 4000); - sends[i] = msg.size_s(); - piCout << "send" << i << msg.size_s(); - p1.send(p0.name(), msg); - piMSleep(100); - } - - piSleep(1); - for (int i = 0; i < count; ++i) - if (!recs.contains(i)) missed << i; - piCout << "errors" << errors; - piCout << "missed" << missed; - - - /*PIDataTransfer tr0, tr1; - CONNECTL(&tr0, receiveFinished, ([&](bool ok){ - PIByteArray ba = tr0.data(); - uint cnt = *(uint*)(ba.data()); - piCout << "rec " << cnt << ok << ba.size_s(); - })); - - CONNECTL(&tr1, sendRequest, ([&](PIByteArray & data){tr0.received(data);})); - CONNECTL(&tr0, sendRequest, ([&](PIByteArray & data){tr1.received(data);})); - - int count = 10; - for (int i = 0; i < count; ++i) { - PIByteArray msg; - msg << (uint)i; - msg.enlarge(randomi() % 3800 + 4000); - //sends[i] = msg.size_s(); - piCout << "send" << i << msg.size_s(); - tr1.send(msg); - //piMSleep(100); - }*/ - - - return 0; - - const int iters = 4; - const int sz = 10000000; - - -// { -// piCout << "PIVector push back"; -// PIVector v; -// //v.reserve(sz); -// for (int n=0; n v; -// v.reserve(sz); -// for (int n=0; n v; -// //v.reserve(sz); -// for (int n=0; n v; -// //v.reserve(sz); -// for (int n=0; n v; -// //v.reserve(sz); -// for (int n=0; n v; -// //v.reserve(sz); -// for (int n=0; n ind; - for (int i=0; i v; - //v.reserve(sz); - for (int n=0; n v; - //v.reserve(sz); - for (int n=0; n v; - //v.reserve(sz); - for (int n=0; n v; -// PIDeque v2; -// //v.reserve(sz); -// for (int n=0; n v; -// PIDeque> v2; -// //v.reserve(sz); -// for (int n=0; n(i, v[i]); -// piCout << tm.elapsed_m(); -// v.clear(); -// v2.clear(); -// } -// } - - + PIVector x; + x.resize(16, [](size_t i) {return i+1;}); + piCout << x; + PIVector> m = x.reshape(4,4); + piCout << m; + PIVector y; + y = m.reshape(); + piCout << y; return 0; } diff --git a/utils/cloud_dispatcher/dispatcherserver.cpp b/utils/cloud_dispatcher/dispatcherserver.cpp index cdf79ecd..ad326e5b 100644 --- a/utils/cloud_dispatcher/dispatcherserver.cpp +++ b/utils/cloud_dispatcher/dispatcherserver.cpp @@ -127,8 +127,8 @@ const DispatcherClient * DispatcherServer::getServer(int index) { map_mutex.lock(); if (index >=0 && index < clients.size_s()) { tmp = clients[index]; + if (index_c_servers.contains(tmp)) ret = tmp; } - if (index_c_servers.contains(tmp)) ret = tmp; map_mutex.unlock(); return ret; }