将您的源文件复制到项目文件夹。
在我们的示例中,文件是 openmpi_test.cpp 和 openmpi_test.h 。 如果您想将它们用作示例,请复制粘贴以下代码,或者使用您自己的文件:
#include
#include
void print_processors() {
// Initialize the MPI environment
MPI_Init(nullptr, nullptr);
// Get the number of processes
int world_size;
MPI_Comm_size(MPI_COMM_WORLD, &world_size);
// Get the rank of the process
int world_rank;
MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
// Get the name of the processor
char processor_name[MPI_MAX_PROCESSOR_NAME];
int name_len;
MPI_Get_processor_name(processor_name, &name_len);
// Print off a hello world message
printf("Hello world from processor %s, rank %d out of %d processors\n",
processor_name, world_rank, world_size);
// Finalize the MPI environment.
MPI_Finalize();
}
#ifndef OPENMPI_TEST_OPENPMI_HELLO_H
#define OPENMPI_TEST_OPENPMI_HELLO_H
void print_processors();
#endif //OPENMPI_TEST_OPENPMI_HELLO_H
#include "openmpi_test.h"
int main() {
print_processors();
return 0;
}
此时,新文件尚未添加到 CMake 项目结构中,OpenMPI 代码无法被 IDE 识别: