1,105,242 Community Members

Deciding if a piece of code can be parallelized

Member Avatar
CPT
Light Poster
41 posts since Aug 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
1
 

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

Member Avatar
Momerath
Senior Poster
3,824 posts since Aug 2010
Reputation Points: 1,323 [?]
Q&As Helped to Solve: 662 [?]
Skill Endorsements: 19 [?]
Featured
 
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).

Member Avatar
rubberman
Senior Poster
3,989 posts since Mar 2010
Reputation Points: 513 [?]
Q&As Helped to Solve: 500 [?]
Skill Endorsements: 87 [?]
 
0
 

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! :-)

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: