有两个任务。 一个通过MPI进行通信,另一个通过一些有趣的事情。 我使用mpi非阻塞调用,以便允许正在运行的线程在等待信息时执行其他任务。 但这不起作用。
一些代码解释。如果您愿意,可以直接跳至代码。 它创建两个过程。 第一个进程将休眠10秒钟,然后通过阻塞调用发送一些数据。 第二个过程(这是您感兴趣的过程)将创建两个具有共享整数(完成)的线程。一个任务将被实例化一次,而另一个任务将被实例化与线程数相同的次数(在本例中为2)。 第一个任务将使用非阻塞调用接收数据,并且应屈服,以便正在运行的线程可以执行另一个任务(但这不会发生)。该任务仅由一个线程运行。 第二项任务只是打印一些很酷的东西。两个线程都实例化了此任务。
代码如下:
if (rank == 0)
{
buffer[1000] = 5;
sleep(10);
printf("Process %d sent data\n", rank);
MPI_Send(buffer, SIZE, MPI_DOUBLE, 1, 5, MPI_COMM_WORLD);
}
else if (rank == 1)
{
#pragma omp parallel num_threads(2) shared (done)
{
#pragma omp single nowait
{
#pragma omp task
{
int flag = 0;
printf("Thread %d is receiving data\n", omp_get_thread_num());
MPI_Irecv(buffer, SIZE, MPI_DOUBLE, 0, 5, MPI_COMM_WORLD, &request);
MPI_Test(&request, &flag, &status);
while (flag == 0)
{
#pragma omp taskyield
MPI_Test(&request, &flag, &status);
printf("Thread %d is wasting time\n", omp_get_thread_num());
sleep(1);
}
done=1;
printf("Thread %d received data\n", omp_get_thread_num());
}
}
#pragma omp task
{
printf("Thread %d entered to the cool task\n", omp_get_thread_num());
while (done == 0)
{
printf("Thread %d is doing some cool stuff\n", omp_get_thread_num());
sleep(1); /* Or not */
}
}
}
}
这是输出:
Thread 0 is receiving data
Thread 1 entered to the cool task
Thread 1 is doing some cool stuff
Thread 0 is wasting time
Thread 1 is doing some cool stuff
Thread 0 is wasting time
Thread 1 is doing some cool stuff
Thread 0 is wasting time
Thread 1 is doing some cool stuff
Thread 0 is wasting time
Thread 1 is doing some cool stuff
Thread 0 is wasting time
Thread 1 is doing some cool stuff
Thread 0 is wasting time
Thread 1 is doing some cool stuff
Thread 0 is wasting time
Thread 1 is doing some cool stuff
Thread 0 is wasting time
Thread 1 is doing some cool stuff
Thread 0 is wasting time
Thread 1 is doing some cool stuff
Thread 0 is wasting time
Process 0 sent data
Thread 1 is doing some cool stuff
Thread 0 is wasting time
Thread 1 is doing some cool stuff
Thread 0 is wasting time
Thread 1 is doing some cool stuff
Thread 0 received data
Thread 0 entered to the cool task
您可以看到,线程0直到完成通信任务后才进入酷任务。
编译命令:
mpicc -fopenmp pruebas.c -o prueba
执行命令:
mpirun -np 2 --allow-run-as-root ./prueba (worry not, read below)
其他信息:
The program is being ran in a docker container with ubuntu.
gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.10)
mpicc --show output: gcc -I/usr/lib/openmpi/include/openmpi/opal/mca/event/libevent2021/libevent -I/usr/lib/openmpi/include/openmpi/opal/mca/event/libevent2021/libevent/include -I/usr/lib/openmpi/include -I/usr/lib/openmpi/include/openmpi -pthread -Wl,-rpath -Wl,/usr/lib/openmpi/lib -Wl,--enable-new-dtags -L/usr/lib/openmpi/lib -lmpi
答案 0 :(得分:2)
OpenMP规范不需要说taskyield
做任何事情
taskyield构造指定可以暂停当前任务 以便执行其他任务。
因此它允许进行重新安排,但不需要重新安排。 (在这一点上,任务调度算法的描述也很清楚)。因此,gcc完全有权利不进行重新安排。
但是,我确实知道LLVM(和Intel)OpenMP运行时确实产生了,并且我们可以看到gcc is generating a call into the runtime,所以有可能仅将LLVM运行时与二进制文件一起使用就足够了。或者,尝试使用clang或the Intel compilers
(完全公开:我为Intel工作:-))