Matrix chain multiplication: parallel and sequential
Yep, that’s the project than use threads. Nothing more is worth to add — the problem is well-known, as well as the algorithm.
Yep, that’s the project than use threads. Nothing more is worth to add — the problem is well-known, as well as the algorithm.
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/
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
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.
Ostatnimi czasy miałem okazję bliżej przyjrzeć się dwóch narzędziom do projektowania interfejsu. Postaram się w skrócie opisać, co mi się w nich podobało, a co nie.
Na początek Qt Designer, bo jego wcześniej się nauczyłem. Interfejs jest podobny do GIMP-a, czyli dużo niezadokowanych okienek: z kontrolkami, z właściwościami, ze slotami, z hierarchią kontrole i oczywiście okno naszego projektu. Glade natomiast to w całości jedno okno. Wiele osób narzeka na ten “gimpowy” interfejs, jednak w tym przypadku Qt Designer ładnej wygląda, chociaż może niestaranności wykonania Glade’a (głównie chodzi o okno z właściwościami, które, jak to trafnie pan w video tutorialu ujął, it’s never wide enough).
Wśród kontrolek Designera mamy pełen zasób biblioteki Qt (w tym np. kontrolka przeglądarki WWW opartej na WebKicie), jednak są to tylko stricte widżety. Nie będziemy mogli zdefiniować modelu dla *View (TreeView, ListView, etc.), a na tych kontrolkach miałem oparte oba interfejsy projektów (w sumie obie były pewnym interfejsem bazy danych). Glade potrafi tworzyć modele (o ile oczywiście wybierzemy projekt GtkBuilder, ale libglade nie jest już rozwijana, więc wybór tego pierwszego powinien być jasny), jednak chwilę musiałem pogooglać, jak to się robi. Jest to póki co mało intuicyjne (przynajmniej dla mnie). Natomiast w Designerze łatwiej udało mi się ustawić “sortowalność” kolumn (w sumie to w Glade jeszcze do tego nie doszedłem, choć próbowałem).
Ogólnie lepsze wrażenie sprawia jednak Designer. Jest bardziej intuicyjny, pozwala przenosić kontrolki myszką (nie trzeba ich wycinać albo zmieniać albo wpisywać pozycji we właściwościach) i edytować je na oknie. Od razu mamy także podgląd naszego okna. O wiele prościej się też moim zdaniem obsługuje kontenery, choć na początku miałem trochę trudności. W Glade trzeba trochę się namęczyć, żeby stworzyć pożądany efekt na okienku, żeby wszystko było równiutko.
To, co mi się jeszcze spodobało w Designerze, to sloty, chociaż to bardziej kwestia Qt niż samego Designera. Sloty bardzo ułatwiają podpinanie sygnałów. Możemy np. sygnał “clicked()” przycisku podpiąć pod slot “show()” okienka bez pisania żadnej procedury obsługi. Natomiast sygnały niestandardowe, które wymagają odrobiny naszego kodu, trzeba podpinać ręcznie (w kodzie). Glade potrafi zapamiętać nazwy naszych handlerów i automatycznie je podpiąć (w Pythonie trzeba jednak podać mu słownik odwzorowań nazw na funkcje jako argument).
Kiedy już stworzymy interfejs, nadchodzi etap programowania. Sama obsługa naszego interfejsu lepsza jest jednak w Glade. Tworzymy obiekt GtkBuilder i ładujemy nasz plik *.glade. Następnie wywołujemy funkcję, której podajemy nazwę widżetu, a ona zwraca nam wskaźnik (w Pythonie referencję) do niego i już możemy go używać. W Qt Designerze musimy odpowiednim narzędziem skonwertować nasz plik *.ui na kod źródłowy z klasą, od której nasze okno dziedziczy (lub enkapsuluje jej obiekt).
Co zatem jest lepsze? Oba programy są dobre. Na obu oparto potężne środowiska graficzne, zatem oba nadadzą się do stworzenia Twojego projektu. Każda ma swoje mocne i słabe strony, więc wybór w tym przypadku będzie indywidualny i często przypadkowy (tzw. kwestia gustu).
UPDATE: Glade jednak potrafi przenosić kontrolki, a Qt dynamicznie ładować interfejs.
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
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
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.
Krótki wstęp do semantyki gier, który przygotowałem w ramach projektu końcowego z Semantyk Języków Programowania.
Update: notatki pozostałych studentów.
95% muzyki w sieci to nielegalne pobrania.
When you have six million people breaking the law, it’s the law that needs changing, not the people.
Ostatnio miałem ponowną styczność z TeX-em, tym razem konkretnie z Beamerem (całkiem fajne narzędzie do tworzenia ładnych i profesjonalnych prezentacji; być może wrzucę tę, którą zrobiłem na zajęcia). Jest to jeden z programów, które są owiane dla mnie lekką tajemnicą… Już sam fakt że został napisany przez legendarnego Donalda Knutha, powoduje bojaźn i trwogę podczas używania. No dobra, przesadzam, ale TeX to program wyjątkowy. Nie dlatego, że jest bezbłędny (ma już ze 30 lat chyba, a ostatni błąd wykryto w nim kilkanaście lat temu), ale dlatego, że jest napisany… literacko.
Donald Knuth napisał książkę-program pod tytułem TeX: The Program (ISBN: 0201134373). Następnie (a może uprzednio?) napisał narzędzie, które nazwał WEB. WEB pozwalał mu automatycznie skonwertować książkę-program do poprawnego programu w Pascalu (w tamtych czasach był to bardzo popularny język), który po skompilowaniu robił to, co opisano w książkoprogramie. Oprócz tego, pozwalał też wygenerować… książkę. Z nagłówkami, sekcjami, stronami — ot, PDF lub PostScript, nadający się do wydrukowania.
Książkoprogram to tak naprawdę dokumentacja i kod w jednym. Programista pisze na przemian kod i tekst. Może przy pisaniu kodu korzystać ze wszystkich dobrodziejstw TeX-a do opisywania swojego kodu, tworzyć odnośniki do innych fragmentów kodu, etc. Lepiej to zobaczyć na przykładzie: kwadrat.w. Użyłem wesji WEB dla C/C++, która nazywa się CWEB. Aby otrzymać program C, wystarczy polecnie:
$ ctangle kwadrat This is CTANGLE, Version 3.64 (Web2C 7.5.6) Writing the output file (kwadrat.c): Done. (No errors were found.) $ gcc -o kwadrat kwadrat.c -lm $ ./kwadrat Podaj a: 1 Podaj b: 10 Podaj c: 5 x = -9.472136 x = -0.527864
Aby otrzymać dokument TeX-a, należy napisać:
$ cweave kwadrat This is CWEAVE, Version 3.64 (Web2C 7.5.6) Writing the output file... Writing the index... Done. (No errors were found.) $ pdftex kwadrat.tex This is pdfTeXk, Version 3.141592-1.40.3 (Web2C 7.5.6) (...) Output written on kwadrat.pdf (3 pages, 63651 bytes). Transcript written on kwadrat.log.
Oto wygenerowane pliki: kwadrat.tex, kwadrat.pdf i kwadrat.c. Niestety nie wiem jak zmusić czystego TeX-a do poprawnego czytania UTF-8, dlatego w PDF-ie nie ma polskich liter.
Szczerze mówiąc nie mam zdania na temat literate programming. Jeśli ktoś lubi tak pisać, to proszę bardzo — całkiem przyjemnie się czyta taki dokument. Jednak ja nie lubię się rozpisywać nad kodem, dla mnie sam kod (z komentarzami) stanowi wystarczającą dokumentację. ;)
Warto jeszcze dodać, że wsparcie dla literate programming jest zaimplementowane w Haskellu. Ale to już lepiej poczytać na Haskell Wiki.
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/
Recent Comments