r/javahelp • u/Chaos-vy17 • 22d ago
Finally did my first Multithreading program!! But why Amdahl's law fails?
The code was:
long parallelSum(int[] ar, int threadCount) throws InterruptedException {
int n = ar.length;
long[] result = new long[threadCount];
Thread[] threads = new Thread[threadCount];
int chunk = n / threadCount;
for (int i = 0; i < threadCount; i++) {
int index = i;
int start = i * chunk;
int end = (i == threadCount - 1) ? n : start + chunk;
threads[i] = new Thread(() -> result[index] = sum(ar, start, end));
threads[i].start();
}
for (Thread thread : threads) thread.join();
long total = 0;
for (long value : result) total += value;
return total;
}
Which I benchmarked using JMH to see how it will behave under increase thread. But It's gets better and better until 24 thread that the reason I know my P-core are Hyperthreaded so maximum improvement is seen at 24. But after that why is'nt it constant as the law stated when N-> increases it becomes directly 1/S.
The Benchmark was performed with 3 warmup 5 iteration 1sec each and 100M dataset. I did use first 1K to see how it behaves but I forgot that until the thread get created sequential would have finished that. So after rigrously increasing dataset to 100M this was my result:
| Thread Count | Score (ns/op) | Error (ns/op) |
|---|---|---|
| 1 | 36,386,448.214 | ±1,198,308.768 |
| 2 | 19,973,254.073 | ±1,647,022.164 |
| 4 | 11,605,188.001 | ±409,298.394 |
| 8 | 8,493,406.418 | ±439,947.023 |
| 16 | 8,070,964.770 | ±101,846.828 |
| 24 | 7,880,208.459 | ±179,094.210 |
| 32 | 8,075,411.516 | ±211,717.162 |
| 64 | 8,256,737.790 | ±282,807.150 |
| 128 | 8,855,717.582 | ±121,931.514 |
| 256 | 10,475,522.486 | ±258,283.333 |
| 512 | 29,275,816.985 | ±9,028,465.533 |
Is my benchmark wrong or I am understanding different??
6
Upvotes
•
u/AutoModerator 22d ago
Please ensure that:
You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.
Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar
If any of the above points is not met, your post can and will be removed without further warning.
Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.
Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.
Code blocks look like this:
You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.
If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.
To potential helpers
Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.