Thursday, October 4, 2007

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.

No comments: