Archive

Archive for the ‘English’ Category

Matrix chain multiplication: parallel and sequential

June 25th, 2009 No comments

Yep, that’s the project that uses threads. Nothing more is worth to add — the problem is well-known, as well as the algorithm.

Source: http://www.srednikpe.org/src/matrix/

C++ hidden feature

June 22nd, 2009 1 comment

This is one I didn’t know. Conditional expressions in C++ can be lvalue:

(a == 0 ? a : b) = 42;

More hidden features: http://beerpla.net/2009/06/21/hidden-features-of-perl-php-javascript-c-c-c-java-ruby-python-and-others-collection-of-incredibly-useful-lists/

boost::thread and std::list

June 22nd, 2009 4 comments

I had to create many threads in C++. Of course I didn’t want to use pthread, because it’s ugly C. I chose boost::thread instead. Then I needed some container to keep all these threads. Arrays were out of the question for the same reason as pthread. I could use boost:array but stl::list was easier.

The problem was—how to keep this thread objects? I couldn’t use std::list<boost::thread> nor std::list<boost::thread &> because threads cannot be copied. I had to dynamically create the object in order to prevent the destruction after the scope. But for some reason I got compiler error while trying std::list< std::auto_ptr<boost::thread> > in the line where I pushed_back newly created threads.

The cause of the error was that the argument of std::list::push_back() was a constant reference. And while assinging one auto_ptr to another, the first one is getting call release() method, but it can’t since the reference is constant and release() change the state of the object.

To solve this problem one can use boost::shared_ptr.

Example:

#include <iostream>
#include <list>
#include <boost/thread.hpp>
#include <boost/date_time.hpp>
#include <boost/smart_ptr.hpp>
 
using std::list;
using std::cout;
using std::endl;
 
struct foo
{
	void operator() (int id)
	{
		cout << "starting thread " << id << '\n';
		boost::this_thread::sleep(boost::posix_time::seconds(3));
		cout << "ending thread " << id << '\n';
	}
};
 
int main()
{
	typedef boost::shared_ptr<boost::thread> thread_ptr;
	list<thread_ptr> thread;
 
	cout << "creating threads\n";
 
	for (int i = 0; i < 10; ++i)
	{
		thread.push_back(boost::shared_ptr<boost::thread>(new boost::thread(foo(), i)));
	}
 
	cout << "after creation, now joining them\n";
 
	for (list<thread_ptr>::iterator it = thread.begin(); it != thread.end(); ++it)
	{
		(*it)->join();
	}
 
	cout << "ending program" << endl;
 
	return 0;
}

And the result:

tomek@notlob:~% g++ -o threads threads.cpp -O2 -Wall -lboost_thread-mt
tomek@notlob:~% ./threads
creating threads
starting thread 1
starting thread 0
starting thread 2
starting thread 3
starting thread starting thread 4
5
starting thread 6
starting thread 7
starting thread 8
starting thread 9
after creation, now joining them
ending thread 1
ending thread 3
ending thread 5
ending thread 4
ending thread 0
ending thread 2
ending thread 6
ending thread 8
ending thread 7
ending thread 9
ending program

How not to name variables

June 13th, 2009 No comments

This is is a Python line of code which I found in my project I work with two colleagues:

for mid, mda, xmht, xmat, mhs, mas, mehs, meas, mphs, mpas, mrou in q:
    # ....

No comments.

HP Pavilion dv6780el on Debian Squeeze

May 29th, 2009 1 comment

Recently I bought a laptop HP Pavilion dv6780el. It was shipped with Vista, but was used only once for download Debian Testing snapshot: two horrible hours for searching CD burning program for Windows (this is shareware, this is demo, this is adware, oh, this is freeware, but actually it can’t burn CD image onto DVD disk *sigh*). In the end I copied the image to my old laptop and after 5 minutes I’ve got a CD ready to install.

% lspci
00:00.0 Host bridge: Intel Corporation Mobile PM965/GM965/GL960 Memory Controller Hub (rev 0c)
00:01.0 PCI bridge: Intel Corporation Mobile PM965/GM965/GL960 PCI Express Root Port (rev 0c)
00:1a.0 USB Controller: Intel Corporation 82801H (ICH8 Family) USB UHCI Controller #4 (rev 03)
00:1a.1 USB Controller: Intel Corporation 82801H (ICH8 Family) USB UHCI Controller #5 (rev 03)
00:1a.7 USB Controller: Intel Corporation 82801H (ICH8 Family) USB2 EHCI Controller #2 (rev 03)
00:1b.0 Audio device: Intel Corporation 82801H (ICH8 Family) HD Audio Controller (rev 03)
00:1c.0 PCI bridge: Intel Corporation 82801H (ICH8 Family) PCI Express Port 1 (rev 03)
00:1c.1 PCI bridge: Intel Corporation 82801H (ICH8 Family) PCI Express Port 2 (rev 03)
00:1c.5 PCI bridge: Intel Corporation 82801H (ICH8 Family) PCI Express Port 6 (rev 03)
00:1d.0 USB Controller: Intel Corporation 82801H (ICH8 Family) USB UHCI Controller #1 (rev 03)
00:1d.1 USB Controller: Intel Corporation 82801H (ICH8 Family) USB UHCI Controller #2 (rev 03)
00:1d.2 USB Controller: Intel Corporation 82801H (ICH8 Family) USB UHCI Controller #3 (rev 03)
00:1d.7 USB Controller: Intel Corporation 82801H (ICH8 Family) USB2 EHCI Controller #1 (rev 03)
00:1e.0 PCI bridge: Intel Corporation 82801 Mobile PCI Bridge (rev f3)
00:1f.0 ISA bridge: Intel Corporation 82801HEM (ICH8M) LPC Interface Controller (rev 03)
00:1f.1 IDE interface: Intel Corporation 82801HBM/HEM (ICH8M/ICH8M-E) IDE Controller (rev 03)
00:1f.2 SATA controller: Intel Corporation 82801HBM/HEM (ICH8M/ICH8M-E) SATA AHCI Controller (rev 03)
00:1f.3 SMBus: Intel Corporation 82801H (ICH8 Family) SMBus Controller (rev 03)
01:00.0 VGA compatible controller: nVidia Corporation GeForce 8400M GS (rev a1)
02:00.0 Network controller: Intel Corporation PRO/Wireless 3945ABG [Golan] Network Connection (rev 02)
06:00.0 Ethernet controller: Realtek Semiconductor Co., Ltd. RTL8101E/RTL8102E PCI Express Fast Ethernet controller (rev 01)
07:09.0 FireWire (IEEE 1394): Ricoh Co Ltd R5C832 IEEE 1394 Controller (rev 05)
07:09.1 SD Host controller: Ricoh Co Ltd R5C822 SD/SDIO/MMC/MS/MSPro Host Adapter (rev 22)
07:09.2 System peripheral: Ricoh Co Ltd R5C843 MMC Host Controller (rev 12)
07:09.3 System peripheral: Ricoh Co Ltd R5C592 Memory Stick Bus Host Adapter (rev 12)
07:09.4 System peripheral: Ricoh Co Ltd xD-Picture Card Controller (rev ff)

I installed Debian Squeeze amd64 and almost everythinng worked out-of-box, except few things.

Wi-Fi
I had to install binary firmware for Intel 3945 since non-free packages are not in default Debian installation. Fortunately I was able to connect with wire:

sudo aptitude install firmware-iwlwifi

NVIDIA drivers
The default X driver is nv which performance is poor. Again, I had to manually install non-free software. Just googled “nvidia drivers linux” and followed the instructions. The good news is that the quailty of the official NVIDIA drivers for X server is very satisfactionary.

Muting speakers when headphones are plugged-in
This is one I haven’t solved yet. I just mutted the front speakers in ALSA Mixer, but that’s only a workaround.
UPDATE: It works after updating alsa-base to version 1.0.20

ACPI
I noticed that when I tried first time to exhaust the battery it didn’t show any notification like “Low battery level”; it just died out of energy. Maybe it’s just a GNOME setting, dunno.

What I haven’t tested is fax modem and card bus.

The battery works for 2h15m which is quite short time.

Program verification conditions

December 22nd, 2008 No comments

Yay, another parser in Haskell! That was a Haskell week, but I’m glad I had an opportunity to recall this beautiful full-of-math language.

This program reads an annotated program — a program with assertions. Assertions are just logic formulas. We use them to describe the program state between instructions, ie. variable x is equal to variable y squared. But assertions cannot be random, they must be derived from Hoare’s logic axioms and rules (which base on the formal semantic of the language). How do we know if there is an derivation of a program?

Here verification condicitions come. They are logic formulas too, but generated from annotated program. If all of them are true, then the program is derivable. More information here: http://www.macs.hw.ac.uk/~air/hic-hisd/ and http://www.daimi.au.dk/~bra8130/Wiley_book/wiley.html. The whole point is about formal verification of programs. Nowadays compiler can tell only whether given program has a syntax or type error. It doesn’t know anything about how this program should work and can’t say something like: “This program certainly does not multiply matrices.”. Assertions are some way to tell the programmer’s intentions to the compiler: “This is my piece of code and it should multiply two matrices”, and thus, by generating verification conditions and using automated theorem proving it could say: “It does.”.

So after a boring digression… this program reads an annotated program in a simple imperative language and generates verification conditions. That’s all.

All files are here: http://srednikpe.org/src/vc/

Reverse Polish notation calculator in Haskell

December 22nd, 2008 6 comments

Just a few lines of code, written when I was slightly bored.

module Main where
 
data Token = Plus | Minus | Divide | Times | Num Integer
 
rpnEval :: [Token] -> Integer
rpnEval = rpnEval' [] where
		rpnEval' (i:s) [] = i
		rpnEval' s ((Num i):t) = rpnEval' (i:s) t
		rpnEval' (a:b:s) (op:t) = rpnEval' ((evalOp op a b):s) t
 
		evalOp Plus = (+)
		evalOp Minus = (-)
		evalOp Times = (*)
		evalOp Divide = div
 
toToken :: String -> Token
toToken "+" = Plus
toToken "-" = Minus
toToken "*" = Times
toToken "/" = Divide
toToken s = Num (read s)
 
main = do
	l <- getContents
	mapM print $ map (rpnEval . (map toToken) . words) (lines l)

Update: See comments for better solution.

Math jokes

June 29th, 2008 No comments

From here.

An engineer, a physicist and a mathematician are staying in a hotel.
The engineer wakes up and smells smoke. He goes out into the hallway and sees a fire, so he fills a trash can from his room with water and douses the fire. He goes back to bed.
Later, the physicist wakes up and smells smoke. He opens his door and sees a fire in the hallway. He walks down the hall to a fire hose and after calculating the flame velocity, distance, water pressure, trajectory, etc. extinguishes the fire with the minimum amount of water and energy needed.
Later, the mathematician wakes up and smells smoke. He goes to the hall, sees the fire and then the fire hose. He thinks for a moment and then exclaims, “Ah, a solution exists!” and then goes back to bed.

Asked if he believes in one God, a mathematician answered:
” Yes, up to isomorphism.”

A mathematician, scientist, and engineer are each asked: “Suppose we define a horse’s tail to be a leg. How many legs does a horse have?” The mathematician answers “5″; the scientist “1″; and the engineer says “But you can’t do that!

One day a farmer called up an engineer, a physicist, and a mathematician and asked them to fence of the largest possible area with the least amount of fence.
The engineer made the fence in a circle and proclaimed that he had the most efficient design.
The physicist made a long, straight line and proclaimed “We can assume the length is infinite…” and pointed out that fencing off half of the Earth was certainly a more efficient way to do it.
The Mathematician just laughed at them. He built a tiny fence around himself and said “I declare myself to be on the outside.”

A mathematician belives nothing until it is proven
A physicist believes everything until it is proven wrong
A chemist doesn’t care
biologist doesn’t understand the question.

Two male mathematicians are in a bar. The first one says to the second that the average person knows very little about basic mathematics. The second one disagrees, and claims that most people can cope with a reasonable amount of math.
The first mathematician goes off to the washroom, and in his absence the second calls over the waitress. He tells her that in a few minutes, after his friend has returned, he will call her over and ask her a question. All she has to do is answer one third x cubed.
She repeats “one thir — dex cue”?
He repeats “one third x cubed”.
Her: `one thir dex cuebd’? Yes, that’s right, he says. So she agrees, and goes off mumbling to herself, “one thir dex cuebd…”.
The first guy returns and the second proposes a bet to prove his point, that most people do know something about basic math. He says he will ask the blonde waitress an integral, and the first laughingly agrees. The second man calls over the waitress and asks “what is the integral of x squared?”.
The waitress says “one third x cubed” and while walking away, turns back and says over her shoulder “plus a constant!”

Just another wget clone

June 10th, 2008 No comments

While nothing interesting is happening in my life I post my recent program. Simply wget clone, with -m option it downloads all images and some stylesheets from the page and changes them to local adresses. Sorry for Polish comments and names in source file.

Source

LOLCode Compiler (my own!)

April 23rd, 2008 No comments

Hah! My dream have come through! I wrote my first compiler with my own lexer and parser. It compiles LOLCode (slightly modified version, see in specification [in polish, but only output language]) to stack machine language. My project contains also stack machine which interprets output files and prints them in human readable assembler and three examples of LOLCode programs. It’s all GPL so you can learn how it works. It’s written in C and I didn’t have much time to write pretty dynamic arrays and If someone will fix it, I would be verry happy. If you’ve got question about how it works, go ahead, write me and ask.

Source: http://www.srednikpe.org/src/lcc/