We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,612 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Deciding if a piece of code can be parallelized

Hello everyone, I'm having problems grasping some concepts of parallel programming; to be more precise, given some pieces of code, I have to determine which of them can be parallel and which cannot(this was in a test in a parallel programming class):

a) a=2; b=3;                    //yes-variables are independently initialised
b) a=3;b=a;                     //depends on the synchronisation
c) a=3;b=a;c=b;                 //depends on which gets executed first
d) b=a;c=b;a=4;                 //no-because some variables might get init to the new value while other to the old value
e) a=2;b=a+3;a=3;               //no-it's not clear to which value of 'a' the var. 'b' wil get calculated
f) for(i=0;i<100;i++) 
    b[i]=i;                     //yes- variable depends solely on the index
g) for(i=0;i<100;i++) 
    b[i]=f(b[i]);               //yes-variable doesn't depend on another variable
h) for(i=0;i<100;i++)
    for(j=0;j<100;j++)   
        b[i][j]= f(b[i][j-1]);  //no-current variable depends on previous 
i) a=f(x); b=g(x);              //variables get initialised independently
j) for(i=0;i<100;i++) 
    b[i] = f(b[index[i]]);      //same as i)

so my question is how far off am I from the correct answers ?
PS: if you like, you can contribute to the thread by posting questions(test questions, interviews etc.) of this type here as it might also help others

3
Contributors
2
Replies
6 Days
Discussion Span
3 Months Ago
Last Updated
15
Views
CPT
Light Poster
35 posts since Aug 2010
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 0

For lines 10-12 you can parallelize the outer loop, but not the inner. Also it will error on the first iteration as there is no index -1 (j = 0, j-1 = -1).

Momerath
Senior Poster
3,726 posts since Aug 2010
Reputation Points: 1,322
Solved Threads: 624
Skill Endorsements: 12

I think basically got it correct, with Momerath's comments applied. Actually, it may not error on line 12 if -1 is not a valid index, though it should; however, -1 IS a valid array index in theory. If this code was provided as part of the test, you might get extra credit if you point that out! :-)

rubberman
Posting Maven
2,571 posts since Mar 2010
Reputation Points: 365
Solved Threads: 305
Skill Endorsements: 51

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0750 seconds using 2.68MB