Ich bin neu in OpenMP und Parallel Programmierung und versuche damit zu experimentieren. Ich habe eine einfache for-Schleife von 30 Elementen und jedes der Elemente wird von einer process()
Funktion verarbeitet. Aber ich habe bewusst ein Element (das 5th
Element) verzögert. Hier ist der Code:Openmp: Alle Threads stoppen wegen Verzögerung in einem Thread
std::mutex mu;
void print_msg(const char* msg, size_t n)
{
mu.lock();
cout << msg << n << endl;
mu.unlock();
}
void process(size_t i)
{
print_msg("Processing ... ", i);
if (i == 5) // the 5th element is big, so takes time
for(int u=0; u<std::numeric_limits<int>::max(); u++);
}
int main(int argc, char *argv[])
{
#pragma omp parallel
{
#pragma omp for ordered schedule(dynamic, 3)
for(size_t i=0; i<30; i++)
{
process(i);
}
}
return 0;
}
Was ich erwartet hatte:
Der Faden, der das 5th
Element zugeordnet wurde (und die anderen zwei Elemente) verzögert erhalten, aber der Rest wird parallel fortgesetzt.
Das Ergebnis:
Hier ist das Ergebnis mit der Verzögerungsposition erwähnt ...
Processing ... 1
Processing ... 0
Processing ... 4
Processing ... 2
Processing ... 5
Processing ... 3
Processing ... 6
Processing ... 7
Processing ... 8
[[ Here the execution paused for a couple of seconds and then
the below printed out all at once ]]
Processing ... 9
Processing ... 11
Processing ... 12
Processing ... 13
Processing ... 10
Processing ... 14
Processing ... 15
Processing ... 16
Processing ... 17
Processing ... 21
Processing ... 20
Processing ... 19
Processing ... 18
Processing ... 22
Processing ... 24
Processing ... 23
Processing ... 26
Processing ... 27
Processing ... 25
Processing ... 29
Processing ... 28
mir So scheint es, dass nicht nur der Faden enthält, gestoppt 5th
Element, aber alle andere Threads stoppten ebenfalls. Ist es ein normales Verhalten?
Dank blockiert werden. Das hat geholfen. :) –