Archive for category Devel

Low constrast theme

As many developers, my eyes suffer from many hours spent on the screen. I like dark and colored syntax highlighting, but for my eyes this kind of contrast is another story.

I googled “syntax highlighting eyes” and found the Zenburn theme (official page http://slinky.imukuppi.org/zenburn/).

I have done a small adaptation to Xemacs from the Emacs theme. It can be found here (https://github.com/dbrock/zenburn-el/blob/master/zenburn.el)

Zenburn screenshot

 

The custom.el file is here (http://julien.boucaron.free.fr/xemacs/custom.el_ZENBURN)

No Comments

Qt links

CYGWIN: Build instruction and patch for 4.6.2

http://wiki.lyx.org/LyX/LyXOnCygwin

No Comments

Cross compilers for Solaris 2.8

Using the following URL: http://www.cis.upenn.edu/~milom/cross-compile.html
I setup a bash shell script to build a cross compiler tool chain under Linux for Solaris 8: gcc 3.4.6 and 4.4.3.
You need to create a sysroot using a Solaris 8 hosts for includes and libs.

Here is a tiny patch for gcc 3.4.6 (gcc/collect2.c):

1537c1537
<       redir_handle = open (redir, O_WRONLY | O_TRUNC | O_CREAT, S_IRWXU);

>       redir_handle = open (redir, O_WRONLY | O_TRUNC | O_CREAT);

The script is as follow:

FTPMIRROR=ftp.gnu.org
FTPPATH=gnu
WGET=wget

BINUTILS=binutils-2.20.1
GCC3=gcc-3.4.6
GCC4=gcc-4.4.3
TARSUFFIX=.tar.gz

TAR=tar
GUNZIP=gunzip

SYSROOT=SYSROOT.tar
TARGET=sparc-sun-solaris2.8
PREFIX=/home/boucaron/cross/solaris8

function check {
    if  which ${WGET}
	then
	echo "WGET found"
	else
	echo "WGET is missing, we need WGET"
	exit
    fi
    if which ${TAR}
	then
	echo "TAR found"
	else
	echo "TAR is missing, we need TAR"
	exit
    fi
    if which ${GUNZIP}
	then
	echo "GUNZIP found"
	else
	echo "GUNZIP is missing, we need GUNZIP"
	exit
    fi
}

function download {
    if [ -f ${BINUTILS}${TARSUFFIX} ]
	then
	 echo "${BINUTILS}${TARSUFFIX} present"
	else
	    ${WGET} ftp://${FTPMIRROR}/${FTPPATH}/binutils/${BINUTILS}${TARSUFFIX}

    fi
    if [ -f ${GCC3}${TARSUFFIX} ]
	then
	echo "${GCC3}${TARSUFFIX} present"
	else
	${WGET} ftp://${FTPMIRROR}/${FTPPATH}/gcc/${GCC3}/${GCC3}${TARSUFFIX}

    fi
    if [ -f ${GCC4}${TARSUFFIX} ]
	then
	echo "${GCC4}${TARSUFFIX} present"
	else
	${WGET} ftp://${FTPMIRROR}/${FTPPATH}/gcc/${GCC4}/${GCC4}${TARSUFFIX}
    fi
}

function decompress {
    if [ ! -d ${BINUTILS} ]
	then
	${TAR} xfz ${BINUTILS}${TARSUFFIX}
    fi
    if [ ! -d ${GCC3} ]
	then
	${TAR} xfz ${GCC3}${TARSUFFIX}
    fi
    if [ ! -d ${GCC4} ]
	then
	${TAR} xfz ${GCC4}${TARSUFFIX}
    fi
}

function create_build_dirs {
    mkdir -p binutils-build
    mkdir -p gcc3-build
    mkdir -p gcc4-build
}

function sysroot {
    if [ -d SYSROOT ]
	then
	echo "SYSROOT present"
    else
	if [ -f ${SYSROOT} ]
	    then
	    ${TAR} -xf ${SYSROOT}
	else
	    echo "Missing sysroot file"
	fi
    fi
}

function build_prefix {
    mkdir -p ${PREFIX}
}

function build_binutils {
    echo "Building binutils"
    cd binutils-build && \
	./../${BINUTILS}/configure -target=${TARGET} --prefix=${PREFIX} -with-sysroot=../SYSROOT -v  && \
	make && \
	make install && \
	cd ..
    export PATH=${PREFIX}/bin:${PATH}
}

function build_gcc3 {
    echo "Building gcc3"
    cd gcc3-build && \
	./../${GCC3}/configure  --target=${TARGET} --with-gnu-as --with-gnu-ld  --prefix=${PREFIX} -with-sysroot="`pwd`/../SYSROOT" --disable-libgcj --disable-gomp --disable-nls --enable-languages=c,c++ -v && \
#	cp -f ../gcc-3.4.6-gcc-collect2.c ../gcc-3.4.6/gcc/collect2.c && \
	make all && \
	make install && \
	cd ..
}

function build_gcc4 {
    echo "Building gcc4"
     cd gcc4-build && \
	./../${GCC4}/configure --target=${TARGET} --with-gnu-as --with-gnu-ld  --prefix=${PREFIX} -with-sysroot="`pwd`/../SYSROOT" --disable-libgcj --disable-libgomp --disable-nls --enable-languages=c,c++ -v && \
	make all && \
	make install && \
	cd ..
}

function main {
    check
    download
    decompress
    create_build_dirs
    sysroot
    build_binutils
    build_gcc3
    build_gcc4
}
main

syntax highlighted by Code2HTML, v. 0.9.1

No Comments

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

Cross compiling lpsolve under linux for win32

Here is the attached Makefile (no other patch)

<<

#
# Makefile for cross-compiling lp_solve using GNU Make
# Usage:
#   make -f Makefile.MinGW [all | bin | lib | clean]
#
# $Revision: 1.1 $
#

AR = i586-mingw32msvc-ar
ARFLAGS = rv
CC = i586-mingw32msvc-gcc
LEX = flex
YACC = bison

INCLUDE = -I. -Ibfp -Ibfp/bfp_LUSOL -Ibfp/bfp_LUSOL/LUSOL -Icolamd -Ishared

# Uncomment any of the following two depending on your profile
#DEBUG = -g -pg
DEBUG = -O2

DEFINE = -DYY_NEVER_INTERACTIVE -DPARSER_LP -DINVERSE_ACTIVE=INVERSE_LUSOL -DRoleIsExternalInvEngine

# Uncomment any of the following two depending on how easily compiler messages scare you
#CFLAGS= $(INCLUDE) $(DEBUG) $(DEFINE) -fpic -Wall -pedantic -trigraphs
CFLAGS= $(INCLUDE) $(DEBUG) $(DEFINE)  -mno-cygwin -DWIN32 -Wall

LFLAGS = -L
YFLAGS = --no-lines -y

LDFLAGS = -lm -Wl,--allow-multiple-definition

demo.exe: demo/demo.c $(OBJECTS)
	$(CC) $(CFLAGS) $(DEBUG) $(DEFINE) $(INCLUDE) $< $(OBJECTS) -o demo/demo $(LDFLAGS)

LUSOLSRC = bfp/bfp_LUSOL/lp_LUSOL.c bfp/bfp_LUSOL/LUSOL/lusol.c
LUSOLOBJS = bfp/bfp_LUSOL/lp_LUSOL.o bfp/bfp_LUSOL/LUSOL/lusol.o

OBJECTS = $(LUSOLOBJS) lp_MDO.o shared/commonlib.o colamd/colamd.o \
shared/mmio.o shared/myblas.o ini.o fortify.o lp_rlp.o lp_crash.o  \
lp_Hash.o lp_lib.o lp_wlp.o lp_matrix.o lp_mipbb.o lp_MPS.o	   \
lp_params.o lp_presolve.o lp_price.o lp_pricePSE.o lp_report.o	   \
lp_scale.o lp_simplex.o lp_SOS.o lp_utils.o yacc_read.o

SRC = $(LUSOLSRC) lp_MDO.c shared/commonlib.c colamd/colamd.c	   \
shared/mmio.c shared/myblas.c ini.c fortify.c lp_rlp.c lp_crash.c  \
lp_Hash.c lp_lib.c lp_wlp.c lp_matrix.c lp_mipbb.c lp_MPS.c	   \
lp_params.c lp_presolve.c lp_price.c lp_pricePSE.c lp_report.c	   \
lp_scale.c lp_simplex.c lp_SOS.c lp_utils.c yacc_read.c

LIBRARIES = liblpsolve55.a liblpsolve55.so liblpsolve55.dll
BINARIES = lp_solve.exe demo.exe
ALL =  $(BINARIES) $(DEMOS) $(LIBRARIES)
.PHONY=clean lp_solve

all: $(ALL)
lib: $(LIBRARIES)
bin: $(BINARIES)
objects: $(OBJECTS)

lp_rlp.o: lp_rlp.l lp_rlp.y
	$(LEX) $(LFLAGS) lp_rlp.l
	sed -e "s/yy/lp_yy/g" lex.yy.c >lp_rlp.h
	rm -f lex.yy.c

	$(YACC) $(YFLAGS) lp_rlp.y
	sed -e "s/yy/lp_yy/g" y.tab.c >lp_rlp.c
	rm -f y.tab.c

	$(CC) $(CFLAGS) $(DEBUG) $(DEFINE) $(INCLUDE) -c $*.c

lp_solve.exe: lp_solve/lp_solve.c $(OBJECTS)
	$(CC) $(CFLAGS) $(DEBUG) $(DEFINE) $(INCLUDE) $< $(OBJECTS) -o lp_solve/lp_solve.exe $(LDFLAGS)

demo.exe: demo/demo.c $(OBJECTS)
	$(CC) $(CFLAGS) $(DEBUG) $(DEFINE) $(INCLUDE) $< $(OBJECTS) -o demo/demo.exe $(LDFLAGS)

liblpsolve55.a: $(OBJECTS)
	$(AR) $(ARFLAGS) lpsolve55/$@ `echo $(SRC)|sed 's/[.]c/.o/g'`
	ranlib lpsolve55/$@

liblpsolve55.so: $(OBJECTS)
	$(CC) -shared -Wl,-Bsymbolic -Wl,-soname,$@ -o lpsolve55/$@ `echo $(SRC)|sed 's/[.]c/.o/g'` $(LDFLAGS)

liblpsolve55.dll: $(OBJECTS)
	$(CC) -shared  -o $@ -Wl,--out-implib,liblpsolve55.a $(OBJECTS) $(LDFLAGS)

clean:
	rm -f $(OBJECTS)  *.so *.a lp_solve/lp_solve.exe demo/demo.exe lpsolve55/*.so lpsolve55/*.a lpsolve55/*.dll

>>

No Comments

Tiny Synchronous FileServer using Boost ASIO

This is a tiny example of use for Boost ASIO, here is the code for the server

/**
 * Author: Julien Boucaron <julien.boucaron@free.fr>
 *
 * A tiny example of use of Boost ASIO
 * to create a simple synchronous file_server
 *
 * Released under Boost License
 *
 */

#include <iostream>
#include <fstream>
#include <string>
#include <boost/asio.hpp>

using boost::asio::ip::tcp;

int main()
{
  try
  {
    const unsigned int port = 10000; //port the server listen
    const unsigned int buff_size = 16384; //size of the send buffer

    boost::asio::io_service io_service; //main asio object
    tcp::endpoint endpoint(tcp::v4(), port); //endpoint
    tcp::acceptor acceptor(io_service, endpoint);  //we accept connection there

    //server loop
    while(1) {
      tcp::socket socket(io_service);  //create a socket
      acceptor.accept(socket); //attach to the acceptor
      //we have got a new connection !
      std::cout << " Remote @:port  " << socket.remote_endpoint().address() << " : "
		<< socket.remote_endpoint().port() << std::endl;

      std::fstream file("file"); //we open this file

      char* buff = new char[buff_size]; //creating the buffer
      unsigned int count = 0; //counter
      std::cout << "Sending" << std::endl;
      while( !file.eof() ) { //loop until there is no more data to send
	memset(buff,0,buff_size); //cleanup the buffer
	file.read(buff,buff_size); //read some data 
	boost::system::error_code ignored_error;
	unsigned int len = file.gcount(); //get the effective number of bytes read
	boost::asio::write(socket, boost::asio::buffer(buff,len),
			   boost::asio::transfer_all(), ignored_error); //send
	count+=len; //increment counter
      }

      file.close(); //close file
      delete(buff);  //delete buffer
      std::cout << "Finished" << std::endl;
      std::cout << "Sent "  << count << " bytes" << std::endl;
    }

  }
  catch (std::exception& e)
  {
    std::cerr << e.what() << std::endl;
  }

  return 0;
}

Now, the code for the client

/**
 * Author: Julien Boucaron <julien.boucaron@free.fr>
 *
 * A tiny example of use of Boost ASIO
 * to create a simple synchronous file_client
 *
 * Released under Boost License
 *
 */

#include <iostream>
#include <string>
#include <fstream>
#include <boost/asio.hpp>

using boost::asio::ip::tcp;

int main(int argc, char* argv[])
{
  try
  {

    const char *port = "10000"; //the port we connect
    const unsigned int buff_size = 65536; //the size of the read buffer

    if (argc != 3)
    {
      std::cerr << "Usage: file_client <host> <filename>" << std::endl;
      return 1;
    }
    boost::asio::io_service io_service; //asio main object
    tcp::resolver resolver(io_service); //a resolver for name to @
    tcp::resolver::query query(argv[1], port); //ask the dns for this resolver
    tcp::resolver::iterator endpoint_iterator = resolver.resolve(query); //iterator if multiple answers for a given name
    tcp::resolver::iterator end;

    tcp::socket socket(io_service); //attach a socket to the main asio object
    socket.connect(*endpoint_iterator); //connect to the first returned object

    std::fstream f(argv[2],std::fstream::out); //open in write mode the output file
    unsigned int count = 0; //a counter
    while(1) { //loop until (see break after)
      boost::array<char, buff_size> buf; //create read buffer
      boost::system::error_code error; //in case of error
      size_t len = socket.read_some(boost::asio::buffer(buf), error); //read data
      std::cout << "Read " << len <<  std::endl;
      count += len;
      std::cout << "Read a total of " << count << " bytes " << std::endl;
      if (error == boost::asio::error::eof ) { //if end of file reached
	f.write(buf.data(),len); //finish write data
	f.close();   break; // Connection closed cleanly by peer.
      }
      else if (error) {
        throw boost::system::system_error(error); // Some other error.
      }
      else {
	f.write(buf.data(),len); //write some data 
      }
    }
  }
  catch (std::exception& e)
  {
    std::cout << "Exception: " << e.what() << std::endl;
  }

  return 0;
}

No Comments

jujuPdf

This is a simple example of how to use the poppler library (pdf rendering used in xpdf for instance) in Qt.

The code is under GPLv3 license.

Follow this link http://julien.boucaron.free.fr/download/

No Comments

Antlr – Lustre Grammar

Here is my grammar for the Lustre synchronous language using antlr v3, not yet finished but basically working.

/*
* This is a grammar for the LUSTRE
* Synchronous Language
*
* (c) 2008 Julien Boucaron
*
* TODO
*  –> expression every expression
*  –> add const modifier for arguments
*  –> nested node definitions
*  –> check with last version
*/

grammar Lustre;

//////////////
// $<PARSER
//////////////

lustre
: (compilationUnit)* EOF
;

compilationUnit
:
constDeclaration
nodeDeclaration nodeDefinition
;

//////////////
// $<DECL
//////////////

constDeclaration
:
(‘const’ (IDENTIFIER)
(‘,’ IDENTIFIER)*
‘:’ BASICTYPE (‘^’ (shift_expression) )?
‘;’)*
;

nodeDeclaration
:
‘node’ IDENTIFIER ‘(‘ nodeArgs ‘)’
( ‘returns’ ) ‘(‘ nodeArgs ‘)’
constDeclaration nodeVars*
‘;’
;

nodeVars:
‘var’ nodeArgs ‘;’
;

nodeArgs
:
(nodeArgsHelper ‘:’ BASICTYPE (‘^’ (shift_expression) )?)+
;

nodeArgsHelper
:
IDENTIFIER
| IDENTIFIER ‘,’ nodeArgsHelper
;

//////////////
// $>DECL
//////////////

nodeDefinition
:
‘let’
nodeBody*
‘tel’
‘;’
;

nodeBody
:
label? nodeAff ‘:=’ expression ‘;’
| label if_nodebody ‘;’
| asserT ‘;’
| gotO ‘;’
| forStatement ‘;’
;

label    :
IDENTIFIER ‘:’
;

asserT    :
‘assert’  expression
;

forStatement :
‘for’ IDENTIFIER ‘in’ ‘[' expression ('..' expression)? ']‘
‘let’
nodeBody+
‘tel’
;

nodeAff
:
IDENTIFIER vectorHelper? |
‘(‘ IDENTIFIER vectorHelper?
(‘,’ IDENTIFIER vectorHelper?)+ ‘)’
;

vectorHelper
:
‘[' expression ('..' expression )?  ']‘
;
//bug present there

//////////////
// $<EXPRESSION
//////////////

expression
:
binaryExpression
;

unaryExpression
:
BOOLCONSTANT
| REAL
| DECIMAL
| preExpression (‘pre’)? IDENTIFIER vectorHelper?
| preExpression (‘pre’)? IDENTIFIER? concatOrPar  vectorHelper?
| if_expression
| current
| gotO
;

preExpression
:
(‘not’|'!’|'~’)? signExpression
;
signExpression
:
(‘+’|'-’|'++’|'–’)?
;

concatOrPar
:
‘(‘ expression (‘,’ expression )* ‘)’
//i want one for par expression and the other one for concat…
;

if_expression
:
‘if’ expression ‘then’
expression
‘else’
expression
‘endif’
;
if_nodebody
:
‘if’ expression ‘then’
nodeBody+
‘else’
nodeBody+
‘endif’
;

current    :
‘current’ ‘(‘ expression ‘)’
;

gotO    :
‘goto’ IDENTIFIER
;

binaryExpression
:
followBy_expression (‘->’ followBy_expression)*
;

followBy_expression
:
or_expression ( (‘or’|'nor’) or_expression )*
;

or_expression
:
and_expression ( (‘and’|'nand’) and_expression )*
;

and_expression
:
xor_expression ( (‘xor’|'nxor’) xor_expression)*
;

xor_expression
:
equal_expression ( (‘=’|'!=’) equal_expression)*
;

equal_expression
:
compare_expression ( (‘<’|'<=’|'=>’|'>’) compare_expression)*
;

compare_expression
:
shift_expression ( (‘<<’|'>>’) shift_expression )*
;

shift_expression
:
add_expression ( (‘+’|'-’) add_expression )*
;
add_expression
:
mul_expression ( (‘*’|'/’|'%’) mul_expression )*
;

mul_expression
:
when_expression (‘when’ when_expression)*
;

when_expression
:
unaryExpression
;
//////////////
// $>EXPRESSION
//////////////

//////////////
// $>PARSER
//////////////

//////////////
// $<LEXER
//////////////

BASICTYPE
:
‘bool’ |
‘int’ |
‘real’
;

BOOLCONSTANT
:
‘true’ |
‘false’
;

DECIMAL    :
(’0′..’9′)+
;

REAL    :
FloatPrefix
;

IDENTIFIER
:    ( ‘a’..’z’ | ‘A’..’Z’ | ‘_’ ) ( ‘a’..’z’ | ‘A’..’Z’ | ‘_’ | ’0′..’9′ )*
;

fragment
FloatPrefix
:    ’0′..’9′+ ( ‘.’ ’0′..’9′+ ) ( ( ‘e’ | ‘E’ ) ( ‘+’ | ‘-’ )?  ’0′..’9′+ )?
;

//////////////
// $<Skipped
//////////////
COMMENT    :
‘–’  ~( ‘\n’ | ‘\r’ )* NEWLINE {skip();}
;
NEWLINE
:    ( ‘\n’ | ‘\r’ | ‘\r\n’ ) {skip();}
;
WS
:    ( ‘ ‘ | ‘\u000C’ | ‘\t’ ) {skip();}
;

No Comments

Hello World

Here is the attached modified HelloGPGPU that is compiling just out of the box for Linux (Ubuntu)hellogpgpu-102-ubuntutar.gz

See the attached screenshot

hellogpgpu_screenshot.png

No Comments