12 January 2019

C++ How to dynamically create an array of objects

 #include "stdafx.h"  
 #include <iostream>  
 using namespace std;  
 class Point  
 {  
 public:  
      int x; int y;  
      Point(int _x, int _y)  
      {  
           x = _x;  
           y = _y;  
           cout << "Point constructor:(" << x << "," << y << ")" << endl;  
      }  
      ~Point()  
      {  
           cout << "Point destructor:(" << x << "," << y << ")" << endl;  
      }  
 };  
 std::ostream& operator<< (std::ostream& stream, const Point& p) {  
      return stream << "(" << p.x << "," << p.y << ")" << endl;  
 }  
 double* CreateDoubles()  
 {  
      double* d = new double[3];  
      d[0] = 1.1;  
      d[1] = 2.2;  
      d[2] = 3.3;  
      return d;  
 }  
 Point** CreatePoints()  
 {  
      Point** p = new Point*[2];  
      p[0] = new Point(1, 2);  
      p[1] = new Point(3, 4);  
      return p;  
 }  
 void Test()  
 {  
      double* d = CreateDoubles();  
      cout << "Dynamically created doubles: " << d[0] << ", " << d[1] << ", " << d[2] <<endl <<endl;  
      delete[] d;  
      Point** p = CreatePoints();  
      cout << "Dynamically created Points: " << endl;  
      cout << *p[0] << endl;  
      cout << *p[1] << endl;  
      delete p[0];  
      delete p[1];  
      delete[] p;  
 }  
 int main()  
 {  
      Test();  
      getchar();  
   return 0;  
 }  

29 December 2018

C++ adaptation of Python Pool function

To achieve Python pool function

https://docs.python.org/2/library/multiprocessing.html

from multiprocessing import Pool

def f(x):
    return x*x

if __name__ == '__main__':
    p = Pool(5)
    print(p.map(f, [1, 2, 3]))

This is what I have come up with


 #include "stdafx.h"  
 #include <iostream>  
 #include <vector>  
 #include <unordered_map>  
 #include <mutex>  
 #include <windows.h>  
 #include <functional>   
 using namespace std;  
 std::mutex mtx1;  
 std::mutex mtx2;  
 unordered_map<int, double> A;  
 unordered_map<int, double> B;  
 typedef std::function<double(double)> funcDoubleToDouble;  
 DWORD_PTR createAffinityMask(int numCPU)  
 {  
      DWORD_PTR m = 0;  
      for (int i = 0; i < numCPU; i++)  
      {  
           DWORD_PTR temp = 1 << i;  
           m = m | temp;  
      }  
      return m;  
 }  
 double Square(double x)  
 {  
      return x * x;  
 }  
 void MapFunc(unordered_map<int, double>& source, unordered_map<int, double>& dest, int i, funcDoubleToDouble f, DWORD_PTR pmask)  
 {  
      //set processor affinity  
      HANDLE process = GetCurrentProcess();   
      BOOL success = SetProcessAffinityMask(process, pmask);  
      //lock global variables  
      mtx1.lock();  
      dest[i] = f(source[i]);  
      mtx1.unlock();  
      //unlock  
      //wait a little while to simulate processing  
      clock_t t = clock() + 100; // consume 100ms ...  
      while (clock() < t) {}       // of CPU time  
      printf("i %d:\tlogical CPU %d\tProcessId %d\n", i, GetCurrentProcessorNumber(), GetCurrentProcessId());  
 }  
 /*  
 map<int, double>& source - source map data  
 map<int, double>& dest - destination map data  
 funcDoubleToDouble f - mapping function  
 DWORD_PTR pmask - cpu processor mask (bitmapping)  
 */  
 void PoolMap(unordered_map<int, double>& source, unordered_map<int, double>& dest, funcDoubleToDouble f, DWORD_PTR pmask)  
 {  
      std::thread H[10];  
      for (int i = 0; i < 10; i++)  
      {  
           int index = i;  
           H[i]=thread(MapFunc, std::ref(source), std::ref(dest), i, f, pmask);  
      }  
      for (int i = 0; i < 10; i++)  
      {  
           H[i].join();  
      }  
      return;  
 }  
 int main()  
 {  
      unsigned num_cpus = std::thread::hardware_concurrency(); //retrieve the number of processors  
      DWORD_PTR pmask = createAffinityMask(5); // only use the first 5  
      std::cout << "CPU has " << num_cpus << " processors\n";  
      //initialize sourcevalues  
      for (int i=0;i<10;i++)  
      {  
           A[i] = (double) i;  
      }  
      //set pointer to the function we will use for mapping  
      funcDoubleToDouble f = Square;  
      PoolMap(A, B, f, pmask);  
      //show output of mapping function  
      for (int i = 0; i < 10; i++)  
      {  
           cout << i << ": " << B[i] << endl;  
      }  
      getchar();  
   return 0;  
 }  

28 December 2018

C++ set processor affinity on Windows

 #include "stdafx.h"  
 #include <iostream>  
 #include <vector>  
 #include <mutex>  
 #include <windows.h>  
 using namespace std;  
 DWORD WINAPI threadProc(LPVOID p)  
 {  
      HANDLE process = GetCurrentProcess();  
      DWORD_PTR processAffinityMask = (1 << 1 | 1 << 2);  //use processor 1 and 2
      BOOL success = SetProcessAffinityMask(process, processAffinityMask);  
      clock_t t = clock() + 100; // consume 100ms ...  
      while (clock() < t) {}       // of CPU time  
      printf("thread %d:\tlogical CPU %d\tProcessId %d\n", (int)p, GetCurrentProcessorNumber(), GetCurrentProcessId());  
      return 0;  
 }  
 int main()  
 {  
      unsigned num_cpus = std::thread::hardware_concurrency();  //retrieve the number of processor
      std::cout << "Launching " << num_cpus << " threads\n";  
      for (int i = 0; i < num_cpus; i++)  
      {  
           HANDLE h = CreateThread(NULL, 0, threadProc, (LPVOID)i, 0, NULL);  
      }  
      getchar();  
   return 0;  
 }  

20 November 2018

C++ generic function pointer example / use (void*) as object pointer

 #include "stdafx.h"  
 #include <iostream>  
 #include <functional>  
 using namespace std;  
 class Point  
 {  
 public:  
      Point(double _x, double _y) :x(_x), y(_y) {};  
      double x;  
      double y;  
 };  
 typedef std::function<void*(void*)> genericFunc;  
 void * func1(void * argv) {  
      double * d = (double *)argv;  
      double x = d[0];  
      double y = d[1];  
      Point p(x, y);  
      Point* pp = &p;  
      return (void *)pp;  
 }  
 void * func2(void * argv) {  
      double * d = (double *)argv;  
      double x = d[0];  
      double y = d[1];  
      double z = d[2];  
      double R = x * y * z;  
      return (void *)&R;  
 }  
 void * TestFunc(genericFunc f, void* p)  
 {  
      return f(p);  
 }  
 int main()  
 {  
      double argv[]{ 1.2, 2.3, 3.4 };  
      Point p = * ( (Point*) TestFunc(&func1, (void*)argv) );  
      double d = *((double*)TestFunc(&func2, (void*)argv));  
      cout << "p:" << p.x << "," << p.y << ", d:" << d << endl;  
      getchar();  
   return 0;  
 }  

C++ pass member function pointer as function parameter example

 #include "stdafx.h"  
 #include <vector>  
 #include <iostream>  
 #include <functional>  
 #include <vector>  
 #include <string>  
 using namespace std;  
 typedef std::function<double(int, int)> funcIntToDouble;  
 class parent;  
 class child  
 {  
 public:  
      double childValue;  
      child()  
      {  
           childValue = 10.5;  
      }  
      double InChildFunc(funcIntToDouble f, int x, int i)  
      {  
           double d = f(x, i) + childValue;  
           cout << endl << "InChildFunc: " << d << endl;  
           return d;  
      }  
 };  
 class parent  
 {  
 public:  
      int y;  
      child childObject;  
      vector<double> vec{ 1.1, 2.2, 3.3 };  
      parent()  
      {  
           y = 10.0;  
      }  
      double ParentFunc(int x, int i)  
      {  
           double d = (double)x * y + vec[i];  
           return d;  
      }  
      void CallChildObject(int parentX, int parentI)  
      {  
           funcIntToDouble f = [&](int x, int i) {  
                double d = ParentFunc(x, i);  
                return d;  
           };  
           double r = childObject.InChildFunc(f, parentX, parentI);  
           cout << endl << "CallChildObject: " << r << endl;  
      }  
 };  
 int main()  
 {  
      parent p;  
      p.CallChildObject(10, 1);  
      getchar();  
      return 0;  
 }  

17 November 2018

C++ passing vector as shared_ptr example

 #include <functional>  
 using namespace std;  
 void Test(shared_ptr<vector<int>> v)  
 {  
      vector<int> & r_v = *(v.get());  
      cout << r_v[1];  
 }  
 int main()  
 {  
      shared_ptr<vector<int>> a = make_shared<vector<int>>(std::vector<int>{ 1, 2, 3 });  
      Test(a);  
      getchar();  
      return 0;  
 }  

Function Pointer Object example in C++

 using namespace std;  
 typedef std::function<double(int)> funcIntToDouble;  
 void Test(funcIntToDouble f, int x)  
 {  
      double b = f(x);  
      cout << b;  
 }  
 int main()  
 {  
      cout << "function object example" << endl;  
      int a = 10;  
      funcIntToDouble f = [&](int x) {  
           double r = (double)(x + a) / 10; //able use use variable a from the calling function  
           return r;  
      };  
      Test(f, 15); //pass the function pointer along with value from variables in scope  
      getchar();  
   return 0;  
 }