Reputation: 477
I am not sure If this question is proper to ask here. If someone knows a better platform Let me know. or some reading material. I was wondering if It is Okay to use threads to add multiple arrays via threads. Will that result in faster functions. Normally The way I add two arrays(of same length) using for loops. But I think since, each instruction runs sequentially i.e The first element is added first and second elements starts after the first one has been completed. However using threads, My program won't need to wait to add second element after the first elements has been added. This should result in better performance?
PS: I am treating array as vectors. therefore. [1,2,3,4]+[1,3,5,7] = [2,4,8,11]
Upvotes: 0
Views: 51
Reputation: 251
You are correct, this can be done in parallel. It is also a rather trivial task. It is simply a matter of dividing the arrays into equal parts and assign each part to a thread. It is also a suitable task for a GPU using CUDA or OpenCL.
However, this adds complexity and will only give you better performance if your arrays are rather huge. Unless you are sure that this is a performance bottleneck in your program I would not bother doing this in parallel.
Upvotes: 1
Reputation: 1243
Yes. You've just devised introductory parallel
algorithm.
I've just read an example like yours from one CUDA book. :-D
Upvotes: 1
Reputation: 251
What do you mean by adding arrays? As in concatenating them? Something along the lines of: [a, b, c, d] + [e, f, g] = [a, b, c, d, e, f, g]?
If so, then sure, you can do this using multiple threads. However, doing so is only useful if you're talking about very large arrays or a lot of arrays.
There is always a performance overhead related to concurrency.
Upvotes: 1