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-09-01 19:07:03

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

Parallelising existing code: multiple SPMD areas

Sometimes only specific parts of an application may be worthwhile to parallelise, and it is not cost-effective to re-write the entire application as a single BSP program (although it is in general the right thing to do). MulticoreBSP for C does support multiple SPMD regions in a single code, thus making it possible to write BSP versions of only the compute-intensive highly-parallelisable parts of your application. It works by repeated application of bsp_init and bsp_begin; for example, the following code

#include "mcbsp.h"

#include <stdio.h>
#include <stdlib.h>

void spmd1() {
        bsp_begin( 2 );
        printf( "Hello world from thread %d!\n", bsp_pid() );
        bsp_end();
}

void spmd2() {
        bsp_begin( bsp_nprocs() );
        printf( "Hello world from thread %d!\n", bsp_pid() );
        bsp_end();
}

int main() {
        printf( "Sequential part 1\n" );

        bsp_init( &spmd1, 0, NULL );
        spmd1();

        printf( "Sequential part 2\n" );

        bsp_init( &spmd2, 0, NULL );
        spmd2();

        printf( "Sequential part 3\n" );

        return EXIT_SUCCESS;
}

produces a variant of

Sequential part 1
Hello world from thread 1!
Hello world from thread 0!
Sequential part 2
Hello world from thread 3!
Hello world from thread 2!
Hello world from thread 0!
Hello world from thread 1!
Sequential part 3

(on a quadcore machine). Note the order of the `Hello world' lines may differ. This may also serve as a way to gradually transform a large C code into BSP form. MulticoreBSP does incur an overhead of thread creation and initialisation every time bsp_begin is encountered, so be sure each SPMD areas indeed constitutes enough work to amortise this cost.

Offline

Board footer

Powered by FluxBB