Fork Join Matrix Multiplication in Java

别来无恙 提交于 2019-11-29 12:07:29

As you already noticed, sequential execution of subtasks that belong to the same quadrant is important for this algorithm. So, you need to implement your own seq() function, for example, as follows, and use it as in original code:

public ForkJoinTask<?> seq(final ForkJoinTask<?> a, final ForkJoinTask<?> b) {
    return adapt(new Runnable() {
        public void run() {
            a.invoke();
            b.invoke();
        }
    });
}

You are accumulating the results in C[cRow + i][cCol + j] += s00; and the like. This is not a thread safe operation so you must synchronize the row or ensure that only one task ever updates the cell. Without this you will see random cells being set incorrectly.

I would check you get the right answer with a concurrency of 1.

BTW: float may not be the best choice here. It has a fairly low number of digits of precision and in heavy matrix operations (which I assume you are doing or there wouldn't be much point using multiple threads) the rounding error can use up most or all your precision. I would suggest considering double instead.

e.g. float has about 7 digits of precision and one rule of thumb is that the error is proportional to the number of calculations. So for a 1K x 1K matrix you might have 4 digits of precision left. For 10K x 10K you might only have three at best. double has 16 digits of precision meaning you may have 12 digits of precision after a 10K x 10K mutlipication.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!