Friday, October 26, 2007

Leopard. Comming out tomorrow.

Some of you might know this. Leopard is coming out tomorrow at 6pm.

I will go tomorrow to an apple store and play a little bit with it, unfortunately I'm not buying it until a few months from now.

I really want to see spaces working and the new finder. Quick view sounds interesting and I wonder if you can make your own quick views. i.e. if I'm going to store the output of my program on an archive .dat then I can just open it with quick view and see the data as a plot. I guess this would be rather difficult to achieve, but it would be nice anyway.

I will talk about Leopard a little bit more next week. Take care.

Monday, October 8, 2007

Compiling for PPC from an Intel Mac.

Most mac users, might be familiar with universal applications. An universal application is an application such that it will run natively on either an Intel mac or a Power PC (PPC) mac. I was thinking about parallelization and I realized that if I wanted to do it between my two macs (a mac mini PPC and a Mac Book Pro Intel core duo) I have to do universal applications.

If you use Xcode you might know that making universal binaries (programs) is easy just by modifying the project properties. But for the most part I don't use Xcode I mainly use the terminal to compile. In this case the solution is quite easy too.

I made a Hello World application in C. Usually I will compile using

g++ Hello.cpp

the result is an executable file named a.out (17 Kb in size) that will run on my Mac Book Pro, if I try to run this code in my mac mini an error will occur. Using the flag -arch we can specify a different architecture, i.e.

g++ Hello.cpp -flag ppc

the result is once again an executable named a.out (20 Kb in size), this code will run in the mac mini natively. It will also run on the Intel mac but it will not be native reducing the performance of the code.

Finally if we want to run this code on natively on both PPC and Intel we would use the following command

g++ Hello.cpp -flag i386 -flag ppc

the result is an executable a.out (40 Kb in size), that contains both a PPC native and a Intel native binary.

This flag option is exclusive to mac platform, so it won't work on Linux machines. I try running each one of the 3 executables produced on a Linux machines, as expected no one worked.

I have been having trouble using the GSL (Gnu Scientific Library) and generating universal binaries, my best guess is that I have to compile the libraries again for PPC but I don't have the time, energy nor patience to do it.

Friday, October 5, 2007

It's all about linux.

This week I decided to go for linux related links, just because I like linux. It wasn't hard. Enjoy!
Linux
Linux
Linux

Thursday, October 4, 2007

Scientific programing an database structures. The Linked List part 1/3

In my experience many times database structures are not necessary while doing basic scientific simulations. Most of the time we are dealing with fixed length vectors or matrices. Some of you might disagree with me and might point out that appropriate data management is always necessary. In any case is unfortunate that when the occasion arises this structures are often neglected.

I will try to code from scratch in Objective-C some of the basic database structures and talk a little bit about them. This week I will start with a linked list.

If you are familiar with arrays you are familiar with liked lists. A linked list is a list of nodes, each node stores an object (may be an integer, a double, or any complicated class like a molecule) and a link or pointer to the next object in the list. You might think of it like a shoe box storing something (some data). Each shoe box have a string connected to another shoe box, if you want to locate something you just have to look for it on each box one at the time (following the string so you won't look in the same box twice). I will talk more about linked lists in a next post, but now lets code the nodes.

We will implement a linked list in Objective C.

First make the interface file and name it "LinkedListNode.h"

//2007 chuyandmac.blogspot.com, LinkedListNode.h
//Interface file for the a node to be
//used in a linked list data structure.

#import

@interface LinkedListNode : NSObject {
double value;
LinkedListNode *next;
}

-(id)initWithValue:(double)v;
-(void)setNextNode:(LinkedListNode *)n;
-(void)setValue:(double)v;
-(double)value;
-(LinkedListNode *)next;

@end

Second make the implementation file "LinkedListNode.m"

//2007 chuyandmac.blogspot.com, LinkedListNode.m
//Implementation file for the a node to
//be used in a linked list data structure.

#import "LinkedListNode.h"

@implementation LinkedListNode

-(id)initWithValue:(double) v{
if(self = [super init]){
[self setValue:v];
[self setNextNode:nil];
}
return self;
}

-(void)setNextNode:(LinkedListNode *)n{
next = n;
return;
}

-(void)setValue:(double)v{
value = v;
}

-(double)value{return value;}
-(LinkedListNode *)next{return next;}

@end

Finally make sure they work as expected, make the file "linkedListNode.m"

//2007 chuyandmac.blogspot.com, linkedListNodeTest.m
//Test LinkedListNode.m and LinkedListNode.h
//To compile use
//gcc -ObjC LinkedListNode.m linkedListNodeTest.m -framework Foundation

#import "./LinkedListNode.h"

int main(){
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSLog(@"Testing LinkedListNode.h");
LinkedListNode *node = [[LinkedListNode alloc] initWithValue:2.0];
LinkedListNode *node2 = [[LinkedListNode alloc] initWithValue:3.0];
[node setNextNode:node2];
NSLog(@"node was initialiazed with value %f.",[node value]);
NSLog(@"the second node is linked an has a value %f."
,[[node next] value]);

[node release];
[node2 release];
[pool release];
}

Make sure everything is in the same folder and compile typing the following command in terminal.

gcc -ObjC LinkedListNode.m linkedListNodeTest.m -framework Foundation

An executable named a.out will be created, you can execute it typing in the terminal.

./a.out

The output will look like this. Enjoy!

2007-10-04 23:30:41.786 a.out[609]
Testing LinkedListNode.h
2007-10-04 23:30:41.786 a.out[609]
node was initialiazed with value 2.000000.
2007-10-04 23:30:41.786 a.out[609]
the second node is linked an has a value 3.000000.

Note: I'm still learning ObjC and I'm not familiar yet with memory leaks, I might have to modify this code just as to make sure I don't have them. If you notice any memory leak in this code, please let me know.

Tutorial: Quick trick to make your code run faster on a core duo processor.

The following tutorial should work with an Intel core duo machine regardless the Operating System. Feel free to share with me any suggestions or comments.

Currently I'm working on a code with the following main() method.


int main(){
double T = 0;
double T_delta = 0.01;

for(int i = 0; i < 20; i++){
T += T_delta;
printf("%f %f %f %f %f %f %f %.14f\n",
T,conduct(1.0,T),conduct(2.0,T),
conduct(3.0,T),conduct(4.0,T),
conduct(5.0,T),conduct(6.0,T),
conduct(7.0,T));
}
return 0;
}

I'm calculating the conductivity (for a given model) with 7 different energies over a range of Temperature. Each calculation is independent of the others. I have to say that each one of this calculations requires a lot of cycles but not a lot of memory. This code is not parallel at all and having a core duo processor limits it.


Here is the CPU activity before and after running such program. Notice that one of the cores tops while running the core, but the other doesn't get that much work to do.

First we need to modify our main method to accept parameters. we will give it 3 parameters in this order initial T, delta T, and number of points to calculate.

Our main() method will look like this now.

int main(int argc, char * const argv[]){
if(argc != 4){
printf("Wrong number of arguments.
Use initial_T delta number of points\n");
return 0;
}

double T = atof(argv[1]);
double T_delta = atof(argv[2]);
int N = atoi(argv[3]);

for(int i = 0; i < N; i++){
T += T_delta;
printf("%f %f %f %f %f %f %f %.14f\n",
T,conduct(1.0,T),conduct(2.0,T),
conduct(3.0,T),conduct(4.0,T),
conduct(5.0,T),conduct(6.0,T),
conduct(7.0,T));
}
return 0;
}

To get the same answer as to the other main method we just need to run this method twice with the appropriate parameters. We use the following script to do just that. Just open your favorite text editor and write the following

./run 0 .01 10 >> out &
./run .1 .01 10 >> out &

after saving it (lets say you name it brun) you can use the chmod command on terminal to make it an executable.

chmod 555 brun

running ./brun will produce the same results using more processor resources. In theory this should reduce the computing time by half. In this example it took 12 min to run ./brun vs 20 min it took before splitting the load of work between the two cores.


This tutorial assumes no files are modified during the program execution and the output is printed into screen using the printf command. If not the case some workaround can be found by introducing more program parameters, like input and output files.

I hope this tutorial will be usefull Enjoy.

Tuesday, October 2, 2007

Apple Wireless Keyboard

I run (literally) to the apple store this morning to get the new apple keyboard. I'm posting some pictures of it. So far I really like this keyboard. Long time ago one of my friends ask me if I could find a smaller keyboard. Now I have a smaller keyboard, enjoy.