Click here to Skip to main content
15,895,084 members
Home / Discussions / C#
   

C#

 
AnswerRe: i wait so much because of loops Pin
Luc Pattyn3-May-10 3:49
sitebuilderLuc Pattyn3-May-10 3:49 
AnswerRe: i wait so much because of loops Pin
Michel Godfroid3-May-10 4:04
Michel Godfroid3-May-10 4:04 
GeneralRe: i wait so much because of loops PinPopular
Luc Pattyn3-May-10 4:09
sitebuilderLuc Pattyn3-May-10 4:09 
AnswerRe: i wait so much because of loops [modified] Pin
William Winner3-May-10 8:15
William Winner3-May-10 8:15 
AnswerRe: i wait so much because of loops Pin
Pete O'Hanlon3-May-10 8:24
mvePete O'Hanlon3-May-10 8:24 
GeneralRe: i wait so much because of loops Pin
harold aptroot3-May-10 8:43
harold aptroot3-May-10 8:43 
GeneralRe: i wait so much because of loops Pin
Pete O'Hanlon3-May-10 8:56
mvePete O'Hanlon3-May-10 8:56 
GeneralRe: i wait so much because of loops [modified] Pin
Luc Pattyn3-May-10 9:28
sitebuilderLuc Pattyn3-May-10 9:28 
Hi Harold,

in

for (int k = 0; k < 2000; k++) {
    for (int i = 0; i < 2000; i++) {
        for (int j = 0; j < 2000; j++) {
            G[i, j] = Math.Min(G[i, j], G[i, k] + G[k, j]);
        }
    }
}


each element G[i,j] depends on the elements from the row and column it sits in, so you can execute the iterations of the inner loop in parallel as they all modify a different matrix element, and all read elements that are not going to be written, unless k==i or k==j. You can deal with the former right away (outside the inner loop), and the latter can be handled separately. So it becomes:

for (int k = 0; k < 2000; k++) {
    for (int i = 0; i < 2000; i++) {
        if (i==k) {
             for (int j = 0; j < 2000; j++) {
                G[i, j] = Math.Min(G[i, j], G[i, k] + G[k, j]);
            }
       } else {
            //G[i, j] = Math.Min(G[i, j], G[i, j] + G[j, j]);  // special case k==j
            G[i, k] = Math.Min(G[i, k], G[i, k] + G[k, k]);  // special case k==j
            for (int j = 0; j < 2000; j++) {
                if (j!=k) G[i, j] = Math.Min(G[i, j], G[i, k] + G[k, j]);
            }
        }
    }
}


And now the highlighted loop has no data dependencies any more.

Furthermore, the diagonal elements start at value zero; assuming no negative cycles in the graph, their MIN operation won't affect them, and the special cases do not need to be set aside, so we are back at the original code.

If negative cycles are allowed, proper handling still allows parallellization with negative cycle detection (which turns diagonal elements into negative values), and even then the special cases are not really necessary as the presence of negative cycles makes the results not optimal as the optimum would be minus infinity anyway.

[EDIT]
fixed a mistake, signaled by Pete
[\EDIT]

Smile | :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]

Prolific encyclopedia fixture proof-reader browser patron addict?
We all depend on the beast below.


modified on Monday, May 3, 2010 5:27 PM

GeneralRe: i wait so much because of loops Pin
harold aptroot3-May-10 10:14
harold aptroot3-May-10 10:14 
GeneralRe: i wait so much because of loops Pin
Luc Pattyn3-May-10 10:16
sitebuilderLuc Pattyn3-May-10 10:16 
GeneralRe: i wait so much because of loops Pin
harold aptroot3-May-10 10:31
harold aptroot3-May-10 10:31 
GeneralRe: i wait so much because of loops Pin
Luc Pattyn3-May-10 10:36
sitebuilderLuc Pattyn3-May-10 10:36 
GeneralRe: i wait so much because of loops Pin
harold aptroot3-May-10 10:42
harold aptroot3-May-10 10:42 
GeneralRe: i wait so much because of loops Pin
Luc Pattyn3-May-10 10:56
sitebuilderLuc Pattyn3-May-10 10:56 
AnswerRe: i wait so much because of loops Pin
Pete O'Hanlon3-May-10 10:47
mvePete O'Hanlon3-May-10 10:47 
GeneralRe: i wait so much because of loops Pin
Luc Pattyn3-May-10 11:05
sitebuilderLuc Pattyn3-May-10 11:05 
GeneralRe: i wait so much because of loops Pin
Pete O'Hanlon3-May-10 11:16
mvePete O'Hanlon3-May-10 11:16 
GeneralRe: i wait so much because of loops Pin
Luc Pattyn3-May-10 11:26
sitebuilderLuc Pattyn3-May-10 11:26 
GeneralRe: i wait so much because of loops Pin
Pete O'Hanlon3-May-10 11:23
mvePete O'Hanlon3-May-10 11:23 
GeneralRe: i wait so much because of loops Pin
Luc Pattyn3-May-10 11:27
sitebuilderLuc Pattyn3-May-10 11:27 
GeneralRe: i wait so much because of loops Pin
karayel_kara4-May-10 3:52
karayel_kara4-May-10 3:52 
GeneralRe: i wait so much because of loops Pin
Luc Pattyn4-May-10 4:18
sitebuilderLuc Pattyn4-May-10 4:18 
QuestionMissing special symbols Pin
Sebastian T Xavier3-May-10 1:29
Sebastian T Xavier3-May-10 1:29 
AnswerRe: Missing special symbols PinPopular
Alan N3-May-10 2:24
Alan N3-May-10 2:24 
QuestionRemove items from the list box in C# windows application Pin
yadlaprasad3-May-10 0:31
yadlaprasad3-May-10 0:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.