The MulticoreBSP Forums

A place to discuss the MulticoreBSP library and its applications, and for discussing the use of the Bulk Synchronous Parallel model on shared-memory architectures, or hybrid distributed/shared architectures.

You are not logged in.

#1 2012-11-17 19:48:31

Albert-Jan Yzelman
Moderator
Registered: 2011-08-04
Posts: 32

MulticoreBSP in C++: basic usage

Since MulticoreBSP is now available in C, you can use it from within C++ programs by simply including the mcbsp.h header file in the usual way.

This requires you to call C-style functions from object-oriented code, and this isn't always elegant In particular, having to start an SPMD section by pointing to a global function completely breaks the object-oriented style. To cope with this, MulticoreBSP for C (from version 1.0.1 on) includes a C++-wrapper that objectifies SPMD programs. This class is defined in the mcbsp.hpp header file; including this file also defines all regular BSP primitives (that is, there is no need to include the C-header mcbsp.h).

The C++ header mcbsp.hpp defines the mcbsp namespace which contains the abstract BSP_program class. An SPMD section making use of the MulticoreBSP library may simply extend this class, and then must implement BSP_program's purely virtual methods:

  • virtual void spmd() = 0;

  • virtual BSP_program* newInstance() = 0;

The first method is the entry point of the SPMD code. Each thread involved in execution of this SPMD code has its own instance of the final BSP_program. The second method ensures that the C++-wrapper can create new instances of the user-defined class when it wants to supply a new thread with its own instance of that class. Finally, BSP_program defines only one other public function:

  • void begin( unsigned int P = bsp_nprocs() );

Calling this function will start parallel execution of the SPMD code over a user-supplied number of P processors. If P is not supplied, the maximum available number of processors is used (see the description of bsp_nprocs()). The use of this class deprecates the use of bsp_end(), and bsp_init( ... ); these are implied and handled by the C++-wrapper. The use of bsp_begin( unsigned int P ) is replaced by the use of BSP_program::begin( unsigned int P ).

An object-oriented parallel `Hello world'-example now looks as follows:

#include "mcbsp.hpp"

#include <cstdlib>
#include <iostream>

using namespace mcbsp;

class Hello_World: public BSP_program {

        protected:

                virtual void spmd() {
                        std::cout << "Hello world from thread " << bsp_pid() << std::endl;
                }

                virtual BSP_program * newInstance() {
                        return new Hello_World();
                }

        public:

                Hello_World() {}
};

int main() {
        BSP_program *p = new Hello_World();
        p->begin( 2 );
        p->begin();
        delete p;
        return EXIT_SUCCESS;
}

The example demonstrates calling BSP_program::begin( ... ) with and without parameters. Running on a quad-core computer, example output is:

Hello world from thread Hello world from thread 01

Hello world from thread 3
Hello world from thread 2
Hello world from thread 0
Hello world from thread 1

The order of the Hello-world printouts might change between runs, and `fusion' of output streams (like with the first run on 2 threads) may or may not occur in repeated runs.

Any communication between threads still follows the usual C-style functions on contiguous in-memory bytes; the C++-wrapper does not supply object-oriented communication constructs (at present).

Offline

Board footer

Powered by FluxBB