Parallel processing for loops and pointer defined outside the loop

  • #1
mertcan
345
6
Hi everyone; hope you are well. I have a small question: As far as I have searched, for example we can make integer variable defined outside the for loop private for multiple threads, but is it also possible to apply this situation for an integer pointer defined outside the for loop? I mean if a integer pointer have defined outside the for loop, which is gonna be processed with multi thread, then can we make that pointer private for threads? (I am using visual studio 2022 community and include <omp> structures)
 
Last edited:
Technology news on Phys.org
  • #2
So you want a variable that is local to a loop accessible outside the loop (and indeed outside the process)? Why do you need one that is simultaneously local and not-local?
 
  • Like
Likes mertcan
  • #3
Not sure what you are asking, but it sounds like you might want a variable with thread local storage class (this one is for C++11, similar construct is available is many other languages). Not sure how that relates to OMP though.
 
  • Like
Likes mertcan
  • #4
Vanadium 50 said:
So you want a variable that is local to a loop accessible outside the loop (and indeed outside the process)? Why do you need one that is simultaneously local and not-local?
Thank you for your kind return, I asked because I try to comprehend the structure behind OMP for my project. Whenever I see, during the privatization for "for loop", an integer or double variable examples take place. I have not see an integer pointer example for privatization. So, is it possible to define a pointer in main function but privatize it just before the parallel processing of for loop?
 
  • #5
I don't understand what you wrote - if you are using an auto-translator, you might want to try a different one.

If your loop is changing the value of a variable, reading the contents of that variable outside the loop is undefined. If the loop is not changing the value of the variable, it should be defined and scoped outside that loop.
 
  • Like
Likes mertcan
  • #6
mertcan said:
Thank you for your kind return, I asked because I try to comprehend the structure behind OMP for my project. Whenever I see, during the privatization for "for loop", an integer or double variable examples take place. I have not see an integer pointer example for privatization. So, is it possible to define a pointer in main function but privatize it just before the parallel processing of for loop?
If you simply declare the pointer as private, each thread will have its own pointer, but that pointer will point to a random place in memory. If you declare it as firstprivate, the address pointed to will be the same for all thread, i.e., each thread will have its own pointer, put all those pointer point to the same location (so that memory location will not be private).
 
  • Like
Likes mertcan
  • #7
Vanadium 50 said:
I don't understand what you wrote - if you are using an auto-translator, you might want to try a different one.

If your loop is changing the value of a variable, reading the contents of that variable outside the loop is undefined. If the loop is not changing the value of the variable, it should be defined and scoped outside that loop.
Thank you for your nice answer.
 
  • #8
DrClaude said:
If you simply declare the pointer as private, each thread will have its own pointer, but that pointer will point to a random place in memory. If you declare it as firstprivate, the address pointed to will be the same for all thread, i.e., each thread will have its own pointer, put all those pointer point to the same location (so that memory location will not be private).
Thank you DrClaude for your explanatory and kind return. Please correct me if I understand wrong: lets say we have created a pointer using "new" outside the parallel for loop, and you say during the privatization (without firstprivate) of that pointer, each thread will create that pointer regarding random place in memory. But pointers are created by "new" function, so the other threads use the "new" function that we have just employed above?
 
  • #9
mertcan said:
Thank you DrClaude for your explanatory and kind return. Please correct me if I understand wrong: lets say we have created a pointer using "new" outside the parallel for loop, and you say during the privatization (without firstprivate) of that pointer, each thread will create that pointer regarding random place in memory. But pointers are created by "new" function, so the other threads use the "new" function that we have just employed above?
Yes, each thread will create a new instance of the pointer.
 
  • Like
Likes mertcan
  • #10
Thank you @DrClaude for your valuable return. May I ask a small question related to for loop during parallelization: Lets say we have a nested for loops consisting of 2 for loops (as you know OpenMP does not allow break or goto statements if the loop is parallelized) so if we just parallelize the outer loop, then we can use "break, goto" statements during the inner loop?
 
  • #11
PF is probably not the best resource for learning OpenMP from scratch, especially without seeing your source code or a description of the problem.

If you want to see what your code is actually doing, as opposed to what you think it is oir should be, print omp_get_thread_n.

If you want efficient code, you should use the OMP "collapse" directive. That will preclude jumping out of loops - which is usually not best practice anyway.
 
  • #12
mertcan said:
Lets say we have a nested for loops consisting of 2 for loops (as you know OpenMP does not allow break or goto statements if the loop is parallelized) so if we just parallelize the outer loop, then we can use "break, goto" statements during the inner loop?
I don't see a problem with breaking out of the inner loop or using a goto if the label it goes to is inside the outer loop. The reason you can't break out of a loop is that it is a linear process (it will stop the loop at a given value of the loop variable) that can't be parallelized.
 

Similar threads

  • Programming and Computer Science
Replies
8
Views
2K
  • Programming and Computer Science
Replies
19
Views
2K
  • Programming and Computer Science
Replies
5
Views
832
  • Programming and Computer Science
Replies
31
Views
2K
  • Programming and Computer Science
Replies
11
Views
1K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
2
Views
11K
  • Programming and Computer Science
2
Replies
50
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
1K
Back
Top