Kuru - 2 months ago 11

Java Question

I found similar questions but not what I want, then.

I need to split an matrix 1024x1024 into 4 parts in equal parts, but I have to spend the beginning and end of every part to work in another class with threads. I thought about using two loops, but I have no idea how to change the value of the variables correctly because it exceeds the maximum.

Code:

`private static final int MAX_MATRIX = 1024;`

private static final int NUM_THREADS = 4;

int start_row = 0, end_row = 0, start_column = 0, end_column = 0;

for (int i = 0; i < NUM_THREADS/2; i++){

end_row = start_row + (MAX_MATRIX/2);

for (int j = 0; j < (NUM_THREADS/2); j++){

end_column = start_column + (MAX_MATRIX/2);

myTask[i] = new TaskAnalyze(start_row, end_row, start_column, end_column, int_radom);

myThreads[i] = new Thread(myTask[i],"TASK " + i);

}

start_column += MAX_MATRIX/2;

start_row += MAX_MATRIX/2;

}

int_random is the matrix.

What should have is:

myTask[0] = new TaskAnalyze(0, 512, 0, 512, int_radom);

myTask[1] = new TaskAnalyze(0, 512, 512, 1024, int_radom);

myTask[2] = new TaskAnalyze(512, 1024, 0, 512, int_radom);

myTask[3] = new TaskAnalyze(512, 1024, 512, 1024, int_radom);

Any suggestions?

Answer

Try this:

```
int MAX_MATRIX = 1024;
int NUM_THREADS = 4;
int start_row = 0, end_row = 0, start_column = 0, end_column = 0;
for (int i = 0; i < NUM_THREADS/2; i++){
start_row = i*MAX_MATRIX/2;
end_row = start_row + MAX_MATRIX/2;
for (int j = 0; j < NUM_THREADS/2; j++){
start_column = j*MAX_MATRIX/2;
end_column = start_column + MAX_MATRIX/2;
System.out.println(start_row + "," + end_row + "," + start_column + "," + end_column);
}
}
```

that should give you the following output:

```
0,512,0,512
0,512,512,1024
512,1024,0,512
512,1024,512,1024
```

Then you can replace the `System.out.println`

line with your lines:

```
myTask[i] = new TaskAnalyze(start_row, end_row, start_column, end_column, int_radom);
myThreads[i] = new Thread(myTask[i],"TASK " + i);
```