LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Get Thread ID in Parallel For Loop

So if you have a for loop with some value wired to P in the for loop, how do you determine which of the concurrent threads (i.e. a thread id) is running which loop? Is there a native labview way or do I have to go looking around the Windows API?

 

- Chris

0 Kudos
Message 1 of 10
(4,191 Views)

Chris,

 

why is this for interest for you? Since you cannot influence this by any means, i find this a rather useless info even if there is any option (which i doubt).

 

Norbert

Norbert
----------------------------------------------------------------------------------------------------
CEO: What exactly is stopping us from doing this?
Expert: Geometry
Marketing Manager: Just ignore it.
0 Kudos
Message 2 of 10
(4,185 Views)

I want certain threads to use certain resources so those resources are divided evenly across the threads. Otherwise, they will arbitrarily compete for certain resources and the execution will bog down. This is trivial in an OpenMP Parallel For Loop in the for of

 

This is a trivial function in other parallel programming paradigms (i.e. Matlab or OpenMP).

0 Kudos
Message 3 of 10
(4,181 Views)

Hi majoris,

 

so you have a FOR loop which does (by definition) the very same execution in each iteration - and you want to change that behaviour to do different things in each iteration???

 

You could use the loop count ("i") for your decision, but then you get a state machine instead...

 

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 4 of 10
(4,177 Views)

LabVIEW has the advantage, that you do not need to care about this when programming for multiple threads. LV does create, schedule and manage threads for you without the need for personal effort.

Most people see this as an advantage from LV over other languages.

 

What are those "certain ressources"?

 

Norbert

Norbert
----------------------------------------------------------------------------------------------------
CEO: What exactly is stopping us from doing this?
Expert: Geometry
Marketing Manager: Just ignore it.
0 Kudos
Message 5 of 10
(4,175 Views)

Exactly,

 

Just because the for loop uses the same code for every iteration doesn't mean the code can't contain branches inside the executed code (imagine putting a case structure that did something different based on i). Instead, I want to branch off the thread id. To my knowledge, the iteration counter isn't the same as the current thread. But yes, logically what I want to do is

 

parfor i as 0 to n over p processors

 

curr_proc = get_thread_id

 

if curr_proc == 1

  use this resource

if curr_proc == 2

  use this resource

...

 

end parfor

0 Kudos
Message 6 of 10
(4,172 Views)

This is an excellent question and I have asked that in the past, e.g. at the original NI WEEK presentation. I have the same issues with a FORTRAN dll that needs to run in N instances but I have not figured out a way to keep the instances seperate except creating four copies of the dll, all with unique names. (CodeA.dll, CodeB.dll, etc.). If we had access to the parallel instance, I could use a single parallel FOR loop containing a case structure with cases 0...3 and a dll in each.

 

All is not lost, because we can create a small subVI that can generate the parallell instance number as an output when placed in a parallel FOR loop, but there are restrictions. I am busy at the moment, but I'll try to dig up my code later.

 

 

0 Kudos
Message 7 of 10
(4,155 Views)

LabVIEW 2011 provides the information you requested.

 

PTerminalOutputMenu.png

 

PTerminalOutputExample.png

Message 8 of 10
(4,020 Views)

It makes me sad we will not be upgrading to Labview 2011 immediately : \

 

Thanks though!

0 Kudos
Message 9 of 10
(4,013 Views)

Hello majoris,

 

If I follow your problem correctly (enhanced by 's post) I understand you have multiple resources available to you and you would like to utilize these within your for loop that is running in parallel instances.

 

Your problem occurs when you want to use a resources and it is difficult to determine which resource is available for use.  If you use only a single resource then the parallel instances eventually become sequential as the resource can only be accessed at a single time (defeating the purpose of parallel instances).

 

If possible I would use error wires for flow control where possible and if that is not available then a flat sequence structure may be needed. Here is what I would implement:

 

Create an array with a number of boolean indicators as you have resources

Iterate through the array to find the first available resource index and mark it as in use (toogle boolean value)

Use this index to select an appropriate resource

(flow control needed here)

Access the resource then when complete

Mark the index as not in use (toggle boolean value again)

 

Anthony

Anthony F.
Staff Software Engineer
National Instruments
0 Kudos
Message 10 of 10
(3,982 Views)