Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
1.44/5 (3 votes)
See more:
Hi,

I have an array of 1000 Numbers, Now i want to use TPL (Task Parallel Programming), to write the square of each number from that array in a single note pad file.
(Note that in my project it has the same kind of scenario, where I can't explain as it needs lot of functionality to explain).

can any one help me in doing the same kind of stuff using only TPL concept.
Posted
Comments
Sergey Alexandrovich Kryukov 5-Aug-15 1:42am    
Any questions?
—SA
Sergey Alexandrovich Kryukov 5-Aug-15 3:27am    
What is that beast, "note pad file"? :-)
—SA
[no name] 5-Aug-15 6:42am    
I assume in your real work the calculation part is some more difficult than calculating the square of a number. That means calculation takes significant time.
I had to solve a similar thing. My approach was:
a.) 1 Thread to prepare the calculation tasks and add them to a queue (if no free thread is available)
b.) N Worker threads, which calculate and add the result to a result queue
c.) 1 Thread which does process (save) the results
Depending on the (complexity) time needed to calculate vs. time needed to save the results you can observe funny things.
Saikrishna35 6-Aug-15 3:37am    
Hi,
I hope this will work fine, but Can you please provide a sample code for this approach...
Thanks in advance

Your problem is sequential in nature, and not suited to parallel.

The only way is to transform the problem in 2 problems:
a first problem that can be split in many parallel problems
and a second that gather the results of the first one and write them in a sequential manner.

Update: I don't do threading, but I know how to.
Principle 1:
- create an array of size 1000 to hold the values of squares.
- split the computing of squares between threads.
- Wait until all threads are finish.
- Scan sequentialy the array to write the squares to text file.

You can also convert the squares to strings while in threads to minimize the workload of sequential writing.
 
Share this answer
 
v3
Comments
Saikrishna35 6-Aug-15 3:35am    
Can you please provide a sample code for this approach...ppolymorphe

Don't.
The example you give is not a good candidate for parallel processing: the output needs to be in order in a single file: when you start multi threading 1000 operations, you can't control the order, and each one has to "wait it's turn" to access the file - or all bar one of those running at the same time will find the file in use and fail to write.
In addition, remember that each thread needs an available core to actually run - so unless you have 1000 cores available, multithreading a task like this will make the whole operation take longer than it would in a single thread, as the task setup and switching overhead will outweigh the actual processing by orders of magnitude!


I appreciate that this isn't actually what you are trying to do in your project, but many of the same problems are going to occur if your "real world" task is in any way similar (as it should be to be a useful description here).


I'd stop and think about what you are doing and why you need to parallelise it before you go any further!

 
Share this answer
 
Comments
Saikrishna35 5-Aug-15 2:19am    
Hi Griff,
Thanks for suggestion. But I am sure my project will suitable with the TPL for this task, can you please help me in getting the code part of this scenario.

Thanks.
Sergey Alexandrovich Kryukov 5-Aug-15 3:18am    
It is only suitable if your goal is the demonstration of abuse. :-)
Please see my post: Unhappy Inquirer or Is the Abuse the Main Purpose of Programming?.
—SA
Sergey Alexandrovich Kryukov 5-Aug-15 3:25am    
Only part of your arguments are correct. You probably assume that TPL will create as many threads as the number of iterations in the parallel loop. This is not how TPL works. It will probably create some more or less reasonable number of threads, so many iterations will go in the same threads. TPL actually abstract out parallelism from threads.

But of course, TPL can create arbitrary order of records in the text. But main thing is that the whole idea to write all data in one stream will totally defeat the purpose of parallel calculation. The threads, no matter how many of them, will be synchronized at the access to this shared resource, so in terms of parallelism, the whole threads will do almost all of its work in sequential order. I say "almost", because some tiny fraction of time can be spent in parallel, at the very fast calculation of the square. :-)

—SA

Please see my comment to Solution 1. The whole idea of calculating square values of the numbers in an array makes little sense, but what really totally defeats the purpose of parallelism is writing all data in a single file. The file stream is the shared resource. This is a shared resource to be accessed by all of the threads created by TPL. It will be functionally equivalent to sequential operation, even though some of the calculations of the squares could go in parallel, but this calculation will be extremely fast compared to the rest of the task.


Moreover, memory and CPU usage overhead of TPL and synchronization may lead to average throughput of your parallel version of the calculation considerably lower, compared with simple sequential loop. Do you want to try it and see? :-)


—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900