LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-05-2018, 06:51 PM   #1
piobair
Member
 
Registered: Aug 2013
Distribution: Debian, Ubuntu
Posts: 160

Rep: Reputation: Disabled
OpenMP questions


I am new to OpenMP.
According to:
nthreads = omp_get_max_threads();
I have four threads available.

Code:
double z, *x;
x = malloc(sizeof(double)*nthreads);
#pragma omp parallel private(id)
{ for( id=0; id < nthreads; id++) x[id] = function(&z, id);
}
y = x[i];
1. Do I have a problem in that I am addressing z with all four threads?
2. Can I assume that all four threads will have been completed prior to execution of (y = x[i]), or do I need to implement locks of some variety?

Last edited by piobair; 03-05-2018 at 06:57 PM.
 
Old 03-06-2018, 01:49 PM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 3,595

Rep: Reputation: 1101Reputation: 1101Reputation: 1101Reputation: 1101Reputation: 1101Reputation: 1101Reputation: 1101Reputation: 1101Reputation: 1101
What do you expect to happen to variable 'z' when your threads are running parallel?
 
Old 03-06-2018, 02:52 PM   #3
piobair
Member
 
Registered: Aug 2013
Distribution: Debian, Ubuntu
Posts: 160

Original Poster
Rep: Reputation: Disabled
Nothing.
What I submitted is actually a very much simplified version of my program.
In actuality, z is a pointer to the head of a line in a linked list (flat matrix structure which includes metadata).
x[id] is the pointer to the head of a different line in that matrix.
x[id +i] points to a different (independent) line than does x[id]
What I want to do is subtract the values pointed to by z from the values pointed to by x[id] and put the answer in x[id]. All that I am doing from the line pointed to by z is reading the value. However, I am reading from that line by all threads.

Quote:
Originally Posted by NevemTeve View Post
What do you expect to happen to variable 'z' when your threads are running parallel?
 
Old 03-06-2018, 09:03 PM   #4
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 9,078
Blog Entries: 4

Rep: Reputation: 3170Reputation: 3170Reputation: 3170Reputation: 3170Reputation: 3170Reputation: 3170Reputation: 3170Reputation: 3170Reputation: 3170Reputation: 3170Reputation: 3170
Quote:
Originally Posted by piobair View Post
1. Do I have a problem in that I am addressing z with all four threads?
2. Can I assume that all four threads will have been completed prior to execution of (y = x[i]), or do I need to implement locks of some variety?
If the data structure being traversed by the threads is stable, and if each thread its own designated portion to work with and does not put results anywhere else, locks are not necessary. I suggest that there should be a parent (main) thread which launches the child threads and then waits for all of them to complete.
 
Old 03-06-2018, 10:49 PM   #5
piobair
Member
 
Registered: Aug 2013
Distribution: Debian, Ubuntu
Posts: 160

Original Poster
Rep: Reputation: Disabled
Thank you.
I am calling the OpenMP pragma from within a single loop, and I only have one level of child threads. I believe that that satisfies your parent thread concern. Do I need a signal that all threads called by the pragma are complete before that loop is reentered, or will the first line subsequent to the pragma not execute until all threads have completed?

Quote:
Originally Posted by sundialsvcs View Post
If the data structure being traversed by the threads is stable, and if each thread its own designated portion to work with and does not put results anywhere else, locks are not necessary. I suggest that there should be a parent (main) thread which launches the child threads and then waits for all of them to complete.
 
Old 03-07-2018, 01:26 AM   #6
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 3,595

Rep: Reputation: 1101Reputation: 1101Reputation: 1101Reputation: 1101Reputation: 1101Reputation: 1101Reputation: 1101Reputation: 1101Reputation: 1101
(Anyways, don't be surprised if it will be slower than without threads, that's normal.)
 
  


Reply

Tags
openmp


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
C/C++ Hybrid[MPI + OpenMP] Programming in Linux: mpicc -openmp bdboy Programming 0 03-20-2013 03:44 AM
Using lock in OpenMP and Pthreads hjazz6 Programming 2 04-14-2011 06:52 PM
[SOLVED] openMP for ubuntu mahmoodn Linux - Software 4 03-29-2011 04:06 AM
Linking OpenMP Mash Programming 2 07-28-2007 02:44 PM
openmp in suse 10.2 ezhaar Programming 2 02-18-2007 12:37 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:25 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration