(1) Install VC++ for Linux development Extnsion (2) sudo apt-get install openssh-server g++ gdb gdbserver on target e.g Raspery Pi. (3) Too->Options-> search connection->conn manager to set up SSH connection to ARM Raspery Pi (4) VS project cross platform console app, main.cpp, remote gdb (5) Headless app will run inside Debug->Linux Console. (6) Raspery Pi ~/project will have file to run ./console1.out #inlcudeis all that needed for now
Sunday, October 30, 2016
Writing Linux C++ code in VS 2015
Using VS code C++ extension for MSVC project
(1) delete all .vscode related file in the MSVC project directories (2) Extension search C++ to install C++ Extension from Microsoft (3) Open Folder to point to the project direct and open .cpp file (4) ctl-shift-build, bring up config task, select "MS build task" nothing to change in task.json (5) hit Debug and config launch, select "C++ Launch Windows", set program "c:\test\demo\debug\demo.exe" if missing include then set includePath in c_cpp_properties.json to point to the include path under Win32
Saturday, October 29, 2016
using iterator_trait to get iterator type to *value and iterator category template specialization to get iter type
tag dispatching--template specialization can be based on the tag
So Dispatch() can pass in typename iterator_traits<T>::iterator_category() to find matching specialization
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
#include <functional>
#include <algorithm>
#include <list>
#include <iterator>
#include <forward_list>
using namespace std;
template<typename InputIter, typename Func>
void adjacent_pair2(InputIter first, InputIter last, Func func)
{
if (first != last)
{
typename std::iterator_traits<InputIter>::value_type trailer = *first;
//auto trailer = *first; // C++ 11,14 cannot tell type
++first;
for (; first != last; ++first)
{
func(trailer, *first);
trailer = *first;
}
}
};
template<class C>
void print_iter_category(C c, std::random_access_iterator_tag)
{
cout << "from random iter tag specialization" << endl;
}
template<class C>
void print_iter_category(C c, std::bidirectional_iterator_tag)
{
cout << "from bi-direct iter tag specialization" << endl;
}
template<class C>
void print_iter_category(C c, std::input_iterator_tag)
{
cout << "from input iter specialization" << endl;
}
template<class C>
void print_iter_category(C c, std::output_iterator_tag)
{
cout << "from output iter specialization" << endl;
}
template<class C>
void print_iter_category(C c, std::forward_iterator_tag)
{
cout << "from fwd iter specialization" << endl;
}
template<typename T>
void Show(T t)
{
print_iter_category(t, typename iterator_traits<T>::iterator_category());
print_iter_category(t, iterator_traits<T>::iterator_category());;
}
int main()
{
vector<int> v = { 2,16,36,109,8,999,90,5 };
func_cout a_func;
adjacent_pair2<vector<int>::iterator, func_cout>(v.begin(), v.end(),a_func);
list<int> l;
Dispatch(l.end()); /bi-dir
Dispatch(v.begin()); // vector is random iter
istream_iterator<int> isIter;
Dispatch(isIter);
ostream_iterator<int> osIter(cout);
Dispatch(osIter);
forward_list<int> fl;
Dispatch(fl.begin()); //fwd
std::string s;
getline(cin, s);
return 0;
}
C++ iterator, Func as operator () or std:function and predicate as func
(1) there are various kinds of iterators: forward, random, input, output,bi-directional, iterator_traits::iterator_category (2) for(; ...) will avoid post increment iterator, since some does not support iter++ (3) C++ func are struct with operator() overridden or std::function as a function_ptr (4) iterator can be de-referenced or indexed in *, [] #include "stdafx.h" #include <iostream> #include <string> #include <vector> #include <functional> #include <algorithm> using namespace std; template<typename ForwardIter, typename Func> void adjacent_pair(ForwardIter first, ForwardIter last, Func func) { if (first != last) { ForwardIter trailer = first; ++first; for (; first != last; ++first, ++trailer) func(*trailer, *first); } }; template<typename SourceIter, typename TargetIter, typename Predicate> std::pair<SourceIter, TargetIter> copy_while(SourceIter srcFirst, SourceIter srcLast, TargetIter tgt, Predicate p) { for (; srcFirst != srcLast && p(*srcFirst); ++srcFirst,++tgt ) *tgt = *srcFirst; return std::make_pair(srcFirst,tgt); }; struct func_cout { inline void operator() (int a, int b) const { cout << a << " " << b << endl; } }; struct func_predicate_even { inline bool operator() (int a) const { return a % 2==0; } } inline void print(int a,int b) { cout << a << "|" << b << endl;} int main() { vector<int> v = { 2,16,36,109,8,999,90,5 }; func_cout a_func; adjacent_pair<vector<int>::iterator, func_cout>(v.begin(), v.end(),a_func); std::function<void(int,int)> f_ptr = print; adjacent_pair<vector<int>::iterator, std::function<void(int, int)>>(v.begin(), v.end(), f_ptr); vector<int> w(5); func_predicate_even pred; auto ret_iter = copy_while(v.begin(),v.end(), w.begin(),pred); cout << *ret_iter.first <<"**"<< *ret_iter.second <<"copied "<<w[1]<< endl; std::string s; getline(cin, s); return 0; }
Thursday, October 27, 2016
Transform a type into awaitable by Adapter or pass trhough
#include "stdafx.h"
#include <experimental\coroutine>
#include <iostream>
#include <string>
#include <future>
#include <thread>
using namespace std;
using namespace std::experimental;
template<typename T>
struct await_adapter
{
T const& t_;
bool await_readY() const;
void await_suspend(std::experimental::coroutine_handle<> h) const;
auto await_resume() const;
};
template<typename T>
class IAsyncOperation
{
};
template<typename T>
await_adapter<IAsyncOperation<T>> operator co_await(IAsyncOperation<T> const& async)
{
return async;
};
struct pass_through
{
int result;
bool await_ready() { return true; }
void await_suspend(std::experimental::coroutine_handle<> h) {};
auto await_resume() { return result; }
};
struct value
{
int result;
};
pass_through operator co_await(value v) // overload operator to make awaitable
{
return{ v.result };
};
void test()
{
auto i = co_await pass_through{ 123 };
auto j = co_await value{ 456 };
}
int main()
{
cout << "main thread id:" << std::this_thread::get_id() <<endl;
test();
//cout << r.get() << endl; //get causes return, no SM setup
std::string s;
getline(cin, s);
return 0;
}
Tuesday, October 25, 2016
co_await vs. future.get()
co_wait will setup a state machine to return, future has to call get() to return
#include "stdafx.h"
#include <experimental\coroutine>
#include <iostream>
#include <string>
#include <future>
#include <thread>
using namespace std;
using namespace std::experimental;
std::future<int> produceAsync()
{
return std::async(std::launch::async, [] {
cout << " future thread id:"<< std::this_thread::get_id()<<endl;
std::this_thread::sleep_for(5s);
return 42;
});
}
std::future<void> ConsumeAsync()
{
auto r = co_await produceAsync(); // set up resume state machine
cout << r << endl;
}
int main()
{
cout << "main thread id:" << std::this_thread::get_id() <<endl;
ConsumeAsync();
auto r = produceAsync();
//cout << r.get() << endl; //get causes return, no SM setup
std::string s;
getline(cin, s);
return 0;
}
Monday, October 24, 2016
generator pipeline and accumulate
#include "stdafx.h"
#include <experimental\generator>
#include <experimental\coroutine>
#include <iostream>
#include <string>
#include <numeric>
using namespace std;
using namespace std::experimental;
std::experimental::generator<int> gen()
{
for (int i = 0;; ++i)
co_yield i;
};
std::experimental::generator<int> take_until(std::experimental::generator<int> g, int sentinel)
{
for (auto v : g)
{
if (v == sentinel)
break;
co_yield v;
}
}
int main()
{
auto g1 = take_until(gen(), 10);
auto a = std::accumulate(g1.begin(), g1.end(), 0);
cout << a << endl;
std::string s;
getline(cin, s);
return 0;
}
how future made avaitable
This is some information of internal coroutinea
awaiter-co_wait as operator on future and traits into coroutine
struct future_awaiter
{
future<T>& _f;
bool await_ready() {
_f.then([ch]() {});
return _f.is_ready();
}
void await_suspend(coroutine_handle<> ch)
{
_f.then([ch]() {ch.resume(); })
}
auto await_resume() { return _f.get(); }
};
template<typename T>
future_awaiter<T> operator co_await(future<T>& value) // transform future=>awaitable
{
return future_awaiter<T>(value);
};
// make future a coroutine type by specialization
template<typename T, typename... Arguments>
struct coroutine_traits<future<T>, Arguments...>
{
struct promise_type
{
promise<T> _promise;
future<T> get_return_object() { return _promise.get_future(); }
auto initialize_suspend() { return suspend_never{}; }
auto final_suspend() { return suspend_never{}; }
template<typename U>
void return_value(U&& value) { _promise.set_value(std::forward<U>(value)); }
void set_exception(std::exception_ptr ex) { _promise.set_exception(std::move(ex)); }
};
};
co_wait co_return co_yield
coroutine can yield from generator, return from future and wait a async lambda #include "stdafx.h" #include <experimental\generator> #include <experimental\coroutine> #include <future> #include <iostream> #include <string> using namespace std; using namespace std::experimental; future<int> compute() { int ret= co_await std::async([] { return 30; }); co_return ret; } std::experimental::generator<int> gen() { for (int i = 0; i < 10; ++i) co_yield i; }; int main() { for (auto v : gen()) { cout << v << endl; } auto i = compute(); cout << i.get() << endl; std::string s; getline(cin, s); return 0; }
Sunday, October 16, 2016
Delayed Evaluation and Expression template
Instead of loop through vector index multiple times when adding mutiple vector.
could keep ref to sum of two and later loop-through once to reduce temp var
#include <iostream>
#include <vector>
#include <cstdlib>
using namespace std;
// temp var one-by-one
//template<typename T>
//inline vector<T> operator+(const vector<T>& x, const vector<T> y)
//{
// vector<T> sum(x.size());
// for(int i=0;i<x.size();i++)
// sum[i]=x[i]+y[i]; //temp var
// return sum;
//}
// delayed evaluation expression template
template <typename T>
struct vector_sum
{
const vector<T> &x, &y;
vector_sum(const vector<T> &x, const vector<T> &y) :x(x), y(y) {}
T operator[](int i) const { return x[i] + y[i]; } // indexer
};
template<typename T>
vector_sum<T> operator+(const vector<T>& x, const vector<T>& y)
{
return{ x,y }; // delayed evaluation to a single loop
};
template<typename T> class vector // new impl of vector
{
const vector<T> &data;
vector& operator=(const vector_sum<T>& that) //assignment
{
check_size(size(that));
int my_size = size(that);
for (int i = 0; i<my_size; ++i)
data[i] = that[i]; // now loop through.
return *this;
}
};
int main() {
std::vector<int> x = { 1,2,3 };
std::vector<int> y = { 4,5,6 };
auto s = x + y;
cout << s[2] << endl;
return 0;
}
Saturday, October 15, 2016
type traits compile time if in typedef
#include "stdafx.h"
#include <iostream>
#include <type_traits>
#include <typeinfo>
using namespace std;
template<bool cond, typename T, typename ElseT>
struct conditional2 {
using type = T;
};
template<typename T, typename ElseT>
struct conditional2<false, T, ElseT>
{
using type = ElseT;
};
int main() {
typedef conditional2<true, int, char>::type T1;
typedef conditional2<1 == 2, int, char>::type T2;
cout << typeid(T1).name() << " " << typeid(T2).name() << endl;
return 0;
}
type traits is_const and variadic template extending std::min
Eclipse CDT tool chain has an issue--"using std::is_heap" not declared inso used VS 2015 instead. #include "stdafx.h" #include <iostream> #include <algorithm> #include <type_traits> using namespace std; template<typename T> struct is_const2 { static const bool value = false; }; template<typename T> struct is_const2<const T> // specialization { static const bool value = true; }; template<typename T> inline T min2(const T& t) { return t; } template<typename T, typename ...P> inline auto min2(const T& t, const P& ...p) { using res_type = std::common_type_t<T, P...>; return std::min(res_type(t), res_type(min2(p...))); }; int main() { cout << "test " << is_const2<const int*>::value << endl; cout << "test " << std::is_const<int* const>::value << endl; cout << "test " << is_const2<const int&>::value << endl; const int i = 123; const double j = 456.1; const char k = 'a'; auto m = min2(i,j,k); return 0; }
Rx Replay
Hot= publish(), cold=hot replay(), hot connect, cold connect
private void SetupReplayObservable()
{
var HotObservable = Observable.FromEvent(
h => _Manager.DataReceived += h,
h => _Manager.DataReceived -= h).Publish();
InfoObservable = HotObservable.Replay();
HotObservable.Connect();
InfoObservable.Connect();
}
Sunday, October 9, 2016
What are the frequency of HFT
Some data collected for High Frequency Trading: penny made each, 40 Million per day=> 1.5 Trades per ms =>42M per 7 hour period(9:30-4:30) HFT account for 70% trades in the market, use to be only 30%. At open Citadel executed 20M shares in 3 minutes --- HFT and Market Maker, 14% of Market trades
Saturday, October 8, 2016
Set Up IDE to compile C++17 code constexpr with statement, recursion in body
(1) VS 2015 cannot compile constexpr with statement body.
(2) Eclipse CDT (C++ dev tool) can but need the following install/setup/config
(3) cdt need compiler and cygwin64 must be installed (latest version 2.6.0)
(4) C++ 17 is supported in 2.6.0 but may not be supported in MinGW.
(5) during cygwin64 install to c:\cygwin64, must install package under devel--gcc,g++, make, gdb
(6) cdt download is cdt 9.1.0 =Eclipse neon 1.
(7) windows->preference->C/C++->Environment add path c:\cygwin64\bin
(8) project ->properties->C/C++ build ->Toolchain Editor ->Current Toolchain=CygWin gcc, builder=CDT internal builder
(9) same as (8), C++ compiler->settings->includes "C:\cygwin64\lib\gcc\x86_64-w64-mingw32\5.4.0\include"
(10) same as (9) C++ compiler->misc->other flag -std=c++17
(11) project ->C/C++ index -> rebuild/search/un-resolved can help to find out why some include are not found
(12) right click on a project can also access properties
#include <iostream>
#include <cmath>
using namespace std;
constexpr bool is_prime(int i)
{
if (i == 1) return false;
if (i % 2 == 0)
return i == 2;
double max_check =sqrt(i) + 1;
for (int j = 3; j < max_check; j += 2)
if (i%j == 0) return false;
return true;
}
using namespace std;
int main() {
cout << "test " <<is_prime(67)<< endl;
return 0;
}
contexpr with recursion and new if statement
constexpr bool is_prime2(int i, int div)
{
return div>=i?true:(i%div==0?false:is_prime2(i,div+2));
}
constexpr bool is_prime(int i)
{
return i==1?false:(i%2==0?i==2:is_prime2(i,3));
}
Dependency Walker for .Net
No such thing exactly but the following tools are close: git https://github.com/mikehadlow/AsmSpy open in VS 2012, restore missing packages (maybe Install-Package Microsoft.Extensions.CommandLineUtils) maybe working in CS 2013. copy AsmSpy, etc in /bin to App asmspy . all >refList.txt other tools http://www.amberfish.net/ http://www.ndepend.com/
Thursday, October 6, 2016
template fibonacci
template<long n>
struct fibonacci {
static const long value = fibonacci<n - 1>::value + fibonacci<n - 2>::value;
};
template<> struct fibonacci<1> { static const long value = 1; };
template<> struct fibonacci<2> { static const long value = 1; };
Wednesday, October 5, 2016
Specialization Template
#include "stdafx.h"
template<int i> struct D { D(void*); operator int(); };
template<int p, int i> struct is_prime {
enum {prim=(p==2) ||(p%i)&&is_prime<(i>2?p:0),i-1>::prim };
};
template<> struct is_prime<0,0> { enum { prim = 1 }; };
template<> struct is_prime<0,1> { enum { prim = 1 }; };
int main()
{
auto p = is_prime<243, 20>::prim;
return 0;
}
Saturday, October 1, 2016
VMMap and RamMap
VMMap:
(1) VM Types: image,mapped-file, Heap, stack(for threads)
(2) VM alloc: Committed, Reserved, Private, Shareable
(3) Task manager does not show type
(4) Working set = RAM
(5) Graph1 -- coloring Private data in private bytes along non-data
Graph 2 -- Working set with portion colored as private data
shows how much private data actually in WS.
RamMap
(6) Page List: Working Set =>Modified Page List (still in RAM)
=> StandBy Page List (Disk) by ModPage Writter
(7) Process Explorer/System Info shows all page List: Zero
StandBy, Modify, Free.
(8) Did global or heap var in your app pass through the list?
(9) File Summary: For each process, Page List view
(10) Priority summary: How much re-pupose on Priority 7=> Ram Pressure
(11) Physical Pages: Mapped-File full path
Subscribe to:
Comments (Atom)