Tuesday, February 10, 2009

Parallel Patterns for C++ Programmer

In Amino project, we've created several experimental parallel patterns in C++. There are already advanced parallel patterns for Java programmers, such as Fork/Join from Doug Lea. But things are so ready for C++ world.

Under the current release of Amino library project, we have created three parallelized version of existing patterns from STL. The method signature is very close to the original one:


  1. Foreach

  2. Pattern Usage Computing Kernel

    vector<int> dataV;

    ThreadPoolExecutor exec;

    for_each(exec, 2, dataV.begin(), dataV.end(), sum);
    exec.shutdown();
    exec.waitTermination();


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


  3. Transform

  4. Pattern Usage Computing Kernel
    UnaryFunc<int> uf;
    vector<int> dataV;

    int i = 0;

    for ( ; i1);
    }

    ThreadPoolExecutor exec;

    // change each elemet to its twice
    transform(exec, 2, dataV.begin(), dataV.end(), dataV.begin(), uf);
    exec.shutdown();
    exec.waitTermination();


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


  5. Accumulate

  6. Pattern Usage Computing Kernel
    vector<int> dataV;

    // Test the function 1
    int result = accumulate<int>::iterator, int, ThreadPoolExecutor>(exec,
    2, dataV.begin(), dataV.end());
    exec.shutdown();
    exec.waitTermination();


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




Please note these patterns are in pretty early stage. The performance is still ridiculous now. Please let us know your opinion about the API design. And contributions are always welcome!

No comments: