29
Apr
0

[Openmp] for schedule static or dynamic

Of course there is a difference between static and dynamic scheduling (every one know that) but if you want to see how it can make a difference look at the example above.

 

What we do

In this example, I run a openmp for (with both schedule mode) for each index “i” of the for the thread has to wait “i” seconds. So at position 10 a thread has to wait 10 s.

Static

	const double startTracked = omp_get_wtime();
	#pragma omp parallel
	{
		#pragma omp for schedule(static) nowait
		for(int idx = 0 ; idx < 10 ; ++idx){
			printf("[%2.2lf]I am %d working on %d\n",omp_get_wtime()-startTracked,omp_get_thread_num(),idx);
			const double overTime = omp_get_wtime() + idx;
			while(omp_get_wtime() < overTime);
		}
		printf("[%2.2lf]I am %d it is over for me!\n",omp_get_wtime()-startTracked,omp_get_thread_num());
	}
	const double endTracked = omp_get_wtime();
	printf("Total Time Static  = %lf s\n", (endTracked-startTracked));

As you feel, the first thread will go a lot faster, you’re right.
Output :

[0.00]I am 0 working on 0
[0.00]I am 0 working on 1
[0.00]I am 1 working on 3
[0.00]I am 3 working on 9
[0.00]I am 2 working on 6
[1.00]I am 0 working on 2
[3.00]I am 0 it is over for me!
[3.00]I am 1 working on 4
[6.00]I am 2 working on 7
[7.00]I am 1 working on 5
[9.00]I am 3 it is over for me!
[12.00]I am 1 it is over for me!
[13.00]I am 2 working on 8
[21.00]I am 2 it is over for me!
Total Time Static  = 21.000506 s

Dynamic

	const double startTracked = omp_get_wtime();
	#pragma omp parallel
	{
		#pragma omp for schedule(dynamic) nowait
		for(int idx = 0 ; idx < 10 ; ++idx){
			printf("[%2.2lf]I am %d working on %d\n",omp_get_wtime()-startTracked,omp_get_thread_num(),idx);
			const double overTime = omp_get_wtime() + idx;
			while(omp_get_wtime() < overTime);
		}
		printf("[%2.2lf]I am %d it is over for me!\n",omp_get_wtime()-startTracked,omp_get_thread_num());
	}
	const double endTracked = omp_get_wtime();
	printf("Total Time Dynamic  = %lf s\n", (endTracked-startTracked));

As you feel, the first thread will go a lot faster, you’re right.
Output :

[0.00]I am 0 working on 0
[0.00]I am 3 working on 2
[0.00]I am 2 working on 1
[0.00]I am 0 working on 4
[0.00]I am 1 working on 3
[1.00]I am 2 working on 5
[2.00]I am 3 working on 6
[3.00]I am 1 working on 7
[4.00]I am 0 working on 8
[6.00]I am 2 working on 9
[8.00]I am 3 it is over for me!
[10.00]I am 1 it is over for me!
[12.00]I am 0 it is over for me!
[15.00]I am 2 it is over for me!
Total Time Dynamic  = 15.000454 s

Conclusion

With the dynamic mode the scheduler organized threads at runtime.
For example, the thread 0 did :
in static mode indexes : 0 / 1 / 2
in dynamic mode indexes : 0 / 4 / 8

Enjoyed reading this post?
Subscribe to the RSS feed and have all new posts delivered straight to you.

Comments are closed.

Celadon theme by the Themes Boutique