Amino C++ Template Library Examples

Introduction

The following examples will show you how to use the C++ interface of the Aimino library.

Example1

This example shows how to use the for_each pattern in Amino.

#include <iostream>

#include <amino/foreach.h>
#include <amino/thread.h>
#include <amino/ftask.h>
#include <amino/tp_exec.h>

#include <sys/time.h>
#include <unistd.h>

using namespace amino;

// global variable for sum result
int result = 0;

void 
sum (int n)
{
    result += n;
}

const static int NUM = 100;

int 
main(int argc, char * argv[]){
    vector<int> dataV;

    int i = 0;

    for ( ; i<NUM; i++) {
        dataV.push_back(i+1);        
    }
    
    ThreadPoolExecutor exec;
    
    vector<int>::iterator it1, it2;
    it1 = dataV.begin();
    it2 = dataV.end();

    struct timeval t1, t2;
    gettimeofday(&t1, NULL);

    for_each(exec, 2, it1, it2, sum);
    exec.shutdown();
    exec.waitTermination();
    gettimeofday(&t2, NULL);

    // calculate the computing time
    int takes1 = (t2.tv_sec - t1.tv_sec) * 1000000 + t2.tv_usec;
    
    cout<<"The execution time is: "<<takes1<<" microseconds!"<<endl;

    cout<<"result:"<<result<<endl;

    return 0;
}

for_each.cpp

Go to top

Example2

This example shows how to use the accumulate pattern in Amino.

#include <string>
#include <cctype>
#include <amino/cstdatomic>
#include <amino/thread.h>
#include <amino/tp_exec.h>
#include <amino/ftask.h>
#include <amino/accumulate.h>

#include <vector>
#include <iostream>
#include <assert.h>
#include <unistd.h>
#include <sys/time.h>

using namespace amino;

template<typename ParaType>
class UnaryFunc {
public:
    ParaType operator()(ParaType element) {
        return 2 * element;
    }
};

const static int NUM = 100;

int 
main(int argc, char* argv[])
{
    //UnaryFunc<int> uf;
    vector<int> dataV;

    int i = 0;

    for ( ; i<NUM; i++) {
        dataV.push_back(i+1);        
    }
    
    ThreadPoolExecutor exec;
    
    vector<int>::iterator it1, it2;
    it1 = dataV.begin();
    it2 = dataV.end();

    struct timeval t1, t2;
    gettimeofday(&t1, NULL);

    // Test the function 1
    int result = accumulate<vector<int>::iterator, int, ThreadPoolExecutor>(exec, 2, it1, it2);
    exec.shutdown();
    exec.waitTermination();
    gettimeofday(&t2, NULL);

    int takes1 = (t2.tv_sec - t1.tv_sec) * 1000000 + t2.tv_usec;
    

    // Test the function 2
    
    cout<<"The execution time is: "<<takes1<<" microseconds!"<<endl;
 
    cout<<"Result: "<<result<<endl;

    return 0;
}

accumulate.cpp

Go to top

Example3

This example shows how to use the transform pattern in Amino.

#include <string>
#include <cctype>
#include <amino/cstdatomic>
#include <amino/thread.h>
#include <amino/tp_exec.h>
#include <amino/ftask.h>
#include <amino/transform.h>

#include <vector>
#include <iostream>
#include <assert.h>
#include <unistd.h>
#include <sys/time.h>

using namespace amino;

template<typename ParaType>
class UnaryFunc {
public:
    ParaType operator()(ParaType element) {
        return 2 * element;
    }
};

// The total number of the vector
const static int NUM = 100;

int 
main(int argc, char* argv[])
{
    UnaryFunc<int> uf;
    vector<int> dataV;

    int i = 0;

    for ( ; i<NUM; i++) {
        dataV.push_back(i+1);        
    }
    
    ThreadPoolExecutor exec;
    
    vector<int>::iterator it1, it2;
    it1 = dataV.begin();
    it2 = dataV.end();

    struct timeval t1, t2;
    gettimeofday(&t1, NULL);

    // change each elemet to its twice
    transform(exec, 2, it1, it2, it1, uf);
    exec.shutdown();
    exec.waitTermination();
    gettimeofday(&t2, NULL);

    int takes1 = (t2.tv_sec - t1.tv_sec) * 1000000 + t2.tv_usec;
    
    cout<<"The execution time is: "<<takes1<<" microseconds!"<<endl;
 
    i = 0;
    for (; i<NUM; i++) {
        cout<<dataV[i]<<endl;
    }

    return 0;
}

transform.cpp

Go to top

Example4

This example shows how to use the single executor in Amino.

#include <iostream>

#include <amino/foreach.h>
#include <amino/thread.h>
#include <amino/ftask.h>
#include <amino/single_exec.h>

#include <sys/time.h>
#include <unistd.h>

using namespace amino;

class TestRunnable:public Runnable {
    private:
        int fNum;

    public:
        TestRunnable(int num):fNum(num){}

        void * run() {
            int i = 0;
            int j = 0;

            for ( ; i<fNum; i++) {
                j = 0;
                for ( ; j<fNum; j++) {
                    cout<<"I'm running!"<<endl;
                }
            }

            return NULL;
        }
};


int 
main(int argc, char * argv[]) {
    SingleExecutor sexec;
    TestRunnable trun(10);

    struct timeval t1, t2;
    gettimeofday(&t1, NULL);

    sexec.execute(&trun);
    sexec.shutdown();
    sexec.waitTermination();
    
    gettimeofday(&t2, NULL);

    // calculate the computing time
    int takes1 = (t2.tv_sec - t1.tv_sec) * 1000000 + t2.tv_usec;
    
    cout<<"The execution time is: "<<takes1<<" microseconds!"<<endl;

    return 0;
}

single_exec.cpp

Go to top

Example5

This example shows how to use the thread pool executor in Amino.

#include <iostream>

#include <amino/foreach.h>
#include <amino/thread.h>
#include <amino/ftask.h>
#include <amino/tp_exec.h>

#include <sys/time.h>
#include <unistd.h>

using namespace amino;

class TestRunnable:public Runnable {
    private:
        int fNum;

    public:
        TestRunnable(int num):fNum(num){}

        void * run() {
            int i = 0;
            int j = 0;

            for ( ; i<fNum; i++) {
                j = 0;
                for ( ; j<fNum; j++) {
                    cout<<"I'm running!"<<endl;
                }
            }

            return NULL;
        }
};


int 
main(int argc, char * argv[]) {
    // Create the a thread pool executor with 2 threads
    ThreadPoolExecutor tpexec(2);
    TestRunnable trun(30);
    FutureTask ftask(&trun);

    struct timeval t1, t2;
    gettimeofday(&t1, NULL);

    tpexec.execute(&ftask);
    
    ftask.get();

    tpexec.shutdown();
    tpexec.waitTermination();
    
    gettimeofday(&t2, NULL);

    // calculate the computing time
    int takes1 = (t2.tv_sec - t1.tv_sec) * 1000000 + t2.tv_usec;
    
    cout<<"The execution time is: "<<takes1<<" microseconds!"<<endl;

    return 0;
}

tp_exec.cpp

Go to top

Next Step