# Writing parallel code for molecular computation [duplicate]

I recently moved towards computational biophysics from an experimental science background. As of now, I am learning the fundamentals and doing some basic monte carlo simulations of LJ fluid on my desktop PC. However, when I tried to run my code on the cluster, it was equally or more slower than my desktop. Later, my lab mates told me to write my code suitable for parallel computation. I have no idea how to do that. Is there any resource, book aimed for people who are not very proficient in computer science, that can teach them about basics of such programming and necessary tools. I wrote my code on c++, I am willing to move to fortran as well.

Having said that, a simple Lennard-Jones Monte Carlo program may not be a bad project to attempt, if you decide to go ahead and learn about parallel programming. The OpenMP approach is relatively easy to understand and implement. Many compilers have a -fopenmp flag, which will switch on the processing of any OpenMP pragmas in the code. A typical pragma is one that is applied to a for loop (or in Fortran, a do loop), to divide the iterations amongst the parallel threads. As an example, if you wanted to sum up $$N$$ numbers, and you have $$P$$ threads available, each thread could sum up (approximately) $$N/P$$ of the numbers, in parallel, and then the results can be combined at the end. You would need to learn some basic OpenMP concepts first: the difference between private and shared variables, and crucially the idea of a reduction variable, which would hold the result of the summation. But it doesn't take too long to master OpenMP at this level.