473,326 Members | 2,732 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

threading using for..next

Hi,
I have some intensive number crunching code that I want to break up
into threads so PCs with several processors can do the job quicker.

I have a loop that will go from say 1 to 10, and for each value,
different numbers will be crunched depending on what the value is.

As it is an intensive process the user may cancel the loop. I need to
know at what value of the loop the process was cancelled, so it can be
restarted at the next value next time.

My initial thought was to allocate 1-5 to thread 1 and 6-10 to thread
2 (for a 2 processor machine) - which shouldn't be too difficult. But
the issue with this if that if the user cancels then its not so neat
to resume as 1 and 6 might be the only ones completed.

Ideally I would like to do the following but am not quite sure how.
Any pointers or code snippets would be appreciated.

for 1 = 1 to 10 step numProcessors (say 2)

next i
Jun 27 '08 #1
6 1024
On 11 May, 07:04, baldrick <philbrier...@hotmail.comwrote:
Hi,
I have some intensive number crunching code that I want to break up
into threads so PCs with several processors can do the job quicker.

I have a loop that will go from say 1 to 10, and for each value,
different numbers will be crunched depending on what the value is.

As it is an intensive process the user may cancel the loop. I need to
know at what value of the loop the process was cancelled, so it can be
restarted at the next value next time.

My initial thought was to allocate 1-5 to thread 1 and 6-10 to thread
2 (for a 2 processor machine) - which shouldn't be too difficult. But
the issue with this if that if the user cancels then its not so neat
to resume as 1 and 6 might be the only ones completed.

Ideally I would like to do the following but am not quite sure how.
Any pointers or code snippets would be appreciated.

for i = 1 to 10 step numThreads (say 2)

allocate i to the next free thread

next i

This way as I loop through 1 to 10, and the user cancels at x, then I
know I have completed 1 through x and need to resume at x+1.

How do I set up the loop to wait for one of the 2 threads to finish
and then allocate the current unprocessed 'i' to that thread?

Hope this makes sense?

Pb

Jun 27 '08 #2
that doesn't require threading. you can use a delegate to stop a loop, and
start it again from sT (startThread)

for i = sT to eT

--
David Glienna
MVP - Visual Developer (Visual Basic)
2006 thru 2008
"baldrick" <ph**********@hotmail.comwrote in message
news:5e**********************************@f36g2000 hsa.googlegroups.com...
On 11 May, 07:04, baldrick <philbrier...@hotmail.comwrote:
>Hi,
I have some intensive number crunching code that I want to break up
into threads so PCs with several processors can do the job quicker.

I have a loop that will go from say 1 to 10, and for each value,
different numbers will be crunched depending on what the value is.

As it is an intensive process the user may cancel the loop. I need to
know at what value of the loop the process was cancelled, so it can be
restarted at the next value next time.

My initial thought was to allocate 1-5 to thread 1 and 6-10 to thread
2 (for a 2 processor machine) - which shouldn't be too difficult. But
the issue with this if that if the user cancels then its not so neat
to resume as 1 and 6 might be the only ones completed.

Ideally I would like to do the following but am not quite sure how.
Any pointers or code snippets would be appreciated.


for i = 1 to 10 step numThreads (say 2)

allocate i to the next free thread

next i

This way as I loop through 1 to 10, and the user cancels at x, then I
know I have completed 1 through x and need to resume at x+1.

How do I set up the loop to wait for one of the 2 threads to finish
and then allocate the current unprocessed 'i' to that thread?

Hope this makes sense?

Pb

Jun 27 '08 #3

"baldrick"
I have some intensive number crunching code that I want to break up
into threads so PCs with several processors can do the job quicker.
It is exactly as you write here, it "can" do the job quicker with threading,
but in the same case it can do the job "slower".

Threading uses managment, so be aware that you are not only because of
marketing information are doing things that has a reverse result than you
think. Threading can be greath as you are doing several jobs which have wait
times like retrieving data from internet.

Have a look simple at the TaskManager and see how many threads are already
running and how they perform on your 2 processors. As it is a function for
your own organisation, and you are allowed to set threadpriority for all
threads to the highest, then maybe you can get some quicker througput time.
Your total processor time will however always be more.

As you persist on using threads in this case, have then a look at the que
class as with that and locking the enque en deque operations you can in a
simple way create a smooth operation.

Cor

Jun 27 '08 #4
"Armin Zingler" <az*******@freenet.deschrieb
However, I don't know if additional work items will have to be
processed later, that means after they some been enqueued in step 1.
correction:
....after they have been enqueued...
....after some have been...

Choose one.
AZ
Jun 27 '08 #5
baldrick,
In addition to the other comments, you may want to review PLINQ.

http://blogs.msdn.com/pfxteam/archiv...Q/default.aspx

PLINQ will take care of spreading your computations across multiple threads,
more importantly across multiple cores for you.

--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"baldrick" <ph**********@hotmail.comwrote in message
news:e5**********************************@s50g2000 hsb.googlegroups.com...
Hi,
I have some intensive number crunching code that I want to break up
into threads so PCs with several processors can do the job quicker.

I have a loop that will go from say 1 to 10, and for each value,
different numbers will be crunched depending on what the value is.

As it is an intensive process the user may cancel the loop. I need to
know at what value of the loop the process was cancelled, so it can be
restarted at the next value next time.

My initial thought was to allocate 1-5 to thread 1 and 6-10 to thread
2 (for a 2 processor machine) - which shouldn't be too difficult. But
the issue with this if that if the user cancels then its not so neat
to resume as 1 and 6 might be the only ones completed.

Ideally I would like to do the following but am not quite sure how.
Any pointers or code snippets would be appreciated.

for 1 = 1 to 10 step numProcessors (say 2)

next i
Jun 27 '08 #6
I forgot to give you this link.

http://msdn.microsoft.com/en-us/libr...ck(VS.85).aspx

Cor

"Cor Ligthert[MVP]" <no************@planet.nlschreef in bericht
news:D6**********************************@microsof t.com...
>
"baldrick"
>I have some intensive number crunching code that I want to break up
into threads so PCs with several processors can do the job quicker.
It is exactly as you write here, it "can" do the job quicker with
threading, but in the same case it can do the job "slower".

Threading uses managment, so be aware that you are not only because of
marketing information are doing things that has a reverse result than you
think. Threading can be greath as you are doing several jobs which have
wait times like retrieving data from internet.

Have a look simple at the TaskManager and see how many threads are already
running and how they perform on your 2 processors. As it is a function for
your own organisation, and you are allowed to set threadpriority for all
threads to the highest, then maybe you can get some quicker througput
time. Your total processor time will however always be more.

As you persist on using threads in this case, have then a look at the que
class as with that and locking the enque en deque operations you can in a
simple way create a smooth operation.

Cor
Jun 27 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: Jane Austine | last post by:
As far as I know python's threading module models after Java's. However, I can't find something equivalent to Java's interrupt and isInterrupted methods, along with InterruptedException....
17
by: Andrae Muys | last post by:
Found myself needing serialised access to a shared generator from multiple threads. Came up with the following def serialise(gen): lock = threading.Lock() while 1: lock.acquire() try: next...
2
by: W.G. Rowland | last post by:
In some ways I'm starting to miss the simpler days of VB5.. Anyway, here's my problem. I'm trying to build a MSDE client using an MDI Parent/Child interface. Enough of the tasks the program...
2
by: Tyson Ackland | last post by:
I have written a very simple threading proggie in an attempt to teach myself threading. I have seen it referred to in articles that Forms are not thread safe. My form has two labels which are...
16
by: One Handed Man \( OHM - Terry Burns \) | last post by:
Sorry if this gets duplicated, but I posted it and cant see it for a long time so repost. . . I have an application which is writing to a graphics object, the main UI thread and a worker thread...
17
by: One Handed Man \( OHM - Terry Burns \) | last post by:
Assumes a Form with a Panel on it., Does the Mutex have to be within the address of a thread start address ? Cheers - OHM '----------- *************** ---------------- Private...
4
by: DBC User | last post by:
I have a background process which reads a table to see if there are any pending requests. If there are any, then it will start a worker thread (only 10 allowed at a time) and executes a method. In...
5
by: Miro | last post by:
I will try my best to ask this question correctly. I think in the end the code will make more sence of what I am trying to accomplish. I am just not sure of what to search for on the net. I...
10
by: Janto Dreijer | last post by:
I have been having problems with the Python 2.4 and 2.5 interpreters on both Linux and Windows crashing on me. Unfortunately it's rather complex code and difficult to pin down the source. So...
14
by: Akihiro KAYAMA | last post by:
Hi all. I found cooperative multi-threading(only one thread runs at once, explicit thread switching) is useful for writing some simulators. With it, I'm able to be free from annoying mutual...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.