在MPI中运行OpenMP

时间:2019-04-09 19:56:43

标签: c++ mpi openmp

所以这就是我要实现的目标。我有一个工作项列表,节点0需要通过MPI消息传递将其分配给节点1、2和3。

对于节点1、2和3,每个节点中有2个读取器线程,它们将连续ping节点0进行工作,并相应地从节点0接收工作。

现在,如果我在节点0中只有1个线程,即节点0将仅通过MPI_Recv侦听其他节点的请求,并通过MPI_Send发送工作,则一切正常。

但是,如果我还希望节点0具有2个读取器线程,这些读取器线程也从列表中拉取工作(这2个线程是除了处理上述工作分配的主线程之外的线程),我该怎么做?

我尝试使用#pragma omp并行并让主线程(omp_get_thread_num == 0)处理工作分配,而其他2个线程使用#pragma ompcritical来访问列表以拉出工作,但这尝试运行该命令时会导致INTEL终止错误。

谢谢!

如果(pid == 0) {     向量list_file; //要分发的文件名列表

// pre-populate list_file with special file name "LIST_COMPLETED" so that when a node receives this file name it knows that there is no more file to be requested from node 0
for (int i = 0; i < (numP)*num_readers; i++)
{
    list_file.push_back(request_termination_message);
}

// add the file names to list_file
GetInputFile(argv, list_file);                  // argv[1] is a text file that contains the input text files to be processed, all the text file names will be added to list_file
int num_file_remaining = list_file.size();      // number of file remained in the queue to be processed

#pragma omp parallel num_threads(num_readers + 1)
{
    if (omp_get_thread_num == 0)
    {
        MPI_Status request_stats;           // for MPI_recv

        // listen to request for file from other nodes as long as there is file left in list_file
        while (num_file_remaining > 0)
        {
            MPI_Recv(NULL, 0, MPI_INT, MPI_ANY_SOURCE, 0, MPI_COMM_WORLD, &request_stats);      // listen to request for file
            MPI_Send(list_file.back(), 5 * MAX_WORD_LENGTH, MPI_CHAR, request_stats.MPI_SOURCE, request_stats.MPI_SOURCE, MPI_COMM_WORLD);          // send the file to respective node
            list_file.pop_back();       // remove the file that was just sent
            num_file_remaining -= 1;    // reduce the number of file remained in the queue
        }
    }
    else
    {
        char* file_name;

        while (strcmp(file_name, request_termination_message) != 0)
        {
            #pragma omp critical
            {
                file_name = list_file.back();
                list_file.pop_back();
                num_file_remaining -= 1;
            }
        }
    }
}

}

0 个答案:

没有答案