Distributing nested loops

Distributing nested loops

Postby kari » 2013-12-05 14:07:05

If you have C code with nested loops and you want to distribute the innermost part to workers, here is an example on how to do that.

For example if you have the following loop structure locally:
Code: Select all
  for (i = 0; i < loopsi; i++) {
    for (j = 0; j < loopsj; j++) {
      result = i + j;
      myarray[i][j] = result;
    }
  }


On the workers you only get the jobidx parameter which goes from 1 to number of jobs. If in the above case both loopsi and loopj would be 5, the number of jobs will be 25. Therefore you need to recompute the values of i and j on the worker code. In MATLAB you could use ind2sub function, but in C you need to do it yourself:

Code: Select all
  int i = (jobidx - 1) % loopsi;
  int j = ((jobidx - 1) / loopsi) % loopsj;


Please see attached codes for full working example.
Attachments
distribution_dist.c
worker code
(809 Bytes) Downloaded 616 times
run_nested_loops.c
Local control code
(4.2 KiB) Downloaded 580 times
kari
Techila Staff
Techila Staff
 
Posts: 48
Joined: 2012-07-18 11:14:09

Return to C/C++

cron