Dvbmonkey’s Blog

March 3, 2009

Open MPI Master & Servant Example – BogoMips

Filed under: linux — dvbmonkey @ 12:44 pm
Tags: , ,

In yesterdays post I introduced a simple ‘master & servant’ technique where I used the rank-0 node to collate results from all the other nodes. To do this I used the methods MPI_Send and MPI_Recv to send/recv 128-byte MPI_CHAR strings. Today I am extending the example by sending/receiving MPI_FLOAT‘s to demonstrate that native C/C++ numerical values can be passed between nodes easily.

What are BogoMips?

From the Wikipedia article, BogoMips are an “unscientific measurement of CPU speed made by the Linux kernel when it boots, to calibrate an internal busy-loop”. If you’ve used Linux for some time you may have noticed the “BogoMips” value seen during the boot-up console messages. Alternatively, you can cat /proc/cpuinfo to see the values your Linux Kernel has calculated during boot.

Example: bogomips.c

Based on the Linux kernel code in init/main.c and include/linux/delay.h and the example ‘Standalone BogoMips’ code by Jeff Tranter. Here is a really simple ‘MPI BogoMips’ calculation, where each node takes an average of 10 BogoMips calculations for itself and MPI_Send‘s the result to the rank-0 node which sum’s them up and prints the total.

/*
 * Based on code Linux kernel code in init/main.c and include/linux/delay.h
 * and the example code by Jeff Tranter (Jeff_Tranter@Mitel.COM)
 */

#include 
#include 
#include 

// #define PORTABLE_BOGOMIPS
#define CLASSIC_BOGOMIPS

#ifdef CLASSIC_BOGOMIPS
    /* the original code from the Linux kernel */
    int HZ = 100;

    #define rdtscl(low) \
         __asm__ __volatile__ ("rdtsc" : "=a" (low) : : "edx")

    //This delay() is the one used on x86's with TSC after 2.2.14.
    //It won't work on a non TSC x86, period.
    void __inline__ delay(unsigned long loops)
    {
        unsigned long bclock, now;
        rdtscl(bclock);
        do {
    	rdtscl(now);
        }
        while ((now - bclock) < loops);
    }
#endif

#ifdef PORTABLE_BOGOMIPS
    /* portable version */
    static void delay(int loops)
    {
        long i;
        for (i = loops; i >= 0; i--);
    }
#endif

/* this should be approx 2 Bo*oMips to start (note initial shift), and will
 *    still work even if initially too large, it will just take slightly longer */
unsigned long loops_per_jiffy = (1 << 12);

/* This is the number of bits of precision for the loops_per_jiffy.  Each
 *    bit takes on average 1.5/HZ seconds.  This (like the original) is a little
 *       better than 1% */
#define LPS_PREC 8

int numprocs, rank, namelen;
char processor_name[MPI_MAX_PROCESSOR_NAME];

//plagiarized straight from the 2.4 sources.
float calibrate_delay(void)
{
    unsigned long ticks, loopbit;
    int lps_precision = LPS_PREC;
    loops_per_jiffy = (1 << 12);
    while (loops_per_jiffy <<= 1) {
	ticks = clock();
	while (ticks == clock())
	    /* nothing */ ;
	ticks = clock();
	delay(loops_per_jiffy);
	ticks = clock() - ticks;
	if (ticks)
	    break;
    }
    loops_per_jiffy >>= 1;
    loopbit = loops_per_jiffy;
    while (lps_precision-- && (loopbit >>= 1)) {
	loops_per_jiffy |= loopbit;
	ticks = clock();
	while (ticks == clock());
	ticks = clock();
	delay(loops_per_jiffy);
	if (clock() != ticks)
	    loops_per_jiffy &= ~loopbit;
    }
    return (loops_per_jiffy / (500000/HZ)) + (float)((loops_per_jiffy/(5000/HZ))%100) / (float)100;
}

int main(int argc, char *argv[])
{
    unsigned long loops_per_sec = 1;
    unsigned long ticks;
    int i;

    MPI_Status stat;
    MPI_Init(&argc, &argv);
    MPI_Comm_size(MPI_COMM_WORLD, &numprocs);
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    MPI_Get_processor_name(processor_name, &namelen);

    float bogomips = 0;
    for ( i = 0; i < 9; i++ ) {
        bogomips += calibrate_delay();
    }
    bogomips = bogomips / (float) 10;

    printf( "[%02d/%02d %s] returned = %f BogoMips\n", rank, numprocs, processor_name, bogomips );

    if ( rank == 0 ) {
        float totalBogomips = bogomips;
        for ( i = 1; i < numprocs; i++ ) {
            float f = 0;
            MPI_Recv(&f, 1, MPI_FLOAT, i, 0, MPI_COMM_WORLD, &stat);
            totalBogomips += f;
        }
        printf( "Total = %f BogoMips\n", totalBogomips );
    } else {
        MPI_Send(&bogomips, 1, MPI_FLOAT, 0, 0, MPI_COMM_WORLD);
    }

    MPI_Finalize();
    return 0;
}

The result of running this should look something like this:

[00/18 mpinode01] 2293.593018 BogoMips
[01/18 mpinode01] 2147.446045 BogoMips
[02/18 mpinode01] 2230.513916 BogoMips
[03/18 mpinode01] 2473.651855 BogoMips
[04/18 mpinode02] 3659.688721 BogoMips
[05/18 mpinode02] 4057.167236 BogoMips
[06/18 mpinode02] 4067.651123 BogoMips
[07/18 mpinode02] 4419.580078 BogoMips
[08/18 mpinode03] 2368.138916 BogoMips
[09/18 mpinode03] 3327.585938 BogoMips
[10/18 mpinode03] 3277.451904 BogoMips
[11/18 mpinode03] 3130.323975 BogoMips
[12/18 mpinode04] 2940.759766 BogoMips
[13/18 mpinode04] 3207.983154 BogoMips
[14/18 mpinode04] 4362.892090 BogoMips
[15/18 mpinode04] 3313.822998 BogoMips
[16/18 mpinode05] 2390.749023 BogoMips
[17/18 mpinode05] 3017.437012 BogoMips
Total = 56686.441406 BogoMips

🙂

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: