Archive for September, 2009

Intel TBB Reduction Code Snippet

ParallelReduction.h

#ifndef _JB_PARRED_H_
#define _JB_PARRED_H_

#include "tbb/parallel_reduce.h"
#include "tbb/blocked_range.h"

using namespace tbb;

struct SumInt {
 int value;
  SumInt();
  SumInt(SumInt &s, split);
  void operator() (const blocked_range<int*>& range);
  void join( SumInt &rhs);
};

int ParallelSumInt( int array[], size_t n );

struct ProductInt {
  int value;
  ProductInt();
  ProductInt(ProductInt &s, split);
  void operator() (const blocked_range<int*>& range);
  void join( ProductInt &rhs);
};

unsigned int ParallelProductInt(int array[], size_t n);

#endif

ParallelReduction.cc

#include "tbb/parallel_reduce.h"
#include "tbb/blocked_range.h"

#include "ParallelReduce.h"

using namespace tbb;

SumInt::SumInt() : value(0)  {}
SumInt::SumInt(SumInt &s, split) {
  value = 0 ;
}

void SumInt::operator() (const blocked_range<int*>& range) {
  int temp = value;
  for( int* a=range.begin(); a!=range.end(); ++a) {
    temp += *a;
  }
  value = temp;
}

void SumInt::join( SumInt &rhs) {
  value += rhs.value;
}

int ParallelSumInt( int array[], size_t n ) {
  SumInt total;
  parallel_reduce(blocked_range<int*> (array, array+n, 1000), total);
  return total.value;
}

ProductInt::ProductInt() : value(1) {}
ProductInt::ProductInt(ProductInt &s, split) {
    value = 1;
}

void ProductInt::operator() (const blocked_range<int*>& range) {
  int temp = value;
  for( int* a=range.begin(); a!=range.end(); ++a) {
    temp *= *a;
  }
  value = temp;
}

void ProductInt::join( ProductInt &rhs) {
  value *= rhs.value;
}

unsigned int ParallelProductInt(int array[], size_t n) {
  ProductInt total;
  parallel_reduce(blocked_range<int*>(array, array+n, 1000), total);
  return total.value;
}

BenchParallelReduce.cc

#include “ParallelReduce.h”

#include “tbb/task_scheduler_init.h”
#include “tbb/tick_count.h”

#include <iostream>
#include <cstdlib>

using namespace std;
using namespace tbb;

void usage() {
cout << “BenchParallelReduce threads? “ << endl;
}

int main(int argc, char**argv) {

task_scheduler_init init(task_scheduler_init::deferred);
int p = 0;
if ( argc == 2 ) {
p = atoi(argv[1]);
}
else if ( argc > 2 ) {
usage(); exit(-1);
}
if ( p != 0 ) { init.initialize(p); }
else {
p = task_scheduler_init::default_num_threads();
init.initialize(p);
}

const int size = 100000000;
int *array = new int[size];
for(int i = 0 ; i < size - 1 ;i++ ) {
array[i] = i;
}

tick_count t0 = tick_count::now();
int res = ParallelSumInt(array, size);
tick_count t1 = tick_count::now();
cout << “Result: “ << res << endl;
cout << “Takes “ << (t1-t0).seconds() << ” seconds” << endl;
cout << “Thread(s) “ << p << endl;

return 0;
}

Files

ParallelReduce.h

ParallelReduce.cc

BenchParallelReduce.cc

No Comments

Irrlicht code snippet: slow down FPS

A simple code snippet to slow down frame rate and slow down CPU usage.

#include <irrlicht.h>
#include <iostream>
using namespace irr;

struct IrrlichtDelayFPS {
  int maxFPS;
  int lastFPS;
  int yieldCounter;
  IrrlichtDevice *device;
  IrrlichtDelayFPS() { maxFPS = 50;  yieldCounter = 0;  device = 0; }
  void delay() {
    video::IVideoDriver* driver = device->getVideoDriver();
    int lastFPS = driver->getFPS();
    for( int i = 0 ; i < yieldCounter ; i++ ) {
      device->yield();
    }
    if ( lastFPS >maxFPS ) { yieldCounter++ ; }
    else {
      if (yieldCounter > 0 ) { --yieldCounter; }
      else { yieldCounter = 0 ; }
    }
  }
};

/* 

  IrrlichtDelayFPS delayFPS;
  delayFPS.maxFPS = 50; //set max FPS
  delayFPS.device = device; //set device

while(device->run()) {
   //your code here
  //delay loop
  delayFPS.delay();
}

*/
IrrlichtFPS

No Comments