libfaster API Documentation  Development Version
Super fast distributted computing
fexample-indexed.cpp
1 
2 #include <iostream>
3 #include "libfaster.h"
4 
5 #define NUMITEMS (100*1000)
6 
7 using namespace std;
8 using namespace faster;
9 
10 //#pragma omp declare target
11 
12 pair<int,int> map1(const int & key, int & input){
13  pair<int,int> result (key, 2*input);
14 
15  return result;
16 }
17 //#pragma omp end declare target
18 
19 
20 pair<int,int> reduce1(const int & keyA, int &a, const int & keyB, int &b){
21  pair<int,int> result ((keyA + keyB), (a+b));
22 
23  return result;
24 }
25 
26 template <typename K>
27 void printHistogram(const std::unordered_map<K, size_t> & hist ){
28  for( auto it = hist.begin(); it != hist.end(); it++){
29  cout << it->first << "\t";
30  }
31  cout << "\n ";
32  for( auto it = hist.begin(); it != hist.end(); it++){
33  cout << it->second << "\t";
34  }
35  cout << "\n ";
36 }
37 
38 int main(int argc, char ** argv){
39  // Init Faster Framework
40  cout << "Init FastLib" << '\n';
41  fastContext fc(argc,argv);
42 
43  fc.registerFunction((void*) &map1);
44  fc.registerFunction((void*) &reduce1);
45 
46  fc.startWorkers();
47  if (!fc.isDriver())
48  return 0;
49 
50  cout << "Generate Data" << '\n';
51  int rawdata[NUMITEMS];
52  int rawKeys[NUMITEMS];
53 
54  for ( int i = 0; i < NUMITEMS; ++i ){
55  rawKeys[i] = 1 + rand() % 10;
56  rawdata[i] = 1 + rand() % 100;
57  }
58 
59  cout << "Import Data" << '\n';
60  indexedFdd <int,int> data(fc, rawKeys, rawdata, NUMITEMS);
61 
62  cout << "Key Histogram\n";
63  printHistogram(data.countByKey());
64 
65  cout << "Process Data" << '\n';
66  pair<int,int> result = data.map<int,int>(&map1)->reduce(&reduce1);
67 
68  cout << "DONE!" << '\n';
69 
70  std::cout << "Result: " << result.first/NUMITEMS << ", " << result.second/NUMITEMS << "\n";
71 
72  return 0;
73 }
void startWorkers()
Start worker machines computation.
Definition: fastContext.cpp:58
STL namespace.
void registerFunction(void *funcP)
Register a user custom function in the context.
Definition: fastContext.cpp:48
bool isDriver()
Checks for the driver process.
Definition: fastContext.cpp:76
Framework context class.
Definition: fastContext.h:66
libfaster main namespace
Definition: _workerFdd.h:11