0

I have a function called doWork(int a, int b, int c, int amount) that computes some amount of a total calculation set. In my main I determine how many calculations each function call should do. What I need is to know is how to pass a,b,c so that each call to the function will do it own slice of the calculation.

int nprocs = 2;
int total = 10

public static void main() {
    //total number of calculations to compute
    int totalLoad = ((total*total*total)/6)-(total/6);
    //number of calculations per function call
    int workLoad = totalLoad/nproc;


    for(int i = 0; i < nproc; i++) {
        doWork(a, b, c, workLoad);
    }
}

public void doWork(int i,int j,int k, int amount) {
    boolean start = true;

    for(; i < total && amount != 0; i++) {
        if(!start) {
            j = i+1;
        }
        for(; j < total && amount != 0; j++) {
            if(!start) {
                k = j;
            }
            for(; k < total && amount != 0; k++) {
                if(start) {
                    start = false;
                }

                //some calculation here
                System.out.println(format(i,j,k));
                amount--;
            }
        }
    }

Each function call needs to handle a different sub-section of the total calculation to be made. For example:

    nproc = 2
    totalLoad = 6

    Total work:       Function call 1:
    calc 1            calc 1
    calc 2            calc 2
    calc 3            calc 3
    calc 4
    calc 5            Function call 2:  
    calc 6            calc 4
                      calc 5
                      calc 6

Also, is there a cleaner way to write the doWork() function. It just seems messy to me.

Sagar Zala
  • 3,925
  • 9
  • 27
  • 54
J. Doe
  • 27
  • 1
  • 6
  • not sure what you are asking, but just set the values of `a` `b` and `c` in your `for` loop – Scary Wombat Sep 03 '18 at 06:53
  • That is what I want to know. What do I set `a` `b` and `c` to – J. Doe Sep 03 '18 at 06:56
  • well that depends, are you wanting to know the logic of how to split up a list of `6` into `2` blocks? – Scary Wombat Sep 03 '18 at 06:59
  • BTW you are you aware of integer division? http://stackoverflow.com/questions/4685450/why-is-the-result-of-1-3-0 – Scary Wombat Sep 03 '18 at 07:00
  • @ScaryWombat yes i am aware of integer division and am handling that else where. – J. Doe Sep 03 '18 at 07:01
  • I know how to split a list of 6 into 2 block. I do not know how to a 3D-matrix into 2 separate blocks. And I need to be able to do this with any arbitrary amount of calculations – J. Doe Sep 03 '18 at 07:04
  • If you're not doing threading then why does it matter how you split it up, I don't understand that? "3D-matrix", isn't a matrix 2D by definition? – Joakim Danielson Sep 03 '18 at 07:07
  • I am treading. I just simplified the question. And I am operating over a 3D-matrix. I just didn't show it. – J. Doe Sep 03 '18 at 07:08

0 Answers0