Wednesday, March 31, 2010

Gentoo Linux on Lenovo u150

Hello, I just bought a Lenovo u150 with the following specs:

Intel CPU SU7300 @ 1.30GHz, 1300 Mhz, Core 2 Duo
RAM: 2GB
Video: 1366x768x60Hz, 11.6inches
Wireless: Intel WiFi Link 5100 AGN
LAN: Broadcom NetLink Gigabit Ethernet
Hard disk: 300 GB (320GB they say)
Bluetooth
Lenovo EasyCamera3

Here a summary of the steps I took to install Gentoo Linux on it.

I used UNetbootin to load the Gentoo Minimal Installation (amd64) iso image into a USB key, and booted the laptop with it. The first thing I noticed is the lack of the wired network interface, so I used the net-setup script to activate the wlan0 interface and connect to Internet without wires.

After creating partitions, compiling the kernel with this configuration and installing the basic packages as described in the Gentoo Handbook, the system rebooted smoothly.

Brightness

Brightness fixes have been published by zaius; based on his explanations I wrote this C++ program (just because I love C++), which just does the same but in a logarithmic fashion, which I find more appropriate for handling low levels of brightness.

Thanks to zaius for his research and ideas; I just hope the fix will be made at the kernel level very soon.

Earphone plug


The speakers do not mute when you plug your earphones in the laptop. I found a workaround by adding the following kernel parameter to grub.conf:

snd-hda-intel.model=olpc-xo-1_5

It may not look nice, you may loose access to the internal microphone, but the parameter works in keeping your music private for you. Keep up to date with information on these issue by subscribing to the Ubuntu Bug.

Update: linux-2.6.35 does not need the kernel parameter.

Thanks for reading.

I do not have any relationship with Lenovo or its trademarks.

Tuesday, March 09, 2010

C++0x tips: improve performance in code using STL containers

Hello, I am hungry for code performance, specially in my simulations involving entire cities full with people, mosquitoes, viruses, homes, flower vases, etc. So it has been frustrating to interrupt the code execution just to realize all it has been doing for hours is creating the cities. Let us examine how the preparation phase takes place:
class houseCls;
class personCls;
//...

class cityCls {
    private:
        vector<houseCls> houses;
        vector<personCls> pedestrians;
    public:
        //...
}

vector<cityCls> country;

int main() {
    for (int i=0; i<nCities; i++)
        country.push_back(cityCls(/* City construction arguments */));
}
On execution, a city is created in the stack, then it is copied into the country vector and then the city in the stack is destroyed. So most runtime is employed in getting the vector ready for simulation. Using an aggregate (array) is out of the question, because things must be dynamic and safety must be ensured in all aspects.
How about just creating the new city in the new space inside the vector? Previously the vector::swap() function helped accomplish this task, but implementation became confuse sometimes for the client programmer.

I took the opportunity to study the improvements of the newest C++ standard, C++0x. I learned about a great STL improvement in terms of memory management, performance, and overall, getting things done. It is the emplace_back() function, which does just what the above question was about.

Use the new C++ standard, at your own risk, taking into account it is evolving.
g++ --pedantic-errors -Wall -Wextra -std=gnu++0x source.cc
Make the following changes to your code:
int main(){
 ///Always reserve memory before starting
 country.reserve(nCities);
 for (int i=0; i<nCities; i++)
  country.emplace_back(/** City construction arguments, which do not need the explicit class name anymore */);
}
You can use explicit copy constructors and gdb to benchmark your code.