libfaster API Documentation  Development Version
Super fast distributted computing
fexample-int-vector.cpp
1 #include <iostream>
2 #include <vector>
3 #include "libfaster.h"
4 
5 #define NUMITEMS 10*1000
6 
7 using namespace std;
8 using namespace faster;
9 
10 // Multiply every array element by 2
11 vector<int> map1(vector<int> & input){
12  vector<int> output(input.size());
13 
14  //multiply the entire vector by 2
15  for ( size_t i = 0; i < input.size(); ++i ){
16  output[i] = input[i] / 2;
17  }
18 
19  return output;
20 
21 }
22 
23 // Sums the values in the respective positions of A and B
24 vector<int> reduce1(vector<int> & a, vector<int> & b){
25  size_t oSize = max(a.size(), b.size());
26  vector<int> out(oSize);
27 
28  for ( size_t i = 0; i < oSize; ++i ){
29  if ( i < a.size()){
30  if( i < b.size()){
31  out[i] = a[i] + b[i];
32  }else{
33  out[i] = a[i];
34  }
35  }else{
36  if( i < b.size()){
37  out[i] = b[i];
38  }
39  }
40  }
41  return out;
42 }
43 
44 
45 int main(int argc, char ** argv){
46  // Init Faster Framework
47  cout << "Init FastLib" << '\n';
48  fastContext fc(argc,argv);
49 
50  fc.registerFunction((void*) &map1);
51  fc.registerFunction((void*) &reduce1);
52 
53  fc.startWorkers();
54  if (!fc.isDriver())
55  return 0;
56 
57  cout << "Generate Data" << '\n';
58  vector<int> rawdata[NUMITEMS];
59 
60  // Create a random adjacency matrix
61  for ( size_t i = 0; i < NUMITEMS; ++i ){
62 
63  // Random number of items
64  size_t dataSize = (rand() % 10) + 1;
65 
66  rawdata[i].assign(dataSize, 2);
67  }
68 
69  cout << "Import Data" << '\n';
70  fdd <vector<int>> data(fc, rawdata, NUMITEMS);
71 
72  cout << "Process Data" << '\n';
73  vector<int> result = data.map<vector<int>>(&map1)->reduce(&reduce1);
74 
75  cout << "DONE!" << '\n';
76 
77  for ( size_t i = 0; i < result.size(); ++i){
78  cout << result[i] << ' ';
79  }
80  cout << '\n';
81 
82 
83  return 0;
84 }
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
Fast Distributted Dataset(FDD) is like a cluster distributted Array. This class is the user side impl...
Definition: fastContext.h:24
libfaster main namespace
Definition: _workerFdd.h:11