0

I have a program that minimizes the energy of a system by varying displacement x[i] in x-dimension using a do while condition loop. I want to extend it to a program that minimizes the energy by varying x[i], y[i], z[i], I suppose, in this case three simultaneous do while loops are needed inside one big do while loop, how can I accomplish this?

Code for problem in one dimension is as shown below:

int main(void)
{
  double eta,energyold;
  energyold=1.0;
  unsigned i,j;
  init();

  do{
    function();
    functiond(); //JACOBIAN
    dgesv(fnd,b,N,dx);
    newpsn();
    energyT();
    eta=fabs(energy-energyold)/energy;
    energyold=energy;
    //printf("%lf %lf %lf\n",eta,energy,norm);
  }while(norm>1e-12);

  for(i=0;i<N;i++)
  {
     printf("The  Equillibrium position of ion %d is: %lf\n",i+1,x[i]);
  }
  return 0;
}
unwind
  • 391,730
  • 64
  • 469
  • 606
user36397
  • 53
  • 6
  • 1
    You might want to read about [threads](http://en.wikipedia.org/wiki/Thread_%28computing%29)? – Some programmer dude Feb 04 '14 at 08:02
  • Current versions of C can't do that out of the box, you'd need extensions from your OS or you could try OpenMP, https://en.wikipedia.org/wiki/OpenMP. – Jens Gustedt Feb 04 '14 at 08:06
  • 1
    Also please indent your code properly when posting here, and have something that compiles without warnings: `main` has a return type of `int`. – Jens Gustedt Feb 04 '14 at 08:08

2 Answers2

2

There are many suggestions to use threads, but I'm not convinced that you really need to.

This sounds more like a pure computational problem, where using threads on a multi-core CPU might cut down the total execution time, but isn't necessary in order to express the computation.

Can't you just use three nested loops (if you need to check all combinations of x/y/z values), or do them one at a time? Thread programming can be rather tricky, so it can be really "worth" avoiding if it's not really necessary for a particular problem.

unwind
  • 391,730
  • 64
  • 469
  • 606
  • My thoughts exactly. He doesn't need threads at all to explore 3D space. +1 – JorenHeit Feb 04 '14 at 08:36
  • If you just need to go through every grid-point on a 3D x-y-z grid, you can just nest 3 for-loops: `for (x in x-grid) { for (y in y-grid) { for (z in z-grid) { do_calculation(x,y,z); } } }` As a one-liner this looks messy. Just expand and you'll see :-) – JorenHeit Feb 04 '14 at 10:23
1

You can try using multithreading. Multithreading does not make your do-while loops to run parallely but concurrently.

If you are working on Linux, you can use pthread library which provide functions for spawning a new thread, assigning task scheduling method, mutexes related functions, etc.

Some Useful Links:

0xF1
  • 6,046
  • 2
  • 27
  • 50
  • "parallel" and "concurrent" mean the same thing. You were looking for "sequentially". – Bart Friederichs Feb 04 '14 at 08:12
  • @BartFriederichs That's not how the term is often used, concurrency means they *might* be executed in parallel but there's no guarantee. Processes on a single-core CPU running a multi-tasking OS are concurrent, but of course not parallel; there is only one CPU. See [Wikipedia](http://en.wikipedia.org/wiki/Concurrent_computing). – unwind Feb 04 '14 at 08:13
  • @BartFriederichs : No it is not like that. Please see [this SO question](http://stackoverflow.com/questions/1050222/concurrency-vs-parallelism-what-is-the-difference) – 0xF1 Feb 04 '14 at 08:15